Skip to content

Commit

Permalink
[emcc.py] Move -c/-s/-E parsing the main arg parsing loop. NFC (#23434)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 authored Jan 16, 2025
1 parent c3bdb49 commit e1c5e8f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
34 changes: 21 additions & 13 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,6 @@ def __init__(self, args):
self.mode = Mode.COMPILE_AND_LINK
# Using tuple here to prevent accidental mutation
self.orig_args = tuple(args)
self.has_dash_c = False
self.has_dash_E = False
self.has_dash_S = False
# List of link options paired with their position on the command line [(i, option), ...].
self.link_flags = []
self.lib_dirs = []
Expand Down Expand Up @@ -179,6 +176,11 @@ def __init__(self):
self.shared = False
self.relocatable = False
self.reproduce = None
self.syntax_only = False
self.dash_c = False
self.dash_E = False
self.dash_S = False
self.dash_M = False


def create_reproduce_file(name, args):
Expand Down Expand Up @@ -817,17 +819,13 @@ def phase_setup(options, state, newargs):
# so it won't think about generating native x86 SSE code.
newargs = [x for x in newargs if x not in SIMD_INTEL_FEATURE_TOWER and x not in SIMD_NEON_FLAGS]

state.has_dash_c = '-c' in newargs or '--precompile' in newargs
state.has_dash_S = '-S' in newargs
state.has_dash_E = '-E' in newargs

if options.post_link:
state.mode = Mode.POST_LINK_ONLY
elif state.has_dash_E or '-M' in newargs or '-MM' in newargs or '-fsyntax-only' in newargs:
elif options.dash_E or options.dash_M:
state.mode = Mode.PREPROCESS_ONLY
elif has_header_inputs:
state.mode = Mode.PCH
elif state.has_dash_c or state.has_dash_S:
elif options.dash_c or options.dash_S or options.syntax_only:
state.mode = Mode.COMPILE_ONLY

if state.mode in (Mode.COMPILE_ONLY, Mode.PREPROCESS_ONLY):
Expand Down Expand Up @@ -984,7 +982,7 @@ def get_clang_command_preprocessed():
def get_clang_command_asm():
return compiler + get_target_flags() + compile_args

# preprocessor-only (-E) support
# preprocessor-only (-E/-M) support
if state.mode == Mode.PREPROCESS_ONLY:
inputs = [i[1] for i in input_files]
cmd = get_clang_command() + inputs
Expand All @@ -993,7 +991,7 @@ def get_clang_command_asm():
# Do not compile, but just output the result from preprocessing stage or
# output the dependency rule. Warning: clang and gcc behave differently
# with -MF! (clang seems to not recognize it)
logger.debug(('just preprocessor ' if state.has_dash_E else 'just dependencies: ') + ' '.join(cmd))
logger.debug(('just preprocessor: ' if options.dash_E else 'just dependencies: ') + ' '.join(cmd))
shared.exec_process(cmd)
assert False, 'exec_process does not return'

Expand Down Expand Up @@ -1026,7 +1024,7 @@ def get_clang_command_asm():
# In COMPILE_AND_LINK we need to compile source files too, but we also need to
# filter out the link flags
assert state.mode == Mode.COMPILE_AND_LINK
assert not state.has_dash_c
assert not options.dash_c
compile_args = filter_out_link_flags(compile_args)
linker_inputs = []
seen_names = {}
Expand Down Expand Up @@ -1069,7 +1067,7 @@ def compile_source_file(i, input_file):
# First, generate LLVM bitcode. For each input file, we get base.o with bitcode
for i, input_file in input_files:
file_suffix = get_file_suffix(input_file)
if file_suffix in SOURCE_ENDINGS + ASSEMBLY_ENDINGS or (state.has_dash_c and file_suffix == '.bc'):
if file_suffix in SOURCE_ENDINGS + ASSEMBLY_ENDINGS or (options.dash_c and file_suffix == '.bc'):
compile_source_file(i, input_file)
elif file_suffix in DYNAMICLIB_ENDINGS:
logger.debug(f'using shared library: {input_file}')
Expand Down Expand Up @@ -1486,6 +1484,16 @@ def consume_arg_file():
exit_with_error(f'unsupported target: {options.target} (emcc only supports wasm64-unknown-emscripten and wasm32-unknown-emscripten)')
elif check_arg('--use-port'):
ports.handle_use_port_arg(settings, consume_arg())
elif arg in ('-c', '--precompile'):
options.dash_c = True
elif arg == '-S':
options.dash_S = True
elif arg == '-E':
options.dash_E = True
elif arg in ('-M', '-MM'):
options.dash_M = True
elif arg == '-fsyntax-only':
options.syntax_only = True

if should_exit:
sys.exit(0)
Expand Down
3 changes: 3 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -3861,6 +3861,9 @@ def test_syntax_only_valid(self):
result = self.run_process([EMCC, test_file('hello_world.c'), '-fsyntax-only'], stdout=PIPE, stderr=STDOUT)
self.assertEqual(result.stdout, '')
self.assertNotExists('a.out.js')
# Even with `-c` and/or `-o`, `-fsyntax-only` should not produce any output
self.run_process([EMCC, test_file('hello_world.c'), '-fsyntax-only', '-c', '-o', 'out.o'])
self.assertNotExists('out.o')

def test_syntax_only_invalid(self):
create_file('src.c', 'int main() {')
Expand Down

0 comments on commit e1c5e8f

Please sign in to comment.