-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.rs
More file actions
76 lines (66 loc) · 2.42 KB
/
Copy pathbuild.rs
File metadata and controls
76 lines (66 loc) · 2.42 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
use std::ffi::OsStr;
fn compile_assembly(
input_path: &std::path::Path,
output_path: &std::path::Path,
) -> Result<(), Box<dyn std::error::Error>> {
#[cfg(target_os = "macos")]
const ASSEMBLER: &str = "aarch64-elf-as";
#[cfg(target_os = "macos")]
const LINKER: &str = "aarch64-elf-ld";
#[cfg(target_os = "macos")]
const OBJCOPY: &str = "aarch64-elf-objcopy";
#[cfg(target_os = "linux")]
const ASSEMBLER: &str = "aarch64-linux-gnu-as";
#[cfg(target_os = "linux")]
const LINKER: &str = "aarch64-linux-gnu-ld";
#[cfg(target_os = "linux")]
const OBJCOPY: &str = "aarch64-linux-gnu-objcopy";
let o_output_path = output_path.join("out.o");
let elf_output_path = output_path.join("out.elf");
let mut bin_output_path = input_path.to_path_buf();
bin_output_path.set_extension("bin");
let as_output = std::process::Command::new(ASSEMBLER)
.arg("-march=armv8.9-a")
.arg(input_path)
.arg("-o")
.arg(&o_output_path)
.output()
.map_err(|err| format!("Could not launch {ASSEMBLER}: {err}"))?;
if !as_output.status.success() {
return Err(format!("{ASSEMBLER} step failed:{:?}", as_output).into());
}
let ld_output = std::process::Command::new(LINKER)
.arg(&o_output_path)
.arg("-o")
.arg(&elf_output_path)
.output()
.map_err(|err| format!("Could not launch {LINKER}: {err}"))?;
if !ld_output.status.success() {
return Err(format!("{LINKER} step failed:{:?}", ld_output).into());
}
let objcopy_output = std::process::Command::new(OBJCOPY)
.arg("-O")
.arg("binary")
.arg(&elf_output_path)
.arg(bin_output_path)
.output()
.map_err(|err| format!("Could not launch {OBJCOPY}: {err}"))?;
if !objcopy_output.status.success() {
return Err(format!("{OBJCOPY} step failed:{:?}", objcopy_output).into());
}
std::fs::remove_file(&o_output_path)?;
std::fs::remove_file(&elf_output_path)?;
Ok(())
}
fn main() {
let mut entries = std::fs::read_dir("./tests/inputs")
.unwrap()
.map(|res| res.map(|e| e.path()))
.collect::<Result<Vec<_>, _>>()
.unwrap();
entries.retain(|p| p.extension() == Some(OsStr::new("S")));
for e in entries {
println!("cargo::rerun-if-changed={}", e.display());
compile_assembly(&e, std::path::Path::new("./tests/inputs")).unwrap();
}
}