Skip to content

Commit f8809c4

Browse files
committed
msvc
1 parent 6ff9477 commit f8809c4

File tree

3 files changed

+66
-30
lines changed

3 files changed

+66
-30
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ travis-ci = { repository = "purpleprotocol/mimalloc_rust" }
2525
libmimalloc-sys2 = { path = "libmimalloc-sys", version = "0.1.47", default-features = false }
2626

2727
[features]
28+
asm = ["libmimalloc-sys2/asm"]
2829
default = []
2930
secure = ["libmimalloc-sys2/secure"]
3031
override = ["libmimalloc-sys2/override"]

libmimalloc-sys/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ cty = { version = "0.2", optional = true }
2222
libc = "0.2"
2323

2424
[build-dependencies]
25+
cc = "1.0"
2526
cmake = "0.1"
2627

2728
[features]
2829
# For debug purposes
2930
etw = []
30-
31+
asm = []
3132
secure = []
3233
override = []
3334
extended = ["cty"]

libmimalloc-sys/build.rs

Lines changed: 63 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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,55 @@ 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 mut build = cc::Build::new();
114+
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("target_arch not defined!");
115+
116+
build
117+
.include("./c_src/mimalloc/include")
118+
.include("./c_src/mimalloc/src")
119+
.file("./c_src/mimalloc/src/static.c")
120+
.define("MI_BUILD_SHARED", "0")
121+
.cpp(false)
122+
.warnings(false)
123+
.flag_if_supported("-w");
124+
125+
if env::var_os("CARGO_FEATURE_SECURE").is_some() {
126+
build.define("MI_SECURE", "4");
127+
}
128+
129+
if env::var_os("CARGO_FEATURE_ASM").is_some() {
130+
build.flag_if_supported("-save-temps");
131+
}
132+
133+
if env::var_os("CARGO_FEATURE_NO_OPT_ARCH").is_none() && target_arch == "arm64" {
134+
build.flag_if_supported("/arch:armv8.1");
135+
}
136+
137+
if env::var_os("CARGO_FEATURE_SKIP_COLLECT_ON_EXIT").is_some() {
138+
build.define("MI_SKIP_COLLECT_ON_EXIT", "1");
139+
}
140+
141+
// Remove heavy debug assertions etc
142+
let profile = std::env::var("PROFILE").unwrap();
143+
match profile.as_str() {
144+
"debug" => build.define("MI_DEBUG_FULL", "3"),
145+
"release" => build.define("MI_DEBUG_FULL", "0").define("MI_DEBUG", "0"),
146+
_ => build.define("MI_DEBUG_FULL", "3"),
147+
};
148+
149+
if build.get_compiler().is_like_msvc() {
150+
build.cpp(true);
151+
}
152+
153+
const LIBS: [&str; 5] = ["psapi", "shell32", "user32", "advapi32", "bcrypt"];
154+
155+
for lib in LIBS {
156+
println!("cargo:rustc-link-lib={}", lib);
157+
}
158+
159+
build.compile("mimalloc_safe_static");
160+
}

0 commit comments

Comments
 (0)