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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
#!/usr/bin/env python
'''
Use CppHeaderParser to parse some C++ headers, and generate binding code for them.
Usage:
bindings_generator.py BASENAME HEADER1 HEADER2 ... [-- JSON]
BASENAME is the name used for output files (with added suffixes).
HEADER1 etc. are the C++ headers to parse
We generate the following:
* BASENAME.c: C bindings file, with generated C wrapper functions. You will
need to build this with your project, and make sure it compiles
properly by adding the proper #includes etc. You can also just
#include this file itself in one of your existing project files.
* BASENAME.js: JavaScript bindings file, with generated JavaScript wrapper
objects. This is a high-level wrapping, using native JS classes.
* JSON: An optional JSON object with various optional options:
ignored: A list of classes and class::methods not to generate code for.
Comma separated.
type_processor: Text that is eval()d into a lambda that is run on
all arguments. For example, you can use this to
change all arguments of type float& to float by
"type_processor": "lambda t: t if t != 'float&' else 'float'"
export: If true, will export all bindings in the .js file. This allows
you to run something like closure compiler advanced opts on
the library+bindings, and the bindings will remain accessible.
For example, JSON can be { "ignored": "class1,class2::func" }.
The C bindings file is basically a tiny C wrapper around the C++ code.
It's only purpose is to make it easy to access the C++ code in the JS
bindings, and to prevent DFE from removing the code we care about. The
JS bindings do more serious work, creating class structures in JS and
linking them to the C bindings.
'''
import os, sys, glob, re
abspath = os.path.abspath(os.path.dirname(__file__))
def path_from_root(*pathelems):
return os.path.join(os.path.sep, *(abspath.split(os.sep)[:-1] + list(pathelems)))
exec(open(path_from_root('tools', 'shared.py'), 'r').read())
# Find ply and CppHeaderParser
sys.path = [path_from_root('third_party', 'ply'), path_from_root('third_party', 'CppHeaderParser')] + sys.path
import CppHeaderParser
#print glob.glob(path_from_root('tests', 'bullet', 'src', 'BulletCollision', 'CollisionDispatch', '*.h'))
basename = sys.argv[1]
ignored = []
type_processor = lambda t: t
export = 0
if '--' in sys.argv:
index = sys.argv.index('--')
json = eval(sys.argv[index+1])
sys.argv = sys.argv[:index]
if json.get('ignored'):
ignored = json['ignored'].split(',')
if json.get('type_processor'):
type_processor = eval(json['type_processor'])
if json.get('export'):
export = json['export']
print 'zz ignoring', ignored
# First pass - read everything
classes = {}
parents = {}
text = ''
for header in sys.argv[2:]:
text += '//// ' + header + '\n'
text += open(header, 'r').read()
all_h_name = basename + '.all.h'
all_h = open(all_h_name, 'w')
all_h.write(text)
all_h.close()
parsed = CppHeaderParser.CppHeader(all_h_name)
for classname, clazz in parsed.classes.iteritems():
print 'zz see', classname
classes[classname] = clazz
clazz['methods'] = clazz['methods']['public'] # CppHeaderParser doesn't have 'public' etc. in structs. so equalize to that
if '::' in classname:
assert classname.count('::') == 1
parents[classname.split('::')[1]] = classname.split('::')[0]
for sname, struct in clazz._public_structs.iteritems():
parents[sname] = classname
classes[classname + '::' + sname] = struct
struct['name'] = sname # Missing in CppHeaderParser
print 'zz seen struct %s in %s' % (sname, classname)
for classname, clazz in classes.iteritems():
# Various precalculations
print 'zz precalc', classname
for method in clazz['methods'][:]:
method['constructor'] = method['constructor'] or (method['name'] == classname) # work around cppheaderparser issue
print 'z constructorhmm?', method['name'], method['constructor']#, constructor, method['name'], classname
args = method['parameters']
#if method['name'] == 'addWheel': print 'qqqq', classname, method
# Fill in some missing stuff
for i in range(len(args)):
if args[i]['pointer'] and '*' not in args[i]['type']:
args[i]['type'] += '*'
if args[i]['reference'] and '&' not in args[i]['type']:
args[i]['type'] += '&'
args[i]['type'] = type_processor(args[i]['type'])
#raw = args[i]['type'].replace('&', '').replace('*', '')
#if raw in classes:
default_param = len(args)+1
for i in range(len(args)):
if args[i].get('default'):
default_param = i+1
break
method['num_args'] = set(range(default_param-1, len(args)+1))
print 'zz ', classname, 'has num_args of', method['num_args']
method['returns_text'] = method['returns']
if method['static']:
method['returns_text'] = method['returns_text'].replace('static', '')
# Fill in some missing stuff
if method.get('returns_const'): method['returns_text'] = 'const ' + method['returns_text']
if method.get('returns_pointer'):
while method['returns_text'].count('*') < method['returns_pointer']:
method['returns_text'] += '*'
if method.get('returns_reference'): method['returns_text'] += '&'
method['returns_text'] = type_processor(method['returns_text'])
print 'zz %s::%s gets %s and returns %s' % (classname, method['name'], str([arg['type'] for arg in method['parameters']]), method['returns_text'])
# Explore all functions we need to generate, including parent classes, handling of overloading, etc.
def clean_type(t):
return t.replace('const ', '').replace('struct ', '').replace('&', '').replace('*', '').replace(' ', '')
def fix_template_value(t): # Not sure why this is needed, might be a bug in CppHeaderParser
if t == 'unsignedshortint':
return 'unsigned short int'
elif t == 'unsignedint':
return 'unsigned int'
return t
for classname, clazz in parsed.classes.iteritems():
clazz['final_methods'] = {}
def explore(subclass, template_name=None, template_value=None):
# Do our functions first, and do not let later classes override
for method in subclass['methods']:
print classname, 'exploring', subclass['name'], '::', method['name']
if method['constructor']:
if clazz != subclass: continue # Subclasses cannot directly use their parent's constructors
if method['destructor']: continue # Nothing to do there
if method['name'] not in clazz['final_methods']:
copied = clazz['final_methods'][method['name']] = {}
for key in ['name', 'constructor', 'static', 'returns', 'returns_text', 'returns_reference', 'returns_pointer', 'destructor', 'pure_virtual']:
copied[key] = method[key]
copied['num_args'] = method['num_args'].copy()
copied['origin'] = subclass
copied['parameters'] = [];
# Copy the arguments, since templating may cause them to be altered
for arg in method['parameters'][:]:
copiedarg = {
'type': arg['type'],
'name': arg['name'],
}
copied['parameters'].append(copiedarg)
if template_name:
# Set template values
copied['returns'] = copied['returns'].replace(template_name, template_value)
copied['returns_text'] = copied['returns_text'].replace(template_name, template_value)
for arg in copied['parameters']:
arg['type'] = arg['type'].replace(template_name, template_value)
else:
# Merge the new function in the best way we can. Shared arguments must match!
curr = clazz['final_methods'][method['name']]
if curr['origin'] is not subclass: continue # child class functions mask/hide parent functions of the same name in C++
if any([curr['parameters'][i]['type'] != method['parameters'][i]['type'] for i in range(min(len(curr['parameters']), len(method['parameters'])))]):
print 'Warning: Cannot mix in overloaded functions', method['name'], 'in class', classname, ', skipping'
continue
# TODO: Other compatibility checks, if any?
if len(method['parameters']) > len(curr['parameters']):
curr['parameters'] = method['parameters']
curr['num_args'] = curr['num_args'].union(method['num_args'])
print 'zz ', classname, 'has an updated num_args of ', curr['num_args']
# Recurse
if subclass.get('inherits'):
for parent in subclass['inherits']:
parent = parent['class'
|