OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
| 3 import argparse |
3 import os | 4 import os |
4 import re | 5 import re |
5 import subprocess | 6 import subprocess |
6 | 7 |
7 BASE_DIR = os.path.dirname(__file__) or os.getcwd() | 8 BASE_DIR = os.path.dirname(__file__) or os.getcwd() |
8 EMSCRIPTEN_PATH = os.path.join(BASE_DIR, '..', 'emscripten') | |
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') |
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') |
(...skipping 10 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(): | 39 def getenv(emscripten_path): |
40 path = [] | 40 path = [] |
41 env = {} | 41 env = {} |
42 output = subprocess.check_output([ | 42 output = subprocess.check_output([ |
43 '/bin/bash', '-c', os.path.join(EMSCRIPTEN_PATH, 'emsdk_env.sh') | 43 '/bin/bash', '-c', os.path.join(emscripten_path, 'emsdk_env.sh') |
44 ]) | 44 ]) |
45 for line in output.splitlines(): | 45 for line in output.splitlines(): |
46 match = re.search(r'^\s*PATH\s*\+=\s*(.*)', line) | 46 match = re.search(r'^\s*PATH\s*\+=\s*(.*)', line) |
47 if match: | 47 if match: |
48 path.append(match.group(1)) | 48 path.append(match.group(1)) |
49 match = re.search(r'^\s*(\w+)\s*=\s*(.*)', line) | 49 match = re.search(r'^\s*(\w+)\s*=\s*(.*)', line) |
50 if match: | 50 if match: |
51 env[match.group(1)] = match.group(2) | 51 env[match.group(1)] = match.group(2) |
52 env['PATH'] = os.pathsep.join([os.environ['PATH']] + path) | 52 env['PATH'] = os.pathsep.join([os.environ['PATH']] + path) |
53 return env | 53 return env |
54 | 54 |
55 | 55 |
56 def generate_bindings(env): | 56 def generate_bindings(env): |
57 params = [ | 57 params = [ |
58 os.path.join(env['EMSCRIPTEN'], 'emcc'), BINDINGS_FILE, | 58 os.path.join(env['EMSCRIPTEN'], 'emcc'), BINDINGS_FILE, |
59 '-o', BINDINGS_GENERATOR, '-std=gnu++14', '-DPRINT_BINDINGS', | 59 '-o', BINDINGS_GENERATOR, '-std=gnu++14', '-DPRINT_BINDINGS', |
60 '-s', 'WARN_ON_UNDEFINED_SYMBOLS=0', | 60 '-s', 'WARN_ON_UNDEFINED_SYMBOLS=0', |
61 ] | 61 ] |
62 subprocess.check_call(params, env=env) | 62 subprocess.check_call(params, env=env) |
63 | 63 |
64 node = subprocess.check_output('which node', env=env, shell=True).strip() | 64 node = subprocess.check_output('which node', env=env, shell=True).strip() |
65 with open(BINDINGS_OUTPUT, 'w') as file: | 65 with open(BINDINGS_OUTPUT, 'w') as file: |
66 subprocess.check_call([node, BINDINGS_GENERATOR], env=env, stdout=file) | 66 subprocess.check_call([node, BINDINGS_GENERATOR], env=env, stdout=file) |
67 | 67 |
68 | 68 |
69 def run_compiler(env): | 69 def run_compiler(env, debug=False, tracing=False): |
70 params = [ | 70 params = [ |
71 os.path.join(env['EMSCRIPTEN'], 'emcc'), | 71 os.path.join(env['EMSCRIPTEN'], 'emcc'), |
72 '-o', COMPILER_OUTPUT, | 72 '-o', COMPILER_OUTPUT, |
73 '--post-js', BINDINGS_OUTPUT, | 73 '--post-js', BINDINGS_OUTPUT, |
74 ] | 74 ] |
75 params.extend(SOURCE_FILES) | 75 params.extend(SOURCE_FILES) |
76 params.extend('-D' + flag for flag in DEFINES) | 76 params.extend('-D' + flag for flag in DEFINES) |
77 for key, value in GENERATION_PARAMS.iteritems(): | 77 for key, value in GENERATION_PARAMS.iteritems(): |
78 params.extend(['-s', '{}={}'.format(key, str(value))]) | 78 params.extend(['-s', '{}={}'.format(key, str(value))]) |
| 79 if debug: |
| 80 params.append('-g1') |
| 81 if tracing: |
| 82 params.append('--tracing') |
79 params.extend(ADDITIONAL_PARAMS) | 83 params.extend(ADDITIONAL_PARAMS) |
80 subprocess.check_call(params, env=env) | 84 subprocess.check_call(params, env=env) |
81 | 85 |
82 | 86 |
83 if __name__ == '__main__': | 87 if __name__ == '__main__': |
84 env = getenv() | 88 parser = argparse.ArgumentParser( |
| 89 description='Compile Emscripten-based C++ code to JavaScript' |
| 90 ) |
| 91 parser.add_argument( |
| 92 '--emscripten', |
| 93 metavar='DIR', |
| 94 default=os.path.join(BASE_DIR, '..', 'emscripten'), |
| 95 help='Emscripten installation directory' |
| 96 ) |
| 97 parser.add_argument( |
| 98 '-d', '--debug', |
| 99 action='store_true', |
| 100 help='Disable code minification' |
| 101 ) |
| 102 parser.add_argument( |
| 103 '-t', '--tracing', |
| 104 action='store_true', |
| 105 help='Enable memory tracing' |
| 106 ) |
| 107 args = parser.parse_args() |
| 108 |
| 109 env = getenv(args.emscripten) |
85 generate_bindings(env) | 110 generate_bindings(env) |
86 run_compiler(env) | 111 run_compiler(env, debug=args.debug, tracing=args.tracing) |
OLD | NEW |