|
| 1 | +use std::borrow::Cow; |
| 2 | +use std::env; |
| 3 | +use std::num::ParseIntError; |
| 4 | + |
| 5 | +use rustc_middle::bug; |
| 6 | +use rustc_session::Session; |
| 7 | + |
| 8 | +#[cfg(test)] |
| 9 | +mod tests; |
| 10 | + |
| 11 | +/// Deployment target or SDK version. |
| 12 | +/// |
| 13 | +/// The size of the numbers in here are limited by Mach-O's `LC_BUILD_VERSION`. |
| 14 | +pub type OsVersion = (u16, u8, u8); |
| 15 | + |
| 16 | +/// Parse an OS version triple (SDK version or deployment target). |
| 17 | +fn parse_version(version: &str) -> Result<OsVersion, ParseIntError> { |
| 18 | + if let Some((major, minor)) = version.split_once('.') { |
| 19 | + let major = major.parse()?; |
| 20 | + if let Some((minor, patch)) = minor.split_once('.') { |
| 21 | + Ok((major, minor.parse()?, patch.parse()?)) |
| 22 | + } else { |
| 23 | + Ok((major, minor.parse()?, 0)) |
| 24 | + } |
| 25 | + } else { |
| 26 | + Ok((version.parse()?, 0, 0)) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/// Get the deployment target based on the standard environment variables, or fall back to the |
| 31 | +/// minimum version supported by `rustc`. |
| 32 | +pub fn deployment_target(sess: &Session) -> OsVersion { |
| 33 | + let target = &sess.target; |
| 34 | + |
| 35 | + // Minimum operating system versions currently supported by `rustc`. |
| 36 | + // |
| 37 | + // When bumping a version in here, remember to update the platform-support docs too. |
| 38 | + // |
| 39 | + // NOTE: If you are looking for `rustc`'s default deployment target, prefer |
| 40 | + // `rustc --print deployment-target`, as the default here may change in future `rustc` versions. |
| 41 | + let os_min = match &*target.os { |
| 42 | + "macos" => (10, 12, 0), |
| 43 | + "ios" => (10, 0, 0), |
| 44 | + "tvos" => (10, 0, 0), |
| 45 | + "watchos" => (5, 0, 0), |
| 46 | + "visionos" => (1, 0, 0), |
| 47 | + _ => bug!("tried to get deployment target for non-Apple platform"), |
| 48 | + }; |
| 49 | + |
| 50 | + // On certain targets it makes sense to raise the minimum OS version. |
| 51 | + // |
| 52 | + // This matches what LLVM does, see in part: |
| 53 | + // <https://github.com/llvm/llvm-project/blob/llvmorg-18.1.8/llvm/lib/TargetParser/Triple.cpp#L1900-L1932> |
| 54 | + let min = match (&*target.os, &*target.arch, &*target.abi) { |
| 55 | + ("macos", "aarch64", _) => (11, 0, 0), |
| 56 | + ("ios", "aarch64", "macabi" | "sim") => (14, 0, 0), |
| 57 | + ("ios", _, _) if target.llvm_target.starts_with("arm64e") => (14, 0, 0), |
| 58 | + // Mac Catalyst defaults to 13.1 in Clang. |
| 59 | + ("ios", _, "macabi") => (13, 1, 0), |
| 60 | + ("tvos", "aarch64", "sim") => (14, 0, 0), |
| 61 | + ("watchos", "aarch64", "sim") => (7, 0, 0), |
| 62 | + _ => os_min, |
| 63 | + }; |
| 64 | + |
| 65 | + // Name of the environment variable used to fetch the deployment target on the given OS. |
| 66 | + let env_var = match &*target.os { |
| 67 | + "macos" => "MACOSX_DEPLOYMENT_TARGET", |
| 68 | + "ios" => "IPHONEOS_DEPLOYMENT_TARGET", |
| 69 | + "watchos" => "WATCHOS_DEPLOYMENT_TARGET", |
| 70 | + "tvos" => "TVOS_DEPLOYMENT_TARGET", |
| 71 | + "visionos" => "XROS_DEPLOYMENT_TARGET", |
| 72 | + _ => bug!("tried to get deployment target env var for non-Apple platform"), |
| 73 | + }; |
| 74 | + |
| 75 | + if let Ok(deployment_target) = env::var(env_var) { |
| 76 | + match parse_version(&deployment_target) { |
| 77 | + // It is common that the deployment target is set too low, e.g. on macOS Aarch64 to also |
| 78 | + // target older x86_64, the user may set a lower deployment target than supported. |
| 79 | + // |
| 80 | + // To avoid such issues, we silently raise the deployment target here. |
| 81 | + // FIXME: We want to show a warning when `version < os_min`. |
| 82 | + Ok(version) => version.max(min), |
| 83 | + // FIXME: Report erroneous environment variable to user. |
| 84 | + Err(_) => min, |
| 85 | + } |
| 86 | + } else { |
| 87 | + // If no deployment target variable is set, default to the minimum found above. |
| 88 | + min |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +fn add_version_to_llvm_target(llvm_target: &str, deployment_target: OsVersion) -> String { |
| 93 | + let mut components = llvm_target.split("-"); |
| 94 | + let arch = components.next().expect("darwin target should have arch"); |
| 95 | + let vendor = components.next().expect("darwin target should have vendor"); |
| 96 | + let os = components.next().expect("darwin target should have os"); |
| 97 | + let environment = components.next(); |
| 98 | + assert_eq!(components.next(), None, "too many LLVM triple components"); |
| 99 | + |
| 100 | + let (major, minor, patch) = deployment_target; |
| 101 | + |
| 102 | + assert!( |
| 103 | + !os.contains(|c: char| c.is_ascii_digit()), |
| 104 | + "LLVM target must not already be versioned" |
| 105 | + ); |
| 106 | + |
| 107 | + if let Some(env) = environment { |
| 108 | + // Insert version into OS, before environment |
| 109 | + format!("{arch}-{vendor}-{os}{major}.{minor}.{patch}-{env}") |
| 110 | + } else { |
| 111 | + format!("{arch}-{vendor}-{os}{major}.{minor}.{patch}") |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/// The target triple depends on the deployment target, and is required to |
| 116 | +/// enable features such as cross-language LTO, and for picking the right |
| 117 | +/// Mach-O commands. |
| 118 | +/// |
| 119 | +/// Certain optimizations also depend on the deployment target. |
| 120 | +pub fn versioned_llvm_target(sess: &Session) -> Cow<'static, str> { |
| 121 | + if sess.target.is_like_osx { |
| 122 | + add_version_to_llvm_target(&sess.target.llvm_target, deployment_target(sess)).into() |
| 123 | + } else { |
| 124 | + sess.target.llvm_target.clone() |
| 125 | + } |
| 126 | +} |
0 commit comments