integrate emscripten to complex cmake project

1,177 views
Skip to first unread message

Nir Azkiel

unread,
Jul 15, 2021, 3:54:47 AM7/15/21
to emscripten-discuss
Hi,

I would like to integrate emscripten and to build a library compiled to WASM on a complex C++ project.

The best flow for me will be integrated it on the main flow on the cmake configure, meaning: if a user set a flag to true (let say BUILD_TO_WASM=true) than the cmake will be built as WASM, otherwise it will build with the default OS toolchain.

I can't find much documantation on how to integrate emscripten into cmake (I want to avoid using emconfigure/emmake)

How can it be done?
"ExternalProject_Add"?

any reference for such a build?

Thanks
Nir

Floh

unread,
Jul 15, 2021, 5:39:15 AM7/15/21
to emscripten-discuss
The "easy way" is to use the emcmake wrapper that comes with the Emscripten SDK, AFAIK what this does is invoke CMake with an Emscripten specific toolchain file (which lives here: https://github.com/emscripten-core/emscripten/blob/main/cmake/Modules/Platform/Emscripten.cmake).

I have an example for this here (see the emscripten build instructions in the readme, and checkout the CMakeLists.txt file which has some emscripten specific settings):


The next option is to skip the emcmake wrapper and use the SDK's toolchain file, or your own variation of this toolchain file. The starting point is to run cmake giving it emscripten's toolchain file:

cmake -DCMAKE_TOOLCHAIN_FILE=[path to emsdk toolchain file] ..

AFAIK including a toolchain file in the CMakeLists.txt file based on a CMake option suffers from a chicken-egg situation, it's more robust to set the toolchain file as part of the command line invocation.

This is basically a "cmake crosscompiling scenario" with a custom toolchain file (as opposed to cmake's builtin cross compiling targets). CMake's documentation for this is here:

Cheers!

Floh

unread,
Jul 15, 2021, 5:43:42 AM7/15/21
to emscripten-discuss
PS: AFAIK what's not possible with cmake is to do a "mixed build" where some parts of the build are compiled to WASM and some parts are compiled as native (or a different cross-compilation target). This is a general limitation of cmake (again - only as far as I know - maybe there's a way around the limitation).
Reply all
Reply to author
Forward
0 new messages