@@ -2309,9 +2309,9 @@ def opt_level_to_str(opt_level, shrink_level=0):
23092309 return '-O' + str (min (opt_level , 3 ))
23102310
23112311 @staticmethod
2312- def js_optimizer (filename , passes , debug = False , extra_info = None , output_filename = None , just_split = False , just_concat = False ):
2312+ def js_optimizer (filename , passes , debug = False , extra_info = None , output_filename = None , just_split = False , just_concat = False , extra_closure_args = [] ):
23132313 from . import js_optimizer
2314- ret = js_optimizer .run (filename , passes , NODE_JS , debug , extra_info , just_split , just_concat )
2314+ ret = js_optimizer .run (filename , passes , NODE_JS , debug , extra_info , just_split , just_concat , extra_closure_args )
23152315 if output_filename :
23162316 safe_move (ret , output_filename )
23172317 ret = output_filename
@@ -2417,62 +2417,26 @@ def calculate_reachable_functions(infile, initial_list, can_reach=True):
24172417 return {'reachable' : list (advised ), 'total_funcs' : len (can_call )}
24182418
24192419 @staticmethod
2420- def closure_compiler (filename , pretty = True , advanced = True ):
2420+ def closure_compiler (filename , pretty = True , advanced = True , extra_closure_args = [] ):
24212421 with ToolchainProfiler .profile_block ('closure_compiler' ):
24222422 if not check_closure_compiler ():
24232423 logger .error ('Cannot run closure compiler' )
24242424 raise Exception ('closure compiler check failed' )
24252425
24262426 # Closure annotations file contains suppressions and annotations to different symbols
2427- CLOSURE_ANNOTATIONS = ['--js' , path_from_root ('src' , 'closure-annotations.js' )]
2427+ CLOSURE_ANNOTATIONS = [path_from_root ('src' , 'closure-annotations.js' )]
24282428
24292429 if not Settings .ASMFS :
24302430 # If we have filesystem disabled, tell Closure not to bark when there are syscalls emitted that still reference the nonexisting FS object.
24312431 if Settings .FILESYSTEM :
2432- CLOSURE_ANNOTATIONS += ['--js' , path_from_root ('src' , 'closure-defined-fs-annotation.js' )]
2432+ CLOSURE_ANNOTATIONS += [path_from_root ('src' , 'closure-defined-fs-annotation.js' )]
24332433 else :
2434- CLOSURE_ANNOTATIONS += ['--js' , path_from_root ('src' , 'closure-undefined-fs-annotation.js' )]
2434+ CLOSURE_ANNOTATIONS += [path_from_root ('src' , 'closure-undefined-fs-annotation.js' )]
24352435
24362436 # Closure externs file contains known symbols to be extern to the minification, Closure
24372437 # should not minify these symbol names.
2438- CLOSURE_EXTERNS = path_from_root ('src' , 'closure-externs.js' )
2439- NODE_EXTERNS_BASE = path_from_root ('third_party' , 'closure-compiler' , 'node-externs' )
2440- NODE_EXTERNS = os .listdir (NODE_EXTERNS_BASE )
2441- NODE_EXTERNS = [os .path .join (NODE_EXTERNS_BASE , name ) for name in NODE_EXTERNS
2442- if name .endswith ('.js' )]
2443- NODE_EXTERNS = [path_from_root ('src' , 'node-externs.js' )] + NODE_EXTERNS
2444- V8_EXTERNS = [path_from_root ('src' , 'v8-externs.js' )]
2445- SPIDERMONKEY_EXTERNS = [path_from_root ('src' , 'spidermonkey-externs.js' )]
2446- BROWSER_EXTERNS_BASE = path_from_root ('third_party' , 'closure-compiler' , 'browser-externs' )
2447- BROWSER_EXTERNS = os .listdir (BROWSER_EXTERNS_BASE )
2448- BROWSER_EXTERNS = [os .path .join (BROWSER_EXTERNS_BASE , name ) for name in BROWSER_EXTERNS
2449- if name .endswith ('.js' )]
2438+ CLOSURE_EXTERNS = [path_from_root ('src' , 'closure-externs.js' )]
24502439
2451- # Something like this (adjust memory as needed):
2452- # java -Xmx1024m -jar CLOSURE_COMPILER --compilation_level ADVANCED_OPTIMIZATIONS --variable_map_output_file src.cpp.o.js.vars --js src.cpp.o.js --js_output_file src.cpp.o.cc.js
2453- outfile = filename + '.cc.js'
2454- args = [JAVA ,
2455- '-Xmx' + (os .environ .get ('JAVA_HEAP_SIZE' ) or '1024m' ), # if you need a larger Java heap, use this environment variable
2456- '-jar' , CLOSURE_COMPILER ,
2457- '--compilation_level' , 'ADVANCED_OPTIMIZATIONS' if advanced else 'SIMPLE_OPTIMIZATIONS' ,
2458- '--language_in' , 'ECMASCRIPT5' ]
2459- if advanced :
2460- args += CLOSURE_ANNOTATIONS
2461- args += ['--externs' , CLOSURE_EXTERNS ]
2462- args += ['--js_output_file' , outfile ]
2463-
2464- if Settings .target_environment_may_be ('node' ):
2465- for extern in NODE_EXTERNS :
2466- args .append ('--externs' )
2467- args .append (extern )
2468- if Settings .target_environment_may_be ('shell' ):
2469- for extern in V8_EXTERNS + SPIDERMONKEY_EXTERNS :
2470- args .append ('--externs' )
2471- args .append (extern )
2472- if Settings .target_environment_may_be ('web' ) or Settings .target_environment_may_be ('worker' ):
2473- for extern in BROWSER_EXTERNS :
2474- args .append ('--externs' )
2475- args .append (extern )
24762440 # Closure compiler needs to know about all exports that come from the asm.js/wasm module, because to optimize for small code size,
24772441 # the exported symbols are added to global scope via a foreach loop in a way that evades Closure's static analysis. With an explicit
24782442 # externs file for the exports, Closure is able to reason about the exports.
@@ -2483,14 +2447,51 @@ def closure_compiler(filename, pretty=True, advanced=True):
24832447 exports_file .write (module_exports_suppressions .encode ())
24842448 exports_file .close ()
24852449
2486- args .append ('--externs' )
2487- args .append (exports_file .name )
2450+ CLOSURE_EXTERNS += [exports_file .name ]
2451+
2452+ # Node.js specific externs
2453+ if Settings .target_environment_may_be ('node' ):
2454+ NODE_EXTERNS_BASE = path_from_root ('third_party' , 'closure-compiler' , 'node-externs' )
2455+ NODE_EXTERNS = os .listdir (NODE_EXTERNS_BASE )
2456+ NODE_EXTERNS = [os .path .join (NODE_EXTERNS_BASE , name ) for name in NODE_EXTERNS
2457+ if name .endswith ('.js' )]
2458+ CLOSURE_EXTERNS += [path_from_root ('src' , 'node-externs.js' )] + NODE_EXTERNS
2459+
2460+ # V8/SpiderMonkey shell specific externs
2461+ if Settings .target_environment_may_be ('shell' ):
2462+ V8_EXTERNS = [path_from_root ('src' , 'v8-externs.js' )]
2463+ SPIDERMONKEY_EXTERNS = [path_from_root ('src' , 'spidermonkey-externs.js' )]
2464+ CLOSURE_EXTERNS += V8_EXTERNS + SPIDERMONKEY_EXTERNS
2465+
2466+ # Web environment specific externs
2467+ if Settings .target_environment_may_be ('web' ) or Settings .target_environment_may_be ('worker' ):
2468+ BROWSER_EXTERNS_BASE = path_from_root ('third_party' , 'closure-compiler' , 'browser-externs' )
2469+ BROWSER_EXTERNS = os .listdir (BROWSER_EXTERNS_BASE )
2470+ BROWSER_EXTERNS = [os .path .join (BROWSER_EXTERNS_BASE , name ) for name in BROWSER_EXTERNS
2471+ if name .endswith ('.js' )]
2472+ CLOSURE_EXTERNS += BROWSER_EXTERNS
2473+
2474+ # Something like this (adjust memory as needed):
2475+ # java -Xmx1024m -jar CLOSURE_COMPILER --compilation_level ADVANCED_OPTIMIZATIONS --variable_map_output_file src.cpp.o.js.vars --js src.cpp.o.js --js_output_file src.cpp.o.cc.js
2476+ outfile = filename + '.cc.js'
2477+ args = [JAVA ,
2478+ '-Xmx' + (os .environ .get ('JAVA_HEAP_SIZE' ) or '1024m' ), # if you need a larger Java heap, use this environment variable
2479+ '-jar' , CLOSURE_COMPILER ,
2480+ '--compilation_level' , 'ADVANCED_OPTIMIZATIONS' if advanced else 'SIMPLE_OPTIMIZATIONS' ,
2481+ '--language_in' , 'ECMASCRIPT5' ]
2482+ for a in CLOSURE_ANNOTATIONS :
2483+ args += ['--js' , a ]
2484+ for e in CLOSURE_EXTERNS :
2485+ args += ['--externs' , e ]
2486+ args += ['--js_output_file' , outfile ]
2487+
24882488 if Settings .IGNORE_CLOSURE_COMPILER_ERRORS :
24892489 args .append ('--jscomp_off=*' )
24902490 if pretty :
24912491 args += ['--formatting' , 'PRETTY_PRINT' ]
24922492 if os .environ .get ('EMCC_CLOSURE_ARGS' ):
24932493 args += shlex .split (os .environ .get ('EMCC_CLOSURE_ARGS' ))
2494+ args += extra_closure_args
24942495 args += ['--js' , filename ]
24952496 logger .debug ('closure compiler: ' + ' ' .join (args ))
24962497 proc = run_process (args , stderr = PIPE , check = False )
0 commit comments