Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 2 | # Copyright (c) 2014 Google, Inc |
| 3 | # |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 4 | |
| 5 | import errno |
| 6 | import glob |
| 7 | import os |
| 8 | import shutil |
Lothar Waßmann | ce6df92 | 2018-04-08 05:14:11 -0600 | [diff] [blame] | 9 | import sys |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 10 | import threading |
| 11 | |
Simon Glass | e5650a8 | 2022-01-22 05:07:33 -0700 | [diff] [blame] | 12 | from buildman import cfgutil |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 13 | from patman import command |
| 14 | from patman import gitutil |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 15 | |
Simon Glass | fd3eea1 | 2015-02-05 22:06:13 -0700 | [diff] [blame] | 16 | RETURN_CODE_RETRY = -1 |
Simon Glass | e0f1971 | 2020-12-16 17:24:17 -0700 | [diff] [blame] | 17 | BASE_ELF_FILENAMES = ['u-boot', 'spl/u-boot-spl', 'tpl/u-boot-tpl'] |
Simon Glass | fd3eea1 | 2015-02-05 22:06:13 -0700 | [diff] [blame] | 18 | |
Thierry Reding | 336d5bd | 2014-08-19 10:22:39 +0200 | [diff] [blame] | 19 | def Mkdir(dirname, parents = False): |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 20 | """Make a directory if it doesn't already exist. |
| 21 | |
| 22 | Args: |
| 23 | dirname: Directory to create |
| 24 | """ |
| 25 | try: |
Thierry Reding | 336d5bd | 2014-08-19 10:22:39 +0200 | [diff] [blame] | 26 | if parents: |
| 27 | os.makedirs(dirname) |
| 28 | else: |
| 29 | os.mkdir(dirname) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 30 | except OSError as err: |
| 31 | if err.errno == errno.EEXIST: |
Lothar Waßmann | ce6df92 | 2018-04-08 05:14:11 -0600 | [diff] [blame] | 32 | if os.path.realpath('.') == os.path.realpath(dirname): |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 33 | print("Cannot create the current working directory '%s'!" % dirname) |
Lothar Waßmann | ce6df92 | 2018-04-08 05:14:11 -0600 | [diff] [blame] | 34 | sys.exit(1) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 35 | pass |
| 36 | else: |
| 37 | raise |
| 38 | |
| 39 | class BuilderJob: |
| 40 | """Holds information about a job to be performed by a thread |
| 41 | |
| 42 | Members: |
| 43 | board: Board object to build |
Simon Glass | df89015 | 2020-03-18 09:42:41 -0600 | [diff] [blame] | 44 | 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 Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 47 | work_in_output: Use the output directory as the work directory and |
| 48 | don't write to a separate output directory. |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 49 | """ |
| 50 | def __init__(self): |
| 51 | self.board = None |
| 52 | self.commits = [] |
Simon Glass | df89015 | 2020-03-18 09:42:41 -0600 | [diff] [blame] | 53 | self.keep_outputs = False |
| 54 | self.step = 1 |
Simon Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 55 | self.work_in_output = False |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 56 | |
| 57 | |
| 58 | class 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 | |
| 84 | class 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 Glass | a29b3ea | 2021-04-11 16:27:25 +1200 | [diff] [blame] | 93 | 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 Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 100 | """ |
Simon Glass | 9bf9a72 | 2021-04-11 16:27:27 +1200 | [diff] [blame] | 101 | def __init__(self, builder, thread_num, mrproper, per_board_out_dir, |
| 102 | test_exception=False): |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 103 | """Set up a new builder thread""" |
| 104 | threading.Thread.__init__(self) |
| 105 | self.builder = builder |
| 106 | self.thread_num = thread_num |
Simon Glass | 6029af1 | 2020-04-09 15:08:51 -0600 | [diff] [blame] | 107 | self.mrproper = mrproper |
Stephen Warren | 97c9690 | 2016-04-11 10:48:44 -0600 | [diff] [blame] | 108 | self.per_board_out_dir = per_board_out_dir |
Simon Glass | 9bf9a72 | 2021-04-11 16:27:27 +1200 | [diff] [blame] | 109 | self.test_exception = test_exception |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 110 | |
| 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 Meier | e0a0e55 | 2014-08-20 22:10:29 +0200 | [diff] [blame] | 121 | mrproper - can be called to clean source |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 122 | 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 Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 125 | kwargs: A list of keyword arguments to pass to command.run_pipe() |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 126 | |
| 127 | Returns: |
| 128 | CommandResult object |
| 129 | """ |
| 130 | return self.builder.do_make(commit, brd, stage, cwd, *args, |
| 131 | **kwargs) |
| 132 | |
Simon Glass | b55b57d | 2016-11-16 14:09:25 -0700 | [diff] [blame] | 133 | def RunCommit(self, commit_upto, brd, work_dir, do_config, config_only, |
Simon Glass | e5650a8 | 2022-01-22 05:07:33 -0700 | [diff] [blame] | 134 | force_build, force_build_failures, work_in_output, |
| 135 | adjust_cfg): |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 136 | """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 Glass | b55b57d | 2016-11-16 14:09:25 -0700 | [diff] [blame] | 146 | config_only: Only configure the source, do not build it |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 147 | 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 Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 150 | work_in_output: Use the output directory as the work directory and |
| 151 | don't write to a separate output directory. |
Simon Glass | e5650a8 | 2022-01-22 05:07:33 -0700 | [diff] [blame] | 152 | 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 Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 159 | |
| 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 Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 169 | if work_in_output or self.builder.in_tree: |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 170 | out_dir = work_dir |
| 171 | else: |
Stephen Warren | 97c9690 | 2016-04-11 10:48:44 -0600 | [diff] [blame] | 172 | 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 Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 177 | |
| 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 Glass | 09f1fe5 | 2014-09-05 19:00:17 -0600 | [diff] [blame] | 183 | if result.already_done: |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 184 | # Get the return code from that build and use it |
| 185 | with open(done_file, 'r') as fd: |
Simon Glass | 4345a6d | 2018-12-10 09:05:23 -0700 | [diff] [blame] | 186 | 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 Glass | fd3eea1 | 2015-02-05 22:06:13 -0700 | [diff] [blame] | 192 | |
| 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 Glass | 09f1fe5 | 2014-09-05 19:00:17 -0600 | [diff] [blame] | 197 | 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 Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 203 | |
| 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 Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 222 | gitutil.checkout(commit.hash, git_dir, work_dir, |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 223 | force=True) |
| 224 | else: |
| 225 | commit = 'current' |
| 226 | |
| 227 | # Set up the environment and command line |
Simon Glass | d48a46c | 2014-12-01 17:34:00 -0700 | [diff] [blame] | 228 | env = self.toolchain.MakeEnvironment(self.builder.full_path) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 229 | Mkdir(out_dir) |
| 230 | args = [] |
| 231 | cwd = work_dir |
Simon Glass | e309625 | 2014-08-28 09:43:42 -0600 | [diff] [blame] | 232 | src_dir = os.path.realpath(work_dir) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 233 | 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 Warren | 97c9690 | 2016-04-11 10:48:44 -0600 | [diff] [blame] | 242 | out_dir = os.path.realpath(out_dir) |
| 243 | args.append('O=%s' % out_dir) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 244 | cwd = None |
Simon Glass | e309625 | 2014-08-28 09:43:42 -0600 | [diff] [blame] | 245 | src_dir = os.getcwd() |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 246 | else: |
Stephen Warren | 97c9690 | 2016-04-11 10:48:44 -0600 | [diff] [blame] | 247 | args.append('O=%s' % out_rel_dir) |
Tom Rini | f06dd81 | 2015-04-01 07:47:41 -0400 | [diff] [blame] | 248 | if self.builder.verbose_build: |
| 249 | args.append('V=1') |
| 250 | else: |
Simon Glass | 655b610 | 2014-12-01 17:34:07 -0700 | [diff] [blame] | 251 | args.append('-s') |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 252 | if self.builder.num_jobs is not None: |
| 253 | args.extend(['-j', str(self.builder.num_jobs)]) |
Daniel Schwierzeck | 20e2ea9 | 2018-01-26 16:31:05 +0100 | [diff] [blame] | 254 | if self.builder.warnings_as_errors: |
| 255 | args.append('KCFLAGS=-Werror') |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 256 | config_args = ['%s_defconfig' % brd.target] |
| 257 | config_out = '' |
| 258 | args.extend(self.builder.toolchains.GetMakeArguments(brd)) |
Simon Glass | f77ca5b | 2019-01-07 16:44:20 -0700 | [diff] [blame] | 259 | args.extend(self.toolchain.MakeArgs()) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 260 | |
Simon Glass | e0f1971 | 2020-12-16 17:24:17 -0700 | [diff] [blame] | 261 | # 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 Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 272 | # If we need to reconfigure, do that now |
Simon Glass | e5650a8 | 2022-01-22 05:07:33 -0700 | [diff] [blame] | 273 | cfg_file = os.path.join(out_dir, '.config') |
| 274 | if do_config or adjust_cfg: |
Stephen Warren | 97c9690 | 2016-04-11 10:48:44 -0600 | [diff] [blame] | 275 | config_out = '' |
Simon Glass | 6029af1 | 2020-04-09 15:08:51 -0600 | [diff] [blame] | 276 | if self.mrproper: |
Stephen Warren | 97c9690 | 2016-04-11 10:48:44 -0600 | [diff] [blame] | 277 | result = self.Make(commit, brd, 'mrproper', cwd, |
| 278 | 'mrproper', *args, env=env) |
| 279 | config_out += result.combined |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 280 | result = self.Make(commit, brd, 'config', cwd, |
| 281 | *(args + config_args), env=env) |
Simon Glass | 413f91a | 2015-02-05 22:06:12 -0700 | [diff] [blame] | 282 | config_out += result.combined |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 283 | do_config = False # No need to configure next time |
Simon Glass | e5650a8 | 2022-01-22 05:07:33 -0700 | [diff] [blame] | 284 | if adjust_cfg: |
| 285 | cfgutil.adjust_cfg_file(cfg_file, adjust_cfg) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 286 | if result.return_code == 0: |
Simon Glass | b55b57d | 2016-11-16 14:09:25 -0700 | [diff] [blame] | 287 | if config_only: |
Simon Glass | 739e851 | 2016-11-13 14:25:51 -0700 | [diff] [blame] | 288 | args.append('cfg') |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 289 | result = self.Make(commit, brd, 'build', cwd, *args, |
| 290 | env=env) |
Simon Glass | e5650a8 | 2022-01-22 05:07:33 -0700 | [diff] [blame] | 291 | if adjust_cfg: |
| 292 | errs = cfgutil.check_cfg_file(cfg_file, adjust_cfg) |
| 293 | if errs: |
| 294 | print('errs', errs) |
| 295 | result.stderr += errs |
| 296 | result.return_code = 1 |
Simon Glass | e309625 | 2014-08-28 09:43:42 -0600 | [diff] [blame] | 297 | result.stderr = result.stderr.replace(src_dir + '/', '') |
Simon Glass | 413f91a | 2015-02-05 22:06:12 -0700 | [diff] [blame] | 298 | if self.builder.verbose_build: |
| 299 | result.stdout = config_out + result.stdout |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 300 | else: |
| 301 | result.return_code = 1 |
| 302 | result.stderr = 'No tool chain for %s\n' % brd.arch |
| 303 | result.already_done = False |
| 304 | |
| 305 | result.toolchain = self.toolchain |
| 306 | result.brd = brd |
| 307 | result.commit_upto = commit_upto |
| 308 | result.out_dir = out_dir |
| 309 | return result, do_config |
| 310 | |
Simon Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 311 | def _WriteResult(self, result, keep_outputs, work_in_output): |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 312 | """Write a built result to the output directory. |
| 313 | |
| 314 | Args: |
| 315 | result: CommandResult object containing result to write |
| 316 | keep_outputs: True to store the output binaries, False |
| 317 | to delete them |
Simon Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 318 | work_in_output: Use the output directory as the work directory and |
| 319 | don't write to a separate output directory. |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 320 | """ |
Simon Glass | fd3eea1 | 2015-02-05 22:06:13 -0700 | [diff] [blame] | 321 | # If we think this might have been aborted with Ctrl-C, record the |
| 322 | # failure but not that we are 'done' with this board. A retry may fix |
| 323 | # it. |
Simon Glass | 2e73745 | 2021-10-19 21:43:23 -0600 | [diff] [blame] | 324 | maybe_aborted = result.stderr and 'No child processes' in result.stderr |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 325 | |
Simon Glass | 2e73745 | 2021-10-19 21:43:23 -0600 | [diff] [blame] | 326 | if result.return_code >= 0 and result.already_done: |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 327 | return |
| 328 | |
| 329 | # Write the output and stderr |
| 330 | output_dir = self.builder._GetOutputDir(result.commit_upto) |
| 331 | Mkdir(output_dir) |
| 332 | build_dir = self.builder.GetBuildDir(result.commit_upto, |
| 333 | result.brd.target) |
| 334 | Mkdir(build_dir) |
| 335 | |
| 336 | outfile = os.path.join(build_dir, 'log') |
| 337 | with open(outfile, 'w') as fd: |
| 338 | if result.stdout: |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 339 | fd.write(result.stdout) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 340 | |
| 341 | errfile = self.builder.GetErrFile(result.commit_upto, |
| 342 | result.brd.target) |
| 343 | if result.stderr: |
| 344 | with open(errfile, 'w') as fd: |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 345 | fd.write(result.stderr) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 346 | elif os.path.exists(errfile): |
| 347 | os.remove(errfile) |
| 348 | |
Simon Glass | 2e73745 | 2021-10-19 21:43:23 -0600 | [diff] [blame] | 349 | # Fatal error |
| 350 | if result.return_code < 0: |
| 351 | return |
| 352 | |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 353 | if result.toolchain: |
| 354 | # Write the build result and toolchain information. |
| 355 | done_file = self.builder.GetDoneFile(result.commit_upto, |
| 356 | result.brd.target) |
| 357 | with open(done_file, 'w') as fd: |
Simon Glass | fd3eea1 | 2015-02-05 22:06:13 -0700 | [diff] [blame] | 358 | if maybe_aborted: |
| 359 | # Special code to indicate we need to retry |
| 360 | fd.write('%s' % RETURN_CODE_RETRY) |
| 361 | else: |
| 362 | fd.write('%s' % result.return_code) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 363 | with open(os.path.join(build_dir, 'toolchain'), 'w') as fd: |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 364 | print('gcc', result.toolchain.gcc, file=fd) |
| 365 | print('path', result.toolchain.path, file=fd) |
| 366 | print('cross', result.toolchain.cross, file=fd) |
| 367 | print('arch', result.toolchain.arch, file=fd) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 368 | fd.write('%s' % result.return_code) |
| 369 | |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 370 | # Write out the image and function size information and an objdump |
Simon Glass | d48a46c | 2014-12-01 17:34:00 -0700 | [diff] [blame] | 371 | env = result.toolchain.MakeEnvironment(self.builder.full_path) |
Simon Glass | 93008e2 | 2021-04-11 16:27:28 +1200 | [diff] [blame] | 372 | with open(os.path.join(build_dir, 'out-env'), 'wb') as fd: |
Simon Glass | 9e90d31 | 2019-01-07 16:44:23 -0700 | [diff] [blame] | 373 | for var in sorted(env.keys()): |
Simon Glass | 93008e2 | 2021-04-11 16:27:28 +1200 | [diff] [blame] | 374 | fd.write(b'%s="%s"' % (var, env[var])) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 375 | lines = [] |
Simon Glass | e0f1971 | 2020-12-16 17:24:17 -0700 | [diff] [blame] | 376 | for fname in BASE_ELF_FILENAMES: |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 377 | cmd = ['%snm' % self.toolchain.cross, '--size-sort', fname] |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 378 | nm_result = command.run_pipe([cmd], capture=True, |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 379 | capture_stderr=True, cwd=result.out_dir, |
| 380 | raise_on_error=False, env=env) |
| 381 | if nm_result.stdout: |
| 382 | nm = self.builder.GetFuncSizesFile(result.commit_upto, |
| 383 | result.brd.target, fname) |
| 384 | with open(nm, 'w') as fd: |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 385 | print(nm_result.stdout, end=' ', file=fd) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 386 | |
| 387 | cmd = ['%sobjdump' % self.toolchain.cross, '-h', fname] |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 388 | dump_result = command.run_pipe([cmd], capture=True, |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 389 | capture_stderr=True, cwd=result.out_dir, |
| 390 | raise_on_error=False, env=env) |
| 391 | rodata_size = '' |
| 392 | if dump_result.stdout: |
| 393 | objdump = self.builder.GetObjdumpFile(result.commit_upto, |
| 394 | result.brd.target, fname) |
| 395 | with open(objdump, 'w') as fd: |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 396 | print(dump_result.stdout, end=' ', file=fd) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 397 | for line in dump_result.stdout.splitlines(): |
| 398 | fields = line.split() |
| 399 | if len(fields) > 5 and fields[1] == '.rodata': |
| 400 | rodata_size = fields[2] |
| 401 | |
| 402 | cmd = ['%ssize' % self.toolchain.cross, fname] |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 403 | size_result = command.run_pipe([cmd], capture=True, |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 404 | capture_stderr=True, cwd=result.out_dir, |
| 405 | raise_on_error=False, env=env) |
| 406 | if size_result.stdout: |
| 407 | lines.append(size_result.stdout.splitlines()[1] + ' ' + |
| 408 | rodata_size) |
| 409 | |
Alex Kiernan | f07ed23 | 2018-05-31 04:48:33 +0000 | [diff] [blame] | 410 | # Extract the environment from U-Boot and dump it out |
| 411 | cmd = ['%sobjcopy' % self.toolchain.cross, '-O', 'binary', |
| 412 | '-j', '.rodata.default_environment', |
| 413 | 'env/built-in.o', 'uboot.env'] |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 414 | command.run_pipe([cmd], capture=True, |
Alex Kiernan | f07ed23 | 2018-05-31 04:48:33 +0000 | [diff] [blame] | 415 | capture_stderr=True, cwd=result.out_dir, |
| 416 | raise_on_error=False, env=env) |
| 417 | ubootenv = os.path.join(result.out_dir, 'uboot.env') |
Simon Glass | e3c85ab | 2020-04-17 17:51:34 -0600 | [diff] [blame] | 418 | if not work_in_output: |
| 419 | self.CopyFiles(result.out_dir, build_dir, '', ['uboot.env']) |
Alex Kiernan | f07ed23 | 2018-05-31 04:48:33 +0000 | [diff] [blame] | 420 | |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 421 | # Write out the image sizes file. This is similar to the output |
| 422 | # of binutil's 'size' utility, but it omits the header line and |
| 423 | # adds an additional hex value at the end of each line for the |
| 424 | # rodata size |
| 425 | if len(lines): |
| 426 | sizes = self.builder.GetSizesFile(result.commit_upto, |
| 427 | result.brd.target) |
| 428 | with open(sizes, 'w') as fd: |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 429 | print('\n'.join(lines), file=fd) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 430 | |
Simon Glass | e3c85ab | 2020-04-17 17:51:34 -0600 | [diff] [blame] | 431 | if not work_in_output: |
| 432 | # Write out the configuration files, with a special case for SPL |
| 433 | for dirname in ['', 'spl', 'tpl']: |
| 434 | self.CopyFiles( |
| 435 | result.out_dir, build_dir, dirname, |
| 436 | ['u-boot.cfg', 'spl/u-boot-spl.cfg', 'tpl/u-boot-tpl.cfg', |
| 437 | '.config', 'include/autoconf.mk', |
| 438 | 'include/generated/autoconf.h']) |
Simon Glass | 4b14c53 | 2015-02-05 22:06:14 -0700 | [diff] [blame] | 439 | |
Simon Glass | e3c85ab | 2020-04-17 17:51:34 -0600 | [diff] [blame] | 440 | # Now write the actual build output |
| 441 | if keep_outputs: |
| 442 | self.CopyFiles( |
| 443 | result.out_dir, build_dir, '', |
| 444 | ['u-boot*', '*.bin', '*.map', '*.img', 'MLO', 'SPL', |
| 445 | 'include/autoconf.mk', 'spl/u-boot-spl*']) |
Simon Glass | 4b14c53 | 2015-02-05 22:06:14 -0700 | [diff] [blame] | 446 | |
| 447 | def CopyFiles(self, out_dir, build_dir, dirname, patterns): |
| 448 | """Copy files from the build directory to the output. |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 449 | |
Simon Glass | 4b14c53 | 2015-02-05 22:06:14 -0700 | [diff] [blame] | 450 | Args: |
| 451 | out_dir: Path to output directory containing the files |
| 452 | build_dir: Place to copy the files |
| 453 | dirname: Source directory, '' for normal U-Boot, 'spl' for SPL |
| 454 | patterns: A list of filenames (strings) to copy, each relative |
| 455 | to the build directory |
| 456 | """ |
| 457 | for pattern in patterns: |
| 458 | file_list = glob.glob(os.path.join(out_dir, dirname, pattern)) |
| 459 | for fname in file_list: |
| 460 | target = os.path.basename(fname) |
| 461 | if dirname: |
| 462 | base, ext = os.path.splitext(target) |
| 463 | if ext: |
| 464 | target = '%s-%s%s' % (base, dirname, ext) |
| 465 | shutil.copy(fname, os.path.join(build_dir, target)) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 466 | |
Simon Glass | a3d0144 | 2021-04-11 16:27:26 +1200 | [diff] [blame] | 467 | def _SendResult(self, result): |
| 468 | """Send a result to the builder for processing |
| 469 | |
| 470 | Args: |
| 471 | result: CommandResult object containing the results of the build |
Simon Glass | 9bf9a72 | 2021-04-11 16:27:27 +1200 | [diff] [blame] | 472 | |
| 473 | Raises: |
| 474 | ValueError if self.test_exception is true (for testing) |
Simon Glass | a3d0144 | 2021-04-11 16:27:26 +1200 | [diff] [blame] | 475 | """ |
Simon Glass | 9bf9a72 | 2021-04-11 16:27:27 +1200 | [diff] [blame] | 476 | if self.test_exception: |
| 477 | raise ValueError('test exception') |
Simon Glass | a3d0144 | 2021-04-11 16:27:26 +1200 | [diff] [blame] | 478 | if self.thread_num != -1: |
| 479 | self.builder.out_queue.put(result) |
| 480 | else: |
| 481 | self.builder.ProcessResult(result) |
| 482 | |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 483 | def RunJob(self, job): |
| 484 | """Run a single job |
| 485 | |
| 486 | A job consists of a building a list of commits for a particular board. |
| 487 | |
| 488 | Args: |
| 489 | job: Job to build |
Simon Glass | c635d89 | 2021-01-30 22:17:46 -0700 | [diff] [blame] | 490 | |
| 491 | Returns: |
| 492 | List of Result objects |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 493 | """ |
| 494 | brd = job.board |
| 495 | work_dir = self.builder.GetThreadDir(self.thread_num) |
| 496 | self.toolchain = None |
| 497 | if job.commits: |
| 498 | # Run 'make board_defconfig' on the first commit |
| 499 | do_config = True |
| 500 | commit_upto = 0 |
| 501 | force_build = False |
| 502 | for commit_upto in range(0, len(job.commits), job.step): |
| 503 | result, request_config = self.RunCommit(commit_upto, brd, |
Simon Glass | b55b57d | 2016-11-16 14:09:25 -0700 | [diff] [blame] | 504 | work_dir, do_config, self.builder.config_only, |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 505 | force_build or self.builder.force_build, |
Simon Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 506 | self.builder.force_build_failures, |
Simon Glass | e5650a8 | 2022-01-22 05:07:33 -0700 | [diff] [blame] | 507 | job.work_in_output, job.adjust_cfg) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 508 | failed = result.return_code or result.stderr |
| 509 | did_config = do_config |
| 510 | if failed and not do_config: |
| 511 | # If our incremental build failed, try building again |
| 512 | # with a reconfig. |
| 513 | if self.builder.force_config_on_failure: |
| 514 | result, request_config = self.RunCommit(commit_upto, |
Simon Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 515 | brd, work_dir, True, False, True, False, |
Simon Glass | e5650a8 | 2022-01-22 05:07:33 -0700 | [diff] [blame] | 516 | job.work_in_output, job.adjust_cfg) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 517 | did_config = True |
| 518 | if not self.builder.force_reconfig: |
| 519 | do_config = request_config |
| 520 | |
| 521 | # If we built that commit, then config is done. But if we got |
| 522 | # an warning, reconfig next time to force it to build the same |
| 523 | # files that created warnings this time. Otherwise an |
| 524 | # incremental build may not build the same file, and we will |
| 525 | # think that the warning has gone away. |
| 526 | # We could avoid this by using -Werror everywhere... |
| 527 | # For errors, the problem doesn't happen, since presumably |
| 528 | # the build stopped and didn't generate output, so will retry |
| 529 | # that file next time. So we could detect warnings and deal |
| 530 | # with them specially here. For now, we just reconfigure if |
| 531 | # anything goes work. |
| 532 | # Of course this is substantially slower if there are build |
| 533 | # errors/warnings (e.g. 2-3x slower even if only 10% of builds |
| 534 | # have problems). |
| 535 | if (failed and not result.already_done and not did_config and |
| 536 | self.builder.force_config_on_failure): |
| 537 | # If this build failed, try the next one with a |
| 538 | # reconfigure. |
| 539 | # Sometimes if the board_config.h file changes it can mess |
| 540 | # with dependencies, and we get: |
| 541 | # make: *** No rule to make target `include/autoconf.mk', |
| 542 | # needed by `depend'. |
| 543 | do_config = True |
| 544 | force_build = True |
| 545 | else: |
| 546 | force_build = False |
| 547 | if self.builder.force_config_on_failure: |
| 548 | if failed: |
| 549 | do_config = True |
| 550 | result.commit_upto = commit_upto |
| 551 | if result.return_code < 0: |
| 552 | raise ValueError('Interrupt') |
| 553 | |
| 554 | # We have the build results, so output the result |
Simon Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 555 | self._WriteResult(result, job.keep_outputs, job.work_in_output) |
Simon Glass | a3d0144 | 2021-04-11 16:27:26 +1200 | [diff] [blame] | 556 | self._SendResult(result) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 557 | else: |
| 558 | # Just build the currently checked-out build |
| 559 | result, request_config = self.RunCommit(None, brd, work_dir, True, |
Simon Glass | b55b57d | 2016-11-16 14:09:25 -0700 | [diff] [blame] | 560 | self.builder.config_only, True, |
Simon Glass | e5650a8 | 2022-01-22 05:07:33 -0700 | [diff] [blame] | 561 | self.builder.force_build_failures, job.work_in_output, |
| 562 | job.adjust_cfg) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 563 | result.commit_upto = 0 |
Simon Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 564 | self._WriteResult(result, job.keep_outputs, job.work_in_output) |
Simon Glass | a3d0144 | 2021-04-11 16:27:26 +1200 | [diff] [blame] | 565 | self._SendResult(result) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 566 | |
| 567 | def run(self): |
| 568 | """Our thread's run function |
| 569 | |
| 570 | This thread picks a job from the queue, runs it, and then goes to the |
| 571 | next job. |
| 572 | """ |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 573 | while True: |
| 574 | job = self.builder.queue.get() |
Simon Glass | 9bf9a72 | 2021-04-11 16:27:27 +1200 | [diff] [blame] | 575 | try: |
| 576 | self.RunJob(job) |
| 577 | except Exception as e: |
Simon Glass | 9bac167 | 2022-01-22 05:07:32 -0700 | [diff] [blame] | 578 | print('Thread exception (use -T0 to run without threads):', e) |
Simon Glass | 9bf9a72 | 2021-04-11 16:27:27 +1200 | [diff] [blame] | 579 | self.builder.thread_exceptions.append(e) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 580 | self.builder.queue.task_done() |