Compiles C source code and returns the binary path
(
source_code: &str,
name: &str,
output_dir: &Path,
)
| 9 | |
| 10 | /// Compiles C source code and returns the binary path |
| 11 | fn compile_c_source( |
| 12 | source_code: &str, |
| 13 | name: &str, |
| 14 | output_dir: &Path, |
| 15 | ) -> Result<std::path::PathBuf, Box<dyn std::error::Error>> { |
| 16 | let source_path = output_dir.join(format!("{name}.c")); |
| 17 | let binary_path = output_dir.join(name); |
| 18 | |
| 19 | fs::write(&source_path, source_code)?; |
| 20 | |
| 21 | let output = Command::new("gcc") |
| 22 | .args(["-o", binary_path.to_str().unwrap()]) |
| 23 | .arg(&source_path) |
| 24 | .output()?; |
| 25 | |
| 26 | if !output.status.success() { |
| 27 | eprintln!("gcc stderr: {}", String::from_utf8_lossy(&output.stderr)); |
| 28 | return Err("Failed to compile C fixture".into()); |
| 29 | } |
| 30 | |
| 31 | Ok(binary_path) |
| 32 | } |
| 33 | |
| 34 | struct AllocationTestCase { |
| 35 | name: &'static str, |
no test coverage detected