From edd9832959933fea312553665d844379ed77456b Mon Sep 17 00:00:00 2001 From: Andrey Zheleznov Date: Mon, 23 Dec 2024 12:37:49 +0300 Subject: [PATCH] Pass target-cpu and codegen-units as RUSTFLAGS This fixes passing `target-cpu` and `codegen-units` to dependencies or to library if invocated for examples or benches. `args` from `cargo rustc -- args` are passed only to the final compiler instance. `RUSTFLAGS` envvar is useful for passing flags to all compiler instances. --- src/main.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 566f391..838e482 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,8 +50,10 @@ fn spawn_cargo( force_single_cgu: bool, ) -> std::io::Result { use std::ffi::OsStr; + use std::fmt::Write; let mut cmd = std::process::Command::new(cargo_path()); + let mut rust_flags = std::env::var("RUSTFLAGS").unwrap_or_default(); // Cargo flags. cmd.arg("rustc") @@ -118,8 +120,11 @@ fn spawn_cargo( .args(cargo.codegen.iter().flat_map(|c| ["-C", c])) // Next, we care about asm/wasm/llvm-ir/llvm-mac. .args(syntax.emit().iter().flat_map(|s| ["--emit", s])) - .args(syntax.format().iter().flat_map(|s| ["-C", s])) - .args(target_cpu.iter().map(|cpu| format!("-Ctarget-cpu={cpu}"))); + .args(syntax.format().iter().flat_map(|s| ["-C", s])); + + if let Some(cpu) = target_cpu { + write!(rust_flags, " -Ctarget-cpu={cpu}").unwrap(); + } { // None corresponds to disasm @@ -135,6 +140,10 @@ fn spawn_cargo( cmd.arg("-Ccodegen-units=1"); } + // `args` from `cargo rustc -- args` are passed only to the final compiler instance. + // `RUSTFLAGS` envvar is useful for passing flags to all compiler instances. + cmd.env("RUSTFLAGS", rust_flags.trim_start()); + if format.verbosity >= 2 { safeprintln!("Running: {cmd:?}"); }