Skip to content

Commit

Permalink
Refactor ld64 arch determination
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Oct 9, 2024
1 parent efaa2d4 commit 8f42605
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
29 changes: 28 additions & 1 deletion compiler/rustc_codegen_ssa/src/apple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

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

Err(AppleSdkError::Missing { sdk_name })
}

/// The architecture name understood by Apple's 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
pub fn ld64_arch(target: &Target) -> &'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),
}
}
27 changes: 2 additions & 25 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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`):
Expand Down

0 comments on commit 8f42605

Please sign in to comment.