From eecc032ab33c5ee420c7669b5407e51e7407d0de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sampo=20Kivist=C3=B6?= Date: Thu, 23 Apr 2026 23:01:09 +0300 Subject: [PATCH 1/2] change mimalloc build to more strictly follow original mimalloc sources --- .gitignore | 3 +- Cargo.toml | 1 + libmimalloc-sys/Cargo.toml | 1 + libmimalloc-sys/build.rs | 71 +++++++++++++++++++++++------ test-override-with-dylib/Cargo.toml | 2 +- 5 files changed, 63 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index cb3fa2e..743f7ed 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /target **/*.rs.bk Cargo.lock -.DS_Store \ No newline at end of file +.DS_Store +.idea diff --git a/Cargo.toml b/Cargo.toml index e1a0531..a8df914 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,6 +34,7 @@ override = ["libmimalloc-sys/override"] debug = ["libmimalloc-sys/debug"] debug_in_debug = ["libmimalloc-sys/debug_in_debug"] local_dynamic_tls = ["libmimalloc-sys/local_dynamic_tls"] +win_direct_tls = ["libmimalloc-sys/win_direct_tls"] no_thp = ["libmimalloc-sys/no_thp"] extended = ["libmimalloc-sys/extended"] v2 = ["libmimalloc-sys/v2"] diff --git a/libmimalloc-sys/Cargo.toml b/libmimalloc-sys/Cargo.toml index b254942..6efd6cd 100644 --- a/libmimalloc-sys/Cargo.toml +++ b/libmimalloc-sys/Cargo.toml @@ -41,6 +41,7 @@ override = [] extended = ["cty"] arena = [] local_dynamic_tls = [] +win_direct_tls = [] no_thp = [] v2 = [] diff --git a/libmimalloc-sys/build.rs b/libmimalloc-sys/build.rs index ce7a665..f108339 100644 --- a/libmimalloc-sys/build.rs +++ b/libmimalloc-sys/build.rs @@ -1,5 +1,6 @@ use std::env; -use std::path::Path; +use std::fs; +use std::path::{Path, PathBuf}; fn main() { let mut build = cc::Build::new(); @@ -11,30 +12,57 @@ fn main() { }; let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set"); - let include_dir = Path::new(&cargo_manifest_dir) - .join("c_src/mimalloc/") - .join(version) + let include_root = Path::new(&cargo_manifest_dir) + .join("c_src") + .join("mimalloc") + .join(version); + let include_dir = include_root .join("include") .to_str() .expect("include path is not valid UTF-8") .to_string(); + let static_source = include_root.join("src").join("static.c"); + // Make the include directory available to consumers via the `DEP_MIMALLOC_INCLUDE_DIR` // environment variable. println!("cargo:INCLUDE_DIR={include_dir}"); build.include(format!("c_src/mimalloc/{version}/include")); build.include(format!("c_src/mimalloc/{version}/src")); - build.file(format!("c_src/mimalloc/{version}/src/static.c")); let target_os = env::var("CARGO_CFG_TARGET_OS").expect("target_os not defined!"); let target_family = env::var("CARGO_CFG_TARGET_FAMILY").expect("target_family not defined!"); + let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default(); let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").expect("target_vendor not defined!"); let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("target_arch not defined!"); + let cargo_debug = env::var("DEBUG") + .map(|value| value == "true" || value == "1") + .unwrap_or(false); + let debug_enabled = env::var_os("CARGO_FEATURE_DEBUG").is_some() + || (env::var_os("CARGO_FEATURE_DEBUG_IN_DEBUG").is_some() && cargo_debug); if target_family != "windows" { build.flag("-Wno-error=date-time"); } + if target_env == "msvc" { + // Mimalloc expects the MSVC/clang-cl build to use the C++ atomics path. + build.cpp(true); + build.std("c++17"); + build.flag_if_supported("/Zc:__cplusplus"); + + let wrapper = + PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR not set")).join("mimalloc-static.cc"); + let include = static_source.to_string_lossy().replace('\\', "/"); + fs::write(&wrapper, format!("#include \"{include}\"\n")) + .expect("failed to write mimalloc C++ wrapper"); + build.file(wrapper); + } else { + build.file(&static_source); + } + + let compiler = build.get_compiler(); + if env::var_os("CARGO_FEATURE_OVERRIDE").is_some() { // Overriding malloc is only available on windows in shared mode, but we // only ever build a static lib. @@ -45,12 +73,19 @@ fn main() { build.define("MI_OSX_ZONE", Some("1")); build.define("MI_OSX_INTERPOSE", Some("1")); } + if !compiler.is_like_msvc() { + build.flag_if_supported("-fno-builtin-malloc"); + } } if env::var_os("CARGO_FEATURE_SECURE").is_some() { build.define("MI_SECURE", "4"); } + if target_os == "windows" && env::var_os("CARGO_FEATURE_WIN_DIRECT_TLS").is_some() { + build.define("MI_WIN_DIRECT_TLS", "1"); + } + let dynamic_tls = env::var("CARGO_FEATURE_LOCAL_DYNAMIC_TLS").is_ok(); if target_family == "unix" && target_os != "haiku" { @@ -61,24 +96,34 @@ fn main() { } } + if target_arch == "aarch64" { + if compiler.is_like_msvc() { + if compiler.is_like_clang() { + build.flag_if_supported("-march=armv8.1-a"); + } else { + build.flag_if_supported("/arch:armv8.1"); + } + } else { + build.flag_if_supported("-march=armv8.1-a"); + } + } + if (target_os == "linux" || target_os == "android") && env::var_os("CARGO_FEATURE_NO_THP").is_some() { build.define("MI_NO_THP", "1"); } - if env::var_os("CARGO_FEATURE_DEBUG").is_some() - || (env::var_os("CARGO_FEATURE_DEBUG_IN_DEBUG").is_some() && cfg!(debug_assertions)) - { + if debug_enabled { build.define("MI_DEBUG", "3"); build.define("MI_SHOW_ERRORS", "1"); } else { - // Remove heavy debug assertions etc + // Remove heavy debug assertions etc. build.define("MI_DEBUG", "0"); - } - - if build.get_compiler().is_like_msvc() { - build.cpp(true); + if !cargo_debug { + build.define("MI_BUILD_RELEASE", None); + build.define("NDEBUG", None); + } } build.compile("mimalloc"); diff --git a/test-override-with-dylib/Cargo.toml b/test-override-with-dylib/Cargo.toml index c1381fc..a31ecea 100644 --- a/test-override-with-dylib/Cargo.toml +++ b/test-override-with-dylib/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" publish = false [dependencies] -libc = { version = "^0.2.8", default-features = false } +libc = { version = "^0.2.186", default-features = false } libmimalloc-sys = { path = "../libmimalloc-sys" } [build-dependencies] From 8d520dd3304c29a9b9114299ec56c6e92023e8a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sampo=20Kivist=C3=B6?= Date: Fri, 24 Apr 2026 08:41:14 +0300 Subject: [PATCH 2/2] bump cc --- libmimalloc-sys/Cargo.toml | 2 +- test-override-with-dylib/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libmimalloc-sys/Cargo.toml b/libmimalloc-sys/Cargo.toml index 6efd6cd..e092d83 100644 --- a/libmimalloc-sys/Cargo.toml +++ b/libmimalloc-sys/Cargo.toml @@ -31,7 +31,7 @@ cty = { version = "0.2", optional = true } libc = "0.2" [build-dependencies] -cc = "1.0" +cc = "1.2" [features] secure = [] diff --git a/test-override-with-dylib/Cargo.toml b/test-override-with-dylib/Cargo.toml index a31ecea..faddf14 100644 --- a/test-override-with-dylib/Cargo.toml +++ b/test-override-with-dylib/Cargo.toml @@ -11,7 +11,7 @@ libc = { version = "^0.2.186", default-features = false } libmimalloc-sys = { path = "../libmimalloc-sys" } [build-dependencies] -cc = "^1.0.13" +cc = "^1.2.60" [features] override = ["libmimalloc-sys/override"]