control.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2013 The Chromium OS Authors.
  3. #
  4. import multiprocessing
  5. import os
  6. import shutil
  7. import subprocess
  8. import sys
  9. from buildman import board
  10. from buildman import bsettings
  11. from buildman import toolchain
  12. from buildman.builder import Builder
  13. from patman import command
  14. from patman import gitutil
  15. from patman import patchstream
  16. from patman import terminal
  17. from patman.terminal import Print
  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. board_warnings):
  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. board_warnings: List of warnings obtained from board selected
  53. """
  54. col = terminal.Color()
  55. print('Dry run, so not doing much. But I would do this:')
  56. print()
  57. if series:
  58. commits = series.commits
  59. else:
  60. commits = None
  61. print(GetActionSummary(False, commits, boards_selected,
  62. options))
  63. print('Build directory: %s' % builder.base_dir)
  64. if commits:
  65. for upto in range(0, len(series.commits), options.step):
  66. commit = series.commits[upto]
  67. print(' ', col.Color(col.YELLOW, commit.hash[:8], bright=False), end=' ')
  68. print(commit.subject)
  69. print()
  70. for arg in why_selected:
  71. if arg != 'all':
  72. print(arg, ': %d boards' % len(why_selected[arg]))
  73. if options.verbose:
  74. print(' %s' % ' '.join(why_selected[arg]))
  75. print(('Total boards to build for each commit: %d\n' %
  76. len(why_selected['all'])))
  77. if board_warnings:
  78. for warning in board_warnings:
  79. print(col.Color(col.YELLOW, warning))
  80. def ShowToolchainPrefix(boards, toolchains):
  81. """Show information about a the tool chain used by one or more boards
  82. The function checks that all boards use the same toolchain, then prints
  83. the correct value for CROSS_COMPILE.
  84. Args:
  85. boards: Boards object containing selected boards
  86. toolchains: Toolchains object containing available toolchains
  87. Return:
  88. None on success, string error message otherwise
  89. """
  90. boards = boards.GetSelectedDict()
  91. tc_set = set()
  92. for brd in boards.values():
  93. tc_set.add(toolchains.Select(brd.arch))
  94. if len(tc_set) != 1:
  95. return 'Supplied boards must share one toolchain'
  96. return False
  97. tc = tc_set.pop()
  98. print(tc.GetEnvArgs(toolchain.VAR_CROSS_COMPILE))
  99. return None
  100. def DoBuildman(options, args, toolchains=None, make_func=None, boards=None,
  101. clean_dir=False, test_thread_exceptions=False):
  102. """The main control code for buildman
  103. Args:
  104. options: Command line options object
  105. args: Command line arguments (list of strings)
  106. toolchains: Toolchains to use - this should be a Toolchains()
  107. object. If None, then it will be created and scanned
  108. make_func: Make function to use for the builder. This is called
  109. to execute 'make'. If this is None, the normal function
  110. will be used, which calls the 'make' tool with suitable
  111. arguments. This setting is useful for tests.
  112. board: Boards() object to use, containing a list of available
  113. boards. If this is None it will be created and scanned.
  114. clean_dir: Used for tests only, indicates that the existing output_dir
  115. should be removed before starting the build
  116. test_thread_exceptions: Uses for tests only, True to make the threads
  117. raise an exception instead of reporting their result. This simulates
  118. a failure in the code somewhere
  119. """
  120. global builder
  121. if options.full_help:
  122. pager = os.getenv('PAGER')
  123. if not pager:
  124. pager = 'more'
  125. fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
  126. 'README')
  127. command.Run(pager, fname)
  128. return 0
  129. gitutil.Setup()
  130. col = terminal.Color()
  131. options.git_dir = os.path.join(options.git, '.git')
  132. no_toolchains = toolchains is None
  133. if no_toolchains:
  134. toolchains = toolchain.Toolchains(options.override_toolchain)
  135. if options.fetch_arch:
  136. if options.fetch_arch == 'list':
  137. sorted_list = toolchains.ListArchs()
  138. print(col.Color(col.BLUE, 'Available architectures: %s\n' %
  139. ' '.join(sorted_list)))
  140. return 0
  141. else:
  142. fetch_arch = options.fetch_arch
  143. if fetch_arch == 'all':
  144. fetch_arch = ','.join(toolchains.ListArchs())
  145. print(col.Color(col.CYAN, '\nDownloading toolchains: %s' %
  146. fetch_arch))
  147. for arch in fetch_arch.split(','):
  148. print()
  149. ret = toolchains.FetchAndInstall(arch)
  150. if ret:
  151. return ret
  152. return 0
  153. if no_toolchains:
  154. toolchains.GetSettings()
  155. toolchains.Scan(options.list_tool_chains and options.verbose)
  156. if options.list_tool_chains:
  157. toolchains.List()
  158. print()
  159. return 0
  160. if options.incremental:
  161. print(col.Color(col.RED,
  162. 'Warning: -I has been removed. See documentation'))
  163. if not options.output_dir:
  164. if options.work_in_output:
  165. sys.exit(col.Color(col.RED, '-w requires that you specify -o'))
  166. options.output_dir = '..'
  167. # Work out what subset of the boards we are building
  168. if not boards:
  169. if not os.path.exists(options.output_dir):
  170. os.makedirs(options.output_dir)
  171. board_file = os.path.join(options.output_dir, 'boards.cfg')
  172. our_path = os.path.dirname(os.path.realpath(__file__))
  173. genboardscfg = os.path.join(our_path, '../genboardscfg.py')
  174. if not os.path.exists(genboardscfg):
  175. genboardscfg = os.path.join(options.git, 'tools/genboardscfg.py')
  176. status = subprocess.call([genboardscfg, '-q', '-o', board_file])
  177. if status != 0:
  178. # Older versions don't support -q
  179. status = subprocess.call([genboardscfg, '-o', board_file])
  180. if status != 0:
  181. sys.exit("Failed to generate boards.cfg")
  182. boards = board.Boards()
  183. boards.ReadBoards(board_file)
  184. exclude = []
  185. if options.exclude:
  186. for arg in options.exclude:
  187. exclude += arg.split(',')
  188. if options.boards:
  189. requested_boards = []
  190. for b in options.boards:
  191. requested_boards += b.split(',')
  192. else:
  193. requested_boards = None
  194. why_selected, board_warnings = boards.SelectBoards(args, exclude,
  195. requested_boards)
  196. selected = boards.GetSelected()
  197. if not len(selected):
  198. sys.exit(col.Color(col.RED, 'No matching boards found'))
  199. if options.print_prefix:
  200. err = ShowToolchainPrefix(boards, toolchains)
  201. if err:
  202. sys.exit(col.Color(col.RED, err))
  203. return 0
  204. # Work out how many commits to build. We want to build everything on the
  205. # branch. We also build the upstream commit as a control so we can see
  206. # problems introduced by the first commit on the branch.
  207. count = options.count
  208. has_range = options.branch and '..' in options.branch
  209. if count == -1:
  210. if not options.branch:
  211. count = 1
  212. else:
  213. if has_range:
  214. count, msg = gitutil.CountCommitsInRange(options.git_dir,
  215. options.branch)
  216. else:
  217. count, msg = gitutil.CountCommitsInBranch(options.git_dir,
  218. options.branch)
  219. if count is None:
  220. sys.exit(col.Color(col.RED, msg))
  221. elif count == 0:
  222. sys.exit(col.Color(col.RED, "Range '%s' has no commits" %
  223. options.branch))
  224. if msg:
  225. print(col.Color(col.YELLOW, msg))
  226. count += 1 # Build upstream commit also
  227. if not count:
  228. str = ("No commits found to process in branch '%s': "
  229. "set branch's upstream or use -c flag" % options.branch)
  230. sys.exit(col.Color(col.RED, str))
  231. if options.work_in_output:
  232. if len(selected) != 1:
  233. sys.exit(col.Color(col.RED,
  234. '-w can only be used with a single board'))
  235. if count != 1:
  236. sys.exit(col.Color(col.RED,
  237. '-w can only be used with a single commit'))
  238. # Read the metadata from the commits. First look at the upstream commit,
  239. # then the ones in the branch. We would like to do something like
  240. # upstream/master~..branch but that isn't possible if upstream/master is
  241. # a merge commit (it will list all the commits that form part of the
  242. # merge)
  243. # Conflicting tags are not a problem for buildman, since it does not use
  244. # them. For example, Series-version is not useful for buildman. On the
  245. # other hand conflicting tags will cause an error. So allow later tags
  246. # to overwrite earlier ones by setting allow_overwrite=True
  247. if options.branch:
  248. if count == -1:
  249. if has_range:
  250. range_expr = options.branch
  251. else:
  252. range_expr = gitutil.GetRangeInBranch(options.git_dir,
  253. options.branch)
  254. upstream_commit = gitutil.GetUpstream(options.git_dir,
  255. options.branch)
  256. series = patchstream.get_metadata_for_list(upstream_commit,
  257. options.git_dir, 1, series=None, allow_overwrite=True)
  258. series = patchstream.get_metadata_for_list(range_expr,
  259. options.git_dir, None, series, allow_overwrite=True)
  260. else:
  261. # Honour the count
  262. series = patchstream.get_metadata_for_list(options.branch,
  263. options.git_dir, count, series=None, allow_overwrite=True)
  264. else:
  265. series = None
  266. if not options.dry_run:
  267. options.verbose = True
  268. if not options.summary:
  269. options.show_errors = True
  270. # By default we have one thread per CPU. But if there are not enough jobs
  271. # we can have fewer threads and use a high '-j' value for make.
  272. if options.threads is None:
  273. options.threads = min(multiprocessing.cpu_count(), len(selected))
  274. if not options.jobs:
  275. options.jobs = max(1, (multiprocessing.cpu_count() +
  276. len(selected) - 1) // len(selected))
  277. if not options.step:
  278. options.step = len(series.commits) - 1
  279. gnu_make = command.Output(os.path.join(options.git,
  280. 'scripts/show-gnu-make'), raise_on_error=False).rstrip()
  281. if not gnu_make:
  282. sys.exit('GNU Make not found')
  283. # Create a new builder with the selected options.
  284. output_dir = options.output_dir
  285. if options.branch:
  286. dirname = options.branch.replace('/', '_')
  287. # As a special case allow the board directory to be placed in the
  288. # output directory itself rather than any subdirectory.
  289. if not options.no_subdirs:
  290. output_dir = os.path.join(options.output_dir, dirname)
  291. if clean_dir and os.path.exists(output_dir):
  292. shutil.rmtree(output_dir)
  293. builder = Builder(toolchains, output_dir, options.git_dir,
  294. options.threads, options.jobs, gnu_make=gnu_make, checkout=True,
  295. show_unknown=options.show_unknown, step=options.step,
  296. no_subdirs=options.no_subdirs, full_path=options.full_path,
  297. verbose_build=options.verbose_build,
  298. mrproper=options.mrproper,
  299. per_board_out_dir=options.per_board_out_dir,
  300. config_only=options.config_only,
  301. squash_config_y=not options.preserve_config_y,
  302. warnings_as_errors=options.warnings_as_errors,
  303. work_in_output=options.work_in_output,
  304. test_thread_exceptions=test_thread_exceptions)
  305. builder.force_config_on_failure = not options.quick
  306. if make_func:
  307. builder.do_make = make_func
  308. # For a dry run, just show our actions as a sanity check
  309. if options.dry_run:
  310. ShowActions(series, why_selected, selected, builder, options,
  311. board_warnings)
  312. else:
  313. builder.force_build = options.force_build
  314. builder.force_build_failures = options.force_build_failures
  315. builder.force_reconfig = options.force_reconfig
  316. builder.in_tree = options.in_tree
  317. # Work out which boards to build
  318. board_selected = boards.GetSelectedDict()
  319. if series:
  320. commits = series.commits
  321. # Number the commits for test purposes
  322. for commit in range(len(commits)):
  323. commits[commit].sequence = commit
  324. else:
  325. commits = None
  326. Print(GetActionSummary(options.summary, commits, board_selected,
  327. options))
  328. # We can't show function sizes without board details at present
  329. if options.show_bloat:
  330. options.show_detail = True
  331. builder.SetDisplayOptions(
  332. options.show_errors, options.show_sizes, options.show_detail,
  333. options.show_bloat, options.list_error_boards, options.show_config,
  334. options.show_environment, options.filter_dtb_warnings,
  335. options.filter_migration_warnings)
  336. if options.summary:
  337. builder.ShowSummary(commits, board_selected)
  338. else:
  339. fail, warned, excs = builder.BuildBoards(
  340. commits, board_selected, options.keep_outputs, options.verbose)
  341. if excs:
  342. return 102
  343. elif fail:
  344. return 100
  345. elif warned and not options.ignore_warnings:
  346. return 101
  347. return 0