Skip to content

Commit b10d2ee

Browse files
Enable wasi-libc emulation features
Those features require explicit macro definitions to be enabled, so add them to the package definition. Only affects WASI builds.
1 parent 5463e8e commit b10d2ee

File tree

5 files changed

+49
-11
lines changed

5 files changed

+49
-11
lines changed

CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ foreach(version ${_SwiftFoundation_versions})
104104
endforeach()
105105
endforeach()
106106

107+
# wasi-libc emulation feature flags
108+
set(_SwiftFoundation_wasi_libc_flags)
109+
if(CMAKE_SYSTEM_NAME STREQUAL "WASI")
110+
list(APPEND _SwiftFoundation_wasi_libc_flags
111+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xcc -D_WASI_EMULATED_SIGNAL>"
112+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xcc -D_WASI_EMULATED_MMAN>")
113+
endif()
114+
107115
include(GNUInstallDirs)
108116
include(SwiftFoundationSwiftSupport)
109117

Package.swift

+28-10
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ var dependencies: [Package.Dependency] {
7070
}
7171
}
7272

73+
let wasiLibcCSettings: [CSetting] = [
74+
.define("_WASI_EMULATED_SIGNAL", .when(platforms: [.wasi])),
75+
.define("_WASI_EMULATED_MMAN", .when(platforms: [.wasi])),
76+
]
77+
7378
let package = Package(
7479
name: "FoundationPreview",
7580
platforms: [.macOS("13.3"), .iOS("16.4"), .tvOS("16.4"), .watchOS("9.4")],
@@ -88,18 +93,27 @@ let package = Package(
8893
"FoundationEssentials",
8994
"FoundationInternationalization",
9095
],
91-
path: "Sources/Foundation"),
96+
path: "Sources/Foundation",
97+
cSettings: wasiLibcCSettings),
9298

9399
// _FoundationCShims (Internal)
94-
.target(name: "_FoundationCShims",
95-
cSettings: [.define("_CRT_SECURE_NO_WARNINGS",
96-
.when(platforms: [.windows]))]),
100+
.target(
101+
name: "_FoundationCShims",
102+
cSettings: [
103+
.define("_CRT_SECURE_NO_WARNINGS", .when(platforms: [.windows]))
104+
] + wasiLibcCSettings
105+
),
97106

98107
// TestSupport (Internal)
99-
.target(name: "TestSupport", dependencies: [
100-
"FoundationEssentials",
101-
"FoundationInternationalization",
102-
], swiftSettings: availabilityMacros + concurrencyChecking),
108+
.target(
109+
name: "TestSupport",
110+
dependencies: [
111+
"FoundationEssentials",
112+
"FoundationInternationalization",
113+
],
114+
cSettings: wasiLibcCSettings,
115+
swiftSettings: availabilityMacros + concurrencyChecking
116+
),
103117

104118
// FoundationEssentials
105119
.target(
@@ -130,11 +144,14 @@ let package = Package(
130144
],
131145
cSettings: [
132146
.define("_GNU_SOURCE", .when(platforms: [.linux]))
133-
],
147+
] + wasiLibcCSettings,
134148
swiftSettings: [
135149
.enableExperimentalFeature("VariadicGenerics"),
136150
.enableExperimentalFeature("AccessLevelOnImport")
137-
] + availabilityMacros + concurrencyChecking
151+
] + availabilityMacros + concurrencyChecking,
152+
linkerSettings: [
153+
.linkedLibrary("wasi-emulated-getpid", .when(platforms: [.wasi])),
154+
]
138155
),
139156
.testTarget(
140157
name: "FoundationEssentialsTests",
@@ -166,6 +183,7 @@ let package = Package(
166183
"CMakeLists.txt",
167184
"Predicate/CMakeLists.txt"
168185
],
186+
cSettings: wasiLibcCSettings,
169187
swiftSettings: [
170188
.enableExperimentalFeature("AccessLevelOnImport")
171189
] + availabilityMacros + concurrencyChecking

Sources/FoundationEssentials/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ target_compile_options(FoundationEssentials PRIVATE
6666
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend StrictConcurrency>"
6767
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-upcoming-feature -Xfrontend InferSendableFromCaptures>")
6868
target_compile_options(FoundationEssentials PRIVATE ${_SwiftFoundation_availability_macros})
69+
target_compile_options(FoundationEssentials PRIVATE ${_SwiftFoundation_wasi_libc_flags})
6970
target_compile_options(FoundationEssentials PRIVATE -package-name "SwiftFoundation")
7071

7172
target_link_libraries(FoundationEssentials PUBLIC

Sources/FoundationInternationalization/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ target_compile_options(FoundationInternationalization PRIVATE
3333
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend StrictConcurrency>"
3434
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-upcoming-feature -Xfrontend InferSendableFromCaptures>")
3535
target_compile_options(FoundationInternationalization PRIVATE ${_SwiftFoundation_availability_macros})
36+
target_compile_options(FoundationInternationalization PRIVATE ${_SwiftFoundation_wasi_libc_flags})
3637
target_compile_options(FoundationInternationalization PRIVATE -package-name "SwiftFoundation")
3738

3839
target_link_libraries(FoundationInternationalization PUBLIC

Sources/_FoundationCShims/include/_CStdlib.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,17 @@
6060
#endif
6161

6262
#if __has_include(<signal.h>)
63-
#include <signal.h>
63+
/* wasi-libc's signal.h is available only if _WASI_EMULATED_SIGNAL is defined */
64+
# if !defined(__wasi__) || defined(_WASI_EMULATED_SIGNAL)
65+
# include <signal.h>
66+
# endif
67+
#endif
68+
69+
#if __has_include(<sys/mman.h>)
70+
/* wasi-libc's mman.h is available only if _WASI_EMULATED_MMAN is defined */
71+
# if !defined(__wasi__) || defined(_WASI_EMULATED_MMAN)
72+
# include <sys/mman.h>
73+
# endif
6474
#endif
6575

6676
#if __has_include(<stdalign.h>)

0 commit comments

Comments
 (0)