Crow/scripts/merge_all.py

98 lines
3.1 KiB
Python
Raw Normal View History

#!/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
2020-11-12 18:54:49 +00:00
from os import path as pt, name as osname
from os.path import sep
2014-08-07 16:14:27 +00:00
import re
from collections import defaultdict
import sys, getopt
if len(sys.argv) < 3:
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")
sys.exit(1)
header_path = sys.argv[1]
output_path = sys.argv[2]
2020-11-12 18:54:49 +00:00
middlewares = [x.rsplit(sep, 1)[-1][:-2] for x in glob(pt.join(header_path, ('crow'+sep+'middlewares'+sep+'*.h*')))]
with open(header_path+'/../LICENSE', 'r') as file:
2021-07-10 18:48:42 +00:00
lsc = '/*' + file.read() + '*/'
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)
else:
middlewares_actual = middlewares
print("Middlewares: " + str(middlewares_actual))
re_depends = re.compile('^#include \"(.*)\"\n', re.MULTILINE)
re_pragma = re.compile('^(.*)#pragma once(.*)\n', re.MULTILINE)
2020-11-12 18:54:49 +00:00
headers = [x.rsplit(sep, 1)[-1] for x in glob(pt.join(header_path, '*.h*'))]
headers += ['crow'+sep + x.rsplit(sep, 1)[-1] for x in glob(pt.join(header_path, 'crow'+sep+'*.h*'))]
headers += [('crow'+sep+'middlewares'+sep + 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:
2022-08-12 11:58:21 +00:00
d = open(pt.join(header_path, header), encoding='UTF-8').read()
2014-08-07 16:14:27 +00:00
match = re_depends.findall(d)
for m in match:
2020-11-12 18:54:49 +00:00
actual_m = m
if (osname == 'nt'): #Windows
actual_m = m.replace('/', '\\')
2014-08-07 16:14:27 +00:00
# m should included before header
2020-11-12 18:54:49 +00:00
edges[actual_m].append(header)
2014-08-07 16:14:27 +00:00
visited = defaultdict(bool)
order = []
2015-07-30 14:59:54 +00:00
2020-11-12 18:54:49 +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)
build = [lsc, '#pragma once']
2014-08-07 16:14:27 +00:00
for header in order:
2022-08-12 11:58:21 +00:00
d = open(pt.join(header_path, header), encoding='UTF-8').read()
d_no_depend = re_depends.sub(lambda x: '', d)
d_no_pragma = re_pragma.sub(lambda x: '', d_no_depend)
build.append(d_no_pragma)
#build.append('\n')
2014-08-07 16:14:27 +00:00
2022-08-12 11:58:21 +00:00
open(output_path, 'w', encoding='UTF-8').write('\n'.join(build))