Skip to content

Commit

Permalink
Pass features to native build commands
Browse files Browse the repository at this point in the history
Closes rust-lang#97
Closes rust-lang#601 (this is an equivalent solution for that problem)
  • Loading branch information
alexcrichton committed Oct 16, 2014
1 parent f5f34e8 commit 27e0224
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/cargo/ops/cargo_rustc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,20 @@ fn compile_custom(pkg: &Package, cmd: &str,
for arg in cmd {
p = p.arg(arg);
}
match cx.resolve.features(pkg.get_package_id()) {
Some(features) => {
for feat in features.iter() {
let feat = feat.as_slice().chars()
.map(|c| c.to_uppercase())
.map(|c| if c == '-' {'_'} else {c})
.collect::<String>();
p = p.env(format!("CARGO_FEATURE_{}", feat).as_slice(), Some("1"));
}
}
None => {}
}


for &(pkg, _) in cx.dep_targets(pkg).iter() {
let name: String = pkg.get_name().chars().map(|c| {
match c {
Expand Down
4 changes: 4 additions & 0 deletions src/doc/native-build.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ commands.
profile currently being built.
* `PROFILE` - name of the profile currently being built (see
[profiles][profile]).
* `CARGO_FEATURE_<name>` - For each activated feature of the package being
built, this environment variable will be present
where `<name>` is the name of the feature uppercased
and having `-` translated to `_`.

[profile]: manifest.html#the-[profile.*]-sections

Expand Down
13 changes: 11 additions & 2 deletions tests/test_cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,9 @@ test!(custom_build_env_vars {
version = "0.5.0"
authors = ["wycats@example.com"]
[features]
foo = []
[[bin]]
name = "foo"
"#)
Expand All @@ -753,6 +756,7 @@ test!(custom_build_env_vars {
use std::io::fs::PathExtensions;
fn main() {{
let _ncpus = os::getenv("NUM_JOBS").unwrap();
let _feat = os::getenv("CARGO_FEATURE_FOO").unwrap();
let debug = os::getenv("DEBUG").unwrap();
assert_eq!(debug.as_slice(), "true");
Expand All @@ -777,7 +781,8 @@ test!(custom_build_env_vars {
}}
"#,
p.root().join("target").join("native").display()));
assert_that(build.cargo_process("build"), execs().with_status(0));
assert_that(build.cargo_process("build").arg("--features").arg("foo"),
execs().with_status(0));


p = p
Expand All @@ -789,6 +794,9 @@ test!(custom_build_env_vars {
authors = ["wycats@example.com"]
build = '{}'
[features]
foo = []
[[bin]]
name = "foo"
Expand All @@ -798,7 +806,8 @@ test!(custom_build_env_vars {
.file("src/foo.rs", r#"
fn main() {}
"#);
assert_that(p.cargo_process("build"), execs().with_status(0));
assert_that(p.cargo_process("build").arg("--features").arg("foo"),
execs().with_status(0));
})

test!(crate_version_env_vars {
Expand Down

2 comments on commit 27e0224

@brson
Copy link

@brson brson commented on 27e0224 Oct 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r+

@alexcrichton
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors: retry

Please sign in to comment.