|
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_STATIC", "ON") |
| 10 | + .define("MI_BUILD_OBJECT", "OFF") |
| 11 | + .define("MI_BUILD_SHARED", "OFF") |
| 12 | + .define("MI_BUILD_TESTS", "OFF"); |
9 | 13 |
|
10 | 14 | 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 | 15 | 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 | + } |
13 | 24 |
|
14 | 25 | 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"); |
20 | 47 | } |
21 | 48 |
|
22 | 49 | if env::var_os("CARGO_FEATURE_SECURE").is_some() { |
23 | | - build.define("MI_SECURE", "4"); |
| 50 | + cmake_config.define("MI_SECURE", "ON"); |
24 | 51 | } |
25 | 52 |
|
26 | 53 | let dynamic_tls = env::var("CARGO_FEATURE_LOCAL_DYNAMIC_TLS").is_ok(); |
27 | 54 |
|
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"); |
34 | 57 | } |
35 | 58 |
|
36 | 59 | if (target_os == "linux" || target_os == "android") |
37 | 60 | && env::var_os("CARGO_FEATURE_NO_THP").is_some() |
38 | 61 | { |
39 | | - build.define("MI_NO_THP", "1"); |
| 62 | + cmake_config.define("MI_NO_THP", "1"); |
40 | 63 | } |
41 | 64 |
|
42 | 65 | if env::var_os("CARGO_FEATURE_DEBUG").is_some() |
43 | 66 | || (env::var_os("CARGO_FEATURE_DEBUG_IN_DEBUG").is_some() && cfg!(debug_assertions)) |
44 | 67 | { |
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"); |
47 | 70 | } else { |
48 | 71 | // Remove heavy debug assertions etc |
49 | | - build.define("MI_DEBUG", "0"); |
| 72 | + cmake_config.define("MI_DEBUG_FULL", "OFF"); |
50 | 73 | } |
51 | 74 |
|
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"); |
54 | 77 | } |
55 | 78 |
|
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 | + } |
57 | 87 |
|
58 | 88 | // on armv6 we need to link with libatomic |
59 | 89 | if target_os == "linux" && target_arch == "arm" { |
|
0 commit comments