1
1
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
+ }
2
62
3
63
fn main ( ) {
4
64
let mut cfg = & mut Config :: new ( "c_src/mimalloc" ) ;
@@ -20,13 +80,21 @@ fn main() {
20
80
// This set mi_option_verbose and mi_option_show_errors options to false.
21
81
cfg = cfg. define ( "mi_defines" , "MI_DEBUG=0" ) ;
22
82
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
+
23
91
if cfg ! ( all( windows, target_env = "msvc" ) ) {
24
92
cfg = cfg. define ( "CMAKE_SH" , "CMAKE_SH-NOTFOUND" ) ;
25
93
26
94
// cc::get_compiler have /nologo /MD default flags that are cmake::Config
27
95
// defaults to. Those flags prevents mimalloc from building on windows
28
96
// extracted from default cmake configuration on windows
29
- if cfg ! ( debug_assertions ) {
97
+ if is_debug {
30
98
// CMAKE_C_FLAGS + CMAKE_C_FLAGS_DEBUG
31
99
cfg = cfg. cflag ( "/DWIN32 /D_WINDOWS /W3 /MDd /Zi /Ob0 /Od /RTC1" ) ;
32
100
} else {
@@ -36,7 +104,7 @@ fn main() {
36
104
}
37
105
38
106
let ( out_dir, out_name) = if cfg ! ( all( windows, target_env = "msvc" ) ) {
39
- if cfg ! ( debug_assertions ) {
107
+ if is_debug {
40
108
if cfg ! ( feature = "secure" ) {
41
109
( "./build/Debug" , "mimalloc-static-secure-debug" )
42
110
} else {
@@ -50,7 +118,7 @@ fn main() {
50
118
}
51
119
}
52
120
} else {
53
- if cfg ! ( debug_assertions ) {
121
+ if is_debug {
54
122
if cfg ! ( feature = "secure" ) {
55
123
( "./build" , "mimalloc-secure-debug" )
56
124
} else {
0 commit comments