blob: 065d836d68c3e58b868f43f32be08cc524f15356 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass190064b2014-08-09 15:33:00 -06002# Copyright (c) 2014 Google, Inc
3#
Simon Glass190064b2014-08-09 15:33:00 -06004
5import errno
6import glob
7import os
8import shutil
Lothar Waßmann409fc022018-04-08 05:14:11 -06009import sys
Simon Glass190064b2014-08-09 15:33:00 -060010import threading
11
Simon Glass2b4806e2022-01-22 05:07:33 -070012from buildman import cfgutil
Simon Glassbf776672020-04-17 18:09:04 -060013from patman import command
14from patman import gitutil
Simon Glass190064b2014-08-09 15:33:00 -060015
Simon Glass88c8dcf2015-02-05 22:06:13 -070016RETURN_CODE_RETRY = -1
Simon Glass73da3d22020-12-16 17:24:17 -070017BASE_ELF_FILENAMES = ['u-boot', 'spl/u-boot-spl', 'tpl/u-boot-tpl']
Simon Glass88c8dcf2015-02-05 22:06:13 -070018
Thierry Redingf3d015c2014-08-19 10:22:39 +020019def Mkdir(dirname, parents = False):
Simon Glass190064b2014-08-09 15:33:00 -060020 """Make a directory if it doesn't already exist.
21
22 Args:
23 dirname: Directory to create
24 """
25 try:
Thierry Redingf3d015c2014-08-19 10:22:39 +020026 if parents:
27 os.makedirs(dirname)
28 else:
29 os.mkdir(dirname)
Simon Glass190064b2014-08-09 15:33:00 -060030 except OSError as err:
31 if err.errno == errno.EEXIST:
Lothar Waßmann409fc022018-04-08 05:14:11 -060032 if os.path.realpath('.') == os.path.realpath(dirname):
Simon Glassc05aa032019-10-31 07:42:53 -060033 print("Cannot create the current working directory '%s'!" % dirname)
Lothar Waßmann409fc022018-04-08 05:14:11 -060034 sys.exit(1)
Simon Glass190064b2014-08-09 15:33:00 -060035 pass
36 else:
37 raise
38
39class BuilderJob:
40 """Holds information about a job to be performed by a thread
41
42 Members:
Simon Glassf4ed4702022-07-11 19:03:57 -060043 brd: Board object to build
Simon Glasse9fbbf62020-03-18 09:42:41 -060044 commits: List of Commit objects to build
45 keep_outputs: True to save build output files
46 step: 1 to process every commit, n to process every nth commit
Simon Glassd829f122020-03-18 09:42:42 -060047 work_in_output: Use the output directory as the work directory and
48 don't write to a separate output directory.
Simon Glass190064b2014-08-09 15:33:00 -060049 """
50 def __init__(self):
Simon Glassf4ed4702022-07-11 19:03:57 -060051 self.brd = None
Simon Glass190064b2014-08-09 15:33:00 -060052 self.commits = []
Simon Glasse9fbbf62020-03-18 09:42:41 -060053 self.keep_outputs = False
54 self.step = 1
Simon Glassd829f122020-03-18 09:42:42 -060055 self.work_in_output = False
Simon Glass190064b2014-08-09 15:33:00 -060056
57
58class ResultThread(threading.Thread):
59 """This thread processes results from builder threads.
60
61 It simply passes the results on to the builder. There is only one
62 result thread, and this helps to serialise the build output.
63 """
64 def __init__(self, builder):
65 """Set up a new result thread
66
67 Args:
68 builder: Builder which will be sent each result
69 """
70 threading.Thread.__init__(self)
71 self.builder = builder
72
73 def run(self):
74 """Called to start up the result thread.
75
76 We collect the next result job and pass it on to the build.
77 """
78 while True:
79 result = self.builder.out_queue.get()
80 self.builder.ProcessResult(result)
81 self.builder.out_queue.task_done()
82
83
84class BuilderThread(threading.Thread):
85 """This thread builds U-Boot for a particular board.
86
87 An input queue provides each new job. We run 'make' to build U-Boot
88 and then pass the results on to the output queue.
89
90 Members:
91 builder: The builder which contains information we might need
92 thread_num: Our thread number (0-n-1), used to decide on a
Simon Glass24993312021-04-11 16:27:25 +120093 temporary directory. If this is -1 then there are no threads
94 and we are the (only) main process
95 mrproper: Use 'make mrproper' before each reconfigure
96 per_board_out_dir: True to build in a separate persistent directory per
97 board rather than a thread-specific directory
98 test_exception: Used for testing; True to raise an exception instead of
99 reporting the build result
Simon Glass190064b2014-08-09 15:33:00 -0600100 """
Simon Glass8116c782021-04-11 16:27:27 +1200101 def __init__(self, builder, thread_num, mrproper, per_board_out_dir,
102 test_exception=False):
Simon Glass190064b2014-08-09 15:33:00 -0600103 """Set up a new builder thread"""
104 threading.Thread.__init__(self)
105 self.builder = builder
106 self.thread_num = thread_num
Simon Glasseb70a2c2020-04-09 15:08:51 -0600107 self.mrproper = mrproper
Stephen Warrenf79f1e02016-04-11 10:48:44 -0600108 self.per_board_out_dir = per_board_out_dir
Simon Glass8116c782021-04-11 16:27:27 +1200109 self.test_exception = test_exception
Simon Glass190064b2014-08-09 15:33:00 -0600110
111 def Make(self, commit, brd, stage, cwd, *args, **kwargs):
112 """Run 'make' on a particular commit and board.
113
114 The source code will already be checked out, so the 'commit'
115 argument is only for information.
116
117 Args:
118 commit: Commit object that is being built
119 brd: Board object that is being built
120 stage: Stage of the build. Valid stages are:
Roger Meierfd18a892014-08-20 22:10:29 +0200121 mrproper - can be called to clean source
Simon Glass190064b2014-08-09 15:33:00 -0600122 config - called to configure for a board
123 build - the main make invocation - it does the build
124 args: A list of arguments to pass to 'make'
Simon Glassd9800692022-01-29 14:14:05 -0700125 kwargs: A list of keyword arguments to pass to command.run_pipe()
Simon Glass190064b2014-08-09 15:33:00 -0600126
127 Returns:
128 CommandResult object
129 """
130 return self.builder.do_make(commit, brd, stage, cwd, *args,
131 **kwargs)
132
Simon Glassa9401b22016-11-16 14:09:25 -0700133 def RunCommit(self, commit_upto, brd, work_dir, do_config, config_only,
Simon Glass2b4806e2022-01-22 05:07:33 -0700134 force_build, force_build_failures, work_in_output,
135 adjust_cfg):
Simon Glass190064b2014-08-09 15:33:00 -0600136 """Build a particular commit.
137
138 If the build is already done, and we are not forcing a build, we skip
139 the build and just return the previously-saved results.
140
141 Args:
142 commit_upto: Commit number to build (0...n-1)
143 brd: Board object to build
144 work_dir: Directory to which the source will be checked out
145 do_config: True to run a make <board>_defconfig on the source
Simon Glassa9401b22016-11-16 14:09:25 -0700146 config_only: Only configure the source, do not build it
Simon Glass190064b2014-08-09 15:33:00 -0600147 force_build: Force a build even if one was previously done
148 force_build_failures: Force a bulid if the previous result showed
149 failure
Simon Glassd829f122020-03-18 09:42:42 -0600150 work_in_output: Use the output directory as the work directory and
151 don't write to a separate output directory.
Simon Glass2b4806e2022-01-22 05:07:33 -0700152 adjust_cfg (list of str): List of changes to make to .config file
153 before building. Each is one of (where C is either CONFIG_xxx
154 or just xxx):
155 C to enable C
156 ~C to disable C
157 C=val to set the value of C (val must have quotes if C is
158 a string Kconfig
Simon Glass190064b2014-08-09 15:33:00 -0600159
160 Returns:
161 tuple containing:
162 - CommandResult object containing the results of the build
163 - boolean indicating whether 'make config' is still needed
164 """
165 # Create a default result - it will be overwritte by the call to
166 # self.Make() below, in the event that we do a build.
167 result = command.CommandResult()
168 result.return_code = 0
Simon Glassd829f122020-03-18 09:42:42 -0600169 if work_in_output or self.builder.in_tree:
Simon Glass190064b2014-08-09 15:33:00 -0600170 out_dir = work_dir
171 else:
Stephen Warrenf79f1e02016-04-11 10:48:44 -0600172 if self.per_board_out_dir:
173 out_rel_dir = os.path.join('..', brd.target)
174 else:
175 out_rel_dir = 'build'
176 out_dir = os.path.join(work_dir, out_rel_dir)
Simon Glass190064b2014-08-09 15:33:00 -0600177
178 # Check if the job was already completed last time
179 done_file = self.builder.GetDoneFile(commit_upto, brd.target)
180 result.already_done = os.path.exists(done_file)
181 will_build = (force_build or force_build_failures or
182 not result.already_done)
Simon Glassfb3954f2014-09-05 19:00:17 -0600183 if result.already_done:
Simon Glass190064b2014-08-09 15:33:00 -0600184 # Get the return code from that build and use it
185 with open(done_file, 'r') as fd:
Simon Glasse74429b2018-12-10 09:05:23 -0700186 try:
187 result.return_code = int(fd.readline())
188 except ValueError:
189 # The file may be empty due to running out of disk space.
190 # Try a rebuild
191 result.return_code = RETURN_CODE_RETRY
Simon Glass88c8dcf2015-02-05 22:06:13 -0700192
193 # Check the signal that the build needs to be retried
194 if result.return_code == RETURN_CODE_RETRY:
195 will_build = True
196 elif will_build:
Simon Glassfb3954f2014-09-05 19:00:17 -0600197 err_file = self.builder.GetErrFile(commit_upto, brd.target)
198 if os.path.exists(err_file) and os.stat(err_file).st_size:
199 result.stderr = 'bad'
200 elif not force_build:
201 # The build passed, so no need to build it again
202 will_build = False
Simon Glass190064b2014-08-09 15:33:00 -0600203
204 if will_build:
205 # We are going to have to build it. First, get a toolchain
206 if not self.toolchain:
207 try:
208 self.toolchain = self.builder.toolchains.Select(brd.arch)
209 except ValueError as err:
210 result.return_code = 10
211 result.stdout = ''
212 result.stderr = str(err)
213 # TODO(sjg@chromium.org): This gets swallowed, but needs
214 # to be reported.
215
216 if self.toolchain:
217 # Checkout the right commit
218 if self.builder.commits:
219 commit = self.builder.commits[commit_upto]
220 if self.builder.checkout:
221 git_dir = os.path.join(work_dir, '.git')
Simon Glass0157b182022-01-29 14:14:11 -0700222 gitutil.checkout(commit.hash, git_dir, work_dir,
Simon Glass190064b2014-08-09 15:33:00 -0600223 force=True)
224 else:
225 commit = 'current'
226
227 # Set up the environment and command line
Simon Glassbb1501f2014-12-01 17:34:00 -0700228 env = self.toolchain.MakeEnvironment(self.builder.full_path)
Simon Glass190064b2014-08-09 15:33:00 -0600229 Mkdir(out_dir)
230 args = []
231 cwd = work_dir
Simon Glass48c1b6a2014-08-28 09:43:42 -0600232 src_dir = os.path.realpath(work_dir)
Simon Glass190064b2014-08-09 15:33:00 -0600233 if not self.builder.in_tree:
234 if commit_upto is None:
235 # In this case we are building in the original source
236 # directory (i.e. the current directory where buildman
237 # is invoked. The output directory is set to this
238 # thread's selected work directory.
239 #
240 # Symlinks can confuse U-Boot's Makefile since
241 # we may use '..' in our path, so remove them.
Stephen Warrenf79f1e02016-04-11 10:48:44 -0600242 out_dir = os.path.realpath(out_dir)
243 args.append('O=%s' % out_dir)
Simon Glass190064b2014-08-09 15:33:00 -0600244 cwd = None
Simon Glass48c1b6a2014-08-28 09:43:42 -0600245 src_dir = os.getcwd()
Simon Glass190064b2014-08-09 15:33:00 -0600246 else:
Stephen Warrenf79f1e02016-04-11 10:48:44 -0600247 args.append('O=%s' % out_rel_dir)
Tom Rinif5e5ece2015-04-01 07:47:41 -0400248 if self.builder.verbose_build:
249 args.append('V=1')
250 else:
Simon Glassd2ce6582014-12-01 17:34:07 -0700251 args.append('-s')
Simon Glass190064b2014-08-09 15:33:00 -0600252 if self.builder.num_jobs is not None:
253 args.extend(['-j', str(self.builder.num_jobs)])
Daniel Schwierzeck2371d1b2018-01-26 16:31:05 +0100254 if self.builder.warnings_as_errors:
255 args.append('KCFLAGS=-Werror')
Simon Glass190064b2014-08-09 15:33:00 -0600256 config_args = ['%s_defconfig' % brd.target]
257 config_out = ''
258 args.extend(self.builder.toolchains.GetMakeArguments(brd))
Simon Glass00beb242019-01-07 16:44:20 -0700259 args.extend(self.toolchain.MakeArgs())
Simon Glass190064b2014-08-09 15:33:00 -0600260
Simon Glass73da3d22020-12-16 17:24:17 -0700261 # Remove any output targets. Since we use a build directory that
262 # was previously used by another board, it may have produced an
263 # SPL image. If we don't remove it (i.e. see do_config and
264 # self.mrproper below) then it will appear to be the output of
265 # this build, even if it does not produce SPL images.
266 build_dir = self.builder.GetBuildDir(commit_upto, brd.target)
267 for elf in BASE_ELF_FILENAMES:
268 fname = os.path.join(out_dir, elf)
269 if os.path.exists(fname):
270 os.remove(fname)
271
Simon Glass190064b2014-08-09 15:33:00 -0600272 # If we need to reconfigure, do that now
Simon Glass2b4806e2022-01-22 05:07:33 -0700273 cfg_file = os.path.join(out_dir, '.config')
274 if do_config or adjust_cfg:
Stephen Warrenf79f1e02016-04-11 10:48:44 -0600275 config_out = ''
Simon Glasseb70a2c2020-04-09 15:08:51 -0600276 if self.mrproper:
Stephen Warrenf79f1e02016-04-11 10:48:44 -0600277 result = self.Make(commit, brd, 'mrproper', cwd,
278 'mrproper', *args, env=env)
279 config_out += result.combined
Simon Glass190064b2014-08-09 15:33:00 -0600280 result = self.Make(commit, brd, 'config', cwd,
281 *(args + config_args), env=env)
Simon Glass40f11fc2015-02-05 22:06:12 -0700282 config_out += result.combined
Simon Glass190064b2014-08-09 15:33:00 -0600283 do_config = False # No need to configure next time
Simon Glass2b4806e2022-01-22 05:07:33 -0700284 if adjust_cfg:
285 cfgutil.adjust_cfg_file(cfg_file, adjust_cfg)
Simon Glass190064b2014-08-09 15:33:00 -0600286 if result.return_code == 0:
Simon Glassa9401b22016-11-16 14:09:25 -0700287 if config_only:
Simon Glassb50113f2016-11-13 14:25:51 -0700288 args.append('cfg')
Simon Glass190064b2014-08-09 15:33:00 -0600289 result = self.Make(commit, brd, 'build', cwd, *args,
290 env=env)
Simon Glass35b6e532022-11-09 19:14:48 -0700291 if (result.return_code == 2 and
292 ('Some images are invalid' in result.stderr)):
293 # This is handled later by the check for output in
294 # stderr
295 result.return_code = 0
Simon Glass2b4806e2022-01-22 05:07:33 -0700296 if adjust_cfg:
297 errs = cfgutil.check_cfg_file(cfg_file, adjust_cfg)
298 if errs:
Simon Glass2b4806e2022-01-22 05:07:33 -0700299 result.stderr += errs
300 result.return_code = 1
Simon Glass48c1b6a2014-08-28 09:43:42 -0600301 result.stderr = result.stderr.replace(src_dir + '/', '')
Simon Glass40f11fc2015-02-05 22:06:12 -0700302 if self.builder.verbose_build:
303 result.stdout = config_out + result.stdout
Simon Glass190064b2014-08-09 15:33:00 -0600304 else:
305 result.return_code = 1
306 result.stderr = 'No tool chain for %s\n' % brd.arch
307 result.already_done = False
308
309 result.toolchain = self.toolchain
310 result.brd = brd
311 result.commit_upto = commit_upto
312 result.out_dir = out_dir
313 return result, do_config
314
Simon Glassd829f122020-03-18 09:42:42 -0600315 def _WriteResult(self, result, keep_outputs, work_in_output):
Simon Glass190064b2014-08-09 15:33:00 -0600316 """Write a built result to the output directory.
317
318 Args:
319 result: CommandResult object containing result to write
320 keep_outputs: True to store the output binaries, False
321 to delete them
Simon Glassd829f122020-03-18 09:42:42 -0600322 work_in_output: Use the output directory as the work directory and
323 don't write to a separate output directory.
Simon Glass190064b2014-08-09 15:33:00 -0600324 """
Simon Glass88c8dcf2015-02-05 22:06:13 -0700325 # If we think this might have been aborted with Ctrl-C, record the
326 # failure but not that we are 'done' with this board. A retry may fix
327 # it.
Simon Glassbafdeb42021-10-19 21:43:23 -0600328 maybe_aborted = result.stderr and 'No child processes' in result.stderr
Simon Glass190064b2014-08-09 15:33:00 -0600329
Simon Glassbafdeb42021-10-19 21:43:23 -0600330 if result.return_code >= 0 and result.already_done:
Simon Glass190064b2014-08-09 15:33:00 -0600331 return
332
333 # Write the output and stderr
334 output_dir = self.builder._GetOutputDir(result.commit_upto)
335 Mkdir(output_dir)
336 build_dir = self.builder.GetBuildDir(result.commit_upto,
337 result.brd.target)
338 Mkdir(build_dir)
339
340 outfile = os.path.join(build_dir, 'log')
341 with open(outfile, 'w') as fd:
342 if result.stdout:
Simon Glassc05aa032019-10-31 07:42:53 -0600343 fd.write(result.stdout)
Simon Glass190064b2014-08-09 15:33:00 -0600344
345 errfile = self.builder.GetErrFile(result.commit_upto,
346 result.brd.target)
347 if result.stderr:
348 with open(errfile, 'w') as fd:
Simon Glassc05aa032019-10-31 07:42:53 -0600349 fd.write(result.stderr)
Simon Glass190064b2014-08-09 15:33:00 -0600350 elif os.path.exists(errfile):
351 os.remove(errfile)
352
Simon Glassbafdeb42021-10-19 21:43:23 -0600353 # Fatal error
354 if result.return_code < 0:
355 return
356
Simon Glass190064b2014-08-09 15:33:00 -0600357 if result.toolchain:
358 # Write the build result and toolchain information.
359 done_file = self.builder.GetDoneFile(result.commit_upto,
360 result.brd.target)
361 with open(done_file, 'w') as fd:
Simon Glass88c8dcf2015-02-05 22:06:13 -0700362 if maybe_aborted:
363 # Special code to indicate we need to retry
364 fd.write('%s' % RETURN_CODE_RETRY)
365 else:
366 fd.write('%s' % result.return_code)
Simon Glass190064b2014-08-09 15:33:00 -0600367 with open(os.path.join(build_dir, 'toolchain'), 'w') as fd:
Simon Glassc05aa032019-10-31 07:42:53 -0600368 print('gcc', result.toolchain.gcc, file=fd)
369 print('path', result.toolchain.path, file=fd)
370 print('cross', result.toolchain.cross, file=fd)
371 print('arch', result.toolchain.arch, file=fd)
Simon Glass190064b2014-08-09 15:33:00 -0600372 fd.write('%s' % result.return_code)
373
Simon Glass190064b2014-08-09 15:33:00 -0600374 # Write out the image and function size information and an objdump
Simon Glassbb1501f2014-12-01 17:34:00 -0700375 env = result.toolchain.MakeEnvironment(self.builder.full_path)
Simon Glassf1a83ab2021-04-11 16:27:28 +1200376 with open(os.path.join(build_dir, 'out-env'), 'wb') as fd:
Simon Glasse5fc79e2019-01-07 16:44:23 -0700377 for var in sorted(env.keys()):
Simon Glassf1a83ab2021-04-11 16:27:28 +1200378 fd.write(b'%s="%s"' % (var, env[var]))
Simon Glass190064b2014-08-09 15:33:00 -0600379 lines = []
Simon Glass73da3d22020-12-16 17:24:17 -0700380 for fname in BASE_ELF_FILENAMES:
Simon Glass190064b2014-08-09 15:33:00 -0600381 cmd = ['%snm' % self.toolchain.cross, '--size-sort', fname]
Simon Glassd9800692022-01-29 14:14:05 -0700382 nm_result = command.run_pipe([cmd], capture=True,
Simon Glass190064b2014-08-09 15:33:00 -0600383 capture_stderr=True, cwd=result.out_dir,
384 raise_on_error=False, env=env)
385 if nm_result.stdout:
386 nm = self.builder.GetFuncSizesFile(result.commit_upto,
387 result.brd.target, fname)
388 with open(nm, 'w') as fd:
Simon Glassc05aa032019-10-31 07:42:53 -0600389 print(nm_result.stdout, end=' ', file=fd)
Simon Glass190064b2014-08-09 15:33:00 -0600390
391 cmd = ['%sobjdump' % self.toolchain.cross, '-h', fname]
Simon Glassd9800692022-01-29 14:14:05 -0700392 dump_result = command.run_pipe([cmd], capture=True,
Simon Glass190064b2014-08-09 15:33:00 -0600393 capture_stderr=True, cwd=result.out_dir,
394 raise_on_error=False, env=env)
395 rodata_size = ''
396 if dump_result.stdout:
397 objdump = self.builder.GetObjdumpFile(result.commit_upto,
398 result.brd.target, fname)
399 with open(objdump, 'w') as fd:
Simon Glassc05aa032019-10-31 07:42:53 -0600400 print(dump_result.stdout, end=' ', file=fd)
Simon Glass190064b2014-08-09 15:33:00 -0600401 for line in dump_result.stdout.splitlines():
402 fields = line.split()
403 if len(fields) > 5 and fields[1] == '.rodata':
404 rodata_size = fields[2]
405
406 cmd = ['%ssize' % self.toolchain.cross, fname]
Simon Glassd9800692022-01-29 14:14:05 -0700407 size_result = command.run_pipe([cmd], capture=True,
Simon Glass190064b2014-08-09 15:33:00 -0600408 capture_stderr=True, cwd=result.out_dir,
409 raise_on_error=False, env=env)
410 if size_result.stdout:
411 lines.append(size_result.stdout.splitlines()[1] + ' ' +
412 rodata_size)
413
Alex Kiernan0ddc5102018-05-31 04:48:33 +0000414 # Extract the environment from U-Boot and dump it out
415 cmd = ['%sobjcopy' % self.toolchain.cross, '-O', 'binary',
416 '-j', '.rodata.default_environment',
417 'env/built-in.o', 'uboot.env']
Simon Glassd9800692022-01-29 14:14:05 -0700418 command.run_pipe([cmd], capture=True,
Alex Kiernan0ddc5102018-05-31 04:48:33 +0000419 capture_stderr=True, cwd=result.out_dir,
420 raise_on_error=False, env=env)
421 ubootenv = os.path.join(result.out_dir, 'uboot.env')
Simon Glass60b285f2020-04-17 17:51:34 -0600422 if not work_in_output:
423 self.CopyFiles(result.out_dir, build_dir, '', ['uboot.env'])
Alex Kiernan0ddc5102018-05-31 04:48:33 +0000424
Simon Glass190064b2014-08-09 15:33:00 -0600425 # Write out the image sizes file. This is similar to the output
426 # of binutil's 'size' utility, but it omits the header line and
427 # adds an additional hex value at the end of each line for the
428 # rodata size
429 if len(lines):
430 sizes = self.builder.GetSizesFile(result.commit_upto,
431 result.brd.target)
432 with open(sizes, 'w') as fd:
Simon Glassc05aa032019-10-31 07:42:53 -0600433 print('\n'.join(lines), file=fd)
Simon Glass190064b2014-08-09 15:33:00 -0600434
Simon Glass60b285f2020-04-17 17:51:34 -0600435 if not work_in_output:
436 # Write out the configuration files, with a special case for SPL
437 for dirname in ['', 'spl', 'tpl']:
438 self.CopyFiles(
439 result.out_dir, build_dir, dirname,
440 ['u-boot.cfg', 'spl/u-boot-spl.cfg', 'tpl/u-boot-tpl.cfg',
441 '.config', 'include/autoconf.mk',
442 'include/generated/autoconf.h'])
Simon Glass970f9322015-02-05 22:06:14 -0700443
Simon Glass60b285f2020-04-17 17:51:34 -0600444 # Now write the actual build output
445 if keep_outputs:
446 self.CopyFiles(
447 result.out_dir, build_dir, '',
448 ['u-boot*', '*.bin', '*.map', '*.img', 'MLO', 'SPL',
449 'include/autoconf.mk', 'spl/u-boot-spl*'])
Simon Glass190064b2014-08-09 15:33:00 -0600450
Simon Glass970f9322015-02-05 22:06:14 -0700451 def CopyFiles(self, out_dir, build_dir, dirname, patterns):
452 """Copy files from the build directory to the output.
453
454 Args:
455 out_dir: Path to output directory containing the files
456 build_dir: Place to copy the files
457 dirname: Source directory, '' for normal U-Boot, 'spl' for SPL
458 patterns: A list of filenames (strings) to copy, each relative
459 to the build directory
460 """
461 for pattern in patterns:
462 file_list = glob.glob(os.path.join(out_dir, dirname, pattern))
463 for fname in file_list:
464 target = os.path.basename(fname)
465 if dirname:
466 base, ext = os.path.splitext(target)
467 if ext:
468 target = '%s-%s%s' % (base, dirname, ext)
469 shutil.copy(fname, os.path.join(build_dir, target))
Simon Glass190064b2014-08-09 15:33:00 -0600470
Simon Glassab9b4f32021-04-11 16:27:26 +1200471 def _SendResult(self, result):
472 """Send a result to the builder for processing
473
474 Args:
475 result: CommandResult object containing the results of the build
Simon Glass8116c782021-04-11 16:27:27 +1200476
477 Raises:
478 ValueError if self.test_exception is true (for testing)
Simon Glassab9b4f32021-04-11 16:27:26 +1200479 """
Simon Glass8116c782021-04-11 16:27:27 +1200480 if self.test_exception:
481 raise ValueError('test exception')
Simon Glassab9b4f32021-04-11 16:27:26 +1200482 if self.thread_num != -1:
483 self.builder.out_queue.put(result)
484 else:
485 self.builder.ProcessResult(result)
486
Simon Glass190064b2014-08-09 15:33:00 -0600487 def RunJob(self, job):
488 """Run a single job
489
490 A job consists of a building a list of commits for a particular board.
491
492 Args:
493 job: Job to build
Simon Glassb82492b2021-01-30 22:17:46 -0700494
495 Returns:
496 List of Result objects
Simon Glass190064b2014-08-09 15:33:00 -0600497 """
Simon Glassf4ed4702022-07-11 19:03:57 -0600498 brd = job.brd
Simon Glass190064b2014-08-09 15:33:00 -0600499 work_dir = self.builder.GetThreadDir(self.thread_num)
500 self.toolchain = None
501 if job.commits:
502 # Run 'make board_defconfig' on the first commit
503 do_config = True
504 commit_upto = 0
505 force_build = False
506 for commit_upto in range(0, len(job.commits), job.step):
507 result, request_config = self.RunCommit(commit_upto, brd,
Simon Glassa9401b22016-11-16 14:09:25 -0700508 work_dir, do_config, self.builder.config_only,
Simon Glass190064b2014-08-09 15:33:00 -0600509 force_build or self.builder.force_build,
Simon Glassd829f122020-03-18 09:42:42 -0600510 self.builder.force_build_failures,
Simon Glass2b4806e2022-01-22 05:07:33 -0700511 job.work_in_output, job.adjust_cfg)
Simon Glass190064b2014-08-09 15:33:00 -0600512 failed = result.return_code or result.stderr
513 did_config = do_config
514 if failed and not do_config:
515 # If our incremental build failed, try building again
516 # with a reconfig.
517 if self.builder.force_config_on_failure:
518 result, request_config = self.RunCommit(commit_upto,
Simon Glassd829f122020-03-18 09:42:42 -0600519 brd, work_dir, True, False, True, False,
Simon Glass2b4806e2022-01-22 05:07:33 -0700520 job.work_in_output, job.adjust_cfg)
Simon Glass190064b2014-08-09 15:33:00 -0600521 did_config = True
522 if not self.builder.force_reconfig:
523 do_config = request_config
524
525 # If we built that commit, then config is done. But if we got
526 # an warning, reconfig next time to force it to build the same
527 # files that created warnings this time. Otherwise an
528 # incremental build may not build the same file, and we will
529 # think that the warning has gone away.
530 # We could avoid this by using -Werror everywhere...
531 # For errors, the problem doesn't happen, since presumably
532 # the build stopped and didn't generate output, so will retry
533 # that file next time. So we could detect warnings and deal
534 # with them specially here. For now, we just reconfigure if
535 # anything goes work.
536 # Of course this is substantially slower if there are build
537 # errors/warnings (e.g. 2-3x slower even if only 10% of builds
538 # have problems).
539 if (failed and not result.already_done and not did_config and
540 self.builder.force_config_on_failure):
541 # If this build failed, try the next one with a
542 # reconfigure.
543 # Sometimes if the board_config.h file changes it can mess
544 # with dependencies, and we get:
545 # make: *** No rule to make target `include/autoconf.mk',
546 # needed by `depend'.
547 do_config = True
548 force_build = True
549 else:
550 force_build = False
551 if self.builder.force_config_on_failure:
552 if failed:
553 do_config = True
554 result.commit_upto = commit_upto
555 if result.return_code < 0:
556 raise ValueError('Interrupt')
557
558 # We have the build results, so output the result
Simon Glassd829f122020-03-18 09:42:42 -0600559 self._WriteResult(result, job.keep_outputs, job.work_in_output)
Simon Glassab9b4f32021-04-11 16:27:26 +1200560 self._SendResult(result)
Simon Glass190064b2014-08-09 15:33:00 -0600561 else:
562 # Just build the currently checked-out build
563 result, request_config = self.RunCommit(None, brd, work_dir, True,
Simon Glassa9401b22016-11-16 14:09:25 -0700564 self.builder.config_only, True,
Simon Glass2b4806e2022-01-22 05:07:33 -0700565 self.builder.force_build_failures, job.work_in_output,
566 job.adjust_cfg)
Simon Glass190064b2014-08-09 15:33:00 -0600567 result.commit_upto = 0
Simon Glassd829f122020-03-18 09:42:42 -0600568 self._WriteResult(result, job.keep_outputs, job.work_in_output)
Simon Glassab9b4f32021-04-11 16:27:26 +1200569 self._SendResult(result)
Simon Glass190064b2014-08-09 15:33:00 -0600570
571 def run(self):
572 """Our thread's run function
573
574 This thread picks a job from the queue, runs it, and then goes to the
575 next job.
576 """
Simon Glass190064b2014-08-09 15:33:00 -0600577 while True:
578 job = self.builder.queue.get()
Simon Glass8116c782021-04-11 16:27:27 +1200579 try:
580 self.RunJob(job)
581 except Exception as e:
Simon Glass8ca09312022-01-22 05:07:32 -0700582 print('Thread exception (use -T0 to run without threads):', e)
Simon Glass8116c782021-04-11 16:27:27 +1200583 self.builder.thread_exceptions.append(e)
Simon Glass190064b2014-08-09 15:33:00 -0600584 self.builder.queue.task_done()