Skip to content

Commit 3b2372c

Browse files
authored
Save intermediate object files when --save-temps is used. NFC (#22239)
clang takes care of saving the `.s`, `.i` and `.bc` files but its up to the compiler driver (in this case emcc) to save the temporary object files. Interestingly if two source files have the same basename then `clang --save-temps` doesn't work since the object files will clobber each other in the current working directory. This is probably a bug in clang so I didn't recreate it here.
1 parent 7de784e commit 3b2372c

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

emcc.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import os
2828
import re
2929
import shlex
30+
import shutil
3031
import sys
3132
import time
3233
import tarfile
@@ -133,6 +134,7 @@ def __init__(self):
133134
self.output_file = None
134135
self.no_minify = False
135136
self.post_link = False
137+
self.save_temps = False
136138
self.executable = False
137139
self.compiler_wrapper = None
138140
self.oformat = None
@@ -984,6 +986,7 @@ def get_clang_command_asm():
984986
# with -MF! (clang seems to not recognize it)
985987
logger.debug(('just preprocessor ' if state.has_dash_E else 'just dependencies: ') + ' '.join(cmd))
986988
shared.exec_process(cmd)
989+
assert False, 'exec_process does not return'
987990

988991
# Precompiled headers support
989992
if state.mode == Mode.PCH:
@@ -996,6 +999,7 @@ def get_clang_command_asm():
996999
cmd += ['-o', options.output_file]
9971000
logger.debug(f"running (for precompiled headers): {cmd[0]} {' '.join(cmd[1:])}")
9981001
shared.exec_process(cmd)
1002+
assert False, 'exec_process does not return'
9991003

10001004
if state.mode == Mode.COMPILE_ONLY:
10011005
inputs = [i[1] for i in input_files]
@@ -1008,6 +1012,7 @@ def get_clang_command_asm():
10081012
if get_file_suffix(options.output_file) == '.bc' and not settings.LTO and '-emit-llvm' not in state.orig_args:
10091013
diagnostics.warning('emcc', '.bc output file suffix used without -flto or -emit-llvm. Consider using .o extension since emcc will output an object file, not a bitcode file')
10101014
shared.exec_process(cmd)
1015+
assert False, 'exec_process does not return'
10111016

10121017
# In COMPILE_AND_LINK we need to compile source files too, but we also need to
10131018
# filter out the link flags
@@ -1054,8 +1059,10 @@ def compile_source_file(i, input_file):
10541059
cmd += ['-Xclang', '-split-dwarf-file', '-Xclang', unsuffixed_basename(input_file) + '.dwo']
10551060
cmd += ['-Xclang', '-split-dwarf-output', '-Xclang', unsuffixed_basename(input_file) + '.dwo']
10561061
shared.check_call(cmd)
1057-
if output_file not in ('-', os.devnull) and not shared.SKIP_SUBPROCS:
1062+
if not shared.SKIP_SUBPROCS:
10581063
assert os.path.exists(output_file)
1064+
if options.save_temps:
1065+
shutil.copyfile(output_file, shared.unsuffixed_basename(input_file) + '.o')
10591066

10601067
# First, generate LLVM bitcode. For each input file, we get base.o with bitcode
10611068
for i, input_file in input_files:
@@ -1184,6 +1191,8 @@ def consume_arg_file():
11841191
settings.LTO = 'full'
11851192
elif arg == "-fno-lto":
11861193
settings.LTO = 0
1194+
elif arg == "--save-temps":
1195+
options.save_temps = True
11871196
elif check_arg('--llvm-lto'):
11881197
logger.warning('--llvm-lto ignored when using llvm backend')
11891198
consume_arg()

test/test_other.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14940,3 +14940,13 @@ def test_std_promise_link(self, *args):
1494014940

1494114941
def test_stack_protector(self):
1494214942
self.do_other_test('test_stack_protector.c', emcc_args=['-fstack-protector'], assert_returncode=NON_ZERO)
14943+
14944+
def test_save_temp(self):
14945+
self.run_process([EMCC, '--save-temps', test_file('hello_world.c')])
14946+
self.assertExists('a.out.js')
14947+
# clang itself takes care of creating these three
14948+
self.assertExists('hello_world.i')
14949+
self.assertExists('hello_world.s')
14950+
self.assertExists('hello_world.bc')
14951+
# emcc takes care of creating the .o
14952+
self.assertExists('hello_world.o')

0 commit comments

Comments
 (0)