-
Notifications
You must be signed in to change notification settings - Fork 0
/
clblas_ext.py
executable file
·51 lines (40 loc) · 1.8 KB
/
clblas_ext.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
#!/usr/bin/env python3
import re
import sys
import argparse
enum_head_pattern = re.compile(r'typedef enum clblasStatus_ {')
enum_member_pattern = re.compile(r'\s*(\w+)\s*(?:=\s*(-?\d+))?,\s*(?:/\*\*<\s*((\S+\s+)*(\S+))\s*\*/)?')
enum_tail_pattern = re.compile(r'}\s*clblasStatus;')
inside_enum = False
enum_values = {}
def int_or_none(x):
if x == None:
return None
else:
return int(x)
if __name__ == '__main__':
parser = argparse.ArgumentParser(prog=sys.argv[0])
parser.add_argument('header', help='Location of clBLAS.h')
args = parser.parse_args()
with open(args.header) as header_file:
for line in header_file:
match_enum_member = re.match(enum_member_pattern, line)
if inside_enum and match_enum_member:
enum_values[match_enum_member.group(1)] = (int_or_none(match_enum_member.group(2)), match_enum_member.group(3))
if re.match(enum_tail_pattern, line):
inside_enum = False
break
if re.match(enum_head_pattern, line):
inside_enum = True;
with open('clblas_ext.h', 'w') as header_file:
print(f'// autogenerated from {sys.argv[0]}', file=header_file)
print('#ifndef CLBLAS_EXT_H\n#define CLBLAS_EXT_H', file=header_file)
print('static inline const char *clblasGetErrorString(clblasStatus status) {', file=header_file)
print('\tswitch (status) {', file=header_file)
for member,(val,desc) in enum_values.items():
print(f'\t\tcase {member}: return "{desc}";', file=header_file)
print('\t\tdefault: return "clblasUnknown";', file=header_file)
print('\t}', file=header_file)
print('}', file=header_file)
print('#endif // CLBLAS_EXT_H', file=header_file)
print('Done. Check clblas_ext.h')