graph-depends 6.9 KB

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