Skip to content
Open
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
34 changes: 27 additions & 7 deletions snmalloc-rs/snmalloc-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@ impl BuildConfig {

trait BuilderDefine {
fn define(&mut self, key: &str, value: &str) -> &mut Self;
/// Set a CMake-style boolean option.
///
/// CMake reads `-DKEY=OFF` as "not enabled". The C preprocessor does not:
/// `-DKEY=OFF` *defines* `KEY`, and snmalloc guards these options with
/// `#if defined(KEY)` / `#ifdef KEY`, so forwarding the "OFF" string on the
/// `build_cc` path enables every option it was meant to disable. Emit the
/// macro on that path only when the option is actually on.
fn define_bool(&mut self, key: &str, enabled: bool) -> &mut Self;
fn flag_if_supported(&mut self, flag: &str) -> &mut Self;
fn build_lib(&mut self, target_lib: &str) -> std::path::PathBuf;
fn configure_output_dir(&mut self, out_dir: &str) -> &mut Self;
Expand All @@ -229,7 +237,15 @@ impl BuilderDefine for cc::Build {
fn define(&mut self, key: &str, value: &str) -> &mut Self {
self.define(key, Some(value))
}


fn define_bool(&mut self, key: &str, enabled: bool) -> &mut Self {
if enabled {
self.define(key, None)
} else {
self
}
}

fn flag_if_supported(&mut self, flag: &str) -> &mut Self {
self.flag_if_supported(flag)
}
Expand Down Expand Up @@ -261,7 +277,11 @@ impl BuilderDefine for cmake::Config {
fn define(&mut self, key: &str, value: &str) -> &mut Self {
self.define(key, value)
}


fn define_bool(&mut self, key: &str, enabled: bool) -> &mut Self {
self.define(key, if enabled { "ON" } else { "OFF" })
}

fn flag_if_supported(&mut self, _flag: &str) -> &mut Self {
self
}
Expand Down Expand Up @@ -452,11 +472,11 @@ fn configure_platform(config: &mut BuildConfig) {

// Feature configurations
config.builder
.define("SNMALLOC_QEMU_WORKAROUND", if config.features.qemu { "ON" } else { "OFF" })
.define("SNMALLOC_ENABLE_DYNAMIC_LOADING", if config.features.notls { "ON" } else { "OFF" })
.define("USE_SNMALLOC_STATS", if config.features.stats { "ON" } else { "OFF" })
.define("SNMALLOC_RUST_LIBC_API", if config.features.libc_api { "ON" } else { "OFF" })
.define("SNMALLOC_USE_CXX17", if cfg!(feature = "usecxx17") { "ON" } else { "OFF" });
.define_bool("SNMALLOC_QEMU_WORKAROUND", config.features.qemu)
.define_bool("SNMALLOC_ENABLE_DYNAMIC_LOADING", config.features.notls)
.define_bool("USE_SNMALLOC_STATS", config.features.stats)
.define_bool("SNMALLOC_RUST_LIBC_API", config.features.libc_api)
.define_bool("SNMALLOC_USE_CXX17", cfg!(feature = "usecxx17"));

if config.features.tracing {
config.builder.define("SNMALLOC_TRACING", "ON");
Expand Down