| @@ -22,8 +22,9 @@ const SYSTEM_LIBS: &[&str] = &[ |
| 22 | 22 | /// How to invoke the C compiler. |
| 23 | 23 | #[derive(Clone, Debug, Default)] |
| 24 | 24 | pub struct CcOptions { |
| 25 | | − /// The compiler (default: `$TILC_CC`, else `cc`). It must support |
| 26 | | − /// `musttail`: clang 13 or later, or GCC 15 or later. |
| 25 | + /// The compiler (default: `$TILC_CC`, else the first of `cc`, `clang` |
| 26 | + /// and `gcc` that supports `musttail`: clang 13 or later, GCC 15 or |
| 27 | + /// later). |
| 27 | 28 | pub cc: Option<String>, |
| 28 | 29 | /// Extra flags (default: `$TILC_CFLAGS`, e.g. sanitizers). |
| 29 | 30 | pub flags: Vec<String>, |
| @@ -90,6 +91,40 @@ fn runtime_dir(opts: &CcOptions) -> Result<PathBuf, BuildError> { |
| 90 | 91 | }) |
| 91 | 92 | } |
| 92 | 93 | |
| 94 | +/// The first of `cc`, `clang`, `gcc` that supports `musttail` (clang 13+, |
| 95 | +/// GCC 15+), found once per process. |
| 96 | +fn default_cc() -> Result<String, BuildError> { |
| 97 | + static FOUND: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new(); |
| 98 | + FOUND |
| 99 | + .get_or_init(|| { |
| 100 | + let dir = std::env::temp_dir().join(format!("tilc-cc-probe-{}", std::process::id())); |
| 101 | + std::fs::create_dir_all(&dir).ok()?; |
| 102 | + let probe = dir.join("probe.c"); |
| 103 | + std::fs::write( |
| 104 | + &probe, |
| 105 | + "static int g(int x) { return x; }\nint f(int x) { __attribute__((musttail)) return g(x); }\n", |
| 106 | + ) |
| 107 | + .ok()?; |
| 108 | + let found = ["cc", "clang", "gcc"].into_iter().find(|cc| { |
| 109 | + Command::new(cc) |
| 110 | + .args(["-c", "-o"]) |
| 111 | + .arg(dir.join("probe.o")) |
| 112 | + .arg(&probe) |
| 113 | + .output() |
| 114 | + .is_ok_and(|o| o.status.success()) |
| 115 | + }); |
| 116 | + let _ = std::fs::remove_dir_all(&dir); |
| 117 | + found.map(str::to_owned) |
| 118 | + }) |
| 119 | + .clone() |
| 120 | + .ok_or_else(|| { |
| 121 | + BuildError::Cc( |
| 122 | + "no C compiler supporting `musttail` was found (tried cc, clang, gcc): install clang 13+ or GCC 15+, or set TILC_CC" |
| 123 | + .into(), |
| 124 | + ) |
| 125 | + }) |
| 126 | +} |
| 127 | + |
| 93 | 128 | /// Compile `c_source` into the executable `output`. The C file and the |
| 94 | 129 | /// runtime header are written next to it (`output.c`, `tilrt.h`). |
| 95 | 130 | pub fn build_executable(c_source: &str, output: &Path, opts: &CcOptions) -> Result<(), BuildError> { |
| @@ -99,11 +134,10 @@ pub fn build_executable(c_source: &str, output: &Path, opts: &CcOptions) -> Resu |
| 99 | 134 | let c_file = output.with_extension("c"); |
| 100 | 135 | std::fs::write(&c_file, c_source)?; |
| 101 | 136 | std::fs::write(dir.join("tilrt.h"), HEADER)?; |
| 102 | | − let cc = opts |
| 103 | | − .cc |
| 104 | | − .clone() |
| 105 | | − .or_else(|| std::env::var("TILC_CC").ok()) |
| 106 | | − .unwrap_or_else(|| "cc".into()); |
| 137 | + let cc = match opts.cc.clone().or_else(|| std::env::var("TILC_CC").ok()) { |
| 138 | + Some(cc) => cc, |
| 139 | + None => default_cc()?, |
| 140 | + }; |
| 107 | 141 | let mut flags = opts.flags.clone(); |
| 108 | 142 | if flags.is_empty() |
| 109 | 143 | && let Ok(env) = std::env::var("TILC_CFLAGS") |