| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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 subprocess | 5 import subprocess |
| 6 import sys | 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 = [ |
| (...skipping 18 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_config): | 39 def getenv(emscripten_config): |
|
Vasily Kuznetsov
2017/03/23 19:26:53
Instead of running `emsdk_env.sh` located at `emsc
| |
| 40 scope = {} | 40 scope = {} |
| 41 execfile(emscripten_config, scope, scope) | 41 execfile(emscripten_config, scope, scope) |
| 42 env = os.environ.copy() | 42 env = os.environ.copy() |
| 43 env.update({ | 43 env.update({ |
| 44 'EM_CONFIG': emscripten_config, | 44 'EM_CONFIG': emscripten_config, |
|
Wladimir Palant
2017/03/23 14:10:57
It seems that Emscripten itself only needs this en
| |
| 45 'EMSCRIPTEN': scope['EMSCRIPTEN_ROOT'], | 45 'EMSCRIPTEN': scope['EMSCRIPTEN_ROOT'], |
| 46 'PYTHON': scope.get('PYTHON', sys.executable), | 46 'PYTHON': scope.get('PYTHON', sys.executable), |
| 47 'NODE_JS': scope.get('NODE_JS', 'node'), | 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 }) | 48 }) |
| 49 return env | 49 return env |
| 50 | 50 |
| 51 | 51 |
| 52 def generate_bindings(env): | 52 def generate_bindings(env): |
| 53 params = [ | 53 params = [ |
| 54 env['PYTHON'], os.path.join(env['EMSCRIPTEN'], 'emcc'), BINDINGS_FILE, | 54 env['PYTHON'], os.path.join(env['EMSCRIPTEN'], 'emcc'), BINDINGS_FILE, |
|
Vasily Kuznetsov
2017/03/23 19:26:53
We use python interpreter configured in emscripten
Wladimir Palant
2017/03/24 06:20:41
There is emcc.bat on Windows but it relies on Pyth
| |
| 55 '-o', BINDINGS_GENERATOR, '-std=gnu++14', '-DPRINT_BINDINGS', | 55 '-o', BINDINGS_GENERATOR, '-std=gnu++14', '-DPRINT_BINDINGS', |
| 56 '-s', 'WARN_ON_UNDEFINED_SYMBOLS=0', | 56 '-s', 'WARN_ON_UNDEFINED_SYMBOLS=0', |
| 57 ] | 57 ] |
| 58 subprocess.check_call(params, env=env) | 58 subprocess.check_call(params, env=env) |
| 59 | 59 |
| 60 with open(BINDINGS_OUTPUT, 'w') as file: | 60 with open(BINDINGS_OUTPUT, 'w') as file: |
| 61 subprocess.check_call([env['NODE_JS'], BINDINGS_GENERATOR], stdout=file) | 61 subprocess.check_call([env['NODE_JS'], BINDINGS_GENERATOR], |
|
Vasily Kuznetsov
2017/03/23 19:26:53
We use the path to node from emscripten config (de
Wladimir Palant
2017/03/24 06:20:41
The drawback here is that Emscripten comes with a
| |
| 62 stdout=file) | |
| 62 | 63 |
| 63 | 64 |
| 64 def run_compiler(env, debug=False, tracing=False): | 65 def run_compiler(env, debug=False, tracing=False): |
| 65 params = [ | 66 params = [ |
| 66 env['PYTHON'], os.path.join(env['EMSCRIPTEN'], 'emcc'), | 67 env['PYTHON'], os.path.join(env['EMSCRIPTEN'], 'emcc'), |
|
Vasily Kuznetsov
2017/03/23 19:26:53
Again invoke python script with a python interpret
| |
| 67 '-o', COMPILER_OUTPUT, | 68 '-o', COMPILER_OUTPUT, |
| 68 '--post-js', BINDINGS_OUTPUT, | 69 '--post-js', BINDINGS_OUTPUT, |
| 69 ] | 70 ] |
| 70 params.extend(SOURCE_FILES) | 71 params.extend(SOURCE_FILES) |
| 71 params.extend('-D' + flag for flag in DEFINES) | 72 params.extend('-D' + flag for flag in DEFINES) |
| 72 for key, value in GENERATION_PARAMS.iteritems(): | 73 for key, value in GENERATION_PARAMS.iteritems(): |
| 73 params.extend(['-s', '{}={}'.format(key, str(value))]) | 74 params.extend(['-s', '{}={}'.format(key, str(value))]) |
| 74 if debug: | 75 if debug: |
| 75 params.append('-g1') | 76 params.append('-g1') |
| 76 if tracing: | 77 if tracing: |
| 77 params.append('--tracing') | 78 params.append('--tracing') |
| 78 params.extend(ADDITIONAL_PARAMS) | 79 params.extend(ADDITIONAL_PARAMS) |
| 79 subprocess.check_call(params, env=env) | 80 subprocess.check_call(params, env=env) |
| 80 | 81 |
| 81 | 82 |
| 82 if __name__ == '__main__': | 83 if __name__ == '__main__': |
| 83 parser = argparse.ArgumentParser( | 84 parser = argparse.ArgumentParser( |
| 84 description='Compile Emscripten-based C++ code to JavaScript' | 85 description='Compile Emscripten-based C++ code to JavaScript' |
| 85 ) | 86 ) |
| 86 parser.add_argument( | 87 parser.add_argument( |
| 87 '--emscripten-config', | 88 '--emscripten-config', |
|
Vasily Kuznetsov
2017/03/23 19:26:53
We switch to using emscripten config instead of us
Wladimir Palant
2017/03/24 06:20:41
emsdk construct_env (which is what we have been ca
| |
| 88 metavar='DIR', | 89 metavar='DIR', |
| 89 default=os.path.expanduser('~/.emscripten'), | 90 default=os.path.expanduser('~/.emscripten'), |
| 90 help='Emscripten installation directory' | 91 help='Emscripten installation directory' |
| 91 ) | 92 ) |
| 92 parser.add_argument( | 93 parser.add_argument( |
| 93 '-d', '--debug', | 94 '-d', '--debug', |
| 94 action='store_true', | 95 action='store_true', |
| 95 help='Disable code minification' | 96 help='Disable code minification' |
| 96 ) | 97 ) |
| 97 parser.add_argument( | 98 parser.add_argument( |
| 98 '-t', '--tracing', | 99 '-t', '--tracing', |
| 99 action='store_true', | 100 action='store_true', |
| 100 help='Enable memory tracing' | 101 help='Enable memory tracing' |
| 101 ) | 102 ) |
| 102 args = parser.parse_args() | 103 args = parser.parse_args() |
| 103 | 104 |
| 104 env = getenv(args.emscripten_config) | 105 env = getenv(args.emscripten_config) |
| 105 generate_bindings(env) | 106 generate_bindings(env) |
| 106 run_compiler(env, debug=args.debug, tracing=args.tracing) | 107 run_compiler(env, debug=args.debug, tracing=args.tracing) |
| LEFT | RIGHT |