forked from xenia-project/FFmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_premake.py
333 lines (255 loc) · 10.5 KB
/
generate_premake.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import os
import re
from typing import Dict
from builtins import filter
MAKEFILE_FILE_NAME = 'Makefile'
# Makefile variables that contain source files - conditionals from arch.mak
file_blocks = [
('HEADERS', None),
('ARCH_HEADERS', None),
('BUILT_HEADERS', None),
('OBJS', None),
('ARMV5TE-OBJS', 'HAVE_ARMV5TE'),
('ARMV6-OBJS', 'HAVE_ARMV6'),
('ARMV8-OBJS', 'HAVE_ARMV8'),
('VFP-OBJS', 'HAVE_VFP'),
('NEON-OBJS', 'HAVE_NEON'),
('MMX-OBJS', 'HAVE_MMX'),
('X86ASM-OBJS', 'HAVE_X86ASM'),
]
template_header = """
-----------------------------------------------------------------------
---- Automatically generated by generate_premake.py. Do not edit ! ----
-----------------------------------------------------------------------
project("{}")
uuid("{}")
kind("StaticLib")
language("C")
ffmpeg_common()
filter("files:not wmaprodec.c")
warnings "Off"
"""
class FileListBuilder:
def __init__(self, architecture_name: str, filter_name: str = ""):
self.filter_name: str = filter_name
self.architecture_name: str = architecture_name # -- libavutil/Makefile:
self.files_by_block_type: Dict[str, list] = {} # For example: OBJS: "adler32.c"
def add_file_to_block(self, block_type: str, file_path: str):
if not self.files_by_block_type.get(block_type):
self.files_by_block_type[block_type] = list()
self.files_by_block_type[block_type].append(file_path)
def __hash__(self):
return self.files_by_block_type.__hash__
def __eq__(self, other):
return self.files_by_block_type == other.files_by_block_type
def build(self):
output_str = ' -- {}:\n'.format(self.architecture_name)
for block_type in self.files_by_block_type:
output_str += ' -- {}:\n'.format(block_type)
if self.filter_name:
output_str += "\n filter({{ \n \"{}\" \n }})\n".format(self.filter_name)
output_str += ' files({\n'
for file_name in self.files_by_block_type[block_type]:
output_str += " \"{}\",\n".format(file_name)
output_str += ' })\n'
return output_str
class PremakeFileBuilder:
def __init__(self, project_name: str, uuid: str):
self.template: str = template_header
self.project_name: str = project_name
self.uuid: str = uuid
self.files: list[FileListBuilder] = []
self.links: list = []
self.output: str = ""
def build(self) -> str:
self.output = template_header.format(self.project_name, self.uuid)
if self.links:
links_str = "\n ".join(f'"{link}",' for link in self.links)
self.output += "\n links({{ \n {} \n }})".format(links_str)
self.output += '\n\n'
for files_builder in self.files:
self.output += files_builder.build()
return self.output
#def add_files(self, file_list: FileListBuilder):
# self.files.append(file_list)
# return self
def add_link(self, link_name: str):
self.links.append(link_name)
return self
def cleanup_duplicates(self):
new_stuff = []
for entry in self.files:
if entry not in new_stuff:
new_stuff.append(entry)
self.files = new_stuff
class Config:
def __init__(self, os_type, architecture_type, config_h, premake_filters):
self.os_type = os_type
self.architecture_type = architecture_type
self.config_header_file_name = config_h
self.premake_filters = premake_filters
self.key_values = []
# gets the config defines from the generated header
def parse_config(file_path: str):
config: Dict[str, str] = {}
# platform configs mostly differ in HAVE_*, which we are not interested in
with open(file_path, 'r') as file:
for line in file:
split = line.rstrip().split(' ', 2)
if len(split) != 3 or split[0] != '#define':
continue
config[split[1]] = split[2].strip('"')
return config
def parse_configs(configs):
for config in configs:
config.key_values = parse_config(config.config_header_file_name)
# Adapted from distutils.sysconfig.parse_makefile
# Regexes needed for parsing Makefile (and similar syntaxes,
# like old-style Setup files).
_variable_rx = re.compile(r"([a-zA-Z][a-zA-Z0-9\-_]+)\s*(\+?)=\s*(.*)")
_variable_conditional_rx = re.compile(r"([a-zA-Z][a-zA-Z0-9\-_]+)-\$\(([a-zA-Z][a-zA-Z0-9\-_]+)\)\s*(\+?)=\s*(.*)")
_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9\-_]*)\)")
_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9\-_]*)}")
def parse_makefile(fn, conf, g=None):
"""Parse a Makefile-style file.
A dictionary containing name/value pairs is returned. If an
optional dictionary is passed in as the second argument, it is
used instead of a new dictionary.
"""
from distutils.text_file import TextFile
fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape")
if g is None:
g = {}
done = {}
notdone = {}
while True:
line = fp.readline()
if line is None: # eof
break
n = cond = app = v = None
m = _variable_rx.match(line)
if m:
n, app, v = m.group(1, 2, 3)
else:
m = _variable_conditional_rx.match(line)
if m:
n, cond, app, v = m.group(1, 2, 3, 4)
if v:
v = v.strip()
# `$$' is a literal `$' in make
tmpv = v.replace('$$', '')
if cond:
boolean = cond in conf and conf[cond]
n += ('-yes' if boolean else '-no')
if "$" in tmpv:
notdone[n] = v
else:
v = v.replace('$$', '$')
if app:
if n not in done:
done[n] = v
else:
done[n] += ' ' + v
else:
done[n] = v
# hacky. just assume they were all +=
notdone_done = {}
# do variable interpolation here
while notdone:
for name in list(notdone):
value = notdone[name]
m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
if m:
n = m.group(1)
found = True
if n in done:
item = str(done[n])
elif n in notdone:
# get it on a subsequent round
found = False
elif n in os.environ:
# do it like make: fall back to environment
item = os.environ[n]
else:
notdone_done[n] = item = ""
if found:
after = value[m.end():]
value = value[:m.start()] + item + after
if "$" in after:
notdone[name] = value
else:
notdone_done[name] = value
del notdone[name]
else:
# bogus variable reference; just drop it since we can't deal
del notdone[name]
for k in notdone_done:
if k in done:
done[k] += ' ' + notdone_done[k]
else:
done[k] = notdone_done[k]
fp.close()
# strip spurious spaces
for k, v in done.items():
if isinstance(v, str):
done[k] = v.strip()
# save the results in the global dictionary
g.update(done)
return g
def is_header_in_fileblocks(header: str):
for file_block in file_blocks:
if header == file_block[0]:
return True
return False
def parse_makefiles_and_create_filters(configs: list[Config], lib_premake: PremakeFileBuilder):
makefiles = {
os.path.join(lib_premake.project_name, MAKEFILE_FILE_NAME): configs,
# Original Makefiles are always included but since symbols are never used we can ignore them:
os.path.join(lib_premake.project_name, 'aarch64', MAKEFILE_FILE_NAME):
list(filter(lambda config: config.architecture_type == 'aarch64', configs)),
os.path.join(lib_premake.project_name, 'x86', MAKEFILE_FILE_NAME):
list(filter(lambda config: config.architecture_type == 'x86_64', configs))
}
# Cache tree of parsed makefile variables
for makefile_path in makefiles:
if not os.path.exists(makefile_path):
continue
for config in configs:
current_arch_files = FileListBuilder(makefile_path, config.premake_filters)
makefile = parse_makefile(makefile_path, config.key_values)
for header in makefile:
if not is_header_in_fileblocks(header):
continue
filenames = ' '.join(makefile[header].split(" ")).split()
# TODO: Somehow handle this exception better
# We have to convert .o files to .c files
if header == "OBJS":
filenames = {filename.replace('.o', '.c') for filename in filenames}
# Sort items
sorted(filenames)
for file_name in filenames:
current_arch_files.add_file_to_block(header, file_name)
#current_arch_files.cleanup_duplicates()
lib_premake.files.append(current_arch_files)
def generate_premake_file(lib_premake: PremakeFileBuilder):
premake_file_path = os.path.join(lib_premake.project_name, 'premake5.lua')
with open(premake_file_path, 'w') as premake:
premake.write(lib_premake.build())
if __name__ == '__main__':
configs = [
Config('windows', 'x86_64', 'config_windows_x86_64.h', 'platforms:Windows'),
Config('linux', 'x86_64', 'config_linux_x86_64.h', 'platforms:Linux'),
Config('android', 'x86_64', 'config_android_x86_64.h', 'platforms:Android-x86_64'),
Config('android', 'aarch64', 'config_android_aarch64.h', 'platforms:Android-ARM64'),
]
parse_configs(configs)
lib_premake_to_generate = [
#PremakeFileBuilder('libavutil', '19216035-F781-4F15-B009-213B7E3A18AC'),
PremakeFileBuilder('libavcodec', '9DB2830C-D326-48ED-B4CC-08EA6A1B7272').add_link("libavutil"),
#PremakeFileBuilder('libavformat', 'CF5EF84C-894E-4D89-9D12-1F5ADE8CEB9E')
]
for lib_premake in lib_premake_to_generate:
parse_makefiles_and_create_filters(configs, lib_premake)
# TODO: get_files_from_makefile
lib_premake.cleanup_duplicates()
generate_premake_file(lib_premake)