Skip to content

Commit 8f9388a

Browse files
authored
Update documentation on using emscripten ports. (#26494)
This is general re-write, but I also added an explicit note about how ports can become generically available one they have been used once (since they are installed into the shared sysroot). See #26467
1 parent c624110 commit 8f9388a

File tree

1 file changed

+49
-37
lines changed

1 file changed

+49
-37
lines changed

site/source/docs/compiling/Building-Projects.rst

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -214,52 +214,60 @@ For example, consider the case where a project "project" uses a library "libstuf
214214
Emscripten Ports
215215
================
216216

217-
Emscripten Ports is a collection of useful libraries, ported to Emscripten. They reside `on GitHub <https://github.com/emscripten-ports>`_, and have integration support in *emcc*. When you request that a port be used, emcc will fetch it from the remote server, set it up and build it locally, then link it with your project, add necessary include to your build commands, etc. For example, SDL2 is in ports, and you can request that it be used with ``--use-port=sdl2``. For example,
217+
Emscripten Ports is a collection of useful libraries, ported to Emscripten. They
218+
reside in the `ports directory`_, and have integration with *emcc*. When you
219+
request that a port be used, emcc will download, build and install it into the
220+
emscripten sysroot. For example, to use the SDL2 port in your project you would
221+
simply add ``--use-port=sdl2`` to your compiler and linker flags. For example:
218222

219223
.. code-block:: bash
220224
221225
emcc test/browser/test_sdl2_glshader.c --use-port=sdl2 -sLEGACY_GL_EMULATION -o sdl2.html
222226
223-
You should see some notifications about SDL2 being used, and built if it wasn't previously. You can then view ``sdl2.html`` in your browser.
227+
If this if your first time using a given port you may see some notifications about
228+
it being downloaded and installed as your program is being compiled.
224229

225230
To see a list of all available ports, run ``emcc --show-ports``.
226231

227-
.. note:: *SDL_image* has also been added to ports, use it with
228-
``--use-port=sdl2_image``. For ``sdl2_image`` to be useful, you generally
229-
need to specify the image formats you are planning on using with e.g.
230-
``--use-port=sdl2_image:formats=bmp,png,xpm,jpg``. This will also ensure that
231-
``IMG_Init`` works properly when you specify those formats. Alternatively,
232-
you can use ``emcc --use-preload-plugins`` and ``--preload-file`` your
233-
images, so the browser codecs decode them (see :ref:`preloading-files`).
234-
A code path in the ``sdl2_image`` port will load through
235-
:c:func:`emscripten_get_preloaded_image_data`, but then your calls to
236-
``IMG_Init`` with those image formats will fail (as while the images will
237-
work through preloading, IMG_Init reports no support for those formats, as
238-
it doesn't have support compiled in - in other words, ``IMG_Init`` does not
239-
report support for formats that only work through preloading).
240-
241-
.. note:: *SDL_net* has also been added to ports, use it with ``--use-port=sdl2_net``.
242-
243-
.. note:: Emscripten also has support for older SDL1, which is built-in.
244-
If you do not specify SDL2 as in the command above, then SDL1 is linked in
245-
and the SDL1 include paths are used. SDL1 has support for *sdl-config*,
246-
which is present in `system/bin <https://github.com/emscripten-core/emscripten/blob/main/system/bin/sdl-config>`_.
247-
Using the native *sdl-config* may result in compilation or missing-symbol errors.
248-
You will need to modify the build system to look for files in
249-
**emscripten/system** or **emscripten/system/bin** in order to use the
250-
Emscripten *sdl-config*.
251-
252-
.. note:: You can also build a library from ports in a manual way if you prefer
253-
that, but then you will need to also apply the python logic that ports does.
254-
That code (under ``tools/ports/``) may do things like ensure necessary JS
255-
functions are included in the build, add exports, and so forth. In general,
256-
it's better to use the ports version as it is what is tested and known to
257-
work.
232+
You can also use the standalone ``embuilder`` tools to explicitly build ports
233+
prior to running the compiler. For example, you can build SDL2 using
234+
``./embuilder build sdl2``. See ``embuilder --help`` for more information,
235+
including a list of all available targets.
258236

259-
.. note:: Since emscripten 3.1.54, ``--use-port`` is the preferred syntax to
260-
use a port in your project. The legacy syntax (for example ``-sUSE_SDL2``,
261-
``-sUSE_SDL_IMAGE=2``) remains available.
237+
Some ports take extra options. For example, when using the ``sdl2_image`` ports
238+
you can specify a list of image formats. e.g.
239+
``--use-port=sdl2_image:formats=bmp,png,xpm,jpg``. See `Port-specific Notes`_
240+
below for more information on this.
241+
242+
Emscripten also has support for the older SDL 1.3, which is built-in. Use can
243+
use this via ``-sUSE_SDL=1``. SDL 1.3 has support for `sdl-config`_. Using the
244+
host version *sdl-config* may result in compilation errors. You may need to
245+
modify the build system to look for *sdl-config** in the emscripten sysroot
246+
(``<sysroot>/bin/sdl-config``).
262247

248+
.. note:: When a port is built and installed into the sysroot it will then be
249+
available to all following ``emcc`` commands. For example, once you have run
250+
``emcc`` with ``--use-port=sdl2`` or run ``./embuilder build sdl2``, future
251+
``emcc`` commands will be able to see the SDL2 headers and libraries.
252+
253+
.. note:: Since emscripten 3.1.54, ``--use-port`` is the preferred syntax to
254+
use a port in your project. The legacy syntax (for example ``-sUSE_SDL2``,
255+
``-sUSE_SDL_IMAGE=2``) remains available.
256+
257+
Port-specific Notes
258+
-------------------
259+
260+
The ``sdl2_image`` port generally requires the specification of set of supported
261+
imagine formats. For example ``--use-port=sdl2_image:formats=bmp,png,xpm,jpg``.
262+
This ensures that ``IMG_Init`` works properly when you specify those formats.
263+
Alternatively, you can use ``emcc --use-preload-plugins`` and ``--preload-file``
264+
your images, so the browser codecs decode them (see :ref:`preloading-files`). A
265+
code path in the ``sdl2_image`` port will load through
266+
:c:func:`emscripten_get_preloaded_image_data`, but then your calls to
267+
``IMG_Init`` with those image formats will fail (as while the images will work
268+
through preloading, ``IMG_Init`` reports no support for those formats, as it
269+
doesn't have support compiled in - in other words, ``IMG_Init`` does not report
270+
support for formats that only work through preloading).
263271

264272
Contrib ports
265273
-------------
@@ -272,7 +280,8 @@ See :ref:`Contrib Ports <Contrib-Ports>` for more information.
272280
Adding more ports
273281
-----------------
274282

275-
The simplest way to add a new port is to put it under the ``contrib`` directory. Basically, the steps are:
283+
The simplest way to add a new port is to put it under the ``contrib`` directory.
284+
Basically, the steps are:
276285

277286
* Make sure the port is open source and has a suitable license.
278287
* Read the ``README.md`` file under ``tools/ports/contrib`` which contains more information.
@@ -447,3 +456,6 @@ Troubleshooting
447456

448457
One solution is to use :ref:`dynamic-linking <Dynamic-Linking>`. This ensures that libraries are linked only once, in the final build stage.
449458
- When generating standalone Wasm, make sure to invoke the ``_start`` or (for ``--no-entry``) ``_initialize`` export before attempting to use the module.
459+
460+
.. _ports directory: https://github.com/emscripten-core/emscripten/tree/main/tools/ports
461+
.. _sdl-config: https://github.com/emscripten-core/emscripten/blob/main/system/bin/sdl-config

0 commit comments

Comments
 (0)