forked from mozilla/cbindgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
executable file
·106 lines (82 loc) · 2.29 KB
/
test.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
#!/bin/python
import os
import glob
import subprocess
import sys
def build_cbindgen():
try:
subprocess.check_output(["cargo", "build"])
return True
except subprocess.CalledProcessError:
return False
def cbindgen(rust_src, out, c, config):
bin = ["target/debug/cbindgen"]
compile = [rust_src, "-o", out]
flags = []
if c:
flags += ["--lang", "c"]
if config != None:
flags += ["--config", config]
command = bin + flags + compile
print(command)
subprocess.check_output(bin + flags + compile)
def gcc(src):
gcc_bin = os.environ.get('CC')
if gcc_bin == None:
gcc_bin = 'gcc'
subprocess.check_output([gcc_bin, "-D", "DEFINED", "-c", src, "-o", "compile-tests/tmp.o"])
os.remove("compile-tests/tmp.o")
def gxx(src):
gxx_bin = os.environ.get('CXX')
if gxx_bin == None:
gxx_bin = 'g++'
subprocess.check_output([gxx_bin, "-D", "DEFINED", "-std=c++11", "-c", src, "-o", "compile-tests/tmp.o"])
os.remove("compile-tests/tmp.o")
def run_compile_test(rust_src, leave_output, c):
if c:
out = rust_src.replace(".rs", ".c")
else:
out = rust_src.replace(".rs", ".cpp")
config = rust_src.replace(".rs", ".toml")
if not os.path.exists(config):
config = None
try:
cbindgen(rust_src, out, c, config)
if c:
gcc(out)
else:
gxx(out)
if not leave_output:
os.remove(out)
except subprocess.CalledProcessError:
if not leave_output and os.path.exists(out):
os.remove(out)
return False
return True
if not build_cbindgen():
exit()
args = sys.argv[1:]
files = [x for x in args if not x.startswith("-")]
flags = [x for x in args if x.startswith("-")]
leave_output = False
c = False
for flag in flags:
if flag == "-l":
leave_output = True
elif flag == "-c":
c = True
tests = []
if len(files) == 0:
tests = glob.glob("compile-tests/*.rs")
else:
tests = files
num_pass = 0
num_fail = 0
for test in tests:
if run_compile_test(test, leave_output, c):
num_pass += 1
print("Pass - %s" % test)
else:
num_fail += 1
print("Fail - %s" % test)
print("Tests complete. %i passed, %i failed." % (num_pass, num_fail))