@@ -19,12 +19,21 @@ fn main() {
1919 let target_env = env:: var ( "CARGO_CFG_TARGET_ENV" ) . expect ( "target_env not defined!" ) ;
2020 let profile = env:: var ( "PROFILE" ) . expect ( "profile not defined!" ) ;
2121
22+ if target_os == "windows" && target_env == "msvc" {
23+ build_mimalloc_win ( ) ;
24+ return ;
25+ }
26+
2227 if env:: var_os ( "CARGO_FEATURE_OVERRIDE" ) . is_some ( ) {
2328 cmake_config. define ( "MI_OVERRIDE" , "ON" ) ;
2429 } else {
2530 cmake_config. define ( "MI_OVERRIDE" , "OFF" ) ;
2631 }
2732
33+ if env:: var_os ( "CARGO_FEATURE_ASM" ) . is_some ( ) {
34+ cmake_config. define ( "MI_SEE_ASM" , "ON" ) ;
35+ }
36+
2837 if env:: var_os ( "CARGO_FEATURE_SKIP_COLLECT_ON_EXIT" ) . is_some ( ) {
2938 cmake_config. define ( "MI_SKIP_COLLECT_ON_EXIT" , "ON" ) ;
3039 }
@@ -42,8 +51,7 @@ fn main() {
4251 cmake_config. define ( "MI_OPT_ARCH" , "OFF" ) ;
4352 }
4453
45- // it's complicated to link ucrt in debug mode on windows
46- if profile == "debug" && target_env != "msvc" {
54+ if profile == "debug" {
4755 cmake_config
4856 . define ( "MI_DEBUG_FULL" , "ON" )
4957 . define ( "MI_SHOW_ERRORS" , "ON" ) ;
@@ -83,35 +91,9 @@ fn main() {
8391 cmake_config. define ( "MI_DEBUG_FULL" , "OFF" ) ;
8492 }
8593
86- if target_env == "msvc" {
87- cmake_config
88- . define ( "MI_USE_CXX" , "ON" )
89- // always turn off debug full and show errors on msvc
90- . define ( "MI_DEBUG_FULL" , "OFF" )
91- . define ( "MI_SHOW_ERRORS" , "OFF" )
92- . profile ( "release" )
93- . static_crt ( false ) ;
94- // Link with libs needed on Windows
95- // https://github.com/microsoft/mimalloc/blob/af21001f7a65eafb8fb16460b018ebf9d75e2ad8/CMakeLists.txt#L487
96- // https://github.com/rust-lang/rust/issues/139352
97- let libs = [ "psapi" , "shell32" , "user32" , "advapi32" , "bcrypt" ] ;
98-
99- for lib in libs {
100- println ! ( "cargo:rustc-link-lib={}" , lib) ;
101- }
102- }
103-
10494 let dst = cmake_config. build ( ) ;
10595
106- if target_os == "windows" {
107- println ! (
108- "cargo:rustc-link-search=native={}/build/Release" ,
109- dst. display( ) ,
110- ) ;
111- println ! ( "cargo:rustc-link-search=native={}/build" , dst. display( ) ) ;
112- } else {
113- println ! ( "cargo:rustc-link-search=native={}/build" , dst. display( ) ) ;
114- }
96+ println ! ( "cargo:rustc-link-search=native={}/build" , dst. display( ) ) ;
11597
11698 println ! ( "cargo:rustc-link-lib=static={}" , mimalloc_base_name) ;
11799
@@ -124,3 +106,68 @@ fn main() {
124106 println ! ( "cargo:rustc-link-lib={}" , atomic_name) ;
125107 }
126108}
109+
110+ fn build_mimalloc_win ( ) {
111+ use std:: env;
112+
113+ let features = env:: var ( "CARGO_CFG_TARGET_FEATURE" )
114+ . map ( |features| {
115+ features
116+ . split ( "," )
117+ . map ( |f| f. to_owned ( ) )
118+ . collect :: < Vec < String > > ( )
119+ } )
120+ . unwrap_or_default ( ) ;
121+
122+ let mut build = cc:: Build :: new ( ) ;
123+ let target_arch = env:: var ( "CARGO_CFG_TARGET_ARCH" ) . expect ( "target_arch not defined!" ) ;
124+
125+ build
126+ . include ( "./c_src/mimalloc/include" )
127+ . include ( "./c_src/mimalloc/src" )
128+ . file ( "./c_src/mimalloc/src/static.c" )
129+ . define ( "MI_BUILD_SHARED" , "0" )
130+ . cpp ( false )
131+ . warnings ( false )
132+ . flag_if_supported ( "-w" ) ;
133+
134+ if env:: var_os ( "CARGO_FEATURE_SECURE" ) . is_some ( ) {
135+ build. define ( "MI_SECURE" , "4" ) ;
136+ }
137+
138+ if env:: var_os ( "CARGO_FEATURE_ASM" ) . is_some ( ) {
139+ build. flag_if_supported ( "-save-temps" ) ;
140+ }
141+
142+ if env:: var_os ( "CARGO_FEATURE_NO_OPT_ARCH" ) . is_none ( ) && target_arch == "arm64" {
143+ build. flag_if_supported ( "/arch:armv8.1" ) ;
144+ }
145+
146+ if env:: var_os ( "CARGO_FEATURE_SKIP_COLLECT_ON_EXIT" ) . is_some ( ) {
147+ build. define ( "MI_SKIP_COLLECT_ON_EXIT" , "1" ) ;
148+ }
149+
150+ // Remove heavy debug assertions etc
151+ let profile = std:: env:: var ( "PROFILE" ) . unwrap ( ) ;
152+ match profile. as_str ( ) {
153+ "debug" => build. define ( "MI_DEBUG_FULL" , "3" ) ,
154+ "release" => build. define ( "MI_DEBUG_FULL" , "0" ) . define ( "MI_DEBUG" , "0" ) ,
155+ _ => build. define ( "MI_DEBUG_FULL" , "3" ) ,
156+ } ;
157+
158+ if build. get_compiler ( ) . is_like_msvc ( ) {
159+ build. cpp ( true ) ;
160+ }
161+
162+ const LIBS : [ & str ; 5 ] = [ "psapi" , "shell32" , "user32" , "advapi32" , "bcrypt" ] ;
163+
164+ for lib in LIBS {
165+ println ! ( "cargo:rustc-link-lib={}" , lib) ;
166+ }
167+
168+ if features. contains ( & "crt-static" . to_string ( ) ) {
169+ build. static_crt ( true ) ;
170+ }
171+
172+ build. compile ( "mimalloc_safe_static" ) ;
173+ }
0 commit comments