Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not pass --target if user is not using --target #121

Merged
merged 1 commit into from
Aug 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,34 @@ use std::path::Path;

fn main() -> io::Result<()> {
let out_dir = env::var_os("OUT_DIR").unwrap();
let target = env::var("TARGET").ok();
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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this shouldn't be a problem because the build script will be rebuilt when RUSTFLAGS is changed, but I may have missed something.

//
// 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 path = Path::new(&out_dir).join("target.rs");
let value = match target {
Some(target) => format!("Some({:?})", target),
Expand Down