Skip to content

Commit 9c257f9

Browse files
hoodmanehugovk
andauthored
Add section on building for Emscripten (#1533)
Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent 6ea7bd6 commit 9c257f9

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

getting-started/setup-building.rst

+91
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,97 @@ used in ``python.sh``:
467467
.. _wasmtime: https://wasmtime.dev
468468
.. _WebAssembly: https://webassembly.org
469469

470+
471+
Emscripten
472+
----------
473+
474+
Emscripten_ is a complete open-source compiler toolchain. It compiles C/C++ code
475+
into WebAssembly_/JavaScript executables, for use in JavaScript runtimes,
476+
including browsers and Node.js.
477+
478+
.. note::
479+
480+
The instructions below assume a Unix-based OS due to cross-compilation for
481+
CPython being designed for ``./configure`` / ``make``.
482+
483+
To build for Emscripten, you will need to cross-compile CPython. This requires a
484+
C compiler just like building for :ref:`Unix <unix-compiling>` as well as:
485+
486+
* The Emscripten compiler
487+
* Node.js
488+
489+
The simplest way to install the Emscripten compiler is:
490+
491+
.. code-block:: sh
492+
493+
# Install Emscripten
494+
git clone https://github.com/emscripten-core/emsdk
495+
./emsdk/emsdk install 4.0.5
496+
./emsdk/emsdk activate 4.0.5
497+
source ./emsdk/emsdk_env.sh
498+
499+
Updating the Emscripten compiler version often causes breakages. For the best
500+
compatibility, use the Emscripten version suggested in the cpython repository in
501+
``Tools/wasm/README.md``.
502+
503+
Building for Emscripten requires doing a cross-build where you have a *build*
504+
Python to help produce an Emscripten build of CPython. This means you build
505+
CPython twice: once to have a version of Python for the build system to use and
506+
another that's the build you ultimately care about (that is, the build Python is
507+
not meant for use by you directly, only the build system).
508+
509+
The easiest way to get a debug build of CPython for Emscripten is to use the
510+
``Tools/wasm/emscripten build`` command (which should be run with a recent
511+
version of Python you have installed on your machine):
512+
513+
.. code-block:: shell
514+
515+
python3 Tools/wasm/emscripten build --quiet -- --config-cache --with-pydebug
516+
517+
That single command will configure and build both the build Python and the
518+
Emscripten build in ``cross-build/build`` and
519+
``cross-build/wasm32-emscripten/build/python/``, respectively.
520+
521+
You can also do each configuration and build step separately; the command above
522+
is a convenience wrapper around the following commands:
523+
524+
.. code-block:: shell
525+
526+
python Tools/wasm/emscripten configure-build-python --quiet -- --config-cache --with-pydebug
527+
python Tools/wasm/emscripten make-build-python --quiet
528+
python Tools/wasm/emscripten make-libffi --quiet
529+
python Tools/wasm/emscripten configure-host --quiet -- --config-cache
530+
python Tools/wasm/emscripten make-host --quiet
531+
532+
.. note::
533+
534+
The ``configure-host`` command infers the use of ``--with-pydebug`` from the
535+
build Python.
536+
537+
Running the separate commands after ``emscripten build`` is useful if you, for
538+
example, only want to run the ``make-host`` step after making code changes.
539+
540+
Once everything is complete, there will be a
541+
``cross-build/wasm32-emscripten/build/python/python.sh`` helper file which you
542+
can use to run the ``python.mjs`` file:
543+
544+
.. code-block:: shell
545+
546+
cross-build/wasm32-emscripten/build/python/python.sh --version
547+
548+
You can also use ``Makefile`` targets and they will work as expected thanks to
549+
the ``HOSTRUNNER`` environment variable having been set to a similar value as
550+
used in ``python.sh``:
551+
552+
.. code-block:: shell
553+
554+
make -C cross-build/wasm32-emscripten/build/python/ test
555+
556+
557+
.. _Emscripten: https://emscripten.org/
558+
.. _WebAssembly: https://webassembly.org
559+
560+
470561
Android
471562
-------
472563

0 commit comments

Comments
 (0)