Skip to content

Commit

Permalink
Add support for Musl libc (#133)
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 Jul 4, 2023
1 parent 4116b8c commit 0062c82
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
9 changes: 6 additions & 3 deletions Sources/System/Internals/CInterop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android)
@_implementationOnly import CSystem
import Glibc
#elseif os(Windows)
import CSystem
import ucrt
#elseif canImport(Glibc)
@_implementationOnly import CSystem
import Glibc
#elseif canImport(Musl)
@_implementationOnly import CSystem
import Musl
#else
#error("Unsupported Platform")
#endif
Expand Down
11 changes: 9 additions & 2 deletions Sources/System/Internals/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android)
import Glibc
#elseif os(Windows)
import CSystem
import ucrt
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import CSystem
import Musl
#else
#error("Unsupported Platform")
#endif
Expand Down Expand Up @@ -454,9 +457,13 @@ internal var _O_WRONLY: CInt { O_WRONLY }
internal var _O_RDWR: CInt { O_RDWR }

#if !os(Windows)
#if canImport(Musl)
internal var _O_ACCMODE: CInt { 0x03|O_SEARCH }
#else
// TODO: API?
@_alwaysEmitIntoClient
internal var _O_ACCMODE: CInt { O_ACCMODE }
#endif

@_alwaysEmitIntoClient
internal var _O_NONBLOCK: CInt { O_NONBLOCK }
Expand Down
16 changes: 12 additions & 4 deletions Sources/System/Internals/Exports.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android)
@_implementationOnly import CSystem
import Glibc
#elseif os(Windows)
import CSystem
import ucrt
#elseif canImport(Glibc)
@_implementationOnly import CSystem
import Glibc
#elseif canImport(Musl)
@_implementationOnly import CSystem
import Musl
#else
#error("Unsupported Platform")
#endif
Expand All @@ -45,11 +48,16 @@ internal var system_errno: CInt {
_ = ucrt._set_errno(newValue)
}
}
#else
#elseif canImport(Glibc)
internal var system_errno: CInt {
get { Glibc.errno }
set { Glibc.errno = newValue }
}
#elseif canImport(Musl)
internal var system_errno: CInt {
get { Musl.errno }
set { Musl.errno = newValue }
}
#endif

// MARK: C stdlib decls
Expand Down
4 changes: 3 additions & 1 deletion Sources/System/Internals/Syscalls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android)
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#elseif os(Windows)
import ucrt
#else
Expand Down

0 comments on commit 0062c82

Please sign in to comment.