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