@@ -1255,6 +1255,11 @@ def check(input_file):
12551255 os .environ ['EMCC_WASM_BACKEND_BINARYEN' ] = '1'
12561256
12571257 if shared .Settings .BINARYEN :
1258+ # set file locations, so that JS glue can find what it needs
1259+ shared .Settings .WASM_TEXT_FILE = os .path .basename (wasm_text_target )
1260+ shared .Settings .WASM_BINARY_FILE = os .path .basename (wasm_binary_target )
1261+ shared .Settings .ASMJS_CODE_FILE = os .path .basename (asm_target )
1262+
12581263 shared .Settings .ASM_JS = 2 # when targeting wasm, we use a wasm Memory, but that is not compatible with asm.js opts
12591264 debug_level = max (1 , debug_level ) # keep whitespace readable, for asm.js parser simplicity
12601265 shared .Settings .GLOBAL_BASE = 1024 # leave some room for mapping global vars
@@ -1282,6 +1287,20 @@ def check(input_file):
12821287 if shared .Building .is_wasm_only () and shared .Settings .EVAL_CTORS :
12831288 logging .debug ('disabling EVAL_CTORS, as in wasm-only mode it hurts more than it helps. TODO: a wasm version of it' )
12841289 shared .Settings .EVAL_CTORS = 0
1290+ # enable async compilation if optimizing and not turned off manually
1291+ if opt_level > 0 :
1292+ if 'BINARYEN_ASYNC_COMPILATION=0' not in settings_changes :
1293+ shared .Settings .BINARYEN_ASYNC_COMPILATION = 1
1294+ if shared .Settings .BINARYEN_ASYNC_COMPILATION == 1 :
1295+ if shared .Building .is_wasm_only ():
1296+ # async compilation requires a swappable module - we swap it in when it's ready
1297+ shared .Settings .SWAPPABLE_ASM_MODULE = 1
1298+ else :
1299+ # if not wasm-only, we can't do async compilation as the build can run in other
1300+ # modes than wasm (like asm.js) which may not support an async step
1301+ shared .Settings .BINARYEN_ASYNC_COMPILATION = 0
1302+ if 'BINARYEN_ASYNC_COMPILATION=1' in settings_changes :
1303+ logging .warning ('BINARYEN_ASYNC_COMPILATION requested, but disabled since not in wasm-only mode' )
12851304
12861305 # wasm outputs are only possible with a side wasm
12871306 if target .endswith (WASM_ENDINGS ):
@@ -1643,9 +1662,11 @@ def save_intermediate(name=None, suffix='js'):
16431662 if DEBUG : save_intermediate ('original' )
16441663
16451664 if shared .Settings .WASM_BACKEND :
1646- # we also received wasm at this stage
1647- wasm_temp = final [:- 3 ] + '.wast'
1648- shutil .move (wasm_temp , wasm_text_target )
1665+ # we also received wast and wasm at this stage
1666+ temp_basename = final [:- 3 ]
1667+ wast_temp = temp_basename + '.wast'
1668+ shutil .move (wast_temp , wasm_text_target )
1669+ shutil .move (temp_basename + '.wasm' , wasm_binary_target )
16491670 open (wasm_text_target + '.mappedGlobals' , 'w' ).write ('{}' ) # no need for mapped globals for now, but perhaps some day
16501671
16511672 if shared .Settings .CYBERDWARF :
@@ -1685,20 +1706,6 @@ def save_intermediate(name=None, suffix='js'):
16851706 file_code = execute ([shared .PYTHON , shared .FILE_PACKAGER , unsuffixed (target ) + '.data' ] + file_args , stdout = PIPE )[0 ]
16861707 pre_js = file_code + pre_js
16871708
1688- if shared .Settings .BINARYEN :
1689- # add in the glue integration code as a pre-js, so it is optimized together with everything else
1690- wasm_js_glue = open (os .path .join (shared .Settings .BINARYEN_ROOT , 'src' , 'js' , 'wasm.js-post.js' )).read ()
1691- wasm_js_glue = wasm_js_glue .replace ('{{{ asmjsCodeFile }}}' , '"' + os .path .basename (asm_target ) + '"' )
1692- wasm_js_glue = wasm_js_glue .replace ('{{{ wasmTextFile }}}' , '"' + os .path .basename (wasm_text_target ) + '"' )
1693- wasm_js_glue = wasm_js_glue .replace ('{{{ wasmBinaryFile }}}' , '"' + os .path .basename (wasm_binary_target ) + '"' )
1694- if shared .Settings .BINARYEN_METHOD :
1695- wasm_js_glue = wasm_js_glue .replace ('{{{ wasmJSMethod }}}' , '(Module[\' wasmJSMethod\' ] || "' + shared .Settings .BINARYEN_METHOD + '")' )
1696- else :
1697- wasm_js_glue = wasm_js_glue .replace ('{{{ wasmJSMethod }}}' , 'null' )
1698- wasm_js_glue = wasm_js_glue .replace ('{{{ WASM_BACKEND }}}' , str (shared .Settings .WASM_BACKEND )) # if wasm backend, wasm contains memory segments
1699- wasm_js_glue += '\n integrateWasmJS(Module);\n ' # add a call
1700- post_module += str (wasm_js_glue ) # we can set up the glue once we have the module
1701-
17021709 # Apply pre and postjs files
17031710 if pre_js or post_module or post_js :
17041711 logging .debug ('applying pre/postjses' )
@@ -2060,10 +2067,8 @@ def do_minify(): # minifies the code. this is also when we do certain optimizati
20602067 # Separate out the asm.js code, if asked. Or, if necessary for another option
20612068 if (separate_asm or shared .Settings .BINARYEN ) and not shared .Settings .WASM_BACKEND :
20622069 logging .debug ('separating asm' )
2063- with misc_temp_files .get_file (suffix = '.js' ) as temp_target :
2064- subprocess .check_call ([shared .PYTHON , shared .path_from_root ('tools' , 'separate_asm.py' ), js_target , asm_target , temp_target ])
2065- generated_text_files_with_native_eols += [asm_target ]
2066- shutil .move (temp_target , js_target )
2070+ subprocess .check_call ([shared .PYTHON , shared .path_from_root ('tools' , 'separate_asm.py' ), js_target , asm_target , js_target ])
2071+ generated_text_files_with_native_eols += [asm_target ]
20672072
20682073 # extra only-my-code logic
20692074 if shared .Settings .ONLY_MY_CODE :
@@ -2121,26 +2126,27 @@ def do_minify(): # minifies the code. this is also when we do certain optimizati
21212126 cmd += ['--mem-max=' + str (shared .Settings .BINARYEN_MEM_MAX )]
21222127 if shared .Building .is_wasm_only ():
21232128 cmd += ['--wasm-only' ] # this asm.js is code not intended to run as asm.js, it is only ever going to be wasm, an can contain special fastcomp-wasm support
2129+ if debug_level >= 2 or profiling_funcs :
2130+ cmd += ['-g' ]
2131+ if emit_symbol_map or shared .Settings .CYBERDWARF :
2132+ cmd += ['--symbolmap=' + target + '.symbols' ]
2133+ cmd += ['-o' , wasm_binary_target ]
21242134 logging .debug ('asm2wasm (asm.js => WebAssembly): ' + ' ' .join (cmd ))
21252135 TimeLogger .update ()
2126- subprocess .check_call (cmd , stdout = open ( wasm_text_target , 'w' ) )
2136+ subprocess .check_call (cmd )
21272137 if import_mem_init :
21282138 # remove and forget about the mem init file in later processing; it does not need to be prefetched in the html, etc.
21292139 os .unlink (memfile )
21302140 memory_init_file = False
21312141 log_time ('asm2wasm' )
21322142 if shared .Settings .BINARYEN_PASSES :
2133- shutil .move (wasm_text_target , wasm_text_target + '.pre' )
2134- cmd = [os .path .join (binaryen_bin , 'wasm-opt' ), wasm_text_target + '.pre' , '-o' , wasm_text_target ] + map (lambda p : '--' + p , shared .Settings .BINARYEN_PASSES .split (',' ))
2143+ shutil .move (wasm_binary_target , wasm_binary_target + '.pre' )
2144+ cmd = [os .path .join (binaryen_bin , 'wasm-opt' ), wasm_binary_target + '.pre' , '-o' , wasm_binary_target ] + map (lambda p : '--' + p , shared .Settings .BINARYEN_PASSES .split (',' ))
21352145 logging .debug ('wasm-opt on BINARYEN_PASSES: ' + ' ' .join (cmd ))
21362146 subprocess .check_call (cmd )
2137- if 'native-wasm' in shared .Settings .BINARYEN_METHOD or 'interpret-binary' in shared .Settings .BINARYEN_METHOD :
2138- cmd = [os .path .join (binaryen_bin , 'wasm-as' ), wasm_text_target , '-o' , wasm_binary_target ]
2139- if debug_level >= 2 or profiling_funcs :
2140- cmd += ['-g' ]
2141- if emit_symbol_map or shared .Settings .CYBERDWARF :
2142- cmd += ['--symbolmap=' + target + '.symbols' ]
2143- logging .debug ('wasm-as (text => binary): ' + ' ' .join (cmd ))
2147+ if 'interpret-s-expr' in shared .Settings .BINARYEN_METHOD :
2148+ cmd = [os .path .join (binaryen_bin , 'wasm-dis' ), wasm_binary_target , '-o' , wasm_text_target ]
2149+ logging .debug ('wasm-dis (binary => text): ' + ' ' .join (cmd ))
21442150 subprocess .check_call (cmd )
21452151 if shared .Settings .BINARYEN_SCRIPTS :
21462152 binaryen_scripts = os .path .join (shared .Settings .BINARYEN_ROOT , 'scripts' )
0 commit comments