From 8abdd964e05e72540513349aa9592c8dddea2300 Mon Sep 17 00:00:00 2001 From: Adam Reichold Date: Wed, 5 Aug 2020 08:48:10 +0200 Subject: [PATCH 1/3] Try to determine if --target was passed to Cargo This checks the output directory for the $CARGO_TARGET_DIR/$TARGET pattern making use of MAIN_SEPARATOR to try to handle Windows. It will also fail if either the path or that pattern are not valid UFT-8, but it will then fall back to the current behavior. --- src/lib.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a478010..d1affb9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -157,6 +157,8 @@ impl AutoCfg { let rustc: PathBuf = rustc.into(); let rustc_version = try!(Version::from_rustc(&rustc)); + let target = env::var_os("TARGET"); + // Sanity check the output directory let dir = dir.into(); let meta = try!(fs::metadata(&dir).map_err(error::from_io)); @@ -170,7 +172,23 @@ impl AutoCfg { // so for now we only apply RUSTFLAGS when cross-compiling an artifact. // // See https://github.com/cuviper/autocfg/pull/10#issuecomment-527575030. - let rustflags = if env::var_os("TARGET") != env::var_os("HOST") { + let dir_contains_target = target + .as_ref() + .and_then(|target| { + dir.to_str().and_then(|dir| { + let mut cargo_target_dir = env::var_os("CARGO_TARGET_DIR") + .map(PathBuf::from) + .unwrap_or_else(|| PathBuf::from("target")); + cargo_target_dir.push(target); + + cargo_target_dir + .to_str() + .map(|cargo_target_dir| dir.contains(&cargo_target_dir)) + }) + }) + .unwrap_or(false); + + let rustflags = if target != env::var_os("HOST") || dir_contains_target { env::var("RUSTFLAGS").ok().map(|rustflags| { // This is meant to match how cargo handles the RUSTFLAG environment // variable. @@ -190,7 +208,7 @@ impl AutoCfg { out_dir: dir, rustc: rustc, rustc_version: rustc_version, - target: env::var_os("TARGET"), + target: target, no_std: false, rustflags: rustflags, }; From 4614739d8e73253225bf5c4ca62b1f629afd4d84 Mon Sep 17 00:00:00 2001 From: Adam Reichold Date: Tue, 18 Aug 2020 09:00:52 +0200 Subject: [PATCH 2/3] Factor out code to detect if dir contains target to make it amenable to unit tests. --- src/lib.rs | 42 +++++++++++++++++++++++++----------------- src/tests.rs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d1affb9..de50135 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -172,23 +172,9 @@ impl AutoCfg { // so for now we only apply RUSTFLAGS when cross-compiling an artifact. // // See https://github.com/cuviper/autocfg/pull/10#issuecomment-527575030. - let dir_contains_target = target - .as_ref() - .and_then(|target| { - dir.to_str().and_then(|dir| { - let mut cargo_target_dir = env::var_os("CARGO_TARGET_DIR") - .map(PathBuf::from) - .unwrap_or_else(|| PathBuf::from("target")); - cargo_target_dir.push(target); - - cargo_target_dir - .to_str() - .map(|cargo_target_dir| dir.contains(&cargo_target_dir)) - }) - }) - .unwrap_or(false); - - let rustflags = if target != env::var_os("HOST") || dir_contains_target { + let rustflags = if target != env::var_os("HOST") + || dir_contains_target(&target, &dir, env::var_os("CARGO_TARGET_DIR")) + { env::var("RUSTFLAGS").ok().map(|rustflags| { // This is meant to match how cargo handles the RUSTFLAG environment // variable. @@ -428,3 +414,25 @@ fn mangle(s: &str) -> String { }) .collect() } + +fn dir_contains_target( + target: &Option, + dir: &PathBuf, + cargo_target_dir: Option, +) -> bool { + target + .as_ref() + .and_then(|target| { + dir.to_str().and_then(|dir| { + let mut cargo_target_dir = cargo_target_dir + .map(PathBuf::from) + .unwrap_or_else(|| PathBuf::from("target")); + cargo_target_dir.push(target); + + cargo_target_dir + .to_str() + .map(|cargo_target_dir| dir.contains(&cargo_target_dir)) + }) + }) + .unwrap_or(false) +} diff --git a/src/tests.rs b/src/tests.rs index 1a8a242..4c67462 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -131,3 +131,39 @@ fn probe_constant() { ac.assert_min(1, 33, ac.probe_constant("{ let x = 1 + 2 + 3; x * x }")); ac.assert_min(1, 39, ac.probe_constant(r#""test".len()"#)); } + +#[test] +fn dir_does_not_contain_target() { + assert!(!super::dir_contains_target( + &Some("x86_64-unknown-linux-gnu".into()), + &"/project/target/debug/build/project-ea75983148559682/out".into(), + None, + )); +} + +#[test] +fn dir_does_contain_target() { + assert!(super::dir_contains_target( + &Some("x86_64-unknown-linux-gnu".into()), + &"/project/target/x86_64-unknown-linux-gnu/debug/build/project-0147aca016480b9d/out".into(), + None, + )); +} + +#[test] +fn dir_does_not_contain_target_with_custom_target_dir() { + assert!(!super::dir_contains_target( + &Some("x86_64-unknown-linux-gnu".into()), + &"/project/custom/debug/build/project-ea75983148559682/out".into(), + Some("custom".into()), + )); +} + +#[test] +fn dir_does_contain_target_with_custom_target_dir() { + assert!(super::dir_contains_target( + &Some("x86_64-unknown-linux-gnu".into()), + &"/project/custom/x86_64-unknown-linux-gnu/debug/build/project-0147aca016480b9d/out".into(), + Some("custom".into()), + )); +} From 0f0e5b6c3ec78ac65514b24487f0ddd66c867f39 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Thu, 20 Aug 2020 17:47:03 -0700 Subject: [PATCH 3/3] Release 1.0.1 --- Cargo.toml | 2 +- README.md | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2f8440b..25fa310 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "autocfg" -version = "1.0.0" +version = "1.0.1" authors = ["Josh Stone "] license = "Apache-2.0 OR MIT" repository = "https://github.com/cuviper/autocfg" diff --git a/README.md b/README.md index 3a9a8fa..3788161 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,9 @@ should only be used when the compiler supports it. ## Release Notes +- 1.0.1 (2020-08-20) + - Apply `RUSTFLAGS` for more `--target` scenarios, by @adamreichold. + - 1.0.0 (2020-01-08) - 🎉 Release 1.0! 🎉 (no breaking changes) - Add `probe_expression` and `emit_expression_cfg` to test arbitrary expressions.