Issues I encountered while compiling with the vtk-wasm-sdk Docker.

When I used the Docker image of kitware/vtk-wasm-sdk, I encountered various problems.
I followed the Usage on DockerHub, downloaded the examples, and then proceeded to compile them.
I am using the Windows platform, and the compilation script is like this:

$erroractionpreference = "stop"

$sdk_version="wasm32-v9.3.1-3657-gfd3dc37e8e-20240728"
$sdk_image="kitware/vtk-wasm-sdk"
$sdk_config="Release"

$sdk_dir="/VTK-install/$sdk_config/lib/cmake/vtk"

docker run `
    --rm `
    -v"$pwd/Cone":/Cone `
    ${sdk_image}:${sdk_version} `
    emcmake cmake -GNinja -S /Cone -B /Cone/build -DCMAKE_BUILD_TYPE="$sdk_config" -DVTK_DIR="$sdk_dir" # -DCMAKE_C_FLAGS=MEMORY64 -DCMAKE_CXX_FLAGS=MEMORY64

docker run `
    --rm `
    -v"$pwd/Cone":/Cone `
    ${sdk_image}:${sdk_version} `
    cmake --build /Cone/build

I used images with tag above:
wasm32-v9.3.1-3657-gfd3dc37e8e-20240728
wasm64-threads-v9.3.1-3657-gfd3dc37e8e-20240728
wasm32-threads-v9.3.1-3657-gfd3dc37e8e-20240728
wasm64-v9.3.1-3657-gfd3dc37e8e-20240728

wasm32-v9.3.1-3657-gfd3dc37e8e-20240728 is works,but others failed.(Added -DCMAKE_C_FLAGS=MEMORY64 -DCMAKE_CXX_FLAGS=MEMORY64 at 64-bit)

the log is:

configure: cmake -GNinja -S /Cone -B /Cone/build -DCMAKE_BUILD_TYPE=Release -DVTK_DIR=/VTK-install/Release/lib/cmake/vtk -DCMAKE_C_FLAGS=MEMORY64 -DCMAKE_CXX_FLAGS=MEMORY64 -DCMAKE_CROSSCOMPILING_EMULATOR=/emsdk/node/16.20.0_64bit/bin/node
-- Found OpenGL: /emsdk/upstream/emscripten/cache/sysroot/include  found components: GLES3 
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - not found
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - no
-- Could NOT find Threads (missing: Threads_FOUND)
-- Could NOT find Threads (missing: Threads_FOUND)
-- Configuring done (9.1s)
CMake Error at /VTK-install/Release/lib/cmake/vtk/VTK-targets.cmake:195 (set_target_properties):
  The link interface of target "VTK::CommonCore" contains:

    Threads::Threads

  but the target was not found.  Possible reasons include:

    * There is a typo in the target name.
    * A find_package call is missing for an IMPORTED target.
    * An ALIAS target is missing.

Call Stack (most recent call first):
  /VTK-install/Release/lib/cmake/vtk/vtk-config.cmake:151 (include)
  CMakeLists.txt:17 (find_package)


CMake Error at /VTK-install/Release/lib/cmake/vtk/VTK-targets.cmake:384 (set_target_properties):
  The link interface of target "VTK::CommonSystem" contains:

    Threads::Threads

  but the target was not found.  Possible reasons include:

    * There is a typo in the target name.
    * A find_package call is missing for an IMPORTED target.
    * An ALIAS target is missing.

Call Stack (most recent call first):
  /VTK-install/Release/lib/cmake/vtk/vtk-config.cmake:151 (include)
  CMakeLists.txt:17 (find_package)


-- Generating done (0.1s)
CMake Generate step failed.  Build files cannot be regenerated correctly.
emcmake: error: 'cmake -GNinja -S /Cone -B /Cone/build -DCMAKE_BUILD_TYPE=Release -DVTK_DIR=/VTK-install/Release/lib/cmake/vtk -DCMAKE_C_FLAGS=MEMORY64 -DCMAKE_CXX_FLAGS=MEMORY64 -DCMAKE_CROSSCOMPILING_EMULATOR=/emsdk/node/16.20.0_64bit/bin/node' failed (returned 1)
ninja: error: loading 'build.ninja': No such file or directory

What is the problem, and how can it be resolved?

Hello @Behtgod!

That Cone example does not automatically enable threads. If you’re developing a threaded VTK wasm application, please see the unit test’s vtk-wasm-sdk/.gitlab/ci/docker/tests/basic/CMakeLists.txt for how to manage threads and 32/64 bit. That unit test is built and run for all images as a sanity check before they are published to dockerhub.

If you still want to run the Cone example with threads or 64-bit, you will need to correctly define the C/Cxx compiler flags and static/exe linker flags so that Emscripten compiler works.

That memory flag is incomplete. Emscripten settings are specified using an -s prefix. Emscripten documents all settings here Emscripten Compiler Settings — Emscripten 3.1.65-git (dev) documentation

As a general rule for any WebAssembly application built using Emscripten, here’s an unofficial table of the CMake flags required when targetting diffferent combinations of 32/64 bit with/without threads.

Architecture CMAKE_C_FLAGS CMAKE_CXX_FLAGS CMAKE_EXE_LINKER_FLAGS CMAKE_STATIC_LINKER_FLAGS
32
64 “-sMEMORY64=1” “-sMEMORY64=1” “-sMEMORY64=1” “-sMEMORY64=1”
32+threads “-pthread” “-pthread” “-pthread” “-pthread”
64+threads “-sMEMORY64=1 -pthread” “-sMEMORY64=1 -pthread” “-sMEMORY64=1 -pthread” “-sMEMORY64=1 -pthread”

Thank you @jaswantp.
I modified CmakeLists and the compilation script, like above:

cmake_minimum_required(VERSION 3.13)
project(Cone)

# -----------------------------------------------------------------------------
# EMSCRIPTEN only
# -----------------------------------------------------------------------------

if (NOT EMSCRIPTEN)
  message("Skipping example: This needs to run inside an Emscripten build environment")
  return ()
endif ()

# -----------------------------------------------------------------------------
# Handle VTK dependency
# -----------------------------------------------------------------------------

find_package(Threads REQUIRED)

find_package(VTK
  COMPONENTS
    FiltersSources      # VTK pipeline
    InteractionStyle    # Mouse handling
    RenderingOpenGL2    # For Rendering
    RenderingUI         # For SDL2 Window
)

if (NOT VTK_FOUND)
  message("Skipping example: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif ()

# -----------------------------------------------------------------------------
# Compile example code
# -----------------------------------------------------------------------------

add_executable(Cone Cone.cxx)

target_link_libraries(Cone
  PRIVATE
    VTK::FiltersSources
    VTK::InteractionStyle
    VTK::RenderingOpenGL2
    VTK::RenderingUI
)

# -----------------------------------------------------------------------------
# WebAssembly build options
# -----------------------------------------------------------------------------
set(emscripten_link_options)
set(emscripten_compile_options)

list(APPEND emscripten_link_options
  "SHELL:-s WASM=1"
)

set(emscripten_debug_options)
set(DEBUGINFO "PROFILE" CACHE STRING "Type of debug info")
set_property(CACHE DEBUGINFO PROPERTY
  STRINGS
    NONE              # -g0
    READABLE_JS       # -g1
    PROFILE           # -g2
    DEBUG_NATIVE      # -g3
)

if(DEBUGINFO STREQUAL "NONE")
  list(APPEND emscripten_debug_options
    "-g0"
  )
elseif(DEBUGINFO STREQUAL "READABLE_JS")
  list(APPEND emscripten_debug_options
    "-g1"
  )
  list(APPEND emscripten_link_options
    "SHELL:-s DEMANGLE_SUPPORT=1"
  )
elseif(DEBUGINFO STREQUAL "PROFILE")
  list(APPEND emscripten_debug_options
    "-g2"
  )
  list(APPEND emscripten_link_options
    "SHELL:-s DEMANGLE_SUPPORT=1"
  )
elseif(DEBUGINFO STREQUAL "DEBUG_NATIVE")
  list(APPEND emscripten_debug_options
    "-g3"
  )
  list(APPEND emscripten_link_options
    "SHELL:-s ASSERTIONS=1"
    "SHELL:-s DEMANGLE_SUPPORT=1"
  )
endif()

# -----------------------------------------------------------------------------
# Build options
# -----------------------------------------------------------------------------
set(emscripten_optimizations)
set(OPTIMIZE "SMALLEST_WITH_CLOSURE" CACHE STRING "Emscripten optimization")
set_property(CACHE OPTIMIZE PROPERTY
  STRINGS
    NO_OPTIMIZATION       # -O0
    LITTLE                # -O1
    MORE                  # -O2
    BEST                  # -O3
    SMALL                 # -Os
    SMALLEST              # -Oz
    SMALLEST_WITH_CLOSURE # -Oz --closure 1
)

if(OPTIMIZE STREQUAL "NO_OPTIMIZATION")
  list(APPEND emscripten_optimizations
    "-O0"
  )
elseif(OPTIMIZE STREQUAL "LITTLE")
  list(APPEND emscripten_optimizations
    "-O1"
  )
elseif(OPTIMIZE STREQUAL "MORE")
  list(APPEND emscripten_optimizations
    "-O2"
  )
elseif(OPTIMIZE STREQUAL "BEST")
  list(APPEND emscripten_optimizations
    "-O3"
  )
elseif(OPTIMIZE STREQUAL "SMALL")
  list(APPEND emscripten_optimizations
    "-Os"
  )
elseif(OPTIMIZE STREQUAL "SMALLEST")
  list(APPEND emscripten_optimizations
    "-Oz"
  )
elseif(OPTIMIZE STREQUAL "SMALLEST_WITH_CLOSURE")
  list(APPEND emscripten_optimizations
    "-Oz"
  )
  list(APPEND emscripten_link_options
    "--closure 1"
  )
endif()

target_compile_options(Cone
  PUBLIC
    ${emscripten_compile_options}
    ${emscripten_optimizations}
    ${emscripten_debug_options}
)

target_link_options(Cone
  PUBLIC
    ${emscripten_link_options}
    ${emscripten_optimizations}
    ${emscripten_debug_options}
)

### ---------- according vtk-wasm-sdk/.gitlab/ci/docker/tests/basic/CMakeLists.txt -------
target_compile_options(Cone PRIVATE "-sMEMORY64" "-Wno-experimental")
target_link_options(Cone PRIVATE "-sMEMORY64" "-Wno-experimental" "-sMAXIMUM_MEMORY=16GB")
target_compile_options(Cone PRIVATE "-pthread")
target_link_options(Cone PRIVATE "-pthread")
### -------------------------------------------------------------------------------------------------------------

# -----------------------------------------------------------------------------
# VTK modules initialization
# -----------------------------------------------------------------------------

vtk_module_autoinit(
  TARGETS  Cone
  MODULES  ${VTK_LIBRARIES}
)

# -----------------------------------------------------------------------------
# Copy HTML to build directory
# -----------------------------------------------------------------------------

add_custom_command(
  TARGET Cone
  POST_BUILD
  COMMAND
    ${CMAKE_COMMAND} -E copy_if_different
      "${CMAKE_CURRENT_SOURCE_DIR}/index.html"
      $<TARGET_FILE_DIR:Cone>
)

$erroractionpreference = "stop"

$sdk_version="wasm64-threads-v9.3.1-3657-gfd3dc37e8e-20240728"

$sdk_image="kitware/vtk-wasm-sdk"
$sdk_config="Release"

$sdk_dir="/VTK-install/$sdk_config/lib/cmake/vtk"

docker run `
    --rm `
    -v"$pwd/Cone":/Cone `
    ${sdk_image}:${sdk_version} `
    emcmake cmake -GNinja -S /Cone -B /Cone/build -DCMAKE_BUILD_TYPE="$sdk_config" `
    -DVTK_DIR="$sdk_dir" `
# ------------------ add as the table -----------------------------
    -DCMAKE_C_FLAGS="-sMEMORY64=1 -pthread" `
    -DCMAKE_CXX_FLAGS="-sMEMORY64=1 -pthread" `
    -DCMAKE_EXE_LINKER_FLAGS="-sMEMORY64=1 -pthread" `
    -DCMAKE_STATIC_LINKER_FLAGS="-sMEMORY64=1 -pthread"

docker run `
    --rm `
    -v"$pwd/Cone":/Cone `
    ${sdk_image}:${sdk_version} `
    cmake --build /Cone/build

then compilation is completed.
But, when I tried to run the example, show nothing and output error in console:

Cone.js:313 Uncaught ReferenceError: SharedArrayBuffer is not defined
    at Cone.js:313:40
:8080/favicon.ico:1 
        
        
       Failed to load resource: the server responded with a status of 404 (File not found)

What’s the issue? Have any suggestions?

WebAssembly uses SharedArrayBuffer instead of ArrayBuffer to share memory with threads. I assume you started a basic HTTP server.

You are getting this error because SharedArrayBuffer can pose security issues when used maliciously. As a safe measure, browsers require certain cross-origin configuration for security purpose. Try this script instead Local HTTP server with COEP+COOP enabled for SharedArrayBuffer · GitHub

Thank you! @jaswantp
Another question, I tried build the example vtkWasmBenchmarks in the blog by docker image with tag wasm32-v9.3.1-3657-gfd3dc37e8e-20240728.
But compile failed, the cmake error is:

  Found package configuration file:

    /VTK-install/Release/lib/cmake/vtk/vtk-config.cmake

  but it set VTK_FOUND to FALSE so package "VTK" is considered to be NOT
  FOUND.  Reason given by package:

  Could not find the VTK package with the following required components:
  RenderingWebGPU.

I tried docker image with tag v9.3.0-2490-g97c85d6930-202404010, it’s ok.

How can I fix it?

The webgpu module is being worked upon right now, so until things have stabilized, please use the 20240410 docker image.

Hello everyone. I tried the same shell script and CMakeLists.txt as Behtgod.
The sdk_version was “wasm64-threads-v9.3.1-4443-g80d5d9f8cd-20241005” in my case.

And after the Cone.wasm and Cone.js were generated, I started the http server with “python server-cross-origin.py 4001”. When I opened the link “localhost:4001”, I got the following errors:
1)wasm streaming compile failed: CompileError: WebAssembly.instantiateStreaming(): invalid memory limits flags 0x7 (enable with --experimental-wasm-memory64) @+4135
2)falling back to ArrayBuffer instantiation
…

Enable wasm64 support in your browser. Go to chrome://flags and search for memory64

1 Like