From a1f630364dbbca15ea0256372f4a1d8cbeb5a909 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Wed, 11 Aug 2021 00:30:20 +0900 Subject: [PATCH 1/2] Revert "Do not pass --target if user is not using --target" This reverts commit 307c77887dc75c3b7f4e3a295e7a4d15b5d7c004. --- build.rs | 29 +---------------------------- 1 file changed, 1 insertion(+), 28 deletions(-) diff --git a/build.rs b/build.rs index 3eee53e..7fb21d2 100644 --- a/build.rs +++ b/build.rs @@ -5,34 +5,7 @@ use std::path::Path; fn main() -> io::Result<()> { let out_dir = env::var_os("OUT_DIR").unwrap(); - let mut target = env::var("TARGET").ok(); - - // When --target flag is passed, cargo does not pass RUSTFLAGS to rustc when - // building proc-macro and build script even if the host and target triples - // are the same. Therefore, if we always pass --target to cargo, tools such - // as coverage that require RUSTFLAGS do not work for tests run by trybuild. - // - // To avoid that problem, do not pass --target to cargo if we know that it - // has not been passed. - // - // Cargo does not have a way to tell the build script whether --target has - // been passed or not, so we use the following heuristic: - // - // - The host and target triples are the same. - // - And RUSTFLAGS is available when *building* the build script. - // - // Note that the second is when building, not when running. This is due to: - // - // - After rust-lang/cargo#9601, cargo does not pass RUSTFLAGS to the build - // script when running. - // - CARGO_ENCODED_RUSTFLAGS, which was introduced in rust-lang/cargo#9601, - // cannot be used for this purpose because it contains the value of - // RUSTFLAGS even if --target is passed and the host and target triples - // are the same. - if target == env::var("HOST").ok() && option_env!("RUSTFLAGS").is_some() { - target = None; - } - + let target = env::var("TARGET").ok(); let path = Path::new(&out_dir).join("target.rs"); let value = match target { Some(target) => format!(r#"Some("{}")"#, target.escape_debug()), From 48db84d61fde4899b3454b4f10e440f552f5cf7b Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Wed, 11 Aug 2021 02:09:15 +0900 Subject: [PATCH 2/2] Expose cfg to always treat target as host --- build.rs | 2 +- src/lib.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/build.rs b/build.rs index 7fb21d2..247cf1c 100644 --- a/build.rs +++ b/build.rs @@ -11,6 +11,6 @@ fn main() -> io::Result<()> { Some(target) => format!(r#"Some("{}")"#, target.escape_debug()), None => "None".to_owned(), }; - let content = format!("const TARGET: Option<&'static str> = {};", value); + let content = format!("const TARGET: Option<&str> = {};", value); fs::write(path, content) } diff --git a/src/lib.rs b/src/lib.rs index 56056f5..b8a4444 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -245,6 +245,22 @@ mod normalize; mod run; mod rustflags; +// When --target flag is passed, cargo does not pass RUSTFLAGS to rustc when +// building proc-macro and build script even if the host and target triples are +// the same. Therefore, if we always pass --target to cargo, tools such as +// coverage that require RUSTFLAGS do not work for tests run by trybuild. +// +// To avoid that problem, do not pass --target to cargo if we know that it has +// not been passed. +// +// Currently, cargo does not have a way to tell the build script whether +// --target has been passed or not, and there is no heuristic that can handle +// this well. +// +// Therefore, expose a cfg to always treat the target as host. +#[cfg(trybuild_no_target)] +const TARGET: Option<&str> = None; +#[cfg(not(trybuild_no_target))] include!(concat!(env!("OUT_DIR"), "/target.rs")); use std::cell::RefCell;