-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoxsnow_python_compatibler.py
239 lines (198 loc) · 8.58 KB
/
foxsnow_python_compatibler.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
#!/usr/bin/env python3
import errno
import json
import os
import pathlib as pt
import pprint
import subprocess as sp
import sys
def parse_c_header(source, defines={}, recursive=True):
c_std_lib = [
'assert.h', 'complex.h', 'ctype.h', 'errno.h',
'fenv.h', 'float.h', 'inttypes.h', 'iso646.h',
'limits.h', 'locale.h', 'math.h', 'setjmp.h',
'signal.h', 'stdalign.h', 'stdarg.h', 'stdatomic.h',
'stdbool.h', 'stddef.h', 'stdint.h', 'stdio.h',
'stdlib.h', 'stdnoreturn.h', 'string.h', 'tgmath.h',
'threads.h', 'time.h', 'uchar.h', 'wchar.h', 'wctype.h']
orig_wd = os.getcwd()
source = pt.Path(source).resolve()
if not source.exists():
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), source.as_posix())
os.chdir(source.parents[0])
result_dict = {
'keyword_defined' : {},
'include_list' : {
'lib':[],
'src':[]
}
}
if defines: result_dict['keyword_defined'].update(defines)
keyword_ifdef = []
file_lines = None
with open(source, 'r', encoding='utf-8') as fp:
file_lines = fp.readlines()
for index, line in enumerate(file_lines):
cur_line = line.strip()
if not cur_line.startswith('#'):
continue
else:
cur_line = cur_line[1:] # remove '#'
token = cur_line.strip().split(' ')
if not token: continue
# Preprocessor IF control
if token[0] == 'ifdef':
if len(token) < 2: raise Exception(f'ifdef line {index} too short')
keyword_ifdef.append((token[1], bool(token[1] in result_dict['keyword_defined'])))
elif token[0] == 'ifndef':
if len(token) < 2: raise Exception(f'ifndef line {index} too short')
keyword_ifdef.append((token[1], bool(not token[1] in result_dict['keyword_defined'])))
elif token[0] == 'endif':
# TODO : FIX HERE (try this code with only keyword_ifdef.pop() then you'll notice a problem.)
try:
keyword_ifdef.pop()
except:
pass
else:
# Preprocessor VAR control
if not keyword_ifdef or keyword_ifdef[-1][1]:
if token[0] == 'define':
if len(token) < 2: raise Exception(f'define line {index} too short')
result_dict['keyword_defined'][token[1]] = None if len(token) < 3 else ''.join(token[2:])
elif token[0] == 'include':
if len(token) < 2: raise Exception(f'include line {index} too short')
value = ''.join(token[1:])
if '<' in value:
result_dict['include_list']['lib'].append(value.replace('<', '').replace('>', ''))
else:
value = value.replace('"', '')
result_dict['include_list']['src'].append(pt.Path(value).resolve(strict=True))
if recursive:
for file in result_dict['include_list']['src']:
recursive_result = parse_c_header(file, result_dict['keyword_defined'], True)
result_dict['include_list']['lib'] += recursive_result['include_list']['lib']
result_dict['include_list']['src'] += recursive_result['include_list']['src']
result_dict['keyword_defined'].update(recursive_result['keyword_defined'])
os.chdir(orig_wd)
#remove duplicated items
result_dict['include_list']['lib'] = list(dict.fromkeys(result_dict['include_list']['lib']))
result_dict['include_list']['src'] = list(dict.fromkeys(result_dict['include_list']['src']))
#remove C standard library
result_dict['include_list']['lib'] = [z for z in result_dict['include_list']['lib']\
if z not in c_std_lib]
return result_dict
def get_target_src_files(target):
result = parse_c_header(target)
#include C files if exists
tmp_src_c_list = list()
for file in result['include_list']['src']:
target_c_src = file.with_suffix('.c')
if target_c_src.exists():
tmp_src_c_list.append(file.with_suffix('.c'))
result['include_list']['src'] += tmp_src_c_list
result['include_list']['src'].sort()
result['include_list']['src'].insert(0, pt.Path(target).resolve())
return result
def include_concat(filename):
result_txt = ''
src_target_list = get_target_src_files(filename)
for src_path in src_target_list['include_list']['src']:
if src_path.suffix == '.h':
result_txt += src_path.read_text()
result_txt += '\n'
return result_txt
def extract_typedef_struct_code(include_data):
struct_data = dict()
struct_parenthesis_stack = list()
enum_parenthesis_stack = list()
tmp_line_buffer = ''
tmp_line_buffer_enum = ''
for index, line in enumerate(include_data.splitlines()):
if 'typedef struct' in line:
struct_parenthesis_stack.append(line)
tmp_line_buffer += (line + '\n')
continue
elif line.startswith('enum'):
enum_parenthesis_stack.append(line)
tmp_line_buffer_enum += (line + '\n')
continue
elif line.startswith('}'):
if enum_parenthesis_stack:
enum_parenthesis_stack.pop()
# TODO : NEED TO PARSE ENUM!
continue
struct_name = line.replace('}', '').replace(';', '').strip()
if struct_name:
struct_data[struct_name] = tmp_line_buffer
tmp_line_buffer = ''
continue
elif struct_parenthesis_stack:
tmp_line_buffer += (line + '\n')
continue
return struct_data
def ctags_parse(project_dir):
orig_wd = pt.Path()
print(orig_wd.absolute())
os.chdir(project_dir)
ctags_proc = None
try:
ctags_proc = sp.run(['ctags', '-R', '--output-format=json'], capture_output=True, check=True, shell=True)
except Exception as e:
raise e
ctags_output = ctags_proc.stdout.decode()
ctags_output_corrected = ''
for line in ctags_output.splitlines():
ctags_output_corrected += f'{line},\n'
ctags_output_corrected = ctags_output_corrected[:-2]
ctags_output_corrected = '{"tagdata":[' + ctags_output_corrected + ']}'
try:
ctags_data = json.loads(ctags_output_corrected)['tagdata']
except json.JSONDecodeError as err:
# grab a reasonable section, say 40 characters.
start, stop = max(0, err.pos - 20), err.pos + 20
snippet = err.doc[start:stop]
print(err)
print('... ' if start else '', snippet, ' ...' if stop < len(err.doc) else '', sep="")
print('^'.rjust(21 if not start else 25))
raise err
function_list = [func_data.get('name', '') for func_data in ctags_data if func_data.get('kind', '') == 'function' and 'glew.c' not in func_data.get('path', '')]
struct_list = [struct_data.get('name', '') for struct_data in ctags_data if struct_data.get('kind', '') == 'typedef' and 'glew.c' not in struct_data.get('path', '')]
global_list = [global_data.get('name', '') for global_data in ctags_data if global_data.get('kind', '') == 'variable' and 'glew.c' not in global_data.get('path', '')]
os.chdir(orig_wd)
return {
'ctags_data': ctags_data,
'function_list': function_list,
'struct_list': struct_list,
'global_list': global_list,
}
def create_vs_def_file(ctags_data):
file_data = 'LIBRARY FOXSNOW\nEXPORTS\n'
for function_name in ctags_data['function_list']:
file_data += f'\t{function_name}\n'
for global_name in ctags_data['global_list']:
file_data += f'\t{global_name} DATA\n'
target_path = pt.Path('foxsnow_dll.def').absolute()
print(target_path)
if target_path.exists():
target_path.unlink()
with target_path.open('w') as fp:
fp.write(file_data)
def create_cffi_json_file(ctags_data, input_file):
# We need to include function and struct definition.
target_path = pt.Path('foxsnow_cffi_data.json').absolute()
print(target_path)
if target_path.exists():
target_path.unlink()
with target_path.open('w') as fp:
fp.write(file_data)
if __name__ == '__main__':
import pprint
if len(sys.argv) > 2:
target_main_src = ''.join(sys.argv[1:])
else:
target_main_src = './foxsnow'
# include_concat_data = include_concat(target_main_src)
# struct_data = extract_typedef_struct_code(include_concat_data)
# print(struct_data.keys())
parsed_data = ctags_parse(target_main_src)
result = create_vs_def_file(parsed_data)