Skip to content

Commit

Permalink
Fix passing --target to Clang
Browse files Browse the repository at this point in the history
Use the same LLVM target triple as rustc does
  • Loading branch information
madsmtm committed Nov 1, 2024
1 parent 02b4aeb commit d351ff6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 86 deletions.
99 changes: 14 additions & 85 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1978,8 +1978,8 @@ impl Build {
// Disambiguate mingw and msvc on Windows. Problem is that
// depending on the origin clang can default to a mismatchig
// run-time.
// FIXME: Convert rustc target to Clang target.
cmd.push_cc_arg(format!("--target={raw_target}").into());
let llvm_target = target.versioned_llvm_target(None);
cmd.push_cc_arg(format!("--target={llvm_target}").into());
}

if cmd.is_like_clang() && target.os == "android" {
Expand Down Expand Up @@ -2065,77 +2065,7 @@ impl Build {
|| (target.os == "android"
&& android_clang_compiler_uses_target_arg_internally(&cmd.path)))
{
if target.os == "macos" {
let arch = map_darwin_target_from_rust_to_compiler_architecture(target);
cmd.args
.push(format!("--target={}-apple-darwin", arch).into());
} else if target.abi == "macabi" {
let arch = map_darwin_target_from_rust_to_compiler_architecture(target);
cmd.args
.push(format!("--target={}-apple-ios-macabi", arch).into());
} else if target.os == "ios" && target.abi == "sim" {
let arch = map_darwin_target_from_rust_to_compiler_architecture(target);
let deployment_target = self.apple_deployment_target(target);
cmd.args.push(
format!("--target={}-apple-ios{}-simulator", arch, deployment_target)
.into(),
);
} else if target.os == "watchos" && target.abi == "sim" {
let arch = map_darwin_target_from_rust_to_compiler_architecture(target);
let deployment_target = self.apple_deployment_target(target);
cmd.args.push(
format!(
"--target={}-apple-watchos{}-simulator",
arch, deployment_target
)
.into(),
);
} else if target.os == "tvos" && target.abi == "sim" {
let arch = map_darwin_target_from_rust_to_compiler_architecture(target);
let deployment_target = self.apple_deployment_target(target);
cmd.args.push(
format!(
"--target={}-apple-tvos{}-simulator",
arch, deployment_target
)
.into(),
);
} else if target.os == "tvos" {
let arch = map_darwin_target_from_rust_to_compiler_architecture(target);
let deployment_target = self.apple_deployment_target(target);
cmd.args.push(
format!("--target={}-apple-tvos{}", arch, deployment_target).into(),
);
} else if target.os == "visionos" && target.abi == "sim" {
let arch = map_darwin_target_from_rust_to_compiler_architecture(target);
let deployment_target = self.apple_deployment_target(target);
cmd.args.push(
format!(
"--target={}-apple-xros{}-simulator",
arch, deployment_target
)
.into(),
);
} else if target.os == "visionos" {
let arch = map_darwin_target_from_rust_to_compiler_architecture(target);
let deployment_target = self.apple_deployment_target(target);
cmd.args.push(
format!("--target={}-apple-xros{}", arch, deployment_target).into(),
);
} else if target.arch == "riscv32" || target.arch == "riscv64" {
// FIXME: Convert rustc target to Clang target
let (_, rest) = raw_target.split_once('-').unwrap();
cmd.args
.push(format!("--target={}-{}", &target.arch, rest).into());
} else if target.os == "uefi" {
if target.arch == "x86_64" {
cmd.args.push("--target=x86_64-unknown-windows-gnu".into());
} else if target.arch == "x86" {
cmd.args.push("--target=i686-unknown-windows-gnu".into())
} else if target.arch == "aarch64" {
cmd.args.push("--target=aarch64-unknown-windows-gnu".into())
}
} else if target.os == "freebsd" {
if target.os == "freebsd" {
// FreeBSD only supports C++11 and above when compiling against libc++
// (available from FreeBSD 10 onwards). Under FreeBSD, clang uses libc++ by
// default on FreeBSD 10 and newer unless `--target` is manually passed to
Expand All @@ -2157,18 +2087,17 @@ impl Build {
if self.cpp && self.cpp_set_stdlib.is_none() {
cmd.push_cc_arg("-stdlib=libc++".into());
}
}

// FIXME: Convert rustc target to Clang target.
cmd.push_cc_arg(format!("--target={}", raw_target).into());
} else if target.os == "windows" {
cmd.args.push(
format!("--target={}-pc-windows-{}", target.full_arch, target.env)
.into(),
)
// Add version information to the target.
let llvm_target = if target.vendor == "apple" {
let deployment_target = self.apple_deployment_target(&target);
target.versioned_llvm_target(Some(&deployment_target))
} else {
// FIXME: Convert rustc target to Clang target.
cmd.push_cc_arg(format!("--target={}", raw_target).into());
}
target.versioned_llvm_target(None)
};

cmd.args.push(format!("--target={llvm_target}").into());
}
}
ToolFamily::Msvc { clang_cl } => {
Expand All @@ -2184,8 +2113,8 @@ impl Build {
cmd.push_cc_arg("-m32".into());
cmd.push_cc_arg("-arch:IA32".into());
} else {
// FIXME: Convert rustc target to Clang target.
cmd.push_cc_arg(format!("--target={}", raw_target).into());
let llvm_target = target.versioned_llvm_target(None);
cmd.push_cc_arg(format!("--target={llvm_target}").into());
}
} else if target.full_arch == "i586" {
cmd.push_cc_arg("-arch:IA32".into());
Expand Down
1 change: 0 additions & 1 deletion src/target/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use super::TargetInfo;

impl TargetInfo {
/// The versioned LLVM/Clang target triple.
#[allow(unused)]
pub(crate) fn versioned_llvm_target(&self, version: Option<&str>) -> Cow<'_, str> {
if let Some(version) = version {
// Only support versioned Apple targets for now.
Expand Down

0 comments on commit d351ff6

Please sign in to comment.