Skip to content
Open
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub(crate) struct RustcCodegenFlags<'a> {
dwarf_version: Option<u32>,
stack_protector: Option<&'a str>,
linker_plugin_lto: Option<bool>,
target_cpu: Option<&'a str>,
}

impl<'this> RustcCodegenFlags<'this> {
Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
];
Expand All @@ -557,6 +569,7 @@ mod tests {
dwarf_version: Some(5),
stack_protector: Some("strong"),
linker_plugin_lto: Some(true),
target_cpu: Some("neoverse-n1"),
},
);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/rustflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Loading