control.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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(options.config_file)
  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 0
  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. sys.exit(col.Color(col.RED, str))
  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. sys.exit(col.Color(col.RED, str))
  107. # Work out what subset of the boards we are building
  108. board_file = os.path.join(options.git, 'boards.cfg')
  109. status = subprocess.call([os.path.join(options.git,
  110. 'tools/genboardscfg.py')])
  111. if status != 0:
  112. sys.exit("Failed to generate boards.cfg")
  113. boards = board.Boards()
  114. boards.ReadBoards(os.path.join(options.git, 'boards.cfg'))
  115. exclude = []
  116. if options.exclude:
  117. for arg in options.exclude:
  118. exclude += arg.split(',')
  119. why_selected = boards.SelectBoards(args, exclude)
  120. selected = boards.GetSelected()
  121. if not len(selected):
  122. sys.exit(col.Color(col.RED, 'No matching boards found'))
  123. # Read the metadata from the commits. First look at the upstream commit,
  124. # then the ones in the branch. We would like to do something like
  125. # upstream/master~..branch but that isn't possible if upstream/master is
  126. # a merge commit (it will list all the commits that form part of the
  127. # merge)
  128. if options.branch:
  129. if count == -1:
  130. range_expr = gitutil.GetRangeInBranch(options.git_dir,
  131. options.branch)
  132. upstream_commit = gitutil.GetUpstream(options.git_dir,
  133. options.branch)
  134. series = patchstream.GetMetaDataForList(upstream_commit,
  135. options.git_dir, 1)
  136. # Conflicting tags are not a problem for buildman, since it does
  137. # not use them. For example, Series-version is not useful for
  138. # buildman. On the other hand conflicting tags will cause an
  139. # error. So allow later tags to overwrite earlier ones.
  140. series.allow_overwrite = True
  141. series = patchstream.GetMetaDataForList(range_expr,
  142. options.git_dir, None, series)
  143. else:
  144. # Honour the count
  145. series = patchstream.GetMetaDataForList(options.branch,
  146. options.git_dir, count)
  147. else:
  148. series = None
  149. options.verbose = True
  150. options.show_errors = True
  151. # By default we have one thread per CPU. But if there are not enough jobs
  152. # we can have fewer threads and use a high '-j' value for make.
  153. if not options.threads:
  154. options.threads = min(multiprocessing.cpu_count(), len(selected))
  155. if not options.jobs:
  156. options.jobs = max(1, (multiprocessing.cpu_count() +
  157. len(selected) - 1) / len(selected))
  158. if not options.step:
  159. options.step = len(series.commits) - 1
  160. gnu_make = command.Output(os.path.join(options.git,
  161. 'scripts/show-gnu-make')).rstrip()
  162. if not gnu_make:
  163. sys.exit('GNU Make not found')
  164. # Create a new builder with the selected options
  165. if options.branch:
  166. dirname = options.branch
  167. else:
  168. dirname = 'current'
  169. output_dir = os.path.join(options.output_dir, dirname)
  170. builder = Builder(toolchains, output_dir, options.git_dir,
  171. options.threads, options.jobs, gnu_make=gnu_make, checkout=True,
  172. show_unknown=options.show_unknown, step=options.step)
  173. builder.force_config_on_failure = not options.quick
  174. # For a dry run, just show our actions as a sanity check
  175. if options.dry_run:
  176. ShowActions(series, why_selected, selected, builder, options)
  177. else:
  178. builder.force_build = options.force_build
  179. builder.force_build_failures = options.force_build_failures
  180. builder.force_reconfig = options.force_reconfig
  181. builder.in_tree = options.in_tree
  182. # Work out which boards to build
  183. board_selected = boards.GetSelectedDict()
  184. if series:
  185. commits = series.commits
  186. else:
  187. commits = None
  188. print GetActionSummary(options.summary, commits, board_selected,
  189. options)
  190. builder.SetDisplayOptions(options.show_errors, options.show_sizes,
  191. options.show_detail, options.show_bloat,
  192. options.list_error_boards)
  193. if options.summary:
  194. # We can't show function sizes without board details at present
  195. if options.show_bloat:
  196. options.show_detail = True
  197. builder.ShowSummary(commits, board_selected)
  198. else:
  199. fail, warned = builder.BuildBoards(commits, board_selected,
  200. options.keep_outputs, options.verbose)
  201. if fail:
  202. return 128
  203. elif warned:
  204. return 129
  205. return 0