Skip to content

CMake SHARED library with Emscripten toolchain is not supported #17804

Open

Description

@Hazardu

Im using CMAKE and Visual Studio 2022 as the generator with the following CMakeLists.txt


set(SOURCE_FILES window.cpp callbacks.cpp main.cpp) 

set(RENDERER_FILES renderer.cpp renderer.h)

add_library(Renderer SHARED ${RENDERER_FILES})

add_executable(EmscriptenHTML5 ${SOURCE_FILES})

set_target_properties(Renderer PROPERTIES 
	COMPILE_FLAGS "-fPIC"
    LINK_FLAGS "-sSIDE_MODULE=1 -sUSE_WEBGL2=1 -sFULL_ES3=1 -sWASM=1 -sWASM_BIGINT -sMIN_WEBGL_VERSION=2 -sASYNCIFY -sEXPORT_ALL=1 --no-entry -fPIC"
    SUFFIX ".wasm"
)
set_target_properties(EmscriptenHTML5 PROPERTIES 
	COMPILE_FLAGS "-fPIC"
    LINK_FLAGS "-sMAIN_MODULE=1 -sUSE_WEBGL2=1 -sFULL_ES3=1 -sWASM=1 -sWASM_BIGINT -sMIN_WEBGL_VERSION=2 -sASYNCIFY -fPIC" 
    SUFFIX ".html"
)
add_dependencies(EmscriptenHTML5 Renderer)
target_link_libraries(EmscriptenHTML5 Renderer)

Toolchain file is defined in CMakeSettings.json (set to emscripten).
I want to use dynamic linking at loadtime.

The resulting Renderer shared library project is treated and compiled as a static library.
In Visual Studio opening a CMake project the Solution Explorer displays the following
image

Verbose output commands:
The verbose output command for linking Renderer uses emar

  [5/6] cmd.exe /C "cd . && "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E rm -f libRenderer.wasm && D:\Hazard\Repos\emsdk\upstream\emscripten\emar.bat qc libRenderer.wasm  CMakeFiles/Renderer.dir/renderer.cpp.o && D:\Hazard\Repos\emsdk\upstream\emscripten\emranlib.bat libRenderer.wasm && cd ."

Version of emscripten/emsdk:
3.1.20

While configuring CMAKE this dev warning is displayed

  ADD_LIBRARY called with SHARED option but the target platform does not
  support dynamic linking.  Building a STATIC library instead.  This may lead
  to problems.
This warning is for project developers.  Use -Wno-dev to suppress it.

Activity

  1. sbc100 commented on Sep 6, 2022

    @sbc100
    Collaborator

    Emscripten support for dynamic linking doesn't yet match what cmake expects so you ca't build and use SHARED libraries in the normal way.

    Until we fix this you might be able to get it working by building your renderer library as an executable with the -sSIDE_MODULE link flag and then adding the resulting wasm file to the link command for the main executable.

  2. Hazardu commented on Sep 6, 2022

    @Hazardu
    Author

    target_link_libraries used in CMake does not allow linking executables to executables, so the workaround does not work.
    However, the solution proposed in #16281 works, and it makes the cmake a whole lot cleaner.

  3. sbc100 commented on Sep 6, 2022

    @sbc100
    Collaborator

    While target_link_libraries doen't work with executables, I think can explicitly add a dependency between one executable and another and then add it to link flags just like any other linker flag.

    I agree that #16281 would be great in the long term, but if you want something that works to today that isn't an option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions