-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
108 lines (93 loc) · 3.4 KB
/
build.rs
File metadata and controls
108 lines (93 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
fn main() {
// === Target Information (for cargo build, zigbuild, cargo-xwin) ===
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
//let target_env = std::env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default();
// let target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
// === Feature Flags ===
let use_snmalloc = std::env::var("CARGO_FEATURE_USE_SNMALLOC").is_ok()
|| std::env::var("CARGO_FEATURE_USE_SNMALLOC_CC").is_ok()
|| std::env::var("CARGO_FEATURE_USE_SNMALLOC_STATIC_LINK").is_ok();
let use_tcmalloc_static = std::env::var("CARGO_FEATURE_USE_TCMALLOC_STATIC").is_ok();
let use_mimalloc = std::env::var("CARGO_FEATURE_USE_MIMALLOC").is_ok();
// === Dispatch ===
match target_os.as_str() {
//"linux" => handle_linux(&target_env, use_snmalloc, use_tcmalloc_static, use_mimalloc),
"linux" => handle_linux(use_snmalloc, use_tcmalloc_static, use_mimalloc),
"windows" => handle_windows(use_snmalloc),
_ => {} // ignore other platforms
}
}
// === Linux-specific linker options ===
fn handle_linux(
//target_env: &str,
use_snmalloc: bool,
use_tcmalloc_static: bool,
use_mimalloc: bool,
) {
if use_tcmalloc_static {
link_tcmalloc_static();
} else if use_snmalloc {
link_snmalloc_linux();
} else if use_mimalloc {
link_mimalloc_linux();
}
}
// === Windows-specific linker options ===
fn handle_windows(use_snmalloc: bool) {
if use_snmalloc {
link_snmalloc_windows();
} else {
link_mimalloc_windows();
}
}
// === Linking patterns ===
fn link_tcmalloc_static() {
// lzma.a (pacman-static)
// println!("cargo:rustc-link-search=native={}", "/usr/lib/pacman/lib");
// println!("cargo:rustc-link-lib=static=lzma");
// libunwind.a (rust-musl or rust-aarch64-musl)
// println!("cargo:rustc-link-search=native={}{}{}", "/usr/lib/rustlib/", target_arch, "-unknown-linux-musl/lib/self-contained/");
// println!("cargo:rustc-link-lib=static=unwind");
// Static link
{
let lib = "tcmalloc_minimal";
println!("cargo:rustc-link-lib=static={}", lib);
}
//for lib in ["stdc++", "unwind", "lzma"] {
{
let lib = "stdc++";
//for lib in ["stdc++"] {
println!("cargo:rustc-link-lib={}", lib);
}
}
fn link_snmalloc_linux() {
// snmalloc (for Linux)
/*
let static_link = std::env::var("CARGO_FEATURE_USE_SNMALLOC_STATIC_LINK").is_ok();
//if target_env == "musl" {
if static_link {
println!("cargo:rustc-link-lib=static=snmallocshim-rust");
} else {
// println!("cargo:rustc-link-lib=static=snmallocshim-rust");
println!("cargo:rustc-link-lib=snmallocshim-rust");
}
*/
}
fn link_snmalloc_windows() {
// snmalloc for Windows
println!("cargo:rustc-link-arg=/nodefaultlib:libucrt.lib");
for lib in ["kernel32", "ntdll", "shell32", "user32", "advapi32", "ucrt"] {
println!("cargo:rustc-link-lib={}", lib);
}
}
fn link_mimalloc_windows() {
// Ref: https://github.com/microsoft/mimalloc/blob/af21001f7a65eafb8fb16460b018ebf9d75e2ad8/CMakeLists.txt#L487
for lib in ["psapi", "shell32", "user32", "advapi32", "bcrypt"] {
println!("cargo:rustc-link-lib={}", lib);
}
}
fn link_mimalloc_linux() {
//for lib in ["atomic"] {
// println!("cargo:rustc-link-lib={}", lib);
//}
}