diff --git a/tools/building.py b/tools/building.py index 38389a21b180c..bd1e6b919ff78 100644 --- a/tools/building.py +++ b/tools/building.py @@ -707,16 +707,8 @@ def isascii(s): return True -@ToolchainProfiler.profile_block('closure_compiler') -def closure_compiler(filename, pretty, advanced=True, extra_closure_args=None): +def get_closure_compiler_and_env(user_args): env = shared.env_with_node_in_path() - user_args = [] - env_args = os.environ.get('EMCC_CLOSURE_ARGS') - if env_args: - user_args += shlex.split(env_args) - if extra_closure_args: - user_args += extra_closure_args - closure_cmd = get_closure_compiler() native_closure_compiler_works = check_closure_compiler(closure_cmd, user_args, env, allowed_to_fail=True) @@ -737,6 +729,20 @@ def add_to_path(dirname): java_home = os.path.dirname(java_bin) env.setdefault('JAVA_HOME', java_home) + return closure_cmd, env + + +@ToolchainProfiler.profile_block('closure_compiler') +def closure_compiler(filename, pretty, advanced=True, extra_closure_args=None): + user_args = [] + env_args = os.environ.get('EMCC_CLOSURE_ARGS') + if env_args: + user_args += shlex.split(env_args) + if extra_closure_args: + user_args += extra_closure_args + + closure_cmd, env = get_closure_compiler_and_env(user_args) + # Closure externs file contains known symbols to be extern to the minification, Closure # should not minify these symbol names. CLOSURE_EXTERNS = [path_from_root('src/closure-externs/closure-externs.js')] @@ -792,10 +798,23 @@ def add_to_path(dirname): # Tell closure never to inject the 'use strict' directive. args += ['--emit_use_strict=false'] + if settings.IGNORE_CLOSURE_COMPILER_ERRORS: + args.append('--jscomp_off=*') + # Specify input file relative to the temp directory to avoid specifying non-7-bit-ASCII path names. + for e in CLOSURE_EXTERNS: + args += ['--externs', e] + args += user_args + + cmd = closure_cmd + args + return run_closure_cmd(cmd, filename, env, pretty=pretty) + + +def run_closure_cmd(cmd, filename, env, pretty): + cmd += ['--js', filename] + # Closure compiler is unable to deal with path names that are not 7-bit ASCII: # https://github.com/google/closure-compiler/issues/3784 tempfiles = configuration.get_temp_files() - outfile = tempfiles.get('.cc.js').name # Safe 7-bit filename def move_to_safe_7bit_ascii_filename(filename): if isascii(filename): @@ -804,23 +823,17 @@ def move_to_safe_7bit_ascii_filename(filename): shutil.copyfile(filename, safe_filename) return os.path.relpath(safe_filename, tempfiles.tmpdir) - for e in CLOSURE_EXTERNS: - args += ['--externs', move_to_safe_7bit_ascii_filename(e)] + for i in range(len(cmd)): + if cmd[i] == '--externs' or cmd[i] == '--js': + cmd[i + 1] = move_to_safe_7bit_ascii_filename(cmd[i + 1]) - for i in range(len(user_args)): - if user_args[i] == '--externs': - user_args[i + 1] = move_to_safe_7bit_ascii_filename(user_args[i + 1]) + outfile = tempfiles.get('.cc.js').name # Safe 7-bit filename # Specify output file relative to the temp directory to avoid specifying non-7-bit-ASCII path names. - args += ['--js_output_file', os.path.relpath(outfile, tempfiles.tmpdir)] - - if settings.IGNORE_CLOSURE_COMPILER_ERRORS: - args.append('--jscomp_off=*') + cmd += ['--js_output_file', os.path.relpath(outfile, tempfiles.tmpdir)] if pretty: - args += ['--formatting', 'PRETTY_PRINT'] - # Specify input file relative to the temp directory to avoid specifying non-7-bit-ASCII path names. - args += ['--js', move_to_safe_7bit_ascii_filename(filename)] - cmd = closure_cmd + args + user_args + cmd += ['--formatting', 'PRETTY_PRINT'] + logger.debug(f'closure compiler: {shared.shlex_join(cmd)}') # Closure compiler does not work if any of the input files contain characters outside the