-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.rs
More file actions
278 lines (250 loc) · 11.6 KB
/
build.rs
File metadata and controls
278 lines (250 loc) · 11.6 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use std::env;
use std::process::Command;
/// Build script for auto-allocator
///
/// Automatically detects platform capabilities and validates mimalloc compatibility:
/// - Checks if mimalloc can compile (GCC version, stdatomic.h availability)
/// - Stops compilation on incompatible systems with clear error messages
/// - Provides upgrade guidance for legacy systems
fn main() {
println!("cargo:rerun-if-changed=build.rs");
validate_platform_compatibility();
}
/// Validates that the current platform can compile mimalloc
/// Stops compilation with clear error message if incompatible
fn validate_platform_compatibility() {
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default();
// Check if this is a debug or release build
let is_debug = env::var("DEBUG").unwrap_or_default() == "true";
match (target_os.as_str(), target_env.as_str(), target_arch.as_str()) {
// Linux systems need careful mimalloc compatibility checking
("linux", "gnu", _) => {
if !can_use_mimalloc_on_linux() {
print_compilation_error_and_exit();
} else {
println!("cargo:warning=Auto-allocator: Linux GNU platform detected");
if is_debug {
println!("cargo:warning= → Will use system allocator (debug build)");
} else {
println!("cargo:warning= → Will use mimalloc (release build)");
}
}
}
// Other Linux environments
("linux", "musl", _) => {
if !can_use_mimalloc_on_linux() {
print_compilation_error_and_exit();
} else {
println!("cargo:warning=Auto-allocator: Linux musl platform detected");
if is_debug {
println!("cargo:warning= → Will use system allocator (debug build)");
} else {
println!("cargo:warning= → Will use mimalloc (release build)");
}
}
}
// Non-Linux platforms - provide information only (actual selection happens at runtime)
_ => {
print_platform_info(target_os.as_str(), target_env.as_str(), target_arch.as_str(), is_debug);
}
}
}
/// Checks if mimalloc can be compiled on this Linux system
/// Returns false for old GCC versions (4.8.x) that lack stdatomic.h
fn can_use_mimalloc_on_linux() -> bool {
// First check if we have stdatomic.h available
if !has_stdatomic_header() {
return false;
}
// Check GCC version if available
if let Some(gcc_version) = get_gcc_version() {
if gcc_version < 49 { // GCC 4.9+ required for reliable stdatomic.h
return false;
}
}
true
}
/// Checks if stdatomic.h is available by trying to compile a test program
fn has_stdatomic_header() -> bool {
let test_program = r#"
#include <stdatomic.h>
int main() {
atomic_int x = ATOMIC_VAR_INIT(0);
return atomic_load(&x);
}
"#;
// Try to compile the test program
match std::process::Command::new("cc")
.arg("-c")
.arg("-o")
.arg("/dev/null")
.arg("-x")
.arg("c")
.arg("-")
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
{
Ok(mut child) => {
if let Some(mut stdin) = child.stdin.take() {
use std::io::Write;
let _ = stdin.write_all(test_program.as_bytes());
}
child.wait().map(|status| status.success()).unwrap_or(false)
}
Err(_) => false, // cc not available
}
}
/// Prints compilation error and exits the build process
fn print_compilation_error_and_exit() {
eprintln!();
eprintln!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
eprintln!("❌ AUTO-ALLOCATOR COMPILATION ERROR");
eprintln!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
eprintln!();
eprintln!("🚫 Cannot compile on this system: mimalloc requires modern GCC with stdatomic.h");
eprintln!();
if let Some(gcc_version) = get_gcc_version() {
eprintln!("📊 System Information:");
eprintln!(" • Detected GCC version: {}.x", gcc_version / 10);
eprintln!(" • Required GCC version: 4.9+");
eprintln!(" • stdatomic.h available: {}", if has_stdatomic_header() { "✅ Yes" } else { "❌ No" });
} else {
eprintln!("📊 System Information:");
eprintln!(" • GCC compiler: Not detected or not available");
eprintln!(" • stdatomic.h available: {}", if has_stdatomic_header() { "✅ Yes" } else { "❌ No" });
}
eprintln!();
eprintln!("ℹ️ Note: You may see additional 'libmimalloc-sys' compilation errors below.");
eprintln!(" This is expected - the same stdatomic.h issue affects mimalloc compilation.");
eprintln!();
eprintln!("💡 Solutions to fix this issue:");
eprintln!();
eprintln!(" 🔧 Option 1 - Upgrade GCC (Recommended):");
eprintln!(" CentOS 7: sudo yum install -y centos-release-scl devtoolset-11-gcc");
eprintln!(" Then: source /opt/rh/devtoolset-11/enable");
eprintln!(" Ubuntu: sudo apt-get install gcc-9");
eprintln!();
eprintln!(" 🔄 Option 2 - Upgrade OS:");
eprintln!(" CentOS 7 → CentOS 8+ / RHEL 8+ / Rocky Linux 8+");
eprintln!(" Ubuntu 16.04 → Ubuntu 18.04+");
eprintln!();
eprintln!(" ⚡ Option 3 - Use system allocator:");
eprintln!(" Remove auto-allocator and use Rust's default system allocator");
eprintln!(" (No performance benefits, but maximum compatibility)");
eprintln!();
eprintln!("🎯 Why this matters:");
eprintln!(" auto-allocator provides 1.6x faster allocation performance, but requires");
eprintln!(" modern compiler support. Legacy systems cannot benefit from this optimization.");
eprintln!();
eprintln!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
std::process::exit(1);
}
/// Prints platform information for non-Linux systems
/// Actual allocator selection happens at runtime in src/lib.rs
fn print_platform_info(target_os: &str, target_env: &str, target_arch: &str, is_debug: bool) {
// Check if this is an embedded platform (must match lib.rs is_embedded_target logic)
// Use target_os = "none" as the universal indicator for embedded/no_std environments
// This covers all current and future embedded architectures automatically
if target_os == "none" {
println!("cargo:warning=Auto-allocator: Embedded platform detected ({})", target_arch);
println!("cargo:warning= → Will use embedded-alloc for resource optimization");
return;
}
match (target_os, target_env, target_arch) {
// WASM
(_, _, "wasm32") => {
println!("cargo:warning=Auto-allocator: WASM platform detected");
println!("cargo:warning= → Will use system allocator for browser compatibility");
}
// Mobile platforms
("android", _, _) => {
println!("cargo:warning=Auto-allocator: Android platform detected");
println!("cargo:warning= → Will use system allocator (Scudo) per Android security policy");
}
("ios", _, _) => {
println!("cargo:warning=Auto-allocator: iOS platform detected");
println!("cargo:warning= → Will use system allocator (libmalloc) per Apple recommendations");
}
// BSD systems
("freebsd", _, _) | ("netbsd", _, _) => {
println!("cargo:warning=Auto-allocator: BSD platform detected ({})", target_os);
println!("cargo:warning= → Will use system allocator (native jemalloc)");
}
("openbsd", _, _) => {
println!("cargo:warning=Auto-allocator: OpenBSD platform detected");
println!("cargo:warning= → Will use system allocator (security-hardened)");
}
// Solaris systems
("solaris", _, _) | ("illumos", _, _) => {
println!("cargo:warning=Auto-allocator: Solaris platform detected ({})", target_os);
println!("cargo:warning= → Will use system allocator (libumem)");
}
// High-performance platforms that support mimalloc
("windows", "msvc", _) => {
println!("cargo:warning=Auto-allocator: Windows MSVC platform detected");
if is_debug {
println!("cargo:warning= → Will use system allocator (debug build)");
} else {
println!("cargo:warning= → Will use mimalloc (release build)");
}
}
("windows", "gnu", _) => {
println!("cargo:warning=Auto-allocator: Windows GNU platform detected");
if is_debug {
println!("cargo:warning= → Will use system allocator (debug build)");
} else {
println!("cargo:warning= → Will use mimalloc (release build)");
}
}
("macos", _, _) => {
println!("cargo:warning=Auto-allocator: macOS platform detected");
if is_debug {
println!("cargo:warning= → Will use system allocator (debug build)");
} else {
println!("cargo:warning= → Will use mimalloc (release build)");
}
}
// Unknown platforms - be conservative
_ => {
println!("cargo:warning=Auto-allocator: Unknown platform detected");
println!("cargo:warning= → Platform: {} env: {} arch: {}", target_os, target_env, target_arch);
println!("cargo:warning= → Will use system allocator (mimalloc not available on this platform)");
}
}
}
/// Attempts to detect GCC version by running the compiler
fn get_gcc_version() -> Option<u32> {
// Check if we're actually using GCC
if let Ok(cc) = env::var("CC") {
if !cc.contains("gcc") {
return None;
}
}
let gcc_cmd = env::var("CC").unwrap_or_else(|_| "gcc".to_string());
let output = Command::new(&gcc_cmd).arg("--version").output().ok()?;
let version_str = String::from_utf8(output.stdout).ok()?;
for line in version_str.lines() {
if line.to_lowercase().contains("gcc") {
for part in line.split_whitespace() {
if let Some(version) = parse_version_number(part) {
return Some(version);
}
}
}
}
None
}
/// Extracts major version number from version string (e.g., "7.5.0" -> 75)
fn parse_version_number(s: &str) -> Option<u32> {
let parts: Vec<&str> = s.split('.').collect();
if parts.len() >= 2 {
if let (Ok(major), Ok(minor)) = (parts[0].parse::<u32>(), parts[1].parse::<u32>()) {
return Some(major * 10 + minor);
}
}
parts.first()?.parse::<u32>().ok().map(|v| v * 10)
}