Skip to content

Commit 7aa9032

Browse files
committed
build(sys): migrate to cmake
1 parent ef2493b commit 7aa9032

File tree

4 files changed

+80
-30
lines changed

4 files changed

+80
-30
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jobs:
1212
name: Rust
1313

1414
strategy:
15+
fail-fast: false
1516
matrix:
1617
os: [ubuntu-latest, macos-latest, windows-latest]
1718
toolchain: ["stable"]

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ libmimalloc-sys2 = { path = "libmimalloc-sys", version = "0.1.39", default-featu
2727
default = []
2828
secure = ["libmimalloc-sys2/secure"]
2929
override = ["libmimalloc-sys2/override"]
30-
debug = ["libmimalloc-sys2/debug"]
31-
debug_in_debug = ["libmimalloc-sys2/debug_in_debug"]
3230
local_dynamic_tls = ["libmimalloc-sys2/local_dynamic_tls"]
3331
no_thp = ["libmimalloc-sys2/no_thp"]
3432
extended = ["libmimalloc-sys2/extended"]
33+
skip_collect_on_exit = ["libmimalloc-sys2/skip_collect_on_exit"]
34+
35+
etw = ["libmimalloc-sys2/etw"]

libmimalloc-sys/Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ license = "MIT"
1111
links = "mimalloc"
1212
exclude = [
1313
"/c_src/mimalloc/bin",
14-
"/c_src/mimalloc/cmake",
1514
"/c_src/mimalloc/doc",
1615
"/c_src/mimalloc/docs",
1716
"/c_src/mimalloc/ide",
@@ -23,17 +22,19 @@ cty = { version = "0.2", optional = true }
2322
libc = "0.2"
2423

2524
[build-dependencies]
26-
cc = "1.0"
25+
cmake = "0.1"
2726

2827
[features]
28+
# For debug purposes
29+
etw = []
30+
2931
secure = []
30-
debug = []
31-
debug_in_debug = []
3232
override = []
3333
extended = ["cty"]
3434
arena = []
3535
local_dynamic_tls = []
3636
no_thp = []
37+
skip_collect_on_exit = []
3738

3839
# Show `extended` on docs.rs since it's the full API surface.
3940
[package.metadata.docs.rs]

libmimalloc-sys/build.rs

Lines changed: 71 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,106 @@
1+
use std::borrow::Cow;
12
use std::env;
23

4+
use cmake::Config;
5+
36
fn main() {
4-
let mut build = cc::Build::new();
7+
let mut cmake_config = Config::new("c_src/mimalloc");
8+
9+
let mut mimalloc_base_name = Cow::Borrowed("mimalloc");
510

6-
build.include("c_src/mimalloc/include");
7-
build.include("c_src/mimalloc/src");
8-
build.file("c_src/mimalloc/src/static.c");
11+
cmake_config
12+
.define("MI_BUILD_STATIC", "ON")
13+
.define("MI_BUILD_OBJECT", "OFF")
14+
.define("MI_BUILD_SHARED", "OFF")
15+
.define("MI_BUILD_TESTS", "OFF");
916

1017
let target_os = env::var("CARGO_CFG_TARGET_OS").expect("target_os not defined!");
11-
let target_family = env::var("CARGO_CFG_TARGET_FAMILY").expect("target_family not defined!");
1218
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("target_arch not defined!");
19+
let target_env = env::var("CARGO_CFG_TARGET_ENV").expect("target_env not defined!");
20+
let profile = env::var("PROFILE").expect("profile not defined!");
1321

1422
if env::var_os("CARGO_FEATURE_OVERRIDE").is_some() {
15-
// Overriding malloc is only available on windows in shared mode, but we
16-
// only ever build a static lib.
17-
if target_family != "windows" {
18-
build.define("MI_MALLOC_OVERRIDE", None);
19-
}
23+
cmake_config.define("MI_OVERRIDE", "ON");
24+
} else {
25+
cmake_config.define("MI_OVERRIDE", "OFF");
26+
}
27+
28+
if env::var_os("CARGO_FEATURE_SKIP_COLLECT_ON_EXIT").is_some() {
29+
cmake_config.define("MI_SKIP_COLLECT_ON_EXIT", "ON");
30+
}
31+
32+
if target_os == "windows" {
33+
mimalloc_base_name = Cow::Owned(format!("{}-static", mimalloc_base_name));
2034
}
2135

2236
if env::var_os("CARGO_FEATURE_SECURE").is_some() {
23-
build.define("MI_SECURE", "4");
37+
cmake_config.define("MI_SECURE", "ON");
38+
mimalloc_base_name = Cow::Owned(format!("{}-secure", mimalloc_base_name));
39+
}
40+
41+
if env::var_os("CARGO_FEATURE_ETW").is_some() {
42+
cmake_config.define("MI_TRACK_ETW", "ON");
43+
}
44+
45+
if profile == "debug" {
46+
cmake_config
47+
.define("MI_DEBUG_FULL", "ON")
48+
.define("MI_SHOW_ERRORS", "ON");
49+
mimalloc_base_name = Cow::Owned(format!("{}-debug", mimalloc_base_name));
50+
}
51+
52+
if target_env == "musl" {
53+
cmake_config.define("MI_LIBC_MUSL", "1");
2454
}
2555

2656
let dynamic_tls = env::var("CARGO_FEATURE_LOCAL_DYNAMIC_TLS").is_ok();
2757

28-
if target_family == "unix" && target_os != "haiku" {
29-
if dynamic_tls {
30-
build.flag_if_supported("-ftls-model=local-dynamic");
31-
} else {
32-
build.flag_if_supported("-ftls-model=initial-exec");
33-
}
58+
if dynamic_tls {
59+
cmake_config.define("MI_LOCAL_DYNAMIC_TLS", "ON");
3460
}
3561

3662
if (target_os == "linux" || target_os == "android")
3763
&& env::var_os("CARGO_FEATURE_NO_THP").is_some()
3864
{
39-
build.define("MI_NO_THP", "1");
65+
cmake_config.define("MI_NO_THP", "1");
4066
}
4167

4268
if env::var_os("CARGO_FEATURE_DEBUG").is_some()
4369
|| (env::var_os("CARGO_FEATURE_DEBUG_IN_DEBUG").is_some() && cfg!(debug_assertions))
4470
{
45-
build.define("MI_DEBUG", "3");
46-
build.define("MI_SHOW_ERRORS", "1");
71+
cmake_config.define("MI_DEBUG_FULL", "ON");
72+
cmake_config.define("MI_SHOW_ERRORS", "ON");
4773
} else {
4874
// Remove heavy debug assertions etc
49-
build.define("MI_DEBUG", "0");
75+
cmake_config.define("MI_DEBUG_FULL", "OFF");
5076
}
5177

52-
if build.get_compiler().is_like_msvc() {
53-
build.cpp(true);
78+
if target_env == "msvc" {
79+
cmake_config.define("MI_USE_CXX", "ON");
80+
if profile == "debug" {
81+
println!("cargo:rustc-link-lib=ucrtd");
82+
} else {
83+
println!("cargo:rustc-link-lib=ucrt");
84+
}
85+
}
86+
87+
let dst = cmake_config.build();
88+
89+
if target_os == "windows" {
90+
println!(
91+
"cargo:rustc-link-search=native={}/build/{}",
92+
dst.display(),
93+
if profile == "debug" {
94+
"Debug"
95+
} else {
96+
"Release"
97+
}
98+
);
99+
} else {
100+
println!("cargo:rustc-link-search=native={}/build", dst.display());
54101
}
55102

56-
build.compile("mimalloc");
103+
println!("cargo:rustc-link-lib=static={}", mimalloc_base_name);
57104

58105
// on armv6 we need to link with libatomic
59106
if target_os == "linux" && target_arch == "arm" {

0 commit comments

Comments
 (0)