Skip to content

Commit

Permalink
build(docker): use emulated pthreads
Browse files Browse the repository at this point in the history
Until wasmtime supports threads in its C-API.
  • Loading branch information
thewtex committed Jan 21, 2025
1 parent c890071 commit 48bf11d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/docker/itk-wasm-base/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ RUN . /itk_wasm_env_vars.sh && sed -i -e "/^set(DCMTK_GIT_REPOSITORY/c\set(DCMTK
RUN . /itk_wasm_env_vars.sh && sed -i -e "/^set(DCMTK_GIT_TAG/c\set(DCMTK_GIT_TAG \"${ITK_WASM_DCMTK_GIT_TAG}\")" \
/ITK/Modules/ThirdParty/DCMTK/DCMTKGitTag.cmake

# Current overwrite for the dockcross image to use emulated threads.
# This can be removed after wasmtime supports threads in its C-API.
COPY clang-wasi-threads-sysroot.sh clang++-wasi-threads-sysroot.sh /usr/local/bin/

ARG CMAKE_BUILD_TYPE=Release

ARG LDFLAGS
Expand Down
8 changes: 4 additions & 4 deletions src/docker/itk-wasm-base/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ done
set -- "${newparams[@]}" # overwrites the original positional params

# Note: also need to set in wasi-sdk-pthread-itkwasm.cmake
wasi_ld_flags="-flto -lwasi-emulated-process-clocks -lwasi-emulated-signal -lc-printscan-long-double"
wasi_c_flags="-flto -msimd128 -D_WASI_EMULATED_PROCESS_CLOCKS -D_WASI_EMULATED_SIGNAL"
wasi_ld_flags="-D_WASI_EMULATED_PTHREAD -flto -lwasi-emulated-process-clocks -lwasi-emulated-signal -lc-printscan-long-double"
wasi_c_flags="-lwasi-emulated-pthread -flto -msimd128 -D_WASI_EMULATED_PROCESS_CLOCKS -D_WASI_EMULATED_SIGNAL"

emscripten_debug_ld_flags="-fno-lto -s ALLOW_MEMORY_GROWTH=1 -s MAXIMUM_MEMORY=4GB"
emscripten_debug_c_flags="-fno-lto -Wno-warn-absolute-paths"

wasi_debug_ld_flags="-fno-lto -lwasi-emulated-process-clocks -lwasi-emulated-signal -lc-printscan-long-double"
wasi_debug_c_flags="-fno-lto -D_WASI_EMULATED_PROCESS_CLOCKS -D_WASI_EMULATED_SIGNAL"
wasi_debug_ld_flags="-D_WASI_EMULATED_PTHREAD -fno-lto -lwasi-emulated-process-clocks -lwasi-emulated-signal -lc-printscan-long-double"
wasi_debug_c_flags="-lwasi-emulated-pthread -fno-lto -D_WASI_EMULATED_PROCESS_CLOCKS -D_WASI_EMULATED_SIGNAL"

$exe $build_cmd $tag_flag quay.io/itkwasm/emscripten-base:latest-$host_arch \
--build-arg IMAGE=quay.io/itkwasm/emscripten-base \
Expand Down
3 changes: 3 additions & 0 deletions src/docker/itk-wasm-base/clang++-wasi-threads-sysroot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env sh

exec ${WASI_SDK_PATH}/bin/clang++ --target=wasm32-wasi-threads --sysroot=${WASI_SYSROOT} "$@"
3 changes: 3 additions & 0 deletions src/docker/itk-wasm-base/clang-wasi-threads-sysroot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env sh

exec ${WASI_SDK_PATH}/bin/clang --target=wasm32-wasi-threads --sysroot=${WASI_SYSROOT} "$@"
6 changes: 3 additions & 3 deletions src/docker/itk-wasm-base/wasi-sdk-pthread-itkwasm.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ set(CMAKE_SYSTEM_NAME WASI)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR wasm32)
set(triple wasm32-wasi-threads)
set(CMAKE_C_FLAGS "-pthread -matomics -mbulk-memory -flto -msimd128 -D_WASI_EMULATED_PROCESS_CLOCKS -D_WASI_EMULATED_SIGNAL ${CMAKE_C_FLAGS}")
set(CMAKE_C_FLAGS "-D_WASI_EMULATED_PTHREAD -mbulk-memory -flto -msimd128 -D_WASI_EMULATED_PROCESS_CLOCKS -D_WASI_EMULATED_SIGNAL ${CMAKE_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_CXX_FLAGS}")
# wasi-threads requires --import-memory.
# wasi requires --export-memory.
# (--export-memory is implicit unless --import-memory is given)
set(CMAKE_EXE_LINKER_FLAGS "-Wl,--import-memory ${CMAKE_EXE_LINKER_FLAGS}")
# set(CMAKE_EXE_LINKER_FLAGS "-Wl,--import-memory ${CMAKE_EXE_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "-Wl,--export-memory ${CMAKE_EXE_LINKER_FLAGS}")
# ITK-Wasm
set(CMAKE_EXE_LINKER_FLAGS "-Wl,--initial-memory=8388608,--max-memory=4294967296,--shared-memory -flto -lwasi-emulated-process-clocks -lwasi-emulated-signal -lc-printscan-long-double ${CMAKE_EXE_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "-lwasi-emulated-pthread -flto -lwasi-emulated-process-clocks -lwasi-emulated-signal -lc-printscan-long-double ${CMAKE_EXE_LINKER_FLAGS}")

if(WIN32)
set(WASI_HOST_EXE_SUFFIX ".exe")
Expand Down

0 comments on commit 48bf11d

Please sign in to comment.