Repository navigation
Expand file tree
/
Copy pathbuild.rs
More file actions
58 lines (50 loc) · 1.65 KB
/
Copy pathbuild.rs
File metadata and controls
58 lines (50 loc) · 1.65 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
use std::env;
use std::fs;
use std::path::Path;
use std::process::Command;
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=src/grammar.pest");
// Store the git hash for version information
let git_hash = get_git_hash();
if let Some(hash) = git_hash {
println!("cargo:rustc-env=GIT_HASH={}", hash);
}
// Set up platform-specific configurations
let target = env::var("TARGET").unwrap();
if target.contains("wasm32") {
println!("cargo:rustc-cfg=feature=\"wasm\"");
} else {
println!("cargo:rustc-cfg=feature=\"native\"");
}
// Set up OS-specific configurations
if target.contains("windows") {
println!("cargo:rustc-cfg=os=\"windows\"");
} else if target.contains("apple") {
println!("cargo:rustc-cfg=os=\"macos\"");
if target.contains("ios") {
println!("cargo:rustc-cfg=os=\"ios\"");
}
} else if target.contains("android") {
println!("cargo:rustc-cfg=os=\"android\"");
} else if target.contains("linux") {
println!("cargo:rustc-cfg=os=\"linux\"");
}
}
fn get_git_hash() -> Option<String> {
let output = Command::new("git")
.args(&["rev-parse", "--short", "HEAD"])
.output()
.ok()?;
if output.status.success() {
let hash = String::from_utf8(output.stdout).ok()?;
Some(hash.trim().to_string())
} else {
// If git command fails, try to read from a file
if Path::new("git_hash.txt").exists() {
fs::read_to_string("git_hash.txt").ok()
} else {
None
}
}
}