Skip to content

Commit

Permalink
feat: Auto-detect and define build_type setting
Browse files Browse the repository at this point in the history
Use the environment variables set by Cargo for the build scripts
to detect and define the `build_type` setting for Conan.

Build the example / test crate using both Debug and Release profiles.
  • Loading branch information
ravenexp committed Oct 13, 2023
1 parent ce1c410 commit 965bae0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ jobs:
- name: Run tests
run: cargo test --verbose -- --test-threads=1
- name: Test build the example crates
run: cargo build -p example-build-script -vv
run: |
cargo build -p example-build-script -vv
cargo build -p example-build-script -vv --release
- name: Test run the build script example
run: cargo run -p example-build-script
fmt:
Expand Down
1 change: 1 addition & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ runtests:
script:
- cargo test --verbose -- --test-threads=1
- cargo build -p example-build-script -vv
- cargo build -p example-build-script -vv --release
- cargo run -p example-build-script
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ fn main() {
}
```

The most commonly used `build_type` Conan setting will be defined automatically
depending on the current Cargo build profile: Debug or Release.

The Conan executable is assumed to be named `conan` unless
the `CONAN` environment variable is set to override.

Expand Down
26 changes: 26 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
//! ConanInstall::new().run().parse().emit();
//! ```
//!
//! The most commonly used `build_type` Conan setting will be defined automatically
//! depending on the current Cargo build profile: Debug or Release.
//!
//! The Conan executable is assumed to be named `conan` unless
//! the `CONAN` environment variable is set to override.
//!
Expand Down Expand Up @@ -181,12 +184,35 @@ impl ConanInstall {
command.arg(build);
}

// Use additional environment variables set by Cargo.
Self::add_settings_from_env(&mut command);

let output = command
.output()
.expect("failed to run the Conan executable");

ConanOutput(output)
}

/// Adds automatic Conan settings arguments derived
/// from the environment variables set by Cargo.
///
/// The following Conan settings are auto-detected and set:
///
/// - `build_type`
fn add_settings_from_env(command: &mut Command) {
match std::env::var("PROFILE").as_deref() {
Ok("debug") => {
command.arg("-s");
command.arg("build_type=Debug");
}
Ok("release") => {
command.arg("-s");
command.arg("build_type=Release");
}
_ => (),
}
}
}

impl ConanOutput {
Expand Down

0 comments on commit 965bae0

Please sign in to comment.