From 5fc15eb3394e73fb19bc4dd5d9043a80296d38f7 Mon Sep 17 00:00:00 2001 From: Ryan Lester Date: Wed, 14 Jun 2017 22:53:04 -0400 Subject: [PATCH] binaryen get_subresource_location timing fix (#5296) --- emcc.py | 25 +++++++++++++++++++++---- tools/shared.py | 10 +++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/emcc.py b/emcc.py index 9b42773adf22e..853ceb5ae29ad 100755 --- a/emcc.py +++ b/emcc.py @@ -1115,10 +1115,10 @@ def check(input_file): os.environ['EMCC_WASM_BACKEND_BINARYEN'] = '1' if shared.Settings.BINARYEN: - # set file locations, so that JS glue can find what it needs - shared.Settings.WASM_TEXT_FILE = shared.JS.get_subresource_location(wasm_text_target) - shared.Settings.WASM_BINARY_FILE = shared.JS.get_subresource_location(wasm_binary_target) - shared.Settings.ASMJS_CODE_FILE = shared.JS.get_subresource_location(asm_target) + # placeholder strings for JS glue, to be replaced with subresource locations in do_binaryen + shared.Settings.WASM_TEXT_FILE = shared.FilenameReplacementStrings.WASM_TEXT_FILE + shared.Settings.WASM_BINARY_FILE = shared.FilenameReplacementStrings.WASM_BINARY_FILE + shared.Settings.ASMJS_CODE_FILE = shared.FilenameReplacementStrings.ASMJS_CODE_FILE shared.Settings.ASM_JS = 2 # when targeting wasm, we use a wasm Memory, but that is not compatible with asm.js opts shared.Settings.GLOBAL_BASE = 1024 # leave some room for mapping global vars @@ -2313,6 +2313,23 @@ def do_binaryen(final, target, asm_target, options, memfile, wasm_binary_target, passes.append('minifyWhitespace') final = shared.Building.js_optimizer_no_asmjs(final, passes) if DEBUG: save_intermediate('postclean', 'js') + # replace placeholder strings with correct subresource locations + f = open(final, 'r') + js = f.read() + f.close() + f = open(final, 'w') + for tup in [ + (wasm_text_target, shared.FilenameReplacementStrings.WASM_TEXT_FILE), + (wasm_binary_target, shared.FilenameReplacementStrings.WASM_BINARY_FILE), + (asm_target, shared.FilenameReplacementStrings.ASMJS_CODE_FILE) + ]: + if not os.path.isfile(tup[0]): + continue + js = js.replace(tup[1], shared.JS.get_subresource_location(tup[0])) + if shared.Settings.SINGLE_FILE: + os.remove(tup[0]) + f.write(js) + f.close() return final diff --git a/tools/shared.py b/tools/shared.py index 54fc76749df07..35c07e0bfa4af 100644 --- a/tools/shared.py +++ b/tools/shared.py @@ -2265,6 +2265,12 @@ def reconfigure_cache(): global Cache Cache = cache.Cache(debug=DEBUG_CACHE) +# Temporary workaround for SINGLE_FILE, pending refactoring +class FilenameReplacementStrings: + WASM_TEXT_FILE = 'FILENAME_REPLACEMENT_STRINGS_WASM_TEXT_FILE' + WASM_BINARY_FILE = 'FILENAME_REPLACEMENT_STRINGS_WASM_BINARY_FILE' + ASMJS_CODE_FILE = 'FILENAME_REPLACEMENT_STRINGS_ASMJS_CODE_FILE' + class JS: memory_initializer_pattern = '/\* memory initializer \*/ allocate\(\[([\d, ]*)\], "i8", ALLOC_NONE, ([\d+Runtime\.GLOBAL_BASEHgb]+)\);' no_memory_initializer_pattern = '/\* no memory initializer \*/' @@ -2279,7 +2285,9 @@ def to_nice_ident(ident): # limited version of the JS function toNiceIdent # Returns the subresource location for run-time access @staticmethod - def get_subresource_location(path, data_uri=Settings.SINGLE_FILE): + def get_subresource_location(path, data_uri=None): + if data_uri is None: + data_uri = Settings.SINGLE_FILE if data_uri: Settings.INCLUDE_THIRD_PARTY_SODIUMUTIL = 1 f = open(path, 'rb')