emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.75k stars 3.3k forks source link

Emscripten CMake toolchain generates .js output when building a SIDE_MODULE, would expect only .wasm #17245

Open Sakari369 opened 2 years ago

Sakari369 commented 2 years ago

I am building a wasm only sidemodule using the following emscripten link flags in my CMakeLists.txt:

set(EMSCRIPTEN_LINK_FLAGS "-s WASM=1 -s SIDE_MODULE=2 -v")

I am generating the makefiles using this command:

emcmake cmake -DCMAKE_TOOLCHAIN_FILE=/opt/homebrew/Cellar/emscripten/3.1.8/libexec/cmake/Modules/Platform/Emscripten.cmake -DCMAKE_BUILD_TYPE=Debug -G "Unix Makefiles" ../

When building the project with make, I would expect the sidemodule to compile to .wasm, not to a .js file. This is the warning that the linking phase also gives:

em++: warning: output suffix .js requested, but wasm side modules are just wasm files; emitting only a .wasm, no .js [-Wemcc]

Please include the following in your bug report:

Version of emscripten/emsdk: emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.8-git clang version 15.0.0 (https://github.com/llvm/llvm-project.git 80ec0ebfdc5692a58e0832125f2c6a991df9d63f) Target: wasm32-unknown-emscripten Thread model: posix InstalledDir: /opt/homebrew/Cellar/emscripten/3.1.8/libexec/llvm/bin

Failing command line in full:

opt/homebrew/Cellar/cmake/3.22.3/bin/cmake -E cmake_link_script CMakeFiles/test_sidemodule.dir/link.txt --verbose=1
/opt/homebrew/Cellar/emscripten/3.1.8/libexec/em++ -g -s STANDALONE_WASM=1 -s WASM=1 -s SIDE_MODULE=2 @CMakeFiles/test_sidemodule.dir/objects1.rsp -o ../lib/test_sidemodule.js
em++: warning: output suffix .js requested, but wasm side modules are just wasm files; emitting only a .wasm, no .js [-Wemcc]
[100%] Built target test_sidemodule
/opt/homebrew/Cellar/cmake/3.22.3/bin/cmake -E cmake_progress_start /Users/sakari/dvl/sumo/mandala/testing/test-omg-core/sidemodule/build_dist/CMakeFiles 0

Full link command and output with -v appended:

[100%] Linking CXX executable ../lib/test_sidemodule.js
em++: warning: output suffix .js requested, but wasm side modules are just wasm files; emitting only a .wasm, no .js [-Wemcc]
 "/opt/homebrew/Cellar/emscripten/3.1.8/libexec/llvm/bin/wasm-ld" -o ../lib/test_sidemodule.js CMakeFiles/test_sidemodule.dir/src/main.cpp.o CMakeFiles/test_sidemodule.dir/src/lerp.cpp.o -L/opt/homebrew/Cellar/emscripten/3.1.8/libexec/cache/sysroot/lib/wasm32-emscripten/pic /opt/homebrew/Cellar/emscripten/3.1.8/libexec/cache/sysroot/lib/wasm32-emscripten/pic/crt1.o -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --import-undefined --import-memory --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=setThrew --export-if-defined=emscripten_stack_get_end --export-if-defined=emscripten_stack_get_free --export-if-defined=emscripten_stack_get_base --export-if-defined=emscripten_stack_set_limits --export-if-defined=stackSave --export-if-defined=stackRestore --export-if-defined=stackAlloc --export-if-defined=__errno_location --export-if-defined=malloc --export-if-defined=free --experimental-pic -shared --no-export-dynamic
 "/opt/homebrew/Cellar/emscripten/3.1.8/libexec/binaryen/bin/wasm-emscripten-finalize" -g --dyncalls-i64 --no-legalize-javascript-ffi --side-module --standalone-wasm --dwarf ../lib/test_sidemodule.js -o ../lib/test_sidemodule.js --detect-features
 "/opt/homebrew/Cellar/emscripten/3.1.8/libexec/llvm/bin/llvm-objcopy" ../lib/test_sidemodule.js ../lib/test_sidemodule.js --remove-section=producers
Sakari369 commented 2 years ago

Looking at the CMAKE toolchain file (/opt/homebrew/Cellar/emscripten/3.1.8/libexec/cmake/Modules/Platform/Emscripten.cmake), I can see the line

set(CMAKE_EXECUTABLE_SUFFIX ".js")

In it. Specifying this in my own CMakeLists.txt fixes the issue:

set(CMAKE_EXECUTABLE_SUFFIX ".wasm")

But I would expect if compiling a sidemodule, that this would be automatically set to '.wasm'.

sbc100 commented 2 years ago

If you can figure out way to get Emscripten.cmake to do the right thing that would be great. Another thing that is wrong here is that a side module is not really an executable so it shouldn't be using CMAKE_EXECUTABLE_SUFFIX at all or saying Linking CXX executable. Side module is more of a shared library than an executable.

Sakari369 commented 2 years ago

If you can figure out way to get Emscripten.cmake to do the right thing that would be great. Another thing that is wrong here is that a side module is not really an executable so it shouldn't be using CMAKE_EXECUTABLE_SUFFIX at all or saying Linking CXX executable. Side module is more of a shared library than an executable.

I tried building it as a library, but this will output an .a file, which cannot be loaded from the browser directly, as the browser expects a .wasm file.

So seems building as an executable with a .wasm target is still the right way to build a sidemodule ?