From 33ae5abc07ea505a9865b4c11329db7a83cb3094 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Thu, 22 Jun 2023 18:38:55 +0100 Subject: [PATCH] Add support for Musl libc (#423) 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. --- Sources/TSCBasic/WritableByteStream.swift | 2 +- Sources/TSCLibc/libc.swift | 2 ++ Sources/TSCUtility/FSWatch.swift | 14 +++++++------- Sources/TSCUtility/IndexStore.swift | 2 +- Sources/TSCUtility/InterruptHandler.swift | 4 ++++ Sources/TSCUtility/dlopen.swift | 2 +- 6 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Sources/TSCBasic/WritableByteStream.swift b/Sources/TSCBasic/WritableByteStream.swift index a59c4dbd..aee907e3 100644 --- a/Sources/TSCBasic/WritableByteStream.swift +++ b/Sources/TSCBasic/WritableByteStream.swift @@ -66,7 +66,7 @@ public extension WritableByteStream { // Public alias to the old name to not introduce API compatibility. public typealias OutputByteStream = WritableByteStream -#if os(Android) +#if os(Android) || canImport(Musl) public typealias FILEPointer = OpaquePointer #else public typealias FILEPointer = UnsafeMutablePointer diff --git a/Sources/TSCLibc/libc.swift b/Sources/TSCLibc/libc.swift index 75dba91e..8c5c898e 100644 --- a/Sources/TSCLibc/libc.swift +++ b/Sources/TSCLibc/libc.swift @@ -10,6 +10,8 @@ #if canImport(Glibc) @_exported import Glibc +#elseif canImport(Musl) +@_exported import Musl #elseif os(Windows) @_exported import CRT @_exported import WinSDK diff --git a/Sources/TSCUtility/FSWatch.swift b/Sources/TSCUtility/FSWatch.swift index b0a4cd8e..5cea437b 100644 --- a/Sources/TSCUtility/FSWatch.swift +++ b/Sources/TSCUtility/FSWatch.swift @@ -54,7 +54,7 @@ public class FSWatch { self._watcher = NoOpWatcher(paths: paths, latency: latency, delegate: _WatcherDelegate(block: block)) #elseif os(Windows) self._watcher = RDCWatcher(paths: paths, latency: latency, delegate: _WatcherDelegate(block: block)) - #elseif canImport(Glibc) + #elseif canImport(Glibc) || canImport(Musl) var ipaths: [AbsolutePath: Inotify.WatchOptions] = [:] // FIXME: We need to recurse here. @@ -106,7 +106,7 @@ extension NoOpWatcher: _FileWatcher{} #elseif os(Windows) extension FSWatch._WatcherDelegate: RDCWatcherDelegate {} extension RDCWatcher: _FileWatcher {} -#elseif canImport(Glibc) +#elseif canImport(Glibc) || canImport(Musl) extension FSWatch._WatcherDelegate: InotifyDelegate {} extension Inotify: _FileWatcher{} #elseif os(macOS) @@ -296,7 +296,7 @@ public final class RDCWatcher { } } -#elseif canImport(Glibc) +#elseif canImport(Glibc) || canImport(Musl) /// The delegate for receiving inotify events. public protocol InotifyDelegate { @@ -621,7 +621,7 @@ public final class Inotify { // FIXME: Swift should provide shims for FD_ macros private func FD_ZERO(_ set: inout fd_set) { - #if os(Android) + #if os(Android) || canImport(Musl) #if arch(arm) set.fds_bits = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) @@ -641,7 +641,7 @@ private func FD_ZERO(_ set: inout fd_set) { private func FD_SET(_ fd: Int32, _ set: inout fd_set) { let intOffset = Int(fd / 16) let bitOffset = Int(fd % 16) - #if os(Android) + #if os(Android) || canImport(Musl) var fd_bits = set.fds_bits let mask: UInt = 1 << bitOffset #else @@ -685,7 +685,7 @@ private func FD_SET(_ fd: Int32, _ set: inout fd_set) { #endif default: break } - #if os(Android) + #if os(Android) || canImport(Musl) set.fds_bits = fd_bits #else set.__fds_bits = fd_bits @@ -695,7 +695,7 @@ private func FD_SET(_ fd: Int32, _ set: inout fd_set) { private func FD_ISSET(_ fd: Int32, _ set: inout fd_set) -> Bool { let intOffset = Int(fd / 32) let bitOffset = Int(fd % 32) - #if os(Android) + #if os(Android) || canImport(Musl) let fd_bits = set.fds_bits let mask: UInt = 1 << bitOffset #else diff --git a/Sources/TSCUtility/IndexStore.swift b/Sources/TSCUtility/IndexStore.swift index 8f633966..7ba5b07b 100644 --- a/Sources/TSCUtility/IndexStore.swift +++ b/Sources/TSCUtility/IndexStore.swift @@ -457,7 +457,7 @@ private struct _DLOpenFlags: RawRepresentable, OptionSet { public static let deepBind: _DLOpenFlags = _DLOpenFlags(rawValue: 0) #else public static let first: _DLOpenFlags = _DLOpenFlags(rawValue: 0) - #if os(Linux) + #if os(Linux) && canImport(Glibc) public static let deepBind: _DLOpenFlags = _DLOpenFlags(rawValue: RTLD_DEEPBIND) #else public static let deepBind: _DLOpenFlags = _DLOpenFlags(rawValue: 0) diff --git a/Sources/TSCUtility/InterruptHandler.swift b/Sources/TSCUtility/InterruptHandler.swift index 8978f83d..c10d4fe1 100644 --- a/Sources/TSCUtility/InterruptHandler.swift +++ b/Sources/TSCUtility/InterruptHandler.swift @@ -8,6 +8,8 @@ See http://swift.org/CONTRIBUTORS.txt for Swift project authors */ +#if !canImport(Musl) + import TSCLibc import TSCBasic @@ -135,3 +137,5 @@ public final class InterruptHandler { thread.join() } } + +#endif diff --git a/Sources/TSCUtility/dlopen.swift b/Sources/TSCUtility/dlopen.swift index 3effa0ee..0d101335 100644 --- a/Sources/TSCUtility/dlopen.swift +++ b/Sources/TSCUtility/dlopen.swift @@ -66,7 +66,7 @@ public struct DLOpenFlags: RawRepresentable, OptionSet { public static let deepBind: DLOpenFlags = DLOpenFlags(rawValue: 0) #else public static let first: DLOpenFlags = DLOpenFlags(rawValue: 0) - #if os(Linux) + #if os(Linux) && canImport(Glibc) public static let deepBind: DLOpenFlags = DLOpenFlags(rawValue: RTLD_DEEPBIND) #else public static let deepBind: DLOpenFlags = DLOpenFlags(rawValue: 0)