From 8f42605cd8dae8f3ba674f256dffc1ba979d151b Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Thu, 10 Oct 2024 00:29:47 +0200 Subject: [PATCH] Refactor ld64 arch determination --- compiler/rustc_codegen_ssa/src/apple.rs | 29 ++++++++++++++++++++- compiler/rustc_codegen_ssa/src/back/link.rs | 27 ++----------------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/apple.rs b/compiler/rustc_codegen_ssa/src/apple.rs index 33450bec328c8..0ff2b7fb79c20 100644 --- a/compiler/rustc_codegen_ssa/src/apple.rs +++ b/compiler/rustc_codegen_ssa/src/apple.rs @@ -3,9 +3,10 @@ use std::io::ErrorKind; use std::path::{Path, PathBuf}; use std::{env, fs}; +use rustc_middle::bug; use rustc_session::Session; use rustc_target::spec::{ - AppleOSVersion, apple_deployment_target_env_var, apple_minimum_deployment_target, + AppleOSVersion, Target, apple_deployment_target_env_var, apple_minimum_deployment_target, apple_os_minimum_deployment_target, }; @@ -213,3 +214,29 @@ pub(crate) fn find_sdk_root(sdk_name: &'static str) -> Result &'static str { + // `target.arch` / `target_arch` is not detailed enough. + let llvm_arch = target.llvm_target.split_once('-').expect("LLVM target must have arch").0; + + // Intentially verbose to ensure that the list always matches correctly + // with the list in the source above. + match llvm_arch { + "armv7k" => "armv7k", + "armv7s" => "armv7s", + "arm64" => "arm64", + "arm64e" => "arm64e", + "arm64_32" => "arm64_32", + // ld64 doesn't understand i686, so fall back to i386 instead. + // + // Same story when linking with cc, since that ends up invoking ld64. + "i386" | "i686" => "i386", + "x86_64" => "x86_64", + "x86_64h" => "x86_64h", + _ => bug!("unsupported architecture {llvm_arch} in Apple target: {}", target.llvm_target), + } +} diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 6f2edfc00e746..c6621612924da 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -50,7 +50,7 @@ use super::command::Command; use super::linker::{self, Linker}; use super::metadata::{MetadataPosition, create_wrapper_file}; use super::rpath::{self, RPathConfig}; -use crate::apple::{deployment_target, find_sdk_root, versioned_llvm_target}; +use crate::apple::{deployment_target, find_sdk_root, ld64_arch, versioned_llvm_target}; use crate::{ CodegenResults, CompiledModule, CrateInfo, NativeLib, common, errors, looks_like_rust_object_file, @@ -2974,32 +2974,9 @@ fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavo return; }; - // `sess.target.arch` (`target_arch`) is not detailed enough. - let llvm_arch = sess.target.llvm_target.split_once('-').expect("LLVM target must have arch").0; let target_os = &*sess.target.os; let target_abi = &*sess.target.abi; - - // The architecture name to forward to the linker. - // - // Supported architecture names can be found in the source: - // https://github.com/apple-oss-distributions/ld64/blob/ld64-951.9/src/abstraction/MachOFileAbstraction.hpp#L578-L648 - // - // Intentially verbose to ensure that the list always matches correctly - // with the list in the source above. - let ld64_arch = match llvm_arch { - "armv7k" => "armv7k", - "armv7s" => "armv7s", - "arm64" => "arm64", - "arm64e" => "arm64e", - "arm64_32" => "arm64_32", - // ld64 doesn't understand i686, so fall back to i386 instead. - // - // Same story when linking with cc, since that ends up invoking ld64. - "i386" | "i686" => "i386", - "x86_64" => "x86_64", - "x86_64h" => "x86_64h", - _ => bug!("unsupported architecture in Apple target: {}", sess.target.llvm_target), - }; + let ld64_arch = ld64_arch(&sess.target); if cc == Cc::No { // From the man page for ld64 (`man ld`):