Skip to content

Commit c8dc6ee

Browse files
authored
Further refactor debug_copy: more uses, rename to save_intermediate (emscripten-core#9878)
1 parent 0d5dc73 commit c8dc6ee

File tree

4 files changed

+18
-25
lines changed

4 files changed

+18
-25
lines changed

emcc.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,6 @@
170170
}
171171

172172

173-
class Intermediate(object):
174-
counter = 0
175-
176-
177173
# this function uses the global 'final' variable, which contains the current
178174
# final output file. if a method alters final, and calls this method, then it
179175
# must modify final globally (i.e. it can't receive final as a param and
@@ -183,20 +179,17 @@ class Intermediate(object):
183179
def save_intermediate(name, suffix='js'):
184180
if not DEBUG:
185181
return
186-
name = os.path.join(shared.get_emscripten_temp_dir(), 'emcc-%d-%s.%s' % (Intermediate.counter, name, suffix))
187182
if isinstance(final, list):
188183
logger.debug('(not saving intermediate %s because deferring linking)' % name)
189184
return
190-
shutil.copyfile(final, name)
191-
Intermediate.counter += 1
185+
shared.Building.save_intermediate(final, name + '.' + suffix)
192186

193187

194188
def save_intermediate_with_wasm(name, wasm_binary):
195189
if not DEBUG:
196190
return
197191
save_intermediate(name) # save the js
198-
name = os.path.join(shared.get_emscripten_temp_dir(), 'emcc-%d-%s.wasm' % (Intermediate.counter - 1, name))
199-
shutil.copyfile(wasm_binary, name)
192+
shared.Building.save_intermediate(wasm_binary, name + '.wasm')
200193

201194

202195
class TimeLogger(object):
@@ -2000,8 +1993,6 @@ def compile_source_file(i, input_file):
20001993
if file_ending.endswith(SOURCE_ENDINGS):
20011994
temp_file = temp_files[pos][1]
20021995
logger.debug('optimizing %s', input_file)
2003-
# if DEBUG:
2004-
# shutil.copyfile(temp_file, os.path.join(shared.configuration.CANONICAL_TEMP_DIR, 'to_opt.bc')) # useful when LLVM opt aborts
20051996
new_temp_file = in_temp(unsuffixed(uniquename(temp_file)) + '.o')
20061997
# after optimizing, lower intrinsics to libc calls so that our linking code
20071998
# will find them (otherwise, llvm.cos.f32() will not link in cosf(), and
@@ -2964,7 +2955,7 @@ def do_binaryen(target, asm_target, options, memfile, wasm_binary_target,
29642955
if not shared.Settings.WASM_BACKEND:
29652956
if DEBUG:
29662957
# save the asm.js input
2967-
shared.safe_copy(asm_target, os.path.join(shared.get_emscripten_temp_dir(), os.path.basename(asm_target)))
2958+
shared.Building.save_intermediate(asm_target, 'asmjs.js')
29682959
cmd = [os.path.join(binaryen_bin, 'asm2wasm'), asm_target, '--total-memory=' + str(shared.Settings.TOTAL_MEMORY)]
29692960
if shared.Settings.BINARYEN_TRAP_MODE not in ('js', 'clamp', 'allow'):
29702961
exit_with_error('invalid BINARYEN_TRAP_MODE value: ' + shared.Settings.BINARYEN_TRAP_MODE + ' (should be js/clamp/allow)')
@@ -3037,7 +3028,7 @@ def do_binaryen(target, asm_target, options, memfile, wasm_binary_target,
30373028
if shared.Settings.STANDALONE_WASM:
30383029
options.binaryen_passes += ['--pass-arg=emscripten-sbrk-val@%d' % shared.Settings.DYNAMIC_BASE]
30393030
if DEBUG:
3040-
shared.safe_copy(wasm_binary_target, os.path.join(shared.get_emscripten_temp_dir(), os.path.basename(wasm_binary_target) + '.pre-byn'))
3031+
shared.Building.save_intermediate(wasm_binary_target, 'pre-byn.wasm')
30413032
args = options.binaryen_passes
30423033
shared.Building.run_wasm_opt(wasm_binary_target,
30433034
wasm_binary_target,
@@ -3056,7 +3047,7 @@ def do_binaryen(target, asm_target, options, memfile, wasm_binary_target,
30563047
shared.check_call([shared.PYTHON, os.path.join(binaryen_scripts, script), final, wasm_text_target], env=script_env)
30573048
if shared.Settings.EVAL_CTORS:
30583049
if DEBUG:
3059-
save_intermediate_with_wasm('pre-eval-ctors', wasm_binary_target)
3050+
shared.Building.save_intermediate(wasm_binary_target, 'pre-ctors.wasm')
30603051
shared.Building.eval_ctors(final, wasm_binary_target, binaryen_bin, debug_info=intermediate_debug_info)
30613052

30623053
# after generating the wasm, do some final operations

emscripten.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2277,14 +2277,14 @@ def finalize_wasm(temp_files, infile, outfile, memfile, DEBUG):
22772277
basename = shared.unsuffixed(outfile.name)
22782278
wasm = basename + '.wasm'
22792279
base_wasm = infile
2280-
shared.Building.debug_copy(infile, 'base.wasm')
2280+
shared.Building.save_intermediate(infile, 'base.wasm')
22812281

22822282
args = ['--detect-features']
22832283

22842284
write_source_map = shared.Settings.DEBUG_LEVEL >= 4
22852285
if write_source_map:
22862286
shared.Building.emit_wasm_source_map(base_wasm, base_wasm + '.map')
2287-
shared.Building.debug_copy(base_wasm + '.map', 'base_wasm.map')
2287+
shared.Building.save_intermediate(base_wasm + '.map', 'base_wasm.map')
22882288
args += ['--output-source-map-url=' + shared.Settings.SOURCE_MAP_BASE + os.path.basename(shared.Settings.WASM_BINARY_FILE) + '.map']
22892289

22902290
# tell binaryen to look at the features section, and if there isn't one, to use MVP
@@ -2324,8 +2324,8 @@ def finalize_wasm(temp_files, infile, outfile, memfile, DEBUG):
23242324
args=args,
23252325
stdout=subprocess.PIPE)
23262326
if write_source_map:
2327-
shared.Building.debug_copy(wasm + '.map', 'post_finalize.map')
2328-
shared.Building.debug_copy(wasm, 'post_finalize.wasm')
2327+
shared.Building.save_intermediate(wasm + '.map', 'post_finalize.map')
2328+
shared.Building.save_intermediate(wasm, 'post_finalize.wasm')
23292329

23302330
if not shared.Settings.MEM_INIT_IN_WASM:
23312331
# we have a separate .mem file. binaryen did not strip any trailing zeros,

tests/test_other.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2292,13 +2292,13 @@ def test_emcc_debug_files(self):
22922292
self.assertFalse(os.path.exists(self.canonical_temp_dir))
22932293
elif debug == '1':
22942294
if self.is_wasm_backend():
2295-
self.assertExists(os.path.join(self.canonical_temp_dir, 'emcc-0-original.js'))
2295+
self.assertExists(os.path.join(self.canonical_temp_dir, 'emcc-3-original.js'))
22962296
else:
22972297
self.assertExists(os.path.join(self.canonical_temp_dir, 'emcc-0-linktime.bc'))
22982298
self.assertExists(os.path.join(self.canonical_temp_dir, 'emcc-1-original.js'))
22992299
elif debug == '2':
23002300
if self.is_wasm_backend():
2301-
self.assertExists(os.path.join(self.canonical_temp_dir, 'emcc-0-original.js'))
2301+
self.assertExists(os.path.join(self.canonical_temp_dir, 'emcc-3-original.js'))
23022302
else:
23032303
self.assertExists(os.path.join(self.canonical_temp_dir, 'emcc-0-basebc.bc'))
23042304
self.assertExists(os.path.join(self.canonical_temp_dir, 'emcc-1-linktime.bc'))

tools/shared.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2920,20 +2920,22 @@ def run_binaryen_command(tool, infile, outfile=None, args=[], debug=False, stdou
29202920

29212921
ret = run_process(cmd, stdout=stdout).stdout
29222922
if outfile:
2923-
Building.debug_copy(outfile, '%s.wasm' % tool)
2923+
Building.save_intermediate(outfile, '%s.wasm' % tool)
29242924
return ret
29252925

29262926
@staticmethod
29272927
def run_wasm_opt(*args, **kwargs):
29282928
return Building.run_binaryen_command('wasm-opt', *args, **kwargs)
29292929

2930-
debug_copy_counter = 0
2930+
save_intermediate_counter = 0
29312931

29322932
@staticmethod
2933-
def debug_copy(src, dst):
2933+
def save_intermediate(src, dst):
29342934
if DEBUG:
2935-
shutil.copyfile(src, os.path.join(CANONICAL_TEMP_DIR, 'emdebug-%d-%s' % (Building.debug_copy_counter, dst)))
2936-
Building.debug_copy_counter += 1
2935+
dst = 'emcc-%d-%s' % (Building.save_intermediate_counter, dst)
2936+
Building.save_intermediate_counter += 1
2937+
logger.debug('saving debug copy %s' % dst)
2938+
shutil.copyfile(src, os.path.join(CANONICAL_TEMP_DIR, dst))
29372939

29382940

29392941
# compatibility with existing emcc, etc. scripts

0 commit comments

Comments
 (0)