|
| 1 | +use itertools::Itertools; |
| 2 | +use std::path::PathBuf; |
| 3 | + |
1 | 4 | #[derive(Debug, PartialEq)]
|
2 | 5 | pub enum Language {
|
3 | 6 | Rust,
|
4 | 7 | C,
|
5 | 8 | }
|
| 9 | + |
| 10 | +pub enum FailureReason { |
| 11 | + RunC(String), |
| 12 | + RunRust(String), |
| 13 | + Difference(String, String, String), |
| 14 | +} |
| 15 | + |
| 16 | +/// Intrinsic test tool |
| 17 | +#[derive(clap::Parser)] |
| 18 | +#[command( |
| 19 | + name = "Intrinsic test tool", |
| 20 | + about = "Generates Rust and C programs for intrinsics and compares the output" |
| 21 | +)] |
| 22 | +pub struct Cli { |
| 23 | + /// The input file containing the intrinsics |
| 24 | + pub input: PathBuf, |
| 25 | + |
| 26 | + /// The rust toolchain to use for building the rust code |
| 27 | + #[arg(long)] |
| 28 | + pub toolchain: Option<String>, |
| 29 | + |
| 30 | + /// The C++ compiler to use for compiling the c++ code |
| 31 | + #[arg(long, default_value_t = String::from("clang++"))] |
| 32 | + pub cppcompiler: String, |
| 33 | + |
| 34 | + /// Run the C programs under emulation with this command |
| 35 | + #[arg(long)] |
| 36 | + pub runner: Option<String>, |
| 37 | + |
| 38 | + /// Filename for a list of intrinsics to skip (one per line) |
| 39 | + #[arg(long)] |
| 40 | + pub skip: Option<PathBuf>, |
| 41 | + |
| 42 | + /// Regenerate test programs, but don't build or run them |
| 43 | + #[arg(long)] |
| 44 | + pub generate_only: bool, |
| 45 | + |
| 46 | + /// Pass a target the test suite |
| 47 | + #[arg(long, default_value_t = String::from("aarch64-unknown-linux-gnu"))] |
| 48 | + pub target: String, |
| 49 | + |
| 50 | + /// Set the linker |
| 51 | + #[arg(long)] |
| 52 | + pub linker: Option<String>, |
| 53 | + |
| 54 | + /// Set the sysroot for the C++ compiler |
| 55 | + #[arg(long)] |
| 56 | + pub cxx_toolchain_dir: Option<String>, |
| 57 | +} |
| 58 | + |
| 59 | +pub struct ProcessedCli { |
| 60 | + pub filename: PathBuf, |
| 61 | + pub toolchain: Option<String>, |
| 62 | + pub cpp_compiler: Option<String>, |
| 63 | + pub c_runner: String, |
| 64 | + pub target: String, |
| 65 | + pub linker: Option<String>, |
| 66 | + pub cxx_toolchain_dir: Option<String>, |
| 67 | + pub skip: Vec<String>, |
| 68 | +} |
| 69 | + |
| 70 | +impl ProcessedCli { |
| 71 | + pub fn new(cli_options: Cli) -> Self { |
| 72 | + let filename = cli_options.input; |
| 73 | + let c_runner = cli_options.runner.unwrap_or_default(); |
| 74 | + let target = cli_options.target; |
| 75 | + let linker = cli_options.linker; |
| 76 | + let cxx_toolchain_dir = cli_options.cxx_toolchain_dir; |
| 77 | + |
| 78 | + let skip = if let Some(filename) = cli_options.skip { |
| 79 | + let data = std::fs::read_to_string(&filename).expect("Failed to open file"); |
| 80 | + data.lines() |
| 81 | + .map(str::trim) |
| 82 | + .filter(|s| !s.contains('#')) |
| 83 | + .map(String::from) |
| 84 | + .collect_vec() |
| 85 | + } else { |
| 86 | + Default::default() |
| 87 | + }; |
| 88 | + |
| 89 | + let (toolchain, cpp_compiler) = if cli_options.generate_only { |
| 90 | + (None, None) |
| 91 | + } else { |
| 92 | + ( |
| 93 | + Some( |
| 94 | + cli_options |
| 95 | + .toolchain |
| 96 | + .map_or_else(String::new, |t| format!("+{t}")), |
| 97 | + ), |
| 98 | + Some(cli_options.cppcompiler), |
| 99 | + ) |
| 100 | + }; |
| 101 | + |
| 102 | + Self { |
| 103 | + toolchain: toolchain, |
| 104 | + cpp_compiler: cpp_compiler, |
| 105 | + c_runner: c_runner, |
| 106 | + target: target, |
| 107 | + linker: linker, |
| 108 | + cxx_toolchain_dir: cxx_toolchain_dir, |
| 109 | + skip: skip, |
| 110 | + filename: filename, |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments