blob: e7d9a29d03ef8a318e1e2835934591e73a03315e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass4a1e88b2014-08-09 15:33:00 -06002# Copyright (c) 2014 Google, Inc
3#
Simon Glass4a1e88b2014-08-09 15:33:00 -06004
Simon Glassd07e5532023-07-19 17:49:09 -06005"""Implementation the bulider threads
6
7This module provides the BuilderThread class, which handles calling the builder
8based on the jobs provided.
9"""
10
Simon Glass4a1e88b2014-08-09 15:33:00 -060011import errno
12import glob
13import os
14import shutil
Lothar Waßmannce6df922018-04-08 05:14:11 -060015import sys
Simon Glass4a1e88b2014-08-09 15:33:00 -060016import threading
17
Simon Glasse5650a82022-01-22 05:07:33 -070018from buildman import cfgutil
Simon Glassa997ea52020-04-17 18:09:04 -060019from patman import gitutil
Simon Glass131444f2023-02-23 18:18:04 -070020from u_boot_pylib import command
Simon Glass4a1e88b2014-08-09 15:33:00 -060021
Simon Glassfd3eea12015-02-05 22:06:13 -070022RETURN_CODE_RETRY = -1
Simon Glasse0f19712020-12-16 17:24:17 -070023BASE_ELF_FILENAMES = ['u-boot', 'spl/u-boot-spl', 'tpl/u-boot-tpl']
Simon Glassfd3eea12015-02-05 22:06:13 -070024
Simon Glassc5077c32023-07-19 17:49:08 -060025def mkdir(dirname, parents = False):
Simon Glass4a1e88b2014-08-09 15:33:00 -060026 """Make a directory if it doesn't already exist.
27
28 Args:
29 dirname: Directory to create
30 """
31 try:
Thierry Reding336d5bd2014-08-19 10:22:39 +020032 if parents:
33 os.makedirs(dirname)
34 else:
35 os.mkdir(dirname)
Simon Glass4a1e88b2014-08-09 15:33:00 -060036 except OSError as err:
37 if err.errno == errno.EEXIST:
Lothar Waßmannce6df922018-04-08 05:14:11 -060038 if os.path.realpath('.') == os.path.realpath(dirname):
Simon Glassd07e5532023-07-19 17:49:09 -060039 print(f"Cannot create the current working directory '{dirname}'!")
Lothar Waßmannce6df922018-04-08 05:14:11 -060040 sys.exit(1)
Simon Glass4a1e88b2014-08-09 15:33:00 -060041 else:
42 raise
43
Simon Glassd07e5532023-07-19 17:49:09 -060044# pylint: disable=R0903
Simon Glass4a1e88b2014-08-09 15:33:00 -060045class BuilderJob:
46 """Holds information about a job to be performed by a thread
47
48 Members:
Simon Glass8132f982022-07-11 19:03:57 -060049 brd: Board object to build
Simon Glassdf890152020-03-18 09:42:41 -060050 commits: List of Commit objects to build
51 keep_outputs: True to save build output files
52 step: 1 to process every commit, n to process every nth commit
Simon Glassb6eb8cf2020-03-18 09:42:42 -060053 work_in_output: Use the output directory as the work directory and
54 don't write to a separate output directory.
Simon Glass4a1e88b2014-08-09 15:33:00 -060055 """
56 def __init__(self):
Simon Glass8132f982022-07-11 19:03:57 -060057 self.brd = None
Simon Glass4a1e88b2014-08-09 15:33:00 -060058 self.commits = []
Simon Glassdf890152020-03-18 09:42:41 -060059 self.keep_outputs = False
60 self.step = 1
Simon Glassb6eb8cf2020-03-18 09:42:42 -060061 self.work_in_output = False
Simon Glass4a1e88b2014-08-09 15:33:00 -060062
63
64class ResultThread(threading.Thread):
65 """This thread processes results from builder threads.
66
67 It simply passes the results on to the builder. There is only one
68 result thread, and this helps to serialise the build output.
69 """
70 def __init__(self, builder):
71 """Set up a new result thread
72
73 Args:
74 builder: Builder which will be sent each result
75 """
76 threading.Thread.__init__(self)
77 self.builder = builder
78
79 def run(self):
80 """Called to start up the result thread.
81
82 We collect the next result job and pass it on to the build.
83 """
84 while True:
85 result = self.builder.out_queue.get()
Simon Glassbc74d942023-07-19 17:49:06 -060086 self.builder.process_result(result)
Simon Glass4a1e88b2014-08-09 15:33:00 -060087 self.builder.out_queue.task_done()
88
89
90class BuilderThread(threading.Thread):
91 """This thread builds U-Boot for a particular board.
92
93 An input queue provides each new job. We run 'make' to build U-Boot
94 and then pass the results on to the output queue.
95
96 Members:
97 builder: The builder which contains information we might need
98 thread_num: Our thread number (0-n-1), used to decide on a
Simon Glassa29b3ea2021-04-11 16:27:25 +120099 temporary directory. If this is -1 then there are no threads
100 and we are the (only) main process
101 mrproper: Use 'make mrproper' before each reconfigure
102 per_board_out_dir: True to build in a separate persistent directory per
103 board rather than a thread-specific directory
104 test_exception: Used for testing; True to raise an exception instead of
105 reporting the build result
Simon Glass4a1e88b2014-08-09 15:33:00 -0600106 """
Simon Glass9bf9a722021-04-11 16:27:27 +1200107 def __init__(self, builder, thread_num, mrproper, per_board_out_dir,
108 test_exception=False):
Simon Glass4a1e88b2014-08-09 15:33:00 -0600109 """Set up a new builder thread"""
110 threading.Thread.__init__(self)
111 self.builder = builder
112 self.thread_num = thread_num
Simon Glass6029af12020-04-09 15:08:51 -0600113 self.mrproper = mrproper
Stephen Warren97c96902016-04-11 10:48:44 -0600114 self.per_board_out_dir = per_board_out_dir
Simon Glass9bf9a722021-04-11 16:27:27 +1200115 self.test_exception = test_exception
Simon Glassd07e5532023-07-19 17:49:09 -0600116 self.toolchain = None
Simon Glass4a1e88b2014-08-09 15:33:00 -0600117
Simon Glassc5077c32023-07-19 17:49:08 -0600118 def make(self, commit, brd, stage, cwd, *args, **kwargs):
Simon Glass4a1e88b2014-08-09 15:33:00 -0600119 """Run 'make' on a particular commit and board.
120
121 The source code will already be checked out, so the 'commit'
122 argument is only for information.
123
124 Args:
125 commit: Commit object that is being built
126 brd: Board object that is being built
127 stage: Stage of the build. Valid stages are:
Roger Meiere0a0e552014-08-20 22:10:29 +0200128 mrproper - can be called to clean source
Simon Glass4a1e88b2014-08-09 15:33:00 -0600129 config - called to configure for a board
130 build - the main make invocation - it does the build
131 args: A list of arguments to pass to 'make'
Simon Glass840be732022-01-29 14:14:05 -0700132 kwargs: A list of keyword arguments to pass to command.run_pipe()
Simon Glass4a1e88b2014-08-09 15:33:00 -0600133
134 Returns:
135 CommandResult object
136 """
137 return self.builder.do_make(commit, brd, stage, cwd, *args,
138 **kwargs)
139
Simon Glassec7132b2023-07-19 17:49:14 -0600140 def _build_args(self, args, brd):
Simon Glass8d0b6b42023-07-19 17:49:13 -0600141 """Set up arguments to the args list based on the settings
142
143 Args:
144 args (list of str): List of string arguments to add things to
Simon Glassec7132b2023-07-19 17:49:14 -0600145 brd (Board): Board to create arguments for
Simon Glass8d0b6b42023-07-19 17:49:13 -0600146 """
147 if self.builder.verbose_build:
148 args.append('V=1')
149 else:
150 args.append('-s')
151 if self.builder.num_jobs is not None:
152 args.extend(['-j', str(self.builder.num_jobs)])
153 if self.builder.warnings_as_errors:
154 args.append('KCFLAGS=-Werror')
155 args.append('HOSTCFLAGS=-Werror')
156 if self.builder.allow_missing:
157 args.append('BINMAN_ALLOW_MISSING=1')
158 if self.builder.no_lto:
159 args.append('NO_LTO=1')
160 if self.builder.reproducible_builds:
161 args.append('SOURCE_DATE_EPOCH=0')
Simon Glassec7132b2023-07-19 17:49:14 -0600162 args.extend(self.builder.toolchains.GetMakeArguments(brd))
163 args.extend(self.toolchain.MakeArgs())
Simon Glass8d0b6b42023-07-19 17:49:13 -0600164
Simon Glassc5077c32023-07-19 17:49:08 -0600165 def run_commit(self, commit_upto, brd, work_dir, do_config, config_only,
Simon Glasse5650a82022-01-22 05:07:33 -0700166 force_build, force_build_failures, work_in_output,
167 adjust_cfg):
Simon Glass4a1e88b2014-08-09 15:33:00 -0600168 """Build a particular commit.
169
170 If the build is already done, and we are not forcing a build, we skip
171 the build and just return the previously-saved results.
172
173 Args:
174 commit_upto: Commit number to build (0...n-1)
175 brd: Board object to build
176 work_dir: Directory to which the source will be checked out
177 do_config: True to run a make <board>_defconfig on the source
Simon Glassb55b57d2016-11-16 14:09:25 -0700178 config_only: Only configure the source, do not build it
Simon Glass4a1e88b2014-08-09 15:33:00 -0600179 force_build: Force a build even if one was previously done
180 force_build_failures: Force a bulid if the previous result showed
181 failure
Simon Glassb6eb8cf2020-03-18 09:42:42 -0600182 work_in_output: Use the output directory as the work directory and
183 don't write to a separate output directory.
Simon Glasse5650a82022-01-22 05:07:33 -0700184 adjust_cfg (list of str): List of changes to make to .config file
185 before building. Each is one of (where C is either CONFIG_xxx
186 or just xxx):
187 C to enable C
188 ~C to disable C
189 C=val to set the value of C (val must have quotes if C is
190 a string Kconfig
Simon Glass4a1e88b2014-08-09 15:33:00 -0600191
192 Returns:
193 tuple containing:
194 - CommandResult object containing the results of the build
195 - boolean indicating whether 'make config' is still needed
196 """
197 # Create a default result - it will be overwritte by the call to
Simon Glassc5077c32023-07-19 17:49:08 -0600198 # self.make() below, in the event that we do a build.
Simon Glass4a1e88b2014-08-09 15:33:00 -0600199 result = command.CommandResult()
200 result.return_code = 0
Simon Glassb6eb8cf2020-03-18 09:42:42 -0600201 if work_in_output or self.builder.in_tree:
Simon Glass4a1e88b2014-08-09 15:33:00 -0600202 out_dir = work_dir
203 else:
Stephen Warren97c96902016-04-11 10:48:44 -0600204 if self.per_board_out_dir:
205 out_rel_dir = os.path.join('..', brd.target)
206 else:
207 out_rel_dir = 'build'
208 out_dir = os.path.join(work_dir, out_rel_dir)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600209
210 # Check if the job was already completed last time
Simon Glassbc74d942023-07-19 17:49:06 -0600211 done_file = self.builder.get_done_file(commit_upto, brd.target)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600212 result.already_done = os.path.exists(done_file)
213 will_build = (force_build or force_build_failures or
214 not result.already_done)
Simon Glass09f1fe52014-09-05 19:00:17 -0600215 if result.already_done:
Simon Glass4a1e88b2014-08-09 15:33:00 -0600216 # Get the return code from that build and use it
Simon Glassd07e5532023-07-19 17:49:09 -0600217 with open(done_file, 'r', encoding='utf-8') as outf:
Simon Glass4345a6d2018-12-10 09:05:23 -0700218 try:
Simon Glassd07e5532023-07-19 17:49:09 -0600219 result.return_code = int(outf.readline())
Simon Glass4345a6d2018-12-10 09:05:23 -0700220 except ValueError:
221 # The file may be empty due to running out of disk space.
222 # Try a rebuild
223 result.return_code = RETURN_CODE_RETRY
Simon Glassfd3eea12015-02-05 22:06:13 -0700224
225 # Check the signal that the build needs to be retried
226 if result.return_code == RETURN_CODE_RETRY:
227 will_build = True
228 elif will_build:
Simon Glassbc74d942023-07-19 17:49:06 -0600229 err_file = self.builder.get_err_file(commit_upto, brd.target)
Simon Glass09f1fe52014-09-05 19:00:17 -0600230 if os.path.exists(err_file) and os.stat(err_file).st_size:
231 result.stderr = 'bad'
232 elif not force_build:
233 # The build passed, so no need to build it again
234 will_build = False
Simon Glass4a1e88b2014-08-09 15:33:00 -0600235
236 if will_build:
237 # We are going to have to build it. First, get a toolchain
238 if not self.toolchain:
239 try:
240 self.toolchain = self.builder.toolchains.Select(brd.arch)
241 except ValueError as err:
242 result.return_code = 10
243 result.stdout = ''
244 result.stderr = str(err)
245 # TODO(sjg@chromium.org): This gets swallowed, but needs
246 # to be reported.
247
248 if self.toolchain:
249 # Checkout the right commit
250 if self.builder.commits:
251 commit = self.builder.commits[commit_upto]
252 if self.builder.checkout:
253 git_dir = os.path.join(work_dir, '.git')
Simon Glass761648b2022-01-29 14:14:11 -0700254 gitutil.checkout(commit.hash, git_dir, work_dir,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600255 force=True)
256 else:
257 commit = 'current'
258
259 # Set up the environment and command line
Simon Glassd48a46c2014-12-01 17:34:00 -0700260 env = self.toolchain.MakeEnvironment(self.builder.full_path)
Simon Glassc5077c32023-07-19 17:49:08 -0600261 mkdir(out_dir)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600262 args = []
263 cwd = work_dir
Simon Glasse3096252014-08-28 09:43:42 -0600264 src_dir = os.path.realpath(work_dir)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600265 if not self.builder.in_tree:
266 if commit_upto is None:
267 # In this case we are building in the original source
268 # directory (i.e. the current directory where buildman
269 # is invoked. The output directory is set to this
270 # thread's selected work directory.
271 #
272 # Symlinks can confuse U-Boot's Makefile since
273 # we may use '..' in our path, so remove them.
Simon Glass21ca0c42023-07-19 17:49:11 -0600274 real_dir = os.path.realpath(out_dir)
275 args.append(f'O={real_dir}')
Simon Glass4a1e88b2014-08-09 15:33:00 -0600276 cwd = None
Simon Glasse3096252014-08-28 09:43:42 -0600277 src_dir = os.getcwd()
Simon Glass4a1e88b2014-08-09 15:33:00 -0600278 else:
Simon Glassd07e5532023-07-19 17:49:09 -0600279 args.append(f'O={out_rel_dir}')
Simon Glassec7132b2023-07-19 17:49:14 -0600280 self._build_args(args, brd)
Simon Glassd07e5532023-07-19 17:49:09 -0600281 config_args = [f'{brd.target}_defconfig']
Simon Glass4a1e88b2014-08-09 15:33:00 -0600282 config_out = ''
Simon Glass4a1e88b2014-08-09 15:33:00 -0600283
Simon Glasse0f19712020-12-16 17:24:17 -0700284 # Remove any output targets. Since we use a build directory that
285 # was previously used by another board, it may have produced an
286 # SPL image. If we don't remove it (i.e. see do_config and
287 # self.mrproper below) then it will appear to be the output of
288 # this build, even if it does not produce SPL images.
Simon Glasse0f19712020-12-16 17:24:17 -0700289 for elf in BASE_ELF_FILENAMES:
290 fname = os.path.join(out_dir, elf)
291 if os.path.exists(fname):
292 os.remove(fname)
293
Simon Glass4a1e88b2014-08-09 15:33:00 -0600294 # If we need to reconfigure, do that now
Simon Glasse5650a82022-01-22 05:07:33 -0700295 cfg_file = os.path.join(out_dir, '.config')
Simon Glass1382b1d2023-02-21 12:40:27 -0700296 cmd_list = []
Simon Glasse5650a82022-01-22 05:07:33 -0700297 if do_config or adjust_cfg:
Simon Glass6029af12020-04-09 15:08:51 -0600298 if self.mrproper:
Simon Glassc5077c32023-07-19 17:49:08 -0600299 result = self.make(commit, brd, 'mrproper', cwd,
Stephen Warren97c96902016-04-11 10:48:44 -0600300 'mrproper', *args, env=env)
301 config_out += result.combined
Simon Glass1382b1d2023-02-21 12:40:27 -0700302 cmd_list.append([self.builder.gnu_make, 'mrproper',
303 *args])
Simon Glassc5077c32023-07-19 17:49:08 -0600304 result = self.make(commit, brd, 'config', cwd,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600305 *(args + config_args), env=env)
Simon Glass1382b1d2023-02-21 12:40:27 -0700306 cmd_list.append([self.builder.gnu_make] + args +
307 config_args)
Simon Glass413f91a2015-02-05 22:06:12 -0700308 config_out += result.combined
Simon Glass4a1e88b2014-08-09 15:33:00 -0600309 do_config = False # No need to configure next time
Simon Glasse5650a82022-01-22 05:07:33 -0700310 if adjust_cfg:
311 cfgutil.adjust_cfg_file(cfg_file, adjust_cfg)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600312 if result.return_code == 0:
Simon Glassb55b57d2016-11-16 14:09:25 -0700313 if config_only:
Simon Glass739e8512016-11-13 14:25:51 -0700314 args.append('cfg')
Simon Glassc5077c32023-07-19 17:49:08 -0600315 result = self.make(commit, brd, 'build', cwd, *args,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600316 env=env)
Simon Glass1382b1d2023-02-21 12:40:27 -0700317 cmd_list.append([self.builder.gnu_make] + args)
Simon Glass199da412022-11-09 19:14:48 -0700318 if (result.return_code == 2 and
319 ('Some images are invalid' in result.stderr)):
320 # This is handled later by the check for output in
321 # stderr
322 result.return_code = 0
Simon Glasse5650a82022-01-22 05:07:33 -0700323 if adjust_cfg:
324 errs = cfgutil.check_cfg_file(cfg_file, adjust_cfg)
325 if errs:
Simon Glasse5650a82022-01-22 05:07:33 -0700326 result.stderr += errs
327 result.return_code = 1
Simon Glasse3096252014-08-28 09:43:42 -0600328 result.stderr = result.stderr.replace(src_dir + '/', '')
Simon Glass413f91a2015-02-05 22:06:12 -0700329 if self.builder.verbose_build:
330 result.stdout = config_out + result.stdout
Simon Glass1382b1d2023-02-21 12:40:27 -0700331 result.cmd_list = cmd_list
Simon Glass4a1e88b2014-08-09 15:33:00 -0600332 else:
333 result.return_code = 1
Simon Glassd07e5532023-07-19 17:49:09 -0600334 result.stderr = f'No tool chain for {brd.arch}\n'
Simon Glass4a1e88b2014-08-09 15:33:00 -0600335 result.already_done = False
336
337 result.toolchain = self.toolchain
338 result.brd = brd
339 result.commit_upto = commit_upto
340 result.out_dir = out_dir
341 return result, do_config
342
Simon Glassc5077c32023-07-19 17:49:08 -0600343 def _write_result(self, result, keep_outputs, work_in_output):
Simon Glass4a1e88b2014-08-09 15:33:00 -0600344 """Write a built result to the output directory.
345
346 Args:
347 result: CommandResult object containing result to write
348 keep_outputs: True to store the output binaries, False
349 to delete them
Simon Glassb6eb8cf2020-03-18 09:42:42 -0600350 work_in_output: Use the output directory as the work directory and
351 don't write to a separate output directory.
Simon Glass4a1e88b2014-08-09 15:33:00 -0600352 """
Simon Glassfd3eea12015-02-05 22:06:13 -0700353 # If we think this might have been aborted with Ctrl-C, record the
354 # failure but not that we are 'done' with this board. A retry may fix
355 # it.
Simon Glass2e737452021-10-19 21:43:23 -0600356 maybe_aborted = result.stderr and 'No child processes' in result.stderr
Simon Glass4a1e88b2014-08-09 15:33:00 -0600357
Simon Glass2e737452021-10-19 21:43:23 -0600358 if result.return_code >= 0 and result.already_done:
Simon Glass4a1e88b2014-08-09 15:33:00 -0600359 return
360
361 # Write the output and stderr
Simon Glass4cb54682023-07-19 17:49:10 -0600362 output_dir = self.builder.get_output_dir(result.commit_upto)
Simon Glassc5077c32023-07-19 17:49:08 -0600363 mkdir(output_dir)
Simon Glassbc74d942023-07-19 17:49:06 -0600364 build_dir = self.builder.get_build_dir(result.commit_upto,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600365 result.brd.target)
Simon Glassc5077c32023-07-19 17:49:08 -0600366 mkdir(build_dir)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600367
368 outfile = os.path.join(build_dir, 'log')
Simon Glassd07e5532023-07-19 17:49:09 -0600369 with open(outfile, 'w', encoding='utf-8') as outf:
Simon Glass4a1e88b2014-08-09 15:33:00 -0600370 if result.stdout:
Simon Glassd07e5532023-07-19 17:49:09 -0600371 outf.write(result.stdout)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600372
Simon Glassbc74d942023-07-19 17:49:06 -0600373 errfile = self.builder.get_err_file(result.commit_upto,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600374 result.brd.target)
375 if result.stderr:
Simon Glassd07e5532023-07-19 17:49:09 -0600376 with open(errfile, 'w', encoding='utf-8') as outf:
377 outf.write(result.stderr)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600378 elif os.path.exists(errfile):
379 os.remove(errfile)
380
Simon Glass2e737452021-10-19 21:43:23 -0600381 # Fatal error
382 if result.return_code < 0:
383 return
384
Simon Glass4a1e88b2014-08-09 15:33:00 -0600385 if result.toolchain:
386 # Write the build result and toolchain information.
Simon Glassbc74d942023-07-19 17:49:06 -0600387 done_file = self.builder.get_done_file(result.commit_upto,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600388 result.brd.target)
Simon Glassd07e5532023-07-19 17:49:09 -0600389 with open(done_file, 'w', encoding='utf-8') as outf:
Simon Glassfd3eea12015-02-05 22:06:13 -0700390 if maybe_aborted:
391 # Special code to indicate we need to retry
Simon Glassd07e5532023-07-19 17:49:09 -0600392 outf.write(f'{RETURN_CODE_RETRY}')
Simon Glassfd3eea12015-02-05 22:06:13 -0700393 else:
Simon Glassd07e5532023-07-19 17:49:09 -0600394 outf.write(f'{result.return_code}')
395 with open(os.path.join(build_dir, 'toolchain'), 'w',
396 encoding='utf-8') as outf:
397 print('gcc', result.toolchain.gcc, file=outf)
398 print('path', result.toolchain.path, file=outf)
399 print('cross', result.toolchain.cross, file=outf)
400 print('arch', result.toolchain.arch, file=outf)
401 outf.write(f'{result.return_code}')
Simon Glass4a1e88b2014-08-09 15:33:00 -0600402
Simon Glass4a1e88b2014-08-09 15:33:00 -0600403 # Write out the image and function size information and an objdump
Simon Glassd48a46c2014-12-01 17:34:00 -0700404 env = result.toolchain.MakeEnvironment(self.builder.full_path)
Simon Glassd07e5532023-07-19 17:49:09 -0600405 with open(os.path.join(build_dir, 'out-env'), 'wb') as outf:
Simon Glass9e90d312019-01-07 16:44:23 -0700406 for var in sorted(env.keys()):
Simon Glassd07e5532023-07-19 17:49:09 -0600407 outf.write(b'%s="%s"' % (var, env[var]))
Simon Glass1382b1d2023-02-21 12:40:27 -0700408
409 with open(os.path.join(build_dir, 'out-cmd'), 'w',
Simon Glassd07e5532023-07-19 17:49:09 -0600410 encoding='utf-8') as outf:
Simon Glass1382b1d2023-02-21 12:40:27 -0700411 for cmd in result.cmd_list:
Simon Glassd07e5532023-07-19 17:49:09 -0600412 print(' '.join(cmd), file=outf)
Simon Glass1382b1d2023-02-21 12:40:27 -0700413
Simon Glass4a1e88b2014-08-09 15:33:00 -0600414 lines = []
Simon Glasse0f19712020-12-16 17:24:17 -0700415 for fname in BASE_ELF_FILENAMES:
Simon Glassd07e5532023-07-19 17:49:09 -0600416 cmd = [f'{self.toolchain.cross}nm', '--size-sort', fname]
Simon Glass840be732022-01-29 14:14:05 -0700417 nm_result = command.run_pipe([cmd], capture=True,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600418 capture_stderr=True, cwd=result.out_dir,
419 raise_on_error=False, env=env)
420 if nm_result.stdout:
Simon Glassd07e5532023-07-19 17:49:09 -0600421 nm_fname = self.builder.get_func_sizes_file(
422 result.commit_upto, result.brd.target, fname)
423 with open(nm_fname, 'w', encoding='utf-8') as outf:
424 print(nm_result.stdout, end=' ', file=outf)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600425
Simon Glassd07e5532023-07-19 17:49:09 -0600426 cmd = [f'{self.toolchain.cross}objdump', '-h', fname]
Simon Glass840be732022-01-29 14:14:05 -0700427 dump_result = command.run_pipe([cmd], capture=True,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600428 capture_stderr=True, cwd=result.out_dir,
429 raise_on_error=False, env=env)
430 rodata_size = ''
431 if dump_result.stdout:
Simon Glassbc74d942023-07-19 17:49:06 -0600432 objdump = self.builder.get_objdump_file(result.commit_upto,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600433 result.brd.target, fname)
Simon Glassd07e5532023-07-19 17:49:09 -0600434 with open(objdump, 'w', encoding='utf-8') as outf:
435 print(dump_result.stdout, end=' ', file=outf)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600436 for line in dump_result.stdout.splitlines():
437 fields = line.split()
438 if len(fields) > 5 and fields[1] == '.rodata':
439 rodata_size = fields[2]
440
Simon Glassd07e5532023-07-19 17:49:09 -0600441 cmd = [f'{self.toolchain.cross}size', fname]
Simon Glass840be732022-01-29 14:14:05 -0700442 size_result = command.run_pipe([cmd], capture=True,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600443 capture_stderr=True, cwd=result.out_dir,
444 raise_on_error=False, env=env)
445 if size_result.stdout:
446 lines.append(size_result.stdout.splitlines()[1] + ' ' +
447 rodata_size)
448
Alex Kiernanf07ed232018-05-31 04:48:33 +0000449 # Extract the environment from U-Boot and dump it out
Simon Glassd07e5532023-07-19 17:49:09 -0600450 cmd = [f'{self.toolchain.cross}objcopy', '-O', 'binary',
Alex Kiernanf07ed232018-05-31 04:48:33 +0000451 '-j', '.rodata.default_environment',
452 'env/built-in.o', 'uboot.env']
Simon Glass840be732022-01-29 14:14:05 -0700453 command.run_pipe([cmd], capture=True,
Alex Kiernanf07ed232018-05-31 04:48:33 +0000454 capture_stderr=True, cwd=result.out_dir,
455 raise_on_error=False, env=env)
Simon Glasse3c85ab2020-04-17 17:51:34 -0600456 if not work_in_output:
Simon Glassc5077c32023-07-19 17:49:08 -0600457 self.copy_files(result.out_dir, build_dir, '', ['uboot.env'])
Alex Kiernanf07ed232018-05-31 04:48:33 +0000458
Simon Glass4a1e88b2014-08-09 15:33:00 -0600459 # Write out the image sizes file. This is similar to the output
460 # of binutil's 'size' utility, but it omits the header line and
461 # adds an additional hex value at the end of each line for the
462 # rodata size
Simon Glassd07e5532023-07-19 17:49:09 -0600463 if lines:
Simon Glassbc74d942023-07-19 17:49:06 -0600464 sizes = self.builder.get_sizes_file(result.commit_upto,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600465 result.brd.target)
Simon Glassd07e5532023-07-19 17:49:09 -0600466 with open(sizes, 'w', encoding='utf-8') as outf:
467 print('\n'.join(lines), file=outf)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600468
Simon Glasse3c85ab2020-04-17 17:51:34 -0600469 if not work_in_output:
470 # Write out the configuration files, with a special case for SPL
471 for dirname in ['', 'spl', 'tpl']:
Simon Glassc5077c32023-07-19 17:49:08 -0600472 self.copy_files(
Simon Glasse3c85ab2020-04-17 17:51:34 -0600473 result.out_dir, build_dir, dirname,
474 ['u-boot.cfg', 'spl/u-boot-spl.cfg', 'tpl/u-boot-tpl.cfg',
475 '.config', 'include/autoconf.mk',
476 'include/generated/autoconf.h'])
Simon Glass4b14c532015-02-05 22:06:14 -0700477
Simon Glasse3c85ab2020-04-17 17:51:34 -0600478 # Now write the actual build output
479 if keep_outputs:
Simon Glassc5077c32023-07-19 17:49:08 -0600480 self.copy_files(
Simon Glasse3c85ab2020-04-17 17:51:34 -0600481 result.out_dir, build_dir, '',
482 ['u-boot*', '*.bin', '*.map', '*.img', 'MLO', 'SPL',
483 'include/autoconf.mk', 'spl/u-boot-spl*'])
Simon Glass4b14c532015-02-05 22:06:14 -0700484
Simon Glassc5077c32023-07-19 17:49:08 -0600485 def copy_files(self, out_dir, build_dir, dirname, patterns):
Simon Glass4b14c532015-02-05 22:06:14 -0700486 """Copy files from the build directory to the output.
Simon Glass4a1e88b2014-08-09 15:33:00 -0600487
Simon Glass4b14c532015-02-05 22:06:14 -0700488 Args:
489 out_dir: Path to output directory containing the files
490 build_dir: Place to copy the files
491 dirname: Source directory, '' for normal U-Boot, 'spl' for SPL
492 patterns: A list of filenames (strings) to copy, each relative
493 to the build directory
494 """
495 for pattern in patterns:
496 file_list = glob.glob(os.path.join(out_dir, dirname, pattern))
497 for fname in file_list:
498 target = os.path.basename(fname)
499 if dirname:
500 base, ext = os.path.splitext(target)
501 if ext:
Simon Glassd07e5532023-07-19 17:49:09 -0600502 target = f'{base}-{dirname}{ext}'
Simon Glass4b14c532015-02-05 22:06:14 -0700503 shutil.copy(fname, os.path.join(build_dir, target))
Simon Glass4a1e88b2014-08-09 15:33:00 -0600504
Simon Glassc5077c32023-07-19 17:49:08 -0600505 def _send_result(self, result):
Simon Glassa3d01442021-04-11 16:27:26 +1200506 """Send a result to the builder for processing
507
508 Args:
509 result: CommandResult object containing the results of the build
Simon Glass9bf9a722021-04-11 16:27:27 +1200510
511 Raises:
512 ValueError if self.test_exception is true (for testing)
Simon Glassa3d01442021-04-11 16:27:26 +1200513 """
Simon Glass9bf9a722021-04-11 16:27:27 +1200514 if self.test_exception:
515 raise ValueError('test exception')
Simon Glassa3d01442021-04-11 16:27:26 +1200516 if self.thread_num != -1:
517 self.builder.out_queue.put(result)
518 else:
Simon Glassbc74d942023-07-19 17:49:06 -0600519 self.builder.process_result(result)
Simon Glassa3d01442021-04-11 16:27:26 +1200520
Simon Glassc5077c32023-07-19 17:49:08 -0600521 def run_job(self, job):
Simon Glass4a1e88b2014-08-09 15:33:00 -0600522 """Run a single job
523
524 A job consists of a building a list of commits for a particular board.
525
526 Args:
527 job: Job to build
Simon Glassc635d892021-01-30 22:17:46 -0700528
529 Returns:
530 List of Result objects
Simon Glass4a1e88b2014-08-09 15:33:00 -0600531 """
Simon Glass8132f982022-07-11 19:03:57 -0600532 brd = job.brd
Simon Glassbc74d942023-07-19 17:49:06 -0600533 work_dir = self.builder.get_thread_dir(self.thread_num)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600534 self.toolchain = None
535 if job.commits:
536 # Run 'make board_defconfig' on the first commit
537 do_config = True
538 commit_upto = 0
539 force_build = False
540 for commit_upto in range(0, len(job.commits), job.step):
Simon Glassc5077c32023-07-19 17:49:08 -0600541 result, request_config = self.run_commit(commit_upto, brd,
Simon Glassb55b57d2016-11-16 14:09:25 -0700542 work_dir, do_config, self.builder.config_only,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600543 force_build or self.builder.force_build,
Simon Glassb6eb8cf2020-03-18 09:42:42 -0600544 self.builder.force_build_failures,
Simon Glasse5650a82022-01-22 05:07:33 -0700545 job.work_in_output, job.adjust_cfg)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600546 failed = result.return_code or result.stderr
547 did_config = do_config
548 if failed and not do_config:
549 # If our incremental build failed, try building again
550 # with a reconfig.
551 if self.builder.force_config_on_failure:
Simon Glassc5077c32023-07-19 17:49:08 -0600552 result, request_config = self.run_commit(commit_upto,
Simon Glassb6eb8cf2020-03-18 09:42:42 -0600553 brd, work_dir, True, False, True, False,
Simon Glasse5650a82022-01-22 05:07:33 -0700554 job.work_in_output, job.adjust_cfg)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600555 did_config = True
556 if not self.builder.force_reconfig:
557 do_config = request_config
558
559 # If we built that commit, then config is done. But if we got
560 # an warning, reconfig next time to force it to build the same
561 # files that created warnings this time. Otherwise an
562 # incremental build may not build the same file, and we will
563 # think that the warning has gone away.
564 # We could avoid this by using -Werror everywhere...
565 # For errors, the problem doesn't happen, since presumably
566 # the build stopped and didn't generate output, so will retry
567 # that file next time. So we could detect warnings and deal
568 # with them specially here. For now, we just reconfigure if
569 # anything goes work.
570 # Of course this is substantially slower if there are build
571 # errors/warnings (e.g. 2-3x slower even if only 10% of builds
572 # have problems).
573 if (failed and not result.already_done and not did_config and
574 self.builder.force_config_on_failure):
575 # If this build failed, try the next one with a
576 # reconfigure.
577 # Sometimes if the board_config.h file changes it can mess
578 # with dependencies, and we get:
579 # make: *** No rule to make target `include/autoconf.mk',
580 # needed by `depend'.
581 do_config = True
582 force_build = True
583 else:
584 force_build = False
585 if self.builder.force_config_on_failure:
586 if failed:
587 do_config = True
588 result.commit_upto = commit_upto
589 if result.return_code < 0:
590 raise ValueError('Interrupt')
591
592 # We have the build results, so output the result
Simon Glassc5077c32023-07-19 17:49:08 -0600593 self._write_result(result, job.keep_outputs, job.work_in_output)
594 self._send_result(result)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600595 else:
596 # Just build the currently checked-out build
Simon Glassc5077c32023-07-19 17:49:08 -0600597 result, request_config = self.run_commit(None, brd, work_dir, True,
Simon Glassb55b57d2016-11-16 14:09:25 -0700598 self.builder.config_only, True,
Simon Glasse5650a82022-01-22 05:07:33 -0700599 self.builder.force_build_failures, job.work_in_output,
600 job.adjust_cfg)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600601 result.commit_upto = 0
Simon Glassc5077c32023-07-19 17:49:08 -0600602 self._write_result(result, job.keep_outputs, job.work_in_output)
603 self._send_result(result)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600604
605 def run(self):
606 """Our thread's run function
607
608 This thread picks a job from the queue, runs it, and then goes to the
609 next job.
610 """
Simon Glass4a1e88b2014-08-09 15:33:00 -0600611 while True:
612 job = self.builder.queue.get()
Simon Glass9bf9a722021-04-11 16:27:27 +1200613 try:
Simon Glassc5077c32023-07-19 17:49:08 -0600614 self.run_job(job)
Simon Glassd07e5532023-07-19 17:49:09 -0600615 except Exception as exc:
616 print('Thread exception (use -T0 to run without threads):',
617 exc)
618 self.builder.thread_exceptions.append(exc)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600619 self.builder.queue.task_done()