control.py 7.4 KB

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