Skip to content

Commit

Permalink
[wasm] Annotate errno as SwiftPrivate by apinotes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kateinoigakukun authored and Anka committed Oct 30, 2024
1 parent 432b070 commit 9537a2b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
28 changes: 28 additions & 0 deletions stdlib/public/Platform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
5 changes: 5 additions & 0 deletions stdlib/public/Platform/SwiftWASILibc.apinotes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Name: SwiftWASILibc
Globals:
# errno macro is importable but we provide explicit Swift wrapper
- Name: errno
SwiftPrivate: true
19 changes: 19 additions & 0 deletions test/stdlib/WASILibcAPI.swift
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 9537a2b

Please sign in to comment.