Skip to content

Commit 09fc7a0

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

File tree

4 files changed

+57
-29
lines changed

4 files changed

+57
-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: 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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@ 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+
etw = []
31+
2932
secure = []
30-
debug = []
31-
debug_in_debug = []
3233
override = []
3334
extended = ["cty"]
3435
arena = []
3536
local_dynamic_tls = []
3637
no_thp = []
38+
skip_collect_on_exit = []
3739

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

libmimalloc-sys/build.rs

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,83 @@
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+
}
25+
26+
if env::var_os("CARGO_FEATURE_SKIP_COLLECT_ON_EXIT").is_some() {
27+
cmake_config.define("MI_SKIP_COLLECT_ON_EXIT", "ON");
2028
}
2129

2230
if env::var_os("CARGO_FEATURE_SECURE").is_some() {
23-
build.define("MI_SECURE", "4");
31+
cmake_config.define("MI_SECURE", "ON");
32+
mimalloc_base_name = Cow::Owned(format!("{}-secure", mimalloc_base_name));
33+
}
34+
35+
if env::var_os("CARGO_FEATURE_ETW").is_some() {
36+
cmake_config.define("MI_TRACK_ETW", "ON");
37+
}
38+
39+
if profile == "debug" {
40+
cmake_config
41+
.define("MI_DEBUG_FULL", "ON")
42+
.define("MI_SHOW_ERRORS", "ON");
43+
mimalloc_base_name = Cow::Owned(format!("{}-debug", mimalloc_base_name));
44+
}
45+
46+
if target_env == "musl" {
47+
cmake_config.define("MI_LIBC_MUSL", "1");
2448
}
2549

2650
let dynamic_tls = env::var("CARGO_FEATURE_LOCAL_DYNAMIC_TLS").is_ok();
2751

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

3656
if (target_os == "linux" || target_os == "android")
3757
&& env::var_os("CARGO_FEATURE_NO_THP").is_some()
3858
{
39-
build.define("MI_NO_THP", "1");
59+
cmake_config.define("MI_NO_THP", "1");
4060
}
4161

4262
if env::var_os("CARGO_FEATURE_DEBUG").is_some()
4363
|| (env::var_os("CARGO_FEATURE_DEBUG_IN_DEBUG").is_some() && cfg!(debug_assertions))
4464
{
45-
build.define("MI_DEBUG", "3");
46-
build.define("MI_SHOW_ERRORS", "1");
65+
cmake_config.define("MI_DEBUG_FULL", "ON");
66+
cmake_config.define("MI_SHOW_ERRORS", "ON");
4767
} else {
4868
// Remove heavy debug assertions etc
49-
build.define("MI_DEBUG", "0");
69+
cmake_config.define("MI_DEBUG_FULL", "OFF");
5070
}
5171

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

56-
build.compile("mimalloc");
76+
let dst = cmake_config.build();
77+
78+
println!("cargo:rustc-link-search=native={}/build", dst.display());
79+
80+
println!("cargo:rustc-link-lib=static={}", mimalloc_base_name);
5781

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

0 commit comments

Comments
 (0)