control.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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, boards=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. board: Boards() object to use, containing a list of available
  85. boards. If this is None it will be created and scanned.
  86. """
  87. if options.full_help:
  88. pager = os.getenv('PAGER')
  89. if not pager:
  90. pager = 'more'
  91. fname = os.path.join(os.path.dirname(sys.argv[0]), 'README')
  92. command.Run(pager, fname)
  93. return 0
  94. gitutil.Setup()
  95. options.git_dir = os.path.join(options.git, '.git')
  96. if not toolchains:
  97. toolchains = toolchain.Toolchains()
  98. toolchains.GetSettings()
  99. toolchains.Scan(options.list_tool_chains)
  100. if options.list_tool_chains:
  101. toolchains.List()
  102. print
  103. return 0
  104. # Work out how many commits to build. We want to build everything on the
  105. # branch. We also build the upstream commit as a control so we can see
  106. # problems introduced by the first commit on the branch.
  107. col = terminal.Color()
  108. count = options.count
  109. if count == -1:
  110. if not options.branch:
  111. count = 1
  112. else:
  113. count = gitutil.CountCommitsInBranch(options.git_dir,
  114. options.branch)
  115. if count is None:
  116. str = ("Branch '%s' not found or has no upstream" %
  117. options.branch)
  118. sys.exit(col.Color(col.RED, str))
  119. count += 1 # Build upstream commit also
  120. if not count:
  121. str = ("No commits found to process in branch '%s': "
  122. "set branch's upstream or use -c flag" % options.branch)
  123. sys.exit(col.Color(col.RED, str))
  124. # Work out what subset of the boards we are building
  125. if not boards:
  126. board_file = os.path.join(options.git, 'boards.cfg')
  127. status = subprocess.call([os.path.join(options.git,
  128. 'tools/genboardscfg.py')])
  129. if status != 0:
  130. sys.exit("Failed to generate boards.cfg")
  131. boards = board.Boards()
  132. boards.ReadBoards(os.path.join(options.git, 'boards.cfg'))
  133. exclude = []
  134. if options.exclude:
  135. for arg in options.exclude:
  136. exclude += arg.split(',')
  137. why_selected = boards.SelectBoards(args, exclude)
  138. selected = boards.GetSelected()
  139. if not len(selected):
  140. sys.exit(col.Color(col.RED, 'No matching boards found'))
  141. # Read the metadata from the commits. First look at the upstream commit,
  142. # then the ones in the branch. We would like to do something like
  143. # upstream/master~..branch but that isn't possible if upstream/master is
  144. # a merge commit (it will list all the commits that form part of the
  145. # merge)
  146. if options.branch:
  147. if count == -1:
  148. range_expr = gitutil.GetRangeInBranch(options.git_dir,
  149. options.branch)
  150. upstream_commit = gitutil.GetUpstream(options.git_dir,
  151. options.branch)
  152. series = patchstream.GetMetaDataForList(upstream_commit,
  153. options.git_dir, 1)
  154. # Conflicting tags are not a problem for buildman, since it does
  155. # not use them. For example, Series-version is not useful for
  156. # buildman. On the other hand conflicting tags will cause an
  157. # error. So allow later tags to overwrite earlier ones.
  158. series.allow_overwrite = True
  159. series = patchstream.GetMetaDataForList(range_expr,
  160. options.git_dir, None, series)
  161. else:
  162. # Honour the count
  163. series = patchstream.GetMetaDataForList(options.branch,
  164. options.git_dir, count)
  165. else:
  166. series = None
  167. options.verbose = True
  168. options.show_errors = True
  169. # By default we have one thread per CPU. But if there are not enough jobs
  170. # we can have fewer threads and use a high '-j' value for make.
  171. if not options.threads:
  172. options.threads = min(multiprocessing.cpu_count(), len(selected))
  173. if not options.jobs:
  174. options.jobs = max(1, (multiprocessing.cpu_count() +
  175. len(selected) - 1) / len(selected))
  176. if not options.step:
  177. options.step = len(series.commits) - 1
  178. gnu_make = command.Output(os.path.join(options.git,
  179. 'scripts/show-gnu-make')).rstrip()
  180. if not gnu_make:
  181. sys.exit('GNU Make not found')
  182. # Create a new builder with the selected options
  183. if options.branch:
  184. dirname = options.branch
  185. else:
  186. dirname = 'current'
  187. output_dir = os.path.join(options.output_dir, dirname)
  188. builder = Builder(toolchains, output_dir, options.git_dir,
  189. options.threads, options.jobs, gnu_make=gnu_make, checkout=True,
  190. show_unknown=options.show_unknown, step=options.step)
  191. builder.force_config_on_failure = not options.quick
  192. if make_func:
  193. builder.do_make = make_func
  194. # For a dry run, just show our actions as a sanity check
  195. if options.dry_run:
  196. ShowActions(series, why_selected, selected, builder, options)
  197. else:
  198. builder.force_build = options.force_build
  199. builder.force_build_failures = options.force_build_failures
  200. builder.force_reconfig = options.force_reconfig
  201. builder.in_tree = options.in_tree
  202. # Work out which boards to build
  203. board_selected = boards.GetSelectedDict()
  204. if series:
  205. commits = series.commits
  206. else:
  207. commits = None
  208. Print(GetActionSummary(options.summary, commits, board_selected,
  209. options))
  210. builder.SetDisplayOptions(options.show_errors, options.show_sizes,
  211. options.show_detail, options.show_bloat,
  212. options.list_error_boards)
  213. if options.summary:
  214. # We can't show function sizes without board details at present
  215. if options.show_bloat:
  216. options.show_detail = True
  217. builder.ShowSummary(commits, board_selected)
  218. else:
  219. fail, warned = builder.BuildBoards(commits, board_selected,
  220. options.keep_outputs, options.verbose)
  221. if fail:
  222. return 128
  223. elif warned:
  224. return 129
  225. return 0