2017-06-07 21:10:26 +02:00
|
|
|
#!/usr/bin/env python
|
2010-05-06 10:09:14 +02:00
|
|
|
|
|
|
|
# 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
|
|
|
|
2017-03-21 09:22:33 +01:00
|
|
|
import brpkgutil
|
2017-02-03 21:57:44 +01:00
|
|
|
|
2014-06-08 16:03:51 +02:00
|
|
|
# Modes of operation:
|
|
|
|
MODE_FULL = 1 # draw full dependency graph for all selected packages
|
2018-01-22 01:44:29 +01:00
|
|
|
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")
|
2016-02-07 22:34:28 +01:00
|
|
|
parser.add_argument("--check-only", "-C", dest="check_only", action="store_true", default=False,
|
|
|
|
help="Only do the dependency checks (circular deps...)")
|
2016-02-07 22:34:25 +01:00
|
|
|
parser.add_argument("--outfile", "-o", metavar="OUT_FILE", dest="outfile",
|
|
|
|
help="File in which to generate the dot representation")
|
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",
|
2018-01-22 01:44:29 +01:00
|
|
|
help="Do not graph past this package (can be given multiple times)." +
|
|
|
|
" Can be a package name or a glob, " +
|
|
|
|
" 'virtual' to stop on virtual packages, or " +
|
|
|
|
"'host' to stop on host 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",
|
2018-01-22 01:44:29 +01:00
|
|
|
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")
|
2016-10-23 19:19:44 +02:00
|
|
|
parser.add_argument("--direct", dest="direct", action='store_true', default=True,
|
|
|
|
help="Draw direct dependencies (the default)")
|
|
|
|
parser.add_argument("--reverse", dest="direct", action='store_false',
|
|
|
|
help="Draw reverse dependencies")
|
2014-04-13 22:42:38 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2016-02-07 22:34:28 +01:00
|
|
|
check_only = args.check_only
|
|
|
|
|
2016-02-07 22:34:25 +01:00
|
|
|
if args.outfile is None:
|
|
|
|
outfile = sys.stdout
|
|
|
|
else:
|
2016-02-07 22:34:28 +01:00
|
|
|
if check_only:
|
|
|
|
sys.stderr.write("don't specify outfile and check-only at the same time\n")
|
|
|
|
sys.exit(1)
|
support/scripts: fix graph-depends when run with python3
Make graph-depends script opening the output file in text mode since
only ascii characters will be written.
This change fixes the following error occuring when the default host
python interpreter is python3:
make: Entering directory '/opt/buildroot'
Getting targets
Getting dependencies for ['toolchain-external', 'toolchain', 'busybox', ...]
Getting dependencies for ['host-python3', 'host-pkgconf', 'host-gettext', ...]
Getting dependencies for ['host-libxml2', 'host-swig', 'host-m4', ...]
Getting version for ['toolchain-external', 'toolchain', 'busybox', ...]
Traceback (most recent call last):
File "/opt/buildroot/support/scripts/graph-depends", line 425, in <module>
outfile.write("digraph G {\n")
TypeError: a bytes-like object is required, not 'str'
Makefile:807: recipe for target 'graph-depends' failed
make[1]: *** [graph-depends] Error 1
Makefile:84: recipe for target '_all' failed
make: *** [_all] Error 2
make: Leaving directory '/opt/buildroot'
While with python2, adding 'b' to the openning mode has no effect on
Linux (c.f. [2]), the above error is expected with python3 (c.f. [1]).
Therefore, just open the outfile in default (i.e. text) mode.
[1] https://docs.python.org/3/library/functions.html#open
[2] https://docs.python.org/2/library/functions.html#open
Signed-off-by: Samuel Martin <s.martin49@gmail.com>
Acked-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
2016-04-17 23:41:48 +02:00
|
|
|
outfile = open(args.outfile, "w")
|
2016-02-07 22:34:25 +01:00
|
|
|
|
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
|
|
|
|
|
2016-10-23 19:19:44 +02:00
|
|
|
if args.direct:
|
2017-03-21 09:22:33 +01:00
|
|
|
get_depends_func = brpkgutil.get_depends
|
2016-10-23 19:19:44 +02:00
|
|
|
arrow_dir = "forward"
|
|
|
|
else:
|
|
|
|
if mode == MODE_FULL:
|
|
|
|
sys.stderr.write("--reverse needs a package\n")
|
|
|
|
sys.exit(1)
|
2017-03-21 09:22:33 +01:00
|
|
|
get_depends_func = brpkgutil.get_rdepends
|
2016-10-23 19:19:44 +02:00
|
|
|
arrow_dir = "back"
|
|
|
|
|
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...
|
2018-01-22 01:44:29 +01:00
|
|
|
colours = args.colours.split(',', 4)
|
2014-06-08 16:03:49 +02:00
|
|
|
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 = []
|
|
|
|
|
2018-01-22 01:44:29 +01:00
|
|
|
|
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(' ')
|
|
|
|
|
2018-01-22 01:44:29 +01:00
|
|
|
|
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 []
|
|
|
|
|
2017-02-03 21:57:45 +01:00
|
|
|
depends = get_depends_func(filtered_pkgs)
|
2013-01-02 08:08:50 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-01-22 01:44:29 +01:00
|
|
|
|
2010-05-06 10:09:14 +02:00
|
|
|
# The Graphviz "dot" utility doesn't like dashes in node names. So for
|
|
|
|
# node names, we strip all dashes.
|
|
|
|
def pkg_node_name(pkg):
|
2018-01-22 01:44:29 +01:00
|
|
|
return pkg.replace("-", "")
|
|
|
|
|
2010-05-06 10:09:14 +02:00
|
|
|
|
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
|
|
|
|
2015-12-15 11:21:41 +01:00
|
|
|
# Basic cache for the results of the is_dep() function, in order to
|
|
|
|
# optimize the execution time. The cache is a dict of dict of boolean
|
|
|
|
# values. The key to the primary dict is "pkg", and the key of the
|
|
|
|
# sub-dicts is "pkg2".
|
|
|
|
is_dep_cache = {}
|
|
|
|
|
2018-01-22 01:44:29 +01:00
|
|
|
|
2015-12-15 11:21:41 +01:00
|
|
|
def is_dep_cache_insert(pkg, pkg2, val):
|
|
|
|
try:
|
|
|
|
is_dep_cache[pkg].update({pkg2: val})
|
|
|
|
except KeyError:
|
|
|
|
is_dep_cache[pkg] = {pkg2: val}
|
|
|
|
|
2018-01-22 01:44:29 +01:00
|
|
|
|
2015-12-15 11:21:41 +01:00
|
|
|
# Retrieves from the cache whether pkg2 is a transitive dependency
|
|
|
|
# of pkg.
|
|
|
|
# Note: raises a KeyError exception if the dependency is not known.
|
|
|
|
def is_dep_cache_lookup(pkg, pkg2):
|
|
|
|
return is_dep_cache[pkg][pkg2]
|
|
|
|
|
2018-01-22 01:44:29 +01: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.
|
2015-12-15 11:21:41 +01:00
|
|
|
# This is the un-cached version.
|
2018-01-22 01:44:29 +01:00
|
|
|
def is_dep_uncached(pkg, pkg2, deps):
|
2015-12-15 11:21:41 +01:00
|
|
|
try:
|
2014-06-08 16:03:46 +02:00
|
|
|
for p in deps[pkg2]:
|
|
|
|
if pkg == p:
|
|
|
|
return True
|
2018-01-22 01:44:29 +01:00
|
|
|
if is_dep(pkg, p, deps):
|
2014-06-08 16:03:46 +02:00
|
|
|
return True
|
2015-12-15 11:21:41 +01:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2014-06-08 16:03:46 +02:00
|
|
|
return False
|
|
|
|
|
2018-01-22 01:44:29 +01:00
|
|
|
|
2015-12-29 23:46:10 +01:00
|
|
|
# See is_dep_uncached() above; this is the cached version.
|
2018-01-22 01:44:29 +01:00
|
|
|
def is_dep(pkg, pkg2, deps):
|
2015-12-15 11:21:41 +01:00
|
|
|
try:
|
|
|
|
return is_dep_cache_lookup(pkg, pkg2)
|
|
|
|
except KeyError:
|
|
|
|
val = is_dep_uncached(pkg, pkg2, deps)
|
|
|
|
is_dep_cache_insert(pkg, pkg2, val)
|
|
|
|
return val
|
|
|
|
|
2018-01-22 01:44:29 +01:00
|
|
|
|
2014-06-08 16:03:46 +02:00
|
|
|
# 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]
|
2018-01-22 01:44:29 +01:00
|
|
|
def remove_transitive_deps(pkg, deps):
|
2014-06-08 16:03:46 +02:00
|
|
|
d = deps[pkg]
|
|
|
|
new_d = []
|
|
|
|
for i in range(len(d)):
|
|
|
|
keep_me = True
|
|
|
|
for j in range(len(d)):
|
2018-01-22 01:44:29 +01:00
|
|
|
if j == i:
|
2014-06-08 16:03:46 +02:00
|
|
|
continue
|
2018-01-22 01:44:29 +01:00
|
|
|
if is_dep(d[i], d[j], deps):
|
2014-06-08 16:03:46 +02:00
|
|
|
keep_me = False
|
|
|
|
if keep_me:
|
|
|
|
new_d.append(d[i])
|
|
|
|
return new_d
|
|
|
|
|
2018-01-22 01:44:29 +01:00
|
|
|
|
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
|
2018-01-22 01:44:29 +01:00
|
|
|
def remove_mandatory_deps(pkg, deps):
|
2015-07-14 13:36:26 +02:00
|
|
|
return [p for p in deps[pkg] if p not in ['toolchain', 'skeleton']]
|
2014-06-08 16:03:47 +02:00
|
|
|
|
2018-01-22 01:44:29 +01:00
|
|
|
|
support/graph-depends: detect circular dependencies
Currently, if there is a circular dependency in the packages, the
graph-depends script just errors out with a Python RuntimeError which is
not caught, resulting in a very-long backtrace which does not provide
any hint as what the real issue is (even if "RuntimeError: maximum
recursion depth exceeded" is a pretty good hint at it).
We fix that by recursing the dependency chain of each package, until we
either end up with a package with no dependency, or with a package
already seen along the current dependency chain.
We need to introduce a new function, check_circular_deps(), because we
can't re-use the existing ones:
- remove_mandatory_deps() does not iterate,
- remove_transitive_deps() does iterate, but we do not call it for the
top-level package if it is not 'all'
- it does not make sense to use those functions anyway, as they were
not designed to _check_ but to _act_ on the dependency chain.
Since we've had time-related issues in the past, we do not want to
introduce yet another time-hog, so here are timings with the circular
dependency check:
$ time python -m cProfile -s cumtime support/scripts/graph-depends
[...]
28352654 function calls (20323050 primitive calls) in 87.292 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.012 0.012 87.292 87.292 graph-depends:24(<module>)
21 0.000 0.000 73.685 3.509 subprocess.py:473(_eintr_retry_call)
7 0.000 0.000 73.655 10.522 subprocess.py:768(communicate)
7 73.653 10.522 73.653 10.522 {method 'read' of 'file' objects}
5/1 0.027 0.005 43.488 43.488 graph-depends:164(get_all_depends)
5 0.003 0.001 43.458 8.692 graph-depends:135(get_depends)
1 0.001 0.001 25.712 25.712 graph-depends:98(get_version)
1 0.001 0.001 13.457 13.457 graph-depends:337(remove_extra_deps)
1717 1.672 0.001 13.050 0.008 graph-depends:290(remove_transitive_deps)
9784086/2672326 5.079 0.000 11.363 0.000 graph-depends:274(is_dep)
2883343/1980154 2.650 0.000 6.942 0.000 graph-depends:262(is_dep_uncached)
1 0.000 0.000 4.529 4.529 graph-depends:121(get_targets)
2883343 1.123 0.000 1.851 0.000 graph-depends:246(is_dep_cache_insert)
9784086 1.783 0.000 1.783 0.000 graph-depends:255(is_dep_cache_lookup)
2881580 0.728 0.000 0.728 0.000 {method 'update' of 'dict' objects}
1 0.001 0.001 0.405 0.405 graph-depends:311(check_circular_deps)
12264/1717 0.290 0.000 0.404 0.000 graph-depends:312(recurse)
[...]
real 1m27.371s
user 1m15.075s
sys 0m12.673s
The cumulative time spent in check_circular_deps is just below 0.5s,
which is largely less than 1% of the total run time.
Signed-off-by: "Yann E. MORIN" <yann.morin.1998@free.fr>
Cc: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
Cc: Samuel Martin <s.martin49@gmail.com>
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
2016-02-07 22:34:27 +01:00
|
|
|
# This function will check that there is no loop in the dependency chain
|
|
|
|
# As a side effect, it builds up the dependency cache.
|
|
|
|
def check_circular_deps(deps):
|
|
|
|
def recurse(pkg):
|
2018-01-22 01:44:29 +01:00
|
|
|
if pkg not in list(deps.keys()):
|
support/graph-depends: detect circular dependencies
Currently, if there is a circular dependency in the packages, the
graph-depends script just errors out with a Python RuntimeError which is
not caught, resulting in a very-long backtrace which does not provide
any hint as what the real issue is (even if "RuntimeError: maximum
recursion depth exceeded" is a pretty good hint at it).
We fix that by recursing the dependency chain of each package, until we
either end up with a package with no dependency, or with a package
already seen along the current dependency chain.
We need to introduce a new function, check_circular_deps(), because we
can't re-use the existing ones:
- remove_mandatory_deps() does not iterate,
- remove_transitive_deps() does iterate, but we do not call it for the
top-level package if it is not 'all'
- it does not make sense to use those functions anyway, as they were
not designed to _check_ but to _act_ on the dependency chain.
Since we've had time-related issues in the past, we do not want to
introduce yet another time-hog, so here are timings with the circular
dependency check:
$ time python -m cProfile -s cumtime support/scripts/graph-depends
[...]
28352654 function calls (20323050 primitive calls) in 87.292 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.012 0.012 87.292 87.292 graph-depends:24(<module>)
21 0.000 0.000 73.685 3.509 subprocess.py:473(_eintr_retry_call)
7 0.000 0.000 73.655 10.522 subprocess.py:768(communicate)
7 73.653 10.522 73.653 10.522 {method 'read' of 'file' objects}
5/1 0.027 0.005 43.488 43.488 graph-depends:164(get_all_depends)
5 0.003 0.001 43.458 8.692 graph-depends:135(get_depends)
1 0.001 0.001 25.712 25.712 graph-depends:98(get_version)
1 0.001 0.001 13.457 13.457 graph-depends:337(remove_extra_deps)
1717 1.672 0.001 13.050 0.008 graph-depends:290(remove_transitive_deps)
9784086/2672326 5.079 0.000 11.363 0.000 graph-depends:274(is_dep)
2883343/1980154 2.650 0.000 6.942 0.000 graph-depends:262(is_dep_uncached)
1 0.000 0.000 4.529 4.529 graph-depends:121(get_targets)
2883343 1.123 0.000 1.851 0.000 graph-depends:246(is_dep_cache_insert)
9784086 1.783 0.000 1.783 0.000 graph-depends:255(is_dep_cache_lookup)
2881580 0.728 0.000 0.728 0.000 {method 'update' of 'dict' objects}
1 0.001 0.001 0.405 0.405 graph-depends:311(check_circular_deps)
12264/1717 0.290 0.000 0.404 0.000 graph-depends:312(recurse)
[...]
real 1m27.371s
user 1m15.075s
sys 0m12.673s
The cumulative time spent in check_circular_deps is just below 0.5s,
which is largely less than 1% of the total run time.
Signed-off-by: "Yann E. MORIN" <yann.morin.1998@free.fr>
Cc: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
Cc: Samuel Martin <s.martin49@gmail.com>
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
2016-02-07 22:34:27 +01:00
|
|
|
return
|
|
|
|
if pkg in not_loop:
|
|
|
|
return
|
|
|
|
not_loop.append(pkg)
|
|
|
|
chain.append(pkg)
|
|
|
|
for p in deps[pkg]:
|
|
|
|
if p in chain:
|
|
|
|
sys.stderr.write("\nRecursion detected for : %s\n" % (p))
|
|
|
|
while True:
|
|
|
|
_p = chain.pop()
|
|
|
|
sys.stderr.write("which is a dependency of: %s\n" % (_p))
|
|
|
|
if p == _p:
|
|
|
|
sys.exit(1)
|
|
|
|
recurse(p)
|
|
|
|
chain.pop()
|
|
|
|
|
|
|
|
not_loop = []
|
|
|
|
chain = []
|
|
|
|
for pkg in list(deps.keys()):
|
|
|
|
recurse(pkg)
|
|
|
|
|
2018-01-22 01:44:29 +01: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':
|
2018-01-22 01:44:29 +01: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':
|
2018-01-22 01:44:29 +01:00
|
|
|
deps[pkg] = remove_transitive_deps(pkg, deps)
|
2014-06-08 16:03:46 +02:00
|
|
|
return deps
|
|
|
|
|
2018-01-22 01:44:29 +01:00
|
|
|
|
support/graph-depends: detect circular dependencies
Currently, if there is a circular dependency in the packages, the
graph-depends script just errors out with a Python RuntimeError which is
not caught, resulting in a very-long backtrace which does not provide
any hint as what the real issue is (even if "RuntimeError: maximum
recursion depth exceeded" is a pretty good hint at it).
We fix that by recursing the dependency chain of each package, until we
either end up with a package with no dependency, or with a package
already seen along the current dependency chain.
We need to introduce a new function, check_circular_deps(), because we
can't re-use the existing ones:
- remove_mandatory_deps() does not iterate,
- remove_transitive_deps() does iterate, but we do not call it for the
top-level package if it is not 'all'
- it does not make sense to use those functions anyway, as they were
not designed to _check_ but to _act_ on the dependency chain.
Since we've had time-related issues in the past, we do not want to
introduce yet another time-hog, so here are timings with the circular
dependency check:
$ time python -m cProfile -s cumtime support/scripts/graph-depends
[...]
28352654 function calls (20323050 primitive calls) in 87.292 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.012 0.012 87.292 87.292 graph-depends:24(<module>)
21 0.000 0.000 73.685 3.509 subprocess.py:473(_eintr_retry_call)
7 0.000 0.000 73.655 10.522 subprocess.py:768(communicate)
7 73.653 10.522 73.653 10.522 {method 'read' of 'file' objects}
5/1 0.027 0.005 43.488 43.488 graph-depends:164(get_all_depends)
5 0.003 0.001 43.458 8.692 graph-depends:135(get_depends)
1 0.001 0.001 25.712 25.712 graph-depends:98(get_version)
1 0.001 0.001 13.457 13.457 graph-depends:337(remove_extra_deps)
1717 1.672 0.001 13.050 0.008 graph-depends:290(remove_transitive_deps)
9784086/2672326 5.079 0.000 11.363 0.000 graph-depends:274(is_dep)
2883343/1980154 2.650 0.000 6.942 0.000 graph-depends:262(is_dep_uncached)
1 0.000 0.000 4.529 4.529 graph-depends:121(get_targets)
2883343 1.123 0.000 1.851 0.000 graph-depends:246(is_dep_cache_insert)
9784086 1.783 0.000 1.783 0.000 graph-depends:255(is_dep_cache_lookup)
2881580 0.728 0.000 0.728 0.000 {method 'update' of 'dict' objects}
1 0.001 0.001 0.405 0.405 graph-depends:311(check_circular_deps)
12264/1717 0.290 0.000 0.404 0.000 graph-depends:312(recurse)
[...]
real 1m27.371s
user 1m15.075s
sys 0m12.673s
The cumulative time spent in check_circular_deps is just below 0.5s,
which is largely less than 1% of the total run time.
Signed-off-by: "Yann E. MORIN" <yann.morin.1998@free.fr>
Cc: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
Cc: Samuel Martin <s.martin49@gmail.com>
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
2016-02-07 22:34:27 +01:00
|
|
|
check_circular_deps(dict_deps)
|
2016-02-07 22:34:28 +01:00
|
|
|
if check_only:
|
|
|
|
sys.exit(0)
|
|
|
|
|
2014-06-08 16:03:46 +02:00
|
|
|
dict_deps = remove_extra_deps(dict_deps)
|
2017-03-21 09:22:33 +01:00
|
|
|
dict_version = brpkgutil.get_version([pkg for pkg in allpkgs
|
2018-01-22 01:44:29 +01:00
|
|
|
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') \
|
2018-01-22 01:44:29 +01:00
|
|
|
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":
|
2016-02-07 22:34:25 +01:00
|
|
|
outfile.write("%s [label = <<I>%s</I>>]\n" % (name, label))
|
2015-01-03 15:29:12 +01:00
|
|
|
else:
|
2016-02-07 22:34:25 +01:00
|
|
|
outfile.write("%s [label = \"%s\"]\n" % (name, label))
|
|
|
|
outfile.write("%s [color=%s,style=filled]\n" % (name, color))
|
2010-05-06 10:09:14 +02:00
|
|
|
|
2018-01-22 01:44:29 +01: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
|
2016-01-27 21:32:14 +01:00
|
|
|
if pkg.startswith("host-") and "host" 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]:
|
2016-01-27 21:32:13 +01:00
|
|
|
if dict_version.get(d) == "virtual" \
|
|
|
|
and "virtual" in exclude_list:
|
|
|
|
continue
|
2016-01-27 21:32:14 +01:00
|
|
|
if d.startswith("host-") \
|
|
|
|
and "host" in exclude_list:
|
|
|
|
continue
|
2015-03-24 23:16:50 +01:00
|
|
|
add = True
|
|
|
|
for p in exclude_list:
|
2018-01-22 01:44:29 +01:00
|
|
|
if fnmatch(d, p):
|
2015-03-24 23:16:50 +01:00
|
|
|
add = False
|
|
|
|
break
|
|
|
|
if add:
|
2016-10-23 19:19:44 +02:00
|
|
|
outfile.write("%s -> %s [dir=%s]\n" % (pkg_node_name(pkg), pkg_node_name(d), arrow_dir))
|
2018-01-22 01:44:29 +01:00
|
|
|
print_pkg_deps(depth + 1, d)
|
|
|
|
|
2014-04-13 22:42:39 +02:00
|
|
|
|
|
|
|
# Start printing the graph data
|
2016-02-07 22:34:25 +01:00
|
|
|
outfile.write("digraph G {\n")
|
2014-04-13 22:42:39 +02:00
|
|
|
|
|
|
|
done_deps = []
|
|
|
|
print_pkg_deps(0, rootpkg)
|
|
|
|
|
2016-02-07 22:34:25 +01:00
|
|
|
outfile.write("}\n")
|