|
| 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 | + } 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)); |
20 | 34 | }
|
21 | 35 |
|
22 | 36 | 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"); |
24 | 54 | }
|
25 | 55 |
|
26 | 56 | let dynamic_tls = env::var("CARGO_FEATURE_LOCAL_DYNAMIC_TLS").is_ok();
|
27 | 57 |
|
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"); |
34 | 60 | }
|
35 | 61 |
|
36 | 62 | if (target_os == "linux" || target_os == "android")
|
37 | 63 | && env::var_os("CARGO_FEATURE_NO_THP").is_some()
|
38 | 64 | {
|
39 |
| - build.define("MI_NO_THP", "1"); |
| 65 | + cmake_config.define("MI_NO_THP", "1"); |
40 | 66 | }
|
41 | 67 |
|
42 | 68 | if env::var_os("CARGO_FEATURE_DEBUG").is_some()
|
43 | 69 | || (env::var_os("CARGO_FEATURE_DEBUG_IN_DEBUG").is_some() && cfg!(debug_assertions))
|
44 | 70 | {
|
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"); |
47 | 73 | } else {
|
48 | 74 | // Remove heavy debug assertions etc
|
49 |
| - build.define("MI_DEBUG", "0"); |
| 75 | + cmake_config.define("MI_DEBUG_FULL", "OFF"); |
50 | 76 | }
|
51 | 77 |
|
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()); |
54 | 101 | }
|
55 | 102 |
|
56 |
| - build.compile("mimalloc"); |
| 103 | + println!("cargo:rustc-link-lib=static={}", mimalloc_base_name); |
57 | 104 |
|
58 | 105 | // on armv6 we need to link with libatomic
|
59 | 106 | if target_os == "linux" && target_arch == "arm" {
|
|
0 commit comments