From 53ba766966682076170bf70dbe968e7cdeb86075 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Tue, 18 Jul 2023 14:45:41 +0100 Subject: [PATCH] Add support for Musl libc with `canImport(Musl)` checks Since Musl is sufficiently different from Glibc (see https://wiki.musl-libc.org/functional-differences-from-glibc.html), it requires a different import, which now should be applied to files that have `import Glibc` in them. Musl is a low footprint libc that's used in Linux distributions such as Alpine Linux, which allows producing fairly small container images. Additionally, unlike Glibc, musl allows full static linking, meaning apps can be easily distributed to an arbitrary Linux distribution that may have a version of Glibc incompatible with the one that Swift is usually built with or no Glibc installed at all. --- Sources/LanguageServerProtocolJSONRPC/DisableSigpipe.swift | 6 ++++-- Sources/SKSupport/dlopen.swift | 4 +++- Sources/SourceKitD/SKDRequestArray.swift | 2 ++ Sources/SourceKitD/SKDRequestDictionary.swift | 2 ++ Sources/SourceKitD/SKDResponse.swift | 2 ++ Sources/SourceKitD/SKDResponseArray.swift | 2 ++ Sources/SourceKitD/SKDResponseDictionary.swift | 2 ++ 7 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Sources/LanguageServerProtocolJSONRPC/DisableSigpipe.swift b/Sources/LanguageServerProtocolJSONRPC/DisableSigpipe.swift index 1f1e59195..6d8267a86 100644 --- a/Sources/LanguageServerProtocolJSONRPC/DisableSigpipe.swift +++ b/Sources/LanguageServerProtocolJSONRPC/DisableSigpipe.swift @@ -13,13 +13,15 @@ #if canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl #endif -#if os(Linux) || os(Android) +#if canImport(Glibc) || canImport(Musl) // This is a lazily initialised global variable that when read for the first time, will ignore SIGPIPE. private let globallyIgnoredSIGPIPE: Bool = { /* no F_SETNOSIGPIPE on Linux :( */ - _ = Glibc.signal(SIGPIPE, SIG_IGN) + _ = signal(SIGPIPE, SIG_IGN) return true }() diff --git a/Sources/SKSupport/dlopen.swift b/Sources/SKSupport/dlopen.swift index 1fb7cb2c5..d8841ef66 100644 --- a/Sources/SKSupport/dlopen.swift +++ b/Sources/SKSupport/dlopen.swift @@ -15,8 +15,10 @@ import CRT import WinSDK #elseif os(iOS) || os(macOS) || os(tvOS) || os(watchOS) import Darwin -#else +#elseif canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl #endif public final class DLHandle { diff --git a/Sources/SourceKitD/SKDRequestArray.swift b/Sources/SourceKitD/SKDRequestArray.swift index d2f2192af..d7c8bbb37 100644 --- a/Sources/SourceKitD/SKDRequestArray.swift +++ b/Sources/SourceKitD/SKDRequestArray.swift @@ -13,6 +13,8 @@ import Csourcekitd #if canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl #elseif canImport(CRT) import CRT #endif diff --git a/Sources/SourceKitD/SKDRequestDictionary.swift b/Sources/SourceKitD/SKDRequestDictionary.swift index d188b52b7..9505a2c6a 100644 --- a/Sources/SourceKitD/SKDRequestDictionary.swift +++ b/Sources/SourceKitD/SKDRequestDictionary.swift @@ -13,6 +13,8 @@ import Csourcekitd #if canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl #elseif canImport(CRT) import CRT #endif diff --git a/Sources/SourceKitD/SKDResponse.swift b/Sources/SourceKitD/SKDResponse.swift index 7e693f185..aac2af3c1 100644 --- a/Sources/SourceKitD/SKDResponse.swift +++ b/Sources/SourceKitD/SKDResponse.swift @@ -13,6 +13,8 @@ import Csourcekitd #if canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl #elseif canImport(CRT) import CRT #endif diff --git a/Sources/SourceKitD/SKDResponseArray.swift b/Sources/SourceKitD/SKDResponseArray.swift index ff55244df..6b6076d0e 100644 --- a/Sources/SourceKitD/SKDResponseArray.swift +++ b/Sources/SourceKitD/SKDResponseArray.swift @@ -13,6 +13,8 @@ import Csourcekitd #if canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl #elseif canImport(CRT) import CRT #endif diff --git a/Sources/SourceKitD/SKDResponseDictionary.swift b/Sources/SourceKitD/SKDResponseDictionary.swift index 3025d54e4..55b0117b5 100644 --- a/Sources/SourceKitD/SKDResponseDictionary.swift +++ b/Sources/SourceKitD/SKDResponseDictionary.swift @@ -13,6 +13,8 @@ import Csourcekitd #if canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl #elseif canImport(CRT) import CRT #endif