Files
buildroot/support/scripts/graph-depends
Yann E. MORIN 67957967a5 graphs: add option to remove transitive dependencies in dependency graph
Currently, all the dependencies of a package are drawn on the dependency
graph, including transitive dependencies (e.g. A->B->C and A->C).

For very big graphs, with lots of packages with lots of dependencies, the
dependency graph can be very dense, and transitive dependencies are
cluttering the graph.

In some cases, only getting the "build-order" dependencies is enough (e.g.
to see what impact a package rebuild would have).

Add a new environment variable to disable drawing transitive dependencies.

Basically, it would turn this graph:

    pkg1 ---> pkg2 ---> pkg3 -------------------.
         |\__________/                 \         \
         |\____________________         \         \
         |                     \         \         \
          `-> pkg4 ---> pkg5 ---> pkg6 ---> pkg7 ---> pkg8
                    \__________/

into that graph:

    pkg1 ---> pkg2 ---> pkg3 -----------.
         |                               \
          `-> pkg4 ---> pkg5 ---> pkg6 ---> pkg7 ---> pkg8

[Thanks to Samuel for the parser hints]

Signed-off-by: "Yann E. MORIN" <yann.morin.1998@free.fr>
Cc: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
Cc: Maxime Hadjinlian <maxime.hadjinlian@gmail.com>
Cc: Samuel Martin <s.martin49@gmail.com>
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
2014-06-08 16:43:04 +02:00

311 lines
9.8 KiB
Python
Executable File

#!/usr/bin/python
# Usage (the graphviz package must be installed in your distribution)
# ./support/scripts/graph-depends [-p package-name] > test.dot
# dot -Tpdf test.dot -o test.pdf
#
# With no arguments, graph-depends will draw a complete graph of
# dependencies for the current configuration.
# If '-p <package-name>' is specified, graph-depends will draw a graph
# of dependencies for the given package name.
# If '-d <depth>' is specified, graph-depends will limit the depth of
# the dependency graph to 'depth' levels.
#
# 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.
#
# Copyright (C) 2010-2013 Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
import sys
import subprocess
import argparse
# In FULL_MODE, we draw the full dependency graph for all selected
# packages
FULL_MODE = 1
# In PKG_MODE, we only draw the dependency graph for a given package
PKG_MODE = 2
mode = 0
max_depth = 0
parser = argparse.ArgumentParser(description="Graph pacakges dependencies")
parser.add_argument("--package", '-p', metavar="PACKAGE",
help="Graph the dependencies of PACKAGE")
parser.add_argument("--depth", '-d', metavar="DEPTH",
help="Limit the dependency graph to DEPTH levels")
parser.add_argument("--transitive", dest="transitive", action='store_true',
default=True)
parser.add_argument("--no-transitive", dest="transitive", action='store_false',
help="Draw (do not draw) transitive dependencies")
args = parser.parse_args()
if args.package is None:
mode = FULL_MODE
else:
mode = PKG_MODE
rootpkg = args.package
if args.depth is not None:
max_depth = int(args.depth)
transitive = args.transitive
allpkgs = []
# Execute the "make show-targets" command to get the list of the main
# Buildroot TARGETS and return it formatted as a Python list. This
# list is used as the starting point for full dependency graphs
def get_targets():
sys.stderr.write("Getting targets\n")
cmd = ["make", "-s", "show-targets"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
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
# 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)
cmd = ["make", "-s" ]
for pkg in pkgs:
cmd.append("%s-show-depends" % pkg)
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = p.communicate()[0]
if p.returncode != 0:
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
# Recursive function that builds the tree of dependencies for a given
# 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):
dependencies = []
# 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)
if newdeps is not None:
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("-","")
# Helper function for remove_redundant_deps(). This function tells
# whether package "pkg" is the dependency of another package that is
# not the special "all" package.
def has_redundant_deps(deps, pkg):
for dep in deps:
if dep[0] != "all" and dep[1] == pkg:
return True
return False
# This function removes redundant dependencies of the special "all"
# package. This "all" package is created to reflect the origin of the
# selection for all packages that are not themselves selected by any
# other package. So for example if you enable libpng, zlib is enabled
# as a dependency. But zlib being selected by libpng, it also appears
# as a dependency of the "all" package. This needlessly complicates
# the generated dependency graph. So when we have the dependency list
# (all -> zlib, all -> libpn, libpng -> zlib), we get rid of the 'all
# -> zlib' dependency, because zlib is already a dependency of a
# regular package.
def remove_redundant_deps(deps):
newdeps = []
for dep in deps:
if dep[0] == "all" and dep[1] == "toolchain":
newdeps.append(dep)
continue
if dep[0] != "all" and dep[1] != "toolchain":
newdeps.append(dep)
continue
if not has_redundant_deps(deps, dep[1]):
newdeps.append(dep)
continue
sys.stderr.write("Removing redundant dep %s -> %s\n" % (dep[0],dep[1]))
return newdeps
TARGET_EXCEPTIONS = [
"target-generic-securetty",
"target-generic-issue",
"target-generic-getty-busybox",
"target-generic-do-remount-rw",
"target-generic-dont-remount-rw",
"target-finalize",
"erase-fakeroots",
"target-generic-hostname",
"target-root-passwd",
"target-post-image",
"target-purgelocales",
]
# In full mode, start with the result of get_targets() to get the main
# targets and then use get_all_depends() for all targets
if mode == FULL_MODE:
targets = get_targets()
dependencies = []
allpkgs.append('all')
filtered_targets = []
for tg in targets:
# Skip uninteresting targets
if tg in TARGET_EXCEPTIONS:
continue
dependencies.append(('all', tg))
filtered_targets.append(tg)
deps = get_all_depends(filtered_targets)
if deps is not None:
dependencies += deps
rootpkg = 'all'
# In pkg mode, start directly with get_all_depends() on the requested
# package
elif mode == PKG_MODE:
dependencies = get_all_depends([rootpkg])
dependencies = remove_redundant_deps(dependencies)
# Make the dependencies a dictionnary { 'pkg':[dep1, dep2, ...] }
dict_deps = {}
for dep in dependencies:
if not dict_deps.has_key(dep[0]):
dict_deps[dep[0]] = []
dict_deps[dep[0]].append(dep[1])
# 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):
if deps.has_key(pkg2):
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
# This functions trims down the dependency list of all packages.
def remove_extra_deps(deps):
for pkg in deps.keys():
if not transitive:
deps[pkg] = remove_transitive_deps(pkg,deps)
return deps
dict_deps = remove_extra_deps(dict_deps)
# Print the attributes of a node: label and fill-color
def print_attrs(pkg):
name = pkg_node_name(pkg)
if pkg == 'all':
label = 'ALL'
else:
label = pkg
if pkg == 'all' or (mode == PKG_MODE and pkg == rootpkg):
color = 'lightblue'
else:
if pkg.startswith('host') \
or pkg.startswith('toolchain') \
or pkg.startswith('rootfs'):
color = 'gainsboro'
else:
color = 'grey'
print "%s [label = \"%s\"]" % (name, label)
print "%s [color=%s,style=filled]" % (name, color)
# 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)
if not dict_deps.has_key(pkg):
return
if max_depth == 0 or depth < max_depth:
for d in dict_deps[pkg]:
print "%s -> %s" % (pkg_node_name(pkg), pkg_node_name(d))
print_pkg_deps(depth+1, d)
# Start printing the graph data
print "digraph G {"
done_deps = []
print_pkg_deps(0, rootpkg)
print "}"