control.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. # Copyright (c) 2013 The Chromium OS Authors.
  2. #
  3. # SPDX-License-Identifier: GPL-2.0+
  4. #
  5. import multiprocessing
  6. import os
  7. import sys
  8. import board
  9. import bsettings
  10. from builder import Builder
  11. import gitutil
  12. import patchstream
  13. import terminal
  14. import toolchain
  15. import command
  16. import subprocess
  17. def GetPlural(count):
  18. """Returns a plural 's' if count is not 1"""
  19. return 's' if count != 1 else ''
  20. def GetActionSummary(is_summary, commits, selected, options):
  21. """Return a string summarising the intended action.
  22. Returns:
  23. Summary string.
  24. """
  25. if commits:
  26. count = len(commits)
  27. count = (count + options.step - 1) / options.step
  28. commit_str = '%d commit%s' % (count, GetPlural(count))
  29. else:
  30. commit_str = 'current source'
  31. str = '%s %s for %d boards' % (
  32. 'Summary of' if is_summary else 'Building', commit_str,
  33. len(selected))
  34. str += ' (%d thread%s, %d job%s per thread)' % (options.threads,
  35. GetPlural(options.threads), options.jobs, GetPlural(options.jobs))
  36. return str
  37. def ShowActions(series, why_selected, boards_selected, builder, options):
  38. """Display a list of actions that we would take, if not a dry run.
  39. Args:
  40. series: Series object
  41. why_selected: Dictionary where each key is a buildman argument
  42. provided by the user, and the value is the boards brought
  43. in by that argument. For example, 'arm' might bring in
  44. 400 boards, so in this case the key would be 'arm' and
  45. the value would be a list of board names.
  46. boards_selected: Dict of selected boards, key is target name,
  47. value is Board object
  48. builder: The builder that will be used to build the commits
  49. options: Command line options object
  50. """
  51. col = terminal.Color()
  52. print 'Dry run, so not doing much. But I would do this:'
  53. print
  54. if series:
  55. commits = series.commits
  56. else:
  57. commits = None
  58. print GetActionSummary(False, commits, boards_selected,
  59. options)
  60. print 'Build directory: %s' % builder.base_dir
  61. if commits:
  62. for upto in range(0, len(series.commits), options.step):
  63. commit = series.commits[upto]
  64. print ' ', col.Color(col.YELLOW, commit.hash, bright=False),
  65. print commit.subject
  66. print
  67. for arg in why_selected:
  68. if arg != 'all':
  69. print arg, ': %d boards' % why_selected[arg]
  70. print ('Total boards to build for each commit: %d\n' %
  71. why_selected['all'])
  72. def DoBuildman(options, args):
  73. """The main control code for buildman
  74. Args:
  75. options: Command line options object
  76. args: Command line arguments (list of strings)
  77. """
  78. gitutil.Setup()
  79. bsettings.Setup()
  80. options.git_dir = os.path.join(options.git, '.git')
  81. toolchains = toolchain.Toolchains()
  82. toolchains.Scan(options.list_tool_chains)
  83. if options.list_tool_chains:
  84. toolchains.List()
  85. print
  86. return
  87. # Work out how many commits to build. We want to build everything on the
  88. # branch. We also build the upstream commit as a control so we can see
  89. # problems introduced by the first commit on the branch.
  90. col = terminal.Color()
  91. count = options.count
  92. if count == -1:
  93. if not options.branch:
  94. count = 1
  95. else:
  96. count = gitutil.CountCommitsInBranch(options.git_dir,
  97. options.branch)
  98. if count is None:
  99. str = ("Branch '%s' not found or has no upstream" %
  100. options.branch)
  101. print col.Color(col.RED, str)
  102. sys.exit(1)
  103. count += 1 # Build upstream commit also
  104. if not count:
  105. str = ("No commits found to process in branch '%s': "
  106. "set branch's upstream or use -c flag" % options.branch)
  107. print col.Color(col.RED, str)
  108. sys.exit(1)
  109. # Work out what subset of the boards we are building
  110. board_file = os.path.join(options.git, 'boards.cfg')
  111. if not os.path.exists(board_file):
  112. print 'Could not find %s' % board_file
  113. status = subprocess.call([os.path.join(options.git,
  114. 'tools/genboardscfg.py')])
  115. if status != 0:
  116. print >> sys.stderr, "Failed to generate boards.cfg"
  117. sys.exit(1)
  118. boards = board.Boards()
  119. boards.ReadBoards(os.path.join(options.git, 'boards.cfg'))
  120. why_selected = boards.SelectBoards(args)
  121. selected = boards.GetSelected()
  122. if not len(selected):
  123. print col.Color(col.RED, 'No matching boards found')
  124. sys.exit(1)
  125. # Read the metadata from the commits. First look at the upstream commit,
  126. # then the ones in the branch. We would like to do something like
  127. # upstream/master~..branch but that isn't possible if upstream/master is
  128. # a merge commit (it will list all the commits that form part of the
  129. # merge)
  130. if options.branch:
  131. range_expr = gitutil.GetRangeInBranch(options.git_dir, options.branch)
  132. upstream_commit = gitutil.GetUpstream(options.git_dir, options.branch)
  133. series = patchstream.GetMetaDataForList(upstream_commit,
  134. options.git_dir, 1)
  135. # Conflicting tags are not a problem for buildman, since it does not
  136. # use them. For example, Series-version is not useful for buildman. On
  137. # the other hand conflicting tags will cause an error. So allow later
  138. # tags to overwrite earlier ones.
  139. series.allow_overwrite = True
  140. series = patchstream.GetMetaDataForList(range_expr, options.git_dir, None,
  141. series)
  142. else:
  143. series = None
  144. # By default we have one thread per CPU. But if there are not enough jobs
  145. # we can have fewer threads and use a high '-j' value for make.
  146. if not options.threads:
  147. options.threads = min(multiprocessing.cpu_count(), len(selected))
  148. if not options.jobs:
  149. options.jobs = max(1, (multiprocessing.cpu_count() +
  150. len(selected) - 1) / len(selected))
  151. if not options.step:
  152. options.step = len(series.commits) - 1
  153. gnu_make = command.Output(os.path.join(options.git,
  154. 'scripts/show-gnu-make')).rstrip()
  155. if not gnu_make:
  156. print >> sys.stderr, 'GNU Make not found'
  157. sys.exit(1)
  158. # Create a new builder with the selected options
  159. if options.branch:
  160. dirname = options.branch
  161. else:
  162. dirname = 'current'
  163. output_dir = os.path.join(options.output_dir, dirname)
  164. builder = Builder(toolchains, output_dir, options.git_dir,
  165. options.threads, options.jobs, gnu_make=gnu_make, checkout=True,
  166. show_unknown=options.show_unknown, step=options.step)
  167. builder.force_config_on_failure = not options.quick
  168. # For a dry run, just show our actions as a sanity check
  169. if options.dry_run:
  170. ShowActions(series, why_selected, selected, builder, options)
  171. else:
  172. builder.force_build = options.force_build
  173. builder.force_build_failures = options.force_build_failures
  174. builder.force_reconfig = options.force_reconfig
  175. builder.in_tree = options.in_tree
  176. # Work out which boards to build
  177. board_selected = boards.GetSelectedDict()
  178. if series:
  179. commits = series.commits
  180. else:
  181. commits = None
  182. print GetActionSummary(options.summary, commits, board_selected,
  183. options)
  184. if options.summary:
  185. # We can't show function sizes without board details at present
  186. if options.show_bloat:
  187. options.show_detail = True
  188. builder.ShowSummary(commits, board_selected,
  189. options.show_errors, options.show_sizes,
  190. options.show_detail, options.show_bloat)
  191. else:
  192. builder.BuildBoards(commits, board_selected,
  193. options.show_errors, options.keep_outputs)