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
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.
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
22 # Copyright (C) 2010-2013 Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
27 from fnmatch import fnmatch
30 MODE_FULL = 1 # draw full dependency graph for all selected packages
31 MODE_PKG = 2 # draw dependency graph for a given package
34 # Limit drawing the dependency graph to this depth. 0 means 'no limit'.
37 # Whether to draw the transitive dependencies
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',
59 parser.add_argument("--no-transitive", dest="transitive", action='store_false',
60 help="Draw (do not draw) transitive dependencies")
61 args = parser.parse_args()
63 if args.package is None:
67 rootpkg = args.package
69 max_depth = args.depth
71 if args.stop_list is None:
74 stop_list = args.stop_list
76 if args.exclude_list is None:
79 exclude_list = args.exclude_list
81 transitive = args.transitive
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)
88 sys.stderr.write("Error: incorrect colour list '%s'\n" % args.colours)
90 root_colour = colours[0]
91 target_colour = colours[1]
92 host_colour = colours[2]
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" ]
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)
108 output = output.split("\n")
109 if len(output) != len(pkgs) + 1:
110 sys.stderr.write("Error getting version\n")
113 for i in range(0, len(pkgs)):
115 version[pkg] = output[i]
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
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:
130 return output.split(' ')
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" ]
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)
145 output = output.split("\n")
146 if len(output) != len(pkgs) + 1:
147 sys.stderr.write("Error getting dependencies\n")
150 for i in range(0, len(pkgs)):
152 pkg_deps = output[i].split(" ")
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):
167 # Filter the packages for which we already have the dependencies
172 filtered_pkgs.append(pkg)
175 if len(filtered_pkgs) == 0:
178 depends = get_depends(filtered_pkgs)
181 for pkg in filtered_pkgs:
182 pkg_deps = depends[pkg]
184 # This package has no dependency.
188 # Add dependencies to the list of dependencies
190 dependencies.append((pkg, dep))
194 newdeps = get_all_depends(deps)
195 if newdeps is not None:
196 dependencies += newdeps
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("-","")
205 TARGET_EXCEPTIONS = [
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()
215 allpkgs.append('all')
216 filtered_targets = []
218 # Skip uninteresting targets
219 if tg in TARGET_EXCEPTIONS:
221 dependencies.append(('all', tg))
222 filtered_targets.append(tg)
223 deps = get_all_depends(filtered_targets)
228 # In pkg mode, start directly with get_all_depends() on the requested
230 elif mode == MODE_PKG:
231 dependencies = get_all_depends([rootpkg])
233 # Make the dependencies a dictionnary { 'pkg':[dep1, dep2, ...] }
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])
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):
248 if is_dep(pkg,p,deps):
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]
259 # - otherwise keep d[i]
260 def remove_transitive_deps(pkg,deps):
263 for i in range(len(d)):
265 for j in range(len(d)):
268 if is_dep(d[i],d[j],deps):
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']
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()):
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)
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")])
293 # Print the attributes of a node: label and fill-color
294 def print_attrs(pkg):
295 name = pkg_node_name(pkg)
300 if pkg == 'all' or (mode == MODE_PKG and pkg == rootpkg):
303 if pkg.startswith('host') \
304 or pkg.startswith('toolchain') \
305 or pkg.startswith('rootfs'):
308 color = target_colour
309 version = dict_version.get(pkg)
310 if version == "virtual":
311 print("%s [label = <<I>%s</I>>]" % (name, label))
313 print("%s [label = \"%s\"]" % (name, label))
314 print("%s [color=%s,style=filled]" % (name, color))
316 # Print the dependency graph of a package
317 def print_pkg_deps(depth, pkg):
320 done_deps.append(pkg)
322 if pkg not in dict_deps:
327 if dict_version.get(pkg) == "virtual" and "virtual" in stop_list:
329 if max_depth == 0 or depth < max_depth:
330 for d in dict_deps[pkg]:
332 for p in exclude_list:
336 if dict_version.get(d) == "virtual" \
337 and "virtual" in exclude_list:
341 print("%s -> %s" % (pkg_node_name(pkg), pkg_node_name(d)))
342 print_pkg_deps(depth+1, d)
344 # Start printing the graph data
348 print_pkg_deps(0, rootpkg)