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