Skip to content

Commit 3f53117

Browse files
authored
Minor cleanups to python linking code. NFC (#12201)
- Remove unused get_final function - Inline trivial do_emscripten function - Remove meaningless return values from building.link and building.link_lld - Remove unused `just_calculate` argument from building.link
1 parent 4fc6505 commit 3f53117

File tree

3 files changed

+8
-59
lines changed

3 files changed

+8
-59
lines changed

emcc.py

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -411,18 +411,6 @@ def find_output_arg(args):
411411
return specified_target, outargs
412412

413413

414-
def do_emscripten(infile, memfile):
415-
# Run Emscripten
416-
outfile = infile + '.o.js'
417-
with ToolchainProfiler.profile_block('emscripten.py'):
418-
emscripten.run(infile, outfile, memfile)
419-
420-
# Detect compilation crashes and errors
421-
assert os.path.exists(outfile), 'Emscripten failed to generate .js'
422-
423-
return outfile
424-
425-
426414
def is_ar_file_with_missing_index(archive_file):
427415
# We parse the archive header outselves because llvm-nm --print-armap is slower and less
428416
# reliable.
@@ -2056,16 +2044,8 @@ def dedup_list(lst):
20562044
shared.Settings.EXPORTED_FUNCTIONS = dedup_list(shared.Settings.EXPORTED_FUNCTIONS)
20572045

20582046
with ToolchainProfiler.profile_block('link'):
2059-
# final will be an array if linking is deferred, otherwise a normal string.
2060-
DEFAULT_FINAL = in_temp(target_basename + '.wasm')
2061-
2062-
def get_final():
2063-
global final
2064-
if isinstance(final, list):
2065-
final = DEFAULT_FINAL
2066-
return final
2067-
20682047
logger.debug('linking: ' + str(linker_inputs))
2048+
final = in_temp(target_basename + '.wasm')
20692049

20702050
# if EMCC_DEBUG=2 then we must link now, so the temp files are complete.
20712051
# if using the wasm backend, we might be using vanilla LLVM, which does not allow our
@@ -2075,7 +2055,7 @@ def get_final():
20752055
if shared.Settings.LLD_REPORT_UNDEFINED and shared.Settings.ERROR_ON_UNDEFINED_SYMBOLS:
20762056
js_funcs = get_all_js_syms(misc_temp_files)
20772057
log_time('JS symbol generation')
2078-
final = building.link_lld(linker_inputs, DEFAULT_FINAL, external_symbol_list=js_funcs)
2058+
building.link_lld(linker_inputs, final, external_symbol_list=js_funcs)
20792059
# Special handling for when the user passed '-Wl,--version'. In this case the linker
20802060
# does not create the output file, but just prints its version and exits with 0.
20812061
if '--version' in linker_inputs:
@@ -2100,7 +2080,9 @@ def get_final():
21002080
if embed_memfile(options):
21012081
shared.Settings.SUPPORT_BASE64_EMBEDDING = 1
21022082

2103-
final = do_emscripten(final, shared.replace_or_append_suffix(target, '.mem'))
2083+
emscripten.run(final, final + '.o.js', shared.replace_or_append_suffix(target, '.mem'))
2084+
final = final + '.o.js'
2085+
21042086
save_intermediate('original')
21052087

21062088
# we also received the wasm at this stage

tests/test_other.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6377,32 +6377,6 @@ def test_canonicalize_nan_warning(self):
63776377
self.assertContained('nan\n', out)
63786378
self.assertContained('0x7fc01234\n', out)
63796379

6380-
@no_wasm_backend('tests our python linking logic')
6381-
def test_link_response_file_does_not_force_absolute_paths(self):
6382-
with_space = 'with space'
6383-
ensure_dir(with_space)
6384-
6385-
create_test_file(os.path.join(with_space, 'main.cpp'), '''
6386-
int main() {
6387-
return 0;
6388-
}
6389-
''')
6390-
6391-
building.emcc(os.path.join(with_space, 'main.cpp'), ['-c', '-g'])
6392-
6393-
with chdir(with_space):
6394-
link_args = building.link(['main.cpp.o'], 'all.bc', just_calculate=True)
6395-
6396-
time.sleep(0.2) # Wait for Windows FS to release access to the directory
6397-
shutil.rmtree(with_space)
6398-
6399-
# We want only the relative path to be in the linker args, it should not be converted to an absolute path.
6400-
if hasattr(self, 'assertCountEqual'):
6401-
self.assertCountEqual(link_args, ['main.cpp.o'])
6402-
else:
6403-
# Python 2 compatibility
6404-
self.assertItemsEqual(link_args, ['main.cpp.o'])
6405-
64066380
def test_memory_growth_noasm(self):
64076381
self.run_process([EMCC, path_from_root('tests', 'hello_world.c'), '-O2', '-s', 'ALLOW_MEMORY_GROWTH=1'])
64086382
src = open('a.out.js').read()

tools/building.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -456,17 +456,15 @@ def link_to_object(linker_inputs, target):
456456
if not Settings.LTO:
457457
link_lld(linker_inputs + ['--relocatable'], target)
458458
else:
459-
link(linker_inputs, target)
459+
link_bitcode(linker_inputs, target)
460460

461461

462462
def link_llvm(linker_inputs, target):
463463
# runs llvm-link to link things.
464464
cmd = [LLVM_LINK] + linker_inputs + ['-o', target]
465465
cmd = get_command_with_possible_response_file(cmd)
466466
print_compiler_stage(cmd)
467-
output = run_process(cmd, stdout=PIPE).stdout
468-
assert os.path.exists(target) and (output is None or 'Could not open input file' not in output), 'Linking error: ' + output
469-
return target
467+
check_call(cmd)
470468

471469

472470
def lld_flags_for_executable(external_symbol_list):
@@ -580,10 +578,9 @@ def link_lld(args, target, external_symbol_list=None):
580578
print_compiler_stage(cmd)
581579
cmd = get_command_with_possible_response_file(cmd)
582580
check_call(cmd)
583-
return target
584581

585582

586-
def link(files, target, force_archive_contents=False, just_calculate=False):
583+
def link_bitcode(files, target, force_archive_contents=False):
587584
# "Full-featured" linking: looks into archives (duplicates lld functionality)
588585
actual_files = []
589586
# Tracking unresolveds is necessary for .a linking, see below.
@@ -716,13 +713,9 @@ def scan_archive_group(group):
716713
# Finish link
717714
# tolerate people trying to link a.so a.so etc.
718715
actual_files = unique_ordered(actual_files)
719-
if just_calculate:
720-
# just calculating; return the link arguments which is the final list of files to link
721-
return actual_files
722716

723717
logger.debug('emcc: linking: %s to %s', actual_files, target)
724718
link_llvm(actual_files, target)
725-
return target
726719

727720

728721
def get_command_with_possible_response_file(cmd):

0 commit comments

Comments
 (0)