control.py 12 KB

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