-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.rs
More file actions
52 lines (45 loc) · 1.87 KB
/
build.rs
File metadata and controls
52 lines (45 loc) · 1.87 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
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
// When building a binary, Rust sets $TARGET to the architecture. Use this to store the binary's
// architecture within the binary. Used to check the host machine native architecture.
fn generate_typed_host_arch() {
let mut target = std::env::var("TARGET").expect("Expected TARGET to be set");
let out_dir = PathBuf::from(std::env::var("OUT_DIR").expect("Expected OUT_DIR to be set"));
// Remove libc reference; does not matter for libc-agnostic bpt architecture description.
for libc in ["gnu", "musl", "uclibc"] {
if let Some(start) = target.find(libc) {
target.replace_range(start..start + libc.len(), "*")
}
}
// Extract libc-agnostic bpt name for architecture
let target = match target.as_str() {
// Special case targets with multiple ABI variations
"arm-unknown-linux-*eabi" => "armv7l",
"arm-unknown-linux-*eabihf" => "armv7hl",
// Common case
_ => target
.split('-')
.next()
.expect("Expected TARGET to include `-`"),
};
// Save untyped, raw string
let path: PathBuf = [&out_dir, Path::new("host-arch")].into_iter().collect();
File::create(path)
.expect("Expected to be able to open host arch file")
.write_all(target.as_bytes())
.expect("Expected to be able to write host arch file");
// Prefix enum type
let target = ["crate::metadata::Arch::", target]
.into_iter()
.collect::<String>();
// Save typed string
let path: PathBuf = [&out_dir, Path::new("host-arch.rs")].into_iter().collect();
File::create(path)
.expect("Expected to be able to open host arch file")
.write_all(target.as_bytes())
.expect("Expected to be able to write host arch file")
}
fn main() {
generate_typed_host_arch();
}