Skip to content

Commit

Permalink
tool: Expose family() and deprecate is_like_xxx() wrappers
Browse files Browse the repository at this point in the history
Intead 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.  Deprecate the existing functions
to point users to the new pattern.
  • Loading branch information
MarijnS95 committed Jan 10, 2025
1 parent 29a92bd commit c5d5bde
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,7 @@ mod command_helpers;
use command_helpers::*;

mod tool;
pub use tool::Tool;
use tool::ToolFamily;
pub use tool::{Tool, ToolFamily};

mod tempfile;

Expand Down Expand Up @@ -710,7 +709,7 @@ impl Build {
let mut cmd = compiler.to_command();
let is_arm = matches!(target.arch, "aarch64" | "arm");
let clang = compiler.is_like_clang();
let gnu = compiler.family == ToolFamily::Gnu;
let gnu = compiler.is_like_gnu();
command_add_output_file(
&mut cmd,
&obj,
Expand Down Expand Up @@ -1754,7 +1753,7 @@ impl Build {
let msvc = target.env == "msvc";
let compiler = self.try_get_compiler()?;
let clang = compiler.is_like_clang();
let gnu = compiler.family == ToolFamily::Gnu;
let gnu = compiler.is_like_gnu();

let is_assembler_msvc = msvc && asm_ext == Some(AsmFileExt::DotAsm);
let (mut cmd, name) = if is_assembler_msvc {
Expand Down
27 changes: 24 additions & 3 deletions src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,33 +395,44 @@ impl Tool {
flags
}

/// The family of this tool, representing convention of arguments etc.
pub fn family(&self) -> ToolFamily {
self.family
}

/// Whether the tool is GNU Compiler Collection-like.
#[deprecated = "Consider matching against the ToolFamily returned by family() instead"]
pub fn is_like_gnu(&self) -> bool {
self.family == ToolFamily::Gnu
}

/// Whether the tool is Clang-like.
#[deprecated = "Consider matching against the ToolFamily returned by family() instead"]
pub fn is_like_clang(&self) -> bool {
matches!(self.family, ToolFamily::Clang { .. })
}

/// Whether the tool is AppleClang under .xctoolchain
#[cfg(target_vendor = "apple")]
#[deprecated = "Consider matching against the ToolFamily returned by family() instead"]
pub(crate) fn is_xctoolchain_clang(&self) -> bool {
let path = self.path.to_string_lossy();
path.contains(".xctoolchain/")
}
#[cfg(not(target_vendor = "apple"))]
#[deprecated = "Consider matching against the ToolFamily returned by family() instead"]
pub(crate) fn is_xctoolchain_clang(&self) -> bool {
false
}

/// Whether the tool is MSVC-like.
#[deprecated = "Consider matching against the ToolFamily returned by family() instead"]
pub fn is_like_msvc(&self) -> bool {
matches!(self.family, ToolFamily::Msvc { .. })
}

/// Whether the tool is `clang-cl`-based MSVC-like.
#[deprecated = "Consider matching against the ToolFamily returned by family() instead"]
pub fn is_like_clang_cl(&self) -> bool {
matches!(self.family, ToolFamily::Msvc { clang_cl: true })
}
Expand All @@ -441,14 +452,24 @@ impl Tool {
///
/// Detection of a family is done on best-effort basis and may not accurately reflect the tool.
#[derive(Copy, Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum ToolFamily {
/// Tool is GNU Compiler Collection-like.
#[non_exhaustive]
Gnu,
/// Tool is Clang-like. It differs from the GCC in a sense that it accepts superset of flags
/// and its cross-compilation approach is different.
Clang { zig_cc: bool },
/// Tool is the MSVC cl.exe.
Msvc { clang_cl: bool },
#[non_exhaustive]
Clang {
/// Tool provided by zig
zig_cc: bool,
},
/// Tool is the MSVC `cl.exe`.
#[non_exhaustive]
Msvc {
/// Whether this is `clang-cl` provided by LLVM
clang_cl: bool,
},
}

impl ToolFamily {
Expand Down

0 comments on commit c5d5bde

Please sign in to comment.