From b7d75701aa439ea185c1c59c25f5c7fb9db32043 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 15 Sep 2025 14:54:34 -0700 Subject: [PATCH 1/2] Always build wasm32-wasip2 with `-fPIC` This commit updates the build of the `wasm32-wasip2` target to always pass `-fPIC` to compilation of wasi-libc. This notably enables using `libc.a` in dynamic linking situations instead of being forced to use `libc.so`. While many applications likely want to use `libc.so` I've found it more flexible if objects are compatible with as many builds as possible. This will enable, for example, building shared libraries in Rust by default since Rust only ships `libc.a`, not `libc.so`, in the pre-built sysroot. This behavior additionally matches the Rust `wasm32-wasip2` target where `-fPIC` is enabled by default there. --- cmake/wasi-sdk-sysroot.cmake | 40 ++++++++++++++++++------------------ src/wasi-libc | 2 +- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/cmake/wasi-sdk-sysroot.cmake b/cmake/wasi-sdk-sysroot.cmake index 25d5837..91aaaf1 100644 --- a/cmake/wasi-sdk-sysroot.cmake +++ b/cmake/wasi-sdk-sysroot.cmake @@ -139,31 +139,31 @@ add_custom_target(compiler-rt DEPENDS compiler-rt-build compiler-rt-post-build) function(define_wasi_libc_sub target target_suffix lto) set(build_dir ${CMAKE_CURRENT_BINARY_DIR}/wasi-libc-${target}${target_suffix}) - if(${target} MATCHES threads) - if(lto) - set(extra_make_flags LTO=full THREAD_MODEL=posix) - else() - set(extra_make_flags THREAD_MODEL=posix) - endif() - elseif(${target} MATCHES p2) - if(lto) - set(extra_make_flags LTO=full WASI_SNAPSHOT=p2 default) - else() - set(extra_make_flags WASI_SNAPSHOT=p2 default libc_so) - endif() - else() - if(lto) - set(extra_make_flags LTO=full default) - else() - set(extra_make_flags default libc_so) - endif() - endif() - string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_UPPER) get_property(directory_cflags DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY COMPILE_OPTIONS) list(APPEND directory_cflags -resource-dir ${wasi_resource_dir}) set(extra_cflags_list "${WASI_SDK_CPU_CFLAGS} ${CMAKE_C_FLAGS} ${directory_cflags} ${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UPPER}}") + + set(extra_make_flags default) + + # If LTO is enabled then pass that on to wasi-libc, and otherwise make sure to + # build a `libc.so` dynamic library. + if(lto) + list(APPEND extra_make_flags LTO=full) + else() + list(APPEND extra_make_flags libc_so) + endif() + + if(${target} MATCHES threads) + list(APPEND extra_make_flags THREAD_MODEL=posix) + elseif(${target} MATCHES p2) + list(APPEND extra_make_flags WASI_SNAPSHOT=p2) + # Always enable `-fPIC` for the `wasm32-wasip2` target. This makes `libc.a` + # more flexible and usable in dynamic linking situations. + list(APPEND extra_cflags_list -fPIC) + endif() + list(JOIN extra_cflags_list " " extra_cflags) if(${target} MATCHES threads) diff --git a/src/wasi-libc b/src/wasi-libc index bbb0c01..008d705 160000 --- a/src/wasi-libc +++ b/src/wasi-libc @@ -1 +1 @@ -Subproject commit bbb0c01b112f8c3aaa1974407c5302a26b88babb +Subproject commit 008d705c9d16dc9057bb6efc220fb539f1fb7fb5 From d61c751015a74dd8d353a5042ddf5f3f47fab4ce Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 16 Sep 2025 07:08:16 -0700 Subject: [PATCH 2/2] Disable shared objects with threads --- cmake/wasi-sdk-sysroot.cmake | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmake/wasi-sdk-sysroot.cmake b/cmake/wasi-sdk-sysroot.cmake index 91aaaf1..c1837c9 100644 --- a/cmake/wasi-sdk-sysroot.cmake +++ b/cmake/wasi-sdk-sysroot.cmake @@ -148,10 +148,11 @@ function(define_wasi_libc_sub target target_suffix lto) set(extra_make_flags default) # If LTO is enabled then pass that on to wasi-libc, and otherwise make sure to - # build a `libc.so` dynamic library. + # build a `libc.so` dynamic library where possible (not compatible with + # threads though) if(lto) list(APPEND extra_make_flags LTO=full) - else() + elseif(NOT ${target} MATCHES threads) list(APPEND extra_make_flags libc_so) endif()