diff --git a/src/flags.rs b/src/flags.rs index f3830212f..206133556 100644 --- a/src/flags.rs +++ b/src/flags.rs @@ -21,6 +21,7 @@ pub(crate) struct RustcCodegenFlags<'a> { dwarf_version: Option, stack_protector: Option<&'a str>, linker_plugin_lto: Option, + target_cpu: Option<&'a str>, } impl<'this> RustcCodegenFlags<'this> { @@ -174,6 +175,10 @@ impl<'this> RustcCodegenFlags<'this> { "-Zstack-protector" | "-Cstack-protector" => { self.stack_protector = flag_not_empty(value)?; } + // https://doc.rust-lang.org/beta/rustc/codegen-options/index.html#target-cpu + "-Ctarget-cpu" => { + self.target_cpu = flag_not_empty(value)?; + } _ => {} } Ok(()) @@ -297,6 +302,12 @@ impl<'this> RustcCodegenFlags<'this> { push_if_supported(cc_flag.into()); } } + // Attempt to pass target-cpu as a clang/gcc -mcpu: The options are mostly the same, + // and push_if_supported excludes it if not supported + // TODO: this probably needs some mapping from Rust CPUs to gcc/clang values? + if let Some(value) = self.target_cpu { + push_if_supported(format!("-mcpu={value}").into()); + } } // Compiler-exclusive flags @@ -535,6 +546,7 @@ mod tests { "-Csymbol-mangling-version=v0", "-Ctarget-cpu=native", "-Ctarget-feature=+sve", + "-Ctarget-cpu=neoverse-n1", // Unstable options "-Ztune-cpu=machine", ]; @@ -557,6 +569,7 @@ mod tests { dwarf_version: Some(5), stack_protector: Some("strong"), linker_plugin_lto: Some(true), + target_cpu: Some("neoverse-n1"), }, ); } diff --git a/tests/rustflags.rs b/tests/rustflags.rs index 3ec1c8eef..933408fd6 100644 --- a/tests/rustflags.rs +++ b/tests/rustflags.rs @@ -44,4 +44,15 @@ fn inherits_rustflags() { .must_have("-msoft-float") .must_have("-gdwarf-5") .must_not_have("-fno-stack-protector"); + + // pass target-cpu as -mcpu to gcc/clang + std::env::set_var("CARGO_ENCODED_RUSTFLAGS", "-Ctarget-cpu=neoverse-n1"); + // add the aarch64-linux-gnu-gcc shim to fake + let test = Test::gnu(); + test.gcc() + .target("aarch64-unknown-linux-gnu") + .host("aarch64-unknown-linux-gnu") + .file("foo.c") + .compile("foo"); + test.cmd(0).must_have("-mcpu=neoverse-n1"); }