Skip to content

Commit 36423eb

Browse files
authored
Allow setting of CLOSURE_ARGS in ports (#24192)
This is the implementation that was discussed in #24109 I can update the Changelog in a later commit if the tests pass in CI and this PR is approved. * I manually/locally ran `other.test_closure_externs` to make sure that the changes were not affecting existing code * I added a test `other.test_closure_args_from_port` that piggy back on `test_closure_externs` to pass the same parameter in the port instead and make sure that these changes actually do what they are supposed to do in the first place. Hopefully I didn't forget anything. The changes are pretty straightforward in the grand scheme of things.
1 parent 5cdadd6 commit 36423eb

File tree

6 files changed

+33
-4
lines changed

6 files changed

+33
-4
lines changed

ChangeLog.md

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ See docs/process.md for more on how version tagging works.
2323
- Programs built with `-sWASM_WORKERS` no longer generate a separate `.ww.js`
2424
file. This is similar to the change that was already made for pthreads in
2525
#21701. This saves on complexity, code size and network requests (#24163)
26+
- Closure arguments can now be used from ports using `settings.CLOSURE_ARGS`
27+
(#24192)
2628

2729
4.0.7 - 04/15/25
2830
----------------

emcc.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ def __init__(self):
142142
self.requested_debug = None
143143
self.emit_symbol_map = False
144144
self.use_closure_compiler = None
145-
self.closure_args = []
146145
self.js_transform = None
147146
self.pre_js = [] # before all js
148147
self.post_js = [] # after all js
@@ -1125,7 +1124,7 @@ def consume_arg_file():
11251124
consume_arg()
11261125
elif check_arg('--closure-args'):
11271126
args = consume_arg()
1128-
options.closure_args += shlex.split(args)
1127+
settings.CLOSURE_ARGS += shlex.split(args)
11291128
elif check_arg('--closure'):
11301129
options.use_closure_compiler = int(consume_arg())
11311130
elif check_arg('--js-transform'):

src/settings_internal.js

+5
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ var LINK_AS_CXX = false;
227227
// emitted in that case for closure compiler.
228228
var MAYBE_CLOSURE_COMPILER = false;
229229

230+
// List of closure args for the closure compiler.
231+
// This list is populated from the --closure-args argument and can be extended
232+
// in ports using settings.CLOSURE_ARGS
233+
var CLOSURE_ARGS = [];
234+
230235
// Set when some minimum browser version triggers doesn't support the minimum
231236
// set of JavaScript features. This triggers transpilation using babel.
232237
var TRANSPILE = false;

test/test_closure_args_port.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import os
2+
3+
4+
def get(ports, settings, shared): # noqa: ARG001
5+
return []
6+
7+
8+
def clear(ports, settings, shared): # noqa: ARG001
9+
pass
10+
11+
12+
def linker_setup(ports, settings): # noqa: ARG001
13+
externs_file = os.path.join(os.path.dirname(os.path.realpath(__file__)),
14+
'test_closure_externs.js')
15+
settings.CLOSURE_ARGS += [f'--externs={externs_file}']

test/test_other.py

+8
Original file line numberDiff line numberDiff line change
@@ -10269,6 +10269,14 @@ def test_closure_externs(self):
1026910269
'--pre-js', test_file('test_closure_externs_pre_js.js')] +
1027010270
args)
1027110271

10272+
# Tests that ports can add closure args by using settings.CLOSURE_ARGS
10273+
@crossplatform
10274+
def test_closure_args_from_port(self):
10275+
self.run_process([EMCC, test_file('hello_world.c'),
10276+
'--use-port', test_file('test_closure_args_port.py'),
10277+
'--closure=1',
10278+
'--pre-js', test_file('test_closure_externs_pre_js.js')])
10279+
1027210280
# Tests that it is possible to enable the Closure compiler via --closure=1 even if any of the input files reside in a path with unicode characters.
1027310281
def test_closure_cmdline_utf8_chars(self):
1027410282
test = "☃ äö Ć € ' 🦠.c"

tools/link.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2195,7 +2195,7 @@ def phase_final_emitting(options, target, js_target, wasm_target):
21952195
# Finally, rerun Closure compile with simple optimizations. It will be able
21962196
# to further minify the code. (n.b. it would not be safe to run in advanced
21972197
# mode)
2198-
final_js = building.closure_compiler(final_js, advanced=False, extra_closure_args=options.closure_args)
2198+
final_js = building.closure_compiler(final_js, advanced=False, extra_closure_args=settings.CLOSURE_ARGS)
21992199
# Run unsafe_optimizations.js once more. This allows the cleanup of newly
22002200
# unused things that closure compiler leaves behind (e.g `new Float64Array(x)`).
22012201
shared.run_js_tool(utils.path_from_root('tools/unsafe_optimizations.mjs'), [final_js, '-o', final_js], cwd=utils.path_from_root('.'))
@@ -2335,7 +2335,7 @@ def phase_binaryen(target, options, wasm_target):
23352335

23362336
if options.use_closure_compiler:
23372337
with ToolchainProfiler.profile_block('closure_compile'):
2338-
final_js = building.closure_compiler(final_js, extra_closure_args=options.closure_args)
2338+
final_js = building.closure_compiler(final_js, extra_closure_args=settings.CLOSURE_ARGS)
23392339
save_intermediate('closure')
23402340

23412341
if settings.TRANSPILE:

0 commit comments

Comments
 (0)