2020-10-31 18:22:34 +00:00
|
|
|
#!/usr/bin/env python3
|
2019-11-05 19:23:29 +00:00
|
|
|
|
2015-07-30 14:59:54 +00:00
|
|
|
"""Merges all the header files."""
|
|
|
|
from glob import glob
|
|
|
|
from os import path as pt
|
2014-08-07 16:14:27 +00:00
|
|
|
import re
|
|
|
|
from collections import defaultdict
|
2020-11-11 02:39:57 +00:00
|
|
|
import sys, getopt
|
2014-09-27 11:03:51 +00:00
|
|
|
|
2020-11-11 02:39:57 +00:00
|
|
|
if len(sys.argv) < 3:
|
2020-11-11 21:14:36 +00:00
|
|
|
print("Usage: {} <CROW_HEADERS_DIRECTORY_PATH> <CROW_OUTPUT_HEADER_PATH> (-i(include) OR -e(exclude) item1,item2...)".format(sys.argv[0]))
|
|
|
|
print("Available middlewares are in `include/crow/middlewares`. Do NOT type the `.h` when including or excluding")
|
2019-11-05 19:35:16 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
header_path = sys.argv[1]
|
|
|
|
output_path = sys.argv[2]
|
2014-09-27 11:03:51 +00:00
|
|
|
|
2020-11-11 20:43:57 +00:00
|
|
|
middlewares = [x.rsplit('/', 1)[-1][:-2] for x in glob(pt.join(header_path, 'crow/middlewares/*.h*'))]
|
2020-11-11 02:39:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
middlewares_actual = []
|
|
|
|
if len(sys.argv) > 3:
|
|
|
|
opts, args = getopt.getopt(sys.argv[3:],"i:e:",["include=","exclude="])
|
|
|
|
if (len(opts) > 1):
|
|
|
|
print("Error:Cannot simultaneously include and exclude middlewares.")
|
|
|
|
sys.exit(1)
|
|
|
|
if (opts[0][0] in ("-i", "--include")):
|
|
|
|
middleware_list = opts[0][1].split(',')
|
|
|
|
for item in middlewares:
|
|
|
|
if (item in middleware_list):
|
|
|
|
middlewares_actual.append(item)
|
|
|
|
elif (opts[0][0] in ("-e", "--exclude")):
|
|
|
|
middleware_list = opts[0][1].split(',')
|
|
|
|
for item in middlewares:
|
|
|
|
if (item not in middleware_list):
|
|
|
|
middlewares_actual.append(item)
|
|
|
|
else:
|
|
|
|
print("ERROR:Unknown argument " + opts[0][0])
|
|
|
|
sys.exit(1)
|
2020-11-11 20:43:57 +00:00
|
|
|
else:
|
|
|
|
middlewares_actual = middlewares
|
2020-11-11 02:39:57 +00:00
|
|
|
print("Middlewares: " + str(middlewares_actual))
|
|
|
|
|
2014-08-07 16:14:27 +00:00
|
|
|
re_depends = re.compile('^#include "(.*)"', re.MULTILINE)
|
2016-08-28 05:46:44 +00:00
|
|
|
headers = [x.rsplit('/', 1)[-1] for x in glob(pt.join(header_path, '*.h*'))]
|
2016-09-21 14:17:29 +00:00
|
|
|
headers += ['crow/' + x.rsplit('/', 1)[-1] for x in glob(pt.join(header_path, 'crow/*.h*'))]
|
2020-11-11 20:43:57 +00:00
|
|
|
headers += [('crow/middlewares/' + x + '.h') for x in middlewares_actual]
|
2015-07-30 14:59:54 +00:00
|
|
|
print(headers)
|
2014-08-07 16:14:27 +00:00
|
|
|
edges = defaultdict(list)
|
|
|
|
for header in headers:
|
2015-07-30 15:05:57 +00:00
|
|
|
d = open(pt.join(header_path, header)).read()
|
2014-08-07 16:14:27 +00:00
|
|
|
match = re_depends.findall(d)
|
|
|
|
for m in match:
|
|
|
|
# m should included before header
|
|
|
|
edges[m].append(header)
|
|
|
|
|
|
|
|
visited = defaultdict(bool)
|
|
|
|
order = []
|
2015-07-30 14:59:54 +00:00
|
|
|
|
|
|
|
|
2014-08-07 16:14:27 +00:00
|
|
|
def dfs(x):
|
2015-07-30 14:59:54 +00:00
|
|
|
"""Ensure all header files are visited."""
|
2014-08-07 16:14:27 +00:00
|
|
|
visited[x] = True
|
|
|
|
for y in edges[x]:
|
|
|
|
if not visited[y]:
|
|
|
|
dfs(y)
|
|
|
|
order.append(x)
|
2015-07-30 14:59:54 +00:00
|
|
|
|
2014-08-07 16:14:27 +00:00
|
|
|
for header in headers:
|
|
|
|
if not visited[header]:
|
|
|
|
dfs(header)
|
|
|
|
|
|
|
|
order = order[::-1]
|
|
|
|
for x in edges:
|
2015-07-30 14:59:54 +00:00
|
|
|
print(x, edges[x])
|
2014-08-07 16:14:27 +00:00
|
|
|
for x in edges:
|
|
|
|
for y in edges[x]:
|
|
|
|
assert order.index(x) < order.index(y), 'cyclic include detected'
|
|
|
|
|
2015-07-30 14:59:54 +00:00
|
|
|
print(order)
|
2014-08-07 16:14:27 +00:00
|
|
|
build = []
|
|
|
|
for header in order:
|
2015-07-30 15:05:57 +00:00
|
|
|
d = open(pt.join(header_path, header)).read()
|
2015-07-30 14:59:54 +00:00
|
|
|
build.append(re_depends.sub(lambda x: '\n', d))
|
2014-08-07 16:14:27 +00:00
|
|
|
build.append('\n')
|
|
|
|
|
2019-11-05 19:35:16 +00:00
|
|
|
open(output_path, 'w').write('\n'.join(build))
|