Skip to content

Commit e905df1

Browse files
Autolink wasi-libc emulation libraries (#843)
* 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. * Link emulation libraries directly if building shared libraries
1 parent ecf5445 commit e905df1

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

CMakeLists.txt

+14-1
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,25 @@ foreach(version ${_SwiftFoundation_versions})
116116
endforeach()
117117
endforeach()
118118

119-
# wasi-libc emulation feature flags
119+
# wasi-libc emulation feature flags passed to the Swift compiler
120120
set(_SwiftFoundation_wasi_libc_flags)
121+
# wasi-libc emulation libraries to link against when building a shared library
122+
set(_SwiftFoundation_wasi_libc_libraries)
121123
if(CMAKE_SYSTEM_NAME STREQUAL "WASI")
122124
list(APPEND _SwiftFoundation_wasi_libc_flags
125+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xcc -D_WASI_EMULATED_GETPID>"
123126
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xcc -D_WASI_EMULATED_SIGNAL>"
124127
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xcc -D_WASI_EMULATED_MMAN>")
128+
if(BUILD_SHARED_LIBS)
129+
# Link emulation libraries to the shared library directly when building a shared library
130+
list(APPEND _SwiftFoundation_wasi_libc_libraries wasi-emulated-getpid wasi-emulated-signal wasi-emulated-mman)
131+
else()
132+
# Emit autolink entries to let clients to link against the emulation libraries
133+
list(APPEND _SwiftFoundation_wasi_libc_flags
134+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-getpid>"
135+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-signal>"
136+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-mman>")
137+
endif()
125138
endif()
126139

127140
include(GNUInstallDirs)

Sources/FoundationEssentials/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ target_compile_options(FoundationEssentials PRIVATE -package-name "SwiftFoundati
7272
target_link_libraries(FoundationEssentials PUBLIC
7373
_FoundationCShims
7474
_FoundationCollections)
75+
target_link_libraries(FoundationEssentials PUBLIC ${_SwiftFoundation_wasi_libc_libraries})
7576

7677
if(NOT BUILD_SHARED_LIBS)
7778
target_compile_options(FoundationEssentials PRIVATE

Sources/FoundationInternationalization/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ target_link_libraries(FoundationInternationalization PUBLIC
4040
FoundationEssentials
4141
_FoundationCShims
4242
_FoundationICU)
43+
target_link_libraries(FoundationInternationalization PUBLIC ${_SwiftFoundation_wasi_libc_libraries})
4344

4445
if(NOT BUILD_SHARED_LIBS)
4546
target_compile_options(FoundationInternationalization PRIVATE

0 commit comments

Comments
 (0)