Skip to content

Commit 8f42605

Browse files
committed
Refactor ld64 arch determination
1 parent efaa2d4 commit 8f42605

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

compiler/rustc_codegen_ssa/src/apple.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ use std::io::ErrorKind;
33
use std::path::{Path, PathBuf};
44
use std::{env, fs};
55

6+
use rustc_middle::bug;
67
use rustc_session::Session;
78
use rustc_target::spec::{
8-
AppleOSVersion, apple_deployment_target_env_var, apple_minimum_deployment_target,
9+
AppleOSVersion, Target, apple_deployment_target_env_var, apple_minimum_deployment_target,
910
apple_os_minimum_deployment_target,
1011
};
1112

@@ -213,3 +214,29 @@ pub(crate) fn find_sdk_root(sdk_name: &'static str) -> Result<PathBuf, AppleSdkE
213214

214215
Err(AppleSdkError::Missing { sdk_name })
215216
}
217+
218+
/// The architecture name understood by Apple's linker.
219+
///
220+
/// Supported architecture names can be found in the source:
221+
/// https://github.com/apple-oss-distributions/ld64/blob/ld64-951.9/src/abstraction/MachOFileAbstraction.hpp#L578-L648
222+
pub fn ld64_arch(target: &Target) -> &'static str {
223+
// `target.arch` / `target_arch` is not detailed enough.
224+
let llvm_arch = target.llvm_target.split_once('-').expect("LLVM target must have arch").0;
225+
226+
// Intentially verbose to ensure that the list always matches correctly
227+
// with the list in the source above.
228+
match llvm_arch {
229+
"armv7k" => "armv7k",
230+
"armv7s" => "armv7s",
231+
"arm64" => "arm64",
232+
"arm64e" => "arm64e",
233+
"arm64_32" => "arm64_32",
234+
// ld64 doesn't understand i686, so fall back to i386 instead.
235+
//
236+
// Same story when linking with cc, since that ends up invoking ld64.
237+
"i386" | "i686" => "i386",
238+
"x86_64" => "x86_64",
239+
"x86_64h" => "x86_64h",
240+
_ => bug!("unsupported architecture {llvm_arch} in Apple target: {}", target.llvm_target),
241+
}
242+
}

compiler/rustc_codegen_ssa/src/back/link.rs

+2-25
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use super::command::Command;
5050
use super::linker::{self, Linker};
5151
use super::metadata::{MetadataPosition, create_wrapper_file};
5252
use super::rpath::{self, RPathConfig};
53-
use crate::apple::{deployment_target, find_sdk_root, versioned_llvm_target};
53+
use crate::apple::{deployment_target, find_sdk_root, ld64_arch, versioned_llvm_target};
5454
use crate::{
5555
CodegenResults, CompiledModule, CrateInfo, NativeLib, common, errors,
5656
looks_like_rust_object_file,
@@ -2974,32 +2974,9 @@ fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavo
29742974
return;
29752975
};
29762976

2977-
// `sess.target.arch` (`target_arch`) is not detailed enough.
2978-
let llvm_arch = sess.target.llvm_target.split_once('-').expect("LLVM target must have arch").0;
29792977
let target_os = &*sess.target.os;
29802978
let target_abi = &*sess.target.abi;
2981-
2982-
// The architecture name to forward to the linker.
2983-
//
2984-
// Supported architecture names can be found in the source:
2985-
// https://github.com/apple-oss-distributions/ld64/blob/ld64-951.9/src/abstraction/MachOFileAbstraction.hpp#L578-L648
2986-
//
2987-
// Intentially verbose to ensure that the list always matches correctly
2988-
// with the list in the source above.
2989-
let ld64_arch = match llvm_arch {
2990-
"armv7k" => "armv7k",
2991-
"armv7s" => "armv7s",
2992-
"arm64" => "arm64",
2993-
"arm64e" => "arm64e",
2994-
"arm64_32" => "arm64_32",
2995-
// ld64 doesn't understand i686, so fall back to i386 instead.
2996-
//
2997-
// Same story when linking with cc, since that ends up invoking ld64.
2998-
"i386" | "i686" => "i386",
2999-
"x86_64" => "x86_64",
3000-
"x86_64h" => "x86_64h",
3001-
_ => bug!("unsupported architecture in Apple target: {}", sess.target.llvm_target),
3002-
};
2979+
let ld64_arch = ld64_arch(&sess.target);
30032980

30042981
if cc == Cc::No {
30052982
// From the man page for ld64 (`man ld`):

0 commit comments

Comments
 (0)