|
1 | 1 | use std::env; |
2 | 2 |
|
| 3 | +use cmake::Config; |
| 4 | + |
3 | 5 | fn main() { |
4 | | - let mut build = cc::Build::new(); |
| 6 | + let mut cmake_config = Config::new("c_src/mimalloc"); |
5 | 7 |
|
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"); |
9 | 11 |
|
10 | 12 | 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 | 13 | 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 | + } |
13 | 22 |
|
14 | 23 | 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"); |
20 | 45 | } |
21 | 46 |
|
22 | 47 | if env::var_os("CARGO_FEATURE_SECURE").is_some() { |
23 | | - build.define("MI_SECURE", "4"); |
| 48 | + cmake_config.define("MI_SECURE", "ON"); |
24 | 49 | } |
25 | 50 |
|
26 | 51 | let dynamic_tls = env::var("CARGO_FEATURE_LOCAL_DYNAMIC_TLS").is_ok(); |
27 | 52 |
|
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"); |
34 | 55 | } |
35 | 56 |
|
36 | 57 | if (target_os == "linux" || target_os == "android") |
37 | 58 | && env::var_os("CARGO_FEATURE_NO_THP").is_some() |
38 | 59 | { |
39 | | - build.define("MI_NO_THP", "1"); |
| 60 | + cmake_config.define("MI_NO_THP", "1"); |
40 | 61 | } |
41 | 62 |
|
42 | 63 | if env::var_os("CARGO_FEATURE_DEBUG").is_some() |
43 | 64 | || (env::var_os("CARGO_FEATURE_DEBUG_IN_DEBUG").is_some() && cfg!(debug_assertions)) |
44 | 65 | { |
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"); |
47 | 68 | } else { |
48 | 69 | // Remove heavy debug assertions etc |
49 | | - build.define("MI_DEBUG", "0"); |
| 70 | + cmake_config.define("MI_DEBUG_FULL", "OFF"); |
50 | 71 | } |
51 | 72 |
|
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"); |
54 | 75 | } |
55 | 76 |
|
56 | | - build.compile("mimalloc"); |
| 77 | + cmake_config.build(); |
57 | 78 |
|
58 | 79 | // on armv6 we need to link with libatomic |
59 | 80 | if target_os == "linux" && target_arch == "arm" { |
|
0 commit comments