Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions getting-started/setup-building.rst
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,93 @@ used in ``python.sh``:
.. _wasmtime: https://wasmtime.dev
.. _WebAssembly: https://webassembly.org


Emscripten
----------

Emscripten_ is a complete open source compiler toolchain. It compiles C/C++ code
Comment thread
hoodmane marked this conversation as resolved.
Outdated
into WebAssembly_/JavaScript executables, for use in JavaScript runtimes,
including browsers and Node.js.

.. note::

The instructions below assume a Unix-based OS due to cross-compilation for
CPython being designed for ``./configure`` / ``make``.

To build for Emscripten, you will need to cross-compile CPython. This requires a
C compiler just like building for :ref:`Unix <unix-compiling>` as well as:

1. The Emscripten compiler
2. Node.js
Comment thread
hoodmane marked this conversation as resolved.
Outdated

The simplest way to install the Emscripten compiler is as follows:
Comment thread
hoodmane marked this conversation as resolved.
Outdated

.. code-block:: sh

# Install Emscripten
git clone emscripten/emsdk
Comment thread
hoodmane marked this conversation as resolved.
Outdated
./emsdk/emsdk install 4.0.5
./emsdk/emsdk activate 4.0.5
Comment thread
hoodmane marked this conversation as resolved.
source ./emsdk/emsdk_env.sh

Building for Emscripten requires doing a cross-build where you have a *build*
Python to help produce a Emscripten build of CPython. This means you build
Comment thread
hoodmane marked this conversation as resolved.
Outdated
CPython twice: once to have a version of Python for the build system to use and
another that's the build you ultimately care about (that is, the build Python is
not meant for use by you directly, only the build system).
Comment thread
hoodmane marked this conversation as resolved.

The easiest way to get a debug build of CPython for Emscripten is to use the
``Tools/wasm/emscripten build`` command (which should be run with a recent
version of Python you have installed on your machine):

.. code-block:: shell

$ python3 Tools/wasm/emscripten build --quiet -- --config-cache --with-pydebug
Comment thread
hoodmane marked this conversation as resolved.
Outdated

That single command will configure and build both the build Python and the
Emscripten build in ``cross-build/build`` and
``cross-build/wasm32-emscripten/build/python/``, respectively.

You can also do each configuration and build step separately; the command above
is a convenience wrapper around the following commands:

.. code-block:: shell

$ python Tools/wasm/emscripten configure-build-python --quiet -- --config-cache --with-pydebug
$ python Tools/wasm/emscripten make-build-python --quiet
$ python Tools/wasm/emscripten make-libffi --quiet
$ python Tools/wasm/emscripten configure-host --quiet -- --config-cache
$ python Tools/wasm/emscripten make-host --quiet
Comment thread
hoodmane marked this conversation as resolved.
Outdated

.. note::

The ``configure-host`` command infers the use of ``--with-pydebug`` from the
build Python.

Running the separate commands after ``emscripten build`` is useful if you, for example, only want to
run the ``make-host`` step after making code changes.
Comment thread
hoodmane marked this conversation as resolved.
Outdated

Once everything is complete, there will be a
``cross-build/wasm32-emscripten/build/python/python.sh`` helper file which you
can use to run the ``python.mjs`` file:

.. code-block:: shell

$ cross-build/wasm32-emscripten/build/python/python.sh --version
Comment thread
hoodmane marked this conversation as resolved.
Outdated

You can also use ``Makefile`` targets and they will work as expected thanks to
the ``HOSTRUNNER`` environment variable having been set to a similar value as
used in ``python.sh``:

.. code-block:: shell

$ make -C cross-build/wasm32-emscripten/build/python/ test
Comment thread
hoodmane marked this conversation as resolved.
Outdated


.. _Emscripten: https://emscripten.org/
.. _WebAssembly: https://webassembly.org


Android
-------

Expand Down
Loading