| // Copyright 2026 The Fuchsia Authors | |
| // | |
| // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 | |
| // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT | |
| // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. | |
| // This file may not be copied, modified, or distributed except according to | |
| // those terms. | |
| //! CLI entrypoint for `anneal-cpp-diff`. | |
| #![forbid(unsafe_code)] | |
| use std::{ | |
| env, fs, | |
| path::{Path, PathBuf}, | |
| process::ExitCode, | |
| }; | |
| use anneal_cpp_diff::{ | |
| compiler_leg::CompilerLegConfig, | |
| corpus::{default_corpus, CorpusCase}, | |
| lean_driver::LeanLegConfig, | |
| run_suite, SuiteConfig, | |
| }; | |
| const USAGE: &str = "usage: anneal-cpp-diff [OPTIONS] | |
| C11/C++20 operational-semantics differential oracle comparing | |
| Trusted.CppSemantics against Clang/UBSan+ASan (and optional GCC/UBSan). | |
| Options: | |
| --list List built-in corpus cases and exit | |
| --filter SUBSTR Only run cases whose ID contains SUBSTR | |
| --repo-root PATH Anneal repository root (default: auto-detect) | |
| --clang-dump PATH Path to anneal-clang-dump binary (or $ANNEAL_CLANG_DUMP) | |
| --lean PATH Path to lean v4.35.0 binary (default: auto-detect) | |
| --lean-cache-dir PATH Directory for caching compiled Trusted.Cpp* .oleans | |
| --cc PATH Primary C compiler (default: clang-19 or clang) | |
| --cxx PATH Primary C++ compiler (default: clang++-19 or clang++) | |
| --gcc PATH Secondary C compiler (default: gcc if available) | |
| --gxx PATH Secondary C++ compiler (default: g++ if available) | |
| --no-gcc Disable secondary GCC/G++ compiler leg | |
| --no-asan Disable AddressSanitizer on the primary compiler leg | |
| --fuel N Max evalStep steps per case (default: 2000) | |
| --batch-size N Cases evaluated per Lean process (default: 16) | |
| --json Print JSON report to stdout instead of human summary | |
| --json-out PATH Write JSON report to PATH | |
| --keep-temp Preserve temporary driver and build files | |
| -h, --help Show this help text"; | |
| struct CliArgs { | |
| list: bool, | |
| filter: Option<String>, | |
| repo_root: Option<PathBuf>, | |
| clang_dump: Option<PathBuf>, | |
| lean_bin: Option<PathBuf>, | |
| lean_cache_dir: Option<PathBuf>, | |
| cc: Option<PathBuf>, | |
| cxx: Option<PathBuf>, | |
| gcc: Option<PathBuf>, | |
| gxx: Option<PathBuf>, | |
| no_gcc: bool, | |
| no_asan: bool, | |
| fuel: usize, | |
| batch_size: usize, | |
| json_stdout: bool, | |
| json_out: Option<PathBuf>, | |
| keep_temp: bool, | |
| } | |
| fn parse_args() -> Result<Option<CliArgs>, String> { | |
| let mut cfg = CliArgs { | |
| list: false, | |
| filter: None, | |
| repo_root: None, | |
| clang_dump: env::var_os("ANNEAL_CLANG_DUMP").map(PathBuf::from), | |
| lean_bin: None, | |
| lean_cache_dir: None, | |
| cc: None, | |
| cxx: None, | |
| gcc: None, | |
| gxx: None, | |
| no_gcc: false, | |
| no_asan: false, | |
| fuel: 2000, | |
| batch_size: 16, | |
| json_stdout: false, | |
| json_out: None, | |
| keep_temp: false, | |
| }; | |
| let mut args = env::args().skip(1); | |
| while let Some(arg) = args.next() { | |
| let mut next_val = | |
| |flag: &str| args.next().ok_or_else(|| format!("missing value for {flag}")); | |
| match arg.as_str() { | |
| "-h" | "--help" => { | |
| println!("{USAGE}"); | |
| return Ok(None); | |
| } | |
| "--list" => cfg.list = true, | |
| "--filter" => cfg.filter = Some(next_val("--filter")?), | |
| "--repo-root" => cfg.repo_root = Some(PathBuf::from(next_val("--repo-root")?)), | |
| "--clang-dump" => cfg.clang_dump = Some(PathBuf::from(next_val("--clang-dump")?)), | |
| "--lean" => cfg.lean_bin = Some(PathBuf::from(next_val("--lean")?)), | |
| "--lean-cache-dir" => { | |
| cfg.lean_cache_dir = Some(PathBuf::from(next_val("--lean-cache-dir")?)) | |
| } | |
| "--cc" => cfg.cc = Some(PathBuf::from(next_val("--cc")?)), | |
| "--cxx" => cfg.cxx = Some(PathBuf::from(next_val("--cxx")?)), | |
| "--gcc" => cfg.gcc = Some(PathBuf::from(next_val("--gcc")?)), | |
| "--gxx" => cfg.gxx = Some(PathBuf::from(next_val("--gxx")?)), | |
| "--no-gcc" => cfg.no_gcc = true, | |
| "--no-asan" => cfg.no_asan = true, | |
| "--fuel" => { | |
| let raw = next_val("--fuel")?; | |
| cfg.fuel = | |
| raw.parse::<usize>().map_err(|e| format!("invalid --fuel {raw:?}: {e}"))?; | |
| } | |
| "--batch-size" => { | |
| let raw = next_val("--batch-size")?; | |
| cfg.batch_size = raw | |
| .parse::<usize>() | |
| .map_err(|e| format!("invalid --batch-size {raw:?}: {e}"))?; | |
| } | |
| "--json" => cfg.json_stdout = true, | |
| "--json-out" => cfg.json_out = Some(PathBuf::from(next_val("--json-out")?)), | |
| "--keep-temp" => cfg.keep_temp = true, | |
| other => return Err(format!("unexpected argument {other:?}")), | |
| } | |
| } | |
| Ok(Some(cfg)) | |
| } | |
| fn find_in_path(candidates: &[&str]) -> Option<PathBuf> { | |
| let path_var = env::var_os("PATH")?; | |
| for cand in candidates { | |
| for dir in env::split_paths(&path_var) { | |
| let p = dir.join(cand); | |
| if p.is_file() { | |
| return Some(p); | |
| } | |
| } | |
| } | |
| None | |
| } | |
| fn detect_repo_root() -> Option<PathBuf> { | |
| let mut cur = env::current_dir().ok()?; | |
| loop { | |
| if cur.join("trusted/lean/Trusted/CppSemantics.lean").is_file() { | |
| return Some(cur); | |
| } | |
| if !cur.pop() { | |
| return None; | |
| } | |
| } | |
| } | |
| fn detect_lean_bin(repo_root: &Path) -> Option<PathBuf> { | |
| if let Ok(tc) = fs::read_to_string(repo_root.join("trusted/lean/lean-toolchain")) { | |
| let slug = tc.trim().replace('/', "--").replace(':', "---"); | |
| if let Some(home) = env::var_os("HOME") { | |
| let elan_lean = | |
| PathBuf::from(home).join(".elan/toolchains").join(slug).join("bin/lean"); | |
| if elan_lean.is_file() { | |
| return Some(elan_lean); | |
| } | |
| } | |
| } | |
| find_in_path(&["lean"]) | |
| } | |
| fn filter_cases(all: Vec<CorpusCase>, filter: Option<&str>) -> Vec<CorpusCase> { | |
| match filter { | |
| None => all, | |
| Some(substr) => all.into_iter().filter(|c| c.id.contains(substr)).collect(), | |
| } | |
| } | |
| fn main() -> ExitCode { | |
| let cli = match parse_args() { | |
| Ok(Some(c)) => c, | |
| Ok(None) => return ExitCode::SUCCESS, | |
| Err(e) => { | |
| eprintln!("anneal-cpp-diff: {e}\n{USAGE}"); | |
| return ExitCode::from(2); | |
| } | |
| }; | |
| let cases = filter_cases(default_corpus(), cli.filter.as_deref()); | |
| if cli.list { | |
| for c in &cases { | |
| println!("{:<44} {:?} {:?} {:?}", c.id, c.language, c.expectation, c.category); | |
| } | |
| println!("total: {} cases", cases.len()); | |
| return ExitCode::SUCCESS; | |
| } | |
| if cases.is_empty() { | |
| eprintln!("anneal-cpp-diff: no corpus cases matched filter"); | |
| return ExitCode::from(2); | |
| } | |
| let Some(repo_root) = cli.repo_root.or_else(detect_repo_root) else { | |
| eprintln!("anneal-cpp-diff: could not locate repo root; pass --repo-root"); | |
| return ExitCode::from(2); | |
| }; | |
| let Some(clang_dump) = cli.clang_dump.or_else(|| find_in_path(&["anneal-clang-dump"])) else { | |
| eprintln!("anneal-cpp-diff: could not locate anneal-clang-dump; pass --clang-dump"); | |
| return ExitCode::from(2); | |
| }; | |
| let Some(lean_bin) = cli.lean_bin.or_else(|| detect_lean_bin(&repo_root)) else { | |
| eprintln!("anneal-cpp-diff: could not locate lean binary; pass --lean"); | |
| return ExitCode::from(2); | |
| }; | |
| let Some(cc) = cli.cc.or_else(|| find_in_path(&["clang-19", "clang", "gcc"])) else { | |
| eprintln!("anneal-cpp-diff: could not locate a C compiler; pass --cc"); | |
| return ExitCode::from(2); | |
| }; | |
| let Some(cxx) = cli.cxx.or_else(|| find_in_path(&["clang++-19", "clang++", "g++"])) else { | |
| eprintln!("anneal-cpp-diff: could not locate a C++ compiler; pass --cxx"); | |
| return ExitCode::from(2); | |
| }; | |
| let primary_label = cc | |
| .file_name() | |
| .map_or_else(|| "primary-cc".to_owned(), |n| n.to_string_lossy().into_owned()); | |
| let primary_compiler = CompilerLegConfig { | |
| label: primary_label, | |
| c_compiler: cc, | |
| cxx_compiler: cxx, | |
| enable_asan: !cli.no_asan, | |
| }; | |
| let secondary_compiler = if cli.no_gcc { | |
| None | |
| } else { | |
| let gcc = cli.gcc.or_else(|| find_in_path(&["gcc"])); | |
| let gxx = cli.gxx.or_else(|| find_in_path(&["g++"])); | |
| match (gcc, gxx) { | |
| (Some(c), Some(x)) => Some(CompilerLegConfig { | |
| label: "gcc-gxx".to_owned(), | |
| c_compiler: c, | |
| cxx_compiler: x, | |
| enable_asan: false, | |
| }), | |
| _ => None, | |
| } | |
| }; | |
| let suite_cfg = SuiteConfig { | |
| lean: LeanLegConfig { | |
| repo_root, | |
| clang_dump, | |
| lean_bin, | |
| lean_cache_dir: cli.lean_cache_dir, | |
| fuel: cli.fuel, | |
| batch_size: cli.batch_size, | |
| }, | |
| primary_compiler, | |
| secondary_compiler, | |
| keep_temp: cli.keep_temp, | |
| }; | |
| let report = match run_suite(&suite_cfg, &cases) { | |
| Ok(r) => r, | |
| Err(e) => { | |
| eprintln!("anneal-cpp-diff: fatal error: {e}"); | |
| return ExitCode::from(2); | |
| } | |
| }; | |
| if let Some(json_out) = &cli.json_out { | |
| match report.to_json_pretty() { | |
| Ok(text) => { | |
| if let Err(e) = fs::write(json_out, text) { | |
| eprintln!("anneal-cpp-diff: cannot write {}: {e}", json_out.display()); | |
| return ExitCode::from(2); | |
| } | |
| } | |
| Err(e) => { | |
| eprintln!("anneal-cpp-diff: JSON serialization error: {e}"); | |
| return ExitCode::from(2); | |
| } | |
| } | |
| } | |
| if cli.json_stdout { | |
| match report.to_json_pretty() { | |
| Ok(text) => println!("{text}"), | |
| Err(e) => { | |
| eprintln!("anneal-cpp-diff: JSON serialization error: {e}"); | |
| return ExitCode::from(2); | |
| } | |
| } | |
| } else { | |
| print!("{}", report.format_human()); | |
| } | |
| if report.summary.all_passed() { | |
| ExitCode::SUCCESS | |
| } else { | |
| ExitCode::from(1) | |
| } | |
| } |