Skip to content

Commit 6582ad9

Browse files
committed
Embedded Wasm: enable import WASILibc
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
1 parent 31c6cb2 commit 6582ad9

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

stdlib/public/Platform/CMakeLists.txt

Lines changed: 58 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,25 @@ 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_subdir}"
429+
COMPONENT sdk-overlay)
430+
message(STATUS "wasilibc_modulemap_target_list is ${wasilibc_modulemap_target_list}")
431+
endif()
432+
413433
set(wasilibc_apinotes_source "SwiftWASILibc.apinotes")
414434
add_custom_command_target(
415435
copy_wasilibc_apinotes_resource
@@ -452,3 +472,41 @@ if(WINDOWS IN_LIST SWIFT_SDKS)
452472
DESTINATION "share"
453473
COMPONENT sdk-overlay)
454474
endif()
475+
476+
if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB)
477+
add_custom_target(embedded-stdlib-platform)
478+
add_dependencies(embedded-libraries embedded-stdlib-platform)
479+
480+
if(SWIFT_WASI_SYSROOT_PATH)
481+
set(arch "wasm32")
482+
set(triple "wasm32-unknown-wasip1")
483+
484+
set(SWIFT_SDK_embedded_ARCH_${arch}_MODULE "${triple}")
485+
set(SWIFT_SDK_embedded_LIB_SUBDIR "embedded")
486+
set(SWIFT_SDK_embedded_ARCH_${arch}_TRIPLE "${triple}")
487+
add_swift_target_library_single(
488+
embedded-stdlib-platform-${triple}
489+
swiftWASILibc
490+
ONLY_SWIFTMODULE
491+
IS_STDLIB IS_FRAGILE
492+
${swift_platform_sources}
493+
POSIXError.swift
494+
495+
GYB_SOURCES
496+
${swift_platform_gyb_sources}
497+
WASILibc.swift.gyb
498+
499+
SWIFT_COMPILE_FLAGS
500+
-enable-experimental-feature Embedded
501+
-Xfrontend -emit-empty-object-file
502+
503+
MODULE_DIR "${CMAKE_BINARY_DIR}/lib/swift/embedded"
504+
SDK "embedded"
505+
ARCHITECTURE "${arch}"
506+
ARCHITECTURE_SUBDIR_NAME "${mod}"
507+
DEPENDS wasilibc_modulemap
508+
INSTALL_IN_COMPONENT sdk-overlay
509+
)
510+
add_dependencies(embedded-stdlib-platform embedded-stdlib-platform-${triple})
511+
endif()
512+
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)