Skip to content

[6.2] Embedded Wasm: enable import WASILibc #83846

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

Merged
merged 4 commits into from
Aug 22, 2025
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
57 changes: 57 additions & 0 deletions stdlib/public/Platform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ if("WASI" IN_LIST SWIFT_SDKS)
set(arch_subdir "${SWIFT_SDK_WASI_LIB_SUBDIR}/${arch}")
set(module_dir "${SWIFTLIB_DIR}/${arch_subdir}")
set(module_dir_static "${SWIFTSTATICLIB_DIR}/${arch_subdir}")
set(module_dir_embedded "${SWIFTLIB_DIR}/embedded/${arch}")

add_custom_command_target(
copy_wasilibc_modulemap_resource
Expand All @@ -529,6 +530,24 @@ if("WASI" IN_LIST SWIFT_SDKS)
COMPONENT sdk-overlay)
endif()

if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB)
add_custom_command_target(
copy_wasilibc_modulemap_embedded_resource
COMMAND
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir} ${module_dir_embedded}
COMMAND
"${CMAKE_COMMAND}" "-E" "copy_if_different"
"${CMAKE_CURRENT_SOURCE_DIR}/${wasilibc_modulemap_source}" ${module_dir_embedded}
OUTPUT "${module_dir_embedded}/${wasilibc_modulemap_source}"
COMMENT "Copying WASILibc modulemap to resource directories")
add_dependencies(sdk-overlay ${copy_wasilibc_modulemap_embedded_resource})
list(APPEND wasilibc_modulemap_target_list ${copy_wasilibc_modulemap_embedded_resource})

swift_install_in_component(FILES "${wasilibc_modulemap_source}"
DESTINATION "lib/swift/embedded/${arch}"
COMPONENT sdk-overlay)
endif()

set(wasilibc_apinotes_source "SwiftWASILibc.apinotes")
add_custom_command_target(
copy_wasilibc_apinotes_resource
Expand Down Expand Up @@ -571,3 +590,41 @@ if(WINDOWS IN_LIST SWIFT_SDKS)
DESTINATION "share"
COMPONENT sdk-overlay)
endif()

if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB)
add_custom_target(embedded-stdlib-platform)
add_dependencies(embedded-libraries embedded-stdlib-platform)

if(SWIFT_WASI_SYSROOT_PATH)
set(arch "wasm32")
set(triple "wasm32-unknown-wasip1")

set(SWIFT_SDK_embedded_ARCH_${arch}_MODULE "${triple}")
set(SWIFT_SDK_embedded_LIB_SUBDIR "embedded")
set(SWIFT_SDK_embedded_ARCH_${arch}_TRIPLE "${triple}")
add_swift_target_library_single(
embedded-stdlib-platform-${triple}
swiftWASILibc
ONLY_SWIFTMODULE
IS_STDLIB IS_FRAGILE
${swift_platform_sources}
POSIXError.swift

GYB_SOURCES
${swift_platform_gyb_sources}
WASILibc.swift.gyb

SWIFT_COMPILE_FLAGS
-enable-experimental-feature Embedded
-Xfrontend -emit-empty-object-file

MODULE_DIR "${CMAKE_BINARY_DIR}/lib/swift/embedded"
SDK "embedded"
ARCHITECTURE "${arch}"
ARCHITECTURE_SUBDIR_NAME "${triple}"
DEPENDS embedded-stdlib-${triple} wasilibc_modulemap
INSTALL_IN_COMPONENT sdk-overlay
)
add_dependencies(embedded-stdlib-platform embedded-stdlib-platform-${triple})
endif()
endif()
18 changes: 18 additions & 0 deletions test/embedded/wasm/wasilibc_functions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -parse-as-library -wmo) | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: OS=wasip1
// REQUIRES: swift_feature_Embedded

import WASILibc
@main struct Main {
static func main() {
puts("Hello")
// CHECK: Hello
let div_result = div(5,2)
print(div_result.quot)
// CHECK: 2
print(div_result.rem)
// CHECK: 1
}
}