Skip to content

Commit 8533e8c

Browse files
authored
Pass deployment target with -m*-version-min= (#1339)
1 parent bf4dcf7 commit 8533e8c

File tree

8 files changed

+342
-378
lines changed

8 files changed

+342
-378
lines changed

dev-tools/gen-target-info/src/main.rs

+4-15
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,9 @@ fn generate_target_mapping(f: &mut File, target_specs: &RustcTargetSpecs) -> std
2626
let env = spec.env.as_deref().unwrap_or("");
2727
let abi = spec.abi.as_deref().unwrap_or("");
2828

29-
let unversioned_llvm_target = if spec.llvm_target.contains("apple") {
30-
// Remove deployment target information from LLVM target triples (we
31-
// will add this in another part of CC).
32-
//
33-
// FIXME(madsmtm): Should become unnecessary after
34-
// https://github.com/rust-lang/rust/pull/131037
35-
let mut components = spec.llvm_target.split("-").collect::<Vec<_>>();
36-
37-
components[2] = components[2].trim_end_matches(|c: char| c.is_numeric() || c == '.');
38-
39-
components.join("-")
29+
// FIXME(madsmtm): Unnecessary once we bump MSRV to Rust 1.74
30+
let llvm_target = if spec.llvm_target == "armv7-apple-ios7.0.0" {
31+
"armv7-apple-ios".to_string()
4032
} else if os == "uefi" {
4133
// Override the UEFI LLVM targets.
4234
//
@@ -80,10 +72,7 @@ fn generate_target_mapping(f: &mut File, target_specs: &RustcTargetSpecs) -> std
8072
writeln!(f, " os: {os:?},")?;
8173
writeln!(f, " env: {env:?},")?;
8274
writeln!(f, " abi: {abi:?},")?;
83-
writeln!(
84-
f,
85-
" unversioned_llvm_target: {unversioned_llvm_target:?},"
86-
)?;
75+
writeln!(f, " llvm_target: {llvm_target:?},")?;
8776
writeln!(f, " }},")?;
8877
writeln!(f, " ),")?;
8978
}

src/lib.rs

+15-29
Original file line numberDiff line numberDiff line change
@@ -2188,17 +2188,14 @@ impl Build {
21882188
}
21892189
}
21902190

2191-
// Add version information to the target.
2192-
let llvm_target = if target.vendor == "apple" {
2193-
let deployment_target = self.apple_deployment_target(target);
2194-
target.versioned_llvm_target(Some(&deployment_target))
2195-
} else {
2196-
target.versioned_llvm_target(None)
2197-
};
2198-
2199-
// Pass `--target` with the LLVM target to properly
2200-
// configure Clang even when cross-compiling.
2201-
cmd.push_cc_arg(format!("--target={llvm_target}").into());
2191+
// Pass `--target` with the LLVM target to properly configure Clang even when
2192+
// cross-compiling.
2193+
//
2194+
// We intentionally don't put the deployment version in here on Apple targets,
2195+
// and instead pass that via `-mmacosx-version-min=` and similar flags, for
2196+
// better compatibility with older versions of Clang that has poor support for
2197+
// versioned target names (especially when it comes to configuration files).
2198+
cmd.push_cc_arg(format!("--target={}", target.llvm_target).into());
22022199
}
22032200
}
22042201
ToolFamily::Msvc { clang_cl } => {
@@ -2214,8 +2211,7 @@ impl Build {
22142211
cmd.push_cc_arg("-m32".into());
22152212
cmd.push_cc_arg("-arch:IA32".into());
22162213
} else {
2217-
let llvm_target = target.versioned_llvm_target(None);
2218-
cmd.push_cc_arg(format!("--target={llvm_target}").into());
2214+
cmd.push_cc_arg(format!("--target={}", target.llvm_target).into());
22192215
}
22202216
} else if target.full_arch == "i586" {
22212217
cmd.push_cc_arg("-arch:IA32".into());
@@ -2645,23 +2641,13 @@ impl Build {
26452641
fn apple_flags(&self, cmd: &mut Tool) -> Result<(), Error> {
26462642
let target = self.get_target()?;
26472643

2648-
// If the compiler is Clang, then we've already specifed the target
2649-
// information (including the deployment target) with the `--target`
2650-
// option, so we don't need to do anything further here.
2651-
//
2652-
// If the compiler is GCC, then we need to specify
2653-
// `-mmacosx-version-min` to set the deployment target, as well
2654-
// as to say that the target OS is macOS.
2644+
// Pass the deployment target via `-mmacosx-version-min=`, `-mtargetos=` and similar.
26552645
//
2656-
// NOTE: GCC does not support `-miphoneos-version-min=` etc. (because
2657-
// it does not support iOS in general), but we specify them anyhow in
2658-
// case we actually have a Clang-like compiler disguised as a GNU-like
2659-
// compiler, or in case GCC adds support for these in the future.
2660-
if !cmd.is_like_clang() {
2661-
let min_version = self.apple_deployment_target(&target);
2662-
cmd.args
2663-
.push(target.apple_version_flag(&min_version).into());
2664-
}
2646+
// It is also necessary on GCC, as it forces a compilation error if the compiler is not
2647+
// configured for Darwin: https://gcc.gnu.org/onlinedocs/gcc/Darwin-Options.html
2648+
let min_version = self.apple_deployment_target(&target);
2649+
cmd.args
2650+
.push(target.apple_version_flag(&min_version).into());
26652651

26662652
// AppleClang sometimes requires sysroot even on macOS
26672653
if cmd.is_xctoolchain_clang() || target.os != "macos" {

src/target.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ pub(crate) struct TargetInfo<'a> {
4444
/// This is the same as the value of `cfg!(target_abi)`.
4545
pub abi: &'a str,
4646
/// The unversioned LLVM/Clang target triple.
47-
unversioned_llvm_target: &'a str,
47+
///
48+
/// NOTE: You should never need to match on this explicitly, use the other
49+
/// fields on [`TargetInfo`] instead.
50+
pub llvm_target: &'a str,
4851
}
4952

5053
impl FromStr for TargetInfo<'_> {

src/target/apple.rs

+12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ impl TargetInfo<'_> {
1818
}
1919

2020
pub(crate) fn apple_version_flag(&self, min_version: &str) -> String {
21+
// There are many aliases for these, and `-mtargetos=` is preferred on Clang nowadays, but
22+
// for compatibility with older Clang, we use the earliest supported name here.
23+
//
24+
// NOTE: GCC does not support `-miphoneos-version-min=` etc. (because it does not support
25+
// iOS in general), but we specify them anyhow in case we actually have a Clang-like
26+
// compiler disguised as a GNU-like compiler, or in case GCC adds support for these in the
27+
// future.
28+
//
29+
// See also:
30+
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mmacos-version-min
31+
// https://clang.llvm.org/docs/AttributeReference.html#availability
32+
// https://gcc.gnu.org/onlinedocs/gcc/Darwin-Options.html#index-mmacosx-version-min
2133
match (self.os, self.abi) {
2234
("macos", "") => format!("-mmacosx-version-min={min_version}"),
2335
("ios", "") => format!("-miphoneos-version-min={min_version}"),

0 commit comments

Comments
 (0)