From 7d4f2ec324f37faccc8ddc608c5b0920515594bb Mon Sep 17 00:00:00 2001 From: Jake Shadle Date: Fri, 12 Feb 2021 11:24:30 +0100 Subject: [PATCH 1/3] Replace cmake with cc --- libmimalloc-sys/Cargo.toml | 2 +- libmimalloc-sys/build.rs | 198 +++++++++---------------------------- 2 files changed, 48 insertions(+), 152 deletions(-) diff --git a/libmimalloc-sys/Cargo.toml b/libmimalloc-sys/Cargo.toml index 5f78830..4c9c3b6 100644 --- a/libmimalloc-sys/Cargo.toml +++ b/libmimalloc-sys/Cargo.toml @@ -14,7 +14,7 @@ links = "mimalloc" cty = { version = "0.2", optional = true } [build-dependencies] -cmake = "0.1" +cc = "1.0" [features] secure = [] diff --git a/libmimalloc-sys/build.rs b/libmimalloc-sys/build.rs index acafe46..afd18b9 100644 --- a/libmimalloc-sys/build.rs +++ b/libmimalloc-sys/build.rs @@ -1,168 +1,64 @@ -#![allow(clippy::collapsible_if)] -use cmake::Config; use std::env; -enum CMakeBuildType { - Debug, - Release, - RelWithDebInfo, - MinSizeRel, -} - -/// Determine the CMake build type that will be picked by `cmake-rs`. -/// -/// This is mostly pasted from `cmake-rs`: -/// https://github.com/alexcrichton/cmake-rs/blob/7f85e323/src/lib.rs#L498 -fn get_cmake_build_type() -> Result { - fn getenv(v: &str) -> Result { - env::var(v).map_err(|_| format!("environment variable `{}` not defined", v)) - } - - // Determine Rust's profile, optimization level, and debug info: - #[derive(PartialEq)] - enum RustProfile { - Debug, - Release, - } - #[derive(PartialEq, Debug)] - enum OptLevel { - Debug, - Release, - Size, - } - - let rust_profile = match getenv("PROFILE")?.as_str() { - "debug" => RustProfile::Debug, - "release" | "bench" => RustProfile::Release, - _ => RustProfile::Release, - }; - - let opt_level = match getenv("OPT_LEVEL")?.as_str() { - "0" => OptLevel::Debug, - "1" | "2" | "3" => OptLevel::Release, - "s" | "z" => OptLevel::Size, - _ => match rust_profile { - RustProfile::Debug => OptLevel::Debug, - RustProfile::Release => OptLevel::Release, - }, - }; - - let debug_info: bool = match getenv("DEBUG")?.as_str() { - "false" => false, - "true" => true, - _ => true, - }; - - Ok(match (opt_level, debug_info) { - (OptLevel::Debug, _) => CMakeBuildType::Debug, - (OptLevel::Release, false) => CMakeBuildType::Release, - (OptLevel::Release, true) => CMakeBuildType::RelWithDebInfo, - (OptLevel::Size, _) => CMakeBuildType::MinSizeRel, - }) -} - fn main() { - let mut cfg = &mut Config::new("c_src/mimalloc"); + let mut build = cc::Build::new(); + + build.include("c_src/mimalloc/include"); + build.files( + [ + "alloc-aligned", + "alloc-posix", + "alloc", + "arena", + "bitmap", + "heap", + "init", + "options", + "os", + "page", + "random", + "region", + "segment", + "stats", + ] + .iter() + .map(|fname| format!("c_src/mimalloc/src/{}.c", fname)), + ); + + build.define("MI_STATIC_LIB", None); + + let target_os = env::var("CARGO_CFG_TARGET_OS").expect("target_os not defined!"); if cfg!(feature = "override") { - cfg = cfg.define("MI_OVERRIDE", "ON"); - } else { - cfg = cfg.define("MI_OVERRIDE", "OFF"); + // Overriding malloc is only available on windows in shared mode, but we + // only ever build a static lib. + if target_os != "windows" { + build.define("MI_MALLOC_OVERRIDE", None); + } } - cfg = cfg.define("MI_BUILD_TESTS", "OFF"); - if cfg!(feature = "secure") { - cfg = cfg.define("MI_SECURE", "ON"); - } else { - cfg = cfg.define("MI_SECURE", "OFF"); + build.define("MI_SECURE", "4"); } - if cfg!(feature = "local_dynamic_tls") { - cfg = cfg.define("MI_LOCAL_DYNAMIC_TLS", "ON"); - } else { - cfg = cfg.define("MI_LOCAL_DYNAMIC_TLS", "OFF"); - } - - // Inject MI_DEBUG=0 - // This set mi_option_verbose and mi_option_show_errors options to false. - cfg = cfg.define("mi_defines", "MI_DEBUG=0"); - - let (is_debug, win_folder) = match get_cmake_build_type() { - Ok(CMakeBuildType::Debug) => (true, "Debug"), - Ok(CMakeBuildType::Release) => (false, "Release"), - Ok(CMakeBuildType::RelWithDebInfo) => (false, "RelWithDebInfo"), - Ok(CMakeBuildType::MinSizeRel) => (false, "MinSizeRel"), - Err(e) => panic!("Cannot determine CMake build type: {}", e), - }; - - // Turning off shared lib, tests, PADDING. - // See also: https://github.com/microsoft/mimalloc/blob/master/CMakeLists.txt - cfg = cfg.define("MI_PADDING", "OFF"); - cfg = cfg.define("MI_BUILD_TESTS", "OFF"); - cfg = cfg.define("MI_BUILD_SHARED", "OFF"); - cfg = cfg.define("MI_BUILD_OBJECT", "OFF"); + let dynamic_tls = cfg!(feature = "local_dynamic_tls"); - let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap(); - if target_env == "msvc" { - cfg = cfg.define("CMAKE_SH", "CMAKE_SH-NOTFOUND"); - - // cc::get_compiler have /nologo /MD default flags that are cmake::Config - // defaults to. Those flags prevents mimalloc from building on windows - // extracted from default cmake configuration on windows - if is_debug { - // CMAKE_C_FLAGS + CMAKE_C_FLAGS_DEBUG - cfg = cfg.cflag("/DWIN32 /D_WINDOWS /W3 /MDd /Zi /Ob0 /Od /RTC1"); - } else { - // CMAKE_C_FLAGS + CMAKE_C_FLAGS_RELEASE - cfg = cfg.cflag("/DWIN32 /D_WINDOWS /W3 /MD /O2 /Ob2 /DNDEBUG"); - } - } else if target_env == "gnu" { - cfg = cfg.define("CMAKE_SH", "CMAKE_SH-NOTFOUND"); - // Those flags prevents mimalloc from building on windows - if is_debug { - // CMAKE_C_FLAGS + CMAKE_C_FLAGS_DEBUG - cfg = cfg.cflag("-ffunction-sections -fdata-sections -m64 -O2 -fpic"); + if env::var("CARGO_CFG_TARGET_FAMILY").expect("target family not set") == "unix" + && target_os != "haiku" + { + if dynamic_tls { + build.flag_if_supported("-ftls-model=local-dynamic"); } else { - // CMAKE_C_FLAGS + CMAKE_C_FLAGS_RELEASE - cfg = cfg.cflag("-ffunction-sections -fdata-sections -m64 -O3 -fpic"); + build.flag_if_supported("-ftls-model=initial-exec"); } - }; + } - let mut out_dir = "./build".to_string(); - if cfg!(all(windows)) { - if target_env == "msvc" { - out_dir.push('/'); - out_dir.push_str(win_folder); - } + // Remove heavy debug assertions etc + build.define("MI_DEBUG", "0"); + + if build.get_compiler().is_like_msvc() { + build.cpp(true); } - let out_name = if cfg!(all(windows)) { - if is_debug { - if cfg!(feature = "secure") { - "mimalloc-static-secure-debug" - } else { - "mimalloc-static-debug" - } - } else if cfg!(feature = "secure") { - "mimalloc-static-secure" - } else { - "mimalloc-static" - } - } else if is_debug { - if cfg!(feature = "secure") { - "mimalloc-secure-debug" - } else { - "mimalloc-debug" - } - } else if cfg!(feature = "secure") { - "mimalloc-secure" - } else { - "mimalloc" - }; - // Build mimalloc-static - let mut dst = cfg.build_target("mimalloc-static").build(); - dst.push(out_dir); - println!("cargo:rustc-link-search=native={}", dst.display()); - println!("cargo:rustc-link-lib={}", out_name); + build.compile("mimalloc"); } From 098f7526d33c0a22ab56ce0a0079259dc480c0a1 Mon Sep 17 00:00:00 2001 From: Jake Shadle Date: Fri, 12 Feb 2021 11:30:26 +0100 Subject: [PATCH 2/3] Cleanup README to bump CI --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a74ca96..7bb1b49 100644 --- a/README.md +++ b/README.md @@ -16,18 +16,18 @@ static GLOBAL: MiMalloc = MiMalloc; ## Requirements -[CMake](https://cmake.org/) and a __C__ compiler are required for building -[mimalloc](https://github.com/microsoft/mimalloc) with cargo. +A __C__ compiler are required for building [mimalloc](https://github.com/microsoft/mimalloc) with cargo. ## Usage without secure mode -By default this library builds mimalloc in secure mode. This add guard pages, +By default this library builds mimalloc in secure mode. This add guard pages, randomized allocation, encrypted free lists, etc. The performance penalty is usually around 10% according to [mimalloc](https://github.com/microsoft/mimalloc) own benchmarks. To disable secure mode, put in `Cargo.toml`: -```rust + +```ini [dependencies] mimalloc = { version = "*", default-features = false } ``` From 4f873cb67285f895209a7968c385db213a0751a7 Mon Sep 17 00:00:00 2001 From: Jake Shadle Date: Fri, 12 Feb 2021 11:35:05 +0100 Subject: [PATCH 3/3] oops --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7bb1b49..03891c1 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ static GLOBAL: MiMalloc = MiMalloc; ## Requirements -A __C__ compiler are required for building [mimalloc](https://github.com/microsoft/mimalloc) with cargo. +A __C__ compiler is required for building [mimalloc](https://github.com/microsoft/mimalloc) with cargo. ## Usage without secure mode