Skip to content

Commit 89d0ba6

Browse files
authored
Always build wasm32-wasip2 with -fPIC (#564)
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.
1 parent 169e79f commit 89d0ba6

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

cmake/wasi-sdk-sysroot.cmake

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,31 +139,32 @@ add_custom_target(compiler-rt DEPENDS compiler-rt-build compiler-rt-post-build)
139139
function(define_wasi_libc_sub target target_suffix lto)
140140
set(build_dir ${CMAKE_CURRENT_BINARY_DIR}/wasi-libc-${target}${target_suffix})
141141

142-
if(${target} MATCHES threads)
143-
if(lto)
144-
set(extra_make_flags LTO=full THREAD_MODEL=posix)
145-
else()
146-
set(extra_make_flags THREAD_MODEL=posix)
147-
endif()
148-
elseif(${target} MATCHES p2)
149-
if(lto)
150-
set(extra_make_flags LTO=full WASI_SNAPSHOT=p2 default)
151-
else()
152-
set(extra_make_flags WASI_SNAPSHOT=p2 default libc_so)
153-
endif()
154-
else()
155-
if(lto)
156-
set(extra_make_flags LTO=full default)
157-
else()
158-
set(extra_make_flags default libc_so)
159-
endif()
160-
endif()
161-
162142
string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_UPPER)
163143
get_property(directory_cflags DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY COMPILE_OPTIONS)
164144
list(APPEND directory_cflags -resource-dir ${wasi_resource_dir})
165145
set(extra_cflags_list
166146
"${WASI_SDK_CPU_CFLAGS} ${CMAKE_C_FLAGS} ${directory_cflags} ${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UPPER}}")
147+
148+
set(extra_make_flags default)
149+
150+
# If LTO is enabled then pass that on to wasi-libc, and otherwise make sure to
151+
# build a `libc.so` dynamic library where possible (not compatible with
152+
# threads though)
153+
if(lto)
154+
list(APPEND extra_make_flags LTO=full)
155+
elseif(NOT ${target} MATCHES threads)
156+
list(APPEND extra_make_flags libc_so)
157+
endif()
158+
159+
if(${target} MATCHES threads)
160+
list(APPEND extra_make_flags THREAD_MODEL=posix)
161+
elseif(${target} MATCHES p2)
162+
list(APPEND extra_make_flags WASI_SNAPSHOT=p2)
163+
# Always enable `-fPIC` for the `wasm32-wasip2` target. This makes `libc.a`
164+
# more flexible and usable in dynamic linking situations.
165+
list(APPEND extra_cflags_list -fPIC)
166+
endif()
167+
167168
list(JOIN extra_cflags_list " " extra_cflags)
168169

169170
if(${target} MATCHES threads)

src/wasi-libc

Submodule wasi-libc updated 106 files

0 commit comments

Comments
 (0)