graph-depends 7.2 KB

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