|
1 | 1 | use std::borrow::Cow;
|
2 | 2 | use std::env;
|
| 3 | +use std::fmt::{Display, from_fn}; |
3 | 4 |
|
4 | 5 | use rustc_session::Session;
|
5 | 6 | use rustc_target::spec::{
|
6 | 7 | AppleOSVersion, apple_deployment_target_env_var, apple_minimum_deployment_target,
|
7 |
| - apple_parse_version, |
| 8 | + apple_os_minimum_deployment_target, apple_parse_version, |
8 | 9 | };
|
9 | 10 |
|
| 11 | +use crate::errors::AppleDeploymentTarget; |
| 12 | + |
10 | 13 | #[cfg(test)]
|
11 | 14 | mod tests;
|
12 | 15 |
|
| 16 | +pub fn pretty_version(version: AppleOSVersion) -> impl Display { |
| 17 | + let (major, minor, patch) = version; |
| 18 | + from_fn(move |f| { |
| 19 | + write!(f, "{major}.{minor}")?; |
| 20 | + if patch != 0 { |
| 21 | + write!(f, ".{patch}")?; |
| 22 | + } |
| 23 | + Ok(()) |
| 24 | + }) |
| 25 | +} |
| 26 | + |
13 | 27 | /// Get the deployment target based on the standard environment variables, or fall back to the
|
14 | 28 | /// minimum version supported by `rustc`.
|
15 | 29 | pub fn deployment_target(sess: &Session) -> AppleOSVersion {
|
| 30 | + let os_min = apple_os_minimum_deployment_target(&sess.target.os); |
16 | 31 | let min = apple_minimum_deployment_target(&sess.target);
|
| 32 | + let env_var = apple_deployment_target_env_var(&sess.target.os); |
17 | 33 |
|
18 |
| - if let Ok(deployment_target) = env::var(apple_deployment_target_env_var(&sess.target.os)) { |
| 34 | + if let Ok(deployment_target) = env::var(env_var) { |
19 | 35 | match apple_parse_version(&deployment_target) {
|
20 |
| - // It is common that the deployment target is set too low, e.g. on macOS Aarch64 to also |
21 |
| - // target older x86_64, the user may set a lower deployment target than supported. |
22 |
| - // |
23 |
| - // To avoid such issues, we silently raise the deployment target here. |
24 |
| - // FIXME: We want to show a warning when `version < os_min`. |
25 |
| - Ok(version) => version.max(min), |
26 |
| - // FIXME: Report erroneous environment variable to user. |
27 |
| - Err(_) => min, |
| 36 | + Ok(version) => { |
| 37 | + // It is common that the deployment target is set a bit too low, for example on |
| 38 | + // macOS Aarch64 to also target older x86_64. So we only want to warn when variable |
| 39 | + // is lower than the minimum OS supported by rustc, not when the variable is lower |
| 40 | + // than the minimum for a specific target. |
| 41 | + if version < os_min { |
| 42 | + sess.dcx().emit_warn(AppleDeploymentTarget::TooLow { |
| 43 | + env_var, |
| 44 | + version: pretty_version(version).to_string(), |
| 45 | + os_min: pretty_version(os_min).to_string(), |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + // Raise the deployment target to the minimum supported. |
| 50 | + version.max(min) |
| 51 | + } |
| 52 | + Err(error) => { |
| 53 | + sess.dcx().emit_err(AppleDeploymentTarget::Invalid { env_var, error }); |
| 54 | + min |
| 55 | + } |
28 | 56 | }
|
29 | 57 | } else {
|
30 | 58 | // If no deployment target variable is set, default to the minimum found above.
|
|
0 commit comments