Skip to content

Commit e30afe8

Browse files
committed
Use cmdline.options consistently. NFC
We had some parts of the codebase that were using the ambient/global `cmdline.options` and other that were passing the options object around as as a param. For a tool like a compiler it think use the global options options object if pretty reasonable and keeps the code simple.
1 parent f11cc0b commit e30afe8

5 files changed

Lines changed: 61 additions & 57 deletions

File tree

emcc.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,17 +344,17 @@ def main(args):
344344
linker_args = [f.value for f in linker_args]
345345
# Delay import of link.py to avoid processing this file when only compiling
346346
from tools import link
347-
link.run_post_link(options.input_files[0], options, linker_args)
347+
link.run_post_link(options.input_files[0], linker_args)
348348
return 0
349349

350350
# Compile source code to object files
351351
# When only compiling this function never returns.
352-
linker_args = phase_compile_inputs(options, state, newargs)
352+
linker_args = phase_compile_inputs(state, newargs)
353353

354354
if state.mode == Mode.COMPILE_AND_LINK:
355355
# Delay import of link.py to avoid processing this file when only compiling
356356
from tools import link
357-
return link.run(options, linker_args)
357+
return link.run(linker_args)
358358
else:
359359
logger.debug('stopping after compile phase')
360360
return 0
@@ -502,7 +502,7 @@ def phase_setup(state):
502502

503503

504504
@ToolchainProfiler.profile_block('compile inputs')
505-
def phase_compile_inputs(options, state, newargs):
505+
def phase_compile_inputs(state, newargs):
506506
if shared.run_via_emxx:
507507
compiler = [shared.CLANG_CXX]
508508
else:

tools/cmdline.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
config,
1919
diagnostics,
2020
feature_matrix,
21-
ports,
2221
shared,
2322
utils,
2423
)
@@ -204,6 +203,9 @@ def parse_args(newargs): # ruff: ignore[complex-structure, too-many-branches, t
204203
205204
To revalidate these numbers, run `ruff check --select=C901,PLR091`.
206205
"""
206+
# TODO(sbc): Remove this import, or move it to the top, once we resolve the
207+
# circuular dependency issue with ports/__init__.py -> system_libs.py -> cmdline.py
208+
from tools import ports
207209
should_exit = False
208210
skip = False
209211
builtin_settings = set(settings.keys())

tools/link.py

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
utils,
3232
webassembly,
3333
)
34-
from .cmdline import OFormat
34+
from .cmdline import OFormat, options
3535
from .feature_matrix import Feature
3636
from .minimal_runtime_shell import generate_minimal_runtime_html
3737
from .settings import (
@@ -339,7 +339,7 @@ def get_binaryen_lowering_passes():
339339
return passes
340340

341341

342-
def get_binaryen_passes(options):
342+
def get_binaryen_passes():
343343
passes = get_binaryen_lowering_passes()
344344
optimizing = should_run_binaryen_optimizer()
345345

@@ -473,7 +473,7 @@ def make_js_executable(script):
473473
pass # can fail if e.g. writing the executable to /dev/null
474474

475475

476-
def do_split_module(wasm_file, options):
476+
def do_split_module(wasm_file):
477477
os.replace(wasm_file, wasm_file + '.orig')
478478
args = ['--instrument']
479479
if options.requested_debug:
@@ -669,7 +669,7 @@ def check_settings():
669669

670670

671671
@ToolchainProfiler.profile()
672-
def setup_sanitizers(options):
672+
def setup_sanitizers():
673673
assert options.sanitize
674674

675675
if settings.WASM_WORKERS:
@@ -774,7 +774,7 @@ def setup_sanitizers(options):
774774
settings.LOAD_SOURCE_MAP = 1
775775

776776

777-
def get_dylibs(options, linker_args):
777+
def get_dylibs(linker_args):
778778
"""Find all the Wasm dynamic libraries specified on the command line.
779779
780780
This can either be via `-lfoo` or via `libfoo.so` directly.
@@ -847,7 +847,7 @@ def add_required_heap_symbols():
847847

848848

849849
@ToolchainProfiler.profile_block('linker_setup')
850-
def phase_linker_setup(options, linker_args): # ruff: ignore[complex-structure, too-many-branches, too-many-statements]
850+
def phase_linker_setup(linker_args): # ruff: ignore[complex-structure, too-many-branches, too-many-statements]
851851
"""Future modifications should consider refactoring to reduce complexity.
852852
853853
* The McCabe cyclomatiic complexity is currently 244 vs 10 recommended.
@@ -864,13 +864,13 @@ def phase_linker_setup(options, linker_args): # ruff: ignore[complex-structure,
864864
default_setting('SIDE_MODULE', 1)
865865

866866
if not settings.SIDE_MODULE and not settings.FAKE_DYLIBS:
867-
options.dylibs = get_dylibs(options, linker_args)
867+
options.dylibs = get_dylibs(linker_args)
868868
# If there are any dynamic libraries on the command line then enable
869869
# `MAIN_MODULE` by default in order to produce JS code that can load them.
870870
if options.dylibs and not settings.MAIN_MODULE:
871871
default_setting('MAIN_MODULE', 2)
872872

873-
linker_args += calc_extra_ldflags(options)
873+
linker_args += calc_extra_ldflags()
874874

875875
# We used to do this check during on startup during `check_sanity`, but
876876
# we now only do it when linking, in order to reduce the overhead when
@@ -1697,7 +1697,7 @@ def limit_incoming_module_api():
16971697
diagnostics.warning('emcc', 'JavaScript output suffix requested, but wasm side modules are just wasm files; emitting only a .wasm, no .js')
16981698

16991699
if options.sanitize:
1700-
setup_sanitizers(options)
1700+
setup_sanitizers()
17011701

17021702
if settings.USE_ASAN or settings.SAFE_HEAP:
17031703
# ASan and SAFE_HEAP check address 0 themselves
@@ -1898,13 +1898,13 @@ def get_full_import_name(name):
18981898

18991899

19001900
@ToolchainProfiler.profile_block('calculate system libraries')
1901-
def phase_calculate_system_libraries(options):
1901+
def phase_calculate_system_libraries():
19021902
extra_files_to_link = []
19031903
# Link in ports and system libraries, if necessary
19041904
if not settings.SIDE_MODULE:
19051905
# Ports are always linked into the main module, never the side module.
19061906
extra_files_to_link += ports.get_libs(settings)
1907-
extra_files_to_link += system_libs.calculate(options)
1907+
extra_files_to_link += system_libs.calculate()
19081908
return extra_files_to_link
19091909

19101910

@@ -1941,7 +1941,7 @@ def phase_link(linker_args, linker_inputs, wasm_target, js_syms):
19411941

19421942

19431943
@ToolchainProfiler.profile_block('post link')
1944-
def phase_post_link(options, in_wasm, wasm_target, target, js_syms, base_metadata=None):
1944+
def phase_post_link(in_wasm, wasm_target, target, js_syms, base_metadata=None):
19451945
global final_js
19461946

19471947
target_basename = unsuffixed_basename(target)
@@ -1965,15 +1965,15 @@ def phase_post_link(options, in_wasm, wasm_target, target, js_syms, base_metadat
19651965
metadata = phase_emscript(in_wasm, wasm_target, js_syms, base_metadata)
19661966

19671967
if settings.EMBIND_AOT:
1968-
phase_embind_aot(options, wasm_target, js_syms)
1968+
phase_embind_aot(wasm_target, js_syms)
19691969

19701970
if options.emit_tsd:
1971-
phase_emit_tsd(options, wasm_target, js_target, js_syms, metadata)
1971+
phase_emit_tsd(wasm_target, js_target, js_syms, metadata)
19721972

19731973
if options.js_transform:
1974-
phase_source_transforms(options)
1974+
phase_source_transforms()
19751975

1976-
phase_binaryen(target, options, wasm_target)
1976+
phase_binaryen(target, wasm_target)
19771977

19781978
# Compute the SHA-256 hash of the final wasm (after binaryen) and substitute
19791979
# the <<< WASM_HASH_VALUE >>> placeholder that preamble.js left in the JS.
@@ -1985,7 +1985,7 @@ def phase_post_link(options, in_wasm, wasm_target, target, js_syms, base_metadat
19851985

19861986
# If we are not emitting any JS then we are all done now
19871987
if options.oformat != OFormat.WASM:
1988-
phase_final_emitting(options, target, js_target, wasm_target)
1988+
phase_final_emitting(target, js_target, wasm_target)
19891989

19901990

19911991
@ToolchainProfiler.profile_block('emscript')
@@ -2007,7 +2007,7 @@ def phase_emscript(in_wasm, wasm_target, js_syms, base_metadata):
20072007
return metadata
20082008

20092009

2010-
def run_embind_gen(options, wasm_target, js_syms, extra_settings):
2010+
def run_embind_gen(wasm_target, js_syms, extra_settings):
20112011
# Save settings so they can be restored after TS generation.
20122012
original_settings = settings.backup()
20132013
settings.attrs.update(extra_settings)
@@ -2096,12 +2096,12 @@ def run_embind_gen(options, wasm_target, js_syms, extra_settings):
20962096

20972097

20982098
@ToolchainProfiler.profile_block('emit tsd')
2099-
def phase_emit_tsd(options, wasm_target, js_target, js_syms, metadata):
2099+
def phase_emit_tsd(wasm_target, js_target, js_syms, metadata):
21002100
logger.debug('emit tsd')
21012101
filename = options.emit_tsd
21022102
embind_tsd = ''
21032103
if settings.EMBIND:
2104-
embind_tsd = run_embind_gen(options, wasm_target, js_syms, {'EMBIND_AOT': False})
2104+
embind_tsd = run_embind_gen(wasm_target, js_syms, {'EMBIND_AOT': False})
21052105
bindgen_ts_files = []
21062106
if settings.WASM_BINDGEN:
21072107
bindgen_ts_files = glob.glob(get_emscripten_temp_dir() + '/bindgen_out/*.d.ts')
@@ -2113,8 +2113,8 @@ def phase_emit_tsd(options, wasm_target, js_target, js_syms, metadata):
21132113

21142114

21152115
@ToolchainProfiler.profile_block('embind aot js')
2116-
def phase_embind_aot(options, wasm_target, js_syms):
2117-
out = run_embind_gen(options, wasm_target, js_syms, {})
2116+
def phase_embind_aot(wasm_target, js_syms):
2117+
out = run_embind_gen(wasm_target, js_syms, {})
21182118
if DEBUG:
21192119
write_file(in_temp('embind_aot.json'), out)
21202120
out = json.loads(out)
@@ -2153,7 +2153,7 @@ def remove_quotes(arg):
21532153

21542154

21552155
@ToolchainProfiler.profile_block('source transforms')
2156-
def phase_source_transforms(options):
2156+
def phase_source_transforms():
21572157
# Apply a source code transformation, if requested
21582158
global final_js
21592159
safe_copy(final_js, final_js + '.tr.js')
@@ -2254,7 +2254,7 @@ def convert_line_endings_in_file(filename, to_eol):
22542254

22552255

22562256
@ToolchainProfiler.profile_block('final emitting')
2257-
def phase_final_emitting(options, target, js_target, wasm_target):
2257+
def phase_final_emitting(target, js_target, wasm_target):
22582258
global final_js
22592259

22602260
if shared.SKIP_SUBPROCS:
@@ -2319,18 +2319,18 @@ def phase_final_emitting(options, target, js_target, wasm_target):
23192319

23202320
# If we were asked to also generate HTML, do that
23212321
if options.oformat == OFormat.HTML:
2322-
generate_html(target, options, js_target, target_basename,
2322+
generate_html(target, js_target, target_basename,
23232323
wasm_target)
23242324

23252325
if settings.SPLIT_MODULE:
2326-
do_split_module(wasm_target, options)
2326+
do_split_module(wasm_target)
23272327

23282328
if settings.EXECUTABLE:
23292329
make_js_executable(js_target)
23302330

23312331

23322332
@ToolchainProfiler.profile_block('binaryen')
2333-
def phase_binaryen(target, options, wasm_target):
2333+
def phase_binaryen(target, wasm_target):
23342334
global final_js
23352335
logger.debug('using binaryen')
23362336
# whether we need to emit -g (function name debug info) in the final wasm
@@ -2353,7 +2353,7 @@ def phase_binaryen(target, options, wasm_target):
23532353
# run wasm-opt if we have work for it: either passes, or if we are using
23542354
# source maps (which requires some extra processing to keep the source map
23552355
# but remove DWARF)
2356-
passes = get_binaryen_passes(options)
2356+
passes = get_binaryen_passes()
23572357
if passes:
23582358
# if asyncify is used, we will use it in the next stage, and so if it is
23592359
# the only reason we need intermediate debug info, we can stop keeping it
@@ -2547,7 +2547,7 @@ def module_export_name_substitution():
25472547
save_intermediate('module_export_name_substitution')
25482548

25492549

2550-
def generate_traditional_runtime_html(target, options, js_target, wasm_target):
2550+
def generate_traditional_runtime_html(target, js_target, wasm_target):
25512551
script = ScriptSource()
25522552

25532553
if settings.EXPORT_NAME != 'Module' and options.shell_html == DEFAULT_SHELL_HTML:
@@ -2686,13 +2686,13 @@ def unescape_nulls(filename):
26862686
logger.debug(f'HTML minification shrunk {filename} from {size_before} to {size_after} bytes, delta={delta} ({delta * 100.0 / size_before:+.2f}%)')
26872687

26882688

2689-
def generate_html(target, options, js_target, target_basename, wasm_target):
2689+
def generate_html(target, js_target, target_basename, wasm_target):
26902690
logger.debug('generating HTML')
26912691

26922692
if settings.MINIMAL_RUNTIME:
2693-
generate_minimal_runtime_html(target, options, js_target, target_basename)
2693+
generate_minimal_runtime_html(target, js_target, target_basename)
26942694
else:
2695-
generate_traditional_runtime_html(target, options, js_target, wasm_target)
2695+
generate_traditional_runtime_html(target, js_target, wasm_target)
26962696

26972697
if settings.MINIFY_HTML and (settings.OPT_LEVEL >= 1 or settings.SHRINK_LEVEL >= 1):
26982698
minify_html(target)
@@ -2759,7 +2759,7 @@ def map_to_js_libs(library_name):
27592759
return None
27602760

27612761

2762-
def process_libraries(options, flags):
2762+
def process_libraries(flags):
27632763
"""Process `-l` and `--js-library` flags."""
27642764
new_flags = []
27652765
system_libs_map = system_libs.Library.get_usable_variations()
@@ -2886,7 +2886,7 @@ def replacement(self):
28862886
return f'<script id="mainScript">\n{self.inline}\n</script>'
28872887

28882888

2889-
def filter_out_fake_dynamic_libs(options, inputs):
2889+
def filter_out_fake_dynamic_libs(inputs):
28902890
"""Filter out "fake" dynamic libraries that are really just intermediate object files."""
28912891
def is_fake_dylib(input_file):
28922892
if get_file_suffix(input_file) in DYLIB_EXTENSIONS and os.path.exists(input_file) and not building.is_wasm_dylib(input_file):
@@ -3067,7 +3067,7 @@ def get_subresource_location_js(path):
30673067

30683068

30693069
@ToolchainProfiler.profile()
3070-
def package_files(options, target):
3070+
def package_files(target):
30713071
rtn = []
30723072
logger.debug('setting up files')
30733073
file_args = ['--from-emcc']
@@ -3110,7 +3110,7 @@ def package_files(options, target):
31103110

31113111

31123112
@ToolchainProfiler.profile_block('calculate linker inputs')
3113-
def phase_calculate_linker_inputs(options, linker_args):
3113+
def phase_calculate_linker_inputs(linker_args):
31143114
using_lld = not (options.oformat == OFormat.OBJECT and settings.LTO)
31153115

31163116
linker_args = filter_link_flags(linker_args, using_lld)
@@ -3129,7 +3129,7 @@ def phase_calculate_linker_inputs(options, linker_args):
31293129
return linker_args
31303130

31313131

3132-
def calc_extra_ldflags(options):
3132+
def calc_extra_ldflags():
31333133
extra_args = []
31343134
system_libpath = str(cache.get_lib_dir(absolute=True))
31353135
system_js_path = utils.path_from_root('src', 'lib')
@@ -3155,14 +3155,14 @@ def calc_extra_ldflags(options):
31553155
return extra_args
31563156

31573157

3158-
def run_post_link(wasm_input, options, linker_args):
3158+
def run_post_link(wasm_input, linker_args):
31593159
settings.limit_settings(None)
3160-
target, wasm_target = phase_linker_setup(options, linker_args)
3161-
process_libraries(options, linker_args)
3162-
phase_post_link(options, wasm_input, wasm_target, target, {})
3160+
target, wasm_target = phase_linker_setup(linker_args)
3161+
process_libraries(linker_args)
3162+
phase_post_link(wasm_input, wasm_target, target, {})
31633163

31643164

3165-
def run(options, linker_args):
3165+
def run(linker_args):
31663166
# We have now passed the compile phase, allow reading/writing of all settings.
31673167
settings.limit_settings(None)
31683168

@@ -3178,24 +3178,24 @@ def run(options, linker_args):
31783178
if options.output_file and options.output_file.startswith('-'):
31793179
exit_with_error(f'invalid output filename: `{options.output_file}`')
31803180

3181-
target, wasm_target = phase_linker_setup(options, linker_args)
3181+
target, wasm_target = phase_linker_setup(linker_args)
31823182

3183-
linker_args = process_libraries(options, linker_args)
3183+
linker_args = process_libraries(linker_args)
31843184

31853185
# Link object files using wasm-ld or llvm-link (for bitcode linking)
3186-
linker_args = phase_calculate_linker_inputs(options, linker_args)
3186+
linker_args = phase_calculate_linker_inputs(linker_args)
31873187

31883188
# Embed and preload files
31893189
if options.preload_files or options.embed_files:
3190-
linker_args += package_files(options, target)
3190+
linker_args += package_files(target)
31913191

31923192
if options.oformat == OFormat.OBJECT:
31933193
logger.debug(f'link_to_object: {linker_args} -> {target}')
31943194
building.link_to_object(linker_args, target)
31953195
logger.debug('stopping after linking to object file')
31963196
return 0
31973197

3198-
system_libs = phase_calculate_system_libraries(options)
3198+
system_libs = phase_calculate_system_libraries()
31993199
# Only add system libraries that have not already been specified.
32003200
# This avoids issues where the user explicitly includes, for example, `-lGL`.
32013201
# This is not normally a problem except in the case of -sMAIN_MODULE=1 where
@@ -3246,6 +3246,6 @@ def add_js_deps(sym):
32463246

32473247
# Perform post-link steps (unless we are running bare mode)
32483248
if options.oformat != OFormat.BARE:
3249-
phase_post_link(options, wasm_target, wasm_target, target, js_syms, base_metadata)
3249+
phase_post_link(wasm_target, wasm_target, target, js_syms, base_metadata)
32503250

32513251
return 0

0 commit comments

Comments
 (0)