Skip to content

Commit 9453946

Browse files
authored
[6.2] Embedded Wasm: enable import WASILibc (#83846)
**Explanation**: Cherry-pick of #83792, merged as 450cb14 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. **Scope**: limited to Embedded Swift for Wasm; **Risk**: low due to limited scope; **Testing**: added new lit test to the existing Embedded Swift test suite; **Issue**: rdar://157467412 **Reviewer**: @bnbarham
1 parent 4c68a06 commit 9453946

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
@@ -504,6 +504,7 @@ if("WASI" IN_LIST SWIFT_SDKS)
504504
set(arch_subdir "${SWIFT_SDK_WASI_LIB_SUBDIR}/${arch}")
505505
set(module_dir "${SWIFTLIB_DIR}/${arch_subdir}")
506506
set(module_dir_static "${SWIFTSTATICLIB_DIR}/${arch_subdir}")
507+
set(module_dir_embedded "${SWIFTLIB_DIR}/embedded/${arch}")
507508

508509
add_custom_command_target(
509510
copy_wasilibc_modulemap_resource
@@ -529,6 +530,24 @@ if("WASI" IN_LIST SWIFT_SDKS)
529530
COMPONENT sdk-overlay)
530531
endif()
531532

533+
if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB)
534+
add_custom_command_target(
535+
copy_wasilibc_modulemap_embedded_resource
536+
COMMAND
537+
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir} ${module_dir_embedded}
538+
COMMAND
539+
"${CMAKE_COMMAND}" "-E" "copy_if_different"
540+
"${CMAKE_CURRENT_SOURCE_DIR}/${wasilibc_modulemap_source}" ${module_dir_embedded}
541+
OUTPUT "${module_dir_embedded}/${wasilibc_modulemap_source}"
542+
COMMENT "Copying WASILibc modulemap to resource directories")
543+
add_dependencies(sdk-overlay ${copy_wasilibc_modulemap_embedded_resource})
544+
list(APPEND wasilibc_modulemap_target_list ${copy_wasilibc_modulemap_embedded_resource})
545+
546+
swift_install_in_component(FILES "${wasilibc_modulemap_source}"
547+
DESTINATION "lib/swift/embedded/${arch}"
548+
COMPONENT sdk-overlay)
549+
endif()
550+
532551
set(wasilibc_apinotes_source "SwiftWASILibc.apinotes")
533552
add_custom_command_target(
534553
copy_wasilibc_apinotes_resource
@@ -571,3 +590,41 @@ if(WINDOWS IN_LIST SWIFT_SDKS)
571590
DESTINATION "share"
572591
COMPONENT sdk-overlay)
573592
endif()
593+
594+
if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB)
595+
add_custom_target(embedded-stdlib-platform)
596+
add_dependencies(embedded-libraries embedded-stdlib-platform)
597+
598+
if(SWIFT_WASI_SYSROOT_PATH)
599+
set(arch "wasm32")
600+
set(triple "wasm32-unknown-wasip1")
601+
602+
set(SWIFT_SDK_embedded_ARCH_${arch}_MODULE "${triple}")
603+
set(SWIFT_SDK_embedded_LIB_SUBDIR "embedded")
604+
set(SWIFT_SDK_embedded_ARCH_${arch}_TRIPLE "${triple}")
605+
add_swift_target_library_single(
606+
embedded-stdlib-platform-${triple}
607+
swiftWASILibc
608+
ONLY_SWIFTMODULE
609+
IS_STDLIB IS_FRAGILE
610+
${swift_platform_sources}
611+
POSIXError.swift
612+
613+
GYB_SOURCES
614+
${swift_platform_gyb_sources}
615+
WASILibc.swift.gyb
616+
617+
SWIFT_COMPILE_FLAGS
618+
-enable-experimental-feature Embedded
619+
-Xfrontend -emit-empty-object-file
620+
621+
MODULE_DIR "${CMAKE_BINARY_DIR}/lib/swift/embedded"
622+
SDK "embedded"
623+
ARCHITECTURE "${arch}"
624+
ARCHITECTURE_SUBDIR_NAME "${triple}"
625+
DEPENDS embedded-stdlib-${triple} wasilibc_modulemap
626+
INSTALL_IN_COMPONENT sdk-overlay
627+
)
628+
add_dependencies(embedded-stdlib-platform embedded-stdlib-platform-${triple})
629+
endif()
630+
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)