Skip to content

Commit fea4a2c

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

File tree

3 files changed

+57
-29
lines changed

3 files changed

+57
-29
lines changed

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ 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"]
36+
asan = ["libmimalloc-sys2/asan"]
37+
valgrind = ["libmimalloc-sys2/valgrind"]

libmimalloc-sys/Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,21 @@ cty = { version = "0.2", optional = true }
2323
libc = "0.2"
2424

2525
[build-dependencies]
26-
cc = "1.0"
26+
cmake = "0.1"
2727

2828
[features]
29+
# For debug purposes
30+
asan = []
31+
valgrind = []
32+
etw = []
33+
2934
secure = []
30-
debug = []
31-
debug_in_debug = []
3235
override = []
3336
extended = ["cty"]
3437
arena = []
3538
local_dynamic_tls = []
3639
no_thp = []
40+
skip_collect_on_exit = []
3741

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

libmimalloc-sys/build.rs

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,80 @@
11
use std::env;
22

3+
use cmake::Config;
4+
35
fn main() {
4-
let mut build = cc::Build::new();
6+
let mut cmake_config = Config::new("c_src/mimalloc");
57

6-
build.include("c_src/mimalloc/include");
7-
build.include("c_src/mimalloc/src");
8-
build.file("c_src/mimalloc/src/static.c");
8+
cmake_config
9+
.define("MI_BUILD_SHARED", "OFF")
10+
.define("MI_BUILD_TESTS", "OFF");
911

1012
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!");
1213
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("target_arch not defined!");
14+
let target_env = env::var("CARGO_CFG_TARGET_ENV").expect("target_env not defined!");
15+
let profile = env::var("PROFILE").expect("profile not defined!");
16+
17+
if profile == "debug" {
18+
cmake_config
19+
.define("MI_DEBUG_FULL", "ON")
20+
.define("MI_SHOW_ERRORS", "ON");
21+
}
1322

1423
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-
}
24+
cmake_config.define("MI_OVERRIDE", "ON");
25+
}
26+
27+
if env::var_os("CARGO_FEATURE_SKIP_COLLECT_ON_EXIT").is_some() {
28+
cmake_config.define("MI_SKIP_COLLECT_ON_EXIT", "ON");
29+
}
30+
31+
if env::var_os("CARGO_FEATURE_ASAN").is_some() {
32+
cmake_config.define("MI_TRACK_ASAN", "ON");
33+
}
34+
35+
if env::var_os("CARGO_FEATURE_VALGRIND").is_some() {
36+
cmake_config.define("MI_TRACK_VALGRIND", "ON");
37+
}
38+
39+
if env::var_os("CARGO_FEATURE_ETW").is_some() {
40+
cmake_config.define("MI_TRACK_ETW", "ON");
41+
}
42+
43+
if target_env == "musl" {
44+
cmake_config.define("MI_LIBC_MUSL", "1");
2045
}
2146

2247
if env::var_os("CARGO_FEATURE_SECURE").is_some() {
23-
build.define("MI_SECURE", "4");
48+
cmake_config.define("MI_SECURE", "ON");
2449
}
2550

2651
let dynamic_tls = env::var("CARGO_FEATURE_LOCAL_DYNAMIC_TLS").is_ok();
2752

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-
}
53+
if dynamic_tls {
54+
cmake_config.define("MI_LOCAL_DYNAMIC_TLS", "ON");
3455
}
3556

3657
if (target_os == "linux" || target_os == "android")
3758
&& env::var_os("CARGO_FEATURE_NO_THP").is_some()
3859
{
39-
build.define("MI_NO_THP", "1");
60+
cmake_config.define("MI_NO_THP", "1");
4061
}
4162

4263
if env::var_os("CARGO_FEATURE_DEBUG").is_some()
4364
|| (env::var_os("CARGO_FEATURE_DEBUG_IN_DEBUG").is_some() && cfg!(debug_assertions))
4465
{
45-
build.define("MI_DEBUG", "3");
46-
build.define("MI_SHOW_ERRORS", "1");
66+
cmake_config.define("MI_DEBUG_FULL", "ON");
67+
cmake_config.define("MI_SHOW_ERRORS", "ON");
4768
} else {
4869
// Remove heavy debug assertions etc
49-
build.define("MI_DEBUG", "0");
70+
cmake_config.define("MI_DEBUG_FULL", "OFF");
5071
}
5172

52-
if build.get_compiler().is_like_msvc() {
53-
build.cpp(true);
73+
if target_env == "msvc" {
74+
cmake_config.define("MI_USE_CXX", "ON");
5475
}
5576

56-
build.compile("mimalloc");
77+
cmake_config.build();
5778

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

0 commit comments

Comments
 (0)