1import os
2
3
4def parse_config():
5    em_config_file = os.getenv("EM_CONFIG") or os.path.expanduser("~/.emscripten")
6    if not os.path.exists(em_config_file):
7        raise RuntimeError("Emscripten configuration file '%s' does not exist" % em_config_file)
8
9    normalized = {}
10    em_config = {}
11    with open(em_config_file) as f:
12        try:
13            # Emscripten configuration file is a Python file with simple assignments.
14            exec(f.read(), em_config)
15        except StandardError as e:
16            raise RuntimeError("Emscripten configuration file '%s' is invalid:\n%s" % (em_config_file, e))
17    normalized["EMCC_ROOT"] = em_config.get("EMSCRIPTEN_ROOT")
18    normalized["NODE_JS"] = em_config.get("NODE_JS")
19    normalized["CLOSURE_BIN"] = os.path.join(normalized["EMCC_ROOT"], "node_modules", ".bin", "google-closure-compiler")
20    return normalized
21
22
23def run_closure_compiler(target, source, env, for_signature):
24    cfg = parse_config()
25    cmd = [cfg["NODE_JS"], cfg["CLOSURE_BIN"]]
26    cmd.extend(["--compilation_level", "ADVANCED_OPTIMIZATIONS"])
27    for f in env["JSEXTERNS"]:
28        cmd.extend(["--externs", f.get_abspath()])
29    for f in source:
30        cmd.extend(["--js", f.get_abspath()])
31    cmd.extend(["--js_output_file", target[0].get_abspath()])
32    return " ".join(cmd)
33
34
35def create_engine_file(env, target, source, externs):
36    if env["use_closure_compiler"]:
37        return env.BuildJS(target, source, JSEXTERNS=externs)
38    return env.Textfile(target, [env.File(s) for s in source])
39