cmakeemscriptenconanconan-2

How to use CMake with Emscripten and Conan 2


I am going mad trying to get CMake, Emscripten and Conan 2 playing together.

First thing I tried was just using conan-cmake's develop2 branch, which supports conan 2 but fails with setting unknown compiler versions to the generated profile file. So I decided to try without conan-cmake.

The project is built using emcmake cmake .. && cmake --build . from the build directory, after activating Emscripten, sourcing their env script. Everything works as long there is non conan.

The problem is, that conan want's me to use their generated CMake toolchain file. But as soon as I do, the one set by emcmake is not used anymore. Setting both manually does not work, too.

There is no documentation from the conan 2 developers regarding Emscripten. Only thing I could find was some Conan 1.x docs. Is Conan 2 totally unusable for anything not yet totally simple or am I doing something wrong?


Solution

  • I got it working the following way:

    emscripten.profile

    [settings]
    os=Emscripten
    arch=wasm
    compiler=clang
    compiler.version=15
    compiler.libcxx=libc++
    build_type=Release
    
    [tool_requires]
    emsdk/3.1.50
    

    Installing via conan install:

    conan install . -pr:b default -pr:h emscripten.profile -s build_type=Release -b missing -of build
    

    This outputs a conan_toolchain.cmake file into the build directory, which sets up CMake for Conan with Emscripten.

    Running CMake using the generated Toolchain file:

    cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
    

    It was pretty unclear to me how to use Emscripten, if Conan already wants you to set a CMAKE_TOOLCHAIN_FILE. The Conan documentation for version 1.x includes notes about Emscripten, while the version 2.x documentation does not.