-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonolithize.py
More file actions
87 lines (77 loc) · 2.71 KB
/
monolithize.py
File metadata and controls
87 lines (77 loc) · 2.71 KB
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
import sys
def load_file(file_path):
libs = []
lines = []
try:
with open(file_path, 'r') as content:
for line in content:
if line.startswith("#include") and ('<' not in line and '>' not in line):
continue
if line.startswith("#include"):
libs.append(line)
continue
lines.append(line)
except FileNotFoundError as _:
print("Warning, file not found: '{}'".format(file_path))
pass
return (libs, ''.join(lines))
def remove_unused(single_source):
lines = single_source.splitlines
main_start = 0
main_end = 0
for i, line in enumerate(lines):
if "int" in line and "main(" in line:
main_start = i
for i, line in enumerate(lines):
block_depth = -1
if line <= main_start:
if "{" in line:
block_depth += 1
if "}" in line:
block_depth -= 1
if block_depth == 0:
main_end = i
main_method = lines[main_start:main_end + 1]
#fuckit, i'm not gonna make it.
pass
def monolithize(hdc_header_path, output_path):
single_source = ""
std_libs = []
with open(hdc_header_path) as header:
for line in header:
line = str(line)
if line.startswith("#include") and ('<' not in line and '>' not in line):
h_path = './' + line.split(' ')[1].replace('"', '').strip()
c_path = h_path.replace('.h', '.c')
print("including header '{}' and corresponding source.".format(h_path))
(libs, code) = load_file(h_path)
std_libs += libs
single_source += code
(libs, code) = load_file(c_path)
std_libs += libs
single_source += code
single_source += '\n'
continue
if line.startswith("#include"):
std_libs.append(line)
continue
if line.startswith("#"):
continue
single_source += line
std_libs = reversed(list(set(std_libs)))
for lib in std_libs:
single_source = lib + '\n' + single_source
with open(output_path, 'w') as output:
output.write(single_source)
def main(argv):
if len(argv) != 3:
print("Usage of this program:\npython monolithize.py <hdc header path> <output path>")
return
hdc_header_path = argv[1]
output_path = argv[2]
print("Monolithizing code: '{}' to '{}'.".format(hdc_header_path, output_path))
monolithize(hdc_header_path, output_path)
print("Done.")
if __name__ == "__main__":
main(sys.argv)
pass