graph-depends 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. # In full mode, start with the result of get_targets() to get the main
  105. # targets and then use get_all_depends() for each individual target.
  106. if mode == FULL_MODE:
  107. targets = get_targets()
  108. dependencies = []
  109. allpkgs.append('all')
  110. for tg in targets:
  111. # Skip uninteresting targets
  112. if tg == 'target-generic-issue' or \
  113. tg == 'target-generic-getty-busybox' or \
  114. tg == 'target-generic-do-remount-rw' or \
  115. tg == 'target-finalize' or \
  116. tg == 'erase-fakeroots' or \
  117. tg == 'target-generic-hostname':
  118. continue
  119. dependencies.append(('all', tg))
  120. deps = get_all_depends(tg)
  121. if deps != None:
  122. dependencies += deps
  123. # In pkg mode, start directly with get_all_depends() on the requested
  124. # package
  125. elif mode == PKG_MODE:
  126. dependencies = get_all_depends(rootpkg)
  127. # Start printing the graph data
  128. print "digraph G {"
  129. # First, the dependencies. Usage of set allows to remove duplicated
  130. # dependencies in the graph
  131. for dep in set(dependencies):
  132. print "%s -> %s" % (pkg_node_name(dep[0]), pkg_node_name(dep[1]))
  133. # Then, the node attributes: color, style and label.
  134. for pkg in allpkgs:
  135. if pkg == 'all':
  136. print "all [label = \"ALL\"]"
  137. print "all [color=lightblue,style=filled]"
  138. continue
  139. print "%s [label = \"%s\"]" % (pkg_node_name(pkg), pkg)
  140. if pkg in unknownpkgs:
  141. print "%s [color=red,style=filled]" % pkg_node_name(pkg)
  142. elif mode == PKG_MODE and pkg == rootpkg:
  143. print "%s [color=lightblue,style=filled]" % pkg_node_name(rootpkg)
  144. else:
  145. print "%s [color=grey,style=filled]" % pkg_node_name(pkg)
  146. print "}"