Skip to content

Commit c788dcc

Browse files
committed
Test codegen when setting deployment target
1 parent 9afe713 commit c788dcc

File tree

7 files changed

+177
-27
lines changed

7 files changed

+177
-27
lines changed

src/tools/run-make-support/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub use env::{env_var, env_var_os, set_current_dir};
7373
pub use run::{cmd, run, run_fail, run_with_args};
7474

7575
/// Helpers for checking target information.
76-
pub use targets::{is_darwin, is_msvc, is_windows, llvm_components_contain, target, uname};
76+
pub use targets::{is_darwin, is_msvc, is_windows, llvm_components_contain, target, uname, apple_os};
7777

7878
/// Helpers for building names of output artifacts that are potentially target-specific.
7979
pub use artifact_names::{

src/tools/run-make-support/src/targets.rs

+18
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ pub fn is_darwin() -> bool {
2828
target().contains("darwin")
2929
}
3030

31+
/// Get the target OS on Apple operating systems.
32+
#[must_use]
33+
pub fn apple_os() -> &'static str {
34+
if target().contains("darwin") {
35+
"macos"
36+
} else if target().contains("ios") {
37+
"ios"
38+
} else if target().contains("tvos") {
39+
"tvos"
40+
} else if target().contains("watchos") {
41+
"watchos"
42+
} else if target().contains("visionos") {
43+
"visionos"
44+
} else {
45+
panic!("not an Apple OS")
46+
}
47+
}
48+
3149
/// Check if `component` is within `LLVM_COMPONENTS`
3250
#[must_use]
3351
pub fn llvm_components_contain(component: &str) -> bool {

src/tools/tidy/src/allowed_run_make_makefiles.txt

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ run-make/incr-add-rust-src-component/Makefile
66
run-make/issue-84395-lto-embed-bitcode/Makefile
77
run-make/jobserver-error/Makefile
88
run-make/libs-through-symlinks/Makefile
9-
run-make/macos-deployment-target/Makefile
109
run-make/split-debuginfo/Makefile
1110
run-make/symbol-mangling-hashed/Makefile
1211
run-make/translation/Makefile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
//! Test codegen when setting deployment targets on Apple platforms.
2+
//!
3+
//! This is important since its a compatibility hazard. The linker will
4+
//! generate load commands differently based on what minimum OS it can assume.
5+
//!
6+
//! See https://github.com/rust-lang/rust/pull/105123.
7+
8+
//@ only-apple
9+
10+
use run_make_support::{apple_os, cmd, run_in_tmpdir, rustc, target};
11+
12+
/// Run vtool to check the `minos` field in LC_BUILD_VERSION.
13+
///
14+
/// On lower deployment targets, LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS and similar
15+
/// are used instead of LC_BUILD_VERSION - these have a `version` field, so also check that.
16+
#[track_caller]
17+
fn minos(file: &str, version: &str) {
18+
cmd("vtool")
19+
.arg("-show-build")
20+
.arg(file)
21+
.run()
22+
.assert_stdout_contains_regex(format!("(minos|version) {version}"));
23+
}
24+
25+
fn main() {
26+
// These versions should generally be higher than the default versions
27+
let (env_var, example_version, higher_example_version) = match apple_os() {
28+
"macos" => ("MACOSX_DEPLOYMENT_TARGET", "12.0", "13.0"),
29+
// armv7s-apple-ios and i386-apple-ios only supports iOS 10.0
30+
"ios" if target() == "armv7s-apple-ios" || target() == "i386-apple-ios" => {
31+
("IPHONEOS_DEPLOYMENT_TARGET", "10.0", "10.0")
32+
}
33+
"ios" => ("IPHONEOS_DEPLOYMENT_TARGET", "15.0", "16.0"),
34+
"watchos" => ("WATCHOS_DEPLOYMENT_TARGET", "7.0", "9.0"),
35+
"tvos" => ("TVOS_DEPLOYMENT_TARGET", "14.0", "15.0"),
36+
"visionos" => ("XROS_DEPLOYMENT_TARGET", "1.1", "1.2"),
37+
_ => unreachable!(),
38+
};
39+
let default_version =
40+
rustc().target(target()).env_remove(env_var).print("deployment-target").run().stdout_utf8();
41+
let default_version = default_version.strip_prefix("deployment_target=").unwrap().trim();
42+
43+
// Test that version makes it to the object file.
44+
run_in_tmpdir(|| {
45+
let rustc = || {
46+
let mut rustc = rustc();
47+
rustc.target(target());
48+
rustc.crate_type("lib");
49+
rustc.emit("obj");
50+
rustc.input("foo.rs");
51+
rustc.output("foo.o");
52+
rustc
53+
};
54+
55+
rustc().env(env_var, example_version).run();
56+
minos("foo.o", example_version);
57+
58+
// FIXME(madsmtm): Doesn't work on Mac Catalyst and the simulator.
59+
if !target().contains("macabi") && !target().contains("sim") {
60+
rustc().env_remove(env_var).run();
61+
minos("foo.o", default_version);
62+
}
63+
});
64+
65+
// Test that version makes it to the linker when linking dylibs.
66+
run_in_tmpdir(|| {
67+
// Certain watchOS targets don't support dynamic linking, so we disable the test on those.
68+
if apple_os() == "watchos" {
69+
return;
70+
}
71+
72+
let rustc = || {
73+
let mut rustc = rustc();
74+
rustc.target(target());
75+
rustc.crate_type("dylib");
76+
rustc.input("foo.rs");
77+
rustc.output("libfoo.dylib");
78+
rustc
79+
};
80+
81+
rustc().env(env_var, example_version).run();
82+
minos("libfoo.dylib", example_version);
83+
84+
// FIXME(madsmtm): Deployment target is not currently passed properly to linker
85+
// rustc().env_remove(env_var).run();
86+
// minos("libfoo.dylib", default_version);
87+
88+
// Test with ld64 instead
89+
90+
rustc().arg("-Clinker-flavor=ld").env(env_var, example_version).run();
91+
minos("libfoo.dylib", example_version);
92+
93+
rustc().arg("-Clinker-flavor=ld").env_remove(env_var).run();
94+
minos("libfoo.dylib", default_version);
95+
});
96+
97+
// Test that version makes it to the linker when linking executables.
98+
run_in_tmpdir(|| {
99+
let rustc = || {
100+
let mut rustc = rustc();
101+
rustc.target(target());
102+
rustc.crate_type("bin");
103+
rustc.input("foo.rs");
104+
rustc.output("foo");
105+
rustc
106+
};
107+
108+
// FIXME(madsmtm): Doesn't work on watchOS for some reason?
109+
if !target().contains("watchos") {
110+
rustc().env(env_var, example_version).run();
111+
minos("foo", example_version);
112+
113+
// FIXME(madsmtm): Deployment target is not currently passed properly to linker
114+
// rustc().env_remove(env_var).run();
115+
// minos("foo", default_version);
116+
}
117+
118+
// Test with ld64 instead
119+
120+
rustc().arg("-Clinker-flavor=ld").env(env_var, example_version).run();
121+
minos("foo", example_version);
122+
123+
rustc().arg("-Clinker-flavor=ld").env_remove(env_var).run();
124+
minos("foo", default_version);
125+
});
126+
127+
// Test that changing the deployment target busts the incremental cache.
128+
run_in_tmpdir(|| {
129+
let rustc = || {
130+
let mut rustc = rustc();
131+
rustc.target(target());
132+
rustc.incremental("incremental");
133+
rustc.crate_type("lib");
134+
rustc.emit("obj");
135+
rustc.input("foo.rs");
136+
rustc.output("foo.o");
137+
rustc
138+
};
139+
140+
// FIXME(madsmtm): Incremental cache is not yet busted
141+
// https://github.com/rust-lang/rust/issues/118204
142+
let higher_example_version = example_version;
143+
let default_version = example_version;
144+
145+
rustc().env(env_var, example_version).run();
146+
minos("foo.o", example_version);
147+
148+
rustc().env(env_var, higher_example_version).run();
149+
minos("foo.o", higher_example_version);
150+
151+
// FIXME(madsmtm): Doesn't work on Mac Catalyst and the simulator.
152+
if !target().contains("macabi") && !target().contains("sim") {
153+
rustc().env_remove(env_var).run();
154+
minos("foo.o", default_version);
155+
}
156+
});
157+
}

tests/run-make/macos-deployment-target/Makefile

-21
This file was deleted.

tests/run-make/macos-deployment-target/with_deployment_target.rs

-4
This file was deleted.

0 commit comments

Comments
 (0)