@@ -462,8 +462,55 @@ fn compiler_rt_for_profiler(builder: &Builder<'_>) -> PathBuf {
462462/// Configure cargo to compile the standard library, adding appropriate env vars
463463/// and such.
464464pub fn std_cargo ( builder : & Builder < ' _ > , target : TargetSelection , stage : u32 , cargo : & mut Cargo ) {
465- if let Some ( target) = env:: var_os ( "MACOSX_STD_DEPLOYMENT_TARGET" ) {
466- cargo. env ( "MACOSX_DEPLOYMENT_TARGET" , target) ;
465+ // rustc already ensures that it builds with the minimum deployment
466+ // target, so ideally we shouldn't need to do anything here.
467+ //
468+ // However, `cc` currently defaults to a higher version for backwards
469+ // compatibility, which means that compiler-rt, which is built via
470+ // compiler-builtins' build script, gets built with a higher deployment
471+ // target. This in turn causes warnings while linking, and is generally
472+ // a compatibility hazard.
473+ //
474+ // So, at least until https://github.com/rust-lang/cc-rs/issues/1171, or
475+ // perhaps https://github.com/rust-lang/cargo/issues/13115 is resolved, we
476+ // explicitly set the deployment target environment variables to avoid
477+ // this issue.
478+ //
479+ // This place also serves as an extension point if we ever wanted to raise
480+ // rustc's default deployment target while keeping the prebuilt `std` at
481+ // a lower version, so it's kinda nice to have in any case.
482+ if target. contains ( "apple" ) && !builder. config . dry_run ( ) {
483+ // Query rustc for the deployment target.
484+ let mut cmd = command ( builder. rustc ( cargo. compiler ( ) ) ) ;
485+ cmd. arg ( "--target" ) . arg ( target. rustc_target_arg ( ) ) ;
486+ cmd. arg ( "--print=deployment-target" ) ;
487+ let output = cmd. run_capture_stdout ( builder) . stdout ( ) ;
488+
489+ let value = output. split ( '=' ) . last ( ) . unwrap ( ) . trim ( ) ;
490+
491+ // FIXME: Simplify after https://github.com/rust-lang/rust/pull/133041
492+ let env_var = if target. contains ( "apple-darwin" ) {
493+ "MACOSX_DEPLOYMENT_TARGET"
494+ } else if target. contains ( "apple-ios" ) {
495+ "IPHONEOS_DEPLOYMENT_TARGET"
496+ } else if target. contains ( "apple-tvos" ) {
497+ "TVOS_DEPLOYMENT_TARGET"
498+ } else if target. contains ( "apple-watchos" ) {
499+ "WATCHOS_DEPLOYMENT_TARGET"
500+ } else if target. contains ( "apple-visionos" ) {
501+ "XROS_DEPLOYMENT_TARGET"
502+ } else {
503+ panic ! ( "unknown target OS for apple target" ) ;
504+ } ;
505+
506+ // Unconditionally set the env var (if it was set in the environment
507+ // already, rustc should've picked that up).
508+ cargo. env ( env_var, value) ;
509+
510+ // Allow CI to override the deployment target for `std`.
511+ if let Some ( target) = env:: var_os ( "MACOSX_STD_DEPLOYMENT_TARGET" ) {
512+ cargo. env ( "MACOSX_DEPLOYMENT_TARGET" , target) ;
513+ }
467514 }
468515
469516 // Paths needed by `library/profiler_builtins/build.rs`.
0 commit comments