Skip to content

Commit c86692f

Browse files
Enable wasi-libc emulation features (#777)
* 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. * Prefer `TARGET_OS_WASI` over `__wasi__` And explain why we need definition checks for `signal.h` and `sys/mman.h`
1 parent 7a98f9c commit c86692f

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
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

Lines changed: 26 additions & 9 deletions
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: "swift-foundation",
7580
platforms: [.macOS("13.3"), .iOS("16.4"), .tvOS("16.4"), .watchOS("9.4")],
@@ -80,15 +85,23 @@ let package = Package(
8085
dependencies: dependencies,
8186
targets: [
8287
// _FoundationCShims (Internal)
83-
.target(name: "_FoundationCShims",
84-
cSettings: [.define("_CRT_SECURE_NO_WARNINGS",
85-
.when(platforms: [.windows]))]),
88+
.target(
89+
name: "_FoundationCShims",
90+
cSettings: [
91+
.define("_CRT_SECURE_NO_WARNINGS", .when(platforms: [.windows]))
92+
] + wasiLibcCSettings
93+
),
8694

8795
// TestSupport (Internal)
88-
.target(name: "TestSupport", dependencies: [
89-
"FoundationEssentials",
90-
"FoundationInternationalization",
91-
], swiftSettings: availabilityMacros + concurrencyChecking),
96+
.target(
97+
name: "TestSupport",
98+
dependencies: [
99+
"FoundationEssentials",
100+
"FoundationInternationalization",
101+
],
102+
cSettings: wasiLibcCSettings,
103+
swiftSettings: availabilityMacros + concurrencyChecking
104+
),
92105

93106
// FoundationEssentials
94107
.target(
@@ -119,11 +132,14 @@ let package = Package(
119132
],
120133
cSettings: [
121134
.define("_GNU_SOURCE", .when(platforms: [.linux]))
122-
],
135+
] + wasiLibcCSettings,
123136
swiftSettings: [
124137
.enableExperimentalFeature("VariadicGenerics"),
125138
.enableExperimentalFeature("AccessLevelOnImport")
126-
] + availabilityMacros + concurrencyChecking
139+
] + availabilityMacros + concurrencyChecking,
140+
linkerSettings: [
141+
.linkedLibrary("wasi-emulated-getpid", .when(platforms: [.wasi])),
142+
]
127143
),
128144
.testTarget(
129145
name: "FoundationEssentialsTests",
@@ -155,6 +171,7 @@ let package = Package(
155171
"CMakeLists.txt",
156172
"Predicate/CMakeLists.txt"
157173
],
174+
cSettings: wasiLibcCSettings,
158175
swiftSettings: [
159176
.enableExperimentalFeature("AccessLevelOnImport")
160177
] + availabilityMacros + concurrencyChecking

Sources/FoundationEssentials/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,21 @@
6060
#endif
6161

6262
#if __has_include(<signal.h>)
63-
#include <signal.h>
63+
/// Guard against including `signal.h` on WASI. The `signal.h` header file
64+
/// itself is available in wasi-libc, but it's just a stub that doesn't actually
65+
/// do anything. And also including it requires a special macro definition
66+
/// (`_WASI_EMULATED_SIGNAL`) and it causes compilation errors without the macro.
67+
# if !TARGET_OS_WASI || defined(_WASI_EMULATED_SIGNAL)
68+
# include <signal.h>
69+
# endif
70+
#endif
71+
72+
#if __has_include(<sys/mman.h>)
73+
/// Similar to `signal.h`, guard against including `sys/mman.h` on WASI unless
74+
/// `_WASI_EMULATED_MMAN` is enabled.
75+
# if !TARGET_OS_WASI || defined(_WASI_EMULATED_MMAN)
76+
# include <sys/mman.h>
77+
# endif
6478
#endif
6579

6680
#if __has_include(<stdalign.h>)

0 commit comments

Comments
 (0)