From 9b703f3232127125c7b0584a3373c431802925f0 Mon Sep 17 00:00:00 2001 From: Alex Reinking Date: Tue, 25 Jun 2024 16:59:43 -0700 Subject: [PATCH] Provide a minimum OS version for MachO objects (#8323) This gives LLVM enough information to generate a "platform load-command" in the object file. Fixes #7941 --- src/LLVM_Runtime_Linker.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/LLVM_Runtime_Linker.cpp b/src/LLVM_Runtime_Linker.cpp index 69f32c35831e..09e58bea894b 100644 --- a/src/LLVM_Runtime_Linker.cpp +++ b/src/LLVM_Runtime_Linker.cpp @@ -422,6 +422,25 @@ llvm::DataLayout get_data_layout_for_target(Target target) { namespace Internal { +namespace { + +std::optional get_os_version_constraint(const llvm::Triple &triple) { + if (!triple.isOSBinFormatMachO()) { + return std::nullopt; + } + + if (triple.getOS() == llvm::Triple::MacOSX && triple.getArch() == llvm::Triple::x86_64) { + // At time of writing (June 2024), this is one version prior + // to the oldest version still supported by Apple. + return llvm::VersionTuple(11, 0, 0); + } + + llvm::VersionTuple t = triple.getMinimumSupportedOSVersion(); + return t.empty() ? std::nullopt : std::make_optional(t); +} + +} // namespace + llvm::Triple get_triple_for_target(const Target &target) { llvm::Triple triple; @@ -555,6 +574,14 @@ llvm::Triple get_triple_for_target(const Target &target) { // Return default-constructed triple. Must be set later. } + // Setting a minimum OS version here enables LLVM to include platform + // metadata in the MachO object file. Without this, Xcode 15's ld + // issues warnings about missing the "platform load command". + if (auto version = get_os_version_constraint(triple)) { + // llvm::Triple determines the version by parsing the OSName. + triple.setOSName((triple.getOSName() + version->getAsString()).str()); + } + return triple; }