Skip to content

Commit

Permalink
Honor RUSTFLAGS's target-cpu if given
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoqun committed Apr 2, 2020
1 parent 195263f commit bca41bb
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,27 @@ fn write_tables() {
not(any(target_os = "android", target_os = "ios"))
))]
fn compile_simd_c() {
fn guess_target_cpu() -> String {
// Copied and adapted from https://github.com/alexcrichton/proc-macro2/blob/4173a21dc497c67326095e438ff989cc63cd9279/build.rs#L115
// Licensed under Apache-2.0 + MIT (compatible because we're MIT)
let rustflags = env::var_os("RUSTFLAGS");
if let Some(rustflags) = rustflags {
for mut flag in rustflags.to_string_lossy().split(' ') {
if flag.starts_with("-C") {
flag = &flag["-C".len()..];
}
if flag.starts_with("target-cpu=") {
return flag["target-cpu=".len()..].to_owned()
}
}
}

"native".to_string()
}

cc::Build::new()
.opt_level(3)
.flag("-march=native")
.flag(&format!("-march={}", guess_target_cpu()))
.flag("-std=c11")
.file("simd_c/reedsolomon.c")
.compile("reedsolomon");
Expand Down

0 comments on commit bca41bb

Please sign in to comment.