forked from datasetq/datasetq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
129 lines (116 loc) · 4.41 KB
/
build.rs
File metadata and controls
129 lines (116 loc) · 4.41 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
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
fn main() {
// Capture build information
capture_build_info();
// Only patch for WASM targets
let target = env::var("TARGET").unwrap();
if !target.contains("wasm32") {
return;
}
println!("cargo:rerun-if-changed=build.rs");
// Use cargo metadata to find the exact location of zstd-sys
if let Ok(output) = Command::new("cargo")
.args(["metadata", "--format-version", "1"])
.output()
{
if let Ok(metadata) = String::from_utf8(output.stdout) {
// Parse to find zstd-sys package
for line in metadata.lines() {
if line.contains("\"zstd-sys\"") && line.contains("\"manifest_path\"") {
// Extract path and patch
if let Some(start) = line.find("\":\"") {
if let Some(end) = line[start + 3..].find("\"") {
let manifest_path = &line[start + 3..start + 3 + end];
if let Some(parent) = PathBuf::from(manifest_path).parent() {
let shim_path = parent.join("wasm-shim/stdlib.h");
if shim_path.exists() {
patch_stdlib_h(&shim_path);
return;
}
}
}
}
}
}
}
}
// Fallback: search for zstd-sys in common locations
if let Ok(cargo_home) = env::var("CARGO_HOME") {
let registry = PathBuf::from(cargo_home).join("registry/src");
if let Ok(entries) = fs::read_dir(®istry) {
for entry in entries.flatten() {
let path = entry.path();
if let Ok(crates) = fs::read_dir(&path) {
for crate_entry in crates.flatten() {
let crate_path = crate_entry.path();
if let Some(crate_name) = crate_path.file_name() {
if crate_name.to_string_lossy().starts_with("zstd-sys-") {
let shim_path = crate_path.join("wasm-shim/stdlib.h");
if shim_path.exists() {
patch_stdlib_h(&shim_path);
return;
}
}
}
}
}
}
}
}
}
fn patch_stdlib_h(path: &PathBuf) {
if let Ok(content) = fs::read_to_string(path) {
// Check if already patched
if content.contains("qsort_r") {
return;
}
// Add qsort_r shim
let patched = content.replace(
"#endif // _STDLIB_H",
r#"
/* qsort_r shim for WASM - ignores context parameter */
#define qsort_r(base, nitems, size, compar, arg) \
rust_zstd_wasm_shim_qsort(base, nitems, size, (int (*)(const void*, const void*))compar)
#endif // _STDLIB_H"#,
);
if let Err(e) = fs::write(path, patched) {
eprintln!("Warning: Failed to patch zstd-sys wasm-shim: {e}");
}
}
}
fn capture_build_info() {
// Capture git hash
if let Ok(output) = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
{
if output.status.success() {
if let Ok(git_hash) = String::from_utf8(output.stdout) {
println!("cargo:rustc-env=GIT_HASH={}", git_hash.trim());
}
}
}
// Capture build date
if let Ok(output) = Command::new("date").args(["+%Y-%m-%d"]).output() {
if output.status.success() {
if let Ok(build_date) = String::from_utf8(output.stdout) {
println!("cargo:rustc-env=BUILD_DATE={}", build_date.trim());
}
}
}
// Capture rustc version
if let Ok(output) = Command::new("rustc").args(["--version"]).output() {
if output.status.success() {
if let Ok(rustc_version) = String::from_utf8(output.stdout) {
// Extract just version number
if let Some(version) = rustc_version.split_whitespace().nth(1) {
println!("cargo:rustc-env=RUSTC_VERSION={version}");
}
}
}
}
println!("cargo:rerun-if-changed=.git/HEAD");
}