Skip to content

Commit fa5b2aa

Browse files
authored
Avoid converting user JS library paths to absolute paths. NFC (#23798)
System library paths are converted to absolute paths, both in link.py and in modules.mjs, but use JS libraries are left in their original form. Added a test to confirm that a user library can have the same name as as system library without issue. Split out from #23787
1 parent a9e18d8 commit fa5b2aa

File tree

5 files changed

+46
-28
lines changed

5 files changed

+46
-28
lines changed

emcc.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,6 @@ def consume_arg_file():
12191219
options.memory_profiler = True
12201220
newargs[i] = ''
12211221
settings_changes.append('EMSCRIPTEN_TRACING=1')
1222-
settings.JS_LIBRARIES.append('libtrace.js')
12231222
elif check_flag('--emit-symbol-map'):
12241223
options.emit_symbol_map = True
12251224
settings.EMIT_SYMBOL_MAP = 1

src/modules.mjs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
import * as path from 'node:path';
8-
import * as fs from 'node:fs';
98
import {fileURLToPath} from 'node:url';
109
import assert from 'node:assert';
1110

@@ -201,20 +200,15 @@ function calculateLibraries() {
201200
libraries.push('liblittle_endian_heap.js');
202201
}
203202

203+
// Resolve system libraries
204+
libraries = libraries.map((filename) => path.join(systemLibdir, filename));
205+
204206
// Add all user specified JS library files to the link.
205207
// These must be added last after all Emscripten-provided system libraries
206208
// above, so that users can override built-in JS library symbols in their
207209
// own code.
208210
libraries.push(...JS_LIBRARIES);
209211

210-
// Resolve all filenames to absolute paths
211-
libraries = libraries.map((filename) => {
212-
if (!path.isAbsolute(filename) && fs.existsSync(path.join(systemLibdir, filename))) {
213-
filename = path.join(systemLibdir, filename);
214-
}
215-
return path.resolve(filename);
216-
});
217-
218212
// Deduplicate libraries to avoid processing any library file multiple times
219213
libraries = libraries.filter((item, pos) => libraries.indexOf(item) == pos);
220214

test/test_other.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5001,6 +5001,23 @@ def test_jslib_new_objects_non_empty(self):
50015001
err = self.expect_fail([EMCC, test_file('hello_world.c'), '--js-library=lib.js', '-sEXPORTED_FUNCTIONS=obj,_main'])
50025002
self.assertContained('cannot stringify Map with data', err)
50035003

5004+
def test_jslib_system_lib_name(self):
5005+
create_file('libcore.js', r'''
5006+
addToLibrary({
5007+
jslibfunc: (x) => 2 * x
5008+
});
5009+
''')
5010+
create_file('src.c', r'''
5011+
#include <emscripten.h>
5012+
#include <stdio.h>
5013+
int jslibfunc(int x);
5014+
int main() {
5015+
printf("jslibfunc: %d\n", jslibfunc(6));
5016+
return 0;
5017+
}
5018+
''')
5019+
self.do_runf('src.c', 'jslibfunc: 12', emcc_args=['--js-library', 'libcore.js'])
5020+
50045021
def test_EMCC_BUILD_DIR(self):
50055022
# EMCC_BUILD_DIR was necessary in the past since we used to force the cwd to be src/ for
50065023
# technical reasons.

tools/link.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,6 @@ def get_js_sym_info():
217217
assert jslibs
218218
input_files.extend(read_file(jslib) for jslib in sorted(jslibs))
219219
for jslib in settings.JS_LIBRARIES:
220-
if not os.path.isabs(jslib):
221-
jslib = utils.path_from_root('src/lib', jslib)
222220
input_files.append(read_file(jslib))
223221
content = '\n'.join(input_files)
224222
content_hash = hashlib.sha1(content.encode('utf-8')).hexdigest()
@@ -629,6 +627,12 @@ def check_browser_versions():
629627
exit_with_error(f'{key} older than {oldest} is not supported')
630628

631629

630+
def add_system_js_lib(lib):
631+
lib = utils.path_from_root('src/lib', lib)
632+
assert os.path.exists(lib)
633+
settings.JS_LIBRARIES.append(lib)
634+
635+
632636
@ToolchainProfiler.profile_block('linker_setup')
633637
def phase_linker_setup(options, linker_args): # noqa: C901, PLR0912, PLR0915
634638
"""Future modifications should consider refactoring to reduce complexity.
@@ -1246,7 +1250,7 @@ def phase_linker_setup(options, linker_args): # noqa: C901, PLR0912, PLR0915
12461250
if settings.WASMFS:
12471251
settings.FILESYSTEM = 1
12481252
settings.SYSCALLS_REQUIRE_FILESYSTEM = 0
1249-
settings.JS_LIBRARIES.append('libwasmfs.js')
1253+
add_system_js_lib('libwasmfs.js')
12501254
if settings.ASSERTIONS:
12511255
# used in assertion checks for unflushed content
12521256
settings.REQUIRED_EXPORTS += ['wasmfs_flush']
@@ -1362,14 +1366,14 @@ def phase_linker_setup(options, linker_args): # noqa: C901, PLR0912, PLR0915
13621366

13631367
if settings.PTHREADS:
13641368
setup_pthreads()
1365-
settings.JS_LIBRARIES.append('libpthread.js')
1369+
add_system_js_lib('libpthread.js')
13661370
if settings.PROXY_TO_PTHREAD:
13671371
settings.PTHREAD_POOL_SIZE_STRICT = 0
13681372
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$runtimeKeepalivePush']
13691373
else:
13701374
if settings.PROXY_TO_PTHREAD:
13711375
exit_with_error('-sPROXY_TO_PTHREAD requires -pthread to work!')
1372-
settings.JS_LIBRARIES.append('libpthread_stub.js')
1376+
add_system_js_lib('libpthread_stub.js')
13731377

13741378
if settings.MEMORY64:
13751379
# Any "pointers" passed to JS will now be i64's, in both modes.
@@ -1383,7 +1387,7 @@ def phase_linker_setup(options, linker_args): # noqa: C901, PLR0912, PLR0915
13831387
# set location of Wasm Worker bootstrap JS file
13841388
if settings.WASM_WORKERS == 1:
13851389
settings.WASM_WORKER_FILE = unsuffixed(os.path.basename(target)) + '.ww.js'
1386-
settings.JS_LIBRARIES.append('libwasm_worker.js')
1390+
add_system_js_lib('libwasm_worker.js')
13871391

13881392
# Set min browser versions based on certain settings such as WASM_BIGINT,
13891393
# PTHREADS, AUDIO_WORKLET
@@ -1401,7 +1405,7 @@ def phase_linker_setup(options, linker_args): # noqa: C901, PLR0912, PLR0915
14011405
if settings.AUDIO_WORKLET:
14021406
if settings.AUDIO_WORKLET == 1:
14031407
settings.AUDIO_WORKLET_FILE = unsuffixed(os.path.basename(target)) + '.aw.js'
1404-
settings.JS_LIBRARIES.append('libwebaudio.js')
1408+
add_system_js_lib('libwebaudio.js')
14051409
if not settings.MINIMAL_RUNTIME:
14061410
# If we are in the audio worklet environment, we can only access the Module object
14071411
# and not the global scope of the main JS script. Therefore we need to export
@@ -1774,11 +1778,13 @@ def get_full_import_name(name):
17741778
if not js_manipulation.isidentifier(settings.EXPORT_NAME):
17751779
exit_with_error(f'EXPORT_NAME is not a valid JS identifier: `{settings.EXPORT_NAME}`')
17761780

1777-
if settings.EMSCRIPTEN_TRACING and settings.ALLOW_MEMORY_GROWTH:
1778-
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['emscripten_trace_report_memory_layout']
1779-
settings.REQUIRED_EXPORTS += ['emscripten_stack_get_current',
1780-
'emscripten_stack_get_base',
1781-
'emscripten_stack_get_end']
1781+
if settings.EMSCRIPTEN_TRACING:
1782+
add_system_js_lib('libtrace.js')
1783+
if settings.ALLOW_MEMORY_GROWTH:
1784+
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['emscripten_trace_report_memory_layout']
1785+
settings.REQUIRED_EXPORTS += ['emscripten_stack_get_current',
1786+
'emscripten_stack_get_base',
1787+
'emscripten_stack_get_end']
17821788

17831789
settings.EMSCRIPTEN_VERSION = utils.EMSCRIPTEN_VERSION
17841790
settings.SOURCE_MAP_BASE = options.source_map_base or ''
@@ -1999,11 +2005,11 @@ def run_embind_gen(options, wasm_target, js_syms, extra_settings):
19992005
setup_environment_settings()
20002006
# Use a separate Wasm file so the JS does not need to be modified after emscripten.emscript.
20012007
settings.SINGLE_FILE = False
2002-
# Embind may be included multiple times, de-duplicate the list first.
2003-
settings.JS_LIBRARIES = dedup_list(settings.JS_LIBRARIES)
20042008
# Replace embind with the TypeScript generation version.
2005-
embind_index = settings.JS_LIBRARIES.index('libembind.js')
2006-
settings.JS_LIBRARIES[embind_index] = 'libembind_gen.js'
2009+
for i, lib in enumerate(settings.JS_LIBRARIES):
2010+
dirname, basename = os.path.split(lib)
2011+
if basename == 'libembind.js':
2012+
settings.JS_LIBRARIES[i] = os.path.join(dirname, 'libembind_gen.js')
20072013
if settings.MEMORY64:
20082014
settings.MIN_NODE_VERSION = 160000
20092015
# Source maps haven't been generated yet and aren't needed to run embind_gen.
@@ -2774,7 +2780,7 @@ def process_libraries(options, flags):
27742780
# Process `-l` and `--js-library` flags
27752781
for flag in flags:
27762782
if flag.startswith('--js-library='):
2777-
js_lib = os.path.abspath(flag.split('=', 1)[1])
2783+
js_lib = flag.split('=', 1)[1]
27782784
settings.JS_LIBRARIES.append(js_lib)
27792785
continue
27802786
if not flag.startswith('-l'):
@@ -2786,7 +2792,8 @@ def process_libraries(options, flags):
27862792

27872793
js_libs = map_to_js_libs(lib)
27882794
if js_libs is not None:
2789-
settings.JS_LIBRARIES += js_libs
2795+
for l in js_libs:
2796+
add_system_js_lib(l)
27902797

27912798
# We don't need to resolve system libraries to absolute paths here, we can just
27922799
# let wasm-ld handle that. However, we do want to map to the correct variant.

tools/maint/gen_sig_info.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ def extract_sig_info(sig_info, extra_settings=None, extra_cflags=None, cxx=False
318318
}
319319
if extra_settings:
320320
settings.update(extra_settings)
321+
settings['JS_LIBRARIES'] = [os.path.join(utils.path_from_root('src/lib'), s) for s in settings['JS_LIBRARIES']]
321322
with tempfiles.get_file('.json') as settings_json:
322323
utils.write_file(settings_json, json.dumps(settings))
323324
output = shared.run_js_tool(utils.path_from_root('tools/compiler.mjs'),

0 commit comments

Comments
 (0)