Skip to content

Commit 9f83cfa

Browse files
authored
Delay processing of --js-library flags. NFC (emscripten-core#23332)
By delaying the processing of `--js-library` arguments we avoid having to jump through hoops and store them alongside their ordinal number for half the time.
1 parent ee7a686 commit 9f83cfa

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

emcc.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -705,9 +705,11 @@ def phase_parse_arguments(state):
705705
settings.WARN_DEPRECATED = 0
706706

707707
for i in range(len(newargs)):
708-
if newargs[i] in ('-l', '-L', '-I', '-z'):
708+
if newargs[i] in ('-l', '-L', '-I', '-z', '--js-library'):
709709
# Scan for flags that can be written as either one or two arguments
710710
# and normalize them to the single argument form.
711+
if newargs[i] == '--js-library':
712+
newargs[i] += '='
711713
newargs[i] += newargs[i + 1]
712714
newargs[i + 1] = ''
713715

@@ -792,6 +794,8 @@ def phase_setup(options, state, newargs):
792794
state.add_link_flag(i + 1, newargs[i + 1])
793795
elif arg.startswith('-z'):
794796
state.add_link_flag(i, newargs[i])
797+
elif arg.startswith('--js-library='):
798+
state.add_link_flag(i, newargs[i])
795799
elif arg.startswith('-Wl,'):
796800
# Multiple comma separated link flags can be specified. Create fake
797801
# fractional indices for these: -Wl,a,b,c,d at index 4 becomes:
@@ -939,7 +943,7 @@ def filter_out_link_flags(args):
939943
def is_link_flag(flag):
940944
if flag in ('-nostdlib', '-nostartfiles', '-nolibc', '-nodefaultlibs', '-s'):
941945
return True
942-
return flag.startswith(('-l', '-L', '-Wl,', '-z'))
946+
return flag.startswith(('-l', '-L', '-Wl,', '-z', '--js-library'))
943947

944948
skip = False
945949
for arg in args:
@@ -1311,7 +1315,7 @@ def consume_arg_file():
13111315
options.memory_profiler = True
13121316
newargs[i] = ''
13131317
settings_changes.append('EMSCRIPTEN_TRACING=1')
1314-
settings.JS_LIBRARIES.append((0, 'library_trace.js'))
1318+
settings.JS_LIBRARIES.append('library_trace.js')
13151319
elif check_flag('--emit-symbol-map'):
13161320
options.emit_symbol_map = True
13171321
settings.EMIT_SYMBOL_MAP = 1
@@ -1346,8 +1350,6 @@ def consume_arg_file():
13461350
options.emit_tsd = consume_arg()
13471351
elif check_flag('--no-entry'):
13481352
options.no_entry = True
1349-
elif check_arg('--js-library'):
1350-
settings.JS_LIBRARIES.append((i + 1, os.path.abspath(consume_arg_file())))
13511353
elif check_flag('--remove-duplicates'):
13521354
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')
13531355
elif check_flag('--jcache'):

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ lint.ignore = [
5050
"PLW2901",
5151
]
5252
lint.per-file-ignores."emrun.py" = [ "PLE0704" ]
53-
lint.mccabe.max-complexity = 48 # Recommended: 10
53+
lint.mccabe.max-complexity = 49 # Recommended: 10
5454
lint.pylint.allow-magic-value-types = [
5555
"bytes",
5656
"float",
5757
"int",
5858
"str",
5959
]
6060
lint.pylint.max-args = 15 # Recommended: 5
61-
lint.pylint.max-branches = 49 # Recommended: 12
61+
lint.pylint.max-branches = 50 # Recommended: 12
6262
lint.pylint.max-returns = 16 # Recommended: 6
6363
lint.pylint.max-statements = 142 # Recommended: 50
6464

tools/link.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ def phase_linker_setup(options, state): # noqa: C901, PLR0912, PLR0915
12401240
state.append_link_flag('--no-whole-archive')
12411241
settings.FILESYSTEM = 1
12421242
settings.SYSCALLS_REQUIRE_FILESYSTEM = 0
1243-
settings.JS_LIBRARIES.append((0, 'library_wasmfs.js'))
1243+
settings.JS_LIBRARIES.append('library_wasmfs.js')
12441244
if settings.ASSERTIONS:
12451245
# used in assertion checks for unflushed content
12461246
settings.REQUIRED_EXPORTS += ['wasmfs_flush']
@@ -1362,14 +1362,14 @@ def phase_linker_setup(options, state): # noqa: C901, PLR0912, PLR0915
13621362

13631363
if settings.PTHREADS:
13641364
setup_pthreads()
1365-
settings.JS_LIBRARIES.append((0, 'library_pthread.js'))
1365+
settings.JS_LIBRARIES.append('library_pthread.js')
13661366
if settings.PROXY_TO_PTHREAD:
13671367
settings.PTHREAD_POOL_SIZE_STRICT = 0
13681368
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$runtimeKeepalivePush']
13691369
else:
13701370
if settings.PROXY_TO_PTHREAD:
13711371
exit_with_error('-sPROXY_TO_PTHREAD requires -pthread to work!')
1372-
settings.JS_LIBRARIES.append((0, 'library_pthread_stub.js'))
1372+
settings.JS_LIBRARIES.append('library_pthread_stub.js')
13731373

13741374
if settings.MEMORY64:
13751375
# 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
13831383
# set location of Wasm Worker bootstrap JS file
13841384
if settings.WASM_WORKERS == 1:
13851385
settings.WASM_WORKER_FILE = unsuffixed(os.path.basename(target)) + '.ww.js'
1386-
settings.JS_LIBRARIES.append((0, 'library_wasm_worker.js'))
1386+
settings.JS_LIBRARIES.append('library_wasm_worker.js')
13871387

13881388
# Set min browser versions based on certain settings such as WASM_BIGINT,
13891389
# PTHREADS, AUDIO_WORKLET
@@ -1401,7 +1401,7 @@ def phase_linker_setup(options, state): # noqa: C901, PLR0912, PLR0915
14011401
if settings.AUDIO_WORKLET:
14021402
if settings.AUDIO_WORKLET == 1:
14031403
settings.AUDIO_WORKLET_FILE = unsuffixed(os.path.basename(target)) + '.aw.js'
1404-
settings.JS_LIBRARIES.append((0, shared.path_from_root('src', 'library_webaudio.js')))
1404+
settings.JS_LIBRARIES.append(shared.path_from_root('src', 'library_webaudio.js'))
14051405
if not settings.MINIMAL_RUNTIME:
14061406
# If we are in the audio worklet environment, we can only access the Module object
14071407
# 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):
28072807

28082808
def process_libraries(state):
28092809
new_flags = []
2810-
libraries = []
28112810
suffixes = STATICLIB_ENDINGS + DYNAMICLIB_ENDINGS
28122811
system_libs_map = system_libs.Library.get_usable_variations()
28132812

2814-
# Find library files
2813+
# Process `-l` and `--js-library` flags
28152814
for i, flag in state.link_flags:
2815+
if flag.startswith('--js-library='):
2816+
js_lib = os.path.abspath(flag.split('=', 1)[1])
2817+
settings.JS_LIBRARIES.append(js_lib)
2818+
continue
28162819
if not flag.startswith('-l'):
28172820
new_flags.append((i, flag))
28182821
continue
@@ -2822,7 +2825,7 @@ def process_libraries(state):
28222825

28232826
js_libs = map_to_js_libs(lib)
28242827
if js_libs is not None:
2825-
libraries += [(i, js_lib) for js_lib in js_libs]
2828+
settings.JS_LIBRARIES += js_libs
28262829

28272830
# We don't need to resolve system libraries to absolute paths here, we can just
28282831
# let wasm-ld handle that. However, we do want to map to the correct variant.
@@ -2848,12 +2851,6 @@ def process_libraries(state):
28482851

28492852
new_flags.append((i, flag))
28502853

2851-
settings.JS_LIBRARIES += libraries
2852-
2853-
# At this point processing JS_LIBRARIES is finished, no more items will be added to it.
2854-
# Sort the input list from (order, lib_name) pairs to a flat array in the right order.
2855-
settings.JS_LIBRARIES.sort(key=lambda lib: lib[0])
2856-
settings.JS_LIBRARIES = [lib[1] for lib in settings.JS_LIBRARIES]
28572854
state.link_flags = new_flags
28582855

28592856

0 commit comments

Comments
 (0)