diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index c035441950d..45d51b9dc58 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -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::(); + 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 { diff --git a/src/doc/native-build.md b/src/doc/native-build.md index b06f9679369..da6dcf56457 100644 --- a/src/doc/native-build.md +++ b/src/doc/native-build.md @@ -84,6 +84,10 @@ commands. profile currently being built. * `PROFILE` - name of the profile currently being built (see [profiles][profile]). +* `CARGO_FEATURE_` - For each activated feature of the package being + built, this environment variable will be present + where `` is the name of the feature uppercased + and having `-` translated to `_`. [profile]: manifest.html#the-[profile.*]-sections diff --git a/tests/test_cargo_compile.rs b/tests/test_cargo_compile.rs index b0ff9d7165d..21caae85202 100644 --- a/tests/test_cargo_compile.rs +++ b/tests/test_cargo_compile.rs @@ -745,6 +745,9 @@ test!(custom_build_env_vars { version = "0.5.0" authors = ["wycats@example.com"] + [features] + foo = [] + [[bin]] name = "foo" "#) @@ -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"); @@ -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 @@ -789,6 +794,9 @@ test!(custom_build_env_vars { authors = ["wycats@example.com"] build = '{}' + [features] + foo = [] + [[bin]] name = "foo" @@ -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 {