2010-05-06 10:09:14 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# Usage (the graphviz package must be installed in your distribution)
|
2014-04-13 22:42:38 +02:00
|
|
|
# ./support/scripts/graph-depends [-p package-name] > test.dot
|
2010-05-06 10:09:14 +02:00
|
|
|
# dot -Tpdf test.dot -o test.pdf
|
|
|
|
#
|
|
|
|
# With no arguments, graph-depends will draw a complete graph of
|
2014-04-13 22:42:38 +02:00
|
|
|
# dependencies for the current configuration.
|
|
|
|
# If '-p <package-name>' is specified, graph-depends will draw a graph
|
|
|
|
# of dependencies for the given package name.
|
2014-04-13 22:42:39 +02:00
|
|
|
# If '-d <depth>' is specified, graph-depends will limit the depth of
|
|
|
|
# the dependency graph to 'depth' levels.
|
2010-05-06 10:09:14 +02:00
|
|
|
#
|
|
|
|
# Limitations
|
|
|
|
#
|
|
|
|
# * Some packages have dependencies that depend on the Buildroot
|
|
|
|
# configuration. For example, many packages have a dependency on
|
|
|
|
# openssl if openssl has been enabled. This tool will graph the
|
|
|
|
# dependencies as they are with the current Buildroot
|
|
|
|
# configuration.
|
|
|
|
#
|
2013-01-02 08:08:52 +01:00
|
|
|
# Copyright (C) 2010-2013 Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
|
2010-05-06 10:09:14 +02:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import subprocess
|
2014-04-13 22:42:38 +02:00
|
|
|
import argparse
|
2015-03-24 23:16:49 +01:00
|
|
|
from fnmatch import fnmatch
|
2010-05-06 10:09:14 +02:00
|
|
|
|
2014-06-08 16:03:51 +02:00
|
|
|
# Modes of operation:
|
|
|
|
MODE_FULL = 1 # draw full dependency graph for all selected packages
|
|
|
|
MODE_PKG = 2 # draw dependency graph for a given package
|
2010-05-06 10:09:14 +02:00
|
|
|
mode = 0
|
2014-06-08 16:03:48 +02:00
|
|
|
|
|
|
|
# Limit drawing the dependency graph to this depth. 0 means 'no limit'.
|
2014-04-13 22:42:39 +02:00
|
|
|
max_depth = 0
|
2010-05-06 10:09:14 +02:00
|
|
|
|
2014-06-08 16:03:50 +02:00
|
|
|
# Whether to draw the transitive dependencies
|
|
|
|
transitive = True
|
|
|
|
|
2015-03-24 23:16:48 +01:00
|
|
|
parser = argparse.ArgumentParser(description="Graph packages dependencies")
|
2014-04-13 22:42:38 +02:00
|
|
|
parser.add_argument("--package", '-p', metavar="PACKAGE",
|
|
|
|
help="Graph the dependencies of PACKAGE")
|
2014-06-08 16:03:48 +02:00
|
|
|
parser.add_argument("--depth", '-d', metavar="DEPTH", dest="depth", type=int, default=0,
|
|
|
|
help="Limit the dependency graph to DEPTH levels; 0 means no limit.")
|
2015-03-24 23:16:48 +01:00
|
|
|
parser.add_argument("--stop-on", "-s", metavar="PACKAGE", dest="stop_list", action="append",
|
|
|
|
help="Do not graph past this package (can be given multiple times)." \
|
2015-03-24 23:16:49 +01:00
|
|
|
+ " Can be a package name or a glob, or" \
|
2015-03-24 23:16:48 +01:00
|
|
|
+ " 'virtual' to stop on virtual packages.")
|
2015-03-24 23:16:50 +01:00
|
|
|
parser.add_argument("--exclude", "-x", metavar="PACKAGE", dest="exclude_list", action="append",
|
|
|
|
help="Like --stop-on, but do not add PACKAGE to the graph.")
|
2014-06-08 16:03:49 +02:00
|
|
|
parser.add_argument("--colours", "-c", metavar="COLOR_LIST", dest="colours",
|
|
|
|
default="lightblue,grey,gainsboro",
|
|
|
|
help="Comma-separated list of the three colours to use" \
|
|
|
|
+ " to draw the top-level package, the target" \
|
|
|
|
+ " packages, and the host packages, in this order." \
|
|
|
|
+ " Defaults to: 'lightblue,grey,gainsboro'")
|
2014-06-08 16:03:46 +02:00
|
|
|
parser.add_argument("--transitive", dest="transitive", action='store_true',
|
2014-06-10 00:28:56 +02:00
|
|
|
default=False)
|
2014-06-08 16:03:46 +02:00
|
|
|
parser.add_argument("--no-transitive", dest="transitive", action='store_false',
|
|
|
|
help="Draw (do not draw) transitive dependencies")
|
2014-04-13 22:42:38 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2014-04-14 22:47:56 +02:00
|
|
|
if args.package is None:
|
2014-06-08 16:03:51 +02:00
|
|
|
mode = MODE_FULL
|
2010-05-06 10:09:14 +02:00
|
|
|
else:
|
2014-06-08 16:03:51 +02:00
|
|
|
mode = MODE_PKG
|
2014-04-13 22:42:38 +02:00
|
|
|
rootpkg = args.package
|
2010-05-06 10:09:14 +02:00
|
|
|
|
2014-06-08 16:03:48 +02:00
|
|
|
max_depth = args.depth
|
2014-04-13 22:42:39 +02:00
|
|
|
|
2015-03-24 23:16:48 +01:00
|
|
|
if args.stop_list is None:
|
|
|
|
stop_list = []
|
|
|
|
else:
|
|
|
|
stop_list = args.stop_list
|
|
|
|
|
2015-03-24 23:16:50 +01:00
|
|
|
if args.exclude_list is None:
|
|
|
|
exclude_list = []
|
|
|
|
else:
|
|
|
|
exclude_list = args.exclude_list
|
|
|
|
|
2014-06-08 16:03:46 +02:00
|
|
|
transitive = args.transitive
|
|
|
|
|
2014-06-08 16:03:49 +02:00
|
|
|
# Get the colours: we need exactly three colours,
|
|
|
|
# so no need not split more than 4
|
|
|
|
# We'll let 'dot' validate the colours...
|
|
|
|
colours = args.colours.split(',',4)
|
|
|
|
if len(colours) != 3:
|
|
|
|
sys.stderr.write("Error: incorrect colour list '%s'\n" % args.colours)
|
|
|
|
sys.exit(1)
|
|
|
|
root_colour = colours[0]
|
|
|
|
target_colour = colours[1]
|
|
|
|
host_colour = colours[2]
|
|
|
|
|
2010-05-06 10:09:14 +02:00
|
|
|
allpkgs = []
|
|
|
|
|
2015-01-03 15:29:12 +01:00
|
|
|
# Execute the "make <pkg>-show-version" command to get the version of a given
|
|
|
|
# list of packages, and return the version formatted as a Python dictionary.
|
|
|
|
def get_version(pkgs):
|
|
|
|
sys.stderr.write("Getting version for %s\n" % pkgs)
|
|
|
|
cmd = ["make", "-s", "--no-print-directory" ]
|
|
|
|
for pkg in pkgs:
|
|
|
|
cmd.append("%s-show-version" % pkg)
|
|
|
|
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
|
|
|
|
output = p.communicate()[0]
|
|
|
|
if p.returncode != 0:
|
|
|
|
sys.stderr.write("Error getting version %s\n" % pkgs)
|
|
|
|
sys.exit(1)
|
|
|
|
output = output.split("\n")
|
|
|
|
if len(output) != len(pkgs) + 1:
|
|
|
|
sys.stderr.write("Error getting version\n")
|
|
|
|
sys.exit(1)
|
|
|
|
version = {}
|
|
|
|
for i in range(0, len(pkgs)):
|
|
|
|
pkg = pkgs[i]
|
|
|
|
version[pkg] = output[i]
|
|
|
|
return version
|
|
|
|
|
2010-05-06 10:09:14 +02:00
|
|
|
# Execute the "make show-targets" command to get the list of the main
|
2015-04-12 18:37:48 +02:00
|
|
|
# Buildroot PACKAGES and return it formatted as a Python list. This
|
2010-05-06 10:09:14 +02:00
|
|
|
# list is used as the starting point for full dependency graphs
|
|
|
|
def get_targets():
|
|
|
|
sys.stderr.write("Getting targets\n")
|
2014-06-23 22:11:23 +02:00
|
|
|
cmd = ["make", "-s", "--no-print-directory", "show-targets"]
|
2014-06-23 22:11:24 +02:00
|
|
|
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
|
2010-05-06 10:09:14 +02:00
|
|
|
output = p.communicate()[0].strip()
|
|
|
|
if p.returncode != 0:
|
|
|
|
return None
|
|
|
|
if output == '':
|
|
|
|
return []
|
|
|
|
return output.split(' ')
|
|
|
|
|
|
|
|
# Execute the "make <pkg>-show-depends" command to get the list of
|
2013-01-02 08:08:50 +01:00
|
|
|
# dependencies of a given list of packages, and return the list of
|
|
|
|
# dependencies formatted as a Python dictionary.
|
|
|
|
def get_depends(pkgs):
|
|
|
|
sys.stderr.write("Getting dependencies for %s\n" % pkgs)
|
2014-06-23 22:11:23 +02:00
|
|
|
cmd = ["make", "-s", "--no-print-directory" ]
|
2013-01-02 08:08:50 +01:00
|
|
|
for pkg in pkgs:
|
|
|
|
cmd.append("%s-show-depends" % pkg)
|
2014-06-23 22:11:24 +02:00
|
|
|
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
|
2013-01-02 08:08:50 +01:00
|
|
|
output = p.communicate()[0]
|
2010-05-06 10:09:14 +02:00
|
|
|
if p.returncode != 0:
|
2013-01-02 08:08:50 +01:00
|
|
|
sys.stderr.write("Error getting dependencies %s\n" % pkgs)
|
|
|
|
sys.exit(1)
|
|
|
|
output = output.split("\n")
|
|
|
|
if len(output) != len(pkgs) + 1:
|
|
|
|
sys.stderr.write("Error getting dependencies\n")
|
|
|
|
sys.exit(1)
|
|
|
|
deps = {}
|
|
|
|
for i in range(0, len(pkgs)):
|
|
|
|
pkg = pkgs[i]
|
|
|
|
pkg_deps = output[i].split(" ")
|
|
|
|
if pkg_deps == ['']:
|
|
|
|
deps[pkg] = []
|
|
|
|
else:
|
|
|
|
deps[pkg] = pkg_deps
|
|
|
|
return deps
|
2010-05-06 10:09:14 +02:00
|
|
|
|
|
|
|
# Recursive function that builds the tree of dependencies for a given
|
2013-01-02 08:08:50 +01:00
|
|
|
# list of packages. The dependencies are built in a list called
|
|
|
|
# 'dependencies', which contains tuples of the form (pkg1 ->
|
|
|
|
# pkg2_on_which_pkg1_depends, pkg3 -> pkg4_on_which_pkg3_depends) and
|
|
|
|
# the function finally returns this list.
|
|
|
|
def get_all_depends(pkgs):
|
2010-05-06 10:09:14 +02:00
|
|
|
dependencies = []
|
|
|
|
|
2013-01-02 08:08:50 +01:00
|
|
|
# Filter the packages for which we already have the dependencies
|
|
|
|
filtered_pkgs = []
|
|
|
|
for pkg in pkgs:
|
|
|
|
if pkg in allpkgs:
|
|
|
|
continue
|
|
|
|
filtered_pkgs.append(pkg)
|
|
|
|
allpkgs.append(pkg)
|
|
|
|
|
|
|
|
if len(filtered_pkgs) == 0:
|
|
|
|
return []
|
|
|
|
|
|
|
|
depends = get_depends(filtered_pkgs)
|
|
|
|
|
|
|
|
deps = set()
|
|
|
|
for pkg in filtered_pkgs:
|
|
|
|
pkg_deps = depends[pkg]
|
|
|
|
|
|
|
|
# This package has no dependency.
|
|
|
|
if pkg_deps == []:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Add dependencies to the list of dependencies
|
|
|
|
for dep in pkg_deps:
|
|
|
|
dependencies.append((pkg, dep))
|
|
|
|
deps.add(dep)
|
|
|
|
|
|
|
|
if len(deps) != 0:
|
|
|
|
newdeps = get_all_depends(deps)
|
2014-04-14 22:47:56 +02:00
|
|
|
if newdeps is not None:
|
2010-05-06 10:09:14 +02:00
|
|
|
dependencies += newdeps
|
|
|
|
|
|
|
|
return dependencies
|
|
|
|
|
|
|
|
# The Graphviz "dot" utility doesn't like dashes in node names. So for
|
|
|
|
# node names, we strip all dashes.
|
|
|
|
def pkg_node_name(pkg):
|
|
|
|
return pkg.replace("-","")
|
|
|
|
|
2013-01-02 08:08:49 +01:00
|
|
|
TARGET_EXCEPTIONS = [
|
|
|
|
"target-finalize",
|
2013-03-05 09:59:21 +01:00
|
|
|
"target-post-image",
|
2013-01-02 08:08:49 +01:00
|
|
|
]
|
|
|
|
|
2010-05-06 10:09:14 +02:00
|
|
|
# In full mode, start with the result of get_targets() to get the main
|
2013-01-02 08:08:50 +01:00
|
|
|
# targets and then use get_all_depends() for all targets
|
2014-06-08 16:03:51 +02:00
|
|
|
if mode == MODE_FULL:
|
2010-05-06 10:09:14 +02:00
|
|
|
targets = get_targets()
|
|
|
|
dependencies = []
|
|
|
|
allpkgs.append('all')
|
2013-01-02 08:08:50 +01:00
|
|
|
filtered_targets = []
|
2010-05-06 10:09:14 +02:00
|
|
|
for tg in targets:
|
|
|
|
# Skip uninteresting targets
|
2013-01-02 08:08:49 +01:00
|
|
|
if tg in TARGET_EXCEPTIONS:
|
2010-05-06 10:09:14 +02:00
|
|
|
continue
|
|
|
|
dependencies.append(('all', tg))
|
2013-01-02 08:08:50 +01:00
|
|
|
filtered_targets.append(tg)
|
|
|
|
deps = get_all_depends(filtered_targets)
|
2014-04-14 22:47:56 +02:00
|
|
|
if deps is not None:
|
2013-01-02 08:08:50 +01:00
|
|
|
dependencies += deps
|
2014-04-13 22:42:39 +02:00
|
|
|
rootpkg = 'all'
|
2010-05-06 10:09:14 +02:00
|
|
|
|
|
|
|
# In pkg mode, start directly with get_all_depends() on the requested
|
|
|
|
# package
|
2014-06-08 16:03:51 +02:00
|
|
|
elif mode == MODE_PKG:
|
2013-01-02 08:08:50 +01:00
|
|
|
dependencies = get_all_depends([rootpkg])
|
2010-05-06 10:09:14 +02:00
|
|
|
|
2014-04-13 22:42:39 +02:00
|
|
|
# Make the dependencies a dictionnary { 'pkg':[dep1, dep2, ...] }
|
|
|
|
dict_deps = {}
|
|
|
|
for dep in dependencies:
|
2014-06-20 22:34:07 +02:00
|
|
|
if dep[0] not in dict_deps:
|
2014-04-13 22:42:39 +02:00
|
|
|
dict_deps[dep[0]] = []
|
|
|
|
dict_deps[dep[0]].append(dep[1])
|
2010-05-06 10:09:14 +02:00
|
|
|
|
2014-06-08 16:03:46 +02:00
|
|
|
# This function return True if pkg is a dependency (direct or
|
|
|
|
# transitive) of pkg2, dependencies being listed in the deps
|
|
|
|
# dictionary. Returns False otherwise.
|
|
|
|
def is_dep(pkg,pkg2,deps):
|
2014-06-20 22:34:07 +02:00
|
|
|
if pkg2 in deps:
|
2014-06-08 16:03:46 +02:00
|
|
|
for p in deps[pkg2]:
|
|
|
|
if pkg == p:
|
|
|
|
return True
|
|
|
|
if is_dep(pkg,p,deps):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
# This function eliminates transitive dependencies; for example, given
|
|
|
|
# these dependency chain: A->{B,C} and B->{C}, the A->{C} dependency is
|
|
|
|
# already covered by B->{C}, so C is a transitive dependency of A, via B.
|
|
|
|
# The functions does:
|
|
|
|
# - for each dependency d[i] of the package pkg
|
|
|
|
# - if d[i] is a dependency of any of the other dependencies d[j]
|
|
|
|
# - do not keep d[i]
|
|
|
|
# - otherwise keep d[i]
|
|
|
|
def remove_transitive_deps(pkg,deps):
|
|
|
|
d = deps[pkg]
|
|
|
|
new_d = []
|
|
|
|
for i in range(len(d)):
|
|
|
|
keep_me = True
|
|
|
|
for j in range(len(d)):
|
|
|
|
if j==i:
|
|
|
|
continue
|
|
|
|
if is_dep(d[i],d[j],deps):
|
|
|
|
keep_me = False
|
|
|
|
if keep_me:
|
|
|
|
new_d.append(d[i])
|
|
|
|
return new_d
|
|
|
|
|
2015-07-14 13:36:26 +02:00
|
|
|
# This function removes the dependency on some 'mandatory' package, like the
|
|
|
|
# 'toolchain' package, or the 'skeleton' package
|
|
|
|
def remove_mandatory_deps(pkg,deps):
|
|
|
|
return [p for p in deps[pkg] if p not in ['toolchain', 'skeleton']]
|
2014-06-08 16:03:47 +02:00
|
|
|
|
2014-06-08 16:03:46 +02:00
|
|
|
# This functions trims down the dependency list of all packages.
|
2014-06-08 16:03:47 +02:00
|
|
|
# It applies in sequence all the dependency-elimination methods.
|
2014-06-08 16:03:46 +02:00
|
|
|
def remove_extra_deps(deps):
|
2014-06-20 22:34:07 +02:00
|
|
|
for pkg in list(deps.keys()):
|
2014-06-08 16:03:47 +02:00
|
|
|
if not pkg == 'all':
|
2015-07-14 13:36:26 +02:00
|
|
|
deps[pkg] = remove_mandatory_deps(pkg,deps)
|
2014-06-20 22:34:07 +02:00
|
|
|
for pkg in list(deps.keys()):
|
2014-06-08 16:03:47 +02:00
|
|
|
if not transitive or pkg == 'all':
|
2014-06-08 16:03:46 +02:00
|
|
|
deps[pkg] = remove_transitive_deps(pkg,deps)
|
|
|
|
return deps
|
|
|
|
|
|
|
|
dict_deps = remove_extra_deps(dict_deps)
|
2015-01-03 15:29:12 +01:00
|
|
|
dict_version = get_version([pkg for pkg in allpkgs
|
|
|
|
if pkg != "all" and not pkg.startswith("root")])
|
2014-06-08 16:03:46 +02:00
|
|
|
|
2014-04-13 22:42:39 +02:00
|
|
|
# Print the attributes of a node: label and fill-color
|
|
|
|
def print_attrs(pkg):
|
2014-06-08 16:03:45 +02:00
|
|
|
name = pkg_node_name(pkg)
|
2010-05-06 10:09:14 +02:00
|
|
|
if pkg == 'all':
|
2014-06-08 16:03:45 +02:00
|
|
|
label = 'ALL'
|
|
|
|
else:
|
|
|
|
label = pkg
|
2014-06-08 16:03:51 +02:00
|
|
|
if pkg == 'all' or (mode == MODE_PKG and pkg == rootpkg):
|
2014-06-08 16:03:49 +02:00
|
|
|
color = root_colour
|
2010-05-06 10:09:14 +02:00
|
|
|
else:
|
2014-06-08 16:03:45 +02:00
|
|
|
if pkg.startswith('host') \
|
|
|
|
or pkg.startswith('toolchain') \
|
|
|
|
or pkg.startswith('rootfs'):
|
2014-06-08 16:03:49 +02:00
|
|
|
color = host_colour
|
2014-06-08 16:03:45 +02:00
|
|
|
else:
|
2014-06-08 16:03:49 +02:00
|
|
|
color = target_colour
|
2015-01-03 15:29:12 +01:00
|
|
|
version = dict_version.get(pkg)
|
|
|
|
if version == "virtual":
|
|
|
|
print("%s [label = <<I>%s</I>>]" % (name, label))
|
|
|
|
else:
|
|
|
|
print("%s [label = \"%s\"]" % (name, label))
|
2014-06-20 22:34:07 +02:00
|
|
|
print("%s [color=%s,style=filled]" % (name, color))
|
2010-05-06 10:09:14 +02:00
|
|
|
|
2014-04-13 22:42:39 +02:00
|
|
|
# Print the dependency graph of a package
|
|
|
|
def print_pkg_deps(depth, pkg):
|
|
|
|
if pkg in done_deps:
|
|
|
|
return
|
|
|
|
done_deps.append(pkg)
|
|
|
|
print_attrs(pkg)
|
2014-06-20 22:34:07 +02:00
|
|
|
if pkg not in dict_deps:
|
2014-04-13 22:42:39 +02:00
|
|
|
return
|
2015-03-24 23:16:49 +01:00
|
|
|
for p in stop_list:
|
|
|
|
if fnmatch(pkg, p):
|
|
|
|
return
|
2015-03-24 23:16:48 +01:00
|
|
|
if dict_version.get(pkg) == "virtual" and "virtual" in stop_list:
|
|
|
|
return
|
2014-04-13 22:42:39 +02:00
|
|
|
if max_depth == 0 or depth < max_depth:
|
|
|
|
for d in dict_deps[pkg]:
|
2015-03-24 23:16:50 +01:00
|
|
|
add = True
|
|
|
|
for p in exclude_list:
|
|
|
|
if fnmatch(d,p):
|
|
|
|
add = False
|
|
|
|
break
|
2015-04-01 00:34:14 +02:00
|
|
|
if dict_version.get(d) == "virtual" \
|
|
|
|
and "virtual" in exclude_list:
|
|
|
|
add = False
|
|
|
|
break
|
2015-03-24 23:16:50 +01:00
|
|
|
if add:
|
|
|
|
print("%s -> %s" % (pkg_node_name(pkg), pkg_node_name(d)))
|
|
|
|
print_pkg_deps(depth+1, d)
|
2014-04-13 22:42:39 +02:00
|
|
|
|
|
|
|
# Start printing the graph data
|
2014-06-20 22:34:07 +02:00
|
|
|
print("digraph G {")
|
2014-04-13 22:42:39 +02:00
|
|
|
|
|
|
|
done_deps = []
|
|
|
|
print_pkg_deps(0, rootpkg)
|
|
|
|
|
2014-06-20 22:34:07 +02:00
|
|
|
print("}")
|