emscripten_helpers.py 1.5 KB

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