control.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # Copyright (c) 2013 The Chromium OS Authors.
  2. #
  3. # See file CREDITS for list of people who contributed to this
  4. # project.
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License as
  8. # published by the Free Software Foundation; either version 2 of
  9. # the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. # MA 02111-1307 USA
  20. #
  21. import multiprocessing
  22. import os
  23. import sys
  24. import board
  25. import bsettings
  26. from builder import Builder
  27. import gitutil
  28. import patchstream
  29. import terminal
  30. import toolchain
  31. def GetPlural(count):
  32. """Returns a plural 's' if count is not 1"""
  33. return 's' if count != 1 else ''
  34. def GetActionSummary(is_summary, count, selected, options):
  35. """Return a string summarising the intended action.
  36. Returns:
  37. Summary string.
  38. """
  39. count = (count + options.step - 1) / options.step
  40. str = '%s %d commit%s for %d boards' % (
  41. 'Summary of' if is_summary else 'Building', count, GetPlural(count),
  42. len(selected))
  43. str += ' (%d thread%s, %d job%s per thread)' % (options.threads,
  44. GetPlural(options.threads), options.jobs, GetPlural(options.jobs))
  45. return str
  46. def ShowActions(series, why_selected, boards_selected, builder, options):
  47. """Display a list of actions that we would take, if not a dry run.
  48. Args:
  49. series: Series object
  50. why_selected: Dictionary where each key is a buildman argument
  51. provided by the user, and the value is the boards brought
  52. in by that argument. For example, 'arm' might bring in
  53. 400 boards, so in this case the key would be 'arm' and
  54. the value would be a list of board names.
  55. boards_selected: Dict of selected boards, key is target name,
  56. value is Board object
  57. builder: The builder that will be used to build the commits
  58. options: Command line options object
  59. """
  60. col = terminal.Color()
  61. print 'Dry run, so not doing much. But I would do this:'
  62. print
  63. print GetActionSummary(False, len(series.commits), boards_selected,
  64. options)
  65. print 'Build directory: %s' % builder.base_dir
  66. for upto in range(0, len(series.commits), options.step):
  67. commit = series.commits[upto]
  68. print ' ', col.Color(col.YELLOW, commit.hash, bright=False),
  69. print commit.subject
  70. print
  71. for arg in why_selected:
  72. if arg != 'all':
  73. print arg, ': %d boards' % why_selected[arg]
  74. print ('Total boards to build for each commit: %d\n' %
  75. why_selected['all'])
  76. def DoBuildman(options, args):
  77. """The main control code for buildman
  78. Args:
  79. options: Command line options object
  80. args: Command line arguments (list of strings)
  81. """
  82. gitutil.Setup()
  83. bsettings.Setup()
  84. options.git_dir = os.path.join(options.git, '.git')
  85. toolchains = toolchain.Toolchains()
  86. toolchains.Scan(options.list_tool_chains)
  87. if options.list_tool_chains:
  88. toolchains.List()
  89. print
  90. return
  91. # Work out how many commits to build. We want to build everything on the
  92. # branch. We also build the upstream commit as a control so we can see
  93. # problems introduced by the first commit on the branch.
  94. col = terminal.Color()
  95. count = options.count
  96. if count == -1:
  97. if not options.branch:
  98. str = 'Please use -b to specify a branch to build'
  99. print col.Color(col.RED, str)
  100. sys.exit(1)
  101. count = gitutil.CountCommitsInBranch(options.git_dir, options.branch)
  102. count += 1 # Build upstream commit also
  103. if not count:
  104. str = ("No commits found to process in branch '%s': "
  105. "set branch's upstream or use -c flag" % options.branch)
  106. print col.Color(col.RED, str)
  107. sys.exit(1)
  108. # Work out what subset of the boards we are building
  109. boards = board.Boards()
  110. boards.ReadBoards(os.path.join(options.git, 'boards.cfg'))
  111. why_selected = boards.SelectBoards(args)
  112. selected = boards.GetSelected()
  113. if not len(selected):
  114. print col.Color(col.RED, 'No matching boards found')
  115. sys.exit(1)
  116. # Read the metadata from the commits. First look at the upstream commit,
  117. # then the ones in the branch. We would like to do something like
  118. # upstream/master~..branch but that isn't possible if upstream/master is
  119. # a merge commit (it will list all the commits that form part of the
  120. # merge)
  121. range_expr = gitutil.GetRangeInBranch(options.git_dir, options.branch)
  122. upstream_commit = gitutil.GetUpstream(options.git_dir, options.branch)
  123. series = patchstream.GetMetaDataForList(upstream_commit, options.git_dir,
  124. 1)
  125. series = patchstream.GetMetaDataForList(range_expr, options.git_dir, None,
  126. series)
  127. # By default we have one thread per CPU. But if there are not enough jobs
  128. # we can have fewer threads and use a high '-j' value for make.
  129. if not options.threads:
  130. options.threads = min(multiprocessing.cpu_count(), len(selected))
  131. if not options.jobs:
  132. options.jobs = max(1, (multiprocessing.cpu_count() +
  133. len(selected) - 1) / len(selected))
  134. if not options.step:
  135. options.step = len(series.commits) - 1
  136. # Create a new builder with the selected options
  137. output_dir = os.path.join('..', options.branch)
  138. builder = Builder(toolchains, output_dir, options.git_dir,
  139. options.threads, options.jobs, checkout=True,
  140. show_unknown=options.show_unknown, step=options.step)
  141. builder.force_config_on_failure = not options.quick
  142. # For a dry run, just show our actions as a sanity check
  143. if options.dry_run:
  144. ShowActions(series, why_selected, selected, builder, options)
  145. else:
  146. builder.force_build = options.force_build
  147. # Work out which boards to build
  148. board_selected = boards.GetSelectedDict()
  149. print GetActionSummary(options.summary, count, board_selected, options)
  150. if options.summary:
  151. # We can't show function sizes without board details at present
  152. if options.show_bloat:
  153. options.show_detail = True
  154. builder.ShowSummary(series.commits, board_selected,
  155. options.show_errors, options.show_sizes,
  156. options.show_detail, options.show_bloat)
  157. else:
  158. builder.BuildBoards(series.commits, board_selected,
  159. options.show_errors, options.keep_outputs)