From 90799cfdf73e69147baab6f92f934c3a74235c5e Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 10 Aug 2024 13:29:48 +0000 Subject: [PATCH 1/2] Revert "[wasm] Fix ambiguous `errno` error when importing WASILibc module" This reverts commit 164ec0adaa80e7a29d2a10fb31ee72baddeda6b2. --- stdlib/public/Platform/Platform.swift | 6 ------ 1 file changed, 6 deletions(-) diff --git a/stdlib/public/Platform/Platform.swift b/stdlib/public/Platform/Platform.swift index ca7417c53574b..0c2d63a0cffdb 100644 --- a/stdlib/public/Platform/Platform.swift +++ b/stdlib/public/Platform/Platform.swift @@ -84,11 +84,6 @@ func _convertDarwinBooleanToBool(_ x: DarwinBoolean) -> Bool { #endif -// wasi-libc defines `errno` in a way ClangImporter can understand, so we don't -// need to define shims for it. On the contrary, if we define the shim, we will -// get an ambiguity error when importing WASILibc module and SwiftWASILibc Clang -// module (or a Clang module that re-exports SwiftWASILibc). -#if !os(WASI) //===----------------------------------------------------------------------===// // sys/errno.h //===----------------------------------------------------------------------===// @@ -101,7 +96,6 @@ public var errno : Int32 { return _swift_stdlib_setErrno(val) } } -#endif //===----------------------------------------------------------------------===// From be08ebb12a99636832f055e94956cb500af1a86d Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 10 Aug 2024 13:50:57 +0000 Subject: [PATCH 2/2] [wasm] Annotate errno as SwiftPrivate by apinotes This patch adds an apinotes file for SwiftWASILibc clang module to mark `errno` macro hidden from Swift code. This resolves ambiguity between the C macro definition and the Swift wrapper in WASILibc overlay module. This change installs the apinotes file to the resource directories for both lib/swift/apinotes and lib/swift_static/apinotes. --- stdlib/public/Platform/CMakeLists.txt | 28 +++++++++++++++++++ stdlib/public/Platform/SwiftWASILibc.apinotes | 5 ++++ test/stdlib/WASILibcAPI.swift | 19 +++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 stdlib/public/Platform/SwiftWASILibc.apinotes create mode 100644 test/stdlib/WASILibcAPI.swift diff --git a/stdlib/public/Platform/CMakeLists.txt b/stdlib/public/Platform/CMakeLists.txt index 928b5d9cb6302..7a7cab3da6d59 100644 --- a/stdlib/public/Platform/CMakeLists.txt +++ b/stdlib/public/Platform/CMakeLists.txt @@ -527,6 +527,34 @@ if("WASI" IN_LIST SWIFT_SDKS) DESTINATION "lib/swift_static/${arch_subdir}" COMPONENT sdk-overlay) endif() + + set(wasilibc_apinotes_source "SwiftWASILibc.apinotes") + add_custom_command_target( + copy_wasilibc_apinotes_resource + COMMAND + "${CMAKE_COMMAND}" "-E" "make_directory" ${SWIFTLIB_DIR}/apinotes ${SWIFTSTATICLIB_DIR}/apinotes + COMMAND + "${CMAKE_COMMAND}" "-E" "copy_if_different" + "${CMAKE_CURRENT_SOURCE_DIR}/${wasilibc_apinotes_source}" ${SWIFTLIB_DIR}/apinotes + COMMAND + "${CMAKE_COMMAND}" "-E" "copy_if_different" + "${CMAKE_CURRENT_SOURCE_DIR}/${wasilibc_apinotes_source}" ${SWIFTSTATICLIB_DIR}/apinotes + OUTPUT + ${SWIFTLIB_DIR}/apinotes/${wasilibc_apinotes_source} + ${SWIFTSTATICLIB_DIR}/apinotes/${wasilibc_apinotes_source} + COMMENT "Copying WASILibc API notes to resource directories") + + list(APPEND wasilibc_modulemap_target_list ${copy_wasilibc_apinotes_resource}) + add_dependencies(sdk-overlay ${copy_wasilibc_apinotes_resource}) + swift_install_in_component(FILES "${wasilibc_apinotes_source}" + DESTINATION "lib/swift/apinotes" + COMPONENT sdk-overlay) + if(SWIFT_BUILD_STATIC_STDLIB) + swift_install_in_component(FILES "${wasilibc_apinotes_source}" + DESTINATION "lib/swift_static/apinotes" + COMPONENT sdk-overlay) + endif() + endforeach() endif() add_custom_target(wasilibc_modulemap DEPENDS ${wasilibc_modulemap_target_list}) diff --git a/stdlib/public/Platform/SwiftWASILibc.apinotes b/stdlib/public/Platform/SwiftWASILibc.apinotes new file mode 100644 index 0000000000000..001acc7ebb596 --- /dev/null +++ b/stdlib/public/Platform/SwiftWASILibc.apinotes @@ -0,0 +1,5 @@ +Name: SwiftWASILibc +Globals: + # errno macro is importable but we provide explicit Swift wrapper + - Name: errno + SwiftPrivate: true diff --git a/test/stdlib/WASILibcAPI.swift b/test/stdlib/WASILibcAPI.swift new file mode 100644 index 0000000000000..fe599bb9f3878 --- /dev/null +++ b/test/stdlib/WASILibcAPI.swift @@ -0,0 +1,19 @@ +// RUN: %target-swift-frontend -typecheck -swift-version 6 %s -verify +// REQUIRES: executable_test +// REQUIRES: OS=wasi + +import WASILibc + +// errno is a global thread-local variable, so it should be accessible +// from any context. + +enum TestErrno { + static func testSyncContext() { + _ = errno + errno = 0 + } + static func testAsyncContext() async { + _ = errno + errno = 0 + } +}