From ac455270257a1b27bfb7a8d85c4b296e93ba03bf Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 10 Aug 2024 16:13:28 +0000 Subject: [PATCH 1/2] Autolink wasi-libc emulation libraries `_WASI_EMULATED_XXX` macros require linking against the corresponding emulation libraries. This patch adds `-Xfrontend -public-autolink-library` flags to the Swift compiler invocation to automatically link against the emulation libraries. Also enable getpid emulation explicitly, as it is available by default but emiting a warning. --- CMakeLists.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 108b65726..a2024859a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -118,8 +118,12 @@ endforeach() set(_SwiftFoundation_wasi_libc_flags) if(CMAKE_SYSTEM_NAME STREQUAL "WASI") list(APPEND _SwiftFoundation_wasi_libc_flags + "SHELL:$<$:-Xcc -D_WASI_EMULATED_GETPID>" + "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-getpid>" "SHELL:$<$:-Xcc -D_WASI_EMULATED_SIGNAL>" - "SHELL:$<$:-Xcc -D_WASI_EMULATED_MMAN>") + "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-signal>" + "SHELL:$<$:-Xcc -D_WASI_EMULATED_MMAN>" + "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-mman>") endif() include(GNUInstallDirs) From 7f4abde248649fab83966ddc36df5e65827e663d Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 16 Aug 2024 09:52:40 +0000 Subject: [PATCH 2/2] Link emulation libraries directly if building shared libraries --- CMakeLists.txt | 19 ++++++++++++++----- Sources/FoundationEssentials/CMakeLists.txt | 1 + .../CMakeLists.txt | 1 + 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a2024859a..79ad98d2d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,16 +114,25 @@ foreach(version ${_SwiftFoundation_versions}) endforeach() endforeach() -# wasi-libc emulation feature flags +# wasi-libc emulation feature flags passed to the Swift compiler set(_SwiftFoundation_wasi_libc_flags) +# wasi-libc emulation libraries to link against when building a shared library +set(_SwiftFoundation_wasi_libc_libraries) if(CMAKE_SYSTEM_NAME STREQUAL "WASI") list(APPEND _SwiftFoundation_wasi_libc_flags "SHELL:$<$:-Xcc -D_WASI_EMULATED_GETPID>" - "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-getpid>" "SHELL:$<$:-Xcc -D_WASI_EMULATED_SIGNAL>" - "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-signal>" - "SHELL:$<$:-Xcc -D_WASI_EMULATED_MMAN>" - "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-mman>") + "SHELL:$<$:-Xcc -D_WASI_EMULATED_MMAN>") + if(BUILD_SHARED_LIBS) + # Link emulation libraries to the shared library directly when building a shared library + list(APPEND _SwiftFoundation_wasi_libc_libraries wasi-emulated-getpid wasi-emulated-signal wasi-emulated-mman) + else() + # Emit autolink entries to let clients to link against the emulation libraries + list(APPEND _SwiftFoundation_wasi_libc_flags + "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-getpid>" + "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-signal>" + "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-mman>") + endif() endif() include(GNUInstallDirs) diff --git a/Sources/FoundationEssentials/CMakeLists.txt b/Sources/FoundationEssentials/CMakeLists.txt index e8417d191..006b6caf7 100644 --- a/Sources/FoundationEssentials/CMakeLists.txt +++ b/Sources/FoundationEssentials/CMakeLists.txt @@ -73,6 +73,7 @@ target_compile_options(FoundationEssentials PRIVATE -package-name "SwiftFoundati target_link_libraries(FoundationEssentials PUBLIC _FoundationCShims _FoundationCollections) +target_link_libraries(FoundationEssentials PUBLIC ${_SwiftFoundation_wasi_libc_libraries}) if(NOT BUILD_SHARED_LIBS) target_compile_options(FoundationEssentials PRIVATE diff --git a/Sources/FoundationInternationalization/CMakeLists.txt b/Sources/FoundationInternationalization/CMakeLists.txt index 857db9cac..baaf74513 100644 --- a/Sources/FoundationInternationalization/CMakeLists.txt +++ b/Sources/FoundationInternationalization/CMakeLists.txt @@ -40,6 +40,7 @@ target_link_libraries(FoundationInternationalization PUBLIC FoundationEssentials _FoundationCShims _FoundationICU) +target_link_libraries(FoundationInternationalization PUBLIC ${_SwiftFoundation_wasi_libc_libraries}) if(NOT BUILD_SHARED_LIBS) target_compile_options(FoundationInternationalization PRIVATE