Skip to content

Commit

Permalink
Auto merge of #3356 - ibabushkin:feature-build-rustflags, r=alexcrichton
Browse files Browse the repository at this point in the history
Implemented string lookup for `build.rustflags` config key

This addresses the immediate issue described in #3052 .
I am, however, unsure about the current state of the deeper issues mentioned in that issue, but if needed, I can take stab at them as well. :)
  • Loading branch information
bors committed Dec 2, 2016
2 parents cd38925 + f440704 commit f33b7b0
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/cargo/ops/cargo_rustc/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,15 +889,15 @@ fn env_args(config: &Config,
// Then the target.*.rustflags value
let target = build_config.requested_target.as_ref().unwrap_or(&build_config.host_triple);
let key = format!("target.{}.{}", target, name);
if let Some(args) = config.get_list(&key)? {
let args = args.val.into_iter().map(|a| a.0);
if let Some(args) = config.get_list_or_split_string(&key)? {
let args = args.val.into_iter();
return Ok(args.collect());
}

// Then the build.rustflags value
let key = format!("build.{}", name);
if let Some(args) = config.get_list(&key)? {
let args = args.val.into_iter().map(|a| a.0);
if let Some(args) = config.get_list_or_split_string(&key)? {
let args = args.val.into_iter();
return Ok(args.collect());
}

Expand Down
30 changes: 30 additions & 0 deletions src/cargo/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,36 @@ impl Config {
}
}

pub fn get_list_or_split_string(&self, key: &str)
-> CargoResult<Option<Value<Vec<String>>>> {
match self.get_env::<String>(key) {
Ok(Some(value)) =>
return Ok(Some(Value {
val: value.val.split(' ').map(str::to_string).collect(),
definition: value.definition
})),
Err(err) => return Err(err),
Ok(None) => (),
}

match self.get(key)? {
Some(CV::List(i, path)) => {
Ok(Some(Value {
val: i.into_iter().map(|(s, _)| s).collect(),
definition: Definition::Path(path),
}))
}
Some(CV::String(i, path)) => {
Ok(Some(Value {
val: i.split(' ').map(str::to_string).collect(),
definition: Definition::Path(path),
}))
}
Some(val) => self.expected("list or string", key, val),
None => Ok(None),
}
}

pub fn get_table(&self, key: &str)
-> CargoResult<Option<Value<HashMap<String, CV>>>> {
match self.get(key)? {
Expand Down
88 changes: 88 additions & 0 deletions tests/rustflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -949,3 +949,91 @@ fn target_rustflags_precedence() {
assert_that(p.cargo("bench"),
execs().with_status(101));
}

#[test]
fn target_rustflags_string_and_array_form1() {
let p1 = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
"#)
.file("src/lib.rs", "")
.file(".cargo/config", r#"
[build]
rustflags = ["--cfg", "foo"]
"#);
p1.build();

assert_that(p1.cargo("build").arg("-v"),
execs().with_status(0).with_stderr("\
[COMPILING] foo v0.0.1 ([..])
[RUNNING] `rustc [..] --cfg foo[..]`
[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
"));

let p2 = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
"#)
.file("src/lib.rs", "")
.file(".cargo/config", r#"
[build]
rustflags = "--cfg foo"
"#);
p2.build();

assert_that(p2.cargo("build").arg("-v"),
execs().with_status(0).with_stderr("\
[COMPILING] foo v0.0.1 ([..])
[RUNNING] `rustc [..] --cfg foo[..]`
[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
"));

}

#[test]
fn target_rustflags_string_and_array_form2() {
let p1 = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
"#)
.file(".cargo/config", &format!(r#"
[target.{}]
rustflags = ["--cfg", "foo"]
"#, rustc_host()))
.file("src/lib.rs", "");
p1.build();

assert_that(p1.cargo("build").arg("-v"),
execs().with_status(0).with_stderr("\
[COMPILING] foo v0.0.1 ([..])
[RUNNING] `rustc [..] --cfg foo[..]`
[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
"));

let p2 = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
"#)
.file(".cargo/config", &format!(r#"
[target.{}]
rustflags = "--cfg foo"
"#, rustc_host()))
.file("src/lib.rs", "");
p2.build();

assert_that(p2.cargo("build").arg("-v"),
execs().with_status(0).with_stderr("\
[COMPILING] foo v0.0.1 ([..])
[RUNNING] `rustc [..] --cfg foo[..]`
[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
"));

}

0 comments on commit f33b7b0

Please sign in to comment.