Skip to content

Commit 7f38264

Browse files
build: Repair the build on WASI platform (swiftlang#5052)
* build: Repair the build on WASI platform Cherry picked from swiftlang#4934 * Add `.windows` to `platformsWithThreads` in `Package.swift` * Reflect `Package.swift` WASI changes in `CMakeLists.txt` (cherry picked from commit 8873088) --------- Co-authored-by: Yuta Saito <[email protected]>
1 parent b3fa847 commit 7f38264

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

CMakeLists.txt

+24-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ list(APPEND _Foundation_common_build_flags
115115
"-DCF_BUILDING_CF"
116116
"-DDEPLOYMENT_ENABLE_LIBDISPATCH"
117117
"-DHAVE_STRUCT_TIMESPEC"
118-
"-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS"
119118
"-Wno-shorten-64-to-32"
120119
"-Wno-deprecated-declarations"
121120
"-Wno-unreachable-code"
@@ -127,6 +126,18 @@ list(APPEND _Foundation_common_build_flags
127126
"-Wno-switch"
128127
"-fblocks")
129128

129+
if(CMAKE_SYSTEM_NAME STREQUAL "WASI")
130+
list(APPEND _Foundation_common_build_flags
131+
"-D_WASI_EMULATED_SIGNAL"
132+
"-DHAVE_STRLCPY"
133+
"-DHAVE_STRLCAT"
134+
)
135+
else()
136+
list(APPEND _Foundation_common_build_flags
137+
"-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS"
138+
)
139+
endif()
140+
130141
if(NOT "${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
131142
list(APPEND _Foundation_common_build_flags
132143
"-fconstant-cfstrings"
@@ -154,10 +165,21 @@ set(_Foundation_swift_build_flags)
154165
list(APPEND _Foundation_swift_build_flags
155166
"-swift-version 6"
156167
"-DDEPLOYMENT_RUNTIME_SWIFT"
157-
"-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS"
158168
"-Xfrontend"
159169
"-require-explicit-sendable")
160170

171+
if(CMAKE_SYSTEM_NAME STREQUAL "WASI")
172+
list(APPEND _Foundation_swift_build_flags
173+
"-D_WASI_EMULATED_SIGNAL"
174+
"-DHAVE_STRLCPY"
175+
"-DHAVE_STRLCAT"
176+
)
177+
else()
178+
list(APPEND _Foundation_swift_build_flags
179+
"-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS"
180+
)
181+
endif()
182+
161183
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android")
162184
list(APPEND _Foundation_common_build_flags
163185
"-D_GNU_SOURCE")

Package.swift

+19-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33

44
import PackageDescription
55

6+
let platformsWithThreads: [Platform] = [
7+
.iOS,
8+
.macOS,
9+
.tvOS,
10+
.watchOS,
11+
.macCatalyst,
12+
.driverKit,
13+
.android,
14+
.linux,
15+
.windows,
16+
]
617
var dispatchIncludeFlags: [CSetting]
718
if let environmentPath = Context.environment["DISPATCH_INCLUDE_PATH"] {
819
dispatchIncludeFlags = [.unsafeFlags([
@@ -31,8 +42,11 @@ let coreFoundationBuildSettings: [CSetting] = [
3142
.define("DEPLOYMENT_ENABLE_LIBDISPATCH"),
3243
.define("DEPLOYMENT_RUNTIME_SWIFT"),
3344
.define("HAVE_STRUCT_TIMESPEC"),
34-
.define("SWIFT_CORELIBS_FOUNDATION_HAS_THREADS"),
45+
.define("SWIFT_CORELIBS_FOUNDATION_HAS_THREADS", .when(platforms: platformsWithThreads)),
3546
.define("_GNU_SOURCE", .when(platforms: [.linux, .android])),
47+
.define("_WASI_EMULATED_SIGNAL", .when(platforms: [.wasi])),
48+
.define("HAVE_STRLCPY", .when(platforms: [.wasi])),
49+
.define("HAVE_STRLCAT", .when(platforms: [.wasi])),
3650
.unsafeFlags([
3751
"-Wno-shorten-64-to-32",
3852
"-Wno-deprecated-declarations",
@@ -61,8 +75,11 @@ let interfaceBuildSettings: [CSetting] = [
6175
.define("CF_BUILDING_CF"),
6276
.define("DEPLOYMENT_ENABLE_LIBDISPATCH"),
6377
.define("HAVE_STRUCT_TIMESPEC"),
64-
.define("SWIFT_CORELIBS_FOUNDATION_HAS_THREADS"),
78+
.define("SWIFT_CORELIBS_FOUNDATION_HAS_THREADS", .when(platforms: platformsWithThreads)),
6579
.define("_GNU_SOURCE", .when(platforms: [.linux, .android])),
80+
.define("_WASI_EMULATED_SIGNAL", .when(platforms: [.wasi])),
81+
.define("HAVE_STRLCPY", .when(platforms: [.wasi])),
82+
.define("HAVE_STRLCAT", .when(platforms: [.wasi])),
6683
.unsafeFlags([
6784
"-Wno-shorten-64-to-32",
6885
"-Wno-deprecated-declarations",

Sources/CoreFoundation/CFBundle.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,13 @@ static CFBundleRef _CFBundleGetBundleWithIdentifier(CFStringRef bundleID, void *
596596

597597
CFBundleRef CFBundleGetBundleWithIdentifier(CFStringRef bundleID) {
598598
// Use the frame that called this as a hint
599-
return _CFBundleGetBundleWithIdentifier(bundleID, __builtin_return_address(0));
599+
void *hint;
600+
#if TARGET_OS_WASI
601+
hint = NULL;
602+
#else
603+
hint = __builtin_frame_address(0);
604+
#endif
605+
return _CFBundleGetBundleWithIdentifier(bundleID, hint);
600606
}
601607

602608
CFBundleRef _CFBundleGetBundleWithIdentifierWithHint(CFStringRef bundleID, void *pointer) {

Sources/CoreFoundation/CFString.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include "CFRuntime_Internal.h"
2929
#include <assert.h>
3030
#include <_foundation_unicode/uchar.h>
31-
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
31+
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI
3232
#include "CFConstantKeys.h"
3333
#include "CFStringLocalizedFormattingInternal.h"
3434
#endif

0 commit comments

Comments
 (0)