-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathtest_examples.py
executable file
·464 lines (385 loc) · 14.9 KB
/
test_examples.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
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
#!/usr/bin/env python3
import argparse
import errno
import logging
import os
import multiprocessing
import re
import sys
from typing import Dict, List, Optional
from common import (
config as c,
pb,
Colors,
die,
get_cmd_or_die,
invoke,
regex,
setup_logging,
transpile,
on_mac
)
cargo = get_cmd_or_die('cargo')
git = get_cmd_or_die('git')
intercept_build = get_cmd_or_die('intercept_build')
make = get_cmd_or_die('make')
python = get_cmd_or_die('python')
rustc = get_cmd_or_die('rustc')
NUM_JOBS = multiprocessing.cpu_count()
EXAMPLES = [
'genann',
'grabc',
'libxml2',
'lil',
'snudown',
'tmux',
'urlparser',
'xzoom'
]
def build_path(path: str, new: str, is_dir: bool) -> str:
err_msg = "`{}` does not exist in {}".format(new, path)
new_path = os.path.join(path, new)
if is_dir:
assert os.path.isdir(new_path), err_msg
else:
assert os.path.isfile(new_path), err_msg
return new_path
def print_blue(msg: str) -> None:
print(Colors.OKBLUE + msg + Colors.NO_COLOR)
class Test:
def __init__(self, args: argparse.Namespace):
self.args = args
self.project_name = ''
self.transpiler_args: List[str] = []
self.ib_cmd: List[str] = []
self.example_dir = ''
self.repo_dir = ''
# Source directory where `Crate` files will live,
# e.g. `rust`
self.rust_src = ''
self.cc_db = ''
def init_submodule(self) -> None:
if self.args.regex_examples.fullmatch(self.project_name):
print_blue("Initializing {}...".format(self.project_name))
with pb.local.cwd(self.example_dir):
invoke(git, ['submodule', 'update', '--init', 'repo'])
def deinit_submodule(self) -> None:
if self.args.regex_examples.fullmatch(self.project_name) and self.args.deinit:
print_blue("Deinitializing {}...".format(self.project_name))
with pb.local.cwd(self.example_dir):
invoke(git, ['submodule', 'deinit', 'repo', '-f'])
# Should be used on projects that utilize GNU Build Systems
def autotools(self, configure_args: List[str] = []) -> None:
with pb.local.cwd(self.repo_dir):
invoke(pb.local['./autogen.sh'])
with pb.local.env(CFLAGS="-g -O0"):
invoke(pb.local['./configure'], configure_args)
# `gen_cc_db` generates the `compile_commands.json` for a project
def gen_cc_db(self) -> None:
with pb.local.cwd(self.repo_dir):
invoke(make, ['clean'])
invoke(intercept_build, *self.ib_cmd)
self.cc_db = build_path(self.repo_dir, 'compile_commands.json',
is_dir=False)
# `transpile` in most cases runs the transpile function from `common.py`,
# which in turn just calls `c2rust transpile *args`
def transpile(self) -> None:
with pb.local.cwd(self.repo_dir):
transpile(self.cc_db,
emit_build_files=False,
extra_transpiler_args=self.transpiler_args)
# `build` is the main builder function, this is where either the `Crate`
# will be built or rustc will be called directly
def build(self) -> None:
with pb.local.cwd(self.rust_src):
invoke(cargo, ['build', '-j{}'.format(NUM_JOBS)])
def test(self) -> None:
pass
def build_and_test(self) -> None:
self.gen_cc_db()
self.transpile()
self.build()
self.test()
class Genann(Test):
def __init__(self, args: argparse.Namespace) -> None:
self.args = args
self.project_name = 'genann'
self.example_dir = build_path(
c.EXAMPLES_DIR, self.project_name, is_dir=True)
self.repo_dir = build_path(self.example_dir, 'repo', is_dir=True)
self.rust_src = os.path.join(self.example_dir, 'rust')
self.transpiler_args = ['--emit-build-files', '--overwrite-existing',
'--output-dir', self.rust_src]
self.ib_cmd = ['make']
self.init_submodule()
def __del__(self) -> None:
self.deinit_submodule()
def test(self) -> None:
rm = get_cmd_or_die('rm')
ln = get_cmd_or_die('ln')
for N in (1, 4):
test = 'example{}'.format(N)
with pb.local.cwd(self.repo_dir):
invoke(rm, ['-rf', self.rust_src])
self._transpile_example(test)
with pb.local.cwd(self.rust_src):
# Create a link to the example data files
invoke(ln, ['-sf', build_path(self.repo_dir, 'example', True)])
invoke(cargo, ['run'])
# Helper function that transpiles whatever test is
# passed in as `main`
def _transpile_example(self, main: str) -> None:
transpile(self.cc_db,
emit_build_files=False,
extra_transpiler_args=['--emit-build-files', '--binary', main,
'--overwrite-existing',
'--output-dir', self.rust_src])
class Grabc(Test):
def __init__(self, args: argparse.Namespace) -> None:
self.args = args
self.project_name = 'grabc'
self.transpiler_args = ['--overwrite-existing']
self.ib_cmd = ['make']
self.example_dir = build_path(
c.EXAMPLES_DIR, self.project_name, is_dir=True)
self.repo_dir = build_path(self.example_dir, 'repo', is_dir=True)
self.build_flags = ['grabc.rs', '-L/usr/x11R6/lib', '-lX11']
self.init_submodule()
def __del__(self) -> None:
self.deinit_submodule()
def build(self) -> None:
with pb.local.cwd(self.repo_dir):
invoke(rustc, *self.build_flags)
class Libxml2(Test):
def __init__(self, args: argparse.Namespace) -> None:
self.args = args
self.project_name = 'libxml2'
self.transpiler_args = []
self.ib_cmd = ['make', 'check', '-j{}'.format(NUM_JOBS)]
self.example_dir = build_path(
c.EXAMPLES_DIR, self.project_name, is_dir=True)
self.repo_dir = build_path(self.example_dir, 'repo', is_dir=True)
self.build_flags: List[str] = []
self.init_submodule()
self.rust_src = os.path.join(self.repo_dir, 'rust')
def __del__(self) -> None:
self.deinit_submodule()
def gen_cc_db(self) -> None:
# Without --disable-static, libtool builds two copies of many source
# files. We can't handle that, so we disable that behavior here.
self.autotools(['--disable-static'])
with pb.local.cwd(self.repo_dir):
invoke(make, ['clean'])
invoke(intercept_build, *self.ib_cmd)
def transpile(self) -> None:
with pb.local.cwd(self.example_dir):
invoke(pb.local['./translate.py'])
invoke(pb.local['./patch_translated_code.py'])
# Iterates through the list of tests, and then runs each one
def test(self) -> None:
# testname -> input_file
tests: Dict[str, List[str]] = {
"xmllint": ['test/bigname.xml'],
"runtest": [],
"testapi": [],
"testSAX": [],
"testURI": ['test/bigname.xml'],
"testdict": [],
"testHTML": ['test/HTML/html5_enc.html'],
"testC14N": ['--', '--with-comments', 'test/c14n/with-comments/example-7.xml'],
"testchar": [],
"testRelax": ['test/bigname.xml'],
"testXPath": ['test/bigname.xml'],
"testModule": [],
"testlimits": [],
# "testReader", Not working at the moment
"testRegexp": ['test/regexp'],
"testrecurse": [],
"testSchemas": ['test/schemas/all_0.xsd'],
"testThreads": [],
"testAutomata": ['test/automata/po'],
}
for test, input_file in tests.items():
with pb.local.cwd(self.rust_src):
example_args = ['run', '--example', test]
example_args.extend(input_file)
invoke(cargo, *example_args)
class Lil(Test):
def __init__(self, args: argparse.Namespace) -> None:
self.args = args
self.project_name = 'lil'
self.example_dir = build_path(
c.EXAMPLES_DIR, self.project_name, is_dir=True)
self.repo_dir = build_path(self.example_dir, 'repo', is_dir=True)
self.rust_src = os.path.join(self.repo_dir, 'rust')
self.transpiler_args = ['--emit-build-files', '-b', 'main',
'--overwrite-existing',
'--output-dir', self.rust_src]
self.ib_cmd = ['make']
self.build_flags: List[str] = []
self.init_submodule()
def __del__(self) -> None:
self.deinit_submodule()
class Snudown(Test):
def __init__(self, args: argparse.Namespace) -> None:
self.args = args
self.project_name = 'snudown'
self.transpiler_args = ['--overwrite-existing']
self.ib_cmd = []
self.example_dir = build_path(
c.EXAMPLES_DIR, self.project_name, is_dir=True)
self.repo_dir = build_path(self.example_dir, 'repo', is_dir=True)
self.build_flags = ['setup.py', 'build', '--translate']
self.init_submodule()
def __del__(self) -> None:
self.deinit_submodule()
def gen_cc_db(self) -> None:
pass
def transpile(self) -> None:
pass
def build(self) -> None:
with pb.local.cwd(self.repo_dir):
invoke(python, *self.build_flags)
def test(self) -> None:
with pb.local.cwd(self.repo_dir):
invoke(python, ['setup.py', 'test'])
class TinyCC(Test):
def __init__(self, args: argparse.Namespace) -> None:
self.args = args
self.project_name = 'tinycc'
self.transpiler_args = []
self.ib_cmd = ['make', '-j{}'.format(NUM_JOBS)]
self.example_dir = build_path(
c.EXAMPLES_DIR, self.project_name, is_dir=True)
self.repo_dir = build_path(self.example_dir, 'repo', is_dir=True)
self.build_flags: List[str] = []
self.init_submodule()
self.rust_src = os.path.join(self.repo_dir, 'rust')
def __del__(self) -> None:
self.deinit_submodule()
def autotools(self, configure_args: List[str] = []) -> None:
os.chdir(self.repo_dir)
invoke(pb.local['./configure'])
def gen_cc_db(self) -> None:
self.autotools()
with pb.local.cwd(self.repo_dir):
invoke(make, ['clean'])
invoke(intercept_build, *self.ib_cmd)
def transpile(self) -> None:
with pb.local.cwd(self.example_dir):
invoke(pb.local['./translate.py'])
def test(self) -> None:
with pb.local.cwd(self.repo_dir):
invoke(make, ['rust-test'])
class Tmux(Test):
def __init__(self, args: argparse.Namespace) -> None:
self.args = args
self.project_name = 'tmux'
self.transpiler_args = []
self.ib_cmd = ['make', 'check', '-j{}'.format(NUM_JOBS)]
self.example_dir = build_path(
c.EXAMPLES_DIR, self.project_name, is_dir=True)
self.repo_dir = build_path(self.example_dir, 'repo', is_dir=True)
self.build_flags: List[str] = []
self.init_submodule()
self.rust_src = os.path.join(self.repo_dir, 'rust')
def __del__(self) -> None:
self.deinit_submodule()
def gen_cc_db(self) -> None:
self.autotools()
with pb.local.cwd(self.repo_dir):
invoke(make, ['clean'])
invoke(intercept_build, *self.ib_cmd)
def transpile(self) -> None:
with pb.local.cwd(self.example_dir):
invoke(pb.local['./translate.py'])
class Urlparser(Test):
def __init__(self, args: argparse.Namespace) -> None:
self.args = args
self.project_name = 'urlparser'
self.transpiler_args = ['--overwrite-existing']
self.ib_cmd = ['make']
self.example_dir = build_path(
c.EXAMPLES_DIR, self.project_name, is_dir=True)
self.repo_dir = build_path(self.example_dir, 'repo', is_dir=True)
self.build_flags = ['test.rs']
self.init_submodule()
def __del__(self) -> None:
self.deinit_submodule()
def build(self) -> None:
with pb.local.cwd(self.repo_dir):
invoke(rustc, *self.build_flags)
class Xzoom(Test):
def __init__(self, args: argparse.Namespace) -> None:
self.args = args
self.project_name = 'xzoom'
self.transpiler_args = ['--overwrite-existing']
self.ib_cmd = ['sh', '-c',
'clang xzoom.c -L/usr/X11R6/lib -lX11 -DTIMER']
self.example_dir = build_path(
c.EXAMPLES_DIR, self.project_name, is_dir=True)
self.repo_dir = build_path(self.example_dir, 'repo', is_dir=True)
self.build_flags = ['xzoom.rs', '-L/usr/x11R6/lib', '-lX11']
self.init_submodule()
def __del__(self) -> None:
self.deinit_submodule()
def gen_cc_db(self) -> None:
with pb.local.cwd(self.repo_dir):
invoke(intercept_build, *self.ib_cmd)
self.cc_db = build_path(self.repo_dir, 'compile_commands.json',
is_dir=False)
def build(self) -> None:
with pb.local.cwd(self.repo_dir):
invoke(rustc, *self.build_flags)
def _is_excluded(name: str) -> bool:
"""
The examples that use x11 and the `f128` crate need to be excluded on macOS.
This function can be extended to exclude examples
on the linux platform as well.
"""
mac_exclusion_set = {
"grabc",
"libxml2",
"tinycc",
"xzoom",
}
return name in mac_exclusion_set and on_mac()
def _parser_args() -> argparse.Namespace:
desc = 'Build and test examples.'
parser = argparse.ArgumentParser(description=desc)
parser.add_argument(
'--only-examples', dest='regex_examples', type=regex, default='.*',
help="Regular Expression to filter which example to build and run"
)
parser.add_argument('--deinit', default=False,
action='store_true', dest='deinit',
help='Deinitialize the submodules, this will remove\
all unstaged changes')
c.add_args(parser)
args = parser.parse_args()
c.update_args(args)
return args
def run(args: argparse.Namespace) -> None:
examples = [
Genann(args),
Grabc(args),
Libxml2(args),
Lil(args),
Snudown(args),
TinyCC(args),
Tmux(args),
Urlparser(args),
Xzoom(args),
]
for example in examples:
if args.regex_examples.fullmatch(example.project_name) and\
not _is_excluded(example.project_name):
example.build_and_test()
print(Colors.OKGREEN + "Done building and testing the examples." +
Colors.NO_COLOR)
def main() -> None:
setup_logging()
args = _parser_args()
run(args)
if __name__ == "__main__":
main()