From a7dbf50702871242b4f3cf71c9781f1c5ac96f17 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Tue, 7 Jan 2025 17:06:55 -0800 Subject: [PATCH] Delay processing of --js-library flags. NFC By delaying the processing of `--js-library` arguments we avoid having jump through hoops and store them alongside their ordinal number for half the time. --- emcc.py | 12 +++++++----- pyproject.toml | 4 ++-- tools/link.py | 25 +++++++++++-------------- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/emcc.py b/emcc.py index 75370fb30230b..a556a6f956149 100644 --- a/emcc.py +++ b/emcc.py @@ -705,9 +705,11 @@ def phase_parse_arguments(state): settings.WARN_DEPRECATED = 0 for i in range(len(newargs)): - if newargs[i] in ('-l', '-L', '-I', '-z'): + if newargs[i] in ('-l', '-L', '-I', '-z', '--js-library'): # Scan for flags that can be written as either one or two arguments # and normalize them to the single argument form. + if newargs[i] == '--js-library': + newargs[i] += '=' newargs[i] += newargs[i + 1] newargs[i + 1] = '' @@ -792,6 +794,8 @@ def phase_setup(options, state, newargs): state.add_link_flag(i + 1, newargs[i + 1]) elif arg.startswith('-z'): state.add_link_flag(i, newargs[i]) + elif arg.startswith('--js-library='): + state.add_link_flag(i, newargs[i]) elif arg.startswith('-Wl,'): # Multiple comma separated link flags can be specified. Create fake # fractional indices for these: -Wl,a,b,c,d at index 4 becomes: @@ -939,7 +943,7 @@ def filter_out_link_flags(args): def is_link_flag(flag): if flag in ('-nostdlib', '-nostartfiles', '-nolibc', '-nodefaultlibs', '-s'): return True - return flag.startswith(('-l', '-L', '-Wl,', '-z')) + return flag.startswith(('-l', '-L', '-Wl,', '-z', '--js-library')) skip = False for arg in args: @@ -1311,7 +1315,7 @@ def consume_arg_file(): options.memory_profiler = True newargs[i] = '' settings_changes.append('EMSCRIPTEN_TRACING=1') - settings.JS_LIBRARIES.append((0, 'library_trace.js')) + settings.JS_LIBRARIES.append('library_trace.js') elif check_flag('--emit-symbol-map'): options.emit_symbol_map = True settings.EMIT_SYMBOL_MAP = 1 @@ -1346,8 +1350,6 @@ def consume_arg_file(): options.emit_tsd = consume_arg() elif check_flag('--no-entry'): options.no_entry = True - elif check_arg('--js-library'): - settings.JS_LIBRARIES.append((i + 1, os.path.abspath(consume_arg_file()))) elif check_flag('--remove-duplicates'): diagnostics.warning('legacy-settings', '--remove-duplicates is deprecated as it is no longer needed. If you cannot link without it, file a bug with a testcase') elif check_flag('--jcache'): diff --git a/pyproject.toml b/pyproject.toml index 874462532f8e3..72076b0713590 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -50,7 +50,7 @@ lint.ignore = [ "PLW2901", ] lint.per-file-ignores."emrun.py" = [ "PLE0704" ] -lint.mccabe.max-complexity = 48 # Recommended: 10 +lint.mccabe.max-complexity = 49 # Recommended: 10 lint.pylint.allow-magic-value-types = [ "bytes", "float", @@ -58,7 +58,7 @@ lint.pylint.allow-magic-value-types = [ "str", ] lint.pylint.max-args = 15 # Recommended: 5 -lint.pylint.max-branches = 49 # Recommended: 12 +lint.pylint.max-branches = 50 # Recommended: 12 lint.pylint.max-returns = 16 # Recommended: 6 lint.pylint.max-statements = 142 # Recommended: 50 diff --git a/tools/link.py b/tools/link.py index 3306f4cea119c..6a7002413910b 100644 --- a/tools/link.py +++ b/tools/link.py @@ -1240,7 +1240,7 @@ def phase_linker_setup(options, state): # noqa: C901, PLR0912, PLR0915 state.append_link_flag('--no-whole-archive') settings.FILESYSTEM = 1 settings.SYSCALLS_REQUIRE_FILESYSTEM = 0 - settings.JS_LIBRARIES.append((0, 'library_wasmfs.js')) + settings.JS_LIBRARIES.append('library_wasmfs.js') if settings.ASSERTIONS: # used in assertion checks for unflushed content settings.REQUIRED_EXPORTS += ['wasmfs_flush'] @@ -1362,14 +1362,14 @@ def phase_linker_setup(options, state): # noqa: C901, PLR0912, PLR0915 if settings.PTHREADS: setup_pthreads() - settings.JS_LIBRARIES.append((0, 'library_pthread.js')) + settings.JS_LIBRARIES.append('library_pthread.js') if settings.PROXY_TO_PTHREAD: settings.PTHREAD_POOL_SIZE_STRICT = 0 settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$runtimeKeepalivePush'] else: if settings.PROXY_TO_PTHREAD: exit_with_error('-sPROXY_TO_PTHREAD requires -pthread to work!') - settings.JS_LIBRARIES.append((0, 'library_pthread_stub.js')) + settings.JS_LIBRARIES.append('library_pthread_stub.js') if settings.MEMORY64: # Any "pointers" passed to JS will now be i64's, in both modes. @@ -1383,7 +1383,7 @@ def phase_linker_setup(options, state): # noqa: C901, PLR0912, PLR0915 # set location of Wasm Worker bootstrap JS file if settings.WASM_WORKERS == 1: settings.WASM_WORKER_FILE = unsuffixed(os.path.basename(target)) + '.ww.js' - settings.JS_LIBRARIES.append((0, 'library_wasm_worker.js')) + settings.JS_LIBRARIES.append('library_wasm_worker.js') # Set min browser versions based on certain settings such as WASM_BIGINT, # PTHREADS, AUDIO_WORKLET @@ -1401,7 +1401,7 @@ def phase_linker_setup(options, state): # noqa: C901, PLR0912, PLR0915 if settings.AUDIO_WORKLET: if settings.AUDIO_WORKLET == 1: settings.AUDIO_WORKLET_FILE = unsuffixed(os.path.basename(target)) + '.aw.js' - settings.JS_LIBRARIES.append((0, shared.path_from_root('src', 'library_webaudio.js'))) + settings.JS_LIBRARIES.append(shared.path_from_root('src', 'library_webaudio.js')) if not settings.MINIMAL_RUNTIME: # If we are in the audio worklet environment, we can only access the Module object # and not the global scope of the main JS script. Therefore we need to export @@ -2807,12 +2807,15 @@ def map_to_js_libs(library_name): def process_libraries(state): new_flags = [] - libraries = [] suffixes = STATICLIB_ENDINGS + DYNAMICLIB_ENDINGS system_libs_map = system_libs.Library.get_usable_variations() - # Find library files + # Process `-l` and `--js-library` flags for i, flag in state.link_flags: + if flag.startswith('--js-library='): + js_lib = os.path.abspath(flag.split('=', 1)[1]) + settings.JS_LIBRARIES.append(js_lib) + continue if not flag.startswith('-l'): new_flags.append((i, flag)) continue @@ -2822,7 +2825,7 @@ def process_libraries(state): js_libs = map_to_js_libs(lib) if js_libs is not None: - libraries += [(i, js_lib) for js_lib in js_libs] + settings.JS_LIBRARIES += js_libs # We don't need to resolve system libraries to absolute paths here, we can just # let wasm-ld handle that. However, we do want to map to the correct variant. @@ -2848,12 +2851,6 @@ def process_libraries(state): new_flags.append((i, flag)) - settings.JS_LIBRARIES += libraries - - # At this point processing JS_LIBRARIES is finished, no more items will be added to it. - # Sort the input list from (order, lib_name) pairs to a flat array in the right order. - settings.JS_LIBRARIES.sort(key=lambda lib: lib[0]) - settings.JS_LIBRARIES = [lib[1] for lib in settings.JS_LIBRARIES] state.link_flags = new_flags