Skip to content

Commit fafdf90

Browse files
kateinoigakukunMaxDesiatov
authored andcommitted
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` (cherry picked from commit c86692f)
1 parent 2ca6906 commit fafdf90

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: "FoundationPreview",
7580
platforms: [.macOS("13.3"), .iOS("16.4"), .tvOS("16.4"), .watchOS("9.4")],
@@ -91,15 +96,23 @@ let package = Package(
9196
path: "Sources/Foundation"),
9297

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

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

104117
// FoundationEssentials
105118
.target(
@@ -130,11 +143,14 @@ let package = Package(
130143
],
131144
cSettings: [
132145
.define("_GNU_SOURCE", .when(platforms: [.linux]))
133-
],
146+
] + wasiLibcCSettings,
134147
swiftSettings: [
135148
.enableExperimentalFeature("VariadicGenerics"),
136149
.enableExperimentalFeature("AccessLevelOnImport")
137-
] + availabilityMacros + concurrencyChecking
150+
] + availabilityMacros + concurrencyChecking,
151+
linkerSettings: [
152+
.linkedLibrary("wasi-emulated-getpid", .when(platforms: [.wasi])),
153+
]
138154
),
139155
.testTarget(
140156
name: "FoundationEssentialsTests",
@@ -166,6 +182,7 @@ let package = Package(
166182
"CMakeLists.txt",
167183
"Predicate/CMakeLists.txt"
168184
],
185+
cSettings: wasiLibcCSettings,
169186
swiftSettings: [
170187
.enableExperimentalFeature("AccessLevelOnImport")
171188
] + 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)