Skip to content

Commit 33ae5ab

Browse files
authored
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.
1 parent 8a19fe9 commit 33ae5ab

File tree

6 files changed

+16
-10
lines changed

6 files changed

+16
-10
lines changed

Sources/TSCBasic/WritableByteStream.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public extension WritableByteStream {
6666
// Public alias to the old name to not introduce API compatibility.
6767
public typealias OutputByteStream = WritableByteStream
6868

69-
#if os(Android)
69+
#if os(Android) || canImport(Musl)
7070
public typealias FILEPointer = OpaquePointer
7171
#else
7272
public typealias FILEPointer = UnsafeMutablePointer<FILE>

Sources/TSCLibc/libc.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#if canImport(Glibc)
1212
@_exported import Glibc
13+
#elseif canImport(Musl)
14+
@_exported import Musl
1315
#elseif os(Windows)
1416
@_exported import CRT
1517
@_exported import WinSDK

Sources/TSCUtility/FSWatch.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class FSWatch {
5454
self._watcher = NoOpWatcher(paths: paths, latency: latency, delegate: _WatcherDelegate(block: block))
5555
#elseif os(Windows)
5656
self._watcher = RDCWatcher(paths: paths, latency: latency, delegate: _WatcherDelegate(block: block))
57-
#elseif canImport(Glibc)
57+
#elseif canImport(Glibc) || canImport(Musl)
5858
var ipaths: [AbsolutePath: Inotify.WatchOptions] = [:]
5959

6060
// FIXME: We need to recurse here.
@@ -106,7 +106,7 @@ extension NoOpWatcher: _FileWatcher{}
106106
#elseif os(Windows)
107107
extension FSWatch._WatcherDelegate: RDCWatcherDelegate {}
108108
extension RDCWatcher: _FileWatcher {}
109-
#elseif canImport(Glibc)
109+
#elseif canImport(Glibc) || canImport(Musl)
110110
extension FSWatch._WatcherDelegate: InotifyDelegate {}
111111
extension Inotify: _FileWatcher{}
112112
#elseif os(macOS)
@@ -296,7 +296,7 @@ public final class RDCWatcher {
296296
}
297297
}
298298

299-
#elseif canImport(Glibc)
299+
#elseif canImport(Glibc) || canImport(Musl)
300300

301301
/// The delegate for receiving inotify events.
302302
public protocol InotifyDelegate {
@@ -621,7 +621,7 @@ public final class Inotify {
621621
// FIXME: <rdar://problem/45794219> Swift should provide shims for FD_ macros
622622

623623
private func FD_ZERO(_ set: inout fd_set) {
624-
#if os(Android)
624+
#if os(Android) || canImport(Musl)
625625
#if arch(arm)
626626
set.fds_bits = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
627627
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) {
641641
private func FD_SET(_ fd: Int32, _ set: inout fd_set) {
642642
let intOffset = Int(fd / 16)
643643
let bitOffset = Int(fd % 16)
644-
#if os(Android)
644+
#if os(Android) || canImport(Musl)
645645
var fd_bits = set.fds_bits
646646
let mask: UInt = 1 << bitOffset
647647
#else
@@ -685,7 +685,7 @@ private func FD_SET(_ fd: Int32, _ set: inout fd_set) {
685685
#endif
686686
default: break
687687
}
688-
#if os(Android)
688+
#if os(Android) || canImport(Musl)
689689
set.fds_bits = fd_bits
690690
#else
691691
set.__fds_bits = fd_bits
@@ -695,7 +695,7 @@ private func FD_SET(_ fd: Int32, _ set: inout fd_set) {
695695
private func FD_ISSET(_ fd: Int32, _ set: inout fd_set) -> Bool {
696696
let intOffset = Int(fd / 32)
697697
let bitOffset = Int(fd % 32)
698-
#if os(Android)
698+
#if os(Android) || canImport(Musl)
699699
let fd_bits = set.fds_bits
700700
let mask: UInt = 1 << bitOffset
701701
#else

Sources/TSCUtility/IndexStore.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ private struct _DLOpenFlags: RawRepresentable, OptionSet {
457457
public static let deepBind: _DLOpenFlags = _DLOpenFlags(rawValue: 0)
458458
#else
459459
public static let first: _DLOpenFlags = _DLOpenFlags(rawValue: 0)
460-
#if os(Linux)
460+
#if os(Linux) && canImport(Glibc)
461461
public static let deepBind: _DLOpenFlags = _DLOpenFlags(rawValue: RTLD_DEEPBIND)
462462
#else
463463
public static let deepBind: _DLOpenFlags = _DLOpenFlags(rawValue: 0)

Sources/TSCUtility/InterruptHandler.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11+
#if !canImport(Musl)
12+
1113
import TSCLibc
1214
import TSCBasic
1315

@@ -135,3 +137,5 @@ public final class InterruptHandler {
135137
thread.join()
136138
}
137139
}
140+
141+
#endif

Sources/TSCUtility/dlopen.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public struct DLOpenFlags: RawRepresentable, OptionSet {
6666
public static let deepBind: DLOpenFlags = DLOpenFlags(rawValue: 0)
6767
#else
6868
public static let first: DLOpenFlags = DLOpenFlags(rawValue: 0)
69-
#if os(Linux)
69+
#if os(Linux) && canImport(Glibc)
7070
public static let deepBind: DLOpenFlags = DLOpenFlags(rawValue: RTLD_DEEPBIND)
7171
#else
7272
public static let deepBind: DLOpenFlags = DLOpenFlags(rawValue: 0)

0 commit comments

Comments
 (0)