Skip to content

Commit c25daad

Browse files
authored
Minor path handling cleanups. NFC. (#15018)
Use shared utilities where possible. This makes this code more explicit and also paves the way for more use of `pathlib` over raw strings in the future.
1 parent 1ee65d5 commit c25daad

File tree

5 files changed

+24
-23
lines changed

5 files changed

+24
-23
lines changed

emcc.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555

5656
logger = logging.getLogger('emcc')
5757

58-
# endings = dot + a suffix, safe to test by filename.endswith(endings)
58+
# endings = dot + a suffix, compare against result of shared.suffix()
5959
C_ENDINGS = ('.c', '.i')
6060
CXX_ENDINGS = ('.cpp', '.cxx', '.cc', '.c++', '.CPP', '.CXX', '.C', '.CC', '.C++', '.ii')
6161
OBJC_ENDINGS = ('.m', '.mi')
@@ -753,7 +753,7 @@ def emsdk_ldflags(user_args):
753753
library_paths = [
754754
shared.Cache.get_lib_dir(absolute=True)
755755
]
756-
ldflags = ['-L' + l for l in library_paths]
756+
ldflags = [f'-L{l}' for l in library_paths]
757757

758758
if '-nostdlib' in user_args:
759759
return ldflags
@@ -1775,7 +1775,7 @@ def default_setting(name, new_default):
17751775
state.forced_stdlibs.append('libfetch')
17761776
settings.JS_LIBRARIES.append((0, 'library_fetch.js'))
17771777
if settings.USE_PTHREADS:
1778-
settings.FETCH_WORKER_FILE = unsuffixed(os.path.basename(target)) + '.fetch.js'
1778+
settings.FETCH_WORKER_FILE = unsuffixed_basename(target) + '.fetch.js'
17791779

17801780
if settings.DEMANGLE_SUPPORT:
17811781
settings.EXPORTED_FUNCTIONS += ['___cxa_demangle']
@@ -1894,7 +1894,7 @@ def default_setting(name, new_default):
18941894
building.user_requested_exports.add('_emscripten_current_thread_process_queued_calls')
18951895

18961896
# set location of worker.js
1897-
settings.PTHREAD_WORKER_FILE = unsuffixed(os.path.basename(target)) + '.worker.js'
1897+
settings.PTHREAD_WORKER_FILE = unsuffixed_basename(target) + '.worker.js'
18981898
else:
18991899
settings.JS_LIBRARIES.append((0, 'library_pthread_stub.js'))
19001900

@@ -2090,7 +2090,7 @@ def check_memory_setting(setting):
20902090
settings.MEM_INIT_IN_WASM = True
20912091

20922092
# wasm side modules have suffix .wasm
2093-
if settings.SIDE_MODULE and target.endswith('.js'):
2093+
if settings.SIDE_MODULE and shared.suffix(target) == '.js':
20942094
diagnostics.warning('emcc', 'output suffix .js requested, but wasm side modules are just wasm files; emitting only a .wasm, no .js')
20952095

20962096
sanitize = set()
@@ -2350,10 +2350,11 @@ def get_language_mode(args):
23502350
def use_cxx(src):
23512351
if 'c++' in language_mode or run_via_emxx:
23522352
return True
2353+
suffix = shared.suffix(src)
23532354
# Next consider the filename
2354-
if src.endswith(C_ENDINGS + OBJC_ENDINGS):
2355+
if suffix in C_ENDINGS + OBJC_ENDINGS:
23552356
return False
2356-
if src.endswith(CXX_ENDINGS):
2357+
if suffix in CXX_ENDINGS:
23572358
return True
23582359
# Finally fall back to the default
23592360
if settings.DEFAULT_TO_CXX:
@@ -2393,7 +2394,7 @@ def get_clang_command_asm(src_file):
23932394
if state.mode == Mode.PCH:
23942395
headers = [header for _, header in input_files]
23952396
for header in headers:
2396-
if not header.endswith(HEADER_ENDINGS):
2397+
if not shared.suffix(header) in HEADER_ENDINGS:
23972398
exit_with_error(f'cannot mix precompiled headers with non-header inputs: {headers} : {header}')
23982399
cmd = get_clang_command(header)
23992400
if options.output_file:
@@ -2423,7 +2424,7 @@ def get_object_filename(input_file):
24232424
return in_temp(unsuffixed(uniquename(input_file)) + options.default_object_extension)
24242425

24252426
def compile_source_file(i, input_file):
2426-
logger.debug('compiling source file: ' + input_file)
2427+
logger.debug(f'compiling source file: {input_file}')
24272428
output_file = get_object_filename(input_file)
24282429
if state.mode not in (Mode.COMPILE_ONLY, Mode.PREPROCESS_ONLY):
24292430
linker_inputs.append((i, output_file))
@@ -2452,10 +2453,10 @@ def compile_source_file(i, input_file):
24522453
if file_suffix in SOURCE_ENDINGS + ASSEMBLY_ENDINGS or (state.has_dash_c and file_suffix == '.bc'):
24532454
compile_source_file(i, input_file)
24542455
elif file_suffix in DYNAMICLIB_ENDINGS:
2455-
logger.debug('using shared library: ' + input_file)
2456+
logger.debug(f'using shared library: {input_file}')
24562457
linker_inputs.append((i, input_file))
24572458
elif building.is_ar(input_file):
2458-
logger.debug('using static library: ' + input_file)
2459+
logger.debug(f'using static library: {input_file}')
24592460
ensure_archive_index(input_file)
24602461
linker_inputs.append((i, input_file))
24612462
elif language_mode:
@@ -2464,7 +2465,7 @@ def compile_source_file(i, input_file):
24642465
exit_with_error('-E or -x required when input is from standard input')
24652466
else:
24662467
# Default to assuming the inputs are object files and pass them to the linker
2467-
logger.debug('using object file: ' + input_file)
2468+
logger.debug(f'using object file: {input_file}')
24682469
linker_inputs.append((i, input_file))
24692470

24702471
return linker_inputs
@@ -2586,7 +2587,7 @@ def phase_source_transforms(options, target):
25862587
file_args.append('--use-preload-plugins')
25872588
if not settings.ENVIRONMENT_MAY_BE_NODE:
25882589
file_args.append('--no-node')
2589-
file_code = shared.check_call([shared.FILE_PACKAGER, unsuffixed(target) + '.data'] + file_args, stdout=PIPE).stdout
2590+
file_code = shared.check_call([shared.FILE_PACKAGER, shared.replace_suffix(target, '.data')] + file_args, stdout=PIPE).stdout
25902591
options.pre_js = js_manipulation.add_files_pre_js(options.pre_js, file_code)
25912592

25922593
# Apply pre and postjs files
@@ -3529,7 +3530,7 @@ def generate_worker_js(target, js_target, target_basename):
35293530

35303531
# compiler output goes in .worker.js file
35313532
else:
3532-
move_file(js_target, unsuffixed(js_target) + '.worker.js')
3533+
move_file(js_target, shared.replace_suffix(js_target, '.worker.js'))
35333534
worker_target_basename = target_basename + '.worker'
35343535
proxy_worker_filename = (settings.PROXY_TO_WORKER_FILENAME or worker_target_basename) + '.js'
35353536

tests/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,8 +1048,8 @@ def _build_and_run(self, filename, expected_output, args=[], output_nicerizer=No
10481048
engines += self.wasm_engines
10491049
if self.get_setting('WASM2C') and not EMTEST_LACKS_NATIVE_CLANG:
10501050
# compile the c file to a native executable.
1051-
c = shared.unsuffixed(js_file) + '.wasm.c'
1052-
executable = shared.unsuffixed(js_file) + '.exe'
1051+
c = shared.replace_suffix(js_file, '.wasm.c')
1052+
executable = shared.replace_suffix(js_file, '.exe')
10531053
cmd = [shared.CLANG_CC, c, '-o', executable] + clang_native.get_clang_native_args()
10541054
self.run_process(cmd, env=clang_native.get_clang_native_env())
10551055
# we can now run the executable directly, without an engine, which

tests/jsrun.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def make_command(filename, engine, args=[]):
1818
# if no engine is needed, indicated by None, then there is a native executable
1919
# provided which we can just run
2020
if engine[0] is None:
21-
executable = shared.unsuffixed(os.path.abspath(filename)) + '.exe'
21+
executable = shared.replace_suffix(os.path.abspath(filename), '.exe')
2222
return [executable] + args
2323
if type(engine) is not list:
2424
engine = [engine]
@@ -44,7 +44,7 @@ def make_command(filename, engine, args=[]):
4444
command_flags += ['run']
4545
if is_wasmer or is_wasmtime:
4646
# in a wasm runtime, run the wasm, not the js
47-
filename = shared.unsuffixed(filename) + '.wasm'
47+
filename = shared.replace_suffix(filename, '.wasm')
4848
# Separates engine flags from script flags
4949
flag_separator = ['--'] if is_d8 or is_jsc else []
5050
return engine + command_flags + [filename] + shell_option_flags + flag_separator + args

tests/test_core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,7 +1735,7 @@ def test_emscripten_get_now(self):
17351735

17361736
def test_emscripten_get_compiler_setting(self):
17371737
src = test_file('core/emscripten_get_compiler_setting.c')
1738-
output = shared.unsuffixed(src) + '.out'
1738+
output = shared.replace_suffix(src, '.out')
17391739
# with assertions, a nice message is shown
17401740
self.set_setting('ASSERTIONS')
17411741
self.do_runf(src, 'You must build with -s RETAIN_COMPILER_SETTINGS=1', assert_returncode=NON_ZERO)
@@ -3532,7 +3532,7 @@ def dylink_testf(self, main, side=None, expected=None, force_c=False, main_emcc_
35323532
# Same as dylink_test but takes source code as filenames on disc.
35333533
old_args = self.emcc_args.copy()
35343534
if not expected:
3535-
outfile = shared.unsuffixed(main) + '.out'
3535+
outfile = shared.replace_suffix(main, '.out')
35363536
expected = read_file(outfile)
35373537
if not side:
35383538
side, ext = os.path.splitext(main)

tools/building.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ def scan_archive_group(group):
530530
def get_command_with_possible_response_file(cmd):
531531
# 8k is a bit of an arbitrary limit, but a reasonable one
532532
# for max command line size before we use a response file
533-
if len(' '.join(cmd)) <= 8192:
533+
if len(shared.shlex_join(cmd)) <= 8192:
534534
return cmd
535535

536536
logger.debug('using response file for %s' % cmd[0])
@@ -813,7 +813,7 @@ def move_to_safe_7bit_ascii_filename(filename):
813813
# Specify input file relative to the temp directory to avoid specifying non-7-bit-ASCII path names.
814814
args += ['--js', move_to_safe_7bit_ascii_filename(filename)]
815815
cmd = closure_cmd + args + user_args
816-
logger.debug('closure compiler: ' + ' '.join(cmd))
816+
logger.debug(f'closure compiler: {shared.shlex_join(cmd)}')
817817

818818
# Closure compiler does not work if any of the input files contain characters outside the
819819
# 7-bit ASCII range. Therefore make sure the command line we pass does not contain any such
@@ -847,7 +847,7 @@ def move_to_safe_7bit_ascii_filename(filename):
847847
logger.error(proc.stderr) # print list of errors (possibly long wall of text if input was minified)
848848

849849
# Exit and print final hint to get clearer output
850-
msg = 'closure compiler failed (rc: %d): %s' % (proc.returncode, shared.shlex_join(cmd))
850+
msg = f'closure compiler failed (rc: {proc.returncode}): {shared.shlex_join(cmd)}'
851851
if not pretty:
852852
msg += ' the error message may be clearer with -g1 and EMCC_DEBUG=2 set'
853853
exit_with_error(msg)

0 commit comments

Comments
 (0)