control.py 13 KB

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