blob: 78405956ef51c4410f1d67cf46f6f91074892986 [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
Simon Glass606e5432023-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 Glass190064b2014-08-09 15:33:00 -060011import errno
12import glob
Simon Glassdab3a4a2023-07-19 17:49:16 -060013import io
Simon Glass190064b2014-08-09 15:33:00 -060014import os
15import shutil
Lothar Waßmann409fc022018-04-08 05:14:11 -060016import sys
Simon Glass190064b2014-08-09 15:33:00 -060017import threading
18
Simon Glass2b4806e2022-01-22 05:07:33 -070019from buildman import cfgutil
Simon Glassbf776672020-04-17 18:09:04 -060020from patman import gitutil
Simon Glass4583c002023-02-23 18:18:04 -070021from u_boot_pylib import command
Simon Glass190064b2014-08-09 15:33:00 -060022
Simon Glass88c8dcf2015-02-05 22:06:13 -070023RETURN_CODE_RETRY = -1
Simon Glass73da3d22020-12-16 17:24:17 -070024BASE_ELF_FILENAMES = ['u-boot', 'spl/u-boot-spl', 'tpl/u-boot-tpl']
Simon Glass88c8dcf2015-02-05 22:06:13 -070025
Simon Glassf06d3332023-07-19 17:49:08 -060026def mkdir(dirname, parents = False):
Simon Glass190064b2014-08-09 15:33:00 -060027 """Make a directory if it doesn't already exist.
28
29 Args:
30 dirname: Directory to create
31 """
32 try:
Thierry Redingf3d015c2014-08-19 10:22:39 +020033 if parents:
34 os.makedirs(dirname)
35 else:
36 os.mkdir(dirname)
Simon Glass190064b2014-08-09 15:33:00 -060037 except OSError as err:
38 if err.errno == errno.EEXIST:
Lothar Waßmann409fc022018-04-08 05:14:11 -060039 if os.path.realpath('.') == os.path.realpath(dirname):
Simon Glass606e5432023-07-19 17:49:09 -060040 print(f"Cannot create the current working directory '{dirname}'!")
Lothar Waßmann409fc022018-04-08 05:14:11 -060041 sys.exit(1)
Simon Glass190064b2014-08-09 15:33:00 -060042 else:
43 raise
44
Simon Glasse5490b72023-07-19 17:49:20 -060045
46def _remove_old_outputs(out_dir):
47 """Remove any old output-target files
48
49 Args:
50 out_dir (str): Output directory for the build
51
52 Since we use a build directory that was previously used by another
53 board, it may have produced an SPL image. If we don't remove it (i.e.
54 see do_config and self.mrproper below) then it will appear to be the
55 output of this build, even if it does not produce SPL images.
56 """
57 for elf in BASE_ELF_FILENAMES:
58 fname = os.path.join(out_dir, elf)
59 if os.path.exists(fname):
60 os.remove(fname)
61
62
Simon Glass606e5432023-07-19 17:49:09 -060063# pylint: disable=R0903
Simon Glass190064b2014-08-09 15:33:00 -060064class BuilderJob:
65 """Holds information about a job to be performed by a thread
66
67 Members:
Simon Glassf4ed4702022-07-11 19:03:57 -060068 brd: Board object to build
Simon Glasse9fbbf62020-03-18 09:42:41 -060069 commits: List of Commit objects to build
70 keep_outputs: True to save build output files
71 step: 1 to process every commit, n to process every nth commit
Simon Glassd829f122020-03-18 09:42:42 -060072 work_in_output: Use the output directory as the work directory and
73 don't write to a separate output directory.
Simon Glass190064b2014-08-09 15:33:00 -060074 """
75 def __init__(self):
Simon Glassf4ed4702022-07-11 19:03:57 -060076 self.brd = None
Simon Glass190064b2014-08-09 15:33:00 -060077 self.commits = []
Simon Glasse9fbbf62020-03-18 09:42:41 -060078 self.keep_outputs = False
79 self.step = 1
Simon Glassd829f122020-03-18 09:42:42 -060080 self.work_in_output = False
Simon Glass190064b2014-08-09 15:33:00 -060081
82
83class ResultThread(threading.Thread):
84 """This thread processes results from builder threads.
85
86 It simply passes the results on to the builder. There is only one
87 result thread, and this helps to serialise the build output.
88 """
89 def __init__(self, builder):
90 """Set up a new result thread
91
92 Args:
93 builder: Builder which will be sent each result
94 """
95 threading.Thread.__init__(self)
96 self.builder = builder
97
98 def run(self):
99 """Called to start up the result thread.
100
101 We collect the next result job and pass it on to the build.
102 """
103 while True:
104 result = self.builder.out_queue.get()
Simon Glass37edf5f2023-07-19 17:49:06 -0600105 self.builder.process_result(result)
Simon Glass190064b2014-08-09 15:33:00 -0600106 self.builder.out_queue.task_done()
107
108
109class BuilderThread(threading.Thread):
110 """This thread builds U-Boot for a particular board.
111
112 An input queue provides each new job. We run 'make' to build U-Boot
113 and then pass the results on to the output queue.
114
115 Members:
116 builder: The builder which contains information we might need
117 thread_num: Our thread number (0-n-1), used to decide on a
Simon Glass24993312021-04-11 16:27:25 +1200118 temporary directory. If this is -1 then there are no threads
119 and we are the (only) main process
120 mrproper: Use 'make mrproper' before each reconfigure
121 per_board_out_dir: True to build in a separate persistent directory per
122 board rather than a thread-specific directory
123 test_exception: Used for testing; True to raise an exception instead of
124 reporting the build result
Simon Glass190064b2014-08-09 15:33:00 -0600125 """
Simon Glass8116c782021-04-11 16:27:27 +1200126 def __init__(self, builder, thread_num, mrproper, per_board_out_dir,
127 test_exception=False):
Simon Glass190064b2014-08-09 15:33:00 -0600128 """Set up a new builder thread"""
129 threading.Thread.__init__(self)
130 self.builder = builder
131 self.thread_num = thread_num
Simon Glasseb70a2c2020-04-09 15:08:51 -0600132 self.mrproper = mrproper
Stephen Warrenf79f1e02016-04-11 10:48:44 -0600133 self.per_board_out_dir = per_board_out_dir
Simon Glass8116c782021-04-11 16:27:27 +1200134 self.test_exception = test_exception
Simon Glass606e5432023-07-19 17:49:09 -0600135 self.toolchain = None
Simon Glass190064b2014-08-09 15:33:00 -0600136
Simon Glassf06d3332023-07-19 17:49:08 -0600137 def make(self, commit, brd, stage, cwd, *args, **kwargs):
Simon Glass190064b2014-08-09 15:33:00 -0600138 """Run 'make' on a particular commit and board.
139
140 The source code will already be checked out, so the 'commit'
141 argument is only for information.
142
143 Args:
144 commit: Commit object that is being built
145 brd: Board object that is being built
146 stage: Stage of the build. Valid stages are:
Roger Meierfd18a892014-08-20 22:10:29 +0200147 mrproper - can be called to clean source
Simon Glass190064b2014-08-09 15:33:00 -0600148 config - called to configure for a board
149 build - the main make invocation - it does the build
150 args: A list of arguments to pass to 'make'
Simon Glassd9800692022-01-29 14:14:05 -0700151 kwargs: A list of keyword arguments to pass to command.run_pipe()
Simon Glass190064b2014-08-09 15:33:00 -0600152
153 Returns:
154 CommandResult object
155 """
156 return self.builder.do_make(commit, brd, stage, cwd, *args,
157 **kwargs)
158
Simon Glassed007bf2023-07-19 17:49:15 -0600159 def _build_args(self, brd, out_dir, out_rel_dir, work_dir, commit_upto):
Simon Glass75247002023-07-19 17:49:13 -0600160 """Set up arguments to the args list based on the settings
161
162 Args:
Simon Glassa06ed7f2023-07-19 17:49:14 -0600163 brd (Board): Board to create arguments for
Simon Glassed007bf2023-07-19 17:49:15 -0600164 out_dir (str): Path to output directory containing the files
165 out_rel_dir (str): Output directory relative to the current dir
166 work_dir (str): Directory to which the source will be checked out
167 commit_upto (int): Commit number to build (0...n-1)
168
169 Returns:
170 tuple:
171 list of str: Arguments to pass to make
172 str: Current working directory, or None if no commit
173 str: Source directory (typically the work directory)
Simon Glass75247002023-07-19 17:49:13 -0600174 """
Simon Glassed007bf2023-07-19 17:49:15 -0600175 args = []
176 cwd = work_dir
177 src_dir = os.path.realpath(work_dir)
178 if not self.builder.in_tree:
179 if commit_upto is None:
180 # In this case we are building in the original source directory
181 # (i.e. the current directory where buildman is invoked. The
182 # output directory is set to this thread's selected work
183 # directory.
184 #
185 # Symlinks can confuse U-Boot's Makefile since we may use '..'
186 # in our path, so remove them.
187 real_dir = os.path.realpath(out_dir)
188 args.append(f'O={real_dir}')
189 cwd = None
190 src_dir = os.getcwd()
191 else:
192 args.append(f'O={out_rel_dir}')
Simon Glass75247002023-07-19 17:49:13 -0600193 if self.builder.verbose_build:
194 args.append('V=1')
195 else:
196 args.append('-s')
197 if self.builder.num_jobs is not None:
198 args.extend(['-j', str(self.builder.num_jobs)])
199 if self.builder.warnings_as_errors:
200 args.append('KCFLAGS=-Werror')
201 args.append('HOSTCFLAGS=-Werror')
202 if self.builder.allow_missing:
203 args.append('BINMAN_ALLOW_MISSING=1')
204 if self.builder.no_lto:
205 args.append('NO_LTO=1')
206 if self.builder.reproducible_builds:
207 args.append('SOURCE_DATE_EPOCH=0')
Simon Glassa06ed7f2023-07-19 17:49:14 -0600208 args.extend(self.builder.toolchains.GetMakeArguments(brd))
209 args.extend(self.toolchain.MakeArgs())
Simon Glassed007bf2023-07-19 17:49:15 -0600210 return args, cwd, src_dir
Simon Glass75247002023-07-19 17:49:13 -0600211
Simon Glassec2f4922023-07-19 17:49:17 -0600212 def _reconfigure(self, commit, brd, cwd, args, env, config_args, config_out,
213 cmd_list):
214 """Reconfigure the build
215
216 Args:
217 commit (Commit): Commit only being built
218 brd (Board): Board being built
219 cwd (str): Current working directory
220 args (list of str): Arguments to pass to make
221 env (dict): Environment strings
222 config_args (list of str): defconfig arg for this board
223 cmd_list (list of str): List to add the commands to, for logging
224
225 Returns:
226 CommandResult object
227 """
228 if self.mrproper:
229 result = self.make(commit, brd, 'mrproper', cwd, 'mrproper', *args,
230 env=env)
231 config_out.write(result.combined)
232 cmd_list.append([self.builder.gnu_make, 'mrproper', *args])
233 result = self.make(commit, brd, 'config', cwd, *(args + config_args),
234 env=env)
235 cmd_list.append([self.builder.gnu_make] + args + config_args)
236 config_out.write(result.combined)
237 return result
238
Simon Glass14c15232023-07-19 17:49:18 -0600239 def _build(self, commit, brd, cwd, args, env, cmd_list, config_only):
240 """Perform the build
241
242 Args:
243 commit (Commit): Commit only being built
244 brd (Board): Board being built
245 cwd (str): Current working directory
246 args (list of str): Arguments to pass to make
247 env (dict): Environment strings
248 cmd_list (list of str): List to add the commands to, for logging
249 config_only (bool): True if this is a config-only build (using the
250 'make cfg' target)
251
252 Returns:
253 CommandResult object
254 """
255 if config_only:
256 args.append('cfg')
257 result = self.make(commit, brd, 'build', cwd, *args, env=env)
258 cmd_list.append([self.builder.gnu_make] + args)
259 if (result.return_code == 2 and
260 ('Some images are invalid' in result.stderr)):
261 # This is handled later by the check for output in stderr
262 result.return_code = 0
263 return result
264
Simon Glass4981bd32023-07-19 17:49:19 -0600265 def _read_done_file(self, commit_upto, brd, result, force_build,
266 force_build_failures):
267 """Check the 'done' file and see if this commit should be built
268
269 Args:
270 commit (Commit): Commit only being built
271 brd (Board): Board being built
272 result (CommandResult): result object to update
273 force_build (bool): Force a build even if one was previously done
274 force_build_failures (bool): Force a bulid if the previous result
275 showed failure
276
277 Returns:
278 bool: True if build should be built
279 """
280 done_file = self.builder.get_done_file(commit_upto, brd.target)
281 result.already_done = os.path.exists(done_file)
282 will_build = (force_build or force_build_failures or
283 not result.already_done)
284 if result.already_done:
285 with open(done_file, 'r', encoding='utf-8') as outf:
286 try:
287 result.return_code = int(outf.readline())
288 except ValueError:
289 # The file may be empty due to running out of disk space.
290 # Try a rebuild
291 result.return_code = RETURN_CODE_RETRY
292
293 # Check the signal that the build needs to be retried
294 if result.return_code == RETURN_CODE_RETRY:
295 will_build = True
296 elif will_build:
297 err_file = self.builder.get_err_file(commit_upto, brd.target)
298 if os.path.exists(err_file) and os.stat(err_file).st_size:
299 result.stderr = 'bad'
300 elif not force_build:
301 # The build passed, so no need to build it again
302 will_build = False
303 return will_build
304
Simon Glass9bdf0232023-07-19 17:49:21 -0600305 def _decide_dirs(self, brd, work_dir, work_in_output):
306 """Decide the output directory to use
307
308 Args:
309 work_dir (str): Directory to which the source will be checked out
310 work_in_output (bool): Use the output directory as the work
311 directory and don't write to a separate output directory.
312
313 Returns:
314 tuple:
315 out_dir (str): Output directory for the build
316 out_rel_dir (str): Output directory relatie to the current dir
317 """
318 if work_in_output or self.builder.in_tree:
319 out_rel_dir = None
320 out_dir = work_dir
321 else:
322 if self.per_board_out_dir:
323 out_rel_dir = os.path.join('..', brd.target)
324 else:
325 out_rel_dir = 'build'
326 out_dir = os.path.join(work_dir, out_rel_dir)
327 return out_dir, out_rel_dir
328
Simon Glassf06d3332023-07-19 17:49:08 -0600329 def run_commit(self, commit_upto, brd, work_dir, do_config, config_only,
Simon Glass2b4806e2022-01-22 05:07:33 -0700330 force_build, force_build_failures, work_in_output,
331 adjust_cfg):
Simon Glass190064b2014-08-09 15:33:00 -0600332 """Build a particular commit.
333
334 If the build is already done, and we are not forcing a build, we skip
335 the build and just return the previously-saved results.
336
337 Args:
338 commit_upto: Commit number to build (0...n-1)
339 brd: Board object to build
340 work_dir: Directory to which the source will be checked out
341 do_config: True to run a make <board>_defconfig on the source
Simon Glassa9401b22016-11-16 14:09:25 -0700342 config_only: Only configure the source, do not build it
Simon Glass190064b2014-08-09 15:33:00 -0600343 force_build: Force a build even if one was previously done
344 force_build_failures: Force a bulid if the previous result showed
345 failure
Simon Glassd829f122020-03-18 09:42:42 -0600346 work_in_output: Use the output directory as the work directory and
347 don't write to a separate output directory.
Simon Glass2b4806e2022-01-22 05:07:33 -0700348 adjust_cfg (list of str): List of changes to make to .config file
349 before building. Each is one of (where C is either CONFIG_xxx
350 or just xxx):
351 C to enable C
352 ~C to disable C
353 C=val to set the value of C (val must have quotes if C is
354 a string Kconfig
Simon Glass190064b2014-08-09 15:33:00 -0600355
356 Returns:
357 tuple containing:
358 - CommandResult object containing the results of the build
359 - boolean indicating whether 'make config' is still needed
360 """
361 # Create a default result - it will be overwritte by the call to
Simon Glassf06d3332023-07-19 17:49:08 -0600362 # self.make() below, in the event that we do a build.
Simon Glass190064b2014-08-09 15:33:00 -0600363 result = command.CommandResult()
364 result.return_code = 0
Simon Glass9bdf0232023-07-19 17:49:21 -0600365 out_dir, out_rel_dir = self._decide_dirs(brd, work_dir, work_in_output)
Simon Glass190064b2014-08-09 15:33:00 -0600366
367 # Check if the job was already completed last time
Simon Glass4981bd32023-07-19 17:49:19 -0600368 will_build = self._read_done_file(commit_upto, brd, result, force_build,
369 force_build_failures)
Simon Glass190064b2014-08-09 15:33:00 -0600370
371 if will_build:
372 # We are going to have to build it. First, get a toolchain
373 if not self.toolchain:
374 try:
375 self.toolchain = self.builder.toolchains.Select(brd.arch)
376 except ValueError as err:
377 result.return_code = 10
378 result.stdout = ''
379 result.stderr = str(err)
380 # TODO(sjg@chromium.org): This gets swallowed, but needs
381 # to be reported.
382
383 if self.toolchain:
384 # Checkout the right commit
385 if self.builder.commits:
386 commit = self.builder.commits[commit_upto]
387 if self.builder.checkout:
388 git_dir = os.path.join(work_dir, '.git')
Simon Glass0157b182022-01-29 14:14:11 -0700389 gitutil.checkout(commit.hash, git_dir, work_dir,
Simon Glass190064b2014-08-09 15:33:00 -0600390 force=True)
391 else:
392 commit = 'current'
393
394 # Set up the environment and command line
Simon Glassbb1501f2014-12-01 17:34:00 -0700395 env = self.toolchain.MakeEnvironment(self.builder.full_path)
Simon Glassf06d3332023-07-19 17:49:08 -0600396 mkdir(out_dir)
Simon Glassed007bf2023-07-19 17:49:15 -0600397
398 args, cwd, src_dir = self._build_args(brd, out_dir, out_rel_dir,
399 work_dir, commit_upto)
Simon Glass606e5432023-07-19 17:49:09 -0600400 config_args = [f'{brd.target}_defconfig']
Simon Glassdab3a4a2023-07-19 17:49:16 -0600401 config_out = io.StringIO()
Simon Glass190064b2014-08-09 15:33:00 -0600402
Simon Glasse5490b72023-07-19 17:49:20 -0600403 _remove_old_outputs(out_dir)
Simon Glass73da3d22020-12-16 17:24:17 -0700404
Simon Glass190064b2014-08-09 15:33:00 -0600405 # If we need to reconfigure, do that now
Simon Glass2b4806e2022-01-22 05:07:33 -0700406 cfg_file = os.path.join(out_dir, '.config')
Simon Glasscd37d5b2023-02-21 12:40:27 -0700407 cmd_list = []
Simon Glass2b4806e2022-01-22 05:07:33 -0700408 if do_config or adjust_cfg:
Simon Glassec2f4922023-07-19 17:49:17 -0600409 result = self._reconfigure(
410 commit, brd, cwd, args, env, config_args, config_out,
411 cmd_list)
Simon Glass190064b2014-08-09 15:33:00 -0600412 do_config = False # No need to configure next time
Simon Glass2b4806e2022-01-22 05:07:33 -0700413 if adjust_cfg:
414 cfgutil.adjust_cfg_file(cfg_file, adjust_cfg)
Simon Glass14c15232023-07-19 17:49:18 -0600415
416 # Now do the build, if everything looks OK
Simon Glass190064b2014-08-09 15:33:00 -0600417 if result.return_code == 0:
Simon Glass14c15232023-07-19 17:49:18 -0600418 result = self._build(commit, brd, cwd, args, env, cmd_list,
419 config_only)
Simon Glass2b4806e2022-01-22 05:07:33 -0700420 if adjust_cfg:
421 errs = cfgutil.check_cfg_file(cfg_file, adjust_cfg)
422 if errs:
Simon Glass2b4806e2022-01-22 05:07:33 -0700423 result.stderr += errs
424 result.return_code = 1
Simon Glass48c1b6a2014-08-28 09:43:42 -0600425 result.stderr = result.stderr.replace(src_dir + '/', '')
Simon Glass40f11fc2015-02-05 22:06:12 -0700426 if self.builder.verbose_build:
Simon Glassdab3a4a2023-07-19 17:49:16 -0600427 result.stdout = config_out.getvalue() + result.stdout
Simon Glasscd37d5b2023-02-21 12:40:27 -0700428 result.cmd_list = cmd_list
Simon Glass190064b2014-08-09 15:33:00 -0600429 else:
430 result.return_code = 1
Simon Glass606e5432023-07-19 17:49:09 -0600431 result.stderr = f'No tool chain for {brd.arch}\n'
Simon Glass190064b2014-08-09 15:33:00 -0600432 result.already_done = False
433
434 result.toolchain = self.toolchain
435 result.brd = brd
436 result.commit_upto = commit_upto
437 result.out_dir = out_dir
438 return result, do_config
439
Simon Glassf06d3332023-07-19 17:49:08 -0600440 def _write_result(self, result, keep_outputs, work_in_output):
Simon Glass190064b2014-08-09 15:33:00 -0600441 """Write a built result to the output directory.
442
443 Args:
444 result: CommandResult object containing result to write
445 keep_outputs: True to store the output binaries, False
446 to delete them
Simon Glassd829f122020-03-18 09:42:42 -0600447 work_in_output: Use the output directory as the work directory and
448 don't write to a separate output directory.
Simon Glass190064b2014-08-09 15:33:00 -0600449 """
Simon Glass88c8dcf2015-02-05 22:06:13 -0700450 # If we think this might have been aborted with Ctrl-C, record the
451 # failure but not that we are 'done' with this board. A retry may fix
452 # it.
Simon Glassbafdeb42021-10-19 21:43:23 -0600453 maybe_aborted = result.stderr and 'No child processes' in result.stderr
Simon Glass190064b2014-08-09 15:33:00 -0600454
Simon Glassbafdeb42021-10-19 21:43:23 -0600455 if result.return_code >= 0 and result.already_done:
Simon Glass190064b2014-08-09 15:33:00 -0600456 return
457
458 # Write the output and stderr
Simon Glass4a7419b2023-07-19 17:49:10 -0600459 output_dir = self.builder.get_output_dir(result.commit_upto)
Simon Glassf06d3332023-07-19 17:49:08 -0600460 mkdir(output_dir)
Simon Glass37edf5f2023-07-19 17:49:06 -0600461 build_dir = self.builder.get_build_dir(result.commit_upto,
Simon Glass190064b2014-08-09 15:33:00 -0600462 result.brd.target)
Simon Glassf06d3332023-07-19 17:49:08 -0600463 mkdir(build_dir)
Simon Glass190064b2014-08-09 15:33:00 -0600464
465 outfile = os.path.join(build_dir, 'log')
Simon Glass606e5432023-07-19 17:49:09 -0600466 with open(outfile, 'w', encoding='utf-8') as outf:
Simon Glass190064b2014-08-09 15:33:00 -0600467 if result.stdout:
Simon Glass606e5432023-07-19 17:49:09 -0600468 outf.write(result.stdout)
Simon Glass190064b2014-08-09 15:33:00 -0600469
Simon Glass37edf5f2023-07-19 17:49:06 -0600470 errfile = self.builder.get_err_file(result.commit_upto,
Simon Glass190064b2014-08-09 15:33:00 -0600471 result.brd.target)
472 if result.stderr:
Simon Glass606e5432023-07-19 17:49:09 -0600473 with open(errfile, 'w', encoding='utf-8') as outf:
474 outf.write(result.stderr)
Simon Glass190064b2014-08-09 15:33:00 -0600475 elif os.path.exists(errfile):
476 os.remove(errfile)
477
Simon Glassbafdeb42021-10-19 21:43:23 -0600478 # Fatal error
479 if result.return_code < 0:
480 return
481
Simon Glass190064b2014-08-09 15:33:00 -0600482 if result.toolchain:
483 # Write the build result and toolchain information.
Simon Glass37edf5f2023-07-19 17:49:06 -0600484 done_file = self.builder.get_done_file(result.commit_upto,
Simon Glass190064b2014-08-09 15:33:00 -0600485 result.brd.target)
Simon Glass606e5432023-07-19 17:49:09 -0600486 with open(done_file, 'w', encoding='utf-8') as outf:
Simon Glass88c8dcf2015-02-05 22:06:13 -0700487 if maybe_aborted:
488 # Special code to indicate we need to retry
Simon Glass606e5432023-07-19 17:49:09 -0600489 outf.write(f'{RETURN_CODE_RETRY}')
Simon Glass88c8dcf2015-02-05 22:06:13 -0700490 else:
Simon Glass606e5432023-07-19 17:49:09 -0600491 outf.write(f'{result.return_code}')
492 with open(os.path.join(build_dir, 'toolchain'), 'w',
493 encoding='utf-8') as outf:
494 print('gcc', result.toolchain.gcc, file=outf)
495 print('path', result.toolchain.path, file=outf)
496 print('cross', result.toolchain.cross, file=outf)
497 print('arch', result.toolchain.arch, file=outf)
498 outf.write(f'{result.return_code}')
Simon Glass190064b2014-08-09 15:33:00 -0600499
Simon Glass190064b2014-08-09 15:33:00 -0600500 # Write out the image and function size information and an objdump
Simon Glassbb1501f2014-12-01 17:34:00 -0700501 env = result.toolchain.MakeEnvironment(self.builder.full_path)
Simon Glass606e5432023-07-19 17:49:09 -0600502 with open(os.path.join(build_dir, 'out-env'), 'wb') as outf:
Simon Glasse5fc79e2019-01-07 16:44:23 -0700503 for var in sorted(env.keys()):
Simon Glass606e5432023-07-19 17:49:09 -0600504 outf.write(b'%s="%s"' % (var, env[var]))
Simon Glasscd37d5b2023-02-21 12:40:27 -0700505
506 with open(os.path.join(build_dir, 'out-cmd'), 'w',
Simon Glass606e5432023-07-19 17:49:09 -0600507 encoding='utf-8') as outf:
Simon Glasscd37d5b2023-02-21 12:40:27 -0700508 for cmd in result.cmd_list:
Simon Glass606e5432023-07-19 17:49:09 -0600509 print(' '.join(cmd), file=outf)
Simon Glasscd37d5b2023-02-21 12:40:27 -0700510
Simon Glass190064b2014-08-09 15:33:00 -0600511 lines = []
Simon Glass73da3d22020-12-16 17:24:17 -0700512 for fname in BASE_ELF_FILENAMES:
Simon Glass606e5432023-07-19 17:49:09 -0600513 cmd = [f'{self.toolchain.cross}nm', '--size-sort', fname]
Simon Glassd9800692022-01-29 14:14:05 -0700514 nm_result = command.run_pipe([cmd], capture=True,
Simon Glass190064b2014-08-09 15:33:00 -0600515 capture_stderr=True, cwd=result.out_dir,
516 raise_on_error=False, env=env)
517 if nm_result.stdout:
Simon Glass606e5432023-07-19 17:49:09 -0600518 nm_fname = self.builder.get_func_sizes_file(
519 result.commit_upto, result.brd.target, fname)
520 with open(nm_fname, 'w', encoding='utf-8') as outf:
521 print(nm_result.stdout, end=' ', file=outf)
Simon Glass190064b2014-08-09 15:33:00 -0600522
Simon Glass606e5432023-07-19 17:49:09 -0600523 cmd = [f'{self.toolchain.cross}objdump', '-h', fname]
Simon Glassd9800692022-01-29 14:14:05 -0700524 dump_result = command.run_pipe([cmd], capture=True,
Simon Glass190064b2014-08-09 15:33:00 -0600525 capture_stderr=True, cwd=result.out_dir,
526 raise_on_error=False, env=env)
527 rodata_size = ''
528 if dump_result.stdout:
Simon Glass37edf5f2023-07-19 17:49:06 -0600529 objdump = self.builder.get_objdump_file(result.commit_upto,
Simon Glass190064b2014-08-09 15:33:00 -0600530 result.brd.target, fname)
Simon Glass606e5432023-07-19 17:49:09 -0600531 with open(objdump, 'w', encoding='utf-8') as outf:
532 print(dump_result.stdout, end=' ', file=outf)
Simon Glass190064b2014-08-09 15:33:00 -0600533 for line in dump_result.stdout.splitlines():
534 fields = line.split()
535 if len(fields) > 5 and fields[1] == '.rodata':
536 rodata_size = fields[2]
537
Simon Glass606e5432023-07-19 17:49:09 -0600538 cmd = [f'{self.toolchain.cross}size', fname]
Simon Glassd9800692022-01-29 14:14:05 -0700539 size_result = command.run_pipe([cmd], capture=True,
Simon Glass190064b2014-08-09 15:33:00 -0600540 capture_stderr=True, cwd=result.out_dir,
541 raise_on_error=False, env=env)
542 if size_result.stdout:
543 lines.append(size_result.stdout.splitlines()[1] + ' ' +
544 rodata_size)
545
Alex Kiernan0ddc5102018-05-31 04:48:33 +0000546 # Extract the environment from U-Boot and dump it out
Simon Glass606e5432023-07-19 17:49:09 -0600547 cmd = [f'{self.toolchain.cross}objcopy', '-O', 'binary',
Alex Kiernan0ddc5102018-05-31 04:48:33 +0000548 '-j', '.rodata.default_environment',
549 'env/built-in.o', 'uboot.env']
Simon Glassd9800692022-01-29 14:14:05 -0700550 command.run_pipe([cmd], capture=True,
Alex Kiernan0ddc5102018-05-31 04:48:33 +0000551 capture_stderr=True, cwd=result.out_dir,
552 raise_on_error=False, env=env)
Simon Glass60b285f2020-04-17 17:51:34 -0600553 if not work_in_output:
Simon Glassf06d3332023-07-19 17:49:08 -0600554 self.copy_files(result.out_dir, build_dir, '', ['uboot.env'])
Alex Kiernan0ddc5102018-05-31 04:48:33 +0000555
Simon Glass190064b2014-08-09 15:33:00 -0600556 # Write out the image sizes file. This is similar to the output
557 # of binutil's 'size' utility, but it omits the header line and
558 # adds an additional hex value at the end of each line for the
559 # rodata size
Simon Glass606e5432023-07-19 17:49:09 -0600560 if lines:
Simon Glass37edf5f2023-07-19 17:49:06 -0600561 sizes = self.builder.get_sizes_file(result.commit_upto,
Simon Glass190064b2014-08-09 15:33:00 -0600562 result.brd.target)
Simon Glass606e5432023-07-19 17:49:09 -0600563 with open(sizes, 'w', encoding='utf-8') as outf:
564 print('\n'.join(lines), file=outf)
Simon Glass190064b2014-08-09 15:33:00 -0600565
Simon Glass60b285f2020-04-17 17:51:34 -0600566 if not work_in_output:
567 # Write out the configuration files, with a special case for SPL
568 for dirname in ['', 'spl', 'tpl']:
Simon Glassf06d3332023-07-19 17:49:08 -0600569 self.copy_files(
Simon Glass60b285f2020-04-17 17:51:34 -0600570 result.out_dir, build_dir, dirname,
571 ['u-boot.cfg', 'spl/u-boot-spl.cfg', 'tpl/u-boot-tpl.cfg',
572 '.config', 'include/autoconf.mk',
573 'include/generated/autoconf.h'])
Simon Glass970f9322015-02-05 22:06:14 -0700574
Simon Glass60b285f2020-04-17 17:51:34 -0600575 # Now write the actual build output
576 if keep_outputs:
Simon Glassf06d3332023-07-19 17:49:08 -0600577 self.copy_files(
Simon Glass60b285f2020-04-17 17:51:34 -0600578 result.out_dir, build_dir, '',
579 ['u-boot*', '*.bin', '*.map', '*.img', 'MLO', 'SPL',
580 'include/autoconf.mk', 'spl/u-boot-spl*'])
Simon Glass190064b2014-08-09 15:33:00 -0600581
Simon Glassf06d3332023-07-19 17:49:08 -0600582 def copy_files(self, out_dir, build_dir, dirname, patterns):
Simon Glass970f9322015-02-05 22:06:14 -0700583 """Copy files from the build directory to the output.
584
585 Args:
586 out_dir: Path to output directory containing the files
587 build_dir: Place to copy the files
588 dirname: Source directory, '' for normal U-Boot, 'spl' for SPL
589 patterns: A list of filenames (strings) to copy, each relative
590 to the build directory
591 """
592 for pattern in patterns:
593 file_list = glob.glob(os.path.join(out_dir, dirname, pattern))
594 for fname in file_list:
595 target = os.path.basename(fname)
596 if dirname:
597 base, ext = os.path.splitext(target)
598 if ext:
Simon Glass606e5432023-07-19 17:49:09 -0600599 target = f'{base}-{dirname}{ext}'
Simon Glass970f9322015-02-05 22:06:14 -0700600 shutil.copy(fname, os.path.join(build_dir, target))
Simon Glass190064b2014-08-09 15:33:00 -0600601
Simon Glassf06d3332023-07-19 17:49:08 -0600602 def _send_result(self, result):
Simon Glassab9b4f32021-04-11 16:27:26 +1200603 """Send a result to the builder for processing
604
605 Args:
606 result: CommandResult object containing the results of the build
Simon Glass8116c782021-04-11 16:27:27 +1200607
608 Raises:
609 ValueError if self.test_exception is true (for testing)
Simon Glassab9b4f32021-04-11 16:27:26 +1200610 """
Simon Glass8116c782021-04-11 16:27:27 +1200611 if self.test_exception:
612 raise ValueError('test exception')
Simon Glassab9b4f32021-04-11 16:27:26 +1200613 if self.thread_num != -1:
614 self.builder.out_queue.put(result)
615 else:
Simon Glass37edf5f2023-07-19 17:49:06 -0600616 self.builder.process_result(result)
Simon Glassab9b4f32021-04-11 16:27:26 +1200617
Simon Glassf06d3332023-07-19 17:49:08 -0600618 def run_job(self, job):
Simon Glass190064b2014-08-09 15:33:00 -0600619 """Run a single job
620
621 A job consists of a building a list of commits for a particular board.
622
623 Args:
624 job: Job to build
Simon Glassb82492b2021-01-30 22:17:46 -0700625
626 Returns:
627 List of Result objects
Simon Glass190064b2014-08-09 15:33:00 -0600628 """
Simon Glassf4ed4702022-07-11 19:03:57 -0600629 brd = job.brd
Simon Glass37edf5f2023-07-19 17:49:06 -0600630 work_dir = self.builder.get_thread_dir(self.thread_num)
Simon Glass190064b2014-08-09 15:33:00 -0600631 self.toolchain = None
632 if job.commits:
633 # Run 'make board_defconfig' on the first commit
634 do_config = True
635 commit_upto = 0
636 force_build = False
637 for commit_upto in range(0, len(job.commits), job.step):
Simon Glassf06d3332023-07-19 17:49:08 -0600638 result, request_config = self.run_commit(commit_upto, brd,
Simon Glassa9401b22016-11-16 14:09:25 -0700639 work_dir, do_config, self.builder.config_only,
Simon Glass190064b2014-08-09 15:33:00 -0600640 force_build or self.builder.force_build,
Simon Glassd829f122020-03-18 09:42:42 -0600641 self.builder.force_build_failures,
Simon Glass2b4806e2022-01-22 05:07:33 -0700642 job.work_in_output, job.adjust_cfg)
Simon Glass190064b2014-08-09 15:33:00 -0600643 failed = result.return_code or result.stderr
644 did_config = do_config
645 if failed and not do_config:
646 # If our incremental build failed, try building again
647 # with a reconfig.
648 if self.builder.force_config_on_failure:
Simon Glassf06d3332023-07-19 17:49:08 -0600649 result, request_config = self.run_commit(commit_upto,
Simon Glassd829f122020-03-18 09:42:42 -0600650 brd, work_dir, True, False, True, False,
Simon Glass2b4806e2022-01-22 05:07:33 -0700651 job.work_in_output, job.adjust_cfg)
Simon Glass190064b2014-08-09 15:33:00 -0600652 did_config = True
653 if not self.builder.force_reconfig:
654 do_config = request_config
655
656 # If we built that commit, then config is done. But if we got
657 # an warning, reconfig next time to force it to build the same
658 # files that created warnings this time. Otherwise an
659 # incremental build may not build the same file, and we will
660 # think that the warning has gone away.
661 # We could avoid this by using -Werror everywhere...
662 # For errors, the problem doesn't happen, since presumably
663 # the build stopped and didn't generate output, so will retry
664 # that file next time. So we could detect warnings and deal
665 # with them specially here. For now, we just reconfigure if
666 # anything goes work.
667 # Of course this is substantially slower if there are build
668 # errors/warnings (e.g. 2-3x slower even if only 10% of builds
669 # have problems).
670 if (failed and not result.already_done and not did_config and
671 self.builder.force_config_on_failure):
672 # If this build failed, try the next one with a
673 # reconfigure.
674 # Sometimes if the board_config.h file changes it can mess
675 # with dependencies, and we get:
676 # make: *** No rule to make target `include/autoconf.mk',
677 # needed by `depend'.
678 do_config = True
679 force_build = True
680 else:
681 force_build = False
682 if self.builder.force_config_on_failure:
683 if failed:
684 do_config = True
685 result.commit_upto = commit_upto
686 if result.return_code < 0:
687 raise ValueError('Interrupt')
688
689 # We have the build results, so output the result
Simon Glassf06d3332023-07-19 17:49:08 -0600690 self._write_result(result, job.keep_outputs, job.work_in_output)
691 self._send_result(result)
Simon Glass190064b2014-08-09 15:33:00 -0600692 else:
693 # Just build the currently checked-out build
Simon Glassf06d3332023-07-19 17:49:08 -0600694 result, request_config = self.run_commit(None, brd, work_dir, True,
Simon Glassa9401b22016-11-16 14:09:25 -0700695 self.builder.config_only, True,
Simon Glass2b4806e2022-01-22 05:07:33 -0700696 self.builder.force_build_failures, job.work_in_output,
697 job.adjust_cfg)
Simon Glass190064b2014-08-09 15:33:00 -0600698 result.commit_upto = 0
Simon Glassf06d3332023-07-19 17:49:08 -0600699 self._write_result(result, job.keep_outputs, job.work_in_output)
700 self._send_result(result)
Simon Glass190064b2014-08-09 15:33:00 -0600701
702 def run(self):
703 """Our thread's run function
704
705 This thread picks a job from the queue, runs it, and then goes to the
706 next job.
707 """
Simon Glass190064b2014-08-09 15:33:00 -0600708 while True:
709 job = self.builder.queue.get()
Simon Glass8116c782021-04-11 16:27:27 +1200710 try:
Simon Glassf06d3332023-07-19 17:49:08 -0600711 self.run_job(job)
Simon Glass606e5432023-07-19 17:49:09 -0600712 except Exception as exc:
713 print('Thread exception (use -T0 to run without threads):',
714 exc)
715 self.builder.thread_exceptions.append(exc)
Simon Glass190064b2014-08-09 15:33:00 -0600716 self.builder.queue.task_done()