Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ConanInstall::build_profile() method #11

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
edition = "2021"
Expand Down
36 changes: 34 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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()
Expand All @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion example-build-script/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
73 changes: 67 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
//!
Expand All @@ -58,15 +60,45 @@
//!
//! ConanInstall::new()
//! .profile(&conan_profile)
//! .detect_profile() // Auto-detect if the profile does not exist
//! .build("missing")
//! .verbosity(ConanVerbosity::Error) // Silence Conan warnings
//! .run()
//! .parse()
//! .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:
Expand Down Expand Up @@ -137,8 +169,10 @@ pub struct ConanInstall {
output_folder: Option<PathBuf>,
/// Conan recipe file path
recipe_path: Option<PathBuf>,
/// Conan profile name
/// Conan host profile name
profile: Option<String>,
/// Conan build profile name
build_profile: Option<String>,
/// Conan profile auto-detection flag
new_profile: bool,
/// Conan build policy
Expand Down Expand Up @@ -213,6 +247,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`.
Expand Down Expand Up @@ -256,6 +305,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);
Expand All @@ -269,8 +322,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() {
Expand All @@ -294,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}");
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}