blob: 47ebf4dcdd901bf9eeda0ce1bdf838161e582d11 [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 Glass8d0b6b42023-07-19 17:49:13 -0600140 def _build_args(self, args):
141 """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
145 """
146 if self.builder.verbose_build:
147 args.append('V=1')
148 else:
149 args.append('-s')
150 if self.builder.num_jobs is not None:
151 args.extend(['-j', str(self.builder.num_jobs)])
152 if self.builder.warnings_as_errors:
153 args.append('KCFLAGS=-Werror')
154 args.append('HOSTCFLAGS=-Werror')
155 if self.builder.allow_missing:
156 args.append('BINMAN_ALLOW_MISSING=1')
157 if self.builder.no_lto:
158 args.append('NO_LTO=1')
159 if self.builder.reproducible_builds:
160 args.append('SOURCE_DATE_EPOCH=0')
161
Simon Glassc5077c32023-07-19 17:49:08 -0600162 def run_commit(self, commit_upto, brd, work_dir, do_config, config_only,
Simon Glasse5650a82022-01-22 05:07:33 -0700163 force_build, force_build_failures, work_in_output,
164 adjust_cfg):
Simon Glass4a1e88b2014-08-09 15:33:00 -0600165 """Build a particular commit.
166
167 If the build is already done, and we are not forcing a build, we skip
168 the build and just return the previously-saved results.
169
170 Args:
171 commit_upto: Commit number to build (0...n-1)
172 brd: Board object to build
173 work_dir: Directory to which the source will be checked out
174 do_config: True to run a make <board>_defconfig on the source
Simon Glassb55b57d2016-11-16 14:09:25 -0700175 config_only: Only configure the source, do not build it
Simon Glass4a1e88b2014-08-09 15:33:00 -0600176 force_build: Force a build even if one was previously done
177 force_build_failures: Force a bulid if the previous result showed
178 failure
Simon Glassb6eb8cf2020-03-18 09:42:42 -0600179 work_in_output: Use the output directory as the work directory and
180 don't write to a separate output directory.
Simon Glasse5650a82022-01-22 05:07:33 -0700181 adjust_cfg (list of str): List of changes to make to .config file
182 before building. Each is one of (where C is either CONFIG_xxx
183 or just xxx):
184 C to enable C
185 ~C to disable C
186 C=val to set the value of C (val must have quotes if C is
187 a string Kconfig
Simon Glass4a1e88b2014-08-09 15:33:00 -0600188
189 Returns:
190 tuple containing:
191 - CommandResult object containing the results of the build
192 - boolean indicating whether 'make config' is still needed
193 """
194 # Create a default result - it will be overwritte by the call to
Simon Glassc5077c32023-07-19 17:49:08 -0600195 # self.make() below, in the event that we do a build.
Simon Glass4a1e88b2014-08-09 15:33:00 -0600196 result = command.CommandResult()
197 result.return_code = 0
Simon Glassb6eb8cf2020-03-18 09:42:42 -0600198 if work_in_output or self.builder.in_tree:
Simon Glass4a1e88b2014-08-09 15:33:00 -0600199 out_dir = work_dir
200 else:
Stephen Warren97c96902016-04-11 10:48:44 -0600201 if self.per_board_out_dir:
202 out_rel_dir = os.path.join('..', brd.target)
203 else:
204 out_rel_dir = 'build'
205 out_dir = os.path.join(work_dir, out_rel_dir)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600206
207 # Check if the job was already completed last time
Simon Glassbc74d942023-07-19 17:49:06 -0600208 done_file = self.builder.get_done_file(commit_upto, brd.target)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600209 result.already_done = os.path.exists(done_file)
210 will_build = (force_build or force_build_failures or
211 not result.already_done)
Simon Glass09f1fe52014-09-05 19:00:17 -0600212 if result.already_done:
Simon Glass4a1e88b2014-08-09 15:33:00 -0600213 # Get the return code from that build and use it
Simon Glassd07e5532023-07-19 17:49:09 -0600214 with open(done_file, 'r', encoding='utf-8') as outf:
Simon Glass4345a6d2018-12-10 09:05:23 -0700215 try:
Simon Glassd07e5532023-07-19 17:49:09 -0600216 result.return_code = int(outf.readline())
Simon Glass4345a6d2018-12-10 09:05:23 -0700217 except ValueError:
218 # The file may be empty due to running out of disk space.
219 # Try a rebuild
220 result.return_code = RETURN_CODE_RETRY
Simon Glassfd3eea12015-02-05 22:06:13 -0700221
222 # Check the signal that the build needs to be retried
223 if result.return_code == RETURN_CODE_RETRY:
224 will_build = True
225 elif will_build:
Simon Glassbc74d942023-07-19 17:49:06 -0600226 err_file = self.builder.get_err_file(commit_upto, brd.target)
Simon Glass09f1fe52014-09-05 19:00:17 -0600227 if os.path.exists(err_file) and os.stat(err_file).st_size:
228 result.stderr = 'bad'
229 elif not force_build:
230 # The build passed, so no need to build it again
231 will_build = False
Simon Glass4a1e88b2014-08-09 15:33:00 -0600232
233 if will_build:
234 # We are going to have to build it. First, get a toolchain
235 if not self.toolchain:
236 try:
237 self.toolchain = self.builder.toolchains.Select(brd.arch)
238 except ValueError as err:
239 result.return_code = 10
240 result.stdout = ''
241 result.stderr = str(err)
242 # TODO(sjg@chromium.org): This gets swallowed, but needs
243 # to be reported.
244
245 if self.toolchain:
246 # Checkout the right commit
247 if self.builder.commits:
248 commit = self.builder.commits[commit_upto]
249 if self.builder.checkout:
250 git_dir = os.path.join(work_dir, '.git')
Simon Glass761648b2022-01-29 14:14:11 -0700251 gitutil.checkout(commit.hash, git_dir, work_dir,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600252 force=True)
253 else:
254 commit = 'current'
255
256 # Set up the environment and command line
Simon Glassd48a46c2014-12-01 17:34:00 -0700257 env = self.toolchain.MakeEnvironment(self.builder.full_path)
Simon Glassc5077c32023-07-19 17:49:08 -0600258 mkdir(out_dir)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600259 args = []
260 cwd = work_dir
Simon Glasse3096252014-08-28 09:43:42 -0600261 src_dir = os.path.realpath(work_dir)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600262 if not self.builder.in_tree:
263 if commit_upto is None:
264 # In this case we are building in the original source
265 # directory (i.e. the current directory where buildman
266 # is invoked. The output directory is set to this
267 # thread's selected work directory.
268 #
269 # Symlinks can confuse U-Boot's Makefile since
270 # we may use '..' in our path, so remove them.
Simon Glass21ca0c42023-07-19 17:49:11 -0600271 real_dir = os.path.realpath(out_dir)
272 args.append(f'O={real_dir}')
Simon Glass4a1e88b2014-08-09 15:33:00 -0600273 cwd = None
Simon Glasse3096252014-08-28 09:43:42 -0600274 src_dir = os.getcwd()
Simon Glass4a1e88b2014-08-09 15:33:00 -0600275 else:
Simon Glassd07e5532023-07-19 17:49:09 -0600276 args.append(f'O={out_rel_dir}')
Simon Glass8d0b6b42023-07-19 17:49:13 -0600277 self._build_args(args)
Simon Glassd07e5532023-07-19 17:49:09 -0600278 config_args = [f'{brd.target}_defconfig']
Simon Glass4a1e88b2014-08-09 15:33:00 -0600279 config_out = ''
280 args.extend(self.builder.toolchains.GetMakeArguments(brd))
Simon Glassf77ca5b2019-01-07 16:44:20 -0700281 args.extend(self.toolchain.MakeArgs())
Simon Glass4a1e88b2014-08-09 15:33:00 -0600282
Simon Glasse0f19712020-12-16 17:24:17 -0700283 # Remove any output targets. Since we use a build directory that
284 # was previously used by another board, it may have produced an
285 # SPL image. If we don't remove it (i.e. see do_config and
286 # self.mrproper below) then it will appear to be the output of
287 # this build, even if it does not produce SPL images.
Simon Glasse0f19712020-12-16 17:24:17 -0700288 for elf in BASE_ELF_FILENAMES:
289 fname = os.path.join(out_dir, elf)
290 if os.path.exists(fname):
291 os.remove(fname)
292
Simon Glass4a1e88b2014-08-09 15:33:00 -0600293 # If we need to reconfigure, do that now
Simon Glasse5650a82022-01-22 05:07:33 -0700294 cfg_file = os.path.join(out_dir, '.config')
Simon Glass1382b1d2023-02-21 12:40:27 -0700295 cmd_list = []
Simon Glasse5650a82022-01-22 05:07:33 -0700296 if do_config or adjust_cfg:
Simon Glass6029af12020-04-09 15:08:51 -0600297 if self.mrproper:
Simon Glassc5077c32023-07-19 17:49:08 -0600298 result = self.make(commit, brd, 'mrproper', cwd,
Stephen Warren97c96902016-04-11 10:48:44 -0600299 'mrproper', *args, env=env)
300 config_out += result.combined
Simon Glass1382b1d2023-02-21 12:40:27 -0700301 cmd_list.append([self.builder.gnu_make, 'mrproper',
302 *args])
Simon Glassc5077c32023-07-19 17:49:08 -0600303 result = self.make(commit, brd, 'config', cwd,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600304 *(args + config_args), env=env)
Simon Glass1382b1d2023-02-21 12:40:27 -0700305 cmd_list.append([self.builder.gnu_make] + args +
306 config_args)
Simon Glass413f91a2015-02-05 22:06:12 -0700307 config_out += result.combined
Simon Glass4a1e88b2014-08-09 15:33:00 -0600308 do_config = False # No need to configure next time
Simon Glasse5650a82022-01-22 05:07:33 -0700309 if adjust_cfg:
310 cfgutil.adjust_cfg_file(cfg_file, adjust_cfg)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600311 if result.return_code == 0:
Simon Glassb55b57d2016-11-16 14:09:25 -0700312 if config_only:
Simon Glass739e8512016-11-13 14:25:51 -0700313 args.append('cfg')
Simon Glassc5077c32023-07-19 17:49:08 -0600314 result = self.make(commit, brd, 'build', cwd, *args,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600315 env=env)
Simon Glass1382b1d2023-02-21 12:40:27 -0700316 cmd_list.append([self.builder.gnu_make] + args)
Simon Glass199da412022-11-09 19:14:48 -0700317 if (result.return_code == 2 and
318 ('Some images are invalid' in result.stderr)):
319 # This is handled later by the check for output in
320 # stderr
321 result.return_code = 0
Simon Glasse5650a82022-01-22 05:07:33 -0700322 if adjust_cfg:
323 errs = cfgutil.check_cfg_file(cfg_file, adjust_cfg)
324 if errs:
Simon Glasse5650a82022-01-22 05:07:33 -0700325 result.stderr += errs
326 result.return_code = 1
Simon Glasse3096252014-08-28 09:43:42 -0600327 result.stderr = result.stderr.replace(src_dir + '/', '')
Simon Glass413f91a2015-02-05 22:06:12 -0700328 if self.builder.verbose_build:
329 result.stdout = config_out + result.stdout
Simon Glass1382b1d2023-02-21 12:40:27 -0700330 result.cmd_list = cmd_list
Simon Glass4a1e88b2014-08-09 15:33:00 -0600331 else:
332 result.return_code = 1
Simon Glassd07e5532023-07-19 17:49:09 -0600333 result.stderr = f'No tool chain for {brd.arch}\n'
Simon Glass4a1e88b2014-08-09 15:33:00 -0600334 result.already_done = False
335
336 result.toolchain = self.toolchain
337 result.brd = brd
338 result.commit_upto = commit_upto
339 result.out_dir = out_dir
340 return result, do_config
341
Simon Glassc5077c32023-07-19 17:49:08 -0600342 def _write_result(self, result, keep_outputs, work_in_output):
Simon Glass4a1e88b2014-08-09 15:33:00 -0600343 """Write a built result to the output directory.
344
345 Args:
346 result: CommandResult object containing result to write
347 keep_outputs: True to store the output binaries, False
348 to delete them
Simon Glassb6eb8cf2020-03-18 09:42:42 -0600349 work_in_output: Use the output directory as the work directory and
350 don't write to a separate output directory.
Simon Glass4a1e88b2014-08-09 15:33:00 -0600351 """
Simon Glassfd3eea12015-02-05 22:06:13 -0700352 # If we think this might have been aborted with Ctrl-C, record the
353 # failure but not that we are 'done' with this board. A retry may fix
354 # it.
Simon Glass2e737452021-10-19 21:43:23 -0600355 maybe_aborted = result.stderr and 'No child processes' in result.stderr
Simon Glass4a1e88b2014-08-09 15:33:00 -0600356
Simon Glass2e737452021-10-19 21:43:23 -0600357 if result.return_code >= 0 and result.already_done:
Simon Glass4a1e88b2014-08-09 15:33:00 -0600358 return
359
360 # Write the output and stderr
Simon Glass4cb54682023-07-19 17:49:10 -0600361 output_dir = self.builder.get_output_dir(result.commit_upto)
Simon Glassc5077c32023-07-19 17:49:08 -0600362 mkdir(output_dir)
Simon Glassbc74d942023-07-19 17:49:06 -0600363 build_dir = self.builder.get_build_dir(result.commit_upto,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600364 result.brd.target)
Simon Glassc5077c32023-07-19 17:49:08 -0600365 mkdir(build_dir)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600366
367 outfile = os.path.join(build_dir, 'log')
Simon Glassd07e5532023-07-19 17:49:09 -0600368 with open(outfile, 'w', encoding='utf-8') as outf:
Simon Glass4a1e88b2014-08-09 15:33:00 -0600369 if result.stdout:
Simon Glassd07e5532023-07-19 17:49:09 -0600370 outf.write(result.stdout)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600371
Simon Glassbc74d942023-07-19 17:49:06 -0600372 errfile = self.builder.get_err_file(result.commit_upto,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600373 result.brd.target)
374 if result.stderr:
Simon Glassd07e5532023-07-19 17:49:09 -0600375 with open(errfile, 'w', encoding='utf-8') as outf:
376 outf.write(result.stderr)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600377 elif os.path.exists(errfile):
378 os.remove(errfile)
379
Simon Glass2e737452021-10-19 21:43:23 -0600380 # Fatal error
381 if result.return_code < 0:
382 return
383
Simon Glass4a1e88b2014-08-09 15:33:00 -0600384 if result.toolchain:
385 # Write the build result and toolchain information.
Simon Glassbc74d942023-07-19 17:49:06 -0600386 done_file = self.builder.get_done_file(result.commit_upto,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600387 result.brd.target)
Simon Glassd07e5532023-07-19 17:49:09 -0600388 with open(done_file, 'w', encoding='utf-8') as outf:
Simon Glassfd3eea12015-02-05 22:06:13 -0700389 if maybe_aborted:
390 # Special code to indicate we need to retry
Simon Glassd07e5532023-07-19 17:49:09 -0600391 outf.write(f'{RETURN_CODE_RETRY}')
Simon Glassfd3eea12015-02-05 22:06:13 -0700392 else:
Simon Glassd07e5532023-07-19 17:49:09 -0600393 outf.write(f'{result.return_code}')
394 with open(os.path.join(build_dir, 'toolchain'), 'w',
395 encoding='utf-8') as outf:
396 print('gcc', result.toolchain.gcc, file=outf)
397 print('path', result.toolchain.path, file=outf)
398 print('cross', result.toolchain.cross, file=outf)
399 print('arch', result.toolchain.arch, file=outf)
400 outf.write(f'{result.return_code}')
Simon Glass4a1e88b2014-08-09 15:33:00 -0600401
Simon Glass4a1e88b2014-08-09 15:33:00 -0600402 # Write out the image and function size information and an objdump
Simon Glassd48a46c2014-12-01 17:34:00 -0700403 env = result.toolchain.MakeEnvironment(self.builder.full_path)
Simon Glassd07e5532023-07-19 17:49:09 -0600404 with open(os.path.join(build_dir, 'out-env'), 'wb') as outf:
Simon Glass9e90d312019-01-07 16:44:23 -0700405 for var in sorted(env.keys()):
Simon Glassd07e5532023-07-19 17:49:09 -0600406 outf.write(b'%s="%s"' % (var, env[var]))
Simon Glass1382b1d2023-02-21 12:40:27 -0700407
408 with open(os.path.join(build_dir, 'out-cmd'), 'w',
Simon Glassd07e5532023-07-19 17:49:09 -0600409 encoding='utf-8') as outf:
Simon Glass1382b1d2023-02-21 12:40:27 -0700410 for cmd in result.cmd_list:
Simon Glassd07e5532023-07-19 17:49:09 -0600411 print(' '.join(cmd), file=outf)
Simon Glass1382b1d2023-02-21 12:40:27 -0700412
Simon Glass4a1e88b2014-08-09 15:33:00 -0600413 lines = []
Simon Glasse0f19712020-12-16 17:24:17 -0700414 for fname in BASE_ELF_FILENAMES:
Simon Glassd07e5532023-07-19 17:49:09 -0600415 cmd = [f'{self.toolchain.cross}nm', '--size-sort', fname]
Simon Glass840be732022-01-29 14:14:05 -0700416 nm_result = command.run_pipe([cmd], capture=True,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600417 capture_stderr=True, cwd=result.out_dir,
418 raise_on_error=False, env=env)
419 if nm_result.stdout:
Simon Glassd07e5532023-07-19 17:49:09 -0600420 nm_fname = self.builder.get_func_sizes_file(
421 result.commit_upto, result.brd.target, fname)
422 with open(nm_fname, 'w', encoding='utf-8') as outf:
423 print(nm_result.stdout, end=' ', file=outf)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600424
Simon Glassd07e5532023-07-19 17:49:09 -0600425 cmd = [f'{self.toolchain.cross}objdump', '-h', fname]
Simon Glass840be732022-01-29 14:14:05 -0700426 dump_result = command.run_pipe([cmd], capture=True,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600427 capture_stderr=True, cwd=result.out_dir,
428 raise_on_error=False, env=env)
429 rodata_size = ''
430 if dump_result.stdout:
Simon Glassbc74d942023-07-19 17:49:06 -0600431 objdump = self.builder.get_objdump_file(result.commit_upto,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600432 result.brd.target, fname)
Simon Glassd07e5532023-07-19 17:49:09 -0600433 with open(objdump, 'w', encoding='utf-8') as outf:
434 print(dump_result.stdout, end=' ', file=outf)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600435 for line in dump_result.stdout.splitlines():
436 fields = line.split()
437 if len(fields) > 5 and fields[1] == '.rodata':
438 rodata_size = fields[2]
439
Simon Glassd07e5532023-07-19 17:49:09 -0600440 cmd = [f'{self.toolchain.cross}size', fname]
Simon Glass840be732022-01-29 14:14:05 -0700441 size_result = command.run_pipe([cmd], capture=True,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600442 capture_stderr=True, cwd=result.out_dir,
443 raise_on_error=False, env=env)
444 if size_result.stdout:
445 lines.append(size_result.stdout.splitlines()[1] + ' ' +
446 rodata_size)
447
Alex Kiernanf07ed232018-05-31 04:48:33 +0000448 # Extract the environment from U-Boot and dump it out
Simon Glassd07e5532023-07-19 17:49:09 -0600449 cmd = [f'{self.toolchain.cross}objcopy', '-O', 'binary',
Alex Kiernanf07ed232018-05-31 04:48:33 +0000450 '-j', '.rodata.default_environment',
451 'env/built-in.o', 'uboot.env']
Simon Glass840be732022-01-29 14:14:05 -0700452 command.run_pipe([cmd], capture=True,
Alex Kiernanf07ed232018-05-31 04:48:33 +0000453 capture_stderr=True, cwd=result.out_dir,
454 raise_on_error=False, env=env)
Simon Glasse3c85ab2020-04-17 17:51:34 -0600455 if not work_in_output:
Simon Glassc5077c32023-07-19 17:49:08 -0600456 self.copy_files(result.out_dir, build_dir, '', ['uboot.env'])
Alex Kiernanf07ed232018-05-31 04:48:33 +0000457
Simon Glass4a1e88b2014-08-09 15:33:00 -0600458 # Write out the image sizes file. This is similar to the output
459 # of binutil's 'size' utility, but it omits the header line and
460 # adds an additional hex value at the end of each line for the
461 # rodata size
Simon Glassd07e5532023-07-19 17:49:09 -0600462 if lines:
Simon Glassbc74d942023-07-19 17:49:06 -0600463 sizes = self.builder.get_sizes_file(result.commit_upto,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600464 result.brd.target)
Simon Glassd07e5532023-07-19 17:49:09 -0600465 with open(sizes, 'w', encoding='utf-8') as outf:
466 print('\n'.join(lines), file=outf)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600467
Simon Glasse3c85ab2020-04-17 17:51:34 -0600468 if not work_in_output:
469 # Write out the configuration files, with a special case for SPL
470 for dirname in ['', 'spl', 'tpl']:
Simon Glassc5077c32023-07-19 17:49:08 -0600471 self.copy_files(
Simon Glasse3c85ab2020-04-17 17:51:34 -0600472 result.out_dir, build_dir, dirname,
473 ['u-boot.cfg', 'spl/u-boot-spl.cfg', 'tpl/u-boot-tpl.cfg',
474 '.config', 'include/autoconf.mk',
475 'include/generated/autoconf.h'])
Simon Glass4b14c532015-02-05 22:06:14 -0700476
Simon Glasse3c85ab2020-04-17 17:51:34 -0600477 # Now write the actual build output
478 if keep_outputs:
Simon Glassc5077c32023-07-19 17:49:08 -0600479 self.copy_files(
Simon Glasse3c85ab2020-04-17 17:51:34 -0600480 result.out_dir, build_dir, '',
481 ['u-boot*', '*.bin', '*.map', '*.img', 'MLO', 'SPL',
482 'include/autoconf.mk', 'spl/u-boot-spl*'])
Simon Glass4b14c532015-02-05 22:06:14 -0700483
Simon Glassc5077c32023-07-19 17:49:08 -0600484 def copy_files(self, out_dir, build_dir, dirname, patterns):
Simon Glass4b14c532015-02-05 22:06:14 -0700485 """Copy files from the build directory to the output.
Simon Glass4a1e88b2014-08-09 15:33:00 -0600486
Simon Glass4b14c532015-02-05 22:06:14 -0700487 Args:
488 out_dir: Path to output directory containing the files
489 build_dir: Place to copy the files
490 dirname: Source directory, '' for normal U-Boot, 'spl' for SPL
491 patterns: A list of filenames (strings) to copy, each relative
492 to the build directory
493 """
494 for pattern in patterns:
495 file_list = glob.glob(os.path.join(out_dir, dirname, pattern))
496 for fname in file_list:
497 target = os.path.basename(fname)
498 if dirname:
499 base, ext = os.path.splitext(target)
500 if ext:
Simon Glassd07e5532023-07-19 17:49:09 -0600501 target = f'{base}-{dirname}{ext}'
Simon Glass4b14c532015-02-05 22:06:14 -0700502 shutil.copy(fname, os.path.join(build_dir, target))
Simon Glass4a1e88b2014-08-09 15:33:00 -0600503
Simon Glassc5077c32023-07-19 17:49:08 -0600504 def _send_result(self, result):
Simon Glassa3d01442021-04-11 16:27:26 +1200505 """Send a result to the builder for processing
506
507 Args:
508 result: CommandResult object containing the results of the build
Simon Glass9bf9a722021-04-11 16:27:27 +1200509
510 Raises:
511 ValueError if self.test_exception is true (for testing)
Simon Glassa3d01442021-04-11 16:27:26 +1200512 """
Simon Glass9bf9a722021-04-11 16:27:27 +1200513 if self.test_exception:
514 raise ValueError('test exception')
Simon Glassa3d01442021-04-11 16:27:26 +1200515 if self.thread_num != -1:
516 self.builder.out_queue.put(result)
517 else:
Simon Glassbc74d942023-07-19 17:49:06 -0600518 self.builder.process_result(result)
Simon Glassa3d01442021-04-11 16:27:26 +1200519
Simon Glassc5077c32023-07-19 17:49:08 -0600520 def run_job(self, job):
Simon Glass4a1e88b2014-08-09 15:33:00 -0600521 """Run a single job
522
523 A job consists of a building a list of commits for a particular board.
524
525 Args:
526 job: Job to build
Simon Glassc635d892021-01-30 22:17:46 -0700527
528 Returns:
529 List of Result objects
Simon Glass4a1e88b2014-08-09 15:33:00 -0600530 """
Simon Glass8132f982022-07-11 19:03:57 -0600531 brd = job.brd
Simon Glassbc74d942023-07-19 17:49:06 -0600532 work_dir = self.builder.get_thread_dir(self.thread_num)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600533 self.toolchain = None
534 if job.commits:
535 # Run 'make board_defconfig' on the first commit
536 do_config = True
537 commit_upto = 0
538 force_build = False
539 for commit_upto in range(0, len(job.commits), job.step):
Simon Glassc5077c32023-07-19 17:49:08 -0600540 result, request_config = self.run_commit(commit_upto, brd,
Simon Glassb55b57d2016-11-16 14:09:25 -0700541 work_dir, do_config, self.builder.config_only,
Simon Glass4a1e88b2014-08-09 15:33:00 -0600542 force_build or self.builder.force_build,
Simon Glassb6eb8cf2020-03-18 09:42:42 -0600543 self.builder.force_build_failures,
Simon Glasse5650a82022-01-22 05:07:33 -0700544 job.work_in_output, job.adjust_cfg)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600545 failed = result.return_code or result.stderr
546 did_config = do_config
547 if failed and not do_config:
548 # If our incremental build failed, try building again
549 # with a reconfig.
550 if self.builder.force_config_on_failure:
Simon Glassc5077c32023-07-19 17:49:08 -0600551 result, request_config = self.run_commit(commit_upto,
Simon Glassb6eb8cf2020-03-18 09:42:42 -0600552 brd, work_dir, True, False, True, False,
Simon Glasse5650a82022-01-22 05:07:33 -0700553 job.work_in_output, job.adjust_cfg)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600554 did_config = True
555 if not self.builder.force_reconfig:
556 do_config = request_config
557
558 # If we built that commit, then config is done. But if we got
559 # an warning, reconfig next time to force it to build the same
560 # files that created warnings this time. Otherwise an
561 # incremental build may not build the same file, and we will
562 # think that the warning has gone away.
563 # We could avoid this by using -Werror everywhere...
564 # For errors, the problem doesn't happen, since presumably
565 # the build stopped and didn't generate output, so will retry
566 # that file next time. So we could detect warnings and deal
567 # with them specially here. For now, we just reconfigure if
568 # anything goes work.
569 # Of course this is substantially slower if there are build
570 # errors/warnings (e.g. 2-3x slower even if only 10% of builds
571 # have problems).
572 if (failed and not result.already_done and not did_config and
573 self.builder.force_config_on_failure):
574 # If this build failed, try the next one with a
575 # reconfigure.
576 # Sometimes if the board_config.h file changes it can mess
577 # with dependencies, and we get:
578 # make: *** No rule to make target `include/autoconf.mk',
579 # needed by `depend'.
580 do_config = True
581 force_build = True
582 else:
583 force_build = False
584 if self.builder.force_config_on_failure:
585 if failed:
586 do_config = True
587 result.commit_upto = commit_upto
588 if result.return_code < 0:
589 raise ValueError('Interrupt')
590
591 # We have the build results, so output the result
Simon Glassc5077c32023-07-19 17:49:08 -0600592 self._write_result(result, job.keep_outputs, job.work_in_output)
593 self._send_result(result)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600594 else:
595 # Just build the currently checked-out build
Simon Glassc5077c32023-07-19 17:49:08 -0600596 result, request_config = self.run_commit(None, brd, work_dir, True,
Simon Glassb55b57d2016-11-16 14:09:25 -0700597 self.builder.config_only, True,
Simon Glasse5650a82022-01-22 05:07:33 -0700598 self.builder.force_build_failures, job.work_in_output,
599 job.adjust_cfg)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600600 result.commit_upto = 0
Simon Glassc5077c32023-07-19 17:49:08 -0600601 self._write_result(result, job.keep_outputs, job.work_in_output)
602 self._send_result(result)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600603
604 def run(self):
605 """Our thread's run function
606
607 This thread picks a job from the queue, runs it, and then goes to the
608 next job.
609 """
Simon Glass4a1e88b2014-08-09 15:33:00 -0600610 while True:
611 job = self.builder.queue.get()
Simon Glass9bf9a722021-04-11 16:27:27 +1200612 try:
Simon Glassc5077c32023-07-19 17:49:08 -0600613 self.run_job(job)
Simon Glassd07e5532023-07-19 17:49:09 -0600614 except Exception as exc:
615 print('Thread exception (use -T0 to run without threads):',
616 exc)
617 self.builder.thread_exceptions.append(exc)
Simon Glass4a1e88b2014-08-09 15:33:00 -0600618 self.builder.queue.task_done()