2022-01-08 22:16:55 +08:00
|
|
|
use regex::Regex;
|
|
|
|
use std::{
|
|
|
|
env,
|
2022-03-22 15:39:15 +08:00
|
|
|
fs::File,
|
2022-01-08 22:16:55 +08:00
|
|
|
io::Write,
|
2022-01-09 12:06:45 +08:00
|
|
|
path::Path,
|
2022-01-08 22:16:55 +08:00
|
|
|
process::{Command, Stdio},
|
|
|
|
};
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
const FILE: &str = "src/codegen/irrt/irrt.c";
|
2022-01-09 10:53:58 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2024-03-06 12:57:20 +08:00
|
|
|
let flags: &[&str] = &[
|
2022-01-09 10:53:58 +08:00
|
|
|
"--target=wasm32",
|
2022-01-08 22:16:55 +08:00
|
|
|
FILE,
|
2023-11-22 13:35:56 +08:00
|
|
|
"-fno-discard-value-names",
|
2024-03-06 12:57:20 +08:00
|
|
|
match env::var("PROFILE").as_deref() {
|
|
|
|
Ok("debug") => "-O0",
|
|
|
|
Ok("release") => "-O3",
|
|
|
|
flavor => panic!("Unknown or missing build flavor {:?}", flavor),
|
|
|
|
},
|
2022-01-08 22:16:55 +08:00
|
|
|
"-emit-llvm",
|
|
|
|
"-S",
|
|
|
|
"-Wall",
|
|
|
|
"-Wextra",
|
|
|
|
"-o",
|
|
|
|
"-",
|
|
|
|
];
|
2023-12-08 17:43:32 +08:00
|
|
|
|
|
|
|
println!("cargo:rerun-if-changed={FILE}");
|
|
|
|
let out_dir = env::var("OUT_DIR").unwrap();
|
|
|
|
let out_path = Path::new(&out_dir);
|
|
|
|
|
2023-11-25 20:15:29 +08:00
|
|
|
let output = Command::new("clang-irrt")
|
2024-03-06 12:57:20 +08:00
|
|
|
.args(flags)
|
2022-01-08 22:16:55 +08:00
|
|
|
.output()
|
|
|
|
.map(|o| {
|
|
|
|
assert!(o.status.success(), "{}", std::str::from_utf8(&o.stderr).unwrap());
|
|
|
|
o
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
2022-03-22 16:04:48 +08:00
|
|
|
// https://github.com/rust-lang/regex/issues/244
|
|
|
|
let output = std::str::from_utf8(&output.stdout).unwrap().replace("\r\n", "\n");
|
2022-01-08 22:16:55 +08:00
|
|
|
let mut filtered_output = String::with_capacity(output.len());
|
|
|
|
|
2023-10-26 13:52:40 +08:00
|
|
|
let regex_filter = Regex::new(r"(?ms:^define.*?\}$)|(?m:^declare.*?$)").unwrap();
|
2022-03-22 16:04:48 +08:00
|
|
|
for f in regex_filter.captures_iter(&output) {
|
2023-12-06 11:05:42 +08:00
|
|
|
assert_eq!(f.len(), 1);
|
2022-01-08 22:16:55 +08:00
|
|
|
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() {
|
|
|
|
let mut file = File::create(out_path.join("irrt.ll")).unwrap();
|
|
|
|
file.write_all(output.as_bytes()).unwrap();
|
|
|
|
let mut file = File::create(out_path.join("irrt-filtered.ll")).unwrap();
|
|
|
|
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")
|
2022-01-08 22:16:55 +08:00
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.arg("-o")
|
2022-03-22 15:39:15 +08:00
|
|
|
.arg(out_path.join("irrt.bc"))
|
2022-01-08 22:16:55 +08:00
|
|
|
.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());
|
2022-01-08 22:16:55 +08:00
|
|
|
}
|