c26e1e06dc5c10bef0318af1eeb7d30063915a03
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / buildroot-2015.05 / support / scripts / graph-depends
1 #!/usr/bin/python
2
3 # Usage (the graphviz package must be installed in your distribution)
4 #  ./support/scripts/graph-depends [-p package-name] > test.dot
5 #  dot -Tpdf test.dot -o test.pdf
6 #
7 # With no arguments, graph-depends will draw a complete graph of
8 # dependencies for the current configuration.
9 # If '-p <package-name>' is specified, graph-depends will draw a graph
10 # of dependencies for the given package name.
11 # If '-d <depth>' is specified, graph-depends will limit the depth of
12 # the dependency graph to 'depth' levels.
13 #
14 # Limitations
15 #
16 #  * Some packages have dependencies that depend on the Buildroot
17 #    configuration. For example, many packages have a dependency on
18 #    openssl if openssl has been enabled. This tool will graph the
19 #    dependencies as they are with the current Buildroot
20 #    configuration.
21 #
22 # Copyright (C) 2010-2013 Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
23
24 import sys
25 import subprocess
26 import argparse
27 from fnmatch import fnmatch
28
29 # Modes of operation:
30 MODE_FULL = 1   # draw full dependency graph for all selected packages
31 MODE_PKG  = 2   # draw dependency graph for a given package
32 mode = 0
33
34 # Limit drawing the dependency graph to this depth. 0 means 'no limit'.
35 max_depth = 0
36
37 # Whether to draw the transitive dependencies
38 transitive = True
39
40 parser = argparse.ArgumentParser(description="Graph packages dependencies")
41 parser.add_argument("--package", '-p', metavar="PACKAGE",
42                     help="Graph the dependencies of PACKAGE")
43 parser.add_argument("--depth", '-d', metavar="DEPTH", dest="depth", type=int, default=0,
44                     help="Limit the dependency graph to DEPTH levels; 0 means no limit.")
45 parser.add_argument("--stop-on", "-s", metavar="PACKAGE", dest="stop_list", action="append",
46                     help="Do not graph past this package (can be given multiple times)." \
47                        + " Can be a package name or a glob, or" \
48                        + " 'virtual' to stop on virtual packages.")
49 parser.add_argument("--exclude", "-x", metavar="PACKAGE", dest="exclude_list", action="append",
50                     help="Like --stop-on, but do not add PACKAGE to the graph.")
51 parser.add_argument("--colours", "-c", metavar="COLOR_LIST", dest="colours",
52                     default="lightblue,grey,gainsboro",
53                     help="Comma-separated list of the three colours to use" \
54                        + " to draw the top-level package, the target" \
55                        + " packages, and the host packages, in this order." \
56                        + " Defaults to: 'lightblue,grey,gainsboro'")
57 parser.add_argument("--transitive", dest="transitive", action='store_true',
58                     default=False)
59 parser.add_argument("--no-transitive", dest="transitive", action='store_false',
60                     help="Draw (do not draw) transitive dependencies")
61 args = parser.parse_args()
62
63 if args.package is None:
64     mode = MODE_FULL
65 else:
66     mode = MODE_PKG
67     rootpkg = args.package
68
69 max_depth = args.depth
70
71 if args.stop_list is None:
72     stop_list = []
73 else:
74     stop_list = args.stop_list
75
76 if args.exclude_list is None:
77     exclude_list = []
78 else:
79     exclude_list = args.exclude_list
80
81 transitive = args.transitive
82
83 # Get the colours: we need exactly three colours,
84 # so no need not split more than 4
85 # We'll let 'dot' validate the colours...
86 colours = args.colours.split(',',4)
87 if len(colours) != 3:
88     sys.stderr.write("Error: incorrect colour list '%s'\n" % args.colours)
89     sys.exit(1)
90 root_colour = colours[0]
91 target_colour = colours[1]
92 host_colour = colours[2]
93
94 allpkgs = []
95
96 # Execute the "make <pkg>-show-version" command to get the version of a given
97 # list of packages, and return the version formatted as a Python dictionary.
98 def get_version(pkgs):
99     sys.stderr.write("Getting version for %s\n" % pkgs)
100     cmd = ["make", "-s", "--no-print-directory" ]
101     for pkg in pkgs:
102         cmd.append("%s-show-version" % pkg)
103     p = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
104     output = p.communicate()[0]
105     if p.returncode != 0:
106         sys.stderr.write("Error getting version %s\n" % pkgs)
107         sys.exit(1)
108     output = output.split("\n")
109     if len(output) != len(pkgs) + 1:
110         sys.stderr.write("Error getting version\n")
111         sys.exit(1)
112     version = {}
113     for i in range(0, len(pkgs)):
114         pkg = pkgs[i]
115         version[pkg] = output[i]
116     return version
117
118 # Execute the "make show-targets" command to get the list of the main
119 # Buildroot PACKAGES and return it formatted as a Python list. This
120 # list is used as the starting point for full dependency graphs
121 def get_targets():
122     sys.stderr.write("Getting targets\n")
123     cmd = ["make", "-s", "--no-print-directory", "show-targets"]
124     p = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
125     output = p.communicate()[0].strip()
126     if p.returncode != 0:
127         return None
128     if output == '':
129         return []
130     return output.split(' ')
131
132 # Execute the "make <pkg>-show-depends" command to get the list of
133 # dependencies of a given list of packages, and return the list of
134 # dependencies formatted as a Python dictionary.
135 def get_depends(pkgs):
136     sys.stderr.write("Getting dependencies for %s\n" % pkgs)
137     cmd = ["make", "-s", "--no-print-directory" ]
138     for pkg in pkgs:
139         cmd.append("%s-show-depends" % pkg)
140     p = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
141     output = p.communicate()[0]
142     if p.returncode != 0:
143         sys.stderr.write("Error getting dependencies %s\n" % pkgs)
144         sys.exit(1)
145     output = output.split("\n")
146     if len(output) != len(pkgs) + 1:
147         sys.stderr.write("Error getting dependencies\n")
148         sys.exit(1)
149     deps = {}
150     for i in range(0, len(pkgs)):
151         pkg = pkgs[i]
152         pkg_deps = output[i].split(" ")
153         if pkg_deps == ['']:
154             deps[pkg] = []
155         else:
156             deps[pkg] = pkg_deps
157     return deps
158
159 # Recursive function that builds the tree of dependencies for a given
160 # list of packages. The dependencies are built in a list called
161 # 'dependencies', which contains tuples of the form (pkg1 ->
162 # pkg2_on_which_pkg1_depends, pkg3 -> pkg4_on_which_pkg3_depends) and
163 # the function finally returns this list.
164 def get_all_depends(pkgs):
165     dependencies = []
166
167     # Filter the packages for which we already have the dependencies
168     filtered_pkgs = []
169     for pkg in pkgs:
170         if pkg in allpkgs:
171             continue
172         filtered_pkgs.append(pkg)
173         allpkgs.append(pkg)
174
175     if len(filtered_pkgs) == 0:
176         return []
177
178     depends = get_depends(filtered_pkgs)
179
180     deps = set()
181     for pkg in filtered_pkgs:
182         pkg_deps = depends[pkg]
183
184         # This package has no dependency.
185         if pkg_deps == []:
186             continue
187
188         # Add dependencies to the list of dependencies
189         for dep in pkg_deps:
190             dependencies.append((pkg, dep))
191             deps.add(dep)
192
193     if len(deps) != 0:
194         newdeps = get_all_depends(deps)
195         if newdeps is not None:
196             dependencies += newdeps
197
198     return dependencies
199
200 # The Graphviz "dot" utility doesn't like dashes in node names. So for
201 # node names, we strip all dashes.
202 def pkg_node_name(pkg):
203     return pkg.replace("-","")
204
205 TARGET_EXCEPTIONS = [
206     "target-finalize",
207     "target-post-image",
208 ]
209
210 # In full mode, start with the result of get_targets() to get the main
211 # targets and then use get_all_depends() for all targets
212 if mode == MODE_FULL:
213     targets = get_targets()
214     dependencies = []
215     allpkgs.append('all')
216     filtered_targets = []
217     for tg in targets:
218         # Skip uninteresting targets
219         if tg in TARGET_EXCEPTIONS:
220             continue
221         dependencies.append(('all', tg))
222         filtered_targets.append(tg)
223     deps = get_all_depends(filtered_targets)
224     if deps is not None:
225         dependencies += deps
226     rootpkg = 'all'
227
228 # In pkg mode, start directly with get_all_depends() on the requested
229 # package
230 elif mode == MODE_PKG:
231     dependencies = get_all_depends([rootpkg])
232
233 # Make the dependencies a dictionnary { 'pkg':[dep1, dep2, ...] }
234 dict_deps = {}
235 for dep in dependencies:
236     if dep[0] not in dict_deps:
237         dict_deps[dep[0]] = []
238     dict_deps[dep[0]].append(dep[1])
239
240 # This function return True if pkg is a dependency (direct or
241 # transitive) of pkg2, dependencies being listed in the deps
242 # dictionary. Returns False otherwise.
243 def is_dep(pkg,pkg2,deps):
244     if pkg2 in deps:
245         for p in deps[pkg2]:
246             if pkg == p:
247                 return True
248             if is_dep(pkg,p,deps):
249                 return True
250     return False
251
252 # This function eliminates transitive dependencies; for example, given
253 # these dependency chain: A->{B,C} and B->{C}, the A->{C} dependency is
254 # already covered by B->{C}, so C is a transitive dependency of A, via B.
255 # The functions does:
256 #   - for each dependency d[i] of the package pkg
257 #     - if d[i] is a dependency of any of the other dependencies d[j]
258 #       - do not keep d[i]
259 #     - otherwise keep d[i]
260 def remove_transitive_deps(pkg,deps):
261     d = deps[pkg]
262     new_d = []
263     for i in range(len(d)):
264         keep_me = True
265         for j in range(len(d)):
266             if j==i:
267                 continue
268             if is_dep(d[i],d[j],deps):
269                 keep_me = False
270         if keep_me:
271             new_d.append(d[i])
272     return new_d
273
274 # This function removes the dependency on the 'toolchain' package
275 def remove_toolchain_deps(pkg,deps):
276     return [p for p in deps[pkg] if not p == 'toolchain']
277
278 # This functions trims down the dependency list of all packages.
279 # It applies in sequence all the dependency-elimination methods.
280 def remove_extra_deps(deps):
281     for pkg in list(deps.keys()):
282         if not pkg == 'all':
283             deps[pkg] = remove_toolchain_deps(pkg,deps)
284     for pkg in list(deps.keys()):
285         if not transitive or pkg == 'all':
286             deps[pkg] = remove_transitive_deps(pkg,deps)
287     return deps
288
289 dict_deps = remove_extra_deps(dict_deps)
290 dict_version = get_version([pkg for pkg in allpkgs
291                                 if pkg != "all" and not pkg.startswith("root")])
292
293 # Print the attributes of a node: label and fill-color
294 def print_attrs(pkg):
295     name = pkg_node_name(pkg)
296     if pkg == 'all':
297         label = 'ALL'
298     else:
299         label = pkg
300     if pkg == 'all' or (mode == MODE_PKG and pkg == rootpkg):
301         color = root_colour
302     else:
303         if pkg.startswith('host') \
304         or pkg.startswith('toolchain') \
305         or pkg.startswith('rootfs'):
306             color = host_colour
307         else:
308             color = target_colour
309     version = dict_version.get(pkg)
310     if version == "virtual":
311         print("%s [label = <<I>%s</I>>]" % (name, label))
312     else:
313         print("%s [label = \"%s\"]" % (name, label))
314     print("%s [color=%s,style=filled]" % (name, color))
315
316 # Print the dependency graph of a package
317 def print_pkg_deps(depth, pkg):
318     if pkg in done_deps:
319         return
320     done_deps.append(pkg)
321     print_attrs(pkg)
322     if pkg not in dict_deps:
323         return
324     for p in stop_list:
325         if fnmatch(pkg, p):
326             return
327     if dict_version.get(pkg) == "virtual" and "virtual" in stop_list:
328         return
329     if max_depth == 0 or depth < max_depth:
330         for d in dict_deps[pkg]:
331             add = True
332             for p in exclude_list:
333                 if fnmatch(d,p):
334                     add = False
335                     break
336                 if dict_version.get(d) == "virtual" \
337                 and "virtual" in exclude_list:
338                     add = False
339                     break
340             if add:
341                 print("%s -> %s" % (pkg_node_name(pkg), pkg_node_name(d)))
342                 print_pkg_deps(depth+1, d)
343
344 # Start printing the graph data
345 print("digraph G {")
346
347 done_deps = []
348 print_pkg_deps(0, rootpkg)
349
350 print("}")