forked from forrestthewoods/lib_fts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfts_amalgamate.py
230 lines (184 loc) · 7.39 KB
/
fts_amalgamate.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
# LICENSE
#
# This software is dual-licensed to the public domain and under the following
# license: you are granted a perpetual, irrevocable license to copy, modify,
# publish, and distribute this file as you see fit.
#
# VERSION
# 0.1.0 (2016-07-16) Initial release
#
# AUTHOR
# Forrest Smith
import os
import re
def fts_amalgamate(config, included_paths):
# Open output file
print 'Opening output file: {0}'.format(config['output'])
output_file = open(config['output'], 'w')
# Append amalgamation header
for line in config['amalgamation_header']:
output_file.write(line + '\n')
# Generate absolute paths for include filtering
exclude_paths = set()
for relpath in config['include_mask']:
exclude_paths.add(os.path.abspath(relpath))
# Append each input file
for relpath in config['input']:
fts_amalgamate_path(relpath, config, output_file, exclude_paths, included_paths)
# Append amalgamation footer
for line in config['amalgamation_footer']:
output_file.write(line + '\n')
# Save output file
print 'Saving output: {0}\n'.format(config['output'])
output_file.close()
def fts_amalgamate_path(relpath, config, output_file, exclude_paths, included_paths):
input_file = open(relpath, 'r')
input_file_abspath = os.path.abspath(relpath)
input_file_dir = os.path.dirname(input_file_abspath)
input_file_basename = os.path.basename(relpath)
# Only amalgamate a given path once
if input_file_abspath in included_paths:
return
included_paths.add(input_file_abspath)
print 'Appending input: {0}'.format(input_file_basename)
# Regex to match #includes. Used to exclude includdes for files being amalgamated
include_pattern = r'#\s*include.*([<"])(.*)[>"]'
# Get extra ops
extra_operations = None
for opsRelPath, ops in config['extra_operations'].items():
opsAbsPath = os.path.abspath(opsRelPath)
if opsAbsPath == input_file_abspath:
extra_operations = ops
break
# File Header
for line in config['file_header']:
output_file.write(line.format(relpath) + '\n')
# Extra file guard
if extra_operations and 'fileGuard' in extra_operations:
output_file.write(extra_operations['fileGuard'] + ' // (AMALGAMATION GUARD)\n\n')
# Data for commenting out code
remove_lines = None
remove_lines_cur = 0
remove_lines_len = -1
if extra_operations and 'remove_lines' in extra_operations:
remove_lines = extra_operations['remove_lines']
remove_lines_len = len(remove_lines)
# Input File
line_num = 0
for line in iter(input_file):
line_num += 1
# Comment out line if in range Begin/End block comment if line range is marked for removal
comment_out = (remove_lines_cur < remove_lines_len
and line_num >= remove_lines[remove_lines_cur]['begin']
and line_num <= remove_lines[remove_lines_cur]['end'])
# Exclude #include statements that include files being amalgamated
match = re.search(include_pattern, line)
exclude = False
if match:
include_type = match.group(1)
include_path = match.group(2)
if include_type == '\"':
include_abspath = os.path.abspath(input_file_dir + "\\" + include_path)
elif include_type == '<':
include_abspath = os.path.abspath(config['include_root'] + '\\' + include_path)
if include_abspath in exclude_paths:
exclude = True
if exclude or comment_out:
line = '// (AMALGAMATED) ' + line
if config['allow_nesting'] and exclude and not include_abspath in included_paths:
fts_amalgamate_path(include_abspath, config, output_file, exclude_paths, included_paths)
# Run any extra operations
if extra_operations and 'findReplace' in extra_operations:
for fr in extra_operations['findReplace']:
line = re.sub(fr['find'], fr['replace'], line)
# Write final line
output_file.write(line)
# Close extra file guard
if extra_operations and 'fileGuard' in extra_operations:
output_file.write('\n' + '#endif //' + extra_operations['fileGuard'] + ' (AMALGAMATION GUARD)')
# File Footer
for line in config['file_footer']:
output_file.write(line.format(relpath) + '\n')
def main():
# Header placed once at top of output file
amalgamation_header = [
'// The file was GENERATED by an amalgamation script.',
'// DO NOT EDIT BY HAND!!!\n\n\n',
]
amalgamation_footer = [
]
# Header placed before the contents of each input file
file_header = [
'// ----------------------------------------------------------------------------',
'// BEGIN FILE: {0}',
'//\n\n',
]
# Footer placed after the contents of each input file
file_footer = [
'\n\n//',
'// END FILE: {0}',
'// ----------------------------------------------------------------------------\n\n\n'
]
headers = [
'foo/a.h',
'foo/b.h',
'foo/c.h',
]
include_extra_operations = {
'foo/a.h' : {
'findReplace' : [
{ 'find': 'globalData', 'replace': 'globalDataA' },
],
},
'foo/b.h' : {
'findReplace' : [
{ 'find': 'globalData', 'replace': 'globalDataB' },
],
'remove_lines' : [
{ 'begin': 261, 'end': 302 },
],
},
'foo/c.h' : {
"fileGuard" : "#ifdef FOO_PLATFORM_WINDOWS"
}
}
source = [
'foo/a.c',
'foo/b.c',
'foo/c.c',
# Nested headers
'foo/c.h',
]
source_extra_operations = {
}
included_paths = set()
# Amalgamate header files
config = {}
config['amalgamation_header'] = list(amalgamation_header)
config['amalgamation_footer'] = list(amalgamation_footer)
config['file_header'] = file_header
config['file_footer'] = file_footer
config['input'] = headers
config['output'] = 'foo_amalg.h'
config['include_mask'] = headers
config['extra_operations'] = include_extra_operations
config['include_root'] = os.path.dirname(os.path.realpath(__file__))
config['allow_nesting'] = False
fts_amalgamate(config, included_paths)
# Amalgamate source files
config = {}
config['amalgamation_header'] = list(amalgamation_header)
config['amalgamation_footer'] = list(amalgamation_footer)
config['amalgamation_header'] += ['#include "foo_amalg.h"']
config['amalgamation_header'] += ['','','']
config['file_header'] = file_header
config['file_footer'] = file_footer
config['input'] = source
config['output'] = 'foo_amalg.c'
config['include_mask'] = headers + ['foo/c.h'] # Amalgamated headers plus nested headers
config['extra_operations'] = source_extra_operations
config['include_root'] = os.path.dirname(os.path.realpath(__file__))
config['allow_nesting'] = True
fts_amalgamate(config, included_paths)
if __name__ == '__main__':
main()