Skip to content

Commit

Permalink
fix targets with zig toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Perez committed Mar 2, 2024
1 parent e9379a4 commit 96e9495
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 34 deletions.
55 changes: 31 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,15 +614,15 @@ impl Build {
if compiler.family.verbose_stderr() {
compiler.remove_arg("-v".into());
}
if compiler.family == ToolFamily::Clang {
if compiler.family.is_clang() {
// Avoid reporting that the arg is unsupported just because the
// compiler complains that it wasn't used.
compiler.push_cc_arg("-Wno-unused-command-line-argument".into());
}

let mut cmd = compiler.to_command();
let is_arm = target.contains("aarch64") || target.contains("arm");
let clang = compiler.family == ToolFamily::Clang;
let clang = compiler.family.is_clang();
let gnu = compiler.family == ToolFamily::Gnu;
command_add_output_file(
&mut cmd,
Expand Down Expand Up @@ -1567,7 +1567,7 @@ impl Build {
let target = self.get_target()?;
let msvc = target.contains("msvc");
let compiler = self.try_get_compiler()?;
let clang = compiler.family == ToolFamily::Clang;
let clang = compiler.family.is_clang();
let gnu = compiler.family == ToolFamily::Gnu;

let is_assembler_msvc = msvc && asm_ext == Some(AsmFileExt::DotAsm);
Expand Down Expand Up @@ -1724,7 +1724,7 @@ impl Build {
if let Some(ref std) = self.std {
let separator = match cmd.family {
ToolFamily::Msvc { .. } => ':',
ToolFamily::Gnu | ToolFamily::Clang => '=',
ToolFamily::Gnu | ToolFamily::Clang { .. } => '=',
};
cmd.push_cc_arg(format!("-std{}{}", separator, std).into());
}
Expand Down Expand Up @@ -1817,23 +1817,23 @@ impl Build {
_ => {}
}
}
ToolFamily::Gnu | ToolFamily::Clang => {
ToolFamily::Gnu | ToolFamily::Clang { .. } => {
// arm-linux-androideabi-gcc 4.8 shipped with Android NDK does
// not support '-Oz'
if opt_level == "z" && cmd.family != ToolFamily::Clang {
if opt_level == "z" && !cmd.family.is_clang() {
cmd.push_opt_unless_duplicate("-Os".into());
} else {
cmd.push_opt_unless_duplicate(format!("-O{}", opt_level).into());
}

if cmd.family == ToolFamily::Clang && target.contains("windows") {
if cmd.family.is_clang() && target.contains("windows") {
// Disambiguate mingw and msvc on Windows. Problem is that
// depending on the origin clang can default to a mismatchig
// run-time.
cmd.push_cc_arg(format!("--target={}", target).into());
}

if cmd.family == ToolFamily::Clang && target.contains("android") {
if cmd.family.is_clang() && target.contains("android") {
// For compatibility with code that doesn't use pre-defined `__ANDROID__` macro.
// If compiler used via ndk-build or cmake (officially supported build methods)
// this macros is defined.
Expand Down Expand Up @@ -1906,7 +1906,7 @@ impl Build {
if self.cpp {
match (self.cpp_set_stdlib.as_ref(), cmd.family) {
(None, _) => {}
(Some(stdlib), ToolFamily::Gnu) | (Some(stdlib), ToolFamily::Clang) => {
(Some(stdlib), ToolFamily::Gnu) | (Some(stdlib), ToolFamily::Clang { .. }) => {
cmd.push_cc_arg(format!("-stdlib=lib{}", stdlib).into());
}
_ => {
Expand All @@ -1920,23 +1920,21 @@ impl Build {

fn target_flags(&self, cmd: &mut Tool, target: &str) {
match cmd.family {
ToolFamily::Clang => {
if !(target.contains("android") && cmd.has_internal_target_arg) {
ToolFamily::Clang { is_zig } => {
let final_target = if !(target.contains("android") && cmd.has_internal_target_arg) {
if target.starts_with("riscv64gc-") {
cmd.args.push(
format!("--target={}", target.replace("riscv64gc", "riscv64")).into(),
);
Some(target.replace("riscv64gc", "riscv64"))
} else if target.starts_with("riscv32gc-") {
cmd.args.push(
format!("--target={}", target.replace("riscv32gc", "riscv32")).into(),
);
Some(target.replace("riscv32gc", "riscv32"))
} else if target.contains("uefi") {
if target.contains("x86_64") {
cmd.args.push("--target=x86_64-unknown-windows-gnu".into());
Some("x86_64-unknown-windows-gnu".into())
} else if target.contains("i686") {
cmd.args.push("--target=i686-unknown-windows-gnu".into())
Some("i686-unknown-windows-gnu".into())
} else if target.contains("aarch64") {
cmd.args.push("--target=aarch64-unknown-windows-gnu".into())
Some("aarch64-unknown-windows-gnu".into())
} else {
None
}
} else if target.ends_with("-freebsd") {
// FreeBSD only supports C++11 and above when compiling against libc++
Expand All @@ -1961,10 +1959,19 @@ impl Build {
cmd.push_cc_arg("-stdlib=libc++".into());
}

cmd.push_cc_arg(format!("--target={}", target).into());
Some(target.to_owned())
} else {
cmd.push_cc_arg(format!("--target={}", target).into());
Some(target.to_owned())
}
} else {
None
};
if let Some(mut final_target) = final_target {
if is_zig {
// Zig uses a different target triple format
final_target = final_target.replace("-unknown-", "-");
}
cmd.push_cc_arg(format!("--target={}", final_target).into());
}
}
ToolFamily::Msvc { clang_cl } => {
Expand Down Expand Up @@ -2523,7 +2530,7 @@ impl Build {
}
}
}
ToolFamily::Clang => {
ToolFamily::Clang { .. } => {
if target.contains("darwin") {
if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target)
{
Expand Down Expand Up @@ -3089,7 +3096,7 @@ impl Build {
// And even extend it to gcc targets by searching for "ar" instead
// of "llvm-ar"...
let compiler = self.get_base_compiler().ok()?;
if compiler.family == ToolFamily::Clang {
if compiler.family.is_clang() {
name = format!("llvm-{}", tool);
search_programs(&mut self.cmd(&compiler.path), &name, &self.cargo_output)
.map(|name| self.cmd(name))
Expand Down
29 changes: 19 additions & 10 deletions src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ impl Tool {
}
};
if stdout.contains("clang") {
ToolFamily::Clang
ToolFamily::Clang {
is_zig: stdout.contains("ziglang"),
}
} else if stdout.contains("GCC") {
ToolFamily::Gnu
} else {
Expand Down Expand Up @@ -130,7 +132,7 @@ impl Tool {
} else if fname.contains("clang") {
match clang_driver {
Some("cl") => ToolFamily::Msvc { clang_cl: true },
_ => ToolFamily::Clang,
_ => detect_family(&path),
}
} else {
detect_family(&path)
Expand Down Expand Up @@ -303,7 +305,7 @@ impl Tool {

/// Whether the tool is Clang-like.
pub fn is_like_clang(&self) -> bool {
self.family == ToolFamily::Clang
self.family.is_clang()
}

/// Whether the tool is AppleClang under .xctoolchain
Expand Down Expand Up @@ -337,7 +339,7 @@ pub enum ToolFamily {
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,
Clang { is_zig: bool },
/// Tool is the MSVC cl.exe.
Msvc { clang_cl: bool },
}
Expand All @@ -349,7 +351,7 @@ impl ToolFamily {
ToolFamily::Msvc { .. } => {
cmd.push_cc_arg("-Z7".into());
}
ToolFamily::Gnu | ToolFamily::Clang => {
ToolFamily::Gnu | ToolFamily::Clang { .. } => {
cmd.push_cc_arg(
dwarf_version
.map_or_else(|| "-g".into(), |v| format!("-gdwarf-{}", v))
Expand All @@ -362,7 +364,7 @@ impl ToolFamily {
/// What the flag to force frame pointers.
pub(crate) fn add_force_frame_pointer(&self, cmd: &mut Tool) {
match *self {
ToolFamily::Gnu | ToolFamily::Clang => {
ToolFamily::Gnu | ToolFamily::Clang { .. } => {
cmd.push_cc_arg("-fno-omit-frame-pointer".into());
}
_ => (),
Expand All @@ -373,27 +375,34 @@ impl ToolFamily {
pub(crate) fn warnings_flags(&self) -> &'static str {
match *self {
ToolFamily::Msvc { .. } => "-W4",
ToolFamily::Gnu | ToolFamily::Clang => "-Wall",
ToolFamily::Gnu | ToolFamily::Clang { .. } => "-Wall",
}
}

/// What the flags to enable extra warnings
pub(crate) fn extra_warnings_flags(&self) -> Option<&'static str> {
match *self {
ToolFamily::Msvc { .. } => None,
ToolFamily::Gnu | ToolFamily::Clang => Some("-Wextra"),
ToolFamily::Gnu | ToolFamily::Clang { .. } => Some("-Wextra"),
}
}

/// What the flag to turn warning into errors
pub(crate) fn warnings_to_errors_flag(&self) -> &'static str {
match *self {
ToolFamily::Msvc { .. } => "-WX",
ToolFamily::Gnu | ToolFamily::Clang => "-Werror",
ToolFamily::Gnu | ToolFamily::Clang { .. } => "-Werror",
}
}

pub(crate) fn verbose_stderr(&self) -> bool {
*self == ToolFamily::Clang
self.is_clang()
}

pub(crate) fn is_clang(&self) -> bool {
match *self {
ToolFamily::Clang { .. } => true,
_ => false,
}
}
}

0 comments on commit 96e9495

Please sign in to comment.