From 2efa7d6bff5f033a6fcbb20f41896d9f08ff59dd Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Thu, 22 Feb 2024 14:28:44 +0000 Subject: [PATCH 1/2] Update driver for fully static Linux. The Swift driver needs to understand the new triple and needs to know the platform name to which that corresponds. rdar://123442522 --- .../GenericUnixToolchain+LinkerSupport.swift | 3 ++- .../Utilities/Triple+Platforms.swift | 14 ++++++++++- Sources/SwiftDriver/Utilities/Triple.swift | 25 +++++++++++++------ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift b/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift index 7238ca7c9..20e2fdfd1 100644 --- a/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift +++ b/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift @@ -18,7 +18,8 @@ import struct TSCBasic.AbsolutePath extension GenericUnixToolchain { private func defaultLinker(for targetTriple: Triple) -> String? { if targetTriple.os == .openbsd || targetTriple.os == .freeBSD || - targetTriple.environment == .android { + targetTriple.environment == .android || + targetTriple.isFullyStaticLinux { return "lld" } diff --git a/Sources/SwiftDriver/Utilities/Triple+Platforms.swift b/Sources/SwiftDriver/Utilities/Triple+Platforms.swift index 380560093..1c8e38c47 100644 --- a/Sources/SwiftDriver/Utilities/Triple+Platforms.swift +++ b/Sources/SwiftDriver/Utilities/Triple+Platforms.swift @@ -292,7 +292,19 @@ extension Triple { return conflatingDarwin ? "darwin" : darwinPlatform.platformName case .linux: - return environment == .android ? "android" : "linux" + switch environment { + case .musl: + if vendor == .swift { + return "linux-static" + } + fallthrough + case .musl, .musleabihf, .musleabi: + return "musl" + case .android: + return "android" + default: + return "linux" + } case .freeBSD: return "freebsd" case .openbsd: diff --git a/Sources/SwiftDriver/Utilities/Triple.swift b/Sources/SwiftDriver/Utilities/Triple.swift index e3cc71dbb..100ddb942 100644 --- a/Sources/SwiftDriver/Utilities/Triple.swift +++ b/Sources/SwiftDriver/Utilities/Triple.swift @@ -1028,6 +1028,7 @@ extension Triple { case mesa case suse case openEmbedded = "oe" + case swift fileprivate static func parse(_ component: Substring) -> Triple.Vendor? { switch component { @@ -1063,6 +1064,8 @@ extension Triple { return .suse case "oe": return .openEmbedded + case "swift": + return .swift default: return nil } @@ -1706,13 +1709,21 @@ fileprivate extension Array { } } -// MARK: - Linker support +// MARK: - Fully static Linux support extension Triple { - /// Returns `true` if a given triple supports producing fully statically linked executables by providing `-static` - /// flag to the linker. This implies statically linking platform's libc, and of those that Swift supports currently - /// only Musl allows that reliably. - var supportsStaticExecutables: Bool { - self.environment == .musl - } + /// Returns `true` if this is the triple for Swift's fully statically + /// linked Linux target. + var isFullyStaticLinux: Bool { + self.vendor == .swift && self.environment == .musl + } + + /// Returns `true` if a given triple supports producing fully + /// statically linked executables by providing `-static` flag to + /// the linker. This implies statically linking platform's libc, + /// and of those that Swift supports currently only Musl allows + /// that reliably. + var supportsStaticExecutables: Bool { + self.isFullyStaticLinux + } } From 7b22cc60abf282e4cf1d024ba715bba20593e5c6 Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Tue, 27 Feb 2024 14:59:49 +0000 Subject: [PATCH 2/2] Add a comment about linux-static. "linux-static" refers to the new fully-statically-linked Linux support; the point here is that we want to distinguish that from just "Linux with musl", which is an entirely reasonable thing for us to support separately. rdar://123442522 --- Sources/SwiftDriver/Utilities/Triple+Platforms.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/SwiftDriver/Utilities/Triple+Platforms.swift b/Sources/SwiftDriver/Utilities/Triple+Platforms.swift index 1c8e38c47..4814ffe81 100644 --- a/Sources/SwiftDriver/Utilities/Triple+Platforms.swift +++ b/Sources/SwiftDriver/Utilities/Triple+Platforms.swift @@ -294,6 +294,8 @@ extension Triple { case .linux: switch environment { case .musl: + // The triple for linux-static is -swift-linux-musl, to distinguish + // it from a "normal" musl set-up (ala Alpine). if vendor == .swift { return "linux-static" }