Skip to content

Commit f125402

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 c0a485e commit f125402

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
@@ -84,6 +84,14 @@ list(APPEND _SwiftFoundation_availability_macros
8484
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPredicateRegex 0.3:macOS 10000, iOS 10000, tvOS 10000, watchOS 10000\">"
8585
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPredicateRegex 0.4:macOS 10000, iOS 10000, tvOS 10000, watchOS 10000\">")
8686

87+
# wasi-libc emulation feature flags
88+
set(_SwiftFoundation_wasi_libc_flags)
89+
if(CMAKE_SYSTEM_NAME STREQUAL "WASI")
90+
list(APPEND _SwiftFoundation_wasi_libc_flags
91+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xcc -D_WASI_EMULATED_SIGNAL>"
92+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xcc -D_WASI_EMULATED_MMAN>")
93+
endif()
94+
8795
include(GNUInstallDirs)
8896
include(SwiftFoundationSwiftSupport)
8997

Package.swift

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

72+
let wasiLibcCSettings: [CSetting] = [
73+
.define("_WASI_EMULATED_SIGNAL", .when(platforms: [.wasi])),
74+
.define("_WASI_EMULATED_MMAN", .when(platforms: [.wasi])),
75+
]
76+
7277
let package = Package(
7378
name: "FoundationPreview",
7479
platforms: [.macOS("13.3"), .iOS("16.4"), .tvOS("16.4"), .watchOS("9.4")],
@@ -87,18 +92,27 @@ let package = Package(
8792
"FoundationEssentials",
8893
"FoundationInternationalization",
8994
],
90-
path: "Sources/Foundation"),
95+
path: "Sources/Foundation",
96+
cSettings: wasiLibcCSettings),
9197

9298
// _FoundationCShims (Internal)
93-
.target(name: "_FoundationCShims",
94-
cSettings: [.define("_CRT_SECURE_NO_WARNINGS",
95-
.when(platforms: [.windows]))]),
99+
.target(
100+
name: "_FoundationCShims",
101+
cSettings: [
102+
.define("_CRT_SECURE_NO_WARNINGS", .when(platforms: [.windows]))
103+
] + wasiLibcCSettings
104+
),
96105

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

103117
// FoundationEssentials
104118
.target(
@@ -129,11 +143,14 @@ let package = Package(
129143
],
130144
cSettings: [
131145
.define("_GNU_SOURCE", .when(platforms: [.linux]))
132-
],
146+
] + wasiLibcCSettings,
133147
swiftSettings: [
134148
.enableExperimentalFeature("VariadicGenerics"),
135149
.enableExperimentalFeature("AccessLevelOnImport")
136-
] + availabilityMacros + concurrencyChecking
150+
] + availabilityMacros + concurrencyChecking,
151+
linkerSettings: [
152+
.linkedLibrary("wasi-emulated-getpid", .when(platforms: [.wasi])),
153+
]
137154
),
138155
.testTarget(
139156
name: "FoundationEssentialsTests",
@@ -165,6 +182,7 @@ let package = Package(
165182
"CMakeLists.txt",
166183
"Predicate/CMakeLists.txt"
167184
],
185+
cSettings: wasiLibcCSettings,
168186
swiftSettings: [
169187
.enableExperimentalFeature("AccessLevelOnImport")
170188
] + 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
@@ -32,6 +32,7 @@ target_compile_options(FoundationInternationalization PRIVATE
3232
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend StrictConcurrency>"
3333
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-upcoming-feature -Xfrontend InferSendableFromCaptures>")
3434
target_compile_options(FoundationInternationalization PRIVATE ${_SwiftFoundation_availability_macros})
35+
target_compile_options(FoundationInternationalization PRIVATE ${_SwiftFoundation_wasi_libc_flags})
3536
target_compile_options(FoundationInternationalization PRIVATE -package-name "SwiftFoundation")
3637

3738
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)