Skip to content

Commit

Permalink
Update build.rs check env cc
Browse files Browse the repository at this point in the history
  • Loading branch information
ryancinsight authored Dec 3, 2024
1 parent 9ca0c82 commit 8b52990
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions snmalloc-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
use std::env;
use std::fs;

#[derive(Debug)]
enum Compiler {
Clang,
Gcc,
Msvc,
Unknown
}

struct BuildConfig {
debug: bool,
optim_level: &'static str,
Expand All @@ -20,6 +28,7 @@ struct BuildConfig {
builder: cc::Build,
#[cfg(not(feature = "build_cc"))]
builder: cmake::Config,
compiler: Compiler
}
impl std::fmt::Debug for BuildConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand Down Expand Up @@ -60,7 +69,7 @@ impl BuildConfig {

#[cfg(not(feature = "build_cc"))]
let builder = Config::new("snmalloc");
let config = Self {
let mut config = Self {
debug,
optim_level: if debug { "-O0" } else { "-O3" },
target_os: env::var("CARGO_CFG_TARGET_OS").expect("target_os not defined!"),
Expand All @@ -78,17 +87,35 @@ impl BuildConfig {
},
features: BuildFeatures::new(),
builder,
compiler: Compiler::Unknown,
};

config.compiler = config.detect_compiler();
config.embed_build_info();
config
}
fn detect_compiler(&self) -> Compiler {
if self.is_msvc() {
return Compiler::Msvc;
}

// Check for clang first since it can masquerade as gcc
if let Ok(cc) = env::var("CC") {
if cc.contains("clang") {
return Compiler::Clang;
}
if cc.contains("gcc") {
return Compiler::Gcc;
}
}

Compiler::Unknown
}
fn embed_build_info(&self) {
println!("cargo:rustc-env=BUILD_TARGET_OS={}", self.target_os);
println!("cargo:rustc-env=BUILD_TARGET_ENV={}", self.target_env);
println!("cargo:rustc-env=BUILD_TARGET_FAMILY={}", self.target_family);
println!("cargo:rustc-env=BUILD_TARGET={}", self.target);
println!("cargo:rustc-env=BUILD_CC={:#?}", self.compiler);
println!("cargo:rustc-env=BUILD_TYPE={}", self.build_type);
println!("cargo:rustc-env=BUILD_DEBUG={}", self.debug);
println!("cargo:rustc-env=BUILD_OPTIM_LEVEL={}", self.optim_level);
Expand Down Expand Up @@ -172,7 +199,16 @@ fn configure_features(config: &mut BuildConfig) {
}

if config.features.lto {
config.builder.define("SNMALLOC_IPO", "ON");
match config.compiler {
Compiler::Unknown => {
// Skip LTO for unknown compiler
}
_ => {
println!("{:?}",config.compiler);
// Enable LTO for known compilers (Clang, Gcc, Msvc)
config.builder.define("SNMALLOC_IPO", "ON");
}
}
}

if config.features.notls {
Expand Down

0 comments on commit 8b52990

Please sign in to comment.