Skip to content

Commit 500d826

Browse files
authored
Remove unneeded Configuration class from shared.py. NFC (#16543)
I don't think this class was ever used to do anything useful. This change bascically just removes the extra class structure, but I have followup to simplify even more. This class was introduced in 8dc0e2b but I'm not sure why since its doesn't actually remove any global variables, only makes their initialization harder to read. I think perhaps that idea was to have less code at the top level, but functions (rather than classes) work fine for that purpose.
1 parent 1f58cab commit 500d826

File tree

8 files changed

+62
-70
lines changed

8 files changed

+62
-70
lines changed

embuilder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373

7474
PORTS = sorted(list(ports.ports_by_name.keys()) + list(PORT_VARIANTS.keys()))
7575

76-
temp_files = shared.configuration.get_temp_files()
76+
temp_files = shared.get_temp_files()
7777
logger = logging.getLogger('embuilder')
7878
legacy_prefixes = {
7979
'libgl': 'libGL',

emcc.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ def run(args):
10461046
# Strip args[0] (program name)
10471047
args = args[1:]
10481048

1049-
misc_temp_files = shared.configuration.get_temp_files()
1049+
misc_temp_files = shared.get_temp_files()
10501050

10511051
# Handle some global flags
10521052

@@ -3392,7 +3392,7 @@ def preprocess_wasm2js_script():
33923392
symbols_file=symbols_file,
33933393
symbols_file_js=symbols_file_js)
33943394

3395-
shared.configuration.get_temp_files().note(wasm2js)
3395+
shared.get_temp_files().note(wasm2js)
33963396

33973397
if settings.WASM == 2:
33983398
safe_copy(wasm2js, wasm2js_template)
@@ -3514,7 +3514,7 @@ def modularize():
35143514
exports["%(EXPORT_NAME)s"] = %(EXPORT_NAME)s;
35153515
''' % {'EXPORT_NAME': settings.EXPORT_NAME})
35163516

3517-
shared.configuration.get_temp_files().note(final_js)
3517+
shared.get_temp_files().note(final_js)
35183518
save_intermediate('modularized')
35193519

35203520

@@ -3534,7 +3534,7 @@ def module_export_name_substitution():
35343534
if settings.MINIMAL_RUNTIME and not settings.MODULARIZE and (shared.target_environment_may_be('node') or shared.target_environment_may_be('shell')):
35353535
src = 'if(typeof Module==="undefined"){var Module={};}\n' + src
35363536
write_file(final_js, src)
3537-
shared.configuration.get_temp_files().note(final_js)
3537+
shared.get_temp_files().note(final_js)
35383538
save_intermediate('module_export_name_substitution')
35393539

35403540

emscripten.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def compile_settings():
171171
settings['LEGACY_SETTINGS'] = [l[0] for l in settings['LEGACY_SETTINGS']]
172172

173173
# Save settings to a file to work around v8 issue 1579
174-
with shared.configuration.get_temp_files().get_file('.json') as settings_file:
174+
with shared.get_temp_files().get_file('.json') as settings_file:
175175
with open(settings_file, 'w') as s:
176176
json.dump(settings.dict(), s, sort_keys=True, indent=2)
177177

tools/building.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from .shared import LLVM_NM, EMCC, EMAR, EMXX, EMRANLIB, WASM_LD, LLVM_AR
2727
from .shared import LLVM_LINK, LLVM_OBJCOPY
2828
from .shared import try_delete, run_process, check_call, exit_with_error
29-
from .shared import configuration, path_from_root
29+
from .shared import path_from_root
3030
from .shared import asmjs_mangle, DEBUG
3131
from .shared import TEMP_DIR
3232
from .shared import CANONICAL_TEMP_DIR, LLVM_DWARFDUMP, demangle_c_symbol_name
@@ -270,7 +270,7 @@ def link_llvm(linker_inputs, target):
270270
def lld_flags_for_executable(external_symbols):
271271
cmd = []
272272
if external_symbols:
273-
undefs = configuration.get_temp_files().get('.undefined').name
273+
undefs = shared.get_temp_files().get('.undefined').name
274274
utils.write_file(undefs, '\n'.join(external_symbols))
275275
cmd.append('--allow-undefined-file=%s' % undefs)
276276
else:
@@ -625,7 +625,7 @@ def acorn_optimizer(filename, passes, extra_info=None, return_output=False):
625625
optimizer = path_from_root('tools/acorn-optimizer.js')
626626
original_filename = filename
627627
if extra_info is not None:
628-
temp_files = configuration.get_temp_files()
628+
temp_files = shared.get_temp_files()
629629
temp = temp_files.get('.js').name
630630
shutil.copyfile(filename, temp)
631631
with open(temp, 'a') as f:
@@ -642,7 +642,7 @@ def acorn_optimizer(filename, passes, extra_info=None, return_output=False):
642642
cmd += ['verbose']
643643
if not return_output:
644644
next = original_filename + '.jso.js'
645-
configuration.get_temp_files().note(next)
645+
shared.get_temp_files().note(next)
646646
check_call(cmd, stdout=open(next, 'w'))
647647
save_intermediate(next, '%s.js' % passes[0])
648648
return next
@@ -802,7 +802,7 @@ def closure_compiler(filename, pretty, advanced=True, extra_closure_args=None):
802802
if settings.WASM_FUNCTION_EXPORTS and not settings.DECLARE_ASM_MODULE_EXPORTS:
803803
# Generate an exports file that records all the exported symbols from the wasm module.
804804
module_exports_suppressions = '\n'.join(['/**\n * @suppress {duplicate, undefinedVars}\n */\nvar %s;\n' % asmjs_mangle(i) for i in settings.WASM_FUNCTION_EXPORTS])
805-
exports_file = configuration.get_temp_files().get('_module_exports.js')
805+
exports_file = shared.get_temp_files().get('_module_exports.js')
806806
exports_file.write(module_exports_suppressions.encode())
807807
exports_file.close()
808808

@@ -866,7 +866,7 @@ def run_closure_cmd(cmd, filename, env, pretty):
866866

867867
# Closure compiler is unable to deal with path names that are not 7-bit ASCII:
868868
# https://github.com/google/closure-compiler/issues/3784
869-
tempfiles = configuration.get_temp_files()
869+
tempfiles = shared.get_temp_files()
870870

871871
def move_to_safe_7bit_ascii_filename(filename):
872872
if isascii(filename):
@@ -987,7 +987,7 @@ def minify_wasm_js(js_file, wasm_file, expensive_optimizations, minify_whitespac
987987
# run binaryen's wasm-metadce to dce both js and wasm
988988
def metadce(js_file, wasm_file, minify_whitespace, debug_info):
989989
logger.debug('running meta-DCE')
990-
temp_files = configuration.get_temp_files()
990+
temp_files = shared.get_temp_files()
991991
# first, get the JS part of the graph
992992
if settings.MAIN_MODULE:
993993
# For the main module we include all exports as possible roots, not just function exports.
@@ -1176,7 +1176,7 @@ def wasm2js(js_file, wasm_file, opt_level, minify_whitespace, use_closure_compil
11761176
wasm2js_js = wasm2js_js.replace('\n function $', '\nfunction $')
11771177
wasm2js_js = wasm2js_js.replace('\n }', '\n}')
11781178
wasm2js_js += '\n// EMSCRIPTEN_GENERATED_FUNCTIONS\n'
1179-
temp = configuration.get_temp_files().get('.js').name
1179+
temp = shared.get_temp_files().get('.js').name
11801180
utils.write_file(temp, wasm2js_js)
11811181
temp = js_optimizer(temp, passes)
11821182
wasm2js_js = utils.read_file(temp)
@@ -1186,7 +1186,7 @@ def wasm2js(js_file, wasm_file, opt_level, minify_whitespace, use_closure_compil
11861186
# TODO: in the non-closure case, we could run a lightweight general-
11871187
# purpose JS minifier here.
11881188
if use_closure_compiler == 2:
1189-
temp = configuration.get_temp_files().get('.js').name
1189+
temp = shared.get_temp_files().get('.js').name
11901190
with open(temp, 'a') as f:
11911191
f.write(wasm2js_js)
11921192
temp = closure_compiler(temp, pretty=not minify_whitespace, advanced=False)

tools/js_optimizer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
from tools.utils import path_from_root
2020
from tools import building, config, shared, utils
2121

22-
configuration = shared.configuration
23-
temp_files = configuration.get_temp_files()
22+
temp_files = shared.get_temp_files()
2423

2524

2625
ACORN_OPTIMIZER = path_from_root('tools/acorn-optimizer.js')

tools/minimal_runtime_shell.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def generate_minimal_runtime_html(target, options, js_target, target_basename):
191191
else:
192192
shell = shell.replace('{{{ DOWNLOAD_JS_AND_WASM_FILES }}}', generate_minimal_runtime_load_statement(target_basename))
193193

194-
temp_files = shared.configuration.get_temp_files()
194+
temp_files = shared.get_temp_files()
195195
with temp_files.get_file(suffix='.js') as shell_temp:
196196
utils.write_file(shell_temp, shell)
197197
shell = shared.read_and_preprocess(shell_temp)

tools/response_file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def escape(arg):
6060
# Register the created .rsp file to be automatically cleaned up once this
6161
# process finishes, so that caller does not have to remember to do it.
6262
from . import shared
63-
shared.configuration.get_temp_files().note(response_filename)
63+
shared.get_temp_files().note(response_filename)
6464

6565
return response_filename
6666

tools/shared.py

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def get_num_cores():
114114

115115

116116
def mp_run_process(command_tuple):
117-
temp_files = configuration.get_temp_files()
117+
temp_files = get_temp_files()
118118
cmd, env, route_stdout_to_temp_files_suffix, pipe_stdout, check, cwd = command_tuple
119119
std_out = temp_files.get(route_stdout_to_temp_files_suffix) if route_stdout_to_temp_files_suffix else (subprocess.PIPE if pipe_stdout else None)
120120
ret = std_out.name if route_stdout_to_temp_files_suffix else None
@@ -163,7 +163,7 @@ def run_multiple_processes(commands, env=os.environ.copy(), route_stdout_to_temp
163163
with ToolchainProfiler.profile_block('run_multiple_processes'):
164164
processes = []
165165
num_parallel_processes = get_num_cores()
166-
temp_files = configuration.get_temp_files()
166+
temp_files = get_temp_files()
167167
i = 0
168168
num_completed = 0
169169

@@ -466,9 +466,9 @@ def replace_or_append_suffix(filename, new_suffix):
466466
# temp directory (TEMP_DIR/emscripten_temp).
467467
def get_emscripten_temp_dir():
468468
"""Returns a path to EMSCRIPTEN_TEMP_DIR, creating one if it didn't exist."""
469-
global configuration, EMSCRIPTEN_TEMP_DIR
469+
global EMSCRIPTEN_TEMP_DIR
470470
if not EMSCRIPTEN_TEMP_DIR:
471-
EMSCRIPTEN_TEMP_DIR = tempfile.mkdtemp(prefix='emscripten_temp_', dir=configuration.TEMP_DIR)
471+
EMSCRIPTEN_TEMP_DIR = tempfile.mkdtemp(prefix='emscripten_temp_', dir=TEMP_DIR)
472472

473473
if not DEBUG_SAVE:
474474
def prepare_to_clean_temp(d):
@@ -485,54 +485,47 @@ def get_canonical_temp_dir(temp_dir):
485485
return os.path.join(temp_dir, 'emscripten_temp')
486486

487487

488-
class Configuration:
489-
def __init__(self):
490-
self.EMSCRIPTEN_TEMP_DIR = None
491-
492-
self.TEMP_DIR = os.environ.get("EMCC_TEMP_DIR", tempfile.gettempdir())
493-
if not os.path.isdir(self.TEMP_DIR):
494-
exit_with_error(f'The temporary directory `{self.TEMP_DIR}` does not exist! Please make sure that the path is correct.')
495-
496-
self.CANONICAL_TEMP_DIR = get_canonical_temp_dir(self.TEMP_DIR)
497-
498-
if DEBUG:
499-
self.EMSCRIPTEN_TEMP_DIR = self.CANONICAL_TEMP_DIR
500-
try:
501-
safe_ensure_dirs(self.EMSCRIPTEN_TEMP_DIR)
502-
except Exception as e:
503-
exit_with_error(str(e) + f'Could not create canonical temp dir. Check definition of TEMP_DIR in {config.EM_CONFIG}')
504-
505-
# Since the canonical temp directory is, by definition, the same
506-
# between all processes that run in DEBUG mode we need to use a multi
507-
# process lock to prevent more than one process from writing to it.
508-
# This is because emcc assumes that it can use non-unique names inside
509-
# the temp directory.
510-
# Sadly we need to allow child processes to access this directory
511-
# though, since emcc can recursively call itself when building
512-
# libraries and ports.
513-
if 'EM_HAVE_TEMP_DIR_LOCK' not in os.environ:
514-
filelock_name = os.path.join(self.EMSCRIPTEN_TEMP_DIR, 'emscripten.lock')
515-
lock = filelock.FileLock(filelock_name)
516-
os.environ['EM_HAVE_TEMP_DIR_LOCK'] = '1'
517-
lock.acquire()
518-
atexit.register(lock.release)
519-
520-
def get_temp_files(self):
521-
if DEBUG_SAVE:
522-
# In debug mode store all temp files in the emscripten-specific temp dir
523-
# and don't worry about cleaning them up.
524-
return tempfiles.TempFiles(get_emscripten_temp_dir(), save_debug_files=True)
525-
else:
526-
# Otherwise use the system tempdir and try to clean up after ourselves.
527-
return tempfiles.TempFiles(self.TEMP_DIR, save_debug_files=False)
488+
def setup_temp_dirs():
489+
global EMSCRIPTEN_TEMP_DIR, CANONICAL_TEMP_DIR, TEMP_DIR
490+
EMSCRIPTEN_TEMP_DIR = None
491+
492+
TEMP_DIR = os.environ.get("EMCC_TEMP_DIR", tempfile.gettempdir())
493+
if not os.path.isdir(TEMP_DIR):
494+
exit_with_error(f'The temporary directory `{TEMP_DIR}` does not exist! Please make sure that the path is correct.')
528495

496+
CANONICAL_TEMP_DIR = get_canonical_temp_dir(TEMP_DIR)
529497

530-
def apply_configuration():
531-
global configuration, EMSCRIPTEN_TEMP_DIR, CANONICAL_TEMP_DIR, TEMP_DIR
532-
configuration = Configuration()
533-
EMSCRIPTEN_TEMP_DIR = configuration.EMSCRIPTEN_TEMP_DIR
534-
CANONICAL_TEMP_DIR = configuration.CANONICAL_TEMP_DIR
535-
TEMP_DIR = configuration.TEMP_DIR
498+
if DEBUG:
499+
EMSCRIPTEN_TEMP_DIR = CANONICAL_TEMP_DIR
500+
try:
501+
safe_ensure_dirs(EMSCRIPTEN_TEMP_DIR)
502+
except Exception as e:
503+
exit_with_error(str(e) + f'Could not create canonical temp dir. Check definition of TEMP_DIR in {config.EM_CONFIG}')
504+
505+
# Since the canonical temp directory is, by definition, the same
506+
# between all processes that run in DEBUG mode we need to use a multi
507+
# process lock to prevent more than one process from writing to it.
508+
# This is because emcc assumes that it can use non-unique names inside
509+
# the temp directory.
510+
# Sadly we need to allow child processes to access this directory
511+
# though, since emcc can recursively call itself when building
512+
# libraries and ports.
513+
if 'EM_HAVE_TEMP_DIR_LOCK' not in os.environ:
514+
filelock_name = os.path.join(EMSCRIPTEN_TEMP_DIR, 'emscripten.lock')
515+
lock = filelock.FileLock(filelock_name)
516+
os.environ['EM_HAVE_TEMP_DIR_LOCK'] = '1'
517+
lock.acquire()
518+
atexit.register(lock.release)
519+
520+
521+
def get_temp_files():
522+
if DEBUG_SAVE:
523+
# In debug mode store all temp files in the emscripten-specific temp dir
524+
# and don't worry about cleaning them up.
525+
return tempfiles.TempFiles(get_emscripten_temp_dir(), save_debug_files=True)
526+
else:
527+
# Otherwise use the system tempdir and try to clean up after ourselves.
528+
return tempfiles.TempFiles(TEMP_DIR, save_debug_files=False)
536529

537530

538531
def target_environment_may_be(environment):
@@ -716,7 +709,7 @@ def get_llvm_target():
716709
EM_NM = bat_suffix(path_from_root('emnm'))
717710
FILE_PACKAGER = bat_suffix(path_from_root('tools/file_packager'))
718711

719-
apply_configuration()
712+
setup_temp_dirs()
720713

721714
Cache = cache.Cache(config.CACHE)
722715

0 commit comments

Comments
 (0)