Skip to content

Commit 450cb14

Browse files
authored
Embedded Wasm: enable import WASILibc (#83792)
WASILibc wasn't built for the embedded stdlib in `stdlib/public/Platform/CMakeLists.txt`. New `copy_wasilibc_modulemap_embedded_resource` and `embedded-stdlib-platform-${triple}` targets are added, the latter for `wasm32-unknown-wasip1` only for now. Also added a `wasilibc_functions.swift` test to verify the result. Resolves #83513 rdar://157467412
2 parents 93daeff + 900c7c9 commit 450cb14

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

stdlib/public/Platform/CMakeLists.txt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ if("WASI" IN_LIST SWIFT_SDKS)
385385
set(arch_subdir "${SWIFT_SDK_WASI_LIB_SUBDIR}/${arch}")
386386
set(module_dir "${SWIFTLIB_DIR}/${arch_subdir}")
387387
set(module_dir_static "${SWIFTSTATICLIB_DIR}/${arch_subdir}")
388+
set(module_dir_embedded "${SWIFTLIB_DIR}/embedded/${arch}")
388389

389390
add_custom_command_target(
390391
copy_wasilibc_modulemap_resource
@@ -410,6 +411,24 @@ if("WASI" IN_LIST SWIFT_SDKS)
410411
COMPONENT sdk-overlay)
411412
endif()
412413

414+
if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB)
415+
add_custom_command_target(
416+
copy_wasilibc_modulemap_embedded_resource
417+
COMMAND
418+
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir} ${module_dir_embedded}
419+
COMMAND
420+
"${CMAKE_COMMAND}" "-E" "copy_if_different"
421+
"${CMAKE_CURRENT_SOURCE_DIR}/${wasilibc_modulemap_source}" ${module_dir_embedded}
422+
OUTPUT "${module_dir_embedded}/${wasilibc_modulemap_source}"
423+
COMMENT "Copying WASILibc modulemap to resource directories")
424+
add_dependencies(sdk-overlay ${copy_wasilibc_modulemap_embedded_resource})
425+
list(APPEND wasilibc_modulemap_target_list ${copy_wasilibc_modulemap_embedded_resource})
426+
427+
swift_install_in_component(FILES "${wasilibc_modulemap_source}"
428+
DESTINATION "lib/swift/embedded/${arch}"
429+
COMPONENT sdk-overlay)
430+
endif()
431+
413432
set(wasilibc_apinotes_source "SwiftWASILibc.apinotes")
414433
add_custom_command_target(
415434
copy_wasilibc_apinotes_resource
@@ -452,3 +471,41 @@ if(WINDOWS IN_LIST SWIFT_SDKS)
452471
DESTINATION "share"
453472
COMPONENT sdk-overlay)
454473
endif()
474+
475+
if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB)
476+
add_custom_target(embedded-stdlib-platform)
477+
add_dependencies(embedded-libraries embedded-stdlib-platform)
478+
479+
if(SWIFT_WASI_SYSROOT_PATH)
480+
set(arch "wasm32")
481+
set(triple "wasm32-unknown-wasip1")
482+
483+
set(SWIFT_SDK_embedded_ARCH_${arch}_MODULE "${triple}")
484+
set(SWIFT_SDK_embedded_LIB_SUBDIR "embedded")
485+
set(SWIFT_SDK_embedded_ARCH_${arch}_TRIPLE "${triple}")
486+
add_swift_target_library_single(
487+
embedded-stdlib-platform-${triple}
488+
swiftWASILibc
489+
ONLY_SWIFTMODULE
490+
IS_STDLIB IS_FRAGILE
491+
${swift_platform_sources}
492+
POSIXError.swift
493+
494+
GYB_SOURCES
495+
${swift_platform_gyb_sources}
496+
WASILibc.swift.gyb
497+
498+
SWIFT_COMPILE_FLAGS
499+
-enable-experimental-feature Embedded
500+
-Xfrontend -emit-empty-object-file
501+
502+
MODULE_DIR "${CMAKE_BINARY_DIR}/lib/swift/embedded"
503+
SDK "embedded"
504+
ARCHITECTURE "${arch}"
505+
ARCHITECTURE_SUBDIR_NAME "${triple}"
506+
DEPENDS embedded-stdlib-${triple} wasilibc_modulemap
507+
INSTALL_IN_COMPONENT sdk-overlay
508+
)
509+
add_dependencies(embedded-stdlib-platform embedded-stdlib-platform-${triple})
510+
endif()
511+
endif()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -parse-as-library -wmo) | %FileCheck %s
2+
3+
// REQUIRES: executable_test
4+
// REQUIRES: OS=wasip1
5+
// REQUIRES: swift_feature_Embedded
6+
7+
import WASILibc
8+
@main struct Main {
9+
static func main() {
10+
puts("Hello")
11+
// CHECK: Hello
12+
let div_result = div(5,2)
13+
print(div_result.quot)
14+
// CHECK: 2
15+
print(div_result.rem)
16+
// CHECK: 1
17+
}
18+
}

0 commit comments

Comments
 (0)