From 68768ddb7986979b205fabc1c7c83c1870fecb23 Mon Sep 17 00:00:00 2001 From: Sergey Kvachonok Date: Wed, 27 Nov 2024 15:48:50 +0300 Subject: [PATCH 1/4] feat: Add `ConanInstall::build_profile()` method Allow setting Conan build profile along with the host profile. Add an alias `ConanInstall::host_profile()` for `ConanInstall::profile()`. Bump the crate version to 0.1.3 (preliminary). Resolves #10 --- Cargo.toml | 2 +- src/lib.rs | 30 +++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 92dea1b..ec1a651 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "conan2" -version = "0.1.2" +version = "0.1.3" description = "Pulls the C/C++ library linking flags from Conan dependencies" authors = ["Sergey Kvachonok "] edition = "2021" diff --git a/src/lib.rs b/src/lib.rs index c4f94f9..0895bf3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -137,8 +137,10 @@ pub struct ConanInstall { output_folder: Option, /// Conan recipe file path recipe_path: Option, - /// Conan profile name + /// Conan host profile name profile: Option, + /// Conan build profile name + build_profile: Option, /// Conan profile auto-detection flag new_profile: bool, /// Conan build policy @@ -213,6 +215,21 @@ impl ConanInstall { self } + /// Sets the Conan host profile name to use for installing dependencies. + /// + /// Matches `--profile:host` Conan executable option. + pub fn host_profile(&mut self, profile: &str) -> &mut ConanInstall { + self.profile(profile) + } + + /// Sets the Conan build profile name to use for installing dependencies. + /// + /// Matches `--profile:build` Conan executable option. + pub fn build_profile(&mut self, profile: &str) -> &mut ConanInstall { + self.build_profile = Some(profile.to_owned()); + self + } + /// Auto-detects and creates the Conan profile to use for installing dependencies. /// /// Schedules `conan profile detect --exist-ok` to run before running `conan install`. @@ -256,6 +273,10 @@ impl ConanInstall { if self.new_profile { Self::run_profile_detect(&conan, self.profile.as_deref()); + + if self.build_profile != self.profile { + Self::run_profile_detect(&conan, self.build_profile.as_deref()); + }; } let mut command = Command::new(conan); @@ -269,8 +290,11 @@ impl ConanInstall { .arg(output_folder); if let Some(profile) = self.profile.as_deref() { - command.arg("--profile"); - command.arg(profile); + command.arg("--profile:host").arg(profile); + } + + if let Some(build_profile) = self.build_profile.as_deref() { + command.arg("--profile:build").arg(build_profile); } if let Some(build) = self.build.as_deref() { From 7882980acad2454a5606819e02cc08124a4fd6ce Mon Sep 17 00:00:00 2001 From: Sergey Kvachonok Date: Wed, 27 Nov 2024 16:27:25 +0300 Subject: [PATCH 2/4] test: Add integration tests for `build_profile()` --- example-build-script/build.rs | 5 ++++- tests/integration_test.rs | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/example-build-script/build.rs b/example-build-script/build.rs index bcc7028..7740970 100644 --- a/example-build-script/build.rs +++ b/example-build-script/build.rs @@ -2,7 +2,10 @@ use conan2::{ConanInstall, ConanVerbosity}; fn main() { ConanInstall::new() - .detect_profile() // Auto-detect "default" profile if not exists + .host_profile("cargo-host") + .build_profile("default") + // Auto-detect "cargo-host" and "default" profiles if none exist + .detect_profile() .build("missing") .verbosity(ConanVerbosity::Error) // Silence Conan warnings .run() diff --git a/tests/integration_test.rs b/tests/integration_test.rs index fc2e705..05fb3fe 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -75,3 +75,18 @@ fn detect_custom_profile() { std::io::stderr().write_all(output.stderr()).unwrap(); assert!(output.is_success()); } + +#[test] +fn host_and_build_profiles() { + let output = ConanInstall::with_recipe(Path::new("tests/conanfile.txt")) + .output_folder(Path::new(env!("CARGO_TARGET_TMPDIR"))) + .host_profile(&format!("{}-dynamic-host-profile", env!("CARGO_PKG_NAME"))) + .build_profile(&format!("{}-dynamic-build-profile", env!("CARGO_PKG_NAME"))) + .detect_profile() + .build("missing") + .verbosity(ConanVerbosity::Debug) + .run(); + + std::io::stderr().write_all(output.stderr()).unwrap(); + assert!(output.is_success()); +} From f371c0d0206dcac17450badc25073c7eedf9764a Mon Sep 17 00:00:00 2001 From: Sergey Kvachonok Date: Wed, 27 Nov 2024 16:36:01 +0300 Subject: [PATCH 3/4] docs: Add examples for `ConanInstall::build_profile()` Restructure the advanced usage sections in the crate docs and project README. --- README.md | 36 ++++++++++++++++++++++++++++++++++-- src/lib.rs | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 012888f..36f2af4 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,8 @@ can also be found in the project repository. ## Advanced usage +### Automatic Conan profile inference from Cargo target + Using custom Conan profiles with names derived from the Cargo target information and a reduced output verbosity level: @@ -64,7 +66,6 @@ fn main() { ConanInstall::new() .profile(&conan_profile) - .detect_profile() // Auto-detect if the profile does not exist .build("missing") .verbosity(ConanVerbosity::Error) // Silence Conan warnings .run() @@ -73,7 +74,38 @@ fn main() { } ``` -## Getting C/C++ include paths from Conan dependencies +### Automatic Conan profile creation + +Creating a custom default Conan profile on the fly with zero configuration: + +```rust +use conan2::{ConanInstall, ConanVerbosity}; + +ConanInstall::new() + .profile("cargo") + .detect_profile() // Run `conan profile detect --exist-ok` for the above + .run() + .parse() + .emit(); +``` + +### Using separate host and build profiles + +Using different values for `--profile:host` and `--profile:build` +arguments of `conan install` command: + +```rust +use conan2::{ConanInstall, ConanVerbosity}; + +ConanInstall::new() + .host_profile("cargo-host") + .build_profile("cargo-build") + .run() + .parse() + .emit(); +``` + +### Getting C/C++ include paths from Conan dependencies To use the list of include paths, do the following after parsing the `conan install` output: diff --git a/src/lib.rs b/src/lib.rs index 0895bf3..0e234c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,6 +46,8 @@ //! //! ## Advanced usage //! +//! ### Automatic Conan profile inference from Cargo target +//! //! Using custom Conan profiles with names derived from the Cargo target information //! and a reduced output verbosity level: //! @@ -58,7 +60,6 @@ //! //! ConanInstall::new() //! .profile(&conan_profile) -//! .detect_profile() // Auto-detect if the profile does not exist //! .build("missing") //! .verbosity(ConanVerbosity::Error) // Silence Conan warnings //! .run() @@ -66,7 +67,38 @@ //! .emit(); //! ``` //! -//! ## Getting C/C++ include paths from Conan dependencies +//! ### Automatic Conan profile creation +//! +//! Creating a custom default Conan profile on the fly with zero configuration: +//! +//! ```no_run +//! use conan2::{ConanInstall, ConanVerbosity}; +//! +//! ConanInstall::new() +//! .profile("cargo") +//! .detect_profile() // Run `conan profile detect --exist-ok` for the above +//! .run() +//! .parse() +//! .emit(); +//! ``` +//! +//! ### Using separate host and build profiles +//! +//! Using different values for `--profile:host` and `--profile:build` +//! arguments of `conan install` command: +//! +//! ```no_run +//! use conan2::{ConanInstall, ConanVerbosity}; +//! +//! ConanInstall::new() +//! .host_profile("cargo-host") +//! .build_profile("cargo-build") +//! .run() +//! .parse() +//! .emit(); +//! ``` +//! +//! ### Getting C/C++ include paths from Conan dependencies //! //! To use the list of include paths, do the following after //! parsing the `conan install` output: From e9adb37a11819d4da806a1cbb6c05894770b9390 Mon Sep 17 00:00:00 2001 From: Sergey Kvachonok Date: Wed, 27 Nov 2024 16:44:11 +0300 Subject: [PATCH 4/4] refactor: Show better errors for `conan profile detect` Silence pedantic Clippy warnings. --- src/lib.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 0e234c3..89d803f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -350,15 +350,20 @@ impl ConanInstall { command.arg("profile").arg("detect").arg("--exist-ok"); if let Some(profile) = profile { + println!("running 'conan profile detect' for profile '{profile}'"); + command.arg("--name").arg(profile); + } else { + println!("running 'conan profile detect' for the default profile"); } let status = command .status() .expect("failed to run the Conan executable"); + #[allow(clippy::manual_assert)] if !status.success() { - panic!("conan profile detect command failed: {}", status); + panic!("'conan profile detect' command failed: {status}"); } }