Skip to content

Commit

Permalink
Add support for Musl libc (#423)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MaxDesiatov authored Jun 22, 2023
1 parent 8a19fe9 commit 33ae5ab
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Sources/TSCBasic/WritableByteStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<FILE>
Expand Down
2 changes: 2 additions & 0 deletions Sources/TSCLibc/libc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions Sources/TSCUtility/FSWatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -621,7 +621,7 @@ public final class Inotify {
// FIXME: <rdar://problem/45794219> 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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/TSCUtility/IndexStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions Sources/TSCUtility/InterruptHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

#if !canImport(Musl)

import TSCLibc
import TSCBasic

Expand Down Expand Up @@ -135,3 +137,5 @@ public final class InterruptHandler {
thread.join()
}
}

#endif
2 changes: 1 addition & 1 deletion Sources/TSCUtility/dlopen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 33ae5ab

Please sign in to comment.