|
| 1 | +use std::borrow::Cow; |
1 | 2 | use std::env;
|
2 | 3 |
|
| 4 | +use cmake::Config; |
| 5 | + |
3 | 6 | 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"); |
5 | 10 |
|
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"); |
9 | 16 |
|
10 | 17 | 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!"); |
12 | 18 | 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!"); |
13 | 21 |
|
14 | 22 | 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"); |
20 | 28 | }
|
21 | 29 |
|
22 | 30 | 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"); |
24 | 48 | }
|
25 | 49 |
|
26 | 50 | let dynamic_tls = env::var("CARGO_FEATURE_LOCAL_DYNAMIC_TLS").is_ok();
|
27 | 51 |
|
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"); |
34 | 54 | }
|
35 | 55 |
|
36 | 56 | if (target_os == "linux" || target_os == "android")
|
37 | 57 | && env::var_os("CARGO_FEATURE_NO_THP").is_some()
|
38 | 58 | {
|
39 |
| - build.define("MI_NO_THP", "1"); |
| 59 | + cmake_config.define("MI_NO_THP", "1"); |
40 | 60 | }
|
41 | 61 |
|
42 | 62 | if env::var_os("CARGO_FEATURE_DEBUG").is_some()
|
43 | 63 | || (env::var_os("CARGO_FEATURE_DEBUG_IN_DEBUG").is_some() && cfg!(debug_assertions))
|
44 | 64 | {
|
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"); |
47 | 67 | } else {
|
48 | 68 | // Remove heavy debug assertions etc
|
49 |
| - build.define("MI_DEBUG", "0"); |
| 69 | + cmake_config.define("MI_DEBUG_FULL", "OFF"); |
50 | 70 | }
|
51 | 71 |
|
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"); |
54 | 74 | }
|
55 | 75 |
|
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); |
57 | 81 |
|
58 | 82 | // on armv6 we need to link with libatomic
|
59 | 83 | if target_os == "linux" && target_arch == "arm" {
|
|
0 commit comments