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 # Basic cache for the results of the is_dep() function, in order to
241 # optimize the execution time. The cache is a dict of dict of boolean
242 # values. The key to the primary dict is "pkg", and the key of the
243 # sub-dicts is "pkg2".
246 def is_dep_cache_insert(pkg
, pkg2
, val
):
248 is_dep_cache
[pkg
].update({pkg2
: val
})
250 is_dep_cache
[pkg
] = {pkg2
: val
}
252 # Retrieves from the cache whether pkg2 is a transitive dependency
254 # Note: raises a KeyError exception if the dependency is not known.
255 def is_dep_cache_lookup(pkg
, pkg2
):
256 return is_dep_cache
[pkg
][pkg2
]
258 # This function return True if pkg is a dependency (direct or
259 # transitive) of pkg2, dependencies being listed in the deps
260 # dictionary. Returns False otherwise.
261 # This is the un-cached version.
262 def is_dep_uncached(pkg
,pkg2
,deps
):
267 if is_dep(pkg
,p
,deps
):
273 # See is_dep_uncached() above; this is the cached version.
274 def is_dep(pkg
,pkg2
,deps
):
276 return is_dep_cache_lookup(pkg
, pkg2
)
278 val
= is_dep_uncached(pkg
, pkg2
, deps
)
279 is_dep_cache_insert(pkg
, pkg2
, val
)
282 # This function eliminates transitive dependencies; for example, given
283 # these dependency chain: A->{B,C} and B->{C}, the A->{C} dependency is
284 # already covered by B->{C}, so C is a transitive dependency of A, via B.
285 # The functions does:
286 # - for each dependency d[i] of the package pkg
287 # - if d[i] is a dependency of any of the other dependencies d[j]
289 # - otherwise keep d[i]
290 def remove_transitive_deps(pkg
,deps
):
293 for i
in range(len(d
)):
295 for j
in range(len(d
)):
298 if is_dep(d
[i
],d
[j
],deps
):
304 # This function removes the dependency on some 'mandatory' package, like the
305 # 'toolchain' package, or the 'skeleton' package
306 def remove_mandatory_deps(pkg
,deps
):
307 return [p
for p
in deps
[pkg
] if p
not in ['toolchain', 'skeleton']]
309 # This functions trims down the dependency list of all packages.
310 # It applies in sequence all the dependency-elimination methods.
311 def remove_extra_deps(deps
):
312 for pkg
in list(deps
.keys()):
314 deps
[pkg
] = remove_mandatory_deps(pkg
,deps
)
315 for pkg
in list(deps
.keys()):
316 if not transitive
or pkg
== 'all':
317 deps
[pkg
] = remove_transitive_deps(pkg
,deps
)
320 dict_deps
= remove_extra_deps(dict_deps
)
321 dict_version
= get_version([pkg
for pkg
in allpkgs
322 if pkg
!= "all" and not pkg
.startswith("root")])
324 # Print the attributes of a node: label and fill-color
325 def print_attrs(pkg
):
326 name
= pkg_node_name(pkg
)
331 if pkg
== 'all' or (mode
== MODE_PKG
and pkg
== rootpkg
):
334 if pkg
.startswith('host') \
335 or pkg
.startswith('toolchain') \
336 or pkg
.startswith('rootfs'):
339 color
= target_colour
340 version
= dict_version
.get(pkg
)
341 if version
== "virtual":
342 print("%s [label = <<I>%s</I>>]" % (name
, label
))
344 print("%s [label = \"%s\"]" % (name
, label
))
345 print("%s [color=%s,style=filled]" % (name
, color
))
347 # Print the dependency graph of a package
348 def print_pkg_deps(depth
, pkg
):
351 done_deps
.append(pkg
)
353 if pkg
not in dict_deps
:
358 if dict_version
.get(pkg
) == "virtual" and "virtual" in stop_list
:
360 if max_depth
== 0 or depth
< max_depth
:
361 for d
in dict_deps
[pkg
]:
363 for p
in exclude_list
:
367 if dict_version
.get(d
) == "virtual" \
368 and "virtual" in exclude_list
:
372 print("%s -> %s" % (pkg_node_name(pkg
), pkg_node_name(d
)))
373 print_pkg_deps(depth
+1, d
)
375 # Start printing the graph data
379 print_pkg_deps(0, rootpkg
)