|  | 
|  | 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 | +} | 
0 commit comments