graph-depends 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. # * The X.org package definitions are only included when
  20. # BR2_PACKAGE_XORG7 is enabled, so if this option is not enabled,
  21. # it isn't possible to graph the dependencies of X.org stack
  22. # components.
  23. #
  24. # Copyright (C) 2010 Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  25. import sys
  26. import subprocess
  27. # In FULL_MODE, we draw the full dependency graph for all selected
  28. # packages
  29. FULL_MODE = 1
  30. # In PKG_MODE, we only draw the dependency graph for a given package
  31. PKG_MODE = 2
  32. mode = 0
  33. if len(sys.argv) == 1:
  34. mode = FULL_MODE
  35. elif len(sys.argv) == 2:
  36. mode = PKG_MODE
  37. rootpkg = sys.argv[1]
  38. else:
  39. print "Usage: graph-depends [package-name]"
  40. sys.exit(1)
  41. allpkgs = []
  42. unknownpkgs = []
  43. # Execute the "make show-targets" command to get the list of the main
  44. # Buildroot TARGETS and return it formatted as a Python list. This
  45. # list is used as the starting point for full dependency graphs
  46. def get_targets():
  47. sys.stderr.write("Getting targets\n")
  48. cmd = ["make", "-s", "show-targets"]
  49. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  50. output = p.communicate()[0].strip()
  51. if p.returncode != 0:
  52. return None
  53. if output == '':
  54. return []
  55. return output.split(' ')
  56. # Execute the "make <pkg>-show-depends" command to get the list of
  57. # dependencies of a given package, and return the list of dependencies
  58. # formatted as a Python list.
  59. def get_depends(pkg):
  60. sys.stderr.write("Getting dependencies for %s\n" % pkg)
  61. cmd = ["make", "-s", "%s-show-depends" % pkg]
  62. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  63. output = p.communicate()[0].strip()
  64. if p.returncode != 0:
  65. return None
  66. if output == '':
  67. return []
  68. return output.split(' ')
  69. # Recursive function that builds the tree of dependencies for a given
  70. # package. The dependencies are built in a list called 'dependencies',
  71. # which contains tuples of the form (pkg1 ->
  72. # pkg2_on_which_pkg1_depends) and the function finally returns this
  73. # list.
  74. def get_all_depends(pkg):
  75. dependencies = []
  76. # We already have the dependencies for this package
  77. if pkg in allpkgs:
  78. return
  79. allpkgs.append(pkg)
  80. depends = get_depends(pkg)
  81. # We couldn't get the dependencies of this package, because it
  82. # doesn't use the generic or autotools infrastructure. Add it to
  83. # unknownpkgs so that it is later rendered in red color to warn
  84. # the user.
  85. if depends == None:
  86. unknownpkgs.append(pkg)
  87. return
  88. # This package has no dependency.
  89. if depends == []:
  90. return
  91. # Add dependencies to the list of dependencies
  92. for dep in depends:
  93. dependencies.append((pkg, dep))
  94. # Recurse into the dependencies
  95. for dep in depends:
  96. newdeps = get_all_depends(dep)
  97. if newdeps != None:
  98. dependencies += newdeps
  99. return dependencies
  100. # The Graphviz "dot" utility doesn't like dashes in node names. So for
  101. # node names, we strip all dashes.
  102. def pkg_node_name(pkg):
  103. return pkg.replace("-","")
  104. # Helper function for remove_redundant_deps(). This function tells
  105. # whether package "pkg" is the dependency of another package that is
  106. # not the special "all" package.
  107. def has_redundant_deps(deps, pkg):
  108. for dep in deps:
  109. if dep[0] != "all" and dep[1] == pkg:
  110. return True
  111. return False
  112. # This function removes redundant dependencies of the special "all"
  113. # package. This "all" package is created to reflect the origin of the
  114. # selection for all packages that are not themselves selected by any
  115. # other package. So for example if you enable libpng, zlib is enabled
  116. # as a dependency. But zlib being selected by libpng, it also appears
  117. # as a dependency of the "all" package. This needlessly complicates
  118. # the generated dependency graph. So when we have the dependency list
  119. # (all -> zlib, all -> libpn, libpng -> zlib), we get rid of the 'all
  120. # -> zlib' dependency, because zlib is already a dependency of a
  121. # regular package.
  122. def remove_redundant_deps(deps):
  123. newdeps = []
  124. for dep in deps:
  125. if dep[0] != "all":
  126. newdeps.append(dep)
  127. continue
  128. if not has_redundant_deps(deps, dep[1]):
  129. newdeps.append(dep)
  130. continue
  131. sys.stderr.write("Removing redundant dep all -> %s\n" % dep[1])
  132. return newdeps
  133. # In full mode, start with the result of get_targets() to get the main
  134. # targets and then use get_all_depends() for each individual target.
  135. if mode == FULL_MODE:
  136. targets = get_targets()
  137. dependencies = []
  138. allpkgs.append('all')
  139. for tg in targets:
  140. # Skip uninteresting targets
  141. if tg == 'target-generic-issue' or \
  142. tg == 'target-generic-getty-busybox' or \
  143. tg == 'target-generic-do-remount-rw' or \
  144. tg == 'target-finalize' or \
  145. tg == 'erase-fakeroots' or \
  146. tg == 'target-generic-hostname':
  147. continue
  148. dependencies.append(('all', tg))
  149. deps = get_all_depends(tg)
  150. if deps != None:
  151. dependencies += deps
  152. # In pkg mode, start directly with get_all_depends() on the requested
  153. # package
  154. elif mode == PKG_MODE:
  155. dependencies = get_all_depends(rootpkg)
  156. dependencies = remove_redundant_deps(dependencies)
  157. # Start printing the graph data
  158. print "digraph G {"
  159. # First, the dependencies. Usage of set allows to remove duplicated
  160. # dependencies in the graph
  161. for dep in set(dependencies):
  162. print "%s -> %s" % (pkg_node_name(dep[0]), pkg_node_name(dep[1]))
  163. # Then, the node attributes: color, style and label.
  164. for pkg in allpkgs:
  165. if pkg == 'all':
  166. print "all [label = \"ALL\"]"
  167. print "all [color=lightblue,style=filled]"
  168. continue
  169. print "%s [label = \"%s\"]" % (pkg_node_name(pkg), pkg)
  170. if pkg in unknownpkgs:
  171. print "%s [color=red,style=filled]" % pkg_node_name(pkg)
  172. elif mode == PKG_MODE and pkg == rootpkg:
  173. print "%s [color=lightblue,style=filled]" % pkg_node_name(rootpkg)
  174. else:
  175. print "%s [color=grey,style=filled]" % pkg_node_name(pkg)
  176. print "}"