Skip to content

Commit 09a0127

Browse files
authored
Merge pull request #16 from athre0z/fix-opt-level
Mirror cmake-rs build type selection logic
2 parents a4a688c + 71775df commit 09a0127

File tree

1 file changed

+71
-3
lines changed

1 file changed

+71
-3
lines changed

libmimalloc-sys/build.rs

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,64 @@
11
use cmake::Config;
2+
use std::env;
3+
4+
enum CMakeBuildType {
5+
Debug,
6+
Release,
7+
RelWithDebInfo,
8+
MinSizeRel,
9+
}
10+
11+
/// Determine the CMake build type that will be picked by `cmake-rs`.
12+
///
13+
/// This is mostly pasted from `cmake-rs`:
14+
/// https://github.com/alexcrichton/cmake-rs/blob/7f85e323/src/lib.rs#L498
15+
fn get_cmake_build_type() -> Result<CMakeBuildType, String> {
16+
fn getenv(v: &str) -> Result<String, String> {
17+
env::var(v).map_err(|_| format!("environment variable `{}` not defined", v))
18+
}
19+
20+
// Determine Rust's profile, optimization level, and debug info:
21+
#[derive(PartialEq)]
22+
enum RustProfile {
23+
Debug,
24+
Release,
25+
}
26+
#[derive(PartialEq, Debug)]
27+
enum OptLevel {
28+
Debug,
29+
Release,
30+
Size,
31+
}
32+
33+
let rust_profile = match getenv("PROFILE")?.as_str() {
34+
"debug" => RustProfile::Debug,
35+
"release" | "bench" => RustProfile::Release,
36+
_ => RustProfile::Release,
37+
};
38+
39+
let opt_level = match getenv("OPT_LEVEL")?.as_str() {
40+
"0" => OptLevel::Debug,
41+
"1" | "2" | "3" => OptLevel::Release,
42+
"s" | "z" => OptLevel::Size,
43+
_ => match rust_profile {
44+
RustProfile::Debug => OptLevel::Debug,
45+
RustProfile::Release => OptLevel::Release,
46+
},
47+
};
48+
49+
let debug_info: bool = match getenv("DEBUG")?.as_str() {
50+
"false" => false,
51+
"true" => true,
52+
_ => true,
53+
};
54+
55+
Ok(match (opt_level, debug_info) {
56+
(OptLevel::Debug, _) => CMakeBuildType::Debug,
57+
(OptLevel::Release, false) => CMakeBuildType::Release,
58+
(OptLevel::Release, true) => CMakeBuildType::RelWithDebInfo,
59+
(OptLevel::Size, _) => CMakeBuildType::MinSizeRel,
60+
})
61+
}
262

363
fn main() {
464
let mut cfg = &mut Config::new("c_src/mimalloc");
@@ -20,13 +80,21 @@ fn main() {
2080
// This set mi_option_verbose and mi_option_show_errors options to false.
2181
cfg = cfg.define("mi_defines", "MI_DEBUG=0");
2282

83+
let is_debug = match get_cmake_build_type() {
84+
Ok(CMakeBuildType::Debug) => true,
85+
Ok(CMakeBuildType::Release) => false,
86+
Ok(CMakeBuildType::RelWithDebInfo) => false,
87+
Ok(CMakeBuildType::MinSizeRel) => false,
88+
Err(e) => panic!("Cannot determine CMake build type: {}", e),
89+
};
90+
2391
if cfg!(all(windows, target_env = "msvc")) {
2492
cfg = cfg.define("CMAKE_SH", "CMAKE_SH-NOTFOUND");
2593

2694
// cc::get_compiler have /nologo /MD default flags that are cmake::Config
2795
// defaults to. Those flags prevents mimalloc from building on windows
2896
// extracted from default cmake configuration on windows
29-
if cfg!(debug_assertions) {
97+
if is_debug {
3098
// CMAKE_C_FLAGS + CMAKE_C_FLAGS_DEBUG
3199
cfg = cfg.cflag("/DWIN32 /D_WINDOWS /W3 /MDd /Zi /Ob0 /Od /RTC1");
32100
} else {
@@ -36,7 +104,7 @@ fn main() {
36104
}
37105

38106
let (out_dir, out_name) = if cfg!(all(windows, target_env = "msvc")) {
39-
if cfg!(debug_assertions) {
107+
if is_debug {
40108
if cfg!(feature = "secure") {
41109
("./build/Debug", "mimalloc-static-secure-debug")
42110
} else {
@@ -50,7 +118,7 @@ fn main() {
50118
}
51119
}
52120
} else {
53-
if cfg!(debug_assertions) {
121+
if is_debug {
54122
if cfg!(feature = "secure") {
55123
("./build", "mimalloc-secure-debug")
56124
} else {

0 commit comments

Comments
 (0)