From 9537a2bb2249760c959bed49b1268fb697235c36 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 10 Aug 2024 13:50:57 +0000 Subject: [PATCH] [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 7e709923ffcd0..3d798e4830403 100644 --- a/stdlib/public/Platform/CMakeLists.txt +++ b/stdlib/public/Platform/CMakeLists.txt @@ -508,6 +508,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 + } +}