From 03a364eb6280c17fbd7228b642530e564d06931b Mon Sep 17 00:00:00 2001 From: futurejones Date: Sun, 3 Mar 2024 09:30:47 +0900 Subject: [PATCH] set default linker to lld for Amazon Linux 2023 --- CMakeLists.txt | 6 ++++++ lib/Driver/UnixToolChains.cpp | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 43747db4d55ca..695fc242f37d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -948,6 +948,10 @@ if(XCODE) swift_common_xcode_cxx_config() endif() +# Check what linux distribution is being used. +# This can be used to determine the default linker to use. +cmake_host_system_information(RESULT DISTRO_NAME QUERY DISTRIB_PRETTY_NAME) + # Which default linker to use. Prefer LLVM_USE_LINKER if it set, otherwise use # our own defaults. This should only be possible in a unified (not stand alone) # build environment. @@ -959,6 +963,8 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows" AND NOT CMAKE_HOST_SYSTEM_NAME STREQ set(SWIFT_USE_LINKER_default "lld") elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin") set(SWIFT_USE_LINKER_default "") +elseif(DISTRO_NAME STREQUAL "Amazon Linux 2023") + set(SWIFT_USE_LINKER_default "lld") else() set(SWIFT_USE_LINKER_default "gold") endif() diff --git a/lib/Driver/UnixToolChains.cpp b/lib/Driver/UnixToolChains.cpp index f6856c17e1639..90afe3ec8b205 100644 --- a/lib/Driver/UnixToolChains.cpp +++ b/lib/Driver/UnixToolChains.cpp @@ -10,6 +10,8 @@ // //===----------------------------------------------------------------------===// +#include + #include "ToolChains.h" #include "swift/Basic/LLVM.h" @@ -108,9 +110,24 @@ ToolChain::InvocationInfo toolchains::GenericUnix::constructInvocation( return II; } +// Amazon Linux 2023 requires lld as the default linker. +bool isAmazonLinux2023Host() { + std::ifstream file("/etc/os-release"); + std::string line; + + while (std::getline(file, line)) { + if (line.substr(0, 12) == "PRETTY_NAME=") { + if (line.substr(12) == "\"Amazon Linux 2023\"") { + file.close(); + return true; + } + } + } + return false; + } std::string toolchains::GenericUnix::getDefaultLinker() const { - if (getTriple().isAndroid()) + if (getTriple().isAndroid() || isAmazonLinux2023Host()) return "lld"; switch (getTriple().getArch()) {