diff --git a/CMakeLists.txt b/CMakeLists.txt index b99fcfa8f..2afb0787e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -104,6 +104,14 @@ foreach(version ${_SwiftFoundation_versions}) endforeach() endforeach() +# wasi-libc emulation feature flags +set(_SwiftFoundation_wasi_libc_flags) +if(CMAKE_SYSTEM_NAME STREQUAL "WASI") + list(APPEND _SwiftFoundation_wasi_libc_flags + "SHELL:$<$:-Xcc -D_WASI_EMULATED_SIGNAL>" + "SHELL:$<$:-Xcc -D_WASI_EMULATED_MMAN>") +endif() + include(GNUInstallDirs) include(SwiftFoundationSwiftSupport) diff --git a/Package.swift b/Package.swift index 8a9807452..b2ab248eb 100644 --- a/Package.swift +++ b/Package.swift @@ -70,6 +70,11 @@ var dependencies: [Package.Dependency] { } } +let wasiLibcCSettings: [CSetting] = [ + .define("_WASI_EMULATED_SIGNAL", .when(platforms: [.wasi])), + .define("_WASI_EMULATED_MMAN", .when(platforms: [.wasi])), +] + let package = Package( name: "swift-foundation", platforms: [.macOS("13.3"), .iOS("16.4"), .tvOS("16.4"), .watchOS("9.4")], @@ -80,15 +85,23 @@ let package = Package( dependencies: dependencies, targets: [ // _FoundationCShims (Internal) - .target(name: "_FoundationCShims", - cSettings: [.define("_CRT_SECURE_NO_WARNINGS", - .when(platforms: [.windows]))]), + .target( + name: "_FoundationCShims", + cSettings: [ + .define("_CRT_SECURE_NO_WARNINGS", .when(platforms: [.windows])) + ] + wasiLibcCSettings + ), // TestSupport (Internal) - .target(name: "TestSupport", dependencies: [ - "FoundationEssentials", - "FoundationInternationalization", - ], swiftSettings: availabilityMacros + concurrencyChecking), + .target( + name: "TestSupport", + dependencies: [ + "FoundationEssentials", + "FoundationInternationalization", + ], + cSettings: wasiLibcCSettings, + swiftSettings: availabilityMacros + concurrencyChecking + ), // FoundationEssentials .target( @@ -119,11 +132,14 @@ let package = Package( ], cSettings: [ .define("_GNU_SOURCE", .when(platforms: [.linux])) - ], + ] + wasiLibcCSettings, swiftSettings: [ .enableExperimentalFeature("VariadicGenerics"), .enableExperimentalFeature("AccessLevelOnImport") - ] + availabilityMacros + concurrencyChecking + ] + availabilityMacros + concurrencyChecking, + linkerSettings: [ + .linkedLibrary("wasi-emulated-getpid", .when(platforms: [.wasi])), + ] ), .testTarget( name: "FoundationEssentialsTests", @@ -155,6 +171,7 @@ let package = Package( "CMakeLists.txt", "Predicate/CMakeLists.txt" ], + cSettings: wasiLibcCSettings, swiftSettings: [ .enableExperimentalFeature("AccessLevelOnImport") ] + availabilityMacros + concurrencyChecking diff --git a/Sources/FoundationEssentials/CMakeLists.txt b/Sources/FoundationEssentials/CMakeLists.txt index d9ebc2ebb..7cddff682 100644 --- a/Sources/FoundationEssentials/CMakeLists.txt +++ b/Sources/FoundationEssentials/CMakeLists.txt @@ -66,6 +66,7 @@ target_compile_options(FoundationEssentials PRIVATE "SHELL:$<$:-Xfrontend -enable-experimental-feature -Xfrontend StrictConcurrency>" "SHELL:$<$:-Xfrontend -enable-upcoming-feature -Xfrontend InferSendableFromCaptures>") target_compile_options(FoundationEssentials PRIVATE ${_SwiftFoundation_availability_macros}) +target_compile_options(FoundationEssentials PRIVATE ${_SwiftFoundation_wasi_libc_flags}) target_compile_options(FoundationEssentials PRIVATE -package-name "SwiftFoundation") target_link_libraries(FoundationEssentials PUBLIC diff --git a/Sources/FoundationInternationalization/CMakeLists.txt b/Sources/FoundationInternationalization/CMakeLists.txt index 5a89ceb88..857db9cac 100644 --- a/Sources/FoundationInternationalization/CMakeLists.txt +++ b/Sources/FoundationInternationalization/CMakeLists.txt @@ -33,6 +33,7 @@ target_compile_options(FoundationInternationalization PRIVATE "SHELL:$<$:-Xfrontend -enable-experimental-feature -Xfrontend StrictConcurrency>" "SHELL:$<$:-Xfrontend -enable-upcoming-feature -Xfrontend InferSendableFromCaptures>") target_compile_options(FoundationInternationalization PRIVATE ${_SwiftFoundation_availability_macros}) +target_compile_options(FoundationInternationalization PRIVATE ${_SwiftFoundation_wasi_libc_flags}) target_compile_options(FoundationInternationalization PRIVATE -package-name "SwiftFoundation") target_link_libraries(FoundationInternationalization PUBLIC diff --git a/Sources/_FoundationCShims/include/_CStdlib.h b/Sources/_FoundationCShims/include/_CStdlib.h index 8967eb7f4..03373934b 100644 --- a/Sources/_FoundationCShims/include/_CStdlib.h +++ b/Sources/_FoundationCShims/include/_CStdlib.h @@ -60,7 +60,21 @@ #endif #if __has_include() -#include +/// Guard against including `signal.h` on WASI. The `signal.h` header file +/// itself is available in wasi-libc, but it's just a stub that doesn't actually +/// do anything. And also including it requires a special macro definition +/// (`_WASI_EMULATED_SIGNAL`) and it causes compilation errors without the macro. +# if !TARGET_OS_WASI || defined(_WASI_EMULATED_SIGNAL) +# include +# endif +#endif + +#if __has_include() +/// Similar to `signal.h`, guard against including `sys/mman.h` on WASI unless +/// `_WASI_EMULATED_MMAN` is enabled. +# if !TARGET_OS_WASI || defined(_WASI_EMULATED_MMAN) +# include +# endif #endif #if __has_include()