2024-08-28 16:33:03 +08:00
|
|
|
use std::ffi::OsStr;
|
2022-01-08 22:16:55 +08:00
|
|
|
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},
|
|
|
|
};
|
|
|
|
|
2024-08-28 16:33:03 +08:00
|
|
|
use itertools::Itertools;
|
2024-10-03 12:37:56 +08:00
|
|
|
use regex::Regex;
|
|
|
|
|
2024-08-28 16:33:03 +08:00
|
|
|
struct IRRTCompilation<'a> {
|
|
|
|
pub file: &'a str,
|
|
|
|
pub gcc_options: Vec<&'a str>,
|
|
|
|
pub cargo_instructions: Vec<&'a str>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extracts the extension-less filename from a [`Path`].
|
|
|
|
fn path_to_extless_filename(path: &Path) -> &str {
|
|
|
|
path.file_name().map(Path::new).and_then(Path::file_stem).and_then(OsStr::to_str).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compiles a source C file into LLVM bitcode.
|
|
|
|
fn compile_file_to_ir(compile_opts: &IRRTCompilation) {
|
2024-07-12 21:52:55 +08:00
|
|
|
let out_dir = env::var("OUT_DIR").unwrap();
|
2024-08-28 16:33:03 +08:00
|
|
|
let out_path = Path::new(&out_dir);
|
2024-07-12 21:52:55 +08:00
|
|
|
let irrt_dir = Path::new("irrt");
|
|
|
|
|
2024-08-28 16:33:03 +08:00
|
|
|
let path = Path::new(compile_opts.file);
|
|
|
|
let filename_without_ext = path_to_extless_filename(path);
|
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-08-27 10:36:51 +08:00
|
|
|
let mut flags: Vec<&str> = vec![
|
2022-01-09 10:53:58 +08:00
|
|
|
"--target=wasm32",
|
2024-07-09 13:31:29 +08:00
|
|
|
"-x",
|
|
|
|
"c++",
|
2024-08-26 15:19:22 +08:00
|
|
|
"-std=c++20",
|
2023-11-22 13:35:56 +08:00
|
|
|
"-fno-discard-value-names",
|
2024-07-05 17:24:57 +08:00
|
|
|
"-fno-exceptions",
|
|
|
|
"-fno-rtti",
|
2022-01-08 22:16:55 +08:00
|
|
|
"-emit-llvm",
|
|
|
|
"-S",
|
|
|
|
"-Wall",
|
|
|
|
"-Wextra",
|
|
|
|
"-o",
|
|
|
|
"-",
|
2024-08-27 10:36:51 +08:00
|
|
|
"-I",
|
|
|
|
irrt_dir.to_str().unwrap(),
|
2022-01-08 22:16:55 +08:00
|
|
|
];
|
2023-12-08 17:43:32 +08:00
|
|
|
|
2024-08-28 16:33:03 +08:00
|
|
|
// Apply custom flags from IRRTCompilation
|
|
|
|
flags.extend_from_slice(&compile_opts.gcc_options);
|
|
|
|
|
2024-08-27 10:36:51 +08:00
|
|
|
match env::var("PROFILE").as_deref() {
|
2024-08-28 16:33:03 +08:00
|
|
|
Ok("debug") => flags.extend_from_slice(&["-O0", "-DIRRT_DEBUG_ASSERT"]),
|
|
|
|
Ok("release") => flags.push("-O3"),
|
2024-08-27 10:36:51 +08:00
|
|
|
flavor => panic!("Unknown or missing build flavor {flavor:?}"),
|
|
|
|
}
|
|
|
|
|
2024-08-28 16:33:03 +08:00
|
|
|
flags.push(path.to_str().unwrap());
|
|
|
|
|
|
|
|
// Tell Cargo to rerun if the main IRRT source is changed
|
|
|
|
println!("cargo:rerun-if-changed={}", path.to_str().unwrap());
|
|
|
|
compile_opts.cargo_instructions.iter().for_each(|inst| println!("cargo::{inst}"));
|
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-07 20:03:34 +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()
|
2024-11-19 13:43:57 +08:00
|
|
|
.inspect(|o| {
|
2022-01-08 22:16:55 +08:00
|
|
|
assert!(o.status.success(), "{}", std::str::from_utf8(&o.stderr).unwrap());
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
2024-08-28 16:33:03 +08:00
|
|
|
let output = std::str::from_utf8(&output.stdout).unwrap();
|
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();
|
2024-08-28 16:33:03 +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}");
|
2024-08-28 16:33:03 +08:00
|
|
|
if env::var("DEBUG_DUMP_IRRT").is_ok() {
|
|
|
|
let mut file = File::create(out_path.join(format!("{filename_without_ext}.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
|
|
|
|
2024-08-28 16:33:03 +08:00
|
|
|
let mut file =
|
|
|
|
File::create(out_path.join(format!("{filename_without_ext}-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")
|
2022-01-08 22:16:55 +08:00
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.arg("-o")
|
2024-08-28 16:33:03 +08:00
|
|
|
.arg(out_path.join(format!("{filename_without_ext}.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-08-28 16:33:03 +08:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let irrt_compilations: &[IRRTCompilation] = &[
|
|
|
|
IRRTCompilation {
|
|
|
|
file: "irrt/irrt.cpp",
|
|
|
|
gcc_options: Vec::default(),
|
|
|
|
cargo_instructions: vec!["rerun-if-changed=irrt/irrt"],
|
|
|
|
},
|
|
|
|
IRRTCompilation {
|
|
|
|
file: "irrt/tracert.cpp",
|
|
|
|
gcc_options: Vec::default(),
|
|
|
|
cargo_instructions: Vec::default(),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
assert!(irrt_compilations
|
|
|
|
.iter()
|
|
|
|
.map(|comp| comp.file)
|
|
|
|
.map(Path::new)
|
|
|
|
.map(path_to_extless_filename)
|
|
|
|
.all_unique());
|
|
|
|
|
|
|
|
for path in irrt_compilations {
|
|
|
|
compile_file_to_ir(path)
|
|
|
|
}
|
|
|
|
}
|