Skip to content

Commit f86048a

Browse files
committed
Don't output version flag on Clang
It is redundant, the version is already specified in `-target`.
1 parent 544c494 commit f86048a

File tree

3 files changed

+42
-22
lines changed

3 files changed

+42
-22
lines changed

src/lib.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -2527,22 +2527,22 @@ impl Build {
25272527
fn apple_flags(&self, cmd: &mut Tool) -> Result<(), Error> {
25282528
let target = self.get_target()?;
25292529

2530-
let min_version = self.apple_deployment_target(&target);
2531-
let version_flag = match (&*target.os, &*target.abi) {
2532-
("macos", "") => Some("-mmacosx-version-min"),
2533-
("ios", "") => Some("-miphoneos-version-min"),
2534-
("ios", "sim") => Some("-mios-simulator-version-min"),
2535-
("tvos", "") => Some("-mappletvos-version-min"),
2536-
("tvos", "sim") => Some("-mappletvsimulator-version-min"),
2537-
("watchos", "") => Some("-mwatchos-version-min"),
2538-
("watchos", "sim") => Some("-mwatchsimulator-version-min"),
2539-
// `-mxros-version-min` does not exist
2540-
// https://github.com/llvm/llvm-project/issues/88271
2541-
_ => None,
2542-
};
2543-
if let Some(version_flag) = version_flag {
2530+
// If the compiler is Clang, then we've already specifed the target
2531+
// information (including the deployment target) with the `--target`
2532+
// option, so we don't need to do anything further here.
2533+
//
2534+
// If the compiler is GCC, then we need to specify
2535+
// `-mmacosx-version-min` to set the deployment target, as well
2536+
// as to say that the target OS is macOS.
2537+
//
2538+
// NOTE: GCC does not support `-miphoneos-version-min=` etc. (because
2539+
// it does not support iOS in general), but we specify them anyhow in
2540+
// case we actually have a Clang-like compiler disguised as a GNU-like
2541+
// compiler, or in case GCC adds support for these in the future.
2542+
if !cmd.is_like_clang() {
2543+
let min_version = self.apple_deployment_target(&target);
25442544
cmd.args
2545-
.push(format!("{}={}", version_flag, min_version).into());
2545+
.push(target.apple_version_flag(&min_version).into());
25462546
}
25472547

25482548
// AppleClang sometimes requires sysroot even on macOS

src/target/apple.rs

+18
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,22 @@ impl TargetInfo {
1616
(os, _) => panic!("invalid Apple target OS {}", os),
1717
}
1818
}
19+
20+
pub(crate) fn apple_version_flag(&self, min_version: &str) -> String {
21+
match (&*self.os, &*self.abi) {
22+
("macos", "") => format!("-mmacosx-version-min={min_version}"),
23+
("ios", "") => format!("-miphoneos-version-min={min_version}"),
24+
("ios", "sim") => format!("-mios-simulator-version-min={min_version}"),
25+
("ios", "macabi") => format!("-mtargetos=ios{min_version}-macabi"),
26+
("tvos", "") => format!("-mappletvos-version-min={min_version}"),
27+
("tvos", "sim") => format!("-mappletvsimulator-version-min={min_version}"),
28+
("watchos", "") => format!("-mwatchos-version-min={min_version}"),
29+
("watchos", "sim") => format!("-mwatchsimulator-version-min={min_version}"),
30+
// `-mxros-version-min` does not exist
31+
// https://github.com/llvm/llvm-project/issues/88271
32+
("visionos", "") => format!("-mtargetos=xros{min_version}"),
33+
("visionos", "sim") => format!("-mtargetos=xros{min_version}-simulator"),
34+
(os, _) => panic!("invalid Apple target OS {}", os),
35+
}
36+
}
1937
}

tests/test.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,9 @@ fn gnu_apple_darwin() {
511511
for (arch, version) in &[("x86_64", "10.7"), ("aarch64", "11.0")] {
512512
let target = format!("{}-apple-darwin", arch);
513513
let test = Test::gnu();
514-
test.gcc()
514+
test.shim("fake-gcc")
515+
.gcc()
516+
.compiler("fake-gcc")
515517
.target(&target)
516518
.host(&target)
517519
// Avoid test maintenance when minimum supported OSes change.
@@ -520,8 +522,7 @@ fn gnu_apple_darwin() {
520522
.compile("foo");
521523

522524
let cmd = test.cmd(0);
523-
test.cmd(0)
524-
.must_have(format!("-mmacosx-version-min={}", version));
525+
cmd.must_have(format!("-mmacosx-version-min={version}"));
525526
cmd.must_not_have("-isysroot");
526527
}
527528
}
@@ -553,7 +554,7 @@ fn macos_cpp_minimums() {
553554
let deployment_arg = exec
554555
.args
555556
.iter()
556-
.find_map(|arg| arg.strip_prefix("-mmacosx-version-min="))
557+
.find_map(|arg| arg.strip_prefix("--target=x86_64-apple-macosx"))
557558
.expect("no deployment target argument was set");
558559

559560
let mut deployment_parts = deployment_arg.split('.').map(|v| v.parse::<u32>().unwrap());
@@ -581,7 +582,7 @@ fn macos_cpp_minimums() {
581582
.compile("foo");
582583

583584
// No C++ leaves it untouched
584-
test.cmd(0).must_have("-mmacosx-version-min=10.7");
585+
test.cmd(0).must_have("--target=x86_64-apple-macosx10.7");
585586
}
586587

587588
#[cfg(target_os = "macos")]
@@ -596,7 +597,7 @@ fn clang_apple_tvos() {
596597
.file("foo.c")
597598
.compile("foo");
598599

599-
test.cmd(0).must_have("-mappletvos-version-min=9.0");
600+
test.cmd(0).must_have("--target=arm64-apple-tvos9.0");
600601
}
601602

602603
#[cfg(target_os = "macos")]
@@ -648,7 +649,8 @@ fn clang_apple_tvsimulator() {
648649
.file("foo.c")
649650
.compile("foo");
650651

651-
test.cmd(0).must_have("-mappletvsimulator-version-min=9.0");
652+
test.cmd(0)
653+
.must_have("--target=x86_64-apple-tvos9.0-simulator");
652654
}
653655

654656
#[cfg(target_os = "macos")]

0 commit comments

Comments
 (0)