blob: 5eb3e12769a6541ffeb3a47e735da1f49dda3e66 [file] [log] [blame]
Brad Bishop316dfdd2018-06-25 12:45:53 -04001#!/usr/bin/env python3
2#
3# Copyright (C) 2018 Wind River Systems, Inc.
4#
Brad Bishopc342db32019-05-15 21:57:59 -04005# SPDX-License-Identifier: GPL-2.0-only
Brad Bishop316dfdd2018-06-25 12:45:53 -04006#
Brad Bishop316dfdd2018-06-25 12:45:53 -04007
8import os
9import sys
10import argparse
11import logging
12import re
13
14class Dot(object):
15 def __init__(self):
16 parser = argparse.ArgumentParser(
17 description="Analyse recipe-depends.dot generated by bitbake -g",
18 epilog="Use %(prog)s --help to get help")
19 parser.add_argument("dotfile",
20 help = "Specify the dotfile", nargs = 1, action='store', default='')
21 parser.add_argument("-k", "--key",
22 help = "Specify the key, e.g., recipe name",
23 action="store", default='')
24 parser.add_argument("-d", "--depends",
25 help = "Print the key's dependencies",
26 action="store_true", default=False)
27 parser.add_argument("-w", "--why",
28 help = "Print why the key is built",
29 action="store_true", default=False)
30 parser.add_argument("-r", "--remove",
31 help = "Remove duplicated dependencies to reduce the size of the dot files."
32 " For example, A->B, B->C, A->C, then A->C can be removed.",
33 action="store_true", default=False)
34
35 self.args = parser.parse_args()
36
37 if len(sys.argv) != 3 and len(sys.argv) < 5:
38 print('ERROR: Not enough args, see --help for usage')
39
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080040 @staticmethod
41 def insert_dep_chain(chain, rdeps, alldeps):
42 """
43 insert elements to chain from rdeps, according to alldeps
44 """
45 # chain should at least contain one element
46 if len(chain) == 0:
47 raise
48
49 inserted_elements = []
50 for rdep in rdeps:
51 if rdep in chain:
52 continue
53 else:
54 for i in range(0, len(chain)-1):
55 if chain[i] in alldeps[rdep] and rdep in alldeps[chain[i+1]]:
56 chain.insert(i+1, rdep)
57 inserted_elements.append(rdep)
58 break
59 if chain[-1] in alldeps[rdep] and rdep not in chain:
60 chain.append(rdep)
61 inserted_elements.append(rdep)
62 return inserted_elements
63
64 @staticmethod
65 def print_dep_chains(key, rdeps, alldeps):
66 rlist = rdeps.copy()
67 chain = []
68 removed_rdeps = [] # hold rdeps removed from rlist
69
70 chain.append(key)
71 while (len(rlist) != 0):
72 # insert chain from rlist
73 inserted_elements = Dot.insert_dep_chain(chain, rlist, alldeps)
74 if not inserted_elements:
75 if chain[-1] in rlist:
76 rlist.remove(chain[-1])
77 removed_rdeps.append(chain[-1])
78 chain.pop()
79 continue
80 else:
81 # insert chain from removed_rdeps
82 Dot.insert_dep_chain(chain, removed_rdeps, alldeps)
83 print(' -> '.join(list(reversed(chain))))
84
Brad Bishop316dfdd2018-06-25 12:45:53 -040085 def main(self):
86 #print(self.args.dotfile[0])
87 # The format is {key: depends}
88 depends = {}
89 with open(self.args.dotfile[0], 'r') as f:
90 for line in f.readlines():
91 if ' -> ' not in line:
92 continue
93 line_no_quotes = line.replace('"', '')
94 m = re.match("(.*) -> (.*)", line_no_quotes)
95 if not m:
96 print('WARNING: Found unexpected line: %s' % line)
97 continue
98 key = m.group(1)
99 if key == "meta-world-pkgdata":
100 continue
101 dep = m.group(2)
102 if key in depends:
103 if not key in depends[key]:
104 depends[key].add(dep)
105 else:
106 print('WARNING: Fonud duplicated line: %s' % line)
107 else:
108 depends[key] = set()
109 depends[key].add(dep)
110
111 if self.args.remove:
112 reduced_depends = {}
113 for k, deps in depends.items():
114 child_deps = set()
115 added = set()
116 # Both direct and indirect depends are already in the dict, so
117 # we don't have to do this recursively.
118 for dep in deps:
119 if dep in depends:
120 child_deps |= depends[dep]
121
122 reduced_depends[k] = deps - child_deps
123 outfile= '%s-reduced%s' % (self.args.dotfile[0][:-4], self.args.dotfile[0][-4:])
124 with open(outfile, 'w') as f:
125 print('Saving reduced dot file to %s' % outfile)
126 f.write('digraph depends {\n')
127 for k, v in reduced_depends.items():
128 for dep in v:
129 f.write('"%s" -> "%s"\n' % (k, dep))
130 f.write('}\n')
131 sys.exit(0)
132
133 if self.args.key not in depends:
134 print("ERROR: Can't find key %s in %s" % (self.args.key, self.args.dotfile[0]))
135 sys.exit(1)
136
137 if self.args.depends:
138 if self.args.key in depends:
139 print('Depends: %s' % ' '.join(depends[self.args.key]))
140
141 reverse_deps = []
142 if self.args.why:
143 for k, v in depends.items():
144 if self.args.key in v and not k in reverse_deps:
145 reverse_deps.append(k)
146 print('Because: %s' % ' '.join(reverse_deps))
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800147 Dot.print_dep_chains(self.args.key, reverse_deps, depends)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400148
149if __name__ == "__main__":
150 try:
151 dot = Dot()
152 ret = dot.main()
153 except Exception as esc:
154 ret = 1
155 import traceback
156 traceback.print_exc()
157 sys.exit(ret)