//! Ties the compiler stages together and invokes the C compiler. use std::collections::HashMap; use std::path::{Path, PathBuf}; use std::process::Command; use std::time::{SystemTime, UNIX_EPOCH}; use crate::ast::{Item, Program}; use crate::error::{Diag, PResult}; use crate::{cheader, check, codegen_c, loader, parser}; /// Read the C headers imported by the program. `dir` is the folder of the main file. pub fn load_c_headers(main: &Program, modules: &[(String, Program)], dir: &Path) -> PResult> { let mut libs: Vec = Vec::new(); let all = std::iter::once(("", main)).chain(modules.iter().map(|(k, p)| (k.as_str(), p))); for (key, p) in all { // `import a.b` lives in a/b.rex: its headers are found next to it let mut here = dir.to_path_buf(); let parts: Vec<&str> = key.split('.').collect(); for part in &parts[..parts.len().saturating_sub(1)] { here.push(part); } for item in &p.items { let Item::Import(i) = item else { continue }; if !i.c || libs.iter().any(|l| l.header == i.path) { continue; } let lib = cheader::load(&i.path, &here, i.link.as_deref()) .map_err(|m| Diag::new(m, i.span).in_module(key))?; libs.push(lib); } } Ok(libs) } /// Rex source -> C source, for a program in a single file. C headers it imports are looked /// for in the current folder and the system's. pub fn compile_to_c(src: &str) -> PResult { let ast = parser::parse(src)?; let libs = load_c_headers(&ast, &[], Path::new("."))?; let hir = check::check_project_c(&ast, &[], libs)?; Ok(codegen_c::generate(&hir)) } /// A whole program: the main file and the files it imports. `read(key)` finds a module /// (see `loader`). Errors come back already rendered, naming the file they are in. pub fn compile_project( main_path: &str, main_src: &str, read: impl FnMut(&str) -> Option<(String, String)>, ) -> Result { Ok(codegen_c::generate(&check_project(main_path, main_src, read, false)?)) } /// Load and check a program; `module_ok`: the main file may lack `main` (it is a module). fn check_project( main_path: &str, main_src: &str, mut read: impl FnMut(&str) -> Option<(String, String)>, module_ok: bool, ) -> Result { let mut seen: HashMap = HashMap::new(); let loaded = loader::load(main_src, &mut |key| { let found = read(key); if let Some((path, src)) = &found { seen.insert(key.to_string(), (path.clone(), src.clone())); } found }); let render = |d: Diag| -> String { match d.file.as_deref().and_then(|k| seen.get(k)) { Some((path, src)) => d.render(path, src), None if d.file.is_some() => d.render(d.file.as_deref().unwrap(), ""), None => d.render(main_path, main_src), } }; let loaded = loaded.map_err(&render)?; let modules: Vec<(String, crate::ast::Program)> = loaded.modules.into_iter().map(|m| (m.key, m.program)).collect(); let dir = loader::project_dir(Path::new(main_path)); let libs = load_c_headers(&loaded.main, &modules, &dir).map_err(&render)?; if module_ok { check::check_module_c(&loaded.main, &modules, libs).map_err(&render) } else { check::check_project_c(&loaded.main, &modules, libs).map_err(&render) } } /// Compile the program whose main file is `path`; imports are found next to it. pub fn compile_file(path: &str) -> Result { let src = std::fs::read_to_string(path).map_err(|e| format!("error: cannot read {path}: {e}"))?; let dir = loader::project_dir(Path::new(path)); compile_project(path, &src, |key| { let file = dir.join(loader::key_file(key)); std::fs::read_to_string(&file).ok().map(|s| (file.display().to_string(), s)) }) } /// Check the file at `path` (and what it imports) without building it. Unlike /// `compile_file`, a file without `main` is fine: it may be a module. pub fn check_file(path: &str) -> Result<(), String> { let src = std::fs::read_to_string(path).map_err(|e| format!("error: cannot read {path}: {e}"))?; let dir = loader::project_dir(Path::new(path)); let read = |key: &str| { let file = dir.join(loader::key_file(key)); std::fs::read_to_string(&file).ok().map(|s| (file.display().to_string(), s)) }; check_project(path, &src, read, true).map(|_| ()) } /// The program in `path` built for `rex test`: the C code, and the names of its test functions /// (`func test_...()` in that file, which take nothing and return nothing or an `error`), in the /// order they were written. The C program runs test number `n` when started with argument `n`. pub fn compile_tests(path: &str) -> Result<(String, Vec), String> { let src = std::fs::read_to_string(path).map_err(|e| format!("error: cannot read {path}: {e}"))?; let dir = loader::project_dir(Path::new(path)); let read = |key: &str| { let file = dir.join(loader::key_file(key)); std::fs::read_to_string(&file).ok().map(|s| (file.display().to_string(), s)) }; let hir = check_project(path, &src, read, true)?; // in the order written in the file let ast = parser::parse(&src).map_err(|d| d.render(path, &src))?; let mut tests = Vec::new(); for item in &ast.items { let Item::Func(f) = item else { continue }; if !f.name.starts_with("test_") { continue; } let Some(h) = hir.funcs.iter().find(|h| h.owner.is_none() && h.name == f.name) else { continue }; if !h.params.is_empty() || !matches!(h.ret, crate::types::Ty::Unit | crate::types::Ty::Error) { let d = Diag::new( format!("`{}` looks like a test, but a test takes nothing and returns nothing or an `error`", f.name), f.span, ); return Err(d.render(path, &src)); } tests.push(f.name.clone()); } Ok((codegen_c::generate_tests(&hir, &tests), tests)) } fn unique_name(tag: &str, ext: &str) -> PathBuf { // the counter makes names differ even when two threads ask in the same nanosecond static COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); let n = COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed); let nanos = SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or(0); std::env::temp_dir().join(format!("rex-{}-{}-{}-{}{}", std::process::id(), nanos, n, tag, ext)) } /// A path for a throw-away executable. pub fn temp_exe() -> PathBuf { unique_name("exe", if cfg!(windows) { ".exe" } else { "" }) } /// Compile C source to an executable with the system C compiler (`$CC`, else `cc`). pub fn build_exe(c_code: &str, out: &Path) -> Result<(), String> { build_exe_with(c_code, out, &["-O2", "-std=c11", "-fwrapv", "-w"]) } /// Like `build_exe`, with explicit compiler flags (used by tests to turn on strict warnings). /// The generated code says which C libraries it needs in `// rex-...:` lines at the top. pub fn build_exe_with(c_code: &str, out: &Path, flags: &[&str]) -> Result<(), String> { let (mut cflags, mut libs, mut sources) = (Vec::new(), Vec::new(), Vec::new()); for line in c_code.lines().take_while(|l| l.starts_with("// rex-")) { let (kind, value) = line["// rex-".len()..].split_once(": ").unwrap_or(("", "")); match kind { "cflag" => cflags.push(value.to_string()), "lib" => libs.push(value.to_string()), "source" => sources.push(PathBuf::from(value)), _ => {} } } let uses_c = c_code.contains("\n#include \""); // C headers often use GNU extensions let flags: Vec<&str> = flags.iter().map(|f| if uses_c && *f == "-std=c11" { "-std=gnu11" } else { *f }).collect(); let cc = cheader::cc(); // C++ files are compiled on their own, then linked in with the C++ library let mut objects: Vec = Vec::new(); let mut c_sources: Vec = Vec::new(); let cleanup = |objects: &[PathBuf]| { for o in objects { let _ = std::fs::remove_file(o); } }; for src in &sources { let ext = src.extension().and_then(|e| e.to_str()).unwrap_or(""); if ext == "c" { c_sources.push(src.clone()); continue; } let obj = unique_name("cpp", ".o"); let cxx = std::env::var("CXX").unwrap_or_else(|_| "c++".to_string()); let r = Command::new(&cxx).args(["-c", "-O2", "-w"]).args(&cflags).arg(src).arg("-o").arg(&obj).output(); match r { Err(e) => { cleanup(&objects); return Err(format!("cannot run the C++ compiler `{cxx}` for {}: {e}", src.display())); } Ok(o) if !o.status.success() => { cleanup(&objects); return Err(format!("{} does not compile:\n{}", src.display(), String::from_utf8_lossy(&o.stderr))); } Ok(_) => objects.push(obj), } } let c_file = unique_name("prog", ".c"); std::fs::write(&c_file, c_code).map_err(|e| format!("cannot write {}: {e}", c_file.display()))?; let mut cmd = Command::new(&cc); cmd.args(&flags).args(&cflags).arg("-o").arg(out).arg(&c_file); for s in &c_sources { // the user's own C file: warnings are theirs to see, but not ours to fail on cmd.arg(s); } cmd.args(&objects).args(&libs).arg("-lm"); // the math functions if !objects.is_empty() { cmd.arg("-lstdc++"); } let result = cmd.output(); let _ = std::fs::remove_file(&c_file); cleanup(&objects); match result { Err(e) => Err(format!("cannot run the C compiler `{cc}`: {e}\nInstall one (gcc or clang) or set $CC.")), Ok(o) if !o.status.success() => { let err = String::from_utf8_lossy(&o.stderr); if err.contains("undefined reference") || err.contains("cannot find -l") || err.contains("Undefined symbols") || err.contains("ld returned") { Err(format!( "linking with the C library failed:\n{err}\nIf a function is missing, name its library in the import: `import \"header.h\" link \"name\"` (for libname.so)." )) } else if uses_c { Err(format!("the C compiler rejected the program:\n{err}")) } else { Err(format!("the C compiler rejected the generated code (this is a bug in Rex):\n{err}")) } } Ok(_) => Ok(()), } } /// Check a whole program without building it, for editors: `main_src` is the text of the main /// file (it may differ from what is on disk) and `read(key)` finds the other files. /// The error's `file` says which module it is in (`None`: the main file). pub fn diagnose( main_src: &str, dir: &Path, mut read: impl FnMut(&str) -> Option<(String, String)>, ) -> Result<(), Diag> { let loaded = loader::load(main_src, &mut |key| read(key))?; let modules: Vec<(String, crate::ast::Program)> = loaded.modules.into_iter().map(|m| (m.key, m.program)).collect(); let libs = load_c_headers(&loaded.main, &modules, dir)?; check::check_module_c(&loaded.main, &modules, libs)?; Ok(()) }