Skip to content

Commit 7cdfa8d

Browse files
authored
Consolidate port loading code + embuilder documentation (#21353)
1 parent 59e6b8f commit 7cdfa8d

File tree

4 files changed

+23
-20
lines changed

4 files changed

+23
-20
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ See docs/process.md for more on how version tagging works.
5050
#21276)
5151
- Added concept of external ports which live outside emscripten and are
5252
loaded on demand using the syntax `--use-port=/path/to/my_port.py` (#21316)
53+
- `embuilder` can now build ports with options as well as external ports using
54+
the same syntax introduced with `--use-port`
55+
(ex: `embuilder sdl2_image:formats=png,jpg`) (#21345)
5356
- Allow comments in response files. Any line starting with `#` is now ignored.
5457
This is useful when listing exported symbols. (#21330)
5558

docs/emcc.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,12 @@ Options that are modified or new in *emcc* are listed below:
471471

472472
"--use-port=<port>"
473473
[compile+link] Use the specified port. If you need to use more than
474-
one port you can use this argument multiple times. For example: "--
475-
use-port=sdl2 --use-port=bzip2". To get the list of available
476-
ports, use "--show-ports".
474+
one port you can use this option multiple times (ex: "--use-
475+
port=sdl2 --use-port=bzip2"). A port can have options separated by
476+
":" (ex: "--use-port=sdl2_image:formats=png,jpg"). To use an
477+
external port, you provide the path to the port directly (ex: "--
478+
use-port=/path/to/my_port.py"). To get the list of available ports,
479+
use "--show-ports".
477480

478481
"--clear-ports"
479482
[general] Manually clears the local copies of ports from the

site/source/docs/tools_reference/emcc.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,13 @@ Options that are modified or new in *emcc* are listed below:
461461

462462
``--use-port=<port>``
463463
[compile+link]
464-
Use the specified port. If you need to use more than one port you can use this
465-
argument multiple times. For example: ``--use-port=sdl2 --use-port=bzip2``.
466-
To get the list of available ports, use ``--show-ports``.
464+
Use the specified port. If you need to use more than one port you can use
465+
this option multiple times (ex: ``--use-port=sdl2 --use-port=bzip2``). A port
466+
can have options separated by ``:``
467+
(ex: ``--use-port=sdl2_image:formats=png,jpg``). To use an external port,
468+
you provide the path to the port directly
469+
(ex: ``--use-port=/path/to/my_port.py``). To get the list of available ports,
470+
use ``--show-ports``.
467471

468472
.. _emcc-clear-ports:
469473

tools/ports/__init__.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import shutil
1010
import glob
1111
import importlib.util
12-
import sys
1312
from typing import Set
1413
from tools import cache
1514
from tools import config
@@ -67,19 +66,14 @@ def init_port(name, port):
6766
validate_port(port)
6867

6968

70-
def load_port_by_name(name):
71-
port = __import__(name, globals(), level=1, fromlist=[None])
72-
init_port(name, port)
73-
74-
75-
def load_port_by_path(path):
76-
name = os.path.splitext(os.path.basename(path))[0]
69+
def load_port(path, name=None):
70+
if not name:
71+
name = shared.unsuffixed_basename(path)
7772
if name in ports_by_name:
7873
utils.exit_with_error(f'port path [`{path}`] is invalid: duplicate port name `{name}`')
7974
module_name = f'tools.ports.{name}'
8075
spec = importlib.util.spec_from_file_location(module_name, path)
8176
port = importlib.util.module_from_spec(spec)
82-
sys.modules[module_name] = port
8377
spec.loader.exec_module(port)
8478
init_port(name, port)
8579
return name
@@ -100,15 +94,14 @@ def read_ports():
10094
for filename in os.listdir(ports_dir):
10195
if not filename.endswith('.py') or filename == '__init__.py':
10296
continue
103-
filename = os.path.splitext(filename)[0]
104-
load_port_by_name(filename)
97+
load_port(os.path.join(ports_dir, filename))
10598

10699
contrib_dir = os.path.join(ports_dir, 'contrib')
107100
for filename in os.listdir(contrib_dir):
108101
if not filename.endswith('.py') or filename == '__init__.py':
109102
continue
110-
filename = os.path.splitext(filename)[0]
111-
load_port_by_name('contrib.' + filename)
103+
name = 'contrib.' + shared.unsuffixed(filename)
104+
load_port(os.path.join(contrib_dir, filename), name)
112105

113106

114107
def get_all_files_under(dirname):
@@ -426,7 +419,7 @@ def error_handler(message):
426419
port_file_path = name
427420
if not os.path.isfile(port_file_path):
428421
error_handler(f'not a valid port path: {port_file_path}')
429-
name = load_port_by_path(port_file_path)
422+
name = load_port(port_file_path)
430423
elif name not in ports_by_name:
431424
error_handler(f'invalid port name: `{name}`')
432425
ports_needed.add(name)

0 commit comments

Comments
 (0)