forked from M-Labs/nac3
1
0
Fork 0
nac3/nac3core/build.rs

135 lines
4.0 KiB
Rust
Raw Normal View History

use regex::Regex;
use std::{
env,
2022-03-22 15:39:15 +08:00
fs::File,
io::Write,
2022-01-09 12:06:45 +08:00
path::Path,
process::{Command, Stdio},
};
2024-07-08 14:16:35 +08:00
fn compile_irrt(irrt_dir: &Path, out_dir: &Path) {
let irrt_cpp_path = irrt_dir.join("irrt.cpp");
/*
* HACK: Sadly, clang doesn't let us emit generic LLVM bitcode.
* Compiling for WASM32 and filtering the output with regex is the closest we can get.
*/
let flags: &[&str] = &[
"--target=wasm32",
2024-07-08 14:16:35 +08:00
irrt_cpp_path.to_str().unwrap(),
2024-07-09 13:31:29 +08:00
"-x",
"c++",
"-fno-discard-value-names",
2024-07-05 17:24:57 +08:00
"-fno-exceptions",
"-fno-rtti",
match env::var("PROFILE").as_deref() {
Ok("debug") => "-O0",
Ok("release") => "-O3",
2024-03-11 14:25:37 +08:00
flavor => panic!("Unknown or missing build flavor {flavor:?}"),
},
"-emit-llvm",
"-S",
"-Wall",
"-Wextra",
2024-07-10 10:17:43 +08:00
"-Werror=return-type",
2024-07-08 14:16:35 +08:00
"-I",
irrt_dir.to_str().unwrap(),
"-o",
"-",
];
2023-12-08 17:43:32 +08:00
println!("cargo:rerun-if-changed={}", out_dir.to_str().unwrap());
2023-12-08 17:43:32 +08:00
let output = Command::new("clang-irrt")
.args(flags)
.output()
.map(|o| {
assert!(o.status.success(), "{}", std::str::from_utf8(&o.stderr).unwrap());
o
})
.unwrap();
// https://github.com/rust-lang/regex/issues/244
let output = std::str::from_utf8(&output.stdout).unwrap().replace("\r\n", "\n");
let mut filtered_output = String::with_capacity(output.len());
// (?ms:^define.*?\}$) to capture `define` blocks
// (?m:^declare.*?$) to capture `declare` blocks
// (?m:^%.+?=\s*type\s*\{.+?\}$) to capture `type` declarations
let regex_filter =
Regex::new(r"(?ms:^define.*?\}$)|(?m:^declare.*?$)|(?m:^%.+?=\s*type\s*\{.+?\}$)").unwrap();
for f in regex_filter.captures_iter(&output) {
assert_eq!(f.len(), 1);
filtered_output.push_str(&f[0]);
filtered_output.push('\n');
}
let filtered_output = Regex::new("(#\\d+)|(, *![0-9A-Za-z.]+)|(![0-9A-Za-z.]+)|(!\".*?\")")
.unwrap()
.replace_all(&filtered_output, "");
2022-03-22 15:39:15 +08:00
println!("cargo:rerun-if-env-changed=DEBUG_DUMP_IRRT");
if env::var("DEBUG_DUMP_IRRT").is_ok() {
2024-07-08 14:16:35 +08:00
let mut file = File::create(out_dir.join("irrt.ll")).unwrap();
2022-03-22 15:39:15 +08:00
file.write_all(output.as_bytes()).unwrap();
2024-07-08 14:16:35 +08:00
let mut file = File::create(out_dir.join("irrt-filtered.ll")).unwrap();
2022-03-22 15:39:15 +08:00
file.write_all(filtered_output.as_bytes()).unwrap();
}
2023-11-25 20:15:29 +08:00
let mut llvm_as = Command::new("llvm-as-irrt")
.stdin(Stdio::piped())
.arg("-o")
2024-07-08 14:16:35 +08:00
.arg(out_dir.join("irrt.bc"))
.spawn()
.unwrap();
llvm_as.stdin.as_mut().unwrap().write_all(filtered_output.as_bytes()).unwrap();
2023-12-08 17:43:32 +08:00
assert!(llvm_as.wait().unwrap().success());
}
2024-07-08 14:16:35 +08:00
fn compile_irrt_test(irrt_dir: &Path, out_dir: &Path) {
let irrt_test_cpp_path = irrt_dir.join("irrt_test.cpp");
let exe_path = out_dir.join("irrt_test.out");
let flags: &[&str] = &[
irrt_test_cpp_path.to_str().unwrap(),
"-x",
"c++",
"-I",
irrt_dir.to_str().unwrap(),
"-g",
"-fno-discard-value-names",
"-O0",
"-Wall",
"-Wextra",
2024-07-10 10:17:43 +08:00
"-Werror=return-type",
2024-07-08 14:16:35 +08:00
"-lm", // for `tgamma()`, `lgamma()`
"-o",
exe_path.to_str().unwrap(),
];
Command::new("clang-irrt-test")
.args(flags)
.output()
.map(|o| {
assert!(o.status.success(), "{}", std::str::from_utf8(&o.stderr).unwrap());
o
})
.unwrap();
println!("cargo:rerun-if-changed={}", out_dir.to_str().unwrap());
2024-07-08 14:16:35 +08:00
}
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let out_dir = Path::new(&out_dir);
let irrt_dir = Path::new("./irrt");
2024-07-08 14:16:35 +08:00
compile_irrt(irrt_dir, out_dir);
// https://github.com/rust-lang/cargo/issues/2549
// `cargo test -F test` to also build `irrt_test.cpp
if cfg!(feature = "test") {
compile_irrt_test(irrt_dir, out_dir);
}
}