LEFT | RIGHT |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 import json | 3 import json |
4 import os | 4 import os |
5 import re | 5 import re |
6 import subprocess | 6 import subprocess |
7 import warnings | 7 import warnings |
8 | 8 |
9 EMSCRIPTEN_PATH = '../emscripten' | 9 EMSCRIPTEN_PATH = '../emscripten' |
10 SOURCE_DIR = './compiled' | 10 SOURCE_DIR = './compiled' |
11 SOURCE_FILES = [ | 11 SOURCE_FILES = [ |
12 os.path.join(SOURCE_DIR, f) | 12 os.path.join(SOURCE_DIR, f) |
13 for f in os.listdir(SOURCE_DIR) | 13 for f in os.listdir(SOURCE_DIR) |
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') |
17 BINDINGS_GENERATOR = os.path.join(SOURCE_DIR, 'bindings.cpp.js') | 17 BINDINGS_GENERATOR = os.path.join(SOURCE_DIR, 'bindings.cpp.js') |
18 BINDINGS_OUTPUT = os.path.join(SOURCE_DIR, 'bindings.js') | 18 BINDINGS_OUTPUT = os.path.join(SOURCE_DIR, 'bindings.js') |
19 COMPILER_OUTPUT = './lib/compiled.js' | 19 COMPILER_OUTPUT = './lib/compiled.js' |
20 GENERATION_PARAMS = { | 20 GENERATION_PARAMS = { |
21 'SHELL_FILE': "'%s'" % os.path.abspath(os.path.join(SOURCE_DIR, 'shell.js')), | 21 'SHELL_FILE': "'%s'" % os.path.abspath(os.path.join(SOURCE_DIR, 'shell.js')), |
22 'ASM_JS': 2, # "almost asm" | 22 'ASM_JS': 2, # "almost asm" |
23 'TOTAL_MEMORY': 16*1024*1024, | 23 'TOTAL_MEMORY': 16*1024*1024, |
24 'TOTAL_STACK': 1*1024*1024, | 24 'TOTAL_STACK': 1*1024*1024, |
25 'ALLOW_MEMORY_GROWTH': 1, | 25 'ALLOW_MEMORY_GROWTH': 1, |
26 'NO_EXIT_RUNTIME': 1, | 26 'NO_EXIT_RUNTIME': 1, |
27 'DISABLE_EXCEPTION_CATCHING': 0, | |
28 'NO_DYNAMIC_EXECUTION': 1, | 27 'NO_DYNAMIC_EXECUTION': 1, |
29 'NO_BROWSER': 1, | 28 'NO_BROWSER': 1, |
30 'NO_FILESYSTEM': 1, | 29 'NO_FILESYSTEM': 1, |
31 'INVOKE_RUN': 0, | 30 'INVOKE_RUN': 0, |
32 'NODE_STDOUT_FLUSH_WORKAROUND': 0, | 31 'NODE_STDOUT_FLUSH_WORKAROUND': 0, |
33 } | 32 } |
34 DEFINES = ['DEBUG'] | 33 DEFINES = [] |
35 ADDITIONAL_PARAMS = ['-O3', '-m32', '-std=gnu++14', '--memory-init-file', '0', | 34 ADDITIONAL_PARAMS = ['-O3', '-m32', '-std=gnu++14', '--memory-init-file', '0', |
36 '--emit-symbol-map'] | 35 '--emit-symbol-map'] |
37 | 36 |
38 def getenv(): | 37 def getenv(): |
39 path = [] | 38 path = [] |
40 env = {} | 39 env = {} |
41 output = subprocess.check_output([ | 40 output = subprocess.check_output([ |
42 '/bin/sh', '-c', os.path.join(EMSCRIPTEN_PATH, 'emsdk_env.sh')]) | 41 '/bin/bash', '-c', os.path.join(EMSCRIPTEN_PATH, 'emsdk_env.sh')]) |
43 for line in output.splitlines(): | 42 for line in output.splitlines(): |
44 match = re.search(r'^\s*PATH\s*\+=\s*(.*)', line) | 43 match = re.search(r'^\s*PATH\s*\+=\s*(.*)', line) |
45 if match: | 44 if match: |
46 path.append(match.group(1)) | 45 path.append(match.group(1)) |
47 match = re.search(r'^\s*(\w+)\s*=\s*(.*)', line) | 46 match = re.search(r'^\s*(\w+)\s*=\s*(.*)', line) |
48 if match: | 47 if match: |
49 env[match.group(1)] = match.group(2) | 48 env[match.group(1)] = match.group(2) |
50 env['PATH'] = ':'.join([os.environ['PATH']] + path) | 49 env['PATH'] = os.pathsep.join([os.environ['PATH']] + path) |
51 return env | 50 return env |
52 | 51 |
53 def generate_bindings(env): | 52 def generate_bindings(env): |
54 params = [os.path.join(env['EMSCRIPTEN'], 'emcc'), BINDINGS_FILE, | 53 params = [os.path.join(env['EMSCRIPTEN'], 'emcc'), BINDINGS_FILE, |
55 '-o', BINDINGS_GENERATOR, '-std=gnu++14', '-DPRINT_BINDINGS', | 54 '-o', BINDINGS_GENERATOR, '-std=gnu++14', '-DPRINT_BINDINGS', |
56 '-s', 'SAFE_HEAP=1', '-s', 'WARN_ON_UNDEFINED_SYMBOLS=0', | 55 '-s', 'WARN_ON_UNDEFINED_SYMBOLS=0', |
57 ] | 56 ] |
58 subprocess.check_call(params, env=env) | 57 subprocess.check_call(params, env=env) |
59 | 58 |
60 node = subprocess.check_output('which node', env=env, shell=True).strip(); | 59 node = subprocess.check_output('which node', env=env, shell=True).strip(); |
61 with open(BINDINGS_OUTPUT, 'w') as file: | 60 with open(BINDINGS_OUTPUT, 'w') as file: |
62 subprocess.check_call([node, BINDINGS_GENERATOR], env=env, stdout=file) | 61 subprocess.check_call([node, BINDINGS_GENERATOR], env=env, stdout=file) |
63 | 62 |
64 def run_compiler(env): | 63 def run_compiler(env): |
65 params = [ | 64 params = [ |
66 os.path.join(env['EMSCRIPTEN'], 'emcc'), | 65 os.path.join(env['EMSCRIPTEN'], 'emcc'), |
67 '-o', COMPILER_OUTPUT, | 66 '-o', COMPILER_OUTPUT, |
68 '--post-js', BINDINGS_OUTPUT, | 67 '--post-js', BINDINGS_OUTPUT, |
69 ] | 68 ] |
70 params.extend(SOURCE_FILES) | 69 params.extend(SOURCE_FILES) |
71 params.extend('-D' + flag for flag in DEFINES) | 70 params.extend('-D' + flag for flag in DEFINES) |
72 for key, value in GENERATION_PARAMS.iteritems(): | 71 for key, value in GENERATION_PARAMS.iteritems(): |
73 params.extend(['-s', '%s=%s' % (key, str(value))]) | 72 params.extend(['-s', '%s=%s' % (key, str(value))]) |
74 params.extend(ADDITIONAL_PARAMS) | 73 params.extend(ADDITIONAL_PARAMS) |
75 subprocess.check_call(params, env=env) | 74 subprocess.check_call(params, env=env) |
76 | 75 |
77 if __name__ == '__main__': | 76 if __name__ == '__main__': |
78 env = getenv() | 77 env = getenv() |
79 generate_bindings(env) | 78 generate_bindings(env) |
80 run_compiler(env) | 79 run_compiler(env) |
LEFT | RIGHT |