control.py 6.6 KB

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