Skip to content

Commit 9e9b92d

Browse files
Update vendored SystemPackage for Musl support
1 parent ca470ec commit 9e9b92d

File tree

6 files changed

+88
-14
lines changed

6 files changed

+88
-14
lines changed

Sources/SystemExtras/Clock.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#if SYSTEM_PACKAGE_DARWIN
22
import Darwin
3-
#elseif os(Linux) || os(FreeBSD) || os(Android)
4-
import CSystem
3+
#elseif canImport(Glibc)
4+
@_implementationOnly import CSystem
55
import Glibc
6+
#elseif canImport(Musl)
7+
@_implementationOnly import CSystem
8+
import Musl
69
#elseif os(Windows)
710
import CSystem
811
import ucrt

Sources/SystemExtras/Constants.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#if SYSTEM_PACKAGE_DARWIN
22
import Darwin
3-
#elseif os(Linux) || os(FreeBSD) || os(Android)
4-
import CSystem
3+
#elseif canImport(Glibc)
4+
@_implementationOnly import CSystem
55
import Glibc
6+
#elseif canImport(Musl)
7+
@_implementationOnly import CSystem
8+
import Musl
69
#elseif os(Windows)
710
import CSystem
811
import ucrt

Sources/SystemExtras/FileAtOperations.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#if SYSTEM_PACKAGE_DARWIN
22
import Darwin
3-
#elseif os(Linux) || os(FreeBSD) || os(Android)
3+
#elseif canImport(Glibc)
4+
@_implementationOnly import CSystem
45
import Glibc
6+
#elseif canImport(Musl)
7+
@_implementationOnly import CSystem
8+
import Musl
59
#elseif os(Windows)
610
import ucrt
711
import WinSDK

Sources/SystemExtras/FileOperations.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#if SYSTEM_PACKAGE_DARWIN
22
import Darwin
3-
#elseif os(Linux) || os(FreeBSD) || os(Android)
3+
#elseif canImport(Glibc)
4+
@_implementationOnly import CSystem
45
import Glibc
6+
#elseif canImport(Musl)
7+
@_implementationOnly import CSystem
8+
import Musl
59
#elseif os(Windows)
610
import ucrt
711
import WinSDK

Sources/SystemExtras/Syscalls.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#if SYSTEM_PACKAGE_DARWIN
22
import Darwin
3-
#elseif os(Linux) || os(FreeBSD) || os(Android)
3+
#elseif canImport(Glibc)
4+
@_implementationOnly import CSystem
45
import Glibc
6+
#elseif canImport(Musl)
7+
@_implementationOnly import CSystem
8+
import Musl
59
#elseif os(Windows)
610
import ucrt
711
import WinSDK

Sources/SystemExtras/Vendor/Exports.swift

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift System open source project
33

4-
Copyright (c) 2020 Apple Inc. and the Swift System project authors
4+
Copyright (c) 2020 - 2024 Apple Inc. and the Swift System project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -14,12 +14,20 @@
1414

1515
#if SYSTEM_PACKAGE_DARWIN
1616
import Darwin
17-
#elseif os(Linux) || os(FreeBSD) || os(Android)
18-
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
26+
#elseif canImport(WASILibc)
27+
import WASILibc
28+
#elseif canImport(Android)
29+
@_implementationOnly import CSystem
30+
import Android
2331
#else
2432
#error("Unsupported Platform")
2533
#endif
@@ -47,11 +55,26 @@ internal var system_errno: CInt {
4755
_ = ucrt._set_errno(newValue)
4856
}
4957
}
50-
#else
58+
#elseif canImport(Glibc)
5159
internal var system_errno: CInt {
5260
get { Glibc.errno }
5361
set { Glibc.errno = newValue }
5462
}
63+
#elseif canImport(Musl)
64+
internal var system_errno: CInt {
65+
get { Musl.errno }
66+
set { Musl.errno = newValue }
67+
}
68+
#elseif canImport(WASILibc)
69+
internal var system_errno: CInt {
70+
get { WASILibc.errno }
71+
set { WASILibc.errno = newValue }
72+
}
73+
#elseif canImport(Android)
74+
internal var system_errno: CInt {
75+
get { Android.errno }
76+
set { Android.errno = newValue }
77+
}
5578
#endif
5679

5780
// MARK: C stdlib decls
@@ -62,7 +85,10 @@ internal func system_strerror(_ __errnum: Int32) -> UnsafeMutablePointer<Int8>!
6285
strerror(__errnum)
6386
}
6487

65-
internal func system_strlen(_ s: UnsafePointer<Int8>) -> Int {
88+
internal func system_strlen(_ s: UnsafePointer<CChar>) -> Int {
89+
strlen(s)
90+
}
91+
internal func system_strlen(_ s: UnsafeMutablePointer<CChar>) -> Int {
6692
strlen(s)
6793
}
6894

@@ -80,7 +106,17 @@ internal func system_platform_strlen(_ s: UnsafePointer<CInterop.PlatformChar>)
80106
#endif
81107
}
82108

83-
// Interop between String and platform string
109+
// memset for raw buffers
110+
// FIXME: Do we really not have something like this in the stdlib already?
111+
internal func system_memset(
112+
_ buffer: UnsafeMutableRawBufferPointer,
113+
to byte: UInt8
114+
) {
115+
guard buffer.count > 0 else { return }
116+
memset(buffer.baseAddress!, CInt(byte), buffer.count)
117+
}
118+
119+
// Interop between String and platfrom string
84120
extension String {
85121
internal func _withPlatformString<Result>(
86122
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
@@ -130,6 +166,24 @@ extension String {
130166
// TLS
131167
#if os(Windows)
132168
internal typealias _PlatformTLSKey = DWORD
169+
#elseif os(WASI) && (swift(<6.1) || !_runtime(_multithreaded))
170+
// Mock TLS storage for single-threaded WASI
171+
internal final class _PlatformTLSKey {
172+
fileprivate init() {}
173+
}
174+
private final class TLSStorage: @unchecked Sendable {
175+
var storage = [ObjectIdentifier: UnsafeMutableRawPointer]()
176+
}
177+
private let sharedTLSStorage = TLSStorage()
178+
179+
func pthread_setspecific(_ key: _PlatformTLSKey, _ p: UnsafeMutableRawPointer?) -> Int {
180+
sharedTLSStorage.storage[ObjectIdentifier(key)] = p
181+
return 0
182+
}
183+
184+
func pthread_getspecific(_ key: _PlatformTLSKey) -> UnsafeMutableRawPointer? {
185+
sharedTLSStorage.storage[ObjectIdentifier(key)]
186+
}
133187
#else
134188
internal typealias _PlatformTLSKey = pthread_key_t
135189
#endif
@@ -141,6 +195,8 @@ internal func makeTLSKey() -> _PlatformTLSKey {
141195
fatalError("Unable to create key")
142196
}
143197
return raw
198+
#elseif os(WASI) && (swift(<6.1) || !_runtime(_multithreaded))
199+
return _PlatformTLSKey()
144200
#else
145201
var raw = pthread_key_t()
146202
guard 0 == pthread_key_create(&raw, nil) else {

0 commit comments

Comments
 (0)