Skip to content

Commit f33b7b0

Browse files
committed
Auto merge of #3356 - ibabushkin:feature-build-rustflags, r=alexcrichton
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. :)
2 parents cd38925 + f440704 commit f33b7b0

File tree

3 files changed

+122
-4
lines changed

3 files changed

+122
-4
lines changed

src/cargo/ops/cargo_rustc/context.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -889,15 +889,15 @@ fn env_args(config: &Config,
889889
// Then the target.*.rustflags value
890890
let target = build_config.requested_target.as_ref().unwrap_or(&build_config.host_triple);
891891
let key = format!("target.{}.{}", target, name);
892-
if let Some(args) = config.get_list(&key)? {
893-
let args = args.val.into_iter().map(|a| a.0);
892+
if let Some(args) = config.get_list_or_split_string(&key)? {
893+
let args = args.val.into_iter();
894894
return Ok(args.collect());
895895
}
896896

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

src/cargo/util/config.rs

+30
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,36 @@ impl Config {
225225
}
226226
}
227227

228+
pub fn get_list_or_split_string(&self, key: &str)
229+
-> CargoResult<Option<Value<Vec<String>>>> {
230+
match self.get_env::<String>(key) {
231+
Ok(Some(value)) =>
232+
return Ok(Some(Value {
233+
val: value.val.split(' ').map(str::to_string).collect(),
234+
definition: value.definition
235+
})),
236+
Err(err) => return Err(err),
237+
Ok(None) => (),
238+
}
239+
240+
match self.get(key)? {
241+
Some(CV::List(i, path)) => {
242+
Ok(Some(Value {
243+
val: i.into_iter().map(|(s, _)| s).collect(),
244+
definition: Definition::Path(path),
245+
}))
246+
}
247+
Some(CV::String(i, path)) => {
248+
Ok(Some(Value {
249+
val: i.split(' ').map(str::to_string).collect(),
250+
definition: Definition::Path(path),
251+
}))
252+
}
253+
Some(val) => self.expected("list or string", key, val),
254+
None => Ok(None),
255+
}
256+
}
257+
228258
pub fn get_table(&self, key: &str)
229259
-> CargoResult<Option<Value<HashMap<String, CV>>>> {
230260
match self.get(key)? {

tests/rustflags.rs

+88
Original file line numberDiff line numberDiff line change
@@ -949,3 +949,91 @@ fn target_rustflags_precedence() {
949949
assert_that(p.cargo("bench"),
950950
execs().with_status(101));
951951
}
952+
953+
#[test]
954+
fn target_rustflags_string_and_array_form1() {
955+
let p1 = project("foo")
956+
.file("Cargo.toml", r#"
957+
[package]
958+
name = "foo"
959+
version = "0.0.1"
960+
"#)
961+
.file("src/lib.rs", "")
962+
.file(".cargo/config", r#"
963+
[build]
964+
rustflags = ["--cfg", "foo"]
965+
"#);
966+
p1.build();
967+
968+
assert_that(p1.cargo("build").arg("-v"),
969+
execs().with_status(0).with_stderr("\
970+
[COMPILING] foo v0.0.1 ([..])
971+
[RUNNING] `rustc [..] --cfg foo[..]`
972+
[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
973+
"));
974+
975+
let p2 = project("foo")
976+
.file("Cargo.toml", r#"
977+
[package]
978+
name = "foo"
979+
version = "0.0.1"
980+
"#)
981+
.file("src/lib.rs", "")
982+
.file(".cargo/config", r#"
983+
[build]
984+
rustflags = "--cfg foo"
985+
"#);
986+
p2.build();
987+
988+
assert_that(p2.cargo("build").arg("-v"),
989+
execs().with_status(0).with_stderr("\
990+
[COMPILING] foo v0.0.1 ([..])
991+
[RUNNING] `rustc [..] --cfg foo[..]`
992+
[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
993+
"));
994+
995+
}
996+
997+
#[test]
998+
fn target_rustflags_string_and_array_form2() {
999+
let p1 = project("foo")
1000+
.file("Cargo.toml", r#"
1001+
[package]
1002+
name = "foo"
1003+
version = "0.0.1"
1004+
"#)
1005+
.file(".cargo/config", &format!(r#"
1006+
[target.{}]
1007+
rustflags = ["--cfg", "foo"]
1008+
"#, rustc_host()))
1009+
.file("src/lib.rs", "");
1010+
p1.build();
1011+
1012+
assert_that(p1.cargo("build").arg("-v"),
1013+
execs().with_status(0).with_stderr("\
1014+
[COMPILING] foo v0.0.1 ([..])
1015+
[RUNNING] `rustc [..] --cfg foo[..]`
1016+
[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
1017+
"));
1018+
1019+
let p2 = project("foo")
1020+
.file("Cargo.toml", r#"
1021+
[package]
1022+
name = "foo"
1023+
version = "0.0.1"
1024+
"#)
1025+
.file(".cargo/config", &format!(r#"
1026+
[target.{}]
1027+
rustflags = "--cfg foo"
1028+
"#, rustc_host()))
1029+
.file("src/lib.rs", "");
1030+
p2.build();
1031+
1032+
assert_that(p2.cargo("build").arg("-v"),
1033+
execs().with_status(0).with_stderr("\
1034+
[COMPILING] foo v0.0.1 ([..])
1035+
[RUNNING] `rustc [..] --cfg foo[..]`
1036+
[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
1037+
"));
1038+
1039+
}

0 commit comments

Comments
 (0)