graph-depends 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #!/usr/bin/python
  2. # Usage (the graphviz package must be installed in your distribution)
  3. # ./support/scripts/graph-depends [-p package-name] > test.dot
  4. # dot -Tpdf test.dot -o test.pdf
  5. #
  6. # With no arguments, graph-depends will draw a complete graph of
  7. # dependencies for the current configuration.
  8. # If '-p <package-name>' is specified, graph-depends will draw a graph
  9. # of dependencies for the given package name.
  10. # If '-d <depth>' is specified, graph-depends will limit the depth of
  11. # the dependency graph to 'depth' levels.
  12. #
  13. # Limitations
  14. #
  15. # * Some packages have dependencies that depend on the Buildroot
  16. # configuration. For example, many packages have a dependency on
  17. # openssl if openssl has been enabled. This tool will graph the
  18. # dependencies as they are with the current Buildroot
  19. # configuration.
  20. #
  21. # Copyright (C) 2010-2013 Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  22. import sys
  23. import subprocess
  24. import argparse
  25. # In FULL_MODE, we draw the full dependency graph for all selected
  26. # packages
  27. FULL_MODE = 1
  28. # In PKG_MODE, we only draw the dependency graph for a given package
  29. PKG_MODE = 2
  30. mode = 0
  31. max_depth = 0
  32. parser = argparse.ArgumentParser(description="Graph pacakges dependencies")
  33. parser.add_argument("--package", '-p', metavar="PACKAGE",
  34. help="Graph the dependencies of PACKAGE")
  35. parser.add_argument("--depth", '-d', metavar="DEPTH",
  36. help="Limit the dependency graph to DEPTH levels")
  37. args = parser.parse_args()
  38. if args.package == None:
  39. mode = FULL_MODE
  40. else:
  41. mode = PKG_MODE
  42. rootpkg = args.package
  43. if args.depth != None:
  44. max_depth = int(args.depth)
  45. allpkgs = []
  46. # Execute the "make show-targets" command to get the list of the main
  47. # Buildroot TARGETS and return it formatted as a Python list. This
  48. # list is used as the starting point for full dependency graphs
  49. def get_targets():
  50. sys.stderr.write("Getting targets\n")
  51. cmd = ["make", "-s", "show-targets"]
  52. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  53. output = p.communicate()[0].strip()
  54. if p.returncode != 0:
  55. return None
  56. if output == '':
  57. return []
  58. return output.split(' ')
  59. # Execute the "make <pkg>-show-depends" command to get the list of
  60. # dependencies of a given list of packages, and return the list of
  61. # dependencies formatted as a Python dictionary.
  62. def get_depends(pkgs):
  63. sys.stderr.write("Getting dependencies for %s\n" % pkgs)
  64. cmd = ["make", "-s" ]
  65. for pkg in pkgs:
  66. cmd.append("%s-show-depends" % pkg)
  67. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  68. output = p.communicate()[0]
  69. if p.returncode != 0:
  70. sys.stderr.write("Error getting dependencies %s\n" % pkgs)
  71. sys.exit(1)
  72. output = output.split("\n")
  73. if len(output) != len(pkgs) + 1:
  74. sys.stderr.write("Error getting dependencies\n")
  75. sys.exit(1)
  76. deps = {}
  77. for i in range(0, len(pkgs)):
  78. pkg = pkgs[i]
  79. pkg_deps = output[i].split(" ")
  80. if pkg_deps == ['']:
  81. deps[pkg] = []
  82. else:
  83. deps[pkg] = pkg_deps
  84. return deps
  85. # Recursive function that builds the tree of dependencies for a given
  86. # list of packages. The dependencies are built in a list called
  87. # 'dependencies', which contains tuples of the form (pkg1 ->
  88. # pkg2_on_which_pkg1_depends, pkg3 -> pkg4_on_which_pkg3_depends) and
  89. # the function finally returns this list.
  90. def get_all_depends(pkgs):
  91. dependencies = []
  92. # Filter the packages for which we already have the dependencies
  93. filtered_pkgs = []
  94. for pkg in pkgs:
  95. if pkg in allpkgs:
  96. continue
  97. filtered_pkgs.append(pkg)
  98. allpkgs.append(pkg)
  99. if len(filtered_pkgs) == 0:
  100. return []
  101. depends = get_depends(filtered_pkgs)
  102. deps = set()
  103. for pkg in filtered_pkgs:
  104. pkg_deps = depends[pkg]
  105. # This package has no dependency.
  106. if pkg_deps == []:
  107. continue
  108. # Add dependencies to the list of dependencies
  109. for dep in pkg_deps:
  110. dependencies.append((pkg, dep))
  111. deps.add(dep)
  112. if len(deps) != 0:
  113. newdeps = get_all_depends(deps)
  114. if newdeps != None:
  115. dependencies += newdeps
  116. return dependencies
  117. # The Graphviz "dot" utility doesn't like dashes in node names. So for
  118. # node names, we strip all dashes.
  119. def pkg_node_name(pkg):
  120. return pkg.replace("-","")
  121. # Helper function for remove_redundant_deps(). This function tells
  122. # whether package "pkg" is the dependency of another package that is
  123. # not the special "all" package.
  124. def has_redundant_deps(deps, pkg):
  125. for dep in deps:
  126. if dep[0] != "all" and dep[1] == pkg:
  127. return True
  128. return False
  129. # This function removes redundant dependencies of the special "all"
  130. # package. This "all" package is created to reflect the origin of the
  131. # selection for all packages that are not themselves selected by any
  132. # other package. So for example if you enable libpng, zlib is enabled
  133. # as a dependency. But zlib being selected by libpng, it also appears
  134. # as a dependency of the "all" package. This needlessly complicates
  135. # the generated dependency graph. So when we have the dependency list
  136. # (all -> zlib, all -> libpn, libpng -> zlib), we get rid of the 'all
  137. # -> zlib' dependency, because zlib is already a dependency of a
  138. # regular package.
  139. def remove_redundant_deps(deps):
  140. newdeps = []
  141. for dep in deps:
  142. if dep[0] != "all":
  143. newdeps.append(dep)
  144. continue
  145. if not has_redundant_deps(deps, dep[1]):
  146. newdeps.append(dep)
  147. continue
  148. sys.stderr.write("Removing redundant dep all -> %s\n" % dep[1])
  149. return newdeps
  150. TARGET_EXCEPTIONS = [
  151. "target-generic-securetty",
  152. "target-generic-issue",
  153. "target-generic-getty-busybox",
  154. "target-generic-do-remount-rw",
  155. "target-generic-dont-remount-rw",
  156. "target-finalize",
  157. "erase-fakeroots",
  158. "target-generic-hostname",
  159. "target-root-passwd",
  160. "target-post-image",
  161. "target-purgelocales",
  162. ]
  163. # In full mode, start with the result of get_targets() to get the main
  164. # targets and then use get_all_depends() for all targets
  165. if mode == FULL_MODE:
  166. targets = get_targets()
  167. dependencies = []
  168. allpkgs.append('all')
  169. filtered_targets = []
  170. for tg in targets:
  171. # Skip uninteresting targets
  172. if tg in TARGET_EXCEPTIONS:
  173. continue
  174. dependencies.append(('all', tg))
  175. filtered_targets.append(tg)
  176. deps = get_all_depends(filtered_targets)
  177. if deps != None:
  178. dependencies += deps
  179. rootpkg = 'all'
  180. # In pkg mode, start directly with get_all_depends() on the requested
  181. # package
  182. elif mode == PKG_MODE:
  183. dependencies = get_all_depends([rootpkg])
  184. dependencies = remove_redundant_deps(dependencies)
  185. # Make the dependencies a dictionnary { 'pkg':[dep1, dep2, ...] }
  186. dict_deps = {}
  187. for dep in dependencies:
  188. if not dict_deps.has_key(dep[0]):
  189. dict_deps[dep[0]] = []
  190. dict_deps[dep[0]].append(dep[1])
  191. # Print the attributes of a node: label and fill-color
  192. def print_attrs(pkg):
  193. if pkg == 'all':
  194. print "all [label = \"ALL\"]"
  195. print "all [color=lightblue,style=filled]"
  196. return
  197. print "%s [label = \"%s\"]" % (pkg_node_name(pkg), pkg)
  198. if mode == PKG_MODE and pkg == rootpkg:
  199. print "%s [color=lightblue,style=filled]" % pkg_node_name(rootpkg)
  200. else:
  201. print "%s [color=grey,style=filled]" % pkg_node_name(pkg)
  202. # Print the dependency graph of a package
  203. def print_pkg_deps(depth, pkg):
  204. if pkg in done_deps:
  205. return
  206. done_deps.append(pkg)
  207. print_attrs(pkg)
  208. if not dict_deps.has_key(pkg):
  209. return
  210. if max_depth == 0 or depth < max_depth:
  211. for d in dict_deps[pkg]:
  212. print "%s -> %s" % (pkg_node_name(pkg), pkg_node_name(d))
  213. print_pkg_deps(depth+1, d)
  214. # Start printing the graph data
  215. print "digraph G {"
  216. done_deps = []
  217. print_pkg_deps(0, rootpkg)
  218. print "}"