From 0413c5c1c954b899a0c79b06bd648ac8d0ad1cb3 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 14 Aug 2021 17:07:57 -0700 Subject: [PATCH] Move trybuild_no_target logic to where --target is set --- src/cargo.rs | 22 +++++++++++++++++++--- src/lib.rs | 16 ---------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/cargo.rs b/src/cargo.rs index a5f3584..67c8ea3 100644 --- a/src/cargo.rs +++ b/src/cargo.rs @@ -123,8 +123,24 @@ fn features(project: &Project) -> Vec { } fn target() -> Vec<&'static str> { - match crate::TARGET { - Some(target) => vec!["--target", target], - None => vec![], + // 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. + if cfg!(trybuild_no_target) { + vec![] + } else if let Some(target) = crate::TARGET { + vec!["--target", target] + } else { + vec![] } } diff --git a/src/lib.rs b/src/lib.rs index b8a4444..56056f5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -245,22 +245,6 @@ 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;