Skip to content

Commit 810316b

Browse files
perseoGIczoidomemsharded
authored
Added Emscripten documentation (#4115)
* Added emscripten documentation * Applied suggestions * Improved emscripten docs * Added extra documentation * Added threads model * Removed emscripten examples section as examples2 PR not merged yet * Applied suggestions * Removed note mark * Update examples/cross_build/emscripten.rst Co-authored-by: James <[email protected]> --------- Co-authored-by: Carlos Zoido <[email protected]> Co-authored-by: James <[email protected]>
1 parent 8555f41 commit 810316b

File tree

6 files changed

+158
-0
lines changed

6 files changed

+158
-0
lines changed

examples/cross_build.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ Cross-building examples
99
cross_build/toolchain_packages
1010
cross_build/android/ndk
1111
cross_build/android/android_studio
12+
cross_build/emscripten
1213
cross_build/tricore
1314
cross_build/linux_to_windows_mingw
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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.
18.8 KB
Loading

integrations.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Xcode.
2222
integrations/makefile
2323
integrations/xcode
2424
integrations/meson
25+
integrations/emscripten
2526
integrations/premake
2627
integrations/android
2728
integrations/jfrog

integrations/emscripten.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.. _integrations_emscripten:
2+
3+
|emscripten_logo| Emscripten
4+
============================
5+
6+
Conan provides support for cross-building for both `asm.js <http://asmjs.org>`_ and `WASM <https://webassembly.org>`_ (Web Assembly) targets
7+
using `Emscripten <https://emscripten.org>`__. This enables developers to compile C/C++ code for the browser
8+
and other JavaScript environments.
9+
10+
For detailed examples and step-by-step instructions, refer to:
11+
12+
- :ref:`Cross-building with Emscripten <examples_cross_build_emscripten>`
13+
14+
.. |emscripten_logo| image:: ../images/integrations/conan-emscripten-logo.png

reference/tools/cmake/cmaketoolchain.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,9 @@ If for some reason using absolute paths was desired, it is possible to do it wit
364364
tc.generate()
365365
366366
367+
368+
.. _conan_cmake_user_toolchain:
369+
367370
Using a custom toolchain file
368371
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
369372

0 commit comments

Comments
 (0)