|
| 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::{cmd, rustc, target, apple_os}; |
| 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 | +fn minos(file: &str, version: &str) { |
| 17 | + cmd("vtool").arg("-show-build").arg(file).run().assert_stdout_contains_regex(format!("(minos|version) {version}")); |
| 18 | +} |
| 19 | + |
| 20 | +fn main() { |
| 21 | + let (env_var, example_version, higher_example_version) = match apple_os() { |
| 22 | + "macos" => ("MACOSX_DEPLOYMENT_TARGET", "12.0", "13.0"), |
| 23 | + "ios" => ("IPHONEOS_DEPLOYMENT_TARGET", "14.0", "15.0"), |
| 24 | + "watchos" => ("WATCHOS_DEPLOYMENT_TARGET", "7.0", "9.0"), |
| 25 | + "tvos" => ("TVOS_DEPLOYMENT_TARGET", "14.0", "15.0"), |
| 26 | + "visionos" => ("XROS_DEPLOYMENT_TARGET", "1.1", "1.2"), |
| 27 | + _ => unreachable!(), |
| 28 | + }; |
| 29 | + let default_version = rustc().target(target()).env_remove(env_var).print("deployment-target").run().stdout_utf8(); |
| 30 | + let default_version = default_version.strip_prefix("deployment_target=").unwrap().trim(); |
| 31 | + |
| 32 | + // Ensure that the versions we test with are not the default |
| 33 | + assert_ne!(default_version, example_version); |
| 34 | + assert_ne!(default_version, higher_example_version); |
| 35 | + |
| 36 | + // Test that version makes it to the object file. |
| 37 | + { |
| 38 | + let mut rustc = rustc(); |
| 39 | + rustc.target(target()); |
| 40 | + rustc.crate_type("lib"); |
| 41 | + rustc.emit("obj"); |
| 42 | + rustc.input("foo.rs"); |
| 43 | + rustc.output("foo.o"); |
| 44 | + |
| 45 | + rustc.env(env_var, example_version).run(); |
| 46 | + minos("foo.o", example_version); |
| 47 | + |
| 48 | + rustc.env_remove(env_var).run(); |
| 49 | + minos("foo.o", default_version); |
| 50 | + } |
| 51 | + |
| 52 | + // Test that version makes it to the linker when linking dylibs. |
| 53 | + { |
| 54 | + let mut rustc = rustc(); |
| 55 | + rustc.target(target()); |
| 56 | + rustc.crate_type("dylib"); |
| 57 | + rustc.input("foo.rs"); |
| 58 | + rustc.output("libfoo.dylib"); |
| 59 | + |
| 60 | + rustc.env(env_var, example_version).run(); |
| 61 | + minos("libfoo.dylib", example_version); |
| 62 | + |
| 63 | + // FIXME(madsmtm): Deployment target is not currently passed properly to linker |
| 64 | + // rustc.env_remove(env_var).run(); |
| 65 | + // minos("libfoo.dylib", default_version); |
| 66 | + |
| 67 | + // Test with ld64 instead |
| 68 | + rustc.arg("-Clinker-flavor=ld"); |
| 69 | + |
| 70 | + rustc.env(env_var, example_version).run(); |
| 71 | + minos("libfoo.dylib", example_version); |
| 72 | + |
| 73 | + rustc.env_remove(env_var).run(); |
| 74 | + minos("libfoo.dylib", default_version); |
| 75 | + } |
| 76 | + |
| 77 | + // Test that version makes it to the linker when linking executables. |
| 78 | + { |
| 79 | + let mut rustc = rustc(); |
| 80 | + rustc.target(target()); |
| 81 | + rustc.crate_type("bin"); |
| 82 | + rustc.input("foo.rs"); |
| 83 | + rustc.output("foo"); |
| 84 | + |
| 85 | + rustc.env(env_var, example_version).run(); |
| 86 | + minos("foo", example_version); |
| 87 | + |
| 88 | + // FIXME(madsmtm): Deployment target is not currently passed properly to linker |
| 89 | + // rustc.env_remove(env_var).run(); |
| 90 | + // minos("foo", default_version); |
| 91 | + |
| 92 | + // Test with ld64 instead |
| 93 | + rustc.arg("-Clinker-flavor=ld"); |
| 94 | + |
| 95 | + rustc.env(env_var, example_version).run(); |
| 96 | + minos("foo", example_version); |
| 97 | + |
| 98 | + rustc.env_remove(env_var).run(); |
| 99 | + minos("foo", default_version); |
| 100 | + } |
| 101 | + |
| 102 | + // Test that changing the deployment target busts the incremental cache. |
| 103 | + { |
| 104 | + let mut rustc = rustc(); |
| 105 | + rustc.target(target()); |
| 106 | + rustc.incremental("incremental"); |
| 107 | + rustc.crate_type("lib"); |
| 108 | + rustc.emit("obj"); |
| 109 | + rustc.input("foo.rs"); |
| 110 | + rustc.output("foo.o"); |
| 111 | + |
| 112 | + rustc.env(env_var, example_version).run(); |
| 113 | + minos("foo.o", example_version); |
| 114 | + |
| 115 | + rustc.env(env_var, higher_example_version).run(); |
| 116 | + minos("foo.o", higher_example_version); |
| 117 | + |
| 118 | + rustc.env_remove(env_var).run(); |
| 119 | + minos("foo.o", default_version); |
| 120 | + } |
| 121 | +} |
0 commit comments