2014-08-07 16:14:27 +00:00
|
|
|
import glob
|
2014-09-27 11:03:51 +00:00
|
|
|
import os
|
2014-08-07 16:14:27 +00:00
|
|
|
import re
|
|
|
|
from collections import defaultdict
|
2014-09-27 11:03:51 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
header_path = "../include"
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
header_path = sys.argv[1]
|
|
|
|
|
2014-08-07 16:14:27 +00:00
|
|
|
OUTPUT = 'crow_all.h'
|
|
|
|
re_depends = re.compile('^#include "(.*)"', re.MULTILINE)
|
2014-09-27 11:03:51 +00:00
|
|
|
headers = [x.rsplit('/',1)[-1] for x in glob.glob(os.path.join(header_path,'*.h'))]
|
2014-08-07 16:14:27 +00:00
|
|
|
print headers
|
|
|
|
edges = defaultdict(list)
|
|
|
|
for header in headers:
|
2014-09-27 11:03:51 +00:00
|
|
|
d = open(os.path.join(header_path, header)).read().decode('utf8')
|
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 = []
|
|
|
|
def dfs(x):
|
|
|
|
visited[x] = True
|
|
|
|
for y in edges[x]:
|
|
|
|
if not visited[y]:
|
|
|
|
dfs(y)
|
|
|
|
order.append(x)
|
|
|
|
|
|
|
|
for header in headers:
|
|
|
|
if not visited[header]:
|
|
|
|
dfs(header)
|
|
|
|
|
|
|
|
order = order[::-1]
|
|
|
|
for x in edges:
|
|
|
|
print x, edges[x]
|
|
|
|
for x in edges:
|
|
|
|
for y in edges[x]:
|
|
|
|
assert order.index(x) < order.index(y), 'cyclic include detected'
|
|
|
|
|
|
|
|
print order
|
|
|
|
build = []
|
|
|
|
for header in order:
|
2014-09-27 11:03:51 +00:00
|
|
|
d = open(os.path.join(header_path, header)).read().decode('utf8')
|
2014-08-07 16:14:27 +00:00
|
|
|
build.append(re_depends.sub(lambda x:'\n', d))
|
|
|
|
build.append('\n')
|
|
|
|
|
|
|
|
open(OUTPUT,'w').write('\n'.join(build))
|
|
|
|
|