Skip to content

Implement a different syntax for contrib ports #24260

Closed

Description

@ypujante

In order to stop hijacking the #23432 issue, I am creating a new entry to track this effort.

From @sbc100:

I've been thinking about how we can make out-of-tree ports work better.

How about something like this:

$ cat tools/ports/contrib/emdawn.py
EXTERNAL_PORT=http://foo.bar/releases/release.zip
SHA1=8407a6393c858ad5e7c7f23f7579d524c403be97
PORT_FILE=myport.py

Activity

  1. ypujante commented on May 6, 2025

    @ypujante
    ContributorAuthor

    @sbc100 I have started investigating these changes.

    The fact that ports are loaded when the system starts (and not on demand) makes this quite challenging: fetching the port (= zip file aka artifact) currently happens when the port is being used, the rest is just static data (like dependencies, options, etc...). But with this new design a remote port only contains information to fetch the artifact and the actual port is inside that artifact.

    I don't think we want to download the remote artifact (even if it happens only once) if the port is never used just so that we can build the list of ports up front...

    At some point (way back when), I had a design where ports would be loaded on demand and that was dropped because the "cost" was deemed negligible. I feel like we need to implement something like that. I am not entirely sure how to merge the 2 concepts otherwise...

    Certainly open for guidance/feedback

  2. kainino0x commented on May 13, 2025

    @kainino0x
    Collaborator

    It would be very nice to have first class support for things like this, though earlier I think we had a "hack" in mind that would just load the port and effectively re-export it. I've tested this locally by dropping it into emscripten/tools/ports/contrib and it works:

    # Copyright 2025 The Emscripten Authors.  All rights reserved.
    # Emscripten is available under two separate licenses, the MIT license and the
    # University of Illinois/NCSA Open Source License.  Both these licenses can be
    # found in the LICENSE file.
    
    # Contrib port wrapper for emdawnwebgpu, Dawn's implementation of webgpu.h
    # for Emscripten (on top of the WebGPU JS API).
    # To use this: --use-port=contrib.emdawnwebgpu-remote
    
    import importlib.util
    import os
    import sys
    
    URL = 'https://github.com/google/dawn/releases'
    DESCRIPTION = "emdawnwebgpu, Dawn's implementation of webgpu.h for Emscripten"
    LICENSE = 'mixed licenses, see license files'
    
    _port_name = 'emdawnwebgpu'
    _url = 'https://github.com/google/dawn/releases/download/v20250512.200721/emdawnwebgpu_pkg-v20250512.200721.zip'
    _hash = 'cc073a592a5d101c2e114c017bf5b4c0927330dc823d8b151d71c5a646f94f00f0f9eb9a8b4274d6960a2ff7ac84e762a0ac2add475ece616aed00e724942e3d'
    
    _loaded = None
    
    def _error_handler(message):
        raise Exception(message)
    def _load(ports):
        global _loaded
        if _loaded is None:
            ports.fetch_project(_port_name, _url, _hash)
            path = os.path.join(ports.get_dir(), _port_name, 'emdawnwebgpu_pkg', 'emdawnwebgpu.port.py')
    
            # Incantation to load the module from a filename
            spec = importlib.util.spec_from_file_location('port_emdawnwebgpu', path)
            mod = importlib.util.module_from_spec(spec)
            sys.modules['port_emdawnwebgpu'] = mod
            spec.loader.exec_module(mod)
            _loaded = mod
    
            # Assume handle_options has already been called (if there are options).
            if _options is not None:
                _loaded.handle_options(_options, _error_handler)
        return _loaded
    
    # Need to re-export these options for --use-port to accept them.
    # (There are no hooks that run before this that would let us download the port.)
    OPTIONS = {
        'cpp_bindings':
        'Add the include path for <webgpu/webgpu_cpp.h> C++ bindings. Default: true.',
        'opt_level':
        "Optimization (-O) level for the bindings' Wasm layer. Default: choose based on -sASSERTIONS.",
    }
    
    # We assume handle_options has been called before other hooks (if there are any
    # options) to override the initial _options.
    _options = {}
    def handle_options(options, error_handler):
        global _options
        _options = options
        if _loaded is not None:
            _loaded.handle_options(_options, _error_handler)
    
    def process_args(ports):
        return _load(ports).process_args(ports)
    
    def linker_setup(ports, settings):
        return _load(ports).linker_setup(ports, settings)
    
    def get(ports, settings, shared):
        return _load(ports).get(ports, settings, shared)
    
    def clear(ports, settings, shared):
        return _load(ports).clear(ports, settings, shared)

    WDYT of adding something like this to Emscripten now so we can get -sUSE_WEBGPU deprecated? I want to deprecate it ASAP so people know they need to migrate (and avoid building things on top of the old thing, continuing to file bugs about missing features, etc.).

  3. sbc100 commented on May 13, 2025

    @sbc100
    Collaborator

    If that hack works I don't see why we it shouldn't land that way first. You could even add a FIXME referring back to this bug,.

  4. ypujante commented on May 13, 2025

    @ypujante
    ContributorAuthor

    I published a non hacky version of emdawn. I just don't see why, since we are looking for a temporary solution, don't we go with a non hacky version.

    But in the end I have no say in this matter.

    That being said I would appreciate you providing guidance on the work that I have already done (PR #24303 ) and if I should continue working on it or not. I offered my help but if you don't need it, please say so, so I stop wasting my time.

  5. sbc100 commented on May 13, 2025

    @sbc100
    Collaborator

    Obviously a non-hacky version is better, and if we can land #24303 first that would be great!

    I think @kainino0x is just asking if we need to block the emdawn release on #24303 or if we can allow them to happen in either order (which seems better, especially if emdawn is trying to get feedback sooner rather then later).

  6. kainino0x commented on May 14, 2025

    @kainino0x
    Collaborator

    I published a non hacky version of emdawn. I just don't see why, since we are looking for a temporary solution, don't we go with a non hacky version.

    Fair. I was actually just looking back at that because I wanted to see how you did the tag/hash thing.

    What do you think of landing your port in Emscripten so we can deprecate USE_WEBGPU? I had a concern I hadn't really thought through, TBH, that if people need to use an emdawnwebgpu release that is not compatible with the built-in port file, they would have to download a release instead. But it's not really that much to ask them to do that. And besides, it probably won't happen that often, I was just nervous about this because I know there are bugs in emdawnwebgpu that we need to fix. But bugfixes shouldn't break it. (And anyway the hack I came up with is not really that stable either.)

    Once #24303 is done then we can upgrade it to use that system and it should be more stable.

  7. ypujante commented on May 14, 2025

    @ypujante
    ContributorAuthor

    @kainino0x I am fine landing a port similar to the one I wrote. The port I wrote is a remix of the one you wrote. And I feel like the one that we should check-in in Emscripten should be a remix of my remix: one that uses your option names and implementation (cpp_bindings and opt_level) since when #24303 land, it will refer to these options... I have a different implementation for optimization level to match the other port I created (ImGui) and it is different from your implementation.

    As far as #24303 is concerned, the work is mostly done but I am stuck at the moment and I am awaiting feedback/guidance in order to wrap it up: the code is essentially doing what you are doing in your "hacked" solution in a generic fashion => as soon as the port is required it gets fetched and replaces the port skeleton. There is a problem with the cachedir not being initialized soon enough and I have no clue how to fix this. Honestly I don't think we are far to be able to complete this work. I just need help from @sbc100 to move forward.

  8. ypujante commented on May 14, 2025

    @ypujante
    ContributorAuthor

    To follow up on this I think I may have found a solution on my own (of course unclear if that will be approved), but now with PR #24303, the external port, defined like this totally works:

    // contrib/emdawn.py
    TAG = 'v20250509.171557'
    
    EXTERNAL_PORT = f'https://github.com/google/dawn/releases/download/{TAG}/emdawnwebgpu_pkg-{TAG}.zip'
    SHA512 = '4b66bf0f64b9616a6420abdad636b3ecefab892dde8f67cd941147bfddf7920f5523ff10160c9a563ef377a0f88b2dfc033527591b2d0753d531de5cbbabde59'
    PORT_FILE = 'emdawnwebgpu_pkg/emdawnwebgpu.port.py'
    
    # contrib port information (required)
    URL = 'https://dawn.googlesource.com/dawn'
    DESCRIPTION = 'Dawn is an open-source and cross-platform implementation of the WebGPU standard'
    LICENSE = 'BSD 3-Clause License'
    > ./emcc --use-port=contrib.emdawn:help
    ports:INFO: retrieving port: contrib.emdawn from https://github.com/google/dawn/releases/download/v20250509.171557/emdawnwebgpu_pkg-v20250509.171557.zip
    ports:INFO: unpacking port: contrib.emdawn
    cache:INFO: deleting cached file: /usr/local/emsdk/emscripten/main/cache/sysroot/lib/wasm32-emscripten/lib_emdawnwebgpu-cb451621-O0.a
    contrib.emdawn (--use-port=contrib.emdawn; mixed licenses, see license files)
    Dawn is an open-source and cross-platform implementation of the WebGPU standard
    Options:
    * cpp_bindings: Add the include path for <webgpu/webgpu_cpp.h> C++ bindings. Default: true.
    * opt_level: Optimization (-O) level for the bindings' Wasm layer. Default: choose based on -sASSERTIONS.
    More info: https://dawn.googlesource.com/dawn
    
    > ./emcc --use-port=contrib.emdawn test/hello_world.c -o /tmp/build/index.js
    cache:INFO: generating port: sysroot/lib/wasm32-emscripten/lib_emdawnwebgpu-cb451621-O0.a... (this will be cached in "/usr/local/emsdk/emscripten/main/cache/sysroot/lib/wasm32-emscripten/lib_emdawnwebgpu-cb451621-O0.a" for subsequent builds)
    system_libs:INFO: compiled 1 inputs in 0.88s
    cache:INFO:  - ok
    
    
  9. sbc100 commented on May 14, 2025

    @sbc100
    Collaborator

    Regarding opt_level specifically. I explained in an earlier post somewhere why I don't think it should be needed. Instead we can just do what do for all our libraries do use a debug variant when -sASSERTIONS (or -O0 which implies it) is used.

    Trying to support all different opt levels leads to a testing matrix explosion that I think its best to avoid unless necessary. Adding extra flavors of a library always in my experience leads to bugs where we didn't test a particular flavor combination. e.g. "I can't use the -0z version dawn with the debug version of libc"

  10. kainino0x commented on May 14, 2025

    @kainino0x
    Collaborator

    Bit of a tangent (especially if #24303 gets landed soon) but...

    Regarding opt_level specifically. I explained in an earlier post somewhere why I don't think it should be needed. Instead we can just do what do for all our libraries do use a debug variant when -sASSERTIONS (or -O0 which implies it) is used.

    The package only supports opt_level=0/2 right now for that reason. I kept it as opt_level to make it trivial for people to modify to add other optimization levels if they want, though this will be complicated a little by a fix I'm making to set NDEBUG (which I forgot to do before), so was considering just making it a boolean assertions=auto/true/false.

    It still needs to be an option if we want it to be possible to build both versions with embuilder (which seems to always set ASSERTIONS=1) - however if we want to fix this in embuilder instead, I would be fine removing the port option entirely; it would be nice to simplify it.

  11. sbc100 commented on May 14, 2025

    @sbc100
    Collaborator

    It still needs to be an option if we want it to be possible to build both versions with embuilder (which seems to always set ASSERTIONS=1) - however if we want to fix this in embuilder instead, I would be fine removing the port option entirely; it would be nice to simplify it.

    Good point. We should add way to build debug flavored libraries using embuilder.

  12. ypujante commented on May 20, 2025

    @ypujante
    ContributorAuthor

    Implemented with #24303.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions