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
2 changes: 1 addition & 1 deletion snmalloc-rs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ The following environment variables are automatically detected and propagated:
- `OPT_LEVEL`: Propagated to the C++ compiler to ensure optimization parity between Rust and C++ components.

### Windows CRT Consistency
On Windows, the build script enforces static CRT linking (`/MT` or `/MTd`) across both `cc` and `cmake` builders. This prevents linker errors and ensures consistency when `snmalloc` is used as a global allocator.
On Windows, the build script uses dynamic linking (`/MD`) by default and static linking (`/MT`) when the `crt-static` target feature is enabled.

**To get the crates compiled, you need to choose either `1mib` or `16mib` to determine the chunk configuration**

Expand Down
24 changes: 19 additions & 5 deletions snmalloc-rs/snmalloc-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ impl BuildConfig {
}
}

fn target_uses_static_crt() -> bool {
env::var("CARGO_CFG_TARGET_FEATURE")
.map(|features| features.split(',').any(|feature| feature == "crt-static"))
.unwrap_or(false)
}

trait BuilderDefine {
fn define(&mut self, key: &str, value: &str) -> &mut Self;
/// Set a CMake-style boolean option.
Expand Down Expand Up @@ -264,7 +270,6 @@ impl BuilderDefine for cc::Build {
.file(source_root.join("src/snmalloc/override/rust.cc"))
.cpp(true)
.debug(debug)
.static_crt(true)
}

fn compiler_define(&mut self, key: &str, value: &str) -> &mut Self {
Expand Down Expand Up @@ -295,11 +300,21 @@ impl BuilderDefine for cmake::Config {
}

fn configure_cpp(&mut self, debug: bool, _source_root: &Path) -> &mut Self {
let static_crt = target_uses_static_crt();
let runtime_library = if static_crt {
"MultiThreaded"
} else {
"MultiThreadedDLL"
};

self.define("SNMALLOC_RUST_SUPPORT", "ON")
.very_verbose(true)
.define("CMAKE_SH", "CMAKE_SH-NOTFOUND")
.always_configure(true)
.static_crt(true)
.static_crt(static_crt)
// Keep the CRT flavor aligned with Rust without enabling the
// Debug CRT for snmalloc's Debug profile.
.define("CMAKE_MSVC_RUNTIME_LIBRARY", runtime_library)
.profile(if debug { "Debug" } else { "Release" })
}

Expand Down Expand Up @@ -584,9 +599,8 @@ fn configure_linking(config: &BuildConfig) {
println!("cargo:rustc-link-lib=userenv");
println!("cargo:rustc-link-lib=bcrypt");
println!("cargo:rustc-link-lib=ntdll");
if config.debug {
println!("cargo:rustc-link-lib=msvcrtd");
} else {
if !target_uses_static_crt() {
// Rust's MSVC target uses the release CRT for every profile.
println!("cargo:rustc-link-lib=msvcrt");
}
}
Expand Down