From 79924b542a16e428398eff038e225a8d5aeab47d Mon Sep 17 00:00:00 2001 From: Inokentiy Babushkin Date: Thu, 1 Dec 2016 19:57:05 +0100 Subject: [PATCH 1/4] Implemented string lookup for `build.rustflags` config key --- src/cargo/ops/cargo_rustc/context.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index d95b7ee75c6..6be2bef024d 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -866,6 +866,8 @@ fn env_args(config: &Config, if let Some(args) = config.get_list(&key)? { let args = args.val.into_iter().map(|a| a.0); return Ok(args.collect()); + } else if let Some(arg) = config.get_string(&key)? { + return Ok(arg.val.split(' ').map(str::to_string).collect()); } Ok(Vec::new()) From 64530b706ec476cb097f7636bf58a0c5f7b4747f Mon Sep 17 00:00:00 2001 From: Inokentiy Babushkin Date: Fri, 2 Dec 2016 00:03:49 +0100 Subject: [PATCH 2/4] Improved error handling to reflect the actual situation, added tests * One of the tests still doesn't pass, this needs further investigation --- src/cargo/ops/cargo_rustc/context.rs | 24 +++++++- tests/rustflags.rs | 86 ++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 3 deletions(-) diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index 6be2bef024d..2ec275e71b3 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -856,19 +856,37 @@ 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 list = config.get_list(&key); + if let Ok(Some(args)) = list { let args = args.val.into_iter().map(|a| a.0); return Ok(args.collect()); } + let string = config.get_string(&key); + if let Ok(Some(arg)) = string { + return Ok(arg.val.split(' ').map(str::to_string).collect()); + } + if list.is_err() && string.is_err() { + if let Some(value) = config.values()?.get(&key) { + return config.expected("list or string", &key, value.clone()); + } + } // Then the build.rustflags value let key = format!("build.{}", name); - if let Some(args) = config.get_list(&key)? { + let list = config.get_list(&key); + if let Ok(Some(args)) = list { let args = args.val.into_iter().map(|a| a.0); return Ok(args.collect()); - } else if let Some(arg) = config.get_string(&key)? { + } + let string = config.get_string(&key); + if let Ok(Some(arg)) = string { return Ok(arg.val.split(' ').map(str::to_string).collect()); } + if list.is_err() && string.is_err() { + if let Some(value) = config.values()?.get(&key) { + return config.expected("list or string", &key, value.clone()); + } + } Ok(Vec::new()) } diff --git a/tests/rustflags.rs b/tests/rustflags.rs index cc9266ac281..1cf9e96cd14 100644 --- a/tests/rustflags.rs +++ b/tests/rustflags.rs @@ -949,3 +949,89 @@ 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", &format!(r#" + [package] + name = "foo" + version = "0.0.1" + + [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", &format!(r#" + [package] + name = "foo" + version = "0.0.1" + + [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 [..] +")); + +} From 4eda23f7c0a6a3bfe585924b836de8ae0a99f8cf Mon Sep 17 00:00:00 2001 From: Inokentiy Babushkin Date: Fri, 2 Dec 2016 16:00:58 +0100 Subject: [PATCH 3/4] Fixed broken testcase for string-based form of rustflags config key. --- tests/rustflags.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/rustflags.rs b/tests/rustflags.rs index 1cf9e96cd14..f5117c31379 100644 --- a/tests/rustflags.rs +++ b/tests/rustflags.rs @@ -997,11 +997,12 @@ fn target_rustflags_string_and_array_form1() { #[test] fn target_rustflags_string_and_array_form2() { let p1 = project("foo") - .file("Cargo.toml", &format!(r#" + .file("Cargo.toml", r#" [package] name = "foo" version = "0.0.1" - + "#) + .file(".cargo/config", &format!(r#" [target.{}] rustflags = ["--cfg", "foo"] "#, rustc_host())) @@ -1016,11 +1017,12 @@ fn target_rustflags_string_and_array_form2() { ")); let p2 = project("foo") - .file("Cargo.toml", &format!(r#" + .file("Cargo.toml", r#" [package] name = "foo" version = "0.0.1" - + "#) + .file(".cargo/config", &format!(r#" [target.{}] rustflags = "--cfg foo" "#, rustc_host())) From f440704e50e31e45d826654de20046dc6c746fa1 Mon Sep 17 00:00:00 2001 From: Inokentiy Babushkin Date: Fri, 2 Dec 2016 19:43:37 +0100 Subject: [PATCH 4/4] Refactored ugly rustflags lookup code into a separate function. --- src/cargo/ops/cargo_rustc/context.rs | 28 ++++---------------------- src/cargo/util/config.rs | 30 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index 2ec275e71b3..9cb4af91082 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -856,37 +856,17 @@ 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); - let list = config.get_list(&key); - if let Ok(Some(args)) = list { - 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()); } - let string = config.get_string(&key); - if let Ok(Some(arg)) = string { - return Ok(arg.val.split(' ').map(str::to_string).collect()); - } - if list.is_err() && string.is_err() { - if let Some(value) = config.values()?.get(&key) { - return config.expected("list or string", &key, value.clone()); - } - } // Then the build.rustflags value let key = format!("build.{}", name); - let list = config.get_list(&key); - if let Ok(Some(args)) = list { - 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()); } - let string = config.get_string(&key); - if let Ok(Some(arg)) = string { - return Ok(arg.val.split(' ').map(str::to_string).collect()); - } - if list.is_err() && string.is_err() { - if let Some(value) = config.values()?.get(&key) { - return config.expected("list or string", &key, value.clone()); - } - } Ok(Vec::new()) } diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index dc97acc0aac..598a6dcccbe 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -225,6 +225,36 @@ impl Config { } } + pub fn get_list_or_split_string(&self, key: &str) + -> CargoResult>>> { + match self.get_env::(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>>> { match self.get(key)? {