Skip to content

Commit 29431fa

Browse files
authored
Don't specify both -target and -mtargetos= on Apple targets (#1384)
1 parent 81929fa commit 29431fa

File tree

5 files changed

+110
-53
lines changed

5 files changed

+110
-53
lines changed

.github/workflows/main.yml

+57-33
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ jobs:
2222
beta,
2323
nightly,
2424
linux32,
25-
macos,
2625
aarch64-macos,
26+
x86_64-macos,
2727
aarch64-ios,
28+
aarch64-ios-sim,
29+
x86_64-ios-sim,
30+
aarch64-ios-macabi,
31+
x86_64-ios-macabi,
2832
win32,
2933
win64,
3034
mingw32,
@@ -49,19 +53,39 @@ jobs:
4953
os: ubuntu-latest
5054
rust: stable
5155
target: i686-unknown-linux-gnu
52-
- build: macos
53-
os: macos-latest
54-
rust: stable
55-
target: x86_64-apple-darwin
5656
- build: aarch64-macos
5757
os: macos-14
5858
rust: stable
5959
target: aarch64-apple-darwin
60+
- build: x86_64-macos
61+
os: macos-13 # x86
62+
rust: stable
63+
target: x86_64-apple-darwin
6064
- build: aarch64-ios
6165
os: macos-latest
6266
rust: stable
6367
target: aarch64-apple-ios
6468
no_run: --no-run
69+
- build: aarch64-ios-sim
70+
os: macos-latest
71+
rust: stable
72+
target: aarch64-apple-ios-sim
73+
no_run: --no-run
74+
- build: x86_64-ios-sim
75+
os: macos-13 # x86
76+
rust: stable
77+
target: x86_64-apple-ios # Simulator
78+
no_run: --no-run
79+
- build: aarch64-ios-macabi
80+
os: macos-latest
81+
rust: stable
82+
target: aarch64-apple-ios-macabi
83+
no_run: --no-run # FIXME(madsmtm): Fix running tests
84+
- build: x86_64-ios-macabi
85+
os: macos-13 # x86
86+
rust: stable
87+
target: x86_64-apple-ios-macabi
88+
no_run: --no-run # FIXME(madsmtm): Fix running tests
6589
- build: windows-aarch64
6690
os: windows-latest
6791
rust: stable
@@ -139,42 +163,42 @@ jobs:
139163
- run: cargo test ${{ matrix.no_run }} --workspace --target ${{ matrix.target }} ${{ matrix.cargo_flags }}
140164

141165
# This is separate from the matrix above because there is no prebuilt rust-std component for these targets.
142-
check-tvos:
166+
check-build-std:
143167
name: Test build-std
144-
runs-on: ${{ matrix.os }}
168+
runs-on: macos-latest
145169
strategy:
146170
matrix:
147-
build: [aarch64-tvos, aarch64-tvos-sim, x86_64-tvos]
148-
include:
149-
- build: aarch64-tvos
150-
os: macos-latest
151-
rust: nightly
152-
target: aarch64-apple-tvos
153-
no_run: --no-run
154-
- build: aarch64-tvos-sim
155-
os: macos-latest
156-
rust: nightly
157-
target: aarch64-apple-tvos-sim
158-
no_run: --no-run
159-
- build: x86_64-tvos
160-
os: macos-latest
161-
rust: nightly
162-
target: x86_64-apple-tvos
163-
no_run: --no-run
171+
target:
172+
- x86_64h-apple-darwin
173+
# FIXME(madsmtm): needs deployment target
174+
# - armv7s-apple-ios
175+
# FIXME(madsmtm): needs deployment target
176+
# - i386-apple-ios # Simulator
177+
- aarch64-apple-tvos
178+
- aarch64-apple-tvos-sim
179+
- x86_64-apple-tvos # Simulator
180+
- aarch64-apple-watchos
181+
- aarch64-apple-watchos-sim
182+
- x86_64-apple-watchos-sim
183+
# FIXME(madsmtm): needs deployment target
184+
# - arm64_32-apple-watchos
185+
- armv7k-apple-watchos
186+
- aarch64-apple-visionos
187+
- aarch64-apple-visionos-sim
164188
steps:
165189
- uses: actions/checkout@v4
166190
- name: Install Rust (rustup)
167191
run: |
168192
set -euxo pipefail
169-
rustup toolchain install ${{ matrix.rust }} --no-self-update --profile minimal
170-
rustup component add rust-src --toolchain ${{ matrix.rust }}
171-
rustup default ${{ matrix.rust }}
193+
rustup toolchain install nightly --no-self-update --profile minimal
194+
rustup component add rust-src --toolchain nightly
195+
rustup default nightly
172196
shell: bash
173197
- run: cargo update
174198
- uses: Swatinem/rust-cache@v2
175-
- run: cargo test -Z build-std=std ${{ matrix.no_run }} --workspace --target ${{ matrix.target }}
176-
- run: cargo test -Z build-std=std ${{ matrix.no_run }} --workspace --target ${{ matrix.target }} --release
177-
- run: cargo test -Z build-std=std ${{ matrix.no_run }} --workspace --target ${{ matrix.target }} --features parallel
199+
- run: cargo test -Z build-std=std --no-run --workspace --target ${{ matrix.target }}
200+
- run: cargo test -Z build-std=std --no-run --workspace --target ${{ matrix.target }} --release
201+
- run: cargo test -Z build-std=std --no-run --workspace --target ${{ matrix.target }} --features parallel
178202

179203
check-wasm:
180204
name: Test wasm
@@ -188,7 +212,7 @@ jobs:
188212
run: |
189213
rustup target add ${{ matrix.target }}
190214
shell: bash
191-
- run: cargo update
215+
- run: cargo update
192216
- uses: Swatinem/rust-cache@v2
193217
- run: cargo test --no-run --target ${{ matrix.target }}
194218
- run: cargo test --no-run --target ${{ matrix.target }} --release
@@ -251,7 +275,7 @@ jobs:
251275
sudo dpkg -i cuda-keyring_1.0-1_all.deb
252276
sudo apt-get update
253277
sudo apt-get -y install cuda-minimal-build-11-8
254-
- run: cargo update
278+
- run: cargo update
255279
- uses: Swatinem/rust-cache@v2
256280
- name: Test 'cudart' feature
257281
shell: bash
@@ -321,7 +345,7 @@ jobs:
321345
name: Tests pass
322346
needs:
323347
- test
324-
- check-tvos
348+
- check-build-std
325349
- check-wasm
326350
- test-wasm32-wasip1-thread
327351
- cuda

dev-tools/cc-test/build.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,17 @@ fn main() {
5050
.compile("bar");
5151

5252
let target = std::env::var("TARGET").unwrap();
53-
let file = target.split('-').next().unwrap();
53+
let arch = match target.split('-').next().unwrap() {
54+
"arm64_32" => "aarch64",
55+
"armv7k" => "armv7",
56+
"armv7s" => "armv7",
57+
"i386" => "i686",
58+
"x86_64h" => "x86_64",
59+
arch => arch,
60+
};
5461
let file = format!(
5562
"src/{}.{}",
56-
file,
63+
arch,
5764
if target.contains("msvc") { "asm" } else { "S" }
5865
);
5966
cc::Build::new().file(file).compile("asm");

dev-tools/cc-test/src/armv7.S

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
.globl asm
2+
.balign 4
23
asm:
34
mov r0, #7
45
bx lr
6+
7+
.globl _asm
8+
.balign 4
9+
_asm:
10+
mov r0, #7
11+
bx lr

src/lib.rs

+24-13
Original file line numberDiff line numberDiff line change
@@ -2188,14 +2188,23 @@ impl Build {
21882188
}
21892189
}
21902190

2191-
// Pass `--target` with the LLVM target to properly configure Clang even when
2192-
// cross-compiling.
2191+
// Pass `--target` with the LLVM target to configure Clang for cross-compiling.
21932192
//
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());
2193+
// NOTE: In the past, we passed this, along with the deployment version in here
2194+
// on Apple targets, but versioned targets were found to have poor compatibility
2195+
// with older versions of Clang, especially around comes to configuration files.
2196+
//
2197+
// Instead, we specify `-arch` along with `-mmacosx-version-min=`, `-mtargetos=`
2198+
// and similar flags in `.apple_flags()`.
2199+
//
2200+
// Note that Clang errors when both `-mtargetos=` and `-target` are specified,
2201+
// so we omit this entirely on Apple targets (it's redundant when specifying
2202+
// both the `-arch` and the deployment target / OS flag) (in theory we _could_
2203+
// specify this on some of the Apple targets that use the older
2204+
// `-m*-version-min=`, but for consistency we omit it entirely).
2205+
if target.vendor != "apple" {
2206+
cmd.push_cc_arg(format!("--target={}", target.llvm_target).into());
2207+
}
21992208
}
22002209
}
22012210
ToolFamily::Msvc { clang_cl } => {
@@ -2233,12 +2242,6 @@ impl Build {
22332242
}
22342243
}
22352244
ToolFamily::Gnu => {
2236-
if target.vendor == "apple" {
2237-
let arch = map_darwin_target_from_rust_to_compiler_architecture(target);
2238-
cmd.args.push("-arch".into());
2239-
cmd.args.push(arch.into());
2240-
}
2241-
22422245
if target.vendor == "kmc" {
22432246
cmd.args.push("-finput-charset=utf-8".into());
22442247
}
@@ -2641,6 +2644,14 @@ impl Build {
26412644
fn apple_flags(&self, cmd: &mut Tool) -> Result<(), Error> {
26422645
let target = self.get_target()?;
26432646

2647+
// Add `-arch` on all compilers. This is a Darwin/Apple-specific flag
2648+
// that works both on GCC and Clang.
2649+
// https://gcc.gnu.org/onlinedocs/gcc/Darwin-Options.html#:~:text=arch
2650+
// https://clang.llvm.org/docs/CommandGuide/clang.html#cmdoption-arch
2651+
let arch = map_darwin_target_from_rust_to_compiler_architecture(&target);
2652+
cmd.args.push("-arch".into());
2653+
cmd.args.push(arch.into());
2654+
26442655
// Pass the deployment target via `-mmacosx-version-min=`, `-mtargetos=` and similar.
26452656
//
26462657
// It is also necessary on GCC, as it forces a compilation error if the compiler is not

tests/test.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,8 @@ fn asm_flags() {
525525

526526
#[test]
527527
fn gnu_apple_darwin() {
528-
for (arch, version) in &[("x86_64", "10.7"), ("aarch64", "11.0")] {
528+
for (arch, ld64_arch, version) in &[("x86_64", "x86_64", "10.7"), ("aarch64", "arm64", "11.0")]
529+
{
529530
let target = format!("{}-apple-darwin", arch);
530531
let test = Test::gnu();
531532
test.shim("fake-gcc")
@@ -539,6 +540,7 @@ fn gnu_apple_darwin() {
539540
.compile("foo");
540541

541542
let cmd = test.cmd(0);
543+
cmd.must_have_in_order("-arch", ld64_arch);
542544
cmd.must_have(format!("-mmacosx-version-min={version}"));
543545
cmd.must_not_have("-isysroot");
544546
}
@@ -614,6 +616,7 @@ fn clang_apple_tvos() {
614616
.file("foo.c")
615617
.compile("foo");
616618

619+
test.cmd(0).must_have_in_order("-arch", "arm64");
617620
test.cmd(0).must_have("-mappletvos-version-min=9.0");
618621
}
619622

@@ -637,7 +640,9 @@ fn clang_apple_mac_catalyst() {
637640
.compile("foo");
638641
let execution = test.cmd(0);
639642

640-
execution.must_have("--target=arm64-apple-ios-macabi");
643+
execution.must_have_in_order("-arch", "arm64");
644+
// --target and -mtargetos= don't mix
645+
execution.must_not_have("--target=arm64-apple-ios-macabi");
641646
execution.must_have("-mtargetos=ios15.0-macabi");
642647
execution.must_have_in_order("-isysroot", sdkroot);
643648
execution.must_have_in_order(
@@ -667,8 +672,7 @@ fn clang_apple_tvsimulator() {
667672
.file("foo.c")
668673
.compile("foo");
669674

670-
test.cmd(0)
671-
.must_have("--target=x86_64-apple-tvos-simulator");
675+
test.cmd(0).must_have_in_order("-arch", "x86_64");
672676
test.cmd(0).must_have("-mappletvsimulator-version-min=9.0");
673677
}
674678

@@ -693,8 +697,12 @@ fn clang_apple_visionos() {
693697

694698
dbg!(test.cmd(0).args);
695699

696-
test.cmd(0).must_have("--target=arm64-apple-xros");
700+
test.cmd(0).must_have_in_order("-arch", "arm64");
701+
// --target and -mtargetos= don't mix.
702+
test.cmd(0).must_not_have("--target=arm64-apple-xros");
697703
test.cmd(0).must_have("-mtargetos=xros1.0");
704+
705+
// Flags that don't exist.
698706
test.cmd(0).must_not_have("-mxros-version-min=1.0");
699707
test.cmd(0).must_not_have("-mxrsimulator-version-min=1.0");
700708
}

0 commit comments

Comments
 (0)