Skip to content

Commit 56c4738

Browse files
committed
[CoreFoundation][Foundation][Tools] Miscellanous musl support.
This is mostly checking for and importing the `Musl` module and, where necessary, using it instead of the `Glibc` module. rdar://123381867
1 parent 321997f commit 56c4738

File tree

10 files changed

+34
-7
lines changed

10 files changed

+34
-7
lines changed

CoreFoundation/Base.subproj/CFUtilities.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1675,7 +1675,7 @@ CFDictionaryRef __CFGetEnvironment() {
16751675
extern char **environ;
16761676
char **envp = environ;
16771677
#elif TARGET_OS_LINUX
1678-
#if !defined(environ) && !TARGET_OS_ANDROID
1678+
#if !defined(environ) && !TARGET_OS_ANDROID && !defined(__musl__)
16791679
#define environ __environ
16801680
#endif
16811681
char **envp = environ;

Sources/Foundation/Data.swift

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
@usableFromInline let memset = Glibc.memset
2929
@usableFromInline let memcpy = Glibc.memcpy
3030
@usableFromInline let memcmp = Glibc.memcmp
31+
#elseif canImport(Musl)
32+
@usableFromInline let calloc = Musl.calloc
33+
@usableFromInline let malloc = Musl.malloc
34+
@usableFromInline let free = Musl.free
35+
@usableFromInline let memset = Musl.memset
36+
@usableFromInline let memcpy = Musl.memcpy
37+
@usableFromInline let memcmp = Musl.memcmp
3138
#elseif canImport(WASILibc)
3239
@usableFromInline let calloc = WASILibc.calloc
3340
@usableFromInline let malloc = WASILibc.malloc
@@ -48,6 +55,8 @@ internal func malloc_good_size(_ size: Int) -> Int {
4855

4956
#if canImport(Glibc)
5057
import Glibc
58+
#elseif canImport(Musl)
59+
import Musl
5160
#elseif canImport(WASILibc)
5261
import WASILibc
5362
#endif

Sources/Foundation/FileHandle.swift

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ import Glibc
2222
fileprivate let _read = Glibc.read(_:_:_:)
2323
fileprivate let _write = Glibc.write(_:_:_:)
2424
fileprivate let _close = Glibc.close(_:)
25+
#elseif canImport(Musl)
26+
import Musl
27+
fileprivate let _read = Musl.read(_:_:_:)
28+
fileprivate let _write = Musl.write(_:_:_:)
29+
fileprivate let _close = Musl.close(_:)
2530
#endif
2631

2732
#if canImport(WinSDK)

Sources/Foundation/Host.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ open class Host: NSObject {
281281
}
282282
var hints = addrinfo()
283283
hints.ai_family = PF_UNSPEC
284-
#if os(macOS) || os(iOS) || os(Android) || os(OpenBSD)
284+
#if os(macOS) || os(iOS) || os(Android) || os(OpenBSD) || canImport(Musl)
285285
hints.ai_socktype = SOCK_STREAM
286286
#else
287287
hints.ai_socktype = Int32(SOCK_STREAM.rawValue)

Sources/Foundation/NSData.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,10 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
494494
let createMode = Int(ucrt.S_IREAD) | Int(ucrt.S_IWRITE)
495495
#elseif canImport(Darwin)
496496
let createMode = Int(S_IRUSR) | Int(S_IWUSR) | Int(S_IRGRP) | Int(S_IWGRP) | Int(S_IROTH) | Int(S_IWOTH)
497-
#else
497+
#elseif canImport(Glibc)
498498
let createMode = Int(Glibc.S_IRUSR) | Int(Glibc.S_IWUSR) | Int(Glibc.S_IRGRP) | Int(Glibc.S_IWGRP) | Int(Glibc.S_IROTH) | Int(Glibc.S_IWOTH)
499+
#elseif canImport(Musl)
500+
let createMode = Int(Musl.S_IRUSR) | Int(Musl.S_IWUSR) | Int(Musl.S_IRGRP) | Int(Musl.S_IWGRP) | Int(Musl.S_IROTH) | Int(Musl.S_IWOTH)
499501
#endif
500502
guard let fh = FileHandle(path: path, flags: flags, createMode: createMode) else {
501503
throw _NSErrorWithErrno(errno, reading: false, path: path)

Sources/Foundation/NSSwiftRuntime.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
// This mimics the behavior of the swift sdk overlay on Darwin
1515
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
1616
@_exported import Darwin
17-
#elseif os(Linux) || os(Android) || CYGWIN || os(OpenBSD)
17+
#elseif canImport(Glibc)
1818
@_exported import Glibc
19+
#elseif canImport(Musl)
20+
@_exported import Musl
1921
#elseif os(WASI)
2022
@_exported import WASILibc
2123
#elseif os(Windows)

Sources/Foundation/NSURL.swift

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ internal let kCFURLWindowsPathStyle = CFURLPathStyle.cfurlWindowsPathStyle
2020
import Darwin
2121
#elseif canImport(Glibc)
2222
import Glibc
23+
#elseif canImport(Musl)
24+
import Musl
2325
#endif
2426

2527
// NOTE: this represents PLATFORM_PATH_STYLE

Sources/Foundation/Process.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ open class Process: NSObject {
776776
}
777777

778778
var taskSocketPair : [Int32] = [0, 0]
779-
#if os(macOS) || os(iOS) || os(Android) || os(OpenBSD)
779+
#if os(macOS) || os(iOS) || os(Android) || os(OpenBSD) || canImport(Musl)
780780
socketpair(AF_UNIX, SOCK_STREAM, 0, &taskSocketPair)
781781
#else
782782
socketpair(AF_UNIX, Int32(SOCK_STREAM.rawValue), 0, &taskSocketPair)

Sources/Foundation/Thread.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import WinSDK
1414

1515
#if canImport(Glibc)
1616
import Glibc
17+
#elseif canImport(Musl)
18+
import Musl
1719
#endif
1820

1921
// WORKAROUND_SR9811
@@ -356,13 +358,15 @@ open class Thread : NSObject {
356358
_cancelled = true
357359
}
358360

361+
// ###TODO: Switch these over to using the Swift runtime's backtracer
362+
// once we have Windows support there.
359363

360364
private class func backtraceAddresses<T>(_ body: (UnsafeMutablePointer<UnsafeMutableRawPointer?>, Int) -> [T]) -> [T] {
361365
// Same as swift/stdlib/public/runtime/Errors.cpp backtrace
362366
let maxSupportedStackDepth = 128;
363367
let addrs = UnsafeMutablePointer<UnsafeMutableRawPointer?>.allocate(capacity: maxSupportedStackDepth)
364368
defer { addrs.deallocate() }
365-
#if os(Android) || os(OpenBSD)
369+
#if os(Android) || os(OpenBSD) || canImport(Musl)
366370
let count = 0
367371
#elseif os(Windows)
368372
let count = RtlCaptureStackBackTrace(0, DWORD(maxSupportedStackDepth),
@@ -383,7 +387,7 @@ open class Thread : NSObject {
383387
}
384388

385389
open class var callStackSymbols: [String] {
386-
#if os(Android) || os(OpenBSD)
390+
#if os(Android) || os(OpenBSD) || canImport(Musl)
387391
return []
388392
#elseif os(Windows)
389393
let hProcess: HANDLE = GetCurrentProcess()

Sources/Tools/plutil/main.swift

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import SwiftFoundation
1212
#elseif canImport(Glibc)
1313
import Foundation
1414
import Glibc
15+
#elseif canImport(Musl)
16+
import Foundation
17+
import Musl
1518
#elseif canImport(CRT)
1619
import Foundation
1720
import CRT

0 commit comments

Comments
 (0)