Skip to content

Commit 0062c82

Browse files
authored
Add support for Musl libc (#133)
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 4116b8c commit 0062c82

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

Sources/System/Internals/CInterop.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99

1010
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
1111
import Darwin
12-
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android)
13-
@_implementationOnly import CSystem
14-
import Glibc
1512
#elseif os(Windows)
1613
import CSystem
1714
import ucrt
15+
#elseif canImport(Glibc)
16+
@_implementationOnly import CSystem
17+
import Glibc
18+
#elseif canImport(Musl)
19+
@_implementationOnly import CSystem
20+
import Musl
1821
#else
1922
#error("Unsupported Platform")
2023
#endif

Sources/System/Internals/Constants.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313

1414
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
1515
import Darwin
16-
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android)
17-
import Glibc
1816
#elseif os(Windows)
1917
import CSystem
2018
import ucrt
19+
#elseif canImport(Glibc)
20+
import Glibc
21+
#elseif canImport(Musl)
22+
import CSystem
23+
import Musl
2124
#else
2225
#error("Unsupported Platform")
2326
#endif
@@ -454,9 +457,13 @@ internal var _O_WRONLY: CInt { O_WRONLY }
454457
internal var _O_RDWR: CInt { O_RDWR }
455458

456459
#if !os(Windows)
460+
#if canImport(Musl)
461+
internal var _O_ACCMODE: CInt { 0x03|O_SEARCH }
462+
#else
457463
// TODO: API?
458464
@_alwaysEmitIntoClient
459465
internal var _O_ACCMODE: CInt { O_ACCMODE }
466+
#endif
460467

461468
@_alwaysEmitIntoClient
462469
internal var _O_NONBLOCK: CInt { O_NONBLOCK }

Sources/System/Internals/Exports.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414

1515
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
1616
import Darwin
17-
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android)
18-
@_implementationOnly import CSystem
19-
import Glibc
2017
#elseif os(Windows)
2118
import CSystem
2219
import ucrt
20+
#elseif canImport(Glibc)
21+
@_implementationOnly import CSystem
22+
import Glibc
23+
#elseif canImport(Musl)
24+
@_implementationOnly import CSystem
25+
import Musl
2326
#else
2427
#error("Unsupported Platform")
2528
#endif
@@ -45,11 +48,16 @@ internal var system_errno: CInt {
4548
_ = ucrt._set_errno(newValue)
4649
}
4750
}
48-
#else
51+
#elseif canImport(Glibc)
4952
internal var system_errno: CInt {
5053
get { Glibc.errno }
5154
set { Glibc.errno = newValue }
5255
}
56+
#elseif canImport(Musl)
57+
internal var system_errno: CInt {
58+
get { Musl.errno }
59+
set { Musl.errno = newValue }
60+
}
5361
#endif
5462

5563
// MARK: C stdlib decls

Sources/System/Internals/Syscalls.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99

1010
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
1111
import Darwin
12-
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android)
12+
#elseif canImport(Glibc)
1313
import Glibc
14+
#elseif canImport(Musl)
15+
import Musl
1416
#elseif os(Windows)
1517
import ucrt
1618
#else

0 commit comments

Comments
 (0)