Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add external ports + options support to embuilder #21345

Merged
merged 8 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 4 additions & 7 deletions embuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from tools import shared
from tools import system_libs
from tools import ports
from tools import utils
from tools.settings import settings
from tools.system_libs import USE_NINJA

Expand Down Expand Up @@ -169,8 +170,8 @@ def get_all_tasks():
return get_system_tasks()[1] + PORTS


def handle_port_error(message):
raise Exception(message)
def handle_port_error(target, message):
utils.exit_with_error(f'Build target invalid `{target}` | {message}')
ypujante marked this conversation as resolved.
Show resolved Hide resolved


def main():
Expand Down Expand Up @@ -294,11 +295,7 @@ def main():
if do_build:
build_port(what)
elif ':' in what or what.endswith('.py'):
try:
name = ports.handle_use_port_arg(settings, what, lambda message: handle_port_error(message))
except Exception as e:
logger.error(f'Build target invalid `{what}` | {e}')
return 1
name = ports.handle_use_port_arg(settings, what, lambda message: handle_port_error(what, message))
if do_clear:
clear_port(name)
if do_build:
Expand Down
25 changes: 0 additions & 25 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -2418,31 +2418,6 @@ def test_external_ports(self):
self.assertFalse(os.path.exists('a4.out.js'))
self.assertContained('Unknown dependency `invalid` for port `external`', stderr)

@crossplatform
def test_embuilder_with_use_port_syntax(self):
if config.FROZEN_CACHE:
self.skipTest("test doesn't work with frozen cache")
self.run_process([EMBUILDER, 'build', 'sdl2_image:formats=png,jpg', '--force'])
self.assertExists(os.path.join(config.CACHE, 'sysroot', 'lib', 'wasm32-emscripten', 'libSDL2_image_jpg-png.a'))

@crossplatform
def test_embuilder_external_ports(self):
if config.FROZEN_CACHE:
self.skipTest("test doesn't work with frozen cache")
simple_port_path = test_file("other/ports/simple.py")
# embuilder handles external port target that ends with .py
self.run_process([EMBUILDER, 'build', f'{simple_port_path}', '--force'])
self.assertExists(os.path.join(config.CACHE, 'sysroot', 'lib', 'wasm32-emscripten', 'lib_simple.a'))
# embuilder handles external port target that contains port options
external_port_path = test_file("other/ports/external.py")
self.run_process([EMBUILDER, 'build', f'{external_port_path}:value1=12:value2=36', '--force'])
self.assertExists(os.path.join(config.CACHE, 'sysroot', 'lib', 'wasm32-emscripten', 'lib_external.a'))
# embuilder handles external port target that contains port options (influences library name,
# like sdl2_image:formats=png)
external_port_path = test_file("other/ports/external.py")
self.run_process([EMBUILDER, 'build', f'{external_port_path}:dependency=sdl2', '--force'])
self.assertExists(os.path.join(config.CACHE, 'sysroot', 'lib', 'wasm32-emscripten', 'lib_external-sdl2.a'))

def test_link_memcpy(self):
# memcpy can show up *after* optimizations, so after our opportunity to link in libc, so it must be special-cased
create_file('main.c', r'''
Expand Down
22 changes: 22 additions & 0 deletions test/test_sanity.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,28 @@ def test_embuilder_wildcards(self):
self.run_process([EMBUILDER, 'build', 'libwebgpu*'])
self.assertGreater(len(glob.glob(glob_match)), 3)

def test_embuilder_with_use_port_syntax(self):
restore_and_set_up()
self.run_process([EMBUILDER, 'build', 'sdl2_image:formats=png,jpg', '--force'])
self.assertExists(os.path.join(config.CACHE, 'sysroot', 'lib', 'wasm32-emscripten', 'libSDL2_image_jpg-png.a'))
self.assertContained('Build target invalid `sdl2_image:formats=invalid` | invalid is not a supported format', self.do([EMBUILDER, 'build', 'sdl2_image:formats=invalid', '--force']))

def test_embuilder_external_ports(self):
restore_and_set_up()
simple_port_path = test_file("other/ports/simple.py")
# embuilder handles external port target that ends with .py
self.run_process([EMBUILDER, 'build', f'{simple_port_path}', '--force'])
self.assertExists(os.path.join(config.CACHE, 'sysroot', 'lib', 'wasm32-emscripten', 'lib_simple.a'))
# embuilder handles external port target that contains port options
external_port_path = test_file("other/ports/external.py")
self.run_process([EMBUILDER, 'build', f'{external_port_path}:value1=12:value2=36', '--force'])
self.assertExists(os.path.join(config.CACHE, 'sysroot', 'lib', 'wasm32-emscripten', 'lib_external.a'))
# embuilder handles external port target that contains port options (influences library name,
# like sdl2_image:formats=png)
external_port_path = test_file("other/ports/external.py")
self.run_process([EMBUILDER, 'build', f'{external_port_path}:dependency=sdl2', '--force'])
self.assertExists(os.path.join(config.CACHE, 'sysroot', 'lib', 'wasm32-emscripten', 'lib_external-sdl2.a'))

def test_binaryen_version(self):
restore_and_set_up()
with open(EM_CONFIG, 'a') as f:
Expand Down
3 changes: 2 additions & 1 deletion tools/ports/contrib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ additional components:
def handle_options(options, error_handler):
# options is of type Dict[str, str]
# in case of error, use error_handler('error message')
ypujante marked this conversation as resolved.
Show resolved Hide resolved
# note that error_handler is guaranteed to never return
```
2. A dictionary called `OPTIONS` (type `Dict[str, str]`) where each key is the
name of the option and the value is a short description of what it does

When emscripten detects that options have been provided, it parses them and
check that they are valid option names for this port (using `OPTIONS`). It then
calls the handler function with these (valid) options. If you detect an error
with a value, you should use `tools.utils.exit_with_error` to report the
with a value, you should use the error handler provided to report the
failure.

> ### Note
Expand Down
11 changes: 10 additions & 1 deletion tools/ports/sdl2_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
'formats': 'A comma separated list of formats (ex: --use-port=sdl2_image:formats=png,jpg)'
}

SUPPORTED_FORMATS = {'avif', 'bmp', 'gif', 'jpg', 'jxl', 'lbm', 'pcx', 'png',
'pnm', 'qoi', 'svg', 'tga', 'tif', 'webp', 'xcf', 'xpm', 'xv'}

ypujante marked this conversation as resolved.
Show resolved Hide resolved
# user options (from --use-port)
opts: Dict[str, Set] = {
'formats': set()
Expand Down Expand Up @@ -89,7 +92,13 @@ def process_dependencies(settings):


def handle_options(options, error_handler):
opts['formats'].update({format.lower().strip() for format in options['formats'].split(',')})
formats = options['formats'].split(',')
for format in formats:
format = format.lower().strip()
if format not in SUPPORTED_FORMATS:
error_handler(f'{format} is not a supported format')
else:
opts['formats'].add(format)


def show():
Expand Down
Loading