control.py 9.3 KB

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