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,
|
2024-07-12 22:29:04 +08:00
|
|
|
path::{Path, PathBuf},
|
2022-01-08 22:16:55 +08:00
|
|
|
process::{Command, Stdio},
|
|
|
|
};
|
|
|
|
|
2024-07-12 22:29:04 +08:00
|
|
|
const CMD_IRRT_CLANG: &str = "clang-irrt";
|
|
|
|
const CMD_IRRT_CLANG_TEST: &str = "clang-irrt-test";
|
|
|
|
const CMD_IRRT_LLVM_AS: &str = "llvm-as-irrt";
|
2024-07-12 21:52:55 +08:00
|
|
|
|
2024-07-12 22:29:04 +08:00
|
|
|
fn get_out_dir() -> PathBuf {
|
|
|
|
PathBuf::from(env::var("OUT_DIR").unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_irrt_dir() -> &'static Path {
|
|
|
|
Path::new("irrt")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compile `irrt.cpp` for use in `src/codegen`
|
|
|
|
fn compile_irrt_cpp() {
|
|
|
|
let out_dir = get_out_dir();
|
|
|
|
let irrt_dir = get_irrt_dir();
|
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-07-12 22:29:04 +08:00
|
|
|
let irrt_cpp_path = irrt_dir.join("irrt.cpp");
|
2024-03-06 12:57:20 +08:00
|
|
|
let flags: &[&str] = &[
|
2022-01-09 10:53:58 +08:00
|
|
|
"--target=wasm32",
|
2024-07-09 13:31:29 +08:00
|
|
|
"-x",
|
|
|
|
"c++",
|
2023-11-22 13:35:56 +08:00
|
|
|
"-fno-discard-value-names",
|
2024-07-05 17:24:57 +08:00
|
|
|
"-fno-exceptions",
|
|
|
|
"-fno-rtti",
|
2024-03-06 12:57:20 +08:00
|
|
|
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:?}"),
|
2024-03-06 12:57:20 +08:00
|
|
|
},
|
2022-01-08 22:16:55 +08:00
|
|
|
"-emit-llvm",
|
|
|
|
"-S",
|
|
|
|
"-Wall",
|
|
|
|
"-Wextra",
|
2024-07-12 20:34:06 +08:00
|
|
|
"-Werror=return-type",
|
2024-07-14 17:09:48 +08:00
|
|
|
"-I",
|
|
|
|
irrt_dir.to_str().unwrap(),
|
2022-01-08 22:16:55 +08:00
|
|
|
"-o",
|
|
|
|
"-",
|
2024-07-12 21:52:55 +08:00
|
|
|
irrt_cpp_path.to_str().unwrap(),
|
2022-01-08 22:16:55 +08:00
|
|
|
];
|
2023-12-08 17:43:32 +08:00
|
|
|
|
2024-07-12 21:52:55 +08:00
|
|
|
// Tell Cargo to rerun if any file under `irrt_dir` (recursive) changes
|
|
|
|
println!("cargo:rerun-if-changed={}", irrt_dir.to_str().unwrap());
|
2023-12-08 17:43:32 +08:00
|
|
|
|
2024-07-12 21:52:55 +08:00
|
|
|
// Compile IRRT and capture the LLVM IR output
|
2024-07-12 22:29:04 +08:00
|
|
|
let output = Command::new(CMD_IRRT_CLANG)
|
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());
|
|
|
|
|
2024-07-12 21:52:55 +08:00
|
|
|
// Filter out irrelevant IR
|
|
|
|
//
|
|
|
|
// Regex:
|
|
|
|
// - `(?ms:^define.*?\}$)` captures LLVM `define` blocks
|
|
|
|
// - `(?m:^declare.*?$)` captures LLVM `declare` lines
|
2024-07-09 21:02:20 +08:00
|
|
|
// - `(?m:^%.+?=\s*type\s*\{.+?\}$)` captures LLVM `type` declarations
|
2024-07-13 14:02:50 +08:00
|
|
|
// - `(?m:^@.+?=.+$)` captures global constants
|
|
|
|
let regex_filter = Regex::new(
|
|
|
|
r"(?ms:^define.*?\}$)|(?m:^declare.*?$)|(?m:^%.+?=\s*type\s*\{.+?\}$)|(?m:^@.+?=.+$)",
|
|
|
|
)
|
|
|
|
.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, "");
|
|
|
|
|
2024-07-12 21:52:55 +08:00
|
|
|
// For debugging
|
|
|
|
// Doing `DEBUG_DUMP_IRRT=1 cargo build -p nac3core` dumps the LLVM IR generated
|
|
|
|
const DEBUG_DUMP_IRRT: &str = "DEBUG_DUMP_IRRT";
|
|
|
|
println!("cargo:rerun-if-env-changed={DEBUG_DUMP_IRRT}");
|
|
|
|
if env::var(DEBUG_DUMP_IRRT).is_ok() {
|
|
|
|
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-12 21:52:55 +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();
|
|
|
|
}
|
|
|
|
|
2024-07-12 21:52:55 +08:00
|
|
|
// Assemble the emitted and filtered IR to .bc
|
|
|
|
// That .bc will be integrated into nac3core's codegen
|
2024-07-12 22:29:04 +08:00
|
|
|
let mut llvm_as = Command::new(CMD_IRRT_LLVM_AS)
|
2022-01-08 22:16:55 +08:00
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.arg("-o")
|
2024-07-12 21:52:55 +08:00
|
|
|
.arg(out_dir.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
|
|
|
}
|
2024-07-12 22:29:04 +08:00
|
|
|
|
|
|
|
/// Compile `irrt_test.cpp` for testing
|
|
|
|
fn compile_irrt_test_cpp() {
|
|
|
|
let out_dir = get_out_dir();
|
|
|
|
let irrt_dir = get_irrt_dir();
|
|
|
|
|
|
|
|
let exe_path = out_dir.join("irrt_test.out"); // Output path of the compiled test executable
|
|
|
|
let irrt_test_cpp_path = irrt_dir.join("irrt_test.cpp");
|
|
|
|
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",
|
|
|
|
"-Werror=return-type",
|
|
|
|
"-lm", // for `tgamma()`, `lgamma()`
|
2024-07-14 17:09:48 +08:00
|
|
|
"-I",
|
|
|
|
irrt_dir.to_str().unwrap(),
|
2024-07-12 22:29:04 +08:00
|
|
|
"-o",
|
|
|
|
exe_path.to_str().unwrap(),
|
|
|
|
];
|
|
|
|
|
|
|
|
Command::new(CMD_IRRT_CLANG_TEST)
|
|
|
|
.args(flags)
|
|
|
|
.output()
|
|
|
|
.map(|o| {
|
|
|
|
assert!(o.status.success(), "{}", std::str::from_utf8(&o.stderr).unwrap());
|
|
|
|
o
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
println!("cargo:rerun-if-changed={}", irrt_dir.to_str().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
compile_irrt_cpp();
|
|
|
|
|
|
|
|
// 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_cpp();
|
|
|
|
}
|
|
|
|
}
|