forked from Hubs-Foundation/ammo.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.py
executable file
·206 lines (156 loc) · 6.9 KB
/
make.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/python
import os, sys, re, json, shutil, multiprocessing
from subprocess import Popen, PIPE, STDOUT
# Definitions
INCLUDES = ['btBulletDynamicsCommon.h',
os.path.join('BulletCollision', 'CollisionShapes', 'btHeightfieldTerrainShape.h'),
os.path.join('BulletCollision', 'CollisionShapes', 'btConvexPolyhedron.h'),
os.path.join('BulletCollision', 'CollisionShapes', 'btShapeHull.h'),
os.path.join('BulletCollision', 'CollisionDispatch', 'btGhostObject.h'),
os.path.join('BulletDynamics', 'Character', 'btKinematicCharacterController.h'),
os.path.join('BulletSoftBody', 'btSoftBody.h'),
os.path.join('BulletSoftBody', 'btSoftRigidDynamicsWorld.h'), os.path.join('BulletSoftBody', 'btDefaultSoftBodySolver.h'),
os.path.join('BulletSoftBody', 'btSoftBodyRigidBodyCollisionConfiguration.h'),
os.path.join('BulletSoftBody', 'btSoftBodyHelpers.h'),
os.path.join('HACD', 'hacdHACD.h'),
os.path.join('VHACD', 'public', 'VHACD.h'),
os.path.join('VHACD', 'inc', 'vhacdTimer.h'),
'sstream',
os.path.join('VHACD', 'inc', 'vhacdVHACD.h'),
os.path.join('..', '..', 'idl_templates.h')]
# Startup
stage_counter = 0
def which(program):
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if os.path.exists(exe_file):
return exe_file
return None
def build():
EMSCRIPTEN_ROOT = os.environ.get('EMSCRIPTEN')
if not EMSCRIPTEN_ROOT:
emcc = which('emcc')
EMSCRIPTEN_ROOT = os.path.dirname(emcc)
if not EMSCRIPTEN_ROOT:
print "ERROR: EMSCRIPTEN_ROOT environment variable (which should be equal to emscripten's root dir) not found"
sys.exit(1)
sys.path.append(EMSCRIPTEN_ROOT)
import tools.shared as emscripten
# Settings
'''
Settings.INLINING_LIMIT = 0
Settings.DOUBLE_MODE = 0
Settings.PRECISE_I64_MATH = 0
Settings.CORRECT_SIGNS = 0
Settings.CORRECT_OVERFLOWS = 0
Settings.CORRECT_ROUNDINGS = 0
'''
wasm = 'wasm' in sys.argv
closure = 'closure' in sys.argv
add_function_support = 'add_func' in sys.argv
#-s ENVIRONMENT=web
args = '-O3 --llvm-lto 1 -s NO_EXIT_RUNTIME=1 -s NO_FILESYSTEM=1 -s EXPORTED_RUNTIME_METHODS=["UTF8ToString"]'
if add_function_support:
args += ' -s RESERVED_FUNCTION_POINTERS=20 -s EXTRA_EXPORTED_RUNTIME_METHODS=["addFunction"]'
if not wasm:
args += ' -s WASM=0 -s AGGRESSIVE_VARIABLE_ELIMINATION=1 -s ELIMINATE_DUPLICATE_FUNCTIONS=1 -s SINGLE_FILE=1 -s LEGACY_VM_SUPPORT=1'
else:
args += ''' -s WASM=1 -s BINARYEN_IGNORE_IMPLICIT_TRAPS=1 -s BINARYEN_TRAP_MODE="clamp"'''
if closure:
args += ' --closure 1 -s IGNORE_CLOSURE_COMPILER_ERRORS=1' # closure complains about the bullet Node class (Node is a DOM thing too)
else:
args += ' -s NO_DYNAMIC_EXECUTION=1'
emcc_args = args.split(' ')
emcc_args += ['-s', 'TOTAL_MEMORY=%d' % (64*1024*1024)] # default 64MB. Compile with ALLOW_MEMORY_GROWTH if you want a growable heap (slower though).
emcc_args += ['-s', 'ALLOW_MEMORY_GROWTH=1'] # resizable heap, with some amount of slowness
emcc_args += '-s EXPORT_NAME="Ammo" -s MODULARIZE=1'.split(' ')
target = 'ammo.js' if not wasm else 'ammo.wasm.js'
print
print '--------------------------------------------------'
print 'Building ammo.js, build type:', emcc_args
print '--------------------------------------------------'
print
'''
import os, sys, re
infile = open(sys.argv[1], 'r').read()
outfile = open(sys.argv[2], 'w')
t1 = infile
while True:
t2 = re.sub(r'\(\n?!\n?1\n?\+\n?\(\n?!\n?1\n?\+\n?(\w)\n?\)\n?\)', lambda m: '(!1+' + m.group(1) + ')', t1)
print len(infile), len(t2)
if t1 == t2: break
t1 = t2
outfile.write(t2)
'''
# Utilities
def stage(text):
global stage_counter
stage_counter += 1
text = 'Stage %d: %s' % (stage_counter, text)
print
print '=' * len(text)
print text
print '=' * len(text)
print
# Main
try:
this_dir = os.getcwd()
os.chdir('bullet')
if not os.path.exists('build'):
os.makedirs('build')
os.chdir('build')
stage('Generate bindings')
Popen([emscripten.PYTHON, os.path.join(EMSCRIPTEN_ROOT, 'tools', 'webidl_binder.py'), os.path.join(this_dir, 'ammo.idl'), 'glue']).communicate()
assert os.path.exists('glue.js')
assert os.path.exists('glue.cpp')
stage('Build bindings')
args = ['-I../src', '-I../Extras', '-c']
for include in INCLUDES:
args += ['-include', include]
emscripten.Building.emcc('glue.cpp', args, 'glue.o')
assert(os.path.exists('glue.o'))
# Use CMake to build, because ./configure doesn't know how to build the extras correctly yet.
cmake_build = True
if cmake_build:
if not os.path.exists('CMakeCache.txt'):
stage('Configure via CMake')
emscripten.Building.configure([emscripten.PYTHON, os.path.join(EMSCRIPTEN_ROOT, 'emcmake'), 'cmake', '..', '-DBUILD_DEMOS=OFF', '-DBUILD_EXTRAS=ON', '-DBUILD_CPU_DEMOS=OFF', '-DUSE_GLUT=OFF', '-DCMAKE_BUILD_TYPE=Release'])
else:
if not os.path.exists('config.h'):
stage('Configure (if this fails, run autogen.sh in bullet/ first)')
emscripten.Building.configure(['../configure', '--disable-demos', '--disable-extras', '--disable-dependency-tracking'])
stage('Make')
CORES = multiprocessing.cpu_count()
if emscripten.WINDOWS:
emscripten.Building.make(['mingw32-make', '-j', str(CORES)])
else:
emscripten.Building.make(['make', '-j', str(CORES)])
stage('Link')
if cmake_build:
bullet_libs = [os.path.join('src', 'BulletSoftBody', 'libBulletSoftBody.a'),
os.path.join('src', 'BulletDynamics', 'libBulletDynamics.a'),
os.path.join('src', 'BulletCollision', 'libBulletCollision.a'),
os.path.join('src', 'LinearMath', 'libLinearMath.a'),
os.path.join('Extras', 'HACD', 'libHACD.a'),
os.path.join('Extras', 'VHACD', 'libVHACD.a')]
else:
bullet_libs = [os.path.join('src', '.libs', 'libBulletSoftBody.a'),
os.path.join('src', '.libs', 'libBulletDynamics.a'),
os.path.join('src', '.libs', 'libBulletCollision.a'),
os.path.join('src', '.libs', 'libLinearMath.a'),
os.path.join('Extras', '.libs', 'libHACD.a'),
os.path.join('Extras', '.libs', 'libVHACD.a')]
stage('emcc: ' + ' '.join(emcc_args))
temp = os.path.join('..', '..', 'builds', target)
emscripten.Building.emcc('-DNOTHING_WAKA_WAKA', emcc_args + ['glue.o'] + bullet_libs + ['--js-transform', 'python %s' % os.path.join('..', '..', 'bundle.py')],
temp)
assert os.path.exists(temp), 'Failed to create script code'
stage('wrap')
wrapped = '''
// This is ammo.js, a port of Bullet Physics to JavaScript. zlib licensed.
''' + open(temp).read()
open(temp, 'w').write(wrapped)
finally:
os.chdir(this_dir);
if __name__ == '__main__':
build()