Skip to content

Commit 9d6742a

Browse files
authored
Move all command line flag processing to emcc.py. NFC (#23497)
This change paves to the way for link.py refactor I'm working on, but it also seems like a good idea to keep argument processing in one place.
1 parent db69527 commit 9d6742a

File tree

3 files changed

+43
-28
lines changed

3 files changed

+43
-28
lines changed

emcc.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,13 @@ def __init__(self):
183183
self.dash_S = False
184184
self.dash_M = False
185185
self.input_language = None
186+
self.nostdlib = False
187+
self.nostdlibxx = False
188+
self.nodefaultlibs = False
189+
self.nolibc = False
190+
self.nostartfiles = False
191+
self.sanitize_minimal_runtime = False
192+
self.sanitize = set()
186193

187194

188195
def create_reproduce_file(name, args):
@@ -1421,6 +1428,22 @@ def consume_arg_file():
14211428
# SSEx is implemented on top of SIMD128 instruction set, but do not pass SSE flags to LLVM
14221429
# so it won't think about generating native x86 SSE code.
14231430
newargs[i] = ''
1431+
elif arg == '-nostdlib':
1432+
options.nostdlib = True
1433+
elif arg == '-nostdlibxx':
1434+
options.nostdlibxx = True
1435+
elif arg == '-nodefaultlibs':
1436+
options.nodefaultlibs = True
1437+
elif arg == '-nolibc':
1438+
options.nolibc = True
1439+
elif arg == '-nostartfiles':
1440+
options.nostartfiles = True
1441+
elif arg == '-fsanitize-minimal-runtime':
1442+
options.sanitize_minimal_runtime = True
1443+
elif arg.startswith('-fsanitize='):
1444+
options.sanitize.update(arg.split('=', 1)[1].split(','))
1445+
elif arg.startswith('-fno-sanitize='):
1446+
options.sanitize.difference_update(arg.split('=', 1)[1].split(','))
14241447
elif arg and (arg == '-' or not arg.startswith('-')):
14251448
options.input_files.append(arg)
14261449

tools/link.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ def phase_linker_setup(options, state): # noqa: C901, PLR0912, PLR0915
655655
if not shared.SKIP_SUBPROCS:
656656
shared.check_llvm_version()
657657

658-
autoconf = os.environ.get('EMMAKEN_JUST_CONFIGURE') or 'conftest.c' in state.orig_args or 'conftest.cpp' in state.orig_args
658+
autoconf = os.environ.get('EMMAKEN_JUST_CONFIGURE') or 'conftest.c' in options.input_files or 'conftest.cpp' in options.input_files
659659
if autoconf:
660660
# configure tests want a more shell-like style, where we emit return codes on exit()
661661
settings.EXIT_RUNTIME = 1
@@ -899,7 +899,7 @@ def phase_linker_setup(options, state): # noqa: C901, PLR0912, PLR0915
899899
# PURE_WASI, or when we are linking without standard libraries because
900900
# STACK_OVERFLOW_CHECK depends on emscripten_stack_get_end which is defined
901901
# in libcompiler-rt.
902-
if not settings.PURE_WASI and '-nostdlib' not in state.orig_args and '-nodefaultlibs' not in state.orig_args:
902+
if not settings.PURE_WASI and not options.nostdlib and not options.nodefaultlibs:
903903
default_setting('STACK_OVERFLOW_CHECK', max(settings.ASSERTIONS, settings.STACK_OVERFLOW_CHECK))
904904

905905
# For users that opt out of WARN_ON_UNDEFINED_SYMBOLS we assume they also
@@ -1536,15 +1536,7 @@ def phase_linker_setup(options, state): # noqa: C901, PLR0912, PLR0915
15361536
if settings.SIDE_MODULE and shared.suffix(target) == '.js':
15371537
diagnostics.warning('emcc', 'output suffix .js requested, but wasm side modules are just wasm files; emitting only a .wasm, no .js')
15381538

1539-
sanitize = set()
1540-
1541-
for arg in state.orig_args:
1542-
if arg.startswith('-fsanitize='):
1543-
sanitize.update(arg.split('=', 1)[1].split(','))
1544-
elif arg.startswith('-fno-sanitize='):
1545-
sanitize.difference_update(arg.split('=', 1)[1].split(','))
1546-
1547-
if sanitize:
1539+
if options.sanitize:
15481540
settings.USE_OFFSET_CONVERTER = 1
15491541
# These symbols are needed by `withBuiltinMalloc` which used to implement
15501542
# the `__noleakcheck` attribute. However this dependency is not yet represented in the JS
@@ -1560,7 +1552,7 @@ def phase_linker_setup(options, state): # noqa: C901, PLR0912, PLR0915
15601552
'emscripten_builtin_free',
15611553
]
15621554

1563-
if ('leak' in sanitize or 'address' in sanitize) and not settings.ALLOW_MEMORY_GROWTH:
1555+
if ('leak' in options.sanitize or 'address' in options.sanitize) and not settings.ALLOW_MEMORY_GROWTH:
15641556
# Increase the minimum memory requirements to account for extra memory
15651557
# that the sanitizers might need (in addition to the shadow memory
15661558
# requirements handled below).
@@ -1576,17 +1568,17 @@ def phase_linker_setup(options, state): # noqa: C901, PLR0912, PLR0915
15761568
exit_with_error('wasm2js is not compatible with USE_OFFSET_CONVERTER (see #14630)')
15771569
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE.append('$UTF8ArrayToString')
15781570

1579-
if sanitize & UBSAN_SANITIZERS:
1580-
if '-fsanitize-minimal-runtime' in state.orig_args:
1571+
if options.sanitize & UBSAN_SANITIZERS:
1572+
if options.sanitize_minimal_runtime:
15811573
settings.UBSAN_RUNTIME = 1
15821574
else:
15831575
settings.UBSAN_RUNTIME = 2
15841576

1585-
if 'leak' in sanitize:
1577+
if 'leak' in options.sanitize:
15861578
settings.USE_LSAN = 1
15871579
default_setting('EXIT_RUNTIME', 1)
15881580

1589-
if 'address' in sanitize:
1581+
if 'address' in options.sanitize:
15901582
settings.USE_ASAN = 1
15911583
default_setting('EXIT_RUNTIME', 1)
15921584
if not settings.UBSAN_RUNTIME:
@@ -1661,7 +1653,7 @@ def phase_linker_setup(options, state): # noqa: C901, PLR0912, PLR0915
16611653
# ASan and SAFE_HEAP check address 0 themselves
16621654
settings.CHECK_NULL_WRITES = 0
16631655

1664-
if sanitize and settings.GENERATE_SOURCE_MAP:
1656+
if options.sanitize and settings.GENERATE_SOURCE_MAP:
16651657
settings.LOAD_SOURCE_MAP = 1
16661658

16671659
if 'GLOBAL_BASE' not in user_settings and not settings.SHRINK_LEVEL and not settings.OPT_LEVEL and not settings.USE_ASAN:
@@ -1792,7 +1784,7 @@ def get_full_import_name(name):
17921784
settings.EMSCRIPTEN_VERSION = utils.EMSCRIPTEN_VERSION
17931785
settings.SOURCE_MAP_BASE = options.source_map_base or ''
17941786

1795-
settings.LINK_AS_CXX = (shared.run_via_emxx or settings.DEFAULT_TO_CXX) and '-nostdlib++' not in state.orig_args
1787+
settings.LINK_AS_CXX = (shared.run_via_emxx or settings.DEFAULT_TO_CXX) and not options.nostdlibxx
17961788

17971789
# WASMFS itself is written in C++, and needs C++ standard libraries
17981790
if settings.WASMFS:
@@ -1867,13 +1859,13 @@ def get_full_import_name(name):
18671859

18681860

18691861
@ToolchainProfiler.profile_block('calculate system libraries')
1870-
def phase_calculate_system_libraries(state, linker_arguments):
1862+
def phase_calculate_system_libraries(options, linker_arguments):
18711863
extra_files_to_link = []
18721864
# Link in ports and system libraries, if necessary
18731865
if not settings.SIDE_MODULE:
18741866
# Ports are always linked into the main module, never the side module.
18751867
extra_files_to_link += ports.get_libs(settings)
1876-
extra_files_to_link += system_libs.calculate(state.orig_args)
1868+
extra_files_to_link += system_libs.calculate(options)
18771869
linker_arguments.extend(extra_files_to_link)
18781870

18791871

@@ -3128,7 +3120,7 @@ def run(linker_inputs, options, state):
31283120
logger.debug('stopping after linking to object file')
31293121
return 0
31303122

3131-
phase_calculate_system_libraries(state, linker_arguments)
3123+
phase_calculate_system_libraries(options, linker_arguments)
31323124

31333125
js_syms = {}
31343126
if (not settings.SIDE_MODULE or settings.ASYNCIFY) and not shared.SKIP_SUBPROCS:

tools/system_libs.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,10 +2234,10 @@ class libstubs(DebugLibrary):
22342234
src_files = ['emscripten_syscall_stubs.c', 'emscripten_libc_stubs.c']
22352235

22362236

2237-
def get_libs_to_link(args):
2237+
def get_libs_to_link(options):
22382238
libs_to_link = []
22392239

2240-
if '-nostdlib' in args:
2240+
if options.nostdlib:
22412241
return libs_to_link
22422242

22432243
already_included = set()
@@ -2277,7 +2277,7 @@ def add_library(libname, whole_archive=False):
22772277
need_whole_archive = lib.name in force_include and lib.get_ext() == '.a'
22782278
libs_to_link.append((lib.get_link_flag(), whole_archive or need_whole_archive))
22792279

2280-
if '-nostartfiles' not in args:
2280+
if not options.nostartfiles:
22812281
if settings.SHARED_MEMORY:
22822282
add_library('crtbegin')
22832283

@@ -2302,7 +2302,7 @@ def add_forced_libs():
23022302
shared.exit_with_error('invalid forced library: %s', forced)
23032303
add_library(forced)
23042304

2305-
if '-nodefaultlibs' in args:
2305+
if options.nodefaultlibs:
23062306
add_forced_libs()
23072307
return libs_to_link
23082308

@@ -2355,7 +2355,7 @@ def add_sanitizer_libs():
23552355
add_library('libstandalonewasm')
23562356
if settings.ALLOW_UNIMPLEMENTED_SYSCALLS:
23572357
add_library('libstubs')
2358-
if '-nolibc' not in args:
2358+
if not options.nolibc:
23592359
if not settings.EXIT_RUNTIME:
23602360
add_library('libnoexit')
23612361
add_library('libc')
@@ -2407,9 +2407,9 @@ def add_sanitizer_libs():
24072407
return libs_to_link
24082408

24092409

2410-
def calculate(args):
2410+
def calculate(options):
24112411

2412-
libs_to_link = get_libs_to_link(args)
2412+
libs_to_link = get_libs_to_link(options)
24132413

24142414
# When LINKABLE is set the entire link command line is wrapped in --whole-archive by
24152415
# building.link_ldd. And since --whole-archive/--no-whole-archive processing does not nest we

0 commit comments

Comments
 (0)