Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable wasi-libc emulation features #777

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:$<$<COMPILE_LANGUAGE:Swift>:-Xcc -D_WASI_EMULATED_SIGNAL>"
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xcc -D_WASI_EMULATED_MMAN>")
endif()

include(GNUInstallDirs)
include(SwiftFoundationSwiftSupport)

Expand Down
35 changes: 26 additions & 9 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")],
Expand All @@ -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(
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -155,6 +171,7 @@ let package = Package(
"CMakeLists.txt",
"Predicate/CMakeLists.txt"
],
cSettings: wasiLibcCSettings,
swiftSettings: [
.enableExperimentalFeature("AccessLevelOnImport")
] + availabilityMacros + concurrencyChecking
Expand Down
1 change: 1 addition & 0 deletions Sources/FoundationEssentials/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ target_compile_options(FoundationEssentials PRIVATE
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend StrictConcurrency>"
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-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
Expand Down
1 change: 1 addition & 0 deletions Sources/FoundationInternationalization/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ target_compile_options(FoundationInternationalization PRIVATE
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend StrictConcurrency>"
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-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
Expand Down
16 changes: 15 additions & 1 deletion Sources/_FoundationCShims/include/_CStdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,21 @@
#endif

#if __has_include(<signal.h>)
#include <signal.h>
/// 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 <signal.h>
# endif
#endif

#if __has_include(<sys/mman.h>)
/// 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 <sys/mman.h>
# endif
#endif

#if __has_include(<stdalign.h>)
Expand Down