From f4b5954764f68b2487f2603dc3e0eed39bcf74e5 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 14 Aug 2022 19:13:15 +0300 Subject: [PATCH] rustc_target: Use `Cow` and link args helpers in `apple_base` --- compiler/rustc_target/src/spec/apple_base.rs | 41 +++++++++----------- compiler/rustc_target/src/spec/mod.rs | 2 +- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_target/src/spec/apple_base.rs b/compiler/rustc_target/src/spec/apple_base.rs index 9bbee88a894ca..2c72bf88a41e8 100644 --- a/compiler/rustc_target/src/spec/apple_base.rs +++ b/compiler/rustc_target/src/spec/apple_base.rs @@ -1,40 +1,37 @@ use std::{borrow::Cow, env}; -use crate::spec::{cvs, DebuginfoKind, FramePointer, SplitDebuginfo, TargetOptions}; +use crate::spec::{cvs, DebuginfoKind, FramePointer, SplitDebuginfo, StaticCow, TargetOptions}; use crate::spec::{LinkArgs, LinkerFlavor, LldFlavor}; fn pre_link_args(os: &'static str, arch: &'static str, abi: &'static str) -> LinkArgs { - let mut args = LinkArgs::new(); - - let platform_name = match abi { - "sim" => format!("{}-simulator", os), - "macabi" => "mac-catalyst".to_string(), - _ => os.to_string(), + let platform_name: StaticCow = match abi { + "sim" => format!("{}-simulator", os).into(), + "macabi" => "mac-catalyst".into(), + _ => os.into(), }; - let platform_version = match os.as_ref() { + let platform_version: StaticCow = match os.as_ref() { "ios" => ios_lld_platform_version(), "tvos" => tvos_lld_platform_version(), "watchos" => watchos_lld_platform_version(), "macos" => macos_lld_platform_version(arch), _ => unreachable!(), - }; - - if abi != "macabi" { - args.insert(LinkerFlavor::Gcc, vec!["-arch".into(), arch.into()]); } + .into(); - args.insert( + let mut args = TargetOptions::link_args( LinkerFlavor::Lld(LldFlavor::Ld64), - vec![ - "-arch".into(), - arch.into(), - "-platform_version".into(), - platform_name.into(), - platform_version.clone().into(), - platform_version.into(), - ], + &["-arch", arch, "-platform_version"], ); + // Manually add owned args unsupported by link arg building helpers. + args.entry(LinkerFlavor::Lld(LldFlavor::Ld64)).or_default().extend([ + platform_name, + platform_version.clone(), + platform_version, + ]); + if abi != "macabi" { + super::add_link_args(&mut args, LinkerFlavor::Gcc, &["-arch", arch]); + } args } @@ -127,7 +124,7 @@ pub fn macos_llvm_target(arch: &str) -> String { format!("{}-apple-macosx{}.{}.0", arch, major, minor) } -pub fn macos_link_env_remove() -> Vec> { +pub fn macos_link_env_remove() -> Vec> { let mut env_remove = Vec::with_capacity(2); // Remove the `SDKROOT` environment variable if it's clearly set for the wrong platform, which // may occur when we're linking a custom build script while targeting iOS for example. diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index 3ae5c9b5d65dd..b4ca1ddcf63c0 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -1526,7 +1526,7 @@ fn add_link_args(link_args: &mut LinkArgs, flavor: LinkerFlavor, args: &[&'stati match flavor { LinkerFlavor::Ld => insert(LinkerFlavor::Lld(LldFlavor::Ld)), LinkerFlavor::Msvc => insert(LinkerFlavor::Lld(LldFlavor::Link)), - LinkerFlavor::Lld(LldFlavor::Wasm) => {} + LinkerFlavor::Lld(LldFlavor::Ld64) | LinkerFlavor::Lld(LldFlavor::Wasm) => {} LinkerFlavor::Lld(lld_flavor) => { panic!("add_link_args: use non-LLD flavor for {:?}", lld_flavor) }