Skip to content

Commit 7bb29f6

Browse files
committed
Add section on building for Emscripten
Heavily copied from Brett's WASI guide.
1 parent f4df9cc commit 7bb29f6

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

getting-started/setup-building.rst

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,83 @@ used in ``python.sh``:
458458
.. _wasmtime: https://wasmtime.dev
459459
.. _WebAssembly: https://webassembly.org
460460

461+
462+
Emscripten
463+
----------
464+
465+
Emscripten_ is a complete open source compiler toolchain. It compiles C/C++ code
466+
into WebAssembly_/JavaScript executables, for use in JavaScript runtimes,
467+
including browsers and Node.js.
468+
469+
.. note::
470+
471+
The instructions below assume a Unix-based OS due to cross-compilation for
472+
CPython being designed for ``./configure`` / ``make``.
473+
474+
To build for Emscripten, you will need to cross-compile CPython. This requires a
475+
C compiler just like building for :ref:`Unix <unix-compiling>` as well as:
476+
477+
1. The Emscripten compiler
478+
2. Node.js
479+
480+
Building for Emscripten requires doing a cross-build where you have a *build*
481+
Python to help produce a Emscripten build of CPython. This means you build
482+
CPython twice: once to have a version of Python for the build system to use and
483+
another that's the build you ultimately care about (that is, the build Python is
484+
not meant for use by you directly, only the build system).
485+
486+
The easiest way to get a debug build of CPython for Emscripten is to use the
487+
``Tools/wasm/emscripten build`` command (which should be run with a recent
488+
version of Python you have installed on your machine):
489+
490+
.. code-block:: shell
491+
492+
$ python3 Tools/wasm/emscripten build --quiet -- --config-cache --with-pydebug
493+
494+
That single command will configure and build both the build Python and the
495+
Emscripten build in ``cross-build/build`` and
496+
``cross-build/wasm32-emscripten/build/python/``, respectively.
497+
498+
You can also do each configuration and build step separately; the command above
499+
is a convenience wrapper around the following commands:
500+
501+
.. code-block:: shell
502+
503+
$ python Tools/wasm/emscripten configure-build-python --quiet -- --config-cache --with-pydebug
504+
$ python Tools/wasm/emscripten make-build-python --quiet
505+
$ python Tools/wasm/emscripten make-libffi --quiet
506+
$ python Tools/wasm/emscripten configure-host --quiet -- --config-cache
507+
$ python Tools/wasm/emscripten make-host --quiet
508+
509+
.. note::
510+
511+
The ``configure-host`` command infers the use of ``--with-pydebug`` from the
512+
build Python.
513+
514+
Running the separate commands after ``emscripten build`` is useful if you, for example, only want to
515+
run the ``make-host`` step after making code changes.
516+
517+
Once everything is complete, there will be a
518+
``cross-build/wasm32-emscripten/build/python/python.sh`` helper file which you
519+
can use to run the ``python.mjs`` file:
520+
521+
.. code-block:: shell
522+
523+
$ cross-build/wasm32-emscripten/build/python/python.sh --version
524+
525+
You can also use ``Makefile`` targets and they will work as expected thanks to
526+
the ``HOSTRUNNER`` environment variable having been set to a similar value as
527+
used in ``python.sh``:
528+
529+
.. code-block:: shell
530+
531+
$ make -C cross-build/wasm32-emscripten/build/python/ test
532+
533+
534+
.. _Emscripten: https://wasi.dev
535+
.. _WebAssembly: https://webassembly.org
536+
537+
461538
Android
462539
-------
463540

0 commit comments

Comments
 (0)