Skip to content

Commit cc342c5

Browse files
committed
tool: Expose family() in favour of incomplete is_like_xxx() wrappers
Instead of adding a new `is_like_xxx()` function every time new information is needed, or new enumeration variants would be added, provide the (now `non_exhaustive`) `enum` directly to callers to match what they're interested in. Also removes `is_like_clang_cl()` again from #1357 since that did not yet make it into a release, and would only cause unnecessary duplication in our API patterns.
1 parent 29a92bd commit cc342c5

File tree

3 files changed

+53
-38
lines changed

3 files changed

+53
-38
lines changed

dev-tools/cc-test/build.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ fn main() {
2323
run_forked_capture_output(&out, "metadata-off");
2424

2525
run_forked_capture_output(&out, "warnings-off");
26-
if cc::Build::new().get_compiler().is_like_msvc() {
26+
if matches!(
27+
cc::Build::new().get_compiler().family(),
28+
cc::ToolFamily::Msvc { .. }
29+
) {
2730
// MSVC doesn't output warnings to stderr, so we can't capture them.
2831
// the test will use this env var to know whether to run the test.
2932
println!("cargo:rustc-env=TEST_WARNINGS_ON=0");
@@ -86,12 +89,12 @@ fn main() {
8689
}
8790

8891
if target.contains("msvc") {
89-
let cc_frontend = if compiler.is_like_msvc() {
90-
"MSVC"
91-
} else if compiler.is_like_clang() {
92-
"CLANG"
93-
} else {
94-
unimplemented!("Unknown compiler that targets msvc but isn't clang-like or msvc-like")
92+
let cc_frontend = match compiler.family() {
93+
cc::ToolFamily::Clang { .. } => "CLANG",
94+
cc::ToolFamily::Msvc { .. } => "MSVC",
95+
f => unimplemented!(
96+
"Unknown compiler `{f:?}` that targets msvc but isn't clang-like or msvc-like"
97+
),
9598
};
9699

97100
// Test that the `windows_registry` module will set PATH by looking for

src/lib.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,7 @@ mod command_helpers;
253253
use command_helpers::*;
254254

255255
mod tool;
256-
pub use tool::Tool;
257-
use tool::ToolFamily;
256+
pub use tool::{Tool, ToolFamily};
258257

259258
mod tempfile;
260259

@@ -701,25 +700,25 @@ impl Build {
701700
if compiler.family.verbose_stderr() {
702701
compiler.remove_arg("-v".into());
703702
}
704-
if compiler.is_like_clang() {
703+
let clang = matches!(compiler.family(), ToolFamily::Clang { .. });
704+
if clang {
705705
// Avoid reporting that the arg is unsupported just because the
706706
// compiler complains that it wasn't used.
707707
compiler.push_cc_arg("-Wno-unused-command-line-argument".into());
708708
}
709709

710710
let mut cmd = compiler.to_command();
711711
let is_arm = matches!(target.arch, "aarch64" | "arm");
712-
let clang = compiler.is_like_clang();
713-
let gnu = compiler.family == ToolFamily::Gnu;
712+
let msvc = matches!(compiler.family(), ToolFamily::Msvc { .. });
714713
command_add_output_file(
715714
&mut cmd,
716715
&obj,
717716
CmdAddOutputFileArgs {
718717
cuda: self.cuda,
719718
is_assembler_msvc: false,
720-
msvc: compiler.is_like_msvc(),
719+
msvc,
721720
clang,
722-
gnu,
721+
gnu: matches!(compiler.family(), ToolFamily::Gnu),
723722
is_asm: false,
724723
is_arm,
725724
},
@@ -733,7 +732,7 @@ impl Build {
733732

734733
// On MSVC skip the CRT by setting the entry point to `main`.
735734
// This way we don't need to add the default library paths.
736-
if compiler.is_like_msvc() {
735+
if msvc {
737736
// Flags from _LINK_ are appended to the linker arguments.
738737
cmd.env("_LINK_", "-entry:main");
739738
}
@@ -1753,8 +1752,6 @@ impl Build {
17531752
let target = self.get_target()?;
17541753
let msvc = target.env == "msvc";
17551754
let compiler = self.try_get_compiler()?;
1756-
let clang = compiler.is_like_clang();
1757-
let gnu = compiler.family == ToolFamily::Gnu;
17581755

17591756
let is_assembler_msvc = msvc && asm_ext == Some(AsmFileExt::DotAsm);
17601757
let (mut cmd, name) = if is_assembler_msvc {
@@ -1782,9 +1779,9 @@ impl Build {
17821779
CmdAddOutputFileArgs {
17831780
cuda: self.cuda,
17841781
is_assembler_msvc,
1785-
msvc: compiler.is_like_msvc(),
1786-
clang,
1787-
gnu,
1782+
msvc: matches!(compiler.family(), ToolFamily::Msvc { .. }),
1783+
clang: matches!(compiler.family(), ToolFamily::Clang { .. }),
1784+
gnu: matches!(compiler.family(), ToolFamily::Gnu),
17881785
is_asm,
17891786
is_arm,
17901787
},
@@ -2036,15 +2033,16 @@ impl Build {
20362033
}
20372034
}
20382035
ToolFamily::Gnu | ToolFamily::Clang { .. } => {
2036+
let clang = matches!(cmd.family, ToolFamily::Clang { .. });
20392037
// arm-linux-androideabi-gcc 4.8 shipped with Android NDK does
20402038
// not support '-Oz'
2041-
if opt_level == "z" && !cmd.is_like_clang() {
2039+
if opt_level == "z" && !clang {
20422040
cmd.push_opt_unless_duplicate("-Os".into());
20432041
} else {
20442042
cmd.push_opt_unless_duplicate(format!("-O{}", opt_level).into());
20452043
}
20462044

2047-
if cmd.is_like_clang() && target.os == "android" {
2045+
if clang && target.os == "android" {
20482046
// For compatibility with code that doesn't use pre-defined `__ANDROID__` macro.
20492047
// If compiler used via ndk-build or cmake (officially supported build methods)
20502048
// this macros is defined.
@@ -2141,7 +2139,9 @@ impl Build {
21412139
family.add_force_frame_pointer(cmd);
21422140
}
21432141

2144-
if !cmd.is_like_msvc() {
2142+
let msvc = matches!(cmd.family, ToolFamily::Msvc { .. });
2143+
2144+
if !msvc {
21452145
if target.arch == "x86" {
21462146
cmd.args.push("-m32".into());
21472147
} else if target.abi == "x32" {
@@ -2653,7 +2653,8 @@ impl Build {
26532653
// it does not support iOS in general), but we specify them anyhow in
26542654
// case we actually have a Clang-like compiler disguised as a GNU-like
26552655
// compiler, or in case GCC adds support for these in the future.
2656-
if !cmd.is_like_clang() {
2656+
let clang = matches!(cmd.family, ToolFamily::Clang { .. });
2657+
if !clang {
26572658
let min_version = self.apple_deployment_target(&target);
26582659
cmd.args
26592660
.push(target.apple_version_flag(&min_version).into());
@@ -3225,7 +3226,8 @@ impl Build {
32253226
// And even extend it to gcc targets by searching for "ar" instead
32263227
// of "llvm-ar"...
32273228
let compiler = self.get_base_compiler().ok()?;
3228-
if compiler.is_like_clang() {
3229+
let clang = matches!(compiler.family, ToolFamily::Clang { .. });
3230+
if clang {
32293231
name = format!("llvm-{}", tool).into();
32303232
self.search_programs(
32313233
&mut self.cmd(&compiler.path),

src/tool.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,11 @@ impl Tool {
281281
let mut chars = flag.chars();
282282

283283
// Only duplicate check compiler flags
284-
if self.is_like_msvc() {
285-
if chars.next() != Some('/') {
286-
return false;
287-
}
288-
} else if (self.is_like_gnu() || self.is_like_clang()) && chars.next() != Some('-') {
284+
let flag_start = match self.family {
285+
ToolFamily::Msvc { .. } => '/',
286+
ToolFamily::Gnu | ToolFamily::Clang { .. } => '-',
287+
};
288+
if chars.next() != Some(flag_start) {
289289
return false;
290290
}
291291

@@ -395,6 +395,11 @@ impl Tool {
395395
flags
396396
}
397397

398+
/// The family of this tool, representing convention of arguments etc.
399+
pub fn family(&self) -> ToolFamily {
400+
self.family
401+
}
402+
398403
/// Whether the tool is GNU Compiler Collection-like.
399404
pub fn is_like_gnu(&self) -> bool {
400405
self.family == ToolFamily::Gnu
@@ -421,11 +426,6 @@ impl Tool {
421426
matches!(self.family, ToolFamily::Msvc { .. })
422427
}
423428

424-
/// Whether the tool is `clang-cl`-based MSVC-like.
425-
pub fn is_like_clang_cl(&self) -> bool {
426-
matches!(self.family, ToolFamily::Msvc { clang_cl: true })
427-
}
428-
429429
/// Supports using `--` delimiter to separate arguments and path to source files.
430430
pub(crate) fn supports_path_delimiter(&self) -> bool {
431431
matches!(
@@ -441,14 +441,24 @@ impl Tool {
441441
///
442442
/// Detection of a family is done on best-effort basis and may not accurately reflect the tool.
443443
#[derive(Copy, Clone, Debug, PartialEq)]
444+
#[non_exhaustive]
444445
pub enum ToolFamily {
445446
/// Tool is GNU Compiler Collection-like.
447+
#[non_exhaustive]
446448
Gnu,
447449
/// Tool is Clang-like. It differs from the GCC in a sense that it accepts superset of flags
448450
/// and its cross-compilation approach is different.
449-
Clang { zig_cc: bool },
450-
/// Tool is the MSVC cl.exe.
451-
Msvc { clang_cl: bool },
451+
#[non_exhaustive]
452+
Clang {
453+
/// Tool provided by zig
454+
zig_cc: bool,
455+
},
456+
/// Tool is the MSVC `cl.exe`.
457+
#[non_exhaustive]
458+
Msvc {
459+
/// Whether this is `clang-cl` provided by LLVM
460+
clang_cl: bool,
461+
},
452462
}
453463

454464
impl ToolFamily {

0 commit comments

Comments
 (0)