From d18663d641d83dd66a601809092f93e9f3129489 Mon Sep 17 00:00:00 2001 From: Yan Pujante Date: Thu, 15 Feb 2024 05:50:06 -0800 Subject: [PATCH 1/4] code cleanup + documentation --- ChangeLog.md | 3 +++ docs/emcc.txt | 9 ++++++--- site/source/docs/tools_reference/emcc.rst | 10 +++++++--- tools/ports/__init__.py | 19 +++++++------------ 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index abc4cfc7405ad..52d0ddd06e09c 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -47,6 +47,9 @@ See docs/process.md for more on how version tagging works. #21276) - Added concept of external ports which live outside emscripten and are loaded on demand using the syntax `--use-port=/path/to/my_port.py` (#21316) +- `embuilder` can now build ports with options as well as external ports using + the same syntax introduced with `--use-port` + (ex: `embuilder sdl2_image:formats=png,jpg`) (#21345) - Allow comments in response files. Any line starting with `#` is now ignored. This is useful when listing exported symbols. (#21330) diff --git a/docs/emcc.txt b/docs/emcc.txt index e9fd0167bf89b..55afc30fa550a 100644 --- a/docs/emcc.txt +++ b/docs/emcc.txt @@ -471,9 +471,12 @@ Options that are modified or new in *emcc* are listed below: "--use-port=" [compile+link] Use the specified port. If you need to use more than - one port you can use this argument multiple times. For example: "-- - use-port=sdl2 --use-port=bzip2". To get the list of available - ports, use "--show-ports". + one port you can use this option multiple times (ex: "--use- + port=sdl2 --use-port=bzip2"). A port can have options separated by + ":" (ex: "--use-port=sdl2_image:formats=png,jpg"). To use an + external port, you provide the path to the port directly (ex: "-- + use-port=/path/to/my_port.py"). To get the list of available ports, + use "--show-ports". "--clear-ports" [general] Manually clears the local copies of ports from the diff --git a/site/source/docs/tools_reference/emcc.rst b/site/source/docs/tools_reference/emcc.rst index a9116455881c2..8dc995529543e 100644 --- a/site/source/docs/tools_reference/emcc.rst +++ b/site/source/docs/tools_reference/emcc.rst @@ -461,9 +461,13 @@ Options that are modified or new in *emcc* are listed below: ``--use-port=`` [compile+link] - Use the specified port. If you need to use more than one port you can use this - argument multiple times. For example: ``--use-port=sdl2 --use-port=bzip2``. - To get the list of available ports, use ``--show-ports``. + Use the specified port. If you need to use more than one port you can use + this option multiple times (ex: ``--use-port=sdl2 --use-port=bzip2``). A port + can have options separated by ``:`` + (ex: ``--use-port=sdl2_image:formats=png,jpg``). To use an external port, + you provide the path to the port directly + (ex: ``--use-port=/path/to/my_port.py``). To get the list of available ports, + use ``--show-ports``. .. _emcc-clear-ports: diff --git a/tools/ports/__init__.py b/tools/ports/__init__.py index 2241991ece9f6..c728835b1780e 100644 --- a/tools/ports/__init__.py +++ b/tools/ports/__init__.py @@ -67,13 +67,9 @@ def init_port(name, port): validate_port(port) -def load_port_by_name(name): - port = __import__(name, globals(), level=1, fromlist=[None]) - init_port(name, port) - - -def load_port_by_path(path): - name = os.path.splitext(os.path.basename(path))[0] +def load_port(path, name = None): + if not name: + name = os.path.splitext(os.path.basename(path))[0] if name in ports_by_name: utils.exit_with_error(f'port path [`{path}`] is invalid: duplicate port name `{name}`') module_name = f'tools.ports.{name}' @@ -100,15 +96,14 @@ def read_ports(): for filename in os.listdir(ports_dir): if not filename.endswith('.py') or filename == '__init__.py': continue - filename = os.path.splitext(filename)[0] - load_port_by_name(filename) + load_port(os.path.join(ports_dir, filename)) contrib_dir = os.path.join(ports_dir, 'contrib') for filename in os.listdir(contrib_dir): if not filename.endswith('.py') or filename == '__init__.py': continue - filename = os.path.splitext(filename)[0] - load_port_by_name('contrib.' + filename) + name = 'contrib.' + os.path.splitext(filename)[0] + load_port(os.path.join(contrib_dir, filename), name) def get_all_files_under(dirname): @@ -426,7 +421,7 @@ def error_handler(message): port_file_path = name if not os.path.isfile(port_file_path): error_handler(f'not a valid port path: {port_file_path}') - name = load_port_by_path(port_file_path) + name = load_port(port_file_path) elif name not in ports_by_name: error_handler(f'invalid port name: `{name}`') ports_needed.add(name) From 11265be8bed2d6bec369fabf9ff17857f755a22e Mon Sep 17 00:00:00 2001 From: Yan Pujante Date: Thu, 15 Feb 2024 06:03:17 -0800 Subject: [PATCH 2/4] flake8 --- tools/ports/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/ports/__init__.py b/tools/ports/__init__.py index c728835b1780e..241e9b8c3cf7f 100644 --- a/tools/ports/__init__.py +++ b/tools/ports/__init__.py @@ -67,7 +67,7 @@ def init_port(name, port): validate_port(port) -def load_port(path, name = None): +def load_port(path, name=None): if not name: name = os.path.splitext(os.path.basename(path))[0] if name in ports_by_name: From 23b8373b4870ef349400d8ed1d6706c6ee4dc15d Mon Sep 17 00:00:00 2001 From: Yan Pujante Date: Thu, 15 Feb 2024 09:53:02 -0800 Subject: [PATCH 3/4] code review --- tools/ports/__init__.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tools/ports/__init__.py b/tools/ports/__init__.py index 241e9b8c3cf7f..0f3a34f8d5d05 100644 --- a/tools/ports/__init__.py +++ b/tools/ports/__init__.py @@ -69,13 +69,12 @@ def init_port(name, port): def load_port(path, name=None): if not name: - name = os.path.splitext(os.path.basename(path))[0] + name = shared.unsuffixed_basename(path) if name in ports_by_name: utils.exit_with_error(f'port path [`{path}`] is invalid: duplicate port name `{name}`') module_name = f'tools.ports.{name}' spec = importlib.util.spec_from_file_location(module_name, path) port = importlib.util.module_from_spec(spec) - sys.modules[module_name] = port spec.loader.exec_module(port) init_port(name, port) return name @@ -102,7 +101,7 @@ def read_ports(): for filename in os.listdir(contrib_dir): if not filename.endswith('.py') or filename == '__init__.py': continue - name = 'contrib.' + os.path.splitext(filename)[0] + name = 'contrib.' + shared.unsuffixed(filename) load_port(os.path.join(contrib_dir, filename), name) From 0c35f5b701af2150bceb9ee18b50e76176c39911 Mon Sep 17 00:00:00 2001 From: Yan Pujante Date: Thu, 15 Feb 2024 09:57:07 -0800 Subject: [PATCH 4/4] flake8 --- tools/ports/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/ports/__init__.py b/tools/ports/__init__.py index 0f3a34f8d5d05..6aa46a834b47f 100644 --- a/tools/ports/__init__.py +++ b/tools/ports/__init__.py @@ -9,7 +9,6 @@ import shutil import glob import importlib.util -import sys from typing import Set from tools import cache from tools import config