Skip to content

Commit 4c47d6a

Browse files
committed
cmdline: allow overriding -jsD directives (#26576)
1 parent 2cf3138 commit 4c47d6a

3 files changed

Lines changed: 16 additions & 14 deletions

File tree

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
addToLibrary({
2-
js_function: function() {
3-
#if CUSTOM_JS_OPTION
4-
return 1;
5-
#else
6-
return 0;
7-
#endif
8-
}
9-
});
2+
js_function: function() {
3+
return {{{ CUSTOM_JS_OPTION }}};
4+
}
5+
});

test/test_jslib.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,15 @@ def test_jslib_legacy(self):
353353
# the -jsDfoo=val syntax:
354354
# See https://github.com/emscripten-core/emscripten/issues/10580.
355355
def test_jslib_custom_settings(self):
356-
self.cflags += ['--js-library', test_file('jslib/test_jslib_custom_settings.js'), '-jsDCUSTOM_JS_OPTION=1']
357-
self.do_run_in_out_file_test('jslib/test_jslib_custom_settings.c')
356+
test_file_path = test_file('jslib/test_jslib_custom_settings.c')
357+
js_lib = test_file('jslib/test_jslib_custom_settings.js')
358358

359-
self.assert_fail([EMCC, '-jsDWASM=0'], 'cannot change built-in settings values with a -jsD directive')
359+
self.do_runf(test_file_path, '1\n', cflags=['--js-library', js_lib, '-jsDCUSTOM_JS_OPTION=1'])
360+
361+
# verify that the settings can be specified more than once, and that the last one wins.
362+
self.do_runf(test_file_path, '2\n', cflags=['--js-library', js_lib, '-jsDCUSTOM_JS_OPTION=1', '-jsDCUSTOM_JS_OPTION=2'])
363+
364+
self.assert_fail([EMCC, '-jsDWASM=1'], 'cannot change built-in settings values with a -jsD directive')
360365

361366
def test_jslib_native_deps(self):
362367
# Verify that memset (which lives in compiled code), can be specified as a JS library

tools/cmdline.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ def parse_args(newargs): # noqa: C901, PLR0912, PLR0915
216216
"""
217217
should_exit = False
218218
skip = False
219+
builtin_settings = set(settings.keys())
219220
LEGACY_ARGS = {'--js-opts', '--llvm-opts', '--llvm-lto', '--memory-init-file'}
220221
LEGACY_FLAGS = {'--separate-asm', '--jcache', '--proxy-to-worker', '--default-obj-ext',
221222
'--embind-emit-tsd', '--remove-duplicates', '--no-heap-copy'}
@@ -572,12 +573,12 @@ def consume_arg_file():
572573
elif arg.startswith('-jsD'):
573574
key = arg.removeprefix('-jsD')
574575
if '=' in key:
575-
key, value = key.split('=')
576+
key, value = key.split('=', 1)
576577
else:
577578
value = '1'
578-
if key in settings.keys():
579+
if key in builtin_settings:
579580
exit_with_error(f'{arg}: cannot change built-in settings values with a -jsD directive. Pass -s{key}={value} instead!')
580-
# Apply user -jsD settings
581+
# Allow overrides/duplicates for user-defined -jsD flags
581582
settings[key] = value
582583
newargs[i] = ''
583584
elif check_flag('-shared'):

0 commit comments

Comments
 (0)