Skip to content

Commit cf6da0b

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

File tree

4 files changed

+67
-29
lines changed

4 files changed

+67
-29
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: 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: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,89 @@
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_STATIC", "ON")
10+
.define("MI_BUILD_OBJECT", "OFF")
11+
.define("MI_BUILD_SHARED", "OFF")
12+
.define("MI_BUILD_TESTS", "OFF");
913

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

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

2249
if env::var_os("CARGO_FEATURE_SECURE").is_some() {
23-
build.define("MI_SECURE", "4");
50+
cmake_config.define("MI_SECURE", "ON");
2451
}
2552

2653
let dynamic_tls = env::var("CARGO_FEATURE_LOCAL_DYNAMIC_TLS").is_ok();
2754

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-
}
55+
if dynamic_tls {
56+
cmake_config.define("MI_LOCAL_DYNAMIC_TLS", "ON");
3457
}
3558

3659
if (target_os == "linux" || target_os == "android")
3760
&& env::var_os("CARGO_FEATURE_NO_THP").is_some()
3861
{
39-
build.define("MI_NO_THP", "1");
62+
cmake_config.define("MI_NO_THP", "1");
4063
}
4164

4265
if env::var_os("CARGO_FEATURE_DEBUG").is_some()
4366
|| (env::var_os("CARGO_FEATURE_DEBUG_IN_DEBUG").is_some() && cfg!(debug_assertions))
4467
{
45-
build.define("MI_DEBUG", "3");
46-
build.define("MI_SHOW_ERRORS", "1");
68+
cmake_config.define("MI_DEBUG_FULL", "ON");
69+
cmake_config.define("MI_SHOW_ERRORS", "ON");
4770
} else {
4871
// Remove heavy debug assertions etc
49-
build.define("MI_DEBUG", "0");
72+
cmake_config.define("MI_DEBUG_FULL", "OFF");
5073
}
5174

52-
if build.get_compiler().is_like_msvc() {
53-
build.cpp(true);
75+
if target_env == "msvc" {
76+
cmake_config.define("MI_USE_CXX", "ON");
5477
}
5578

56-
build.compile("mimalloc");
79+
let dst = cmake_config.build();
80+
81+
println!("cargo:rustc-link-search=native={}/build", dst.display());
82+
if profile == "debug" {
83+
println!("cargo:rustc-link-lib=static=mimalloc-debug");
84+
} else {
85+
println!("cargo:rustc-link-lib=static=mimalloc");
86+
}
5787

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

0 commit comments

Comments
 (0)