| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 | 2 |
| 3 import argparse | 3 import argparse |
| 4 import os | 4 import os |
| 5 import re | |
| 6 import subprocess | 5 import subprocess |
| 6 import sys | |
| 7 | 7 |
| 8 BASE_DIR = os.path.dirname(__file__) | 8 BASE_DIR = os.path.dirname(__file__) |
| 9 SOURCE_DIR = os.path.join(BASE_DIR, 'compiled') | 9 SOURCE_DIR = os.path.join(BASE_DIR, 'compiled') |
| 10 SOURCE_FILES = [ | 10 SOURCE_FILES = [ |
| 11 os.path.join(path, f) | 11 os.path.join(path, f) |
| 12 for (path, dirs, files) in os.walk(SOURCE_DIR) | 12 for (path, dirs, files) in os.walk(SOURCE_DIR) |
| 13 for f in files | 13 for f in files |
| 14 if f.endswith('.cpp') | 14 if f.endswith('.cpp') |
| 15 ] | 15 ] |
| 16 BINDINGS_FILE = os.path.join(SOURCE_DIR, 'bindings.cpp') | 16 BINDINGS_FILE = os.path.join(SOURCE_DIR, 'bindings.cpp') |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 29 'NO_FILESYSTEM': 1, | 29 'NO_FILESYSTEM': 1, |
| 30 'INVOKE_RUN': 0, | 30 'INVOKE_RUN': 0, |
| 31 'TEXTDECODER': 0, | 31 'TEXTDECODER': 0, |
| 32 'EXPORTED_RUNTIME_METHODS': ['cwrap', 'ccall', 'stringToAscii'], | 32 'EXPORTED_RUNTIME_METHODS': ['cwrap', 'ccall', 'stringToAscii'], |
| 33 } | 33 } |
| 34 DEFINES = [] | 34 DEFINES = [] |
| 35 ADDITIONAL_PARAMS = ['-O3', '-m32', '-std=gnu++14', '--memory-init-file', '0', | 35 ADDITIONAL_PARAMS = ['-O3', '-m32', '-std=gnu++14', '--memory-init-file', '0', |
| 36 '--emit-symbol-map'] | 36 '--emit-symbol-map'] |
| 37 | 37 |
| 38 | 38 |
| 39 def getenv(emscripten_path): | 39 def getenv(emscripten_config): |
| 40 path = [] | 40 scope = {} |
| 41 env = {} | 41 execfile(emscripten_config, scope, scope) |
| 42 output = subprocess.check_output([ | 42 env = os.environ.copy() |
| 43 '/bin/bash', '-c', os.path.join(emscripten_path, 'emsdk_env.sh') | 43 env.update({ |
| 44 ]) | 44 'EM_CONFIG': emscripten_config, |
|
Wladimir Palant
2017/03/23 14:10:57
It seems that Emscripten itself only needs this en
| |
| 45 for line in output.splitlines(): | 45 'EMSCRIPTEN': scope['EMSCRIPTEN_ROOT'], |
| 46 match = re.search(r'^\s*PATH\s*\+=\s*(.*)', line) | 46 'PYTHON': scope.get('PYTHON', sys.executable), |
| 47 if match: | 47 'NODE_JS': scope.get('NODE_JS', 'node'), |
|
Wladimir Palant
2017/03/23 14:10:57
This will typically be misconfigured on Windows, s
| |
| 48 path.append(match.group(1)) | 48 }) |
| 49 match = re.search(r'^\s*(\w+)\s*=\s*(.*)', line) | |
| 50 if match: | |
| 51 env[match.group(1)] = match.group(2) | |
| 52 env['PATH'] = os.pathsep.join([os.environ['PATH']] + path) | |
| 53 return env | 49 return env |
| 54 | 50 |
| 55 | 51 |
| 56 def generate_bindings(env): | 52 def generate_bindings(env): |
| 57 params = [ | 53 params = [ |
| 58 os.path.join(env['EMSCRIPTEN'], 'emcc'), BINDINGS_FILE, | 54 env['PYTHON'], os.path.join(env['EMSCRIPTEN'], 'emcc'), BINDINGS_FILE, |
| 59 '-o', BINDINGS_GENERATOR, '-std=gnu++14', '-DPRINT_BINDINGS', | 55 '-o', BINDINGS_GENERATOR, '-std=gnu++14', '-DPRINT_BINDINGS', |
| 60 '-s', 'WARN_ON_UNDEFINED_SYMBOLS=0', | 56 '-s', 'WARN_ON_UNDEFINED_SYMBOLS=0', |
| 61 ] | 57 ] |
| 62 subprocess.check_call(params, env=env) | 58 subprocess.check_call(params, env=env) |
| 63 | 59 |
| 64 node = subprocess.check_output('which node', env=env, shell=True).strip() | |
| 65 with open(BINDINGS_OUTPUT, 'w') as file: | 60 with open(BINDINGS_OUTPUT, 'w') as file: |
| 66 subprocess.check_call([node, BINDINGS_GENERATOR], env=env, stdout=file) | 61 subprocess.check_call([env['NODE_JS'], BINDINGS_GENERATOR], stdout=file) |
| 67 | 62 |
| 68 | 63 |
| 69 def run_compiler(env, debug=False, tracing=False): | 64 def run_compiler(env, debug=False, tracing=False): |
| 70 params = [ | 65 params = [ |
| 71 os.path.join(env['EMSCRIPTEN'], 'emcc'), | 66 env['PYTHON'], os.path.join(env['EMSCRIPTEN'], 'emcc'), |
| 72 '-o', COMPILER_OUTPUT, | 67 '-o', COMPILER_OUTPUT, |
| 73 '--post-js', BINDINGS_OUTPUT, | 68 '--post-js', BINDINGS_OUTPUT, |
| 74 ] | 69 ] |
| 75 params.extend(SOURCE_FILES) | 70 params.extend(SOURCE_FILES) |
| 76 params.extend('-D' + flag for flag in DEFINES) | 71 params.extend('-D' + flag for flag in DEFINES) |
| 77 for key, value in GENERATION_PARAMS.iteritems(): | 72 for key, value in GENERATION_PARAMS.iteritems(): |
| 78 params.extend(['-s', '{}={}'.format(key, str(value))]) | 73 params.extend(['-s', '{}={}'.format(key, str(value))]) |
| 79 if debug: | 74 if debug: |
| 80 params.append('-g1') | 75 params.append('-g1') |
| 81 if tracing: | 76 if tracing: |
| 82 params.append('--tracing') | 77 params.append('--tracing') |
| 83 params.extend(ADDITIONAL_PARAMS) | 78 params.extend(ADDITIONAL_PARAMS) |
| 84 subprocess.check_call(params, env=env) | 79 subprocess.check_call(params, env=env) |
| 85 | 80 |
| 86 | 81 |
| 87 if __name__ == '__main__': | 82 if __name__ == '__main__': |
| 88 parser = argparse.ArgumentParser( | 83 parser = argparse.ArgumentParser( |
| 89 description='Compile Emscripten-based C++ code to JavaScript' | 84 description='Compile Emscripten-based C++ code to JavaScript' |
| 90 ) | 85 ) |
| 91 parser.add_argument( | 86 parser.add_argument( |
| 92 '--emscripten', | 87 '--emscripten-config', |
| 93 metavar='DIR', | 88 metavar='DIR', |
| 94 default=os.path.join(BASE_DIR, '..', 'emscripten'), | 89 default=os.path.expanduser('~/.emscripten'), |
| 95 help='Emscripten installation directory' | 90 help='Emscripten installation directory' |
| 96 ) | 91 ) |
| 97 parser.add_argument( | 92 parser.add_argument( |
| 98 '-d', '--debug', | 93 '-d', '--debug', |
| 99 action='store_true', | 94 action='store_true', |
| 100 help='Disable code minification' | 95 help='Disable code minification' |
| 101 ) | 96 ) |
| 102 parser.add_argument( | 97 parser.add_argument( |
| 103 '-t', '--tracing', | 98 '-t', '--tracing', |
| 104 action='store_true', | 99 action='store_true', |
| 105 help='Enable memory tracing' | 100 help='Enable memory tracing' |
| 106 ) | 101 ) |
| 107 args = parser.parse_args() | 102 args = parser.parse_args() |
| 108 | 103 |
| 109 env = getenv(args.emscripten) | 104 env = getenv(args.emscripten_config) |
| 110 generate_bindings(env) | 105 generate_bindings(env) |
| 111 run_compiler(env, debug=args.debug, tracing=args.tracing) | 106 run_compiler(env, debug=args.debug, tracing=args.tracing) |
| OLD | NEW |