Skip to content

Commit

Permalink
start, specify CPU target
Browse files Browse the repository at this point in the history
  • Loading branch information
ParkMyCar committed Jul 10, 2024
1 parent e33480a commit 92367a1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions decnumber-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ version-sync = "0.9"

[build-dependencies]
cc = "1.0.50"
rustflags = "0.1.4"
29 changes: 28 additions & 1 deletion decnumber-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,42 @@ use std::env;
use std::fs;
use std::path::{Path, PathBuf};

/// Extracts the `target-cpu` from `CARGO_ENCODED_RUSTFLAGS`.
fn target_cpu() -> Option<String> {
rustflags::from_env().find_map(|flag| match flag {
rustflags::Flag::Codegen { opt, value } if opt == "target-cpu" => value,
_ => None,
})
}

fn main() {
let target = env::var("TARGET").unwrap();
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());

let declitend = if cfg!(target_endian = "little") {
"1"
} else {
"0"
};
cc::Build::new()
let mut build = cc::Build::new();

if let Some(target_cpu) = target_cpu() {
if target.contains("x86_64") {
build.flag_if_supported(&format!("-march={target_cpu}"));

// The x86-64-vX targets are generic and do not support tuning.
if !target_cpu.starts_with("x86-64-v") {
build.flag_if_supported(&format!("-mtune={target_cpu}"));
}
} else if target.contains("aarch64") {
// The -mcpu argument is deprecated for x86 but supported for AArch64.
//
// See: https://gcc.gnu.org/onlinedocs/gcc/AArch64-Options.html
build.flag_if_supported(&format!("-mcpu={target_cpu}"));
}
}

build
.define("DECLITEND", Some(declitend))
.file("decnumber/decimal128.c")
.file("decnumber/decimal64.c")
Expand Down

0 comments on commit 92367a1

Please sign in to comment.