Skip to content

Commit 27e0224

Browse files
committed
Pass features to native build commands
Closes #97 Closes #601 (this is an equivalent solution for that problem)
1 parent f5f34e8 commit 27e0224

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/cargo/ops/cargo_rustc/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,20 @@ fn compile_custom(pkg: &Package, cmd: &str,
216216
for arg in cmd {
217217
p = p.arg(arg);
218218
}
219+
match cx.resolve.features(pkg.get_package_id()) {
220+
Some(features) => {
221+
for feat in features.iter() {
222+
let feat = feat.as_slice().chars()
223+
.map(|c| c.to_uppercase())
224+
.map(|c| if c == '-' {'_'} else {c})
225+
.collect::<String>();
226+
p = p.env(format!("CARGO_FEATURE_{}", feat).as_slice(), Some("1"));
227+
}
228+
}
229+
None => {}
230+
}
231+
232+
219233
for &(pkg, _) in cx.dep_targets(pkg).iter() {
220234
let name: String = pkg.get_name().chars().map(|c| {
221235
match c {

src/doc/native-build.md

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ commands.
8484
profile currently being built.
8585
* `PROFILE` - name of the profile currently being built (see
8686
[profiles][profile]).
87+
* `CARGO_FEATURE_<name>` - For each activated feature of the package being
88+
built, this environment variable will be present
89+
where `<name>` is the name of the feature uppercased
90+
and having `-` translated to `_`.
8791

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

tests/test_cargo_compile.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,9 @@ test!(custom_build_env_vars {
745745
version = "0.5.0"
746746
authors = ["[email protected]"]
747747
748+
[features]
749+
foo = []
750+
748751
[[bin]]
749752
name = "foo"
750753
"#)
@@ -753,6 +756,7 @@ test!(custom_build_env_vars {
753756
use std::io::fs::PathExtensions;
754757
fn main() {{
755758
let _ncpus = os::getenv("NUM_JOBS").unwrap();
759+
let _feat = os::getenv("CARGO_FEATURE_FOO").unwrap();
756760
let debug = os::getenv("DEBUG").unwrap();
757761
assert_eq!(debug.as_slice(), "true");
758762
@@ -777,7 +781,8 @@ test!(custom_build_env_vars {
777781
}}
778782
"#,
779783
p.root().join("target").join("native").display()));
780-
assert_that(build.cargo_process("build"), execs().with_status(0));
784+
assert_that(build.cargo_process("build").arg("--features").arg("foo"),
785+
execs().with_status(0));
781786

782787

783788
p = p
@@ -789,6 +794,9 @@ test!(custom_build_env_vars {
789794
authors = ["[email protected]"]
790795
build = '{}'
791796
797+
[features]
798+
foo = []
799+
792800
[[bin]]
793801
name = "foo"
794802
@@ -798,7 +806,8 @@ test!(custom_build_env_vars {
798806
.file("src/foo.rs", r#"
799807
fn main() {}
800808
"#);
801-
assert_that(p.cargo_process("build"), execs().with_status(0));
809+
assert_that(p.cargo_process("build").arg("--features").arg("foo"),
810+
execs().with_status(0));
802811
})
803812

804813
test!(crate_version_env_vars {

0 commit comments

Comments
 (0)