Skip to content

Commit 5a1db79

Browse files
authored
Merge pull request #4876 from al45tair/eng/PR-123381867
Fix swift-corelibs-foundation for Musl compatibility
2 parents 806ba24 + a3c2228 commit 5a1db79

22 files changed

+142
-26
lines changed

CMakeLists.txt

+17-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ project(Foundation
1313
LANGUAGES C Swift)
1414
enable_testing()
1515

16+
if(NOT SWIFT_SYSTEM_NAME)
17+
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
18+
set(SWIFT_SYSTEM_NAME macosx)
19+
else()
20+
set(SWIFT_SYSTEM_NAME "$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
21+
endif()
22+
endif()
23+
1624
# NOTE(compnerd) default to /MD or /MDd by default based on the configuration.
1725
# Cache the variable to allow the user to alter the configuration.
1826
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL" CACHE
@@ -38,6 +46,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
3846
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
3947

4048
option(BUILD_SHARED_LIBS "build shared libraries" ON)
49+
option(BUILD_FULLY_STATIC "build fully static" NO)
4150
option(HAS_LIBDISPATCH_API "has libdispatch API" ON)
4251
option(FOUNDATION_ENABLE_FOUNDATION_NETWORKING "build FoundationNetworking module" ON)
4352
option(FOUNDATION_BUILD_TOOLS "build tools" ON)
@@ -49,6 +58,12 @@ endif()
4958

5059
find_package(ICU COMPONENTS uc i18n REQUIRED OPTIONAL_COMPONENTS data)
5160

61+
# This is needed if we're statically linking, otherwise we can't pull in Dispatch
62+
# because we won't have RT::rt as a CMake target.
63+
if(NOT CMAKE_SYSTEM_NAME STREQUAL Android)
64+
find_package(LibRT)
65+
endif()
66+
5267
include(SwiftSupport)
5368
include(GNUInstallDirs)
5469
include(XCTest)
@@ -101,13 +116,13 @@ if(NOT BUILD_SHARED_LIBS)
101116
endif()
102117

103118
install(TARGETS CoreFoundation CFXMLInterface
104-
DESTINATION lib/swift_static/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>)
119+
DESTINATION lib/swift_static/${SWIFT_SYSTEM_NAME})
105120

106121
if(FOUNDATION_ENABLE_FOUNDATION_NETWORKING)
107122
set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS
108123
CFURLSessionInterface)
109124
install(TARGETS CFURLSessionInterface
110-
DESTINATION lib/swift_static/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>)
125+
DESTINATION lib/swift_static/${SWIFT_SYSTEM_NAME})
111126
endif()
112127
endif()
113128

CoreFoundation/Base.subproj/CFUtilities.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1680,7 +1680,7 @@ CFDictionaryRef __CFGetEnvironment() {
16801680
extern char **environ;
16811681
char **envp = environ;
16821682
#elif TARGET_OS_LINUX
1683-
#if !defined(environ) && !TARGET_OS_ANDROID
1683+
#if !defined(environ) && !TARGET_OS_ANDROID && !defined(__musl__)
16841684
#define environ __environ
16851685
#endif
16861686
char **envp = environ;

CoreFoundation/Base.subproj/CoreFoundation_Prefix.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ static dispatch_queue_t __ ## PREFIX ## Queue(void) { \
189189
#define CF_RETAIN_BALANCED_ELSEWHERE(obj, identified_location) do { } while (0)
190190
#endif
191191

192-
#if (TARGET_OS_LINUX && !TARGET_OS_ANDROID && !TARGET_OS_CYGWIN) || TARGET_OS_WIN32
192+
#if !TARGET_OS_MAC
193+
#if !HAVE_STRLCPY
193194
CF_INLINE size_t
194195
strlcpy(char * dst, const char * src, size_t maxlen) {
195196
const size_t srclen = strlen(src);
@@ -201,7 +202,9 @@ strlcpy(char * dst, const char * src, size_t maxlen) {
201202
}
202203
return srclen;
203204
}
205+
#endif
204206

207+
#if !HAVE_STRLCAT
205208
CF_INLINE size_t
206209
strlcat(char * dst, const char * src, size_t maxlen) {
207210
const size_t srclen = strlen(src);
@@ -216,6 +219,7 @@ strlcat(char * dst, const char * src, size_t maxlen) {
216219
return dstlen + srclen;
217220
}
218221
#endif
222+
#endif // !TARGET_OS_MAC
219223

220224
#if TARGET_OS_WIN32
221225
// Compatibility with boolean.h
@@ -300,7 +304,7 @@ typedef unsigned long fd_mask;
300304
#endif
301305

302306

303-
#if !TARGET_OS_CYGWIN && !TARGET_OS_BSD
307+
#if !TARGET_OS_MAC && !HAVE_ISSETUGID
304308
#define issetugid() 0
305309
#endif
306310

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
#include <features.h>
7070
#include <termios.h>
7171

72+
#ifdef __GLIBC_PREREQ
7273
#if __GLIBC_PREREQ(2, 28) == 0
7374
// required for statx() system call, glibc >=2.28 wraps the kernel function
7475
#include <sys/syscall.h>
@@ -78,7 +79,7 @@
7879
#include <linux/fs.h>
7980
#define AT_STATX_SYNC_AS_STAT 0x0000 /* - Do whatever stat() does */
8081
#endif //__GLIBC_PREREQ(2. 28)
81-
82+
#endif // defined(__GLIBC_PREREQ)
8283
#ifndef __NR_statx
8384
#include <sys/stat.h>
8485
#endif // not __NR_statx
@@ -568,7 +569,7 @@ CF_CROSS_PLATFORM_EXPORT int _CFOpenFile(const char *path, int opts);
568569
static inline int _direntNameLength(struct dirent *entry) {
569570
#ifdef _D_EXACT_NAMLEN // defined on Linux
570571
return _D_EXACT_NAMLEN(entry);
571-
#elif TARGET_OS_ANDROID
572+
#elif TARGET_OS_LINUX || TARGET_OS_ANDROID
572573
return strlen(entry->d_name);
573574
#else
574575
return entry->d_namlen;

CoreFoundation/CMakeLists.txt

+18-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ project(CoreFoundation
66
VERSION 1338
77
LANGUAGES ASM C)
88

9+
include(CheckSymbolExists)
10+
include(CheckIncludeFile)
11+
912
set(CMAKE_C_STANDARD 99)
1013
set(CMAKE_C_STANDARD_REQUIRED YES)
1114

@@ -75,8 +78,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL Linux OR CMAKE_SYSTEM_NAME STREQUAL Android)
7578
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:_GNU_SOURCE>)
7679

7780
if(CMAKE_SYSTEM_NAME STREQUAL Linux)
78-
include(CheckSymbolExists)
79-
include(CheckIncludeFile)
8081
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
8182
check_include_file("sched.h" HAVE_SCHED_H)
8283
if(HAVE_SCHED_H)
@@ -95,6 +96,21 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL Windows)
9596
endif()
9697
endif()
9798

99+
# Look for some functions that may not be present on all platforms
100+
check_symbol_exists(strlcpy "string.h" HAVE_STRLCPY)
101+
check_symbol_exists(strlcat "string.h" HAVE_STRLCAT)
102+
check_symbol_exists(issetugid "unistd.h" HAVE_ISSETUGID)
103+
104+
if(HAVE_STRLCPY)
105+
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:HAVE_STRLCPY>)
106+
endif()
107+
if(HAVE_STRLCAT)
108+
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:HAVE_STRLCAT>)
109+
endif()
110+
if(HAVE_ISSETUGID)
111+
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:HAVE_ISSETUGID>)
112+
endif()
113+
98114
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:U_SHOW_DRAFT_API>)
99115
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:CF_BUILDING_CF>)
100116

Sources/BlocksRuntime/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ set_target_properties(BlocksRuntime PROPERTIES
1111
add_library(BlocksRuntime::BlocksRuntime ALIAS BlocksRuntime)
1212

1313
install(TARGETS BlocksRuntime
14-
ARCHIVE DESTINATION lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>
15-
LIBRARY DESTINATION lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>)
14+
ARCHIVE DESTINATION lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${SWIFT_SYSTEM_NAME}
15+
LIBRARY DESTINATION lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${SWIFT_SYSTEM_NAME})

Sources/Foundation/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ target_link_libraries(Foundation
171171
uuid
172172
PUBLIC
173173
swiftDispatch)
174+
if("${CMAKE_C_COMPILER_TARGET}" MATCHES ".*-musl[^-*]$")
175+
target_link_libraries(Foundation
176+
PUBLIC
177+
fts)
178+
endif()
174179
set_target_properties(Foundation PROPERTIES
175180
INSTALL_RPATH "$ORIGIN"
176181
BUILD_RPATH "$<TARGET_FILE_DIR:swiftDispatch>"

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/FoundationNetworking/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ if(NOT BUILD_SHARED_LIBS)
7272
PRIVATE
7373
"SHELL:-Xfrontend -public-autolink-library -Xfrontend curl")
7474

75+
if(BUILD_FULLY_STATIC)
76+
target_compile_options(FoundationNetworking
77+
PRIVATE
78+
"SHELL:-Xfrontend -public-autolink-library -Xfrontend crypto"
79+
"SHELL:-Xfrontend -public-autolink-library -Xfrontend ssl"
80+
"SHELL:-Xfrontend -public-autolink-library -Xfrontend z")
81+
endif()
82+
7583
# Merge private dependencies into single static objects archive
7684
set_property(TARGET FoundationNetworking PROPERTY STATIC_LIBRARY_OPTIONS
7785
$<TARGET_OBJECTS:CFURLSessionInterface>)

Sources/FoundationXML/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ if(NOT BUILD_SHARED_LIBS)
2323
PRIVATE
2424
"SHELL:-Xfrontend -public-autolink-library -Xfrontend xml2")
2525

26+
if(BUILD_FULLY_STATIC)
27+
target_compile_options(FoundationXML
28+
PRIVATE
29+
"SHELL:-Xfrontend -public-autolink-library -Xfrontend z")
30+
endif()
31+
2632
# Merge private dependencies into single static objects archive
2733
set_property(TARGET FoundationXML PROPERTY STATIC_LIBRARY_OPTIONS
2834
$<TARGET_OBJECTS:CFXMLInterface>)

Sources/Tools/plutil/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ if(NOT CMAKE_SYSTEM_NAME MATCHES "Darwin|Windows")
2828
endif()
2929

3030
set_target_properties(plutil PROPERTIES
31-
INSTALL_RPATH "$ORIGIN/../lib/swift/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
31+
INSTALL_RPATH "$ORIGIN/../lib/swift/${SWIFT_SYSTEM_NAME}")
3232

3333

3434
set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS plutil)

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

Sources/UUID/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ if(NOT BUILD_SHARED_LIBS)
2828
# TODO(drexin): should be installed in architecture specific folder, once
2929
# the layout is fixed for non-Darwin platforms
3030
install(TARGETS uuid
31-
ARCHIVE DESTINATION lib/swift_static/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>
32-
LIBRARY DESTINATION lib/swift_static/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>
31+
ARCHIVE DESTINATION lib/swift_static/${SWIFT_SYSTEM_NAME}
32+
LIBRARY DESTINATION lib/swift_static/${SWIFT_SYSTEM_NAME}
3333
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
3434
endif()

0 commit comments

Comments
 (0)