-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
proj-dep.py
142 lines (130 loc) · 3.42 KB
/
proj-dep.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
from os import listdir
from os import path
from os import system
import re
import getopt, sys
def pri_targets_mk(targets_file):
targets_li = []
files = listdir('./')
for d in files:
if path.isdir(d) and path.exists(d + '/Makefile'):
targets_li.append(d)
targets = ' '.join(targets_li)
print(targets)
def mk_dep_dict(targets_file):
dep_dict = dict()
targets_str = open(targets_file).read().strip()
targets_li = targets_str.split()
for target in targets_li:
dep_path = target + '/' + 'dep'
if not path.exists(target) or not path.exists(dep_path):
continue
files = listdir(dep_path)
regex = re.compile("dep-(.*)\.mk")
if len(files) > 0:
dep_dict[target] = list()
for name in files:
m = regex.match(name)
if m is not None:
dep = m.group(1)
if path.exists(dep):
dep_dict[target].append((dep, 'internal'))
else:
dep_dict[target].append((dep, 'external'))
return dep_dict
def check_dep(targets_file):
dd = mk_dep_dict(targets_file)
flag = 0
for module in dd:
for dep, dep_type in dd[module]:
if (dep_type == 'external'):
print("checking lib" + dep + ' path', end="")
print(' used by ' + module + '...')
res = system('make -C %s check-lib%s'
' > /dev/null' % (module, dep))
if res != 0:
flag = 1
print("\033[91m", end="")
print("linker cannot locate lib" + dep)
print("\033[0m", end="")
if flag == 0:
print("all libraries are located, type `make' to build.")
else:
print("\033[91m", end="")
print("some libraries are not found, building will fail.")
print("\033[0m", end="")
def pri_internal_dep(targets_file):
dd = mk_dep_dict(targets_file)
for module in dd:
dep_dict = dd[module]
if len(dep_dict) > 0:
print('%s-module: ' % (module), end="")
for dep, dep_type in dep_dict:
if (dep_type == 'internal'):
print('%s-module' % (dep), end=" ")
print("")
def pri_dep_dot(targets_file):
dd = mk_dep_dict(targets_file)
extern_dep_li = set()
# begin graph
print("digraph G {")
for module in dd:
print('\t"%s"' % (module))
dep_dict = dd[module]
for dep, dep_type in dep_dict:
if dep_type == 'external':
libdep = 'lib' + dep
print('\t"%s"[shape="box"]' % (libdep))
print('\t"%s" -> "%s"' % (module, libdep))
extern_dep_li.add(libdep)
else:
print('\t"%s" -> "%s"' % (module, dep))
# write rank constraint for external modules
print('\t{')
print('\t\trank = same;')
for libdep in extern_dep_li:
print('\t\t"%s"' % (libdep))
print('\t}')
# end of graph
print("}")
def help(arg0):
print('DESCRIPTION: generate project dependency .mk or .dot.' \
'\n' \
'SYNOPSIS:\n' \
'%s [-h | --help] ' \
'[--dot] ' \
'[--internal-dep]' \
'[--targets]' \
'[--check-dep]' \
'\n' \
% (arg0))
sys.exit(1)
def main():
config_targets_file = "targets.mk"
cmd = sys.argv[0]
args = sys.argv[1:]
try:
opts, args = getopt.getopt(sys.argv[1:], "h", [
'help', 'dot', 'internal-dep', 'targets', 'check-dep'
])
except:
help(cmd)
if len(opts) == 0:
help(cmd)
for opt, arg in opts:
if opt in ("-h", "--help"):
help(cmd)
if opt in ("--dot"):
pri_dep_dot(config_targets_file)
break
if opt in ("--internal-dep"):
pri_internal_dep(config_targets_file)
break
if opt in ("--targets"):
pri_targets_mk(config_targets_file)
break
if opt in ("--check-dep"):
check_dep(config_targets_file)
break
if __name__ == "__main__":
main()