forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#131016 - madsmtm:no-sdk-version-in-object, r=jieyouxu Apple: Do not specify an SDK version in `rlib` object files This was added in rust-lang#114114, but is unnecessary, since it ends up being overwritten when linking anyhow, and it feels wrong to embed some arbitrary SDK version in here. The object files produced by LLVM also do not set this, and the tooling shows `n/a` when it's `0`, so it seems to genuinely be optional in object files. I've also added a test for the different places the SDK version shows up, and documented a bit more in the code how SDK versions work. See rust-lang#129432 for the bigger picture. Tested with (excludes the same few targets as in rust-lang#130435): ```console ./x test tests/run-make/apple-sdk-version --target aarch64-apple-darwin,aarch64-apple-ios,aarch64-apple-ios-macabi,aarch64-apple-ios-sim,aarch64-apple-tvos,aarch64-apple-tvos-sim,aarch64-apple-visionos,aarch64-apple-visionos-sim,aarch64-apple-watchos,aarch64-apple-watchos-sim,arm64_32-apple-watchos,armv7k-apple-watchos,armv7s-apple-ios,x86_64-apple-darwin,x86_64-apple-ios,x86_64-apple-ios-macabi,x86_64-apple-tvos,x86_64-apple-watchos-sim,x86_64h-apple-darwin IPHONEOS_DEPLOYMENT_TARGET=10.0 ./x test tests/run-make/apple-sdk-version --target=i386-apple-ios ``` CC `@BlackHoleFox,` you [originally commented on these values](rust-lang#114114 (comment)). `@rustbot` label O-apple
- Loading branch information
Showing
6 changed files
with
145 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
//! Test codegen when setting SDK version on Apple platforms. | ||
//! | ||
//! This is important since its a compatibility hazard. The linker will | ||
//! generate load commands differently based on what minimum OS it can assume. | ||
//! | ||
//! See https://github.com/rust-lang/rust/issues/129432. | ||
|
||
//@ only-apple | ||
|
||
use run_make_support::{apple_os, cmd, run_in_tmpdir, rustc, target}; | ||
|
||
/// Run vtool to check the `sdk` field in LC_BUILD_VERSION. | ||
/// | ||
/// On lower deployment targets, LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS and similar | ||
/// are used instead of LC_BUILD_VERSION, but both name the relevant variable `sdk`. | ||
#[track_caller] | ||
fn has_sdk_version(file: &str, version: &str) { | ||
cmd("vtool") | ||
.arg("-show-build") | ||
.arg(file) | ||
.run() | ||
.assert_stdout_contains(format!("sdk {version}")); | ||
} | ||
|
||
fn main() { | ||
// Fetch rustc's inferred deployment target. | ||
let current_deployment_target = | ||
rustc().target(target()).print("deployment-target").run().stdout_utf8(); | ||
let current_deployment_target = | ||
current_deployment_target.strip_prefix("deployment_target=").unwrap().trim(); | ||
|
||
// Fetch current SDK version via. xcrun. | ||
// | ||
// Assumes a standard Xcode distribution, where e.g. the macOS SDK's Mac Catalyst | ||
// and the iPhone Simulator version is the same as for the iPhone SDK. | ||
let sdk_name = match apple_os() { | ||
"macos" => "macosx", | ||
"ios" => "iphoneos", | ||
"watchos" => "watchos", | ||
"tvos" => "appletvos", | ||
"visionos" => "xros", | ||
_ => unreachable!(), | ||
}; | ||
let current_sdk_version = | ||
cmd("xcrun").arg("--show-sdk-version").arg("--sdk").arg(sdk_name).run().stdout_utf8(); | ||
let current_sdk_version = current_sdk_version.trim(); | ||
|
||
// Check the SDK version in the object file produced by the codegen backend. | ||
rustc().target(target()).crate_type("lib").emit("obj").input("foo.rs").output("foo.o").run(); | ||
// Set to 0, which means not set or "n/a". | ||
has_sdk_version("foo.o", "n/a"); | ||
|
||
// Check the SDK version in the .rmeta file, as set in `create_object_file`. | ||
// | ||
// This is just to ensure that we don't set some odd version in `create_object_file`, | ||
// if the rmeta file is packed in a different way in the future, this can safely be removed. | ||
rustc().target(target()).crate_type("rlib").input("foo.rs").output("libfoo.rlib").run(); | ||
// Extra .rmeta file (which is encoded as an object file). | ||
cmd("ar").arg("-x").arg("libfoo.rlib").arg("lib.rmeta").run(); | ||
has_sdk_version("lib.rmeta", "n/a"); | ||
|
||
// Test that version makes it to the linker. | ||
for (crate_type, file_ext) in [("bin", ""), ("dylib", ".dylib")] { | ||
// Non-simulator watchOS targets don't support dynamic linking, | ||
// for simplicity we disable the test on all watchOS targets. | ||
if crate_type == "dylib" && apple_os() == "watchos" { | ||
continue; | ||
} | ||
|
||
// Test with clang | ||
let file_name = format!("foo_cc{file_ext}"); | ||
rustc() | ||
.target(target()) | ||
.crate_type("bin") | ||
.arg("-Clinker-flavor=gcc") | ||
.input("foo.rs") | ||
.output(&file_name) | ||
.run(); | ||
has_sdk_version(&file_name, current_sdk_version); | ||
|
||
// Test with ld64 | ||
let file_name = format!("foo_ld{file_ext}"); | ||
rustc() | ||
.target(target()) | ||
.crate_type("bin") | ||
.arg("-Clinker-flavor=ld") | ||
.input("foo.rs") | ||
.output(&file_name) | ||
.run(); | ||
// FIXME(madsmtm): This uses the current deployment target | ||
// instead of the current SDK version like Clang does. | ||
// https://github.com/rust-lang/rust/issues/129432 | ||
has_sdk_version(&file_name, current_deployment_target); | ||
} | ||
} |