|
| 1 | +.. _examples_cross_build_emscripten: |
| 2 | + |
| 3 | +Cross-building with Emscripten - WebAssembly and asm.js |
| 4 | +======================================================= |
| 5 | + |
| 6 | +This example demonstrates how to cross-build a simple C++ project using Emscripten and Conan. |
| 7 | + |
| 8 | +Conan supports `WASM <https://webassembly.org>`_ cross compilation, giving you the flexibility to target different |
| 9 | +JavaScript/WebAssembly runtimes in the browser. |
| 10 | + |
| 11 | +We recommend creating separate Conan profiles for each target. Below are |
| 12 | +recommended profiles and instructions on how to build with them. |
| 13 | + |
| 14 | + |
| 15 | +Setting up Conan profile for WebAssembly (WASM) |
| 16 | +----------------------------------------------- |
| 17 | + |
| 18 | +.. code-block:: text |
| 19 | +
|
| 20 | + [settings] |
| 21 | + arch=wasm |
| 22 | + build_type=Release |
| 23 | + compiler=emcc |
| 24 | + compiler.cppstd=17 |
| 25 | + compiler.libcxx=libc++ |
| 26 | + # Optional settings to enable multithreading (see note below) |
| 27 | + # compiler.threads=posix |
| 28 | + compiler.version=4.0.10 |
| 29 | + os=Emscripten |
| 30 | +
|
| 31 | + [tool_requires] |
| 32 | + emsdk/4.0.10 |
| 33 | +
|
| 34 | + [conf] |
| 35 | + # Optional settings to enable memory allocation |
| 36 | + tools.build:exelinkflags=['-sALLOW_MEMORY_GROWTH=1', '-sMAXIMUM_MEMORY=4GB', '-sINITIAL_MEMORY=64MB'] |
| 37 | + tools.build:sharedlinkflags=['-sALLOW_MEMORY_GROWTH=1', '-sMAXIMUM_MEMORY=4GB', '-sINITIAL_MEMORY=64MB'] |
| 38 | +
|
| 39 | +
|
| 40 | +.. note:: |
| 41 | + |
| 42 | + Conan also supports building for `asm.js <http://asmjs.org>`_ targets, which is a nowadays considered deprecated. |
| 43 | + |
| 44 | + What’s the difference between asm.js and WASM? |
| 45 | + |
| 46 | + - **asm.js** is a subset of JavaScript optimized for speed. It is fully supported by all browsers (even older ones) and compiles to a large ``.js`` file. |
| 47 | + - **WebAssembly (WASM)** is a binary format that is smaller and faster to load and execute. Most modern browsers support it, and it is generally recommended for new projects. **WASM** is also easier to integrate with native browser APIs compared to **asm.js**. |
| 48 | + |
| 49 | + |
| 50 | +Even though Emscripten is not a true runtime environment (like Linux or |
| 51 | +Windows), it is part of a toolchain ecosystem that compiles C/C++ to |
| 52 | +WebAssembly (WASM) and asm.js. |
| 53 | + |
| 54 | +Conan uses ``os=Emscripten`` to: |
| 55 | + |
| 56 | +- Align with the toolchain: Emscripten integrates the compiler, runtime glue, and JavaScript environment, making it practical to treat as an "OS-like" target. |
| 57 | + |
| 58 | +- Support backward compatibility: Many recipes in Conan Center Index use ``os=Emscripten`` to enable or disable features and dependencies that specifically target Emscripten. |
| 59 | + |
| 60 | +- Maintain stability: Changing this setting would break recipes that rely on it, and would complicate compatibility with alternative WASM toolchains. |
| 61 | + |
| 62 | + |
| 63 | +.. note:: |
| 64 | + |
| 65 | + ``wasm`` arch refers to ``WASM 32-bit`` target architecture, which is the |
| 66 | + default. If you wish to target ``WASM64``, set ``arch=wasm64`` in your profile. |
| 67 | + **Note that WASM64 is still experimental** and requires Node.js v20+ and a browser that supports it. |
| 68 | + |
| 69 | +.. important:: |
| 70 | + |
| 71 | + According to `emscripten documentation <https://emscripten.org/docs/api_reference/wasm_workers.html>`_ Emscripten supports two multithreading APIs: |
| 72 | + |
| 73 | + - POSIX Threads API (``posix`` in conan profile) |
| 74 | + - Wasm Workers API (``wasm_workers`` in conan profile) |
| 75 | + |
| 76 | + These two APIs are incompatible with each other and incompatibles with binaries compiled without threading support. |
| 77 | + This incompatibility necessitates the modeling of threading usage within |
| 78 | + the compiler's binary model, allowing conan to distinguish between binaries |
| 79 | + compiled with threading and those compiled without it. |
| 80 | + |
| 81 | + Conan will automatically set compiler and linker flags to enable threading if configured in the profile. |
| 82 | + |
| 83 | + |
| 84 | +The profiles above use the ``emsdk`` package from `Conan Center Index repository <https://conan.io/center/recipes/emsdk>`_, which provides the Emscripten SDK, including ``emcc``, ``em++``, and tools like ``emrun`` and ``node``. |
| 85 | + |
| 86 | +If you prefer to use your system-installed Emscripten instead of the Conan-provided one, ``tool_requires`` could be replaced by custom ``compiler_executables`` and ``buildenv``: |
| 87 | + |
| 88 | +.. code-block:: text |
| 89 | +
|
| 90 | + [conf] |
| 91 | + tools.build:compiler_executables={'c':'/path/to/emcc', 'cpp':'/path/to/em++'} |
| 92 | +
|
| 93 | + [buildenv] |
| 94 | + CC=emcc |
| 95 | + CXX=em++ |
| 96 | + AR=emar |
| 97 | + NM=emnm |
| 98 | + RANLIB=emranlib |
| 99 | + STRIP=emstrip |
| 100 | +
|
| 101 | +
|
| 102 | +This way conan could configure `emsdk` local installation to be used from `CMake`, `Meson`, `Autotools` or other build systems. |
| 103 | + |
| 104 | +In some cases, you might also need the ``Emscripten.cmake`` toolchain file |
| 105 | +for advanced scenarios. This toolchais is already added in our packaged |
| 106 | +`emsdk` but if you are using your own Emscripten installation, you can |
| 107 | +specify it in the profile by using |
| 108 | +:ref:`tools.cmake.cmaketoolchain:user_toolchain<conan_cmake_user_toolchain>` |
| 109 | +and providing the absolute path to your toolchain file. |
| 110 | + |
| 111 | +.. note:: |
| 112 | + |
| 113 | + The ``tools.build:exelinkflags`` and ``tools.build:sharedlinkflags`` in |
| 114 | + previous profiles are recomendations but users can modify them or define |
| 115 | + their values in the CMakeLists.txt file using the |
| 116 | + ``set_target_properties()`` command. |
| 117 | + |
| 118 | + - By enabling ``ALLOW_MEMORY_GROWTH`` we allow the runtime to grow its |
| 119 | + memory dynamically at runtime by calling ``emscripten_resize_heap()``. Without |
| 120 | + this flag, memory is allocated at startup and cannot grow. |
| 121 | + |
| 122 | + - The ``MAXIMUM_MEMORY`` and ``INITIAL_MEMORY`` values specifies the maximum |
| 123 | + and initial memory size for the Emscripten runtime. These values can be |
| 124 | + adjusted based on your application's needs. |
| 125 | + |
| 126 | + Take into account that ``arch=wasm64`` has a theorical exabytes maximum |
| 127 | + memory size, but runtime currently limits it to 16GB, while ``arch=wasm32`` |
| 128 | + has a maximum memory size of 4GB and ``arch=asm.js`` has a maximum memory size of 2GB. |
| 129 | + |
| 130 | + |
| 131 | +.. important:: |
| 132 | + |
| 133 | + ``emcc`` compiler does not guarantee any ABI compatibility between different versions (patches included) |
| 134 | + To ensure a new ``package_id`` is generated when the Emscripten version |
| 135 | + changes, it is recommended to update the ``compiler.version`` setting in your profile accordingly. |
| 136 | + |
| 137 | + This will ensure that the package ID is generated based on the Emscripten |
| 138 | + version, allowing Conan to detect changes in the Emscripten toolchain and |
| 139 | + rebuild the project accordingly. |
0 commit comments