| 1 |
use crate::{ |
| 2 |
command_helpers::{run_output, spawn_and_wait_for_output, CargoOutput, CommandExt}, |
| 3 |
run, |
| 4 |
tempfile::NamedTempfile, |
| 5 |
Error, ErrorKind, OutputKind, |
| 6 |
}; |
| 7 |
use std::{ |
| 8 |
borrow::Cow, |
| 9 |
collections::HashMap, |
| 10 |
env, |
| 11 |
ffi::{OsStr, OsString}, |
| 12 |
io::Write, |
| 13 |
iter, |
| 14 |
path::{Path, PathBuf}, |
| 15 |
process::{Command, Output, Stdio}, |
| 16 |
sync::{Arc, RwLock}, |
| 17 |
}; |
| 18 |
|
| 19 |
pub(crate) type CompilerFamilyLookupCache = HashMap<Box<[Box<OsStr>]>, ToolFamily>; |
| 20 |
|
| 21 |
/// The environment a compiler is probed and invoked in, as set by |
| 22 |
/// [`Build::env`](crate::Build::env). |
| 23 |
pub(crate) type BuildEnv = [(Arc<OsStr>, Arc<OsStr>)]; |
| 24 |
|
| 25 |
/// Separates the arguments from the environment in a |
| 26 |
/// [`CompilerFamilyLookupCache`] key. |
| 27 |
/// |
| 28 |
/// `Command` rejects arguments containing a nul byte, so no argument can |
| 29 |
/// impersonate this and collide with an environment variable name. |
| 30 |
const CACHE_KEY_ENV_SEPARATOR: &str = "\0env\0"; |
| 31 |
|
| 32 |
/// Configuration used to represent an invocation of a C compiler. |
| 33 |
/// |
| 34 |
/// This can be used to figure out what compiler is in use, what the arguments |
| 35 |
/// to it are, and what the environment variables look like for the compiler. |
| 36 |
/// This can be used to further configure other build systems (e.g. forward |
| 37 |
/// along CC and/or CFLAGS) or the `to_command` method can be used to run the |
| 38 |
/// compiler itself. |
| 39 |
#[derive(Clone, Debug)] |
| 40 |
#[allow(missing_docs)] |
| 41 |
pub struct Tool { |
| 42 |
pub(crate) path: PathBuf, |
| 43 |
pub(crate) cc_wrapper_path: Option<PathBuf>, |
| 44 |
pub(crate) cc_wrapper_args: Vec<OsString>, |
| 45 |
pub(crate) args: Vec<OsString>, |
| 46 |
pub(crate) env: Vec<(OsString, OsString)>, |
| 47 |
pub(crate) family: ToolFamily, |
| 48 |
pub(crate) cuda: bool, |
| 49 |
pub(crate) removed_args: Vec<OsString>, |
| 50 |
pub(crate) has_internal_target_arg: bool, |
| 51 |
} |
| 52 |
|
| 53 |
impl Tool { |
| 54 |
pub(crate) fn from_find_msvc_tools(tool: ::find_msvc_tools::Tool) -> Self { |
| 55 |
let mut cc_tool = Self::with_family( |
| 56 |
tool.path().into(), |
| 57 |
ToolFamily::Msvc { |
| 58 |
clang_cl: tool.is_clang_cl(), |
| 59 |
}, |
| 60 |
); |
| 61 |
|
| 62 |
cc_tool.env = tool |
| 63 |
.env() |
| 64 |
.into_iter() |
| 65 |
.map(|(k, v)| (k.clone(), v.clone())) |
| 66 |
.collect(); |
| 67 |
|
| 68 |
cc_tool |
| 69 |
} |
| 70 |
|
| 71 |
pub(crate) fn new( |
| 72 |
path: PathBuf, |
| 73 |
env: &BuildEnv, |
| 74 |
cached_compiler_family: &RwLock<CompilerFamilyLookupCache>, |
| 75 |
cargo_output: &CargoOutput, |
| 76 |
out_dir: Option<&Path>, |
| 77 |
) -> Self { |
| 78 |
Self::with_features( |
| 79 |
path, |
| 80 |
vec![], |
| 81 |
false, |
| 82 |
env, |
| 83 |
cached_compiler_family, |
| 84 |
cargo_output, |
| 85 |
out_dir, |
| 86 |
) |
| 87 |
} |
| 88 |
|
| 89 |
pub(crate) fn with_args( |
| 90 |
path: PathBuf, |
| 91 |
args: Vec<String>, |
| 92 |
env: &BuildEnv, |
| 93 |
cached_compiler_family: &RwLock<CompilerFamilyLookupCache>, |
| 94 |
cargo_output: &CargoOutput, |
| 95 |
out_dir: Option<&Path>, |
| 96 |
) -> Self { |
| 97 |
Self::with_features( |
| 98 |
path, |
| 99 |
args, |
| 100 |
false, |
| 101 |
env, |
| 102 |
cached_compiler_family, |
| 103 |
cargo_output, |
| 104 |
out_dir, |
| 105 |
) |
| 106 |
} |
| 107 |
|
| 108 |
/// Explicitly set the `ToolFamily`, skipping name-based detection. |
| 109 |
pub(crate) fn with_family(path: PathBuf, family: ToolFamily) -> Self { |
| 110 |
Self { |
| 111 |
path, |
| 112 |
cc_wrapper_path: None, |
| 113 |
cc_wrapper_args: Vec::new(), |
| 114 |
args: Vec::new(), |
| 115 |
env: Vec::new(), |
| 116 |
family, |
| 117 |
cuda: false, |
| 118 |
removed_args: Vec::new(), |
| 119 |
has_internal_target_arg: false, |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
pub(crate) fn with_features( |
| 124 |
path: PathBuf, |
| 125 |
args: Vec<String>, |
| 126 |
cuda: bool, |
| 127 |
env: &BuildEnv, |
| 128 |
cached_compiler_family: &RwLock<CompilerFamilyLookupCache>, |
| 129 |
cargo_output: &CargoOutput, |
| 130 |
out_dir: Option<&Path>, |
| 131 |
) -> Self { |
| 132 |
fn is_zig_cc(path: &Path, env: &BuildEnv, cargo_output: &CargoOutput) -> bool { |
| 133 |
run_output( |
| 134 |
Command::new(path) |
| 135 |
.arg("--version") |
| 136 |
.set_family_detection_env(env), |
| 137 |
// tool detection issues should always be shown as warnings |
| 138 |
cargo_output, |
| 139 |
) |
| 140 |
.map(|o| String::from_utf8_lossy(&o).contains("ziglang")) |
| 141 |
.unwrap_or_default() |
| 142 |
|| { |
| 143 |
match path.file_name().map(OsStr::to_string_lossy) { |
| 144 |
Some(fname) => fname.contains("zig"), |
| 145 |
_ => false, |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
fn guess_family_from_stdout( |
| 151 |
stdout: &str, |
| 152 |
path: &Path, |
| 153 |
args: &[String], |
| 154 |
env: &BuildEnv, |
| 155 |
cargo_output: &CargoOutput, |
| 156 |
) -> Result<ToolFamily, Error> { |
| 157 |
cargo_output.print_debug(&stdout); |
| 158 |
|
| 159 |
// https://gitlab.kitware.com/cmake/cmake/-/blob/69a2eeb9dff5b60f2f1e5b425002a0fd45b7cadb/Modules/CMakeDetermineCompilerId.cmake#L267-271 |
| 160 |
// stdin is set to null to ensure that the help output is never paginated. |
| 161 |
let accepts_cl_style_flags = run( |
| 162 |
Command::new(path) |
| 163 |
.args(args) |
| 164 |
.arg("-?") |
| 165 |
.stdin(Stdio::null()) |
| 166 |
.set_family_detection_env(env), |
| 167 |
&{ |
| 168 |
// the errors are not errors! |
| 169 |
let mut cargo_output = cargo_output.clone(); |
| 170 |
cargo_output.warnings = cargo_output.debug; |
| 171 |
cargo_output.output = OutputKind::Discard; |
| 172 |
cargo_output |
| 173 |
}, |
| 174 |
) |
| 175 |
.is_ok(); |
| 176 |
|
| 177 |
let clang = stdout.contains(r#""clang""#); |
| 178 |
let gcc = stdout.contains(r#""gcc""#); |
| 179 |
let emscripten = stdout.contains(r#""emscripten""#); |
| 180 |
let vxworks = stdout.contains(r#""VxWorks""#); |
| 181 |
|
| 182 |
match (clang, accepts_cl_style_flags, gcc, emscripten, vxworks) { |
| 183 |
(clang_cl, true, _, false, false) => Ok(ToolFamily::Msvc { clang_cl }), |
| 184 |
(true, _, _, _, false) | (_, _, _, true, false) => Ok(ToolFamily::Clang { |
| 185 |
zig_cc: is_zig_cc(path, env, cargo_output), |
| 186 |
}), |
| 187 |
(false, false, true, _, false) | (_, _, _, _, true) => Ok(ToolFamily::Gnu), |
| 188 |
(false, false, false, false, false) => { |
| 189 |
cargo_output.print_warning(&"Compiler family detection failed since it does not define `__clang__`, `__GNUC__`, `__EMSCRIPTEN__` or `__VXWORKS__`, also does not accept cl style flag `-?`, fallback to treating it as GNU"); |
| 190 |
Err(Error::new( |
| 191 |
ErrorKind::ToolFamilyMacroNotFound, |
| 192 |
"Expects macro `__clang__`, `__GNUC__` or `__EMSCRIPTEN__`, `__VXWORKS__` or accepts cl style flag `-?`, but found none", |
| 193 |
)) |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
fn detect_family_inner( |
| 199 |
path: &Path, |
| 200 |
args: &[String], |
| 201 |
env: &BuildEnv, |
| 202 |
cargo_output: &CargoOutput, |
| 203 |
out_dir: Option<&Path>, |
| 204 |
) -> Result<ToolFamily, Error> { |
| 205 |
let out_dir = out_dir |
| 206 |
.map(Cow::Borrowed) |
| 207 |
.unwrap_or_else(|| Cow::Owned(env::temp_dir())); |
| 208 |
|
| 209 |
// Ensure all the parent directories exist otherwise temp file creation |
| 210 |
// will fail |
| 211 |
std::fs::create_dir_all(&out_dir).map_err(|err| Error { |
| 212 |
kind: ErrorKind::IOError, |
| 213 |
message: format!("failed to create OUT_DIR '{}': {}", out_dir.display(), err) |
| 214 |
.into(), |
| 215 |
})?; |
| 216 |
|
| 217 |
let mut tmp = |
| 218 |
NamedTempfile::new(&out_dir, "detect_compiler_family.c").map_err(|err| Error { |
| 219 |
kind: ErrorKind::IOError, |
| 220 |
message: format!( |
| 221 |
"failed to create detect_compiler_family.c temp file in '{}': {}", |
| 222 |
out_dir.display(), |
| 223 |
err |
| 224 |
) |
| 225 |
.into(), |
| 226 |
})?; |
| 227 |
let mut tmp_file = tmp.take_file().unwrap(); |
| 228 |
tmp_file.write_all(include_bytes!("detect_compiler_family.c"))?; |
| 229 |
// Close the file handle *now*, otherwise the compiler may fail to open it on Windows |
| 230 |
// (#1082). The file stays on disk and its path remains valid until `tmp` is dropped. |
| 231 |
tmp_file.flush()?; |
| 232 |
tmp_file.sync_data()?; |
| 233 |
drop(tmp_file); |
| 234 |
|
| 235 |
// When expanding the file, the compiler prints a lot of information to stderr |
| 236 |
// that it is not an error, but related to expanding itself. |
| 237 |
// |
| 238 |
// cc would have to disable warning here to prevent generation of too many warnings. |
| 239 |
let mut compiler_detect_output = cargo_output.clone(); |
| 240 |
compiler_detect_output.warnings = compiler_detect_output.debug; |
| 241 |
|
| 242 |
let mut cmd = Command::new(path); |
| 243 |
cmd.arg("-E").arg(tmp.path()).set_family_detection_env(env); |
| 244 |
|
| 245 |
// The -Wslash-u-filename warning is normally part of stdout. |
| 246 |
// But with clang-cl it can be part of stderr instead and exit with a |
| 247 |
// non-zero exit code. |
| 248 |
let mut captured_cargo_output = compiler_detect_output.clone(); |
| 249 |
captured_cargo_output.warnings = true; |
| 250 |
let Output { |
| 251 |
status, |
| 252 |
stdout, |
| 253 |
stderr, |
| 254 |
} = spawn_and_wait_for_output(&mut cmd, &captured_cargo_output)?; |
| 255 |
|
| 256 |
let stdout = if [&stdout, &stderr] |
| 257 |
.iter() |
| 258 |
.any(|o| String::from_utf8_lossy(o).contains("-Wslash-u-filename")) |
| 259 |
{ |
| 260 |
run_output( |
| 261 |
Command::new(path) |
| 262 |
.arg("-E") |
| 263 |
.arg("--") |
| 264 |
.arg(tmp.path()) |
| 265 |
.set_family_detection_env(env), |
| 266 |
&compiler_detect_output, |
| 267 |
)? |
| 268 |
} else { |
| 269 |
if !status.success() { |
| 270 |
return Err(Error::new( |
| 271 |
ErrorKind::ToolExecError, |
| 272 |
format!( |
| 273 |
"command did not execute successfully (status code {status}): {cmd:?}" |
| 274 |
), |
| 275 |
)); |
| 276 |
} |
| 277 |
|
| 278 |
stdout |
| 279 |
}; |
| 280 |
|
| 281 |
let stdout = String::from_utf8_lossy(&stdout); |
| 282 |
guess_family_from_stdout(&stdout, path, args, env, cargo_output) |
| 283 |
} |
| 284 |
let detect_family = |path: &Path, args: &[String]| -> Result<ToolFamily, Error> { |
| 285 |
// The detected family depends on the environment the probes run in |
| 286 |
// - `PATH` decides what a bare compiler name even resolves to - so |
| 287 |
// two lookups that agree on the path and arguments but differ in |
| 288 |
// `Build::env` must not share an entry. |
| 289 |
let cache_key = [path.as_os_str()] |
| 290 |
.iter() |
| 291 |
.cloned() |
| 292 |
.chain(args.iter().map(OsStr::new)) |
| 293 |
.chain(iter::once(OsStr::new(CACHE_KEY_ENV_SEPARATOR))) |
| 294 |
.chain(env.iter().flat_map(|(key, value)| [&**key, &**value])) |
| 295 |
.map(Into::into) |
| 296 |
.collect(); |
| 297 |
if let Some(family) = cached_compiler_family.read().unwrap().get(&cache_key) { |
| 298 |
return Ok(*family); |
| 299 |
} |
| 300 |
|
| 301 |
let family = detect_family_inner(path, args, env, cargo_output, out_dir)?; |
| 302 |
cached_compiler_family |
| 303 |
.write() |
| 304 |
.unwrap() |
| 305 |
.insert(cache_key, family); |
| 306 |
Ok(family) |
| 307 |
}; |
| 308 |
|
| 309 |
let family = detect_family(&path, &args).unwrap_or_else(|e| { |
| 310 |
cargo_output.print_warning(&format_args!( |
| 311 |
"Compiler family detection failed due to error: {e}" |
| 312 |
)); |
| 313 |
match path.file_name().map(OsStr::to_string_lossy) { |
| 314 |
Some(fname) if fname.contains("clang-cl") => ToolFamily::Msvc { clang_cl: true }, |
| 315 |
Some(fname) if fname.ends_with("cl") || fname == "cl.exe" => { |
| 316 |
ToolFamily::Msvc { clang_cl: false } |
| 317 |
} |
| 318 |
Some(fname) if fname.contains("clang") => { |
| 319 |
let is_clang_cl = args |
| 320 |
.iter() |
| 321 |
.any(|a| a.strip_prefix("--driver-mode=") == Some("cl")); |
| 322 |
if is_clang_cl { |
| 323 |
ToolFamily::Msvc { clang_cl: true } |
| 324 |
} else { |
| 325 |
ToolFamily::Clang { |
| 326 |
zig_cc: is_zig_cc(&path, env, cargo_output), |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
Some(fname) if fname.contains("zig") => ToolFamily::Clang { zig_cc: true }, |
| 331 |
_ => ToolFamily::Gnu, |
| 332 |
} |
| 333 |
}); |
| 334 |
|
| 335 |
Tool { |
| 336 |
path, |
| 337 |
cc_wrapper_path: None, |
| 338 |
cc_wrapper_args: Vec::new(), |
| 339 |
args: Vec::new(), |
| 340 |
env: Vec::new(), |
| 341 |
family, |
| 342 |
cuda, |
| 343 |
removed_args: Vec::new(), |
| 344 |
has_internal_target_arg: false, |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
/// Add an argument to be stripped from the final command arguments. |
| 349 |
pub(crate) fn remove_arg(&mut self, flag: OsString) { |
| 350 |
self.removed_args.push(flag); |
| 351 |
} |
| 352 |
|
| 353 |
/// Push an "exotic" flag to the end of the compiler's arguments list. |
| 354 |
/// |
| 355 |
/// Nvidia compiler accepts only the most common compiler flags like `-D`, |
| 356 |
/// `-I`, `-c`, etc. Options meant specifically for the underlying |
| 357 |
/// host C++ compiler have to be prefixed with `-Xcompiler`. |
| 358 |
/// [Another possible future application for this function is passing |
| 359 |
/// clang-specific flags to clang-cl, which otherwise accepts only |
| 360 |
/// MSVC-specific options.] |
| 361 |
pub(crate) fn push_cc_arg(&mut self, flag: OsString) { |
| 362 |
if self.cuda { |
| 363 |
self.args.push("-Xcompiler".into()); |
| 364 |
} |
| 365 |
self.args.push(flag); |
| 366 |
} |
| 367 |
|
| 368 |
/// Checks if an argument or flag has already been specified or conflicts. |
| 369 |
/// |
| 370 |
/// Currently only checks optimization flags. |
| 371 |
pub(crate) fn is_duplicate_opt_arg(&self, flag: &OsString) -> bool { |
| 372 |
let flag = flag.to_str().unwrap(); |
| 373 |
let mut chars = flag.chars(); |
| 374 |
|
| 375 |
// Only duplicate check compiler flags |
| 376 |
if self.is_like_msvc() { |
| 377 |
if chars.next() != Some('/') { |
| 378 |
return false; |
| 379 |
} |
| 380 |
} else if (self.is_like_gnu() || self.is_like_clang()) && chars.next() != Some('-') { |
| 381 |
return false; |
| 382 |
} |
| 383 |
|
| 384 |
// Check for existing optimization flags (-O, /O) |
| 385 |
if chars.next() == Some('O') { |
| 386 |
return self |
| 387 |
.args() |
| 388 |
.iter() |
| 389 |
.any(|a| a.to_str().unwrap_or("").chars().nth(1) == Some('O')); |
| 390 |
} |
| 391 |
|
| 392 |
// TODO Check for existing -m..., -m...=..., /arch:... flags |
| 393 |
false |
| 394 |
} |
| 395 |
|
| 396 |
/// Don't push optimization arg if it conflicts with existing args. |
| 397 |
pub(crate) fn push_opt_unless_duplicate(&mut self, flag: OsString) { |
| 398 |
if self.is_duplicate_opt_arg(&flag) { |
| 399 |
eprintln!("Info: Ignoring duplicate arg {:?}", flag); |
| 400 |
} else { |
| 401 |
self.push_cc_arg(flag); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
/// Converts this compiler into a `Command` that's ready to be run. |
| 406 |
/// |
| 407 |
/// This is useful for when the compiler needs to be executed and the |
| 408 |
/// command returned will already have the initial arguments and environment |
| 409 |
/// variables configured. |
| 410 |
pub fn to_command(&self) -> Command { |
| 411 |
let mut cmd = match self.cc_wrapper_path { |
| 412 |
Some(ref cc_wrapper_path) => { |
| 413 |
let mut cmd = Command::new(cc_wrapper_path); |
| 414 |
cmd.arg(&self.path); |
| 415 |
cmd |
| 416 |
} |
| 417 |
None => Command::new(&self.path), |
| 418 |
}; |
| 419 |
cmd.args(&self.cc_wrapper_args); |
| 420 |
|
| 421 |
cmd.args(self.args.iter().filter(|a| !self.removed_args.contains(a))); |
| 422 |
|
| 423 |
for (k, v) in self.env.iter() { |
| 424 |
cmd.env(k, v); |
| 425 |
} |
| 426 |
|
| 427 |
cmd |
| 428 |
} |
| 429 |
|
| 430 |
/// Returns the path for this compiler. |
| 431 |
/// |
| 432 |
/// Note that this may not be a path to a file on the filesystem, e.g. "cc", |
| 433 |
/// but rather something which will be resolved when a process is spawned. |
| 434 |
pub fn path(&self) -> &Path { |
| 435 |
&self.path |
| 436 |
} |
| 437 |
|
| 438 |
/// Returns the default set of arguments to the compiler needed to produce |
| 439 |
/// executables for the target this compiler generates. |
| 440 |
pub fn args(&self) -> &[OsString] { |
| 441 |
&self.args |
| 442 |
} |
| 443 |
|
| 444 |
/// Returns the set of environment variables needed for this compiler to |
| 445 |
/// operate. |
| 446 |
/// |
| 447 |
/// This is typically only used for MSVC compilers currently. |
| 448 |
pub fn env(&self) -> &[(OsString, OsString)] { |
| 449 |
&self.env |
| 450 |
} |
| 451 |
|
| 452 |
/// Returns the compiler command in format of CC environment variable. |
| 453 |
/// Or empty string if CC env was not present |
| 454 |
/// |
| 455 |
/// This is typically used by configure script |
| 456 |
pub fn cc_env(&self) -> OsString { |
| 457 |
match self.cc_wrapper_path { |
| 458 |
Some(ref cc_wrapper_path) => { |
| 459 |
let mut cc_env = cc_wrapper_path.as_os_str().to_owned(); |
| 460 |
cc_env.push(" "); |
| 461 |
cc_env.push(self.path.to_path_buf().into_os_string()); |
| 462 |
for arg in self.cc_wrapper_args.iter() { |
| 463 |
cc_env.push(" "); |
| 464 |
cc_env.push(arg); |
| 465 |
} |
| 466 |
cc_env |
| 467 |
} |
| 468 |
None => OsString::from(""), |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
/// Returns the compiler flags in format of CFLAGS environment variable. |
| 473 |
/// Important here - this will not be CFLAGS from env, its internal gcc's flags to use as CFLAGS |
| 474 |
/// This is typically used by configure script |
| 475 |
pub fn cflags_env(&self) -> OsString { |
| 476 |
let mut flags = OsString::new(); |
| 477 |
for (i, arg) in self.args.iter().enumerate() { |
| 478 |
if i > 0 { |
| 479 |
flags.push(" "); |
| 480 |
} |
| 481 |
flags.push(arg); |
| 482 |
} |
| 483 |
flags |
| 484 |
} |
| 485 |
|
| 486 |
/// Whether the tool is GNU Compiler Collection-like. |
| 487 |
pub fn is_like_gnu(&self) -> bool { |
| 488 |
self.family == ToolFamily::Gnu |
| 489 |
} |
| 490 |
|
| 491 |
/// Whether the tool is Clang-like. |
| 492 |
pub fn is_like_clang(&self) -> bool { |
| 493 |
matches!(self.family, ToolFamily::Clang { .. }) |
| 494 |
} |
| 495 |
|
| 496 |
/// Whether the tool is AppleClang under .xctoolchain |
| 497 |
#[cfg(target_vendor = "apple")] |
| 498 |
pub(crate) fn is_xctoolchain_clang(&self) -> bool { |
| 499 |
let path = self.path.to_string_lossy(); |
| 500 |
path.contains(".xctoolchain/") |
| 501 |
} |
| 502 |
#[cfg(not(target_vendor = "apple"))] |
| 503 |
pub(crate) fn is_xctoolchain_clang(&self) -> bool { |
| 504 |
false |
| 505 |
} |
| 506 |
|
| 507 |
/// Whether the tool is MSVC-like. |
| 508 |
pub fn is_like_msvc(&self) -> bool { |
| 509 |
matches!(self.family, ToolFamily::Msvc { .. }) |
| 510 |
} |
| 511 |
|
| 512 |
/// Whether the tool is `clang-cl`-based MSVC-like. |
| 513 |
pub fn is_like_clang_cl(&self) -> bool { |
| 514 |
matches!(self.family, ToolFamily::Msvc { clang_cl: true }) |
| 515 |
} |
| 516 |
|
| 517 |
/// Supports using `--` delimiter to separate arguments and path to source files. |
| 518 |
pub(crate) fn supports_path_delimiter(&self) -> bool { |
| 519 |
// homebrew clang and zig-cc does not support this while stock version does |
| 520 |
matches!(self.family, ToolFamily::Msvc { clang_cl: true }) && !self.cuda |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
/// Represents the family of tools this tool belongs to. |
| 525 |
/// |
| 526 |
/// Each family of tools differs in how and what arguments they accept. |
| 527 |
/// |
| 528 |
/// Detection of a family is done on best-effort basis and may not accurately reflect the tool. |
| 529 |
#[derive(Copy, Clone, Debug, PartialEq)] |
| 530 |
pub enum ToolFamily { |
| 531 |
/// Tool is GNU Compiler Collection-like. |
| 532 |
Gnu, |
| 533 |
/// Tool is Clang-like. It differs from the GCC in a sense that it accepts superset of flags |
| 534 |
/// and its cross-compilation approach is different. |
| 535 |
Clang { zig_cc: bool }, |
| 536 |
/// Tool is the MSVC cl.exe. |
| 537 |
Msvc { clang_cl: bool }, |
| 538 |
} |
| 539 |
|
| 540 |
impl ToolFamily { |
| 541 |
/// What the flag to request debug info for this family of tools look like |
| 542 |
pub(crate) fn add_debug_flags( |
| 543 |
&self, |
| 544 |
cmd: &mut Tool, |
| 545 |
debug_opt: &str, |
| 546 |
dwarf_version: Option<u32>, |
| 547 |
) { |
| 548 |
match *self { |
| 549 |
ToolFamily::Msvc { .. } => { |
| 550 |
cmd.push_cc_arg("-Z7".into()); |
| 551 |
} |
| 552 |
ToolFamily::Gnu | ToolFamily::Clang { .. } => { |
| 553 |
match debug_opt { |
| 554 |
// From https://doc.rust-lang.org/cargo/reference/profiles.html#debug |
| 555 |
"" | "0" | "false" | "none" => { |
| 556 |
debug_assert!( |
| 557 |
false, |
| 558 |
"earlier check should have avoided calling add_debug_flags" |
| 559 |
); |
| 560 |
} |
| 561 |
|
| 562 |
// line-directives-only is LLVM-specific; for GCC we have to treat it like "1" |
| 563 |
"line-directives-only" if cmd.is_like_clang() => { |
| 564 |
cmd.push_cc_arg("-gline-directives-only".into()); |
| 565 |
} |
| 566 |
// Clang has -gline-tables-only, but it's an alias for -g1 anyway. |
| 567 |
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-gline-tables-only |
| 568 |
"1" | "limited" | "line-tables-only" | "line-directives-only" => { |
| 569 |
cmd.push_cc_arg("-g1".into()); |
| 570 |
} |
| 571 |
"2" | "true" | "full" => { |
| 572 |
cmd.push_cc_arg("-g".into()); |
| 573 |
} |
| 574 |
_ => { |
| 575 |
// Err on the side of including too much info rather than too little. |
| 576 |
cmd.push_cc_arg("-g".into()); |
| 577 |
} |
| 578 |
} |
| 579 |
if let Some(v) = dwarf_version { |
| 580 |
cmd.push_cc_arg(format!("-gdwarf-{v}").into()); |
| 581 |
} |
| 582 |
} |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
/// What the flags to enable all warnings |
| 587 |
pub(crate) fn warnings_flags(&self) -> &'static str { |
| 588 |
match *self { |
| 589 |
ToolFamily::Msvc { .. } => "-W4", |
| 590 |
ToolFamily::Gnu | ToolFamily::Clang { .. } => "-Wall", |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
pub(crate) fn warnings_suppression_flags(&self) -> &'static str { |
| 595 |
match *self { |
| 596 |
ToolFamily::Msvc { .. } => "-W0", |
| 597 |
ToolFamily::Gnu | ToolFamily::Clang { .. } => "-w", |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
/// What the flags to enable extra warnings |
| 602 |
pub(crate) fn extra_warnings_flags(&self) -> Option<&'static str> { |
| 603 |
match *self { |
| 604 |
ToolFamily::Msvc { .. } => None, |
| 605 |
ToolFamily::Gnu | ToolFamily::Clang { .. } => Some("-Wextra"), |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
/// What the flag to turn warning into errors |
| 610 |
pub(crate) fn warnings_to_errors_flag(&self) -> &'static str { |
| 611 |
match *self { |
| 612 |
ToolFamily::Msvc { .. } => "-WX", |
| 613 |
ToolFamily::Gnu | ToolFamily::Clang { .. } => "-Werror", |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
pub(crate) fn verbose_stderr(&self) -> bool { |
| 618 |
matches!(*self, ToolFamily::Clang { .. }) |
| 619 |
} |
| 620 |
} |