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 | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 12 | from patman import command |
| 13 | from patman import gitutil |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 14 | |
Simon Glass | fd3eea1 | 2015-02-05 22:06:13 -0700 | [diff] [blame] | 15 | RETURN_CODE_RETRY = -1 |
Simon Glass | e0f1971 | 2020-12-16 17:24:17 -0700 | [diff] [blame] | 16 | 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] | 17 | |
Thierry Reding | 336d5bd | 2014-08-19 10:22:39 +0200 | [diff] [blame] | 18 | def Mkdir(dirname, parents = False): |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 19 | """Make a directory if it doesn't already exist. |
| 20 | |
| 21 | Args: |
| 22 | dirname: Directory to create |
| 23 | """ |
| 24 | try: |
Thierry Reding | 336d5bd | 2014-08-19 10:22:39 +0200 | [diff] [blame] | 25 | if parents: |
| 26 | os.makedirs(dirname) |
| 27 | else: |
| 28 | os.mkdir(dirname) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 29 | except OSError as err: |
| 30 | if err.errno == errno.EEXIST: |
Lothar Waßmann | ce6df92 | 2018-04-08 05:14:11 -0600 | [diff] [blame] | 31 | if os.path.realpath('.') == os.path.realpath(dirname): |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 32 | print("Cannot create the current working directory '%s'!" % dirname) |
Lothar Waßmann | ce6df92 | 2018-04-08 05:14:11 -0600 | [diff] [blame] | 33 | sys.exit(1) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 34 | pass |
| 35 | else: |
| 36 | raise |
| 37 | |
| 38 | class BuilderJob: |
| 39 | """Holds information about a job to be performed by a thread |
| 40 | |
| 41 | Members: |
| 42 | board: Board object to build |
Simon Glass | df89015 | 2020-03-18 09:42:41 -0600 | [diff] [blame] | 43 | commits: List of Commit objects to build |
| 44 | keep_outputs: True to save build output files |
| 45 | step: 1 to process every commit, n to process every nth commit |
Simon Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 46 | work_in_output: Use the output directory as the work directory and |
| 47 | don't write to a separate output directory. |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 48 | """ |
| 49 | def __init__(self): |
| 50 | self.board = None |
| 51 | self.commits = [] |
Simon Glass | df89015 | 2020-03-18 09:42:41 -0600 | [diff] [blame] | 52 | self.keep_outputs = False |
| 53 | self.step = 1 |
Simon Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 54 | self.work_in_output = False |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 55 | |
| 56 | |
| 57 | class ResultThread(threading.Thread): |
| 58 | """This thread processes results from builder threads. |
| 59 | |
| 60 | It simply passes the results on to the builder. There is only one |
| 61 | result thread, and this helps to serialise the build output. |
| 62 | """ |
| 63 | def __init__(self, builder): |
| 64 | """Set up a new result thread |
| 65 | |
| 66 | Args: |
| 67 | builder: Builder which will be sent each result |
| 68 | """ |
| 69 | threading.Thread.__init__(self) |
| 70 | self.builder = builder |
| 71 | |
| 72 | def run(self): |
| 73 | """Called to start up the result thread. |
| 74 | |
| 75 | We collect the next result job and pass it on to the build. |
| 76 | """ |
| 77 | while True: |
| 78 | result = self.builder.out_queue.get() |
| 79 | self.builder.ProcessResult(result) |
| 80 | self.builder.out_queue.task_done() |
| 81 | |
| 82 | |
| 83 | class BuilderThread(threading.Thread): |
| 84 | """This thread builds U-Boot for a particular board. |
| 85 | |
| 86 | An input queue provides each new job. We run 'make' to build U-Boot |
| 87 | and then pass the results on to the output queue. |
| 88 | |
| 89 | Members: |
| 90 | builder: The builder which contains information we might need |
| 91 | 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] | 92 | temporary directory. If this is -1 then there are no threads |
| 93 | and we are the (only) main process |
| 94 | mrproper: Use 'make mrproper' before each reconfigure |
| 95 | per_board_out_dir: True to build in a separate persistent directory per |
| 96 | board rather than a thread-specific directory |
| 97 | test_exception: Used for testing; True to raise an exception instead of |
| 98 | reporting the build result |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 99 | """ |
Simon Glass | 9bf9a72 | 2021-04-11 16:27:27 +1200 | [diff] [blame] | 100 | def __init__(self, builder, thread_num, mrproper, per_board_out_dir, |
| 101 | test_exception=False): |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 102 | """Set up a new builder thread""" |
| 103 | threading.Thread.__init__(self) |
| 104 | self.builder = builder |
| 105 | self.thread_num = thread_num |
Simon Glass | 6029af1 | 2020-04-09 15:08:51 -0600 | [diff] [blame] | 106 | self.mrproper = mrproper |
Stephen Warren | 97c9690 | 2016-04-11 10:48:44 -0600 | [diff] [blame] | 107 | self.per_board_out_dir = per_board_out_dir |
Simon Glass | 9bf9a72 | 2021-04-11 16:27:27 +1200 | [diff] [blame] | 108 | self.test_exception = test_exception |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 109 | |
| 110 | def Make(self, commit, brd, stage, cwd, *args, **kwargs): |
| 111 | """Run 'make' on a particular commit and board. |
| 112 | |
| 113 | The source code will already be checked out, so the 'commit' |
| 114 | argument is only for information. |
| 115 | |
| 116 | Args: |
| 117 | commit: Commit object that is being built |
| 118 | brd: Board object that is being built |
| 119 | stage: Stage of the build. Valid stages are: |
Roger Meier | e0a0e55 | 2014-08-20 22:10:29 +0200 | [diff] [blame] | 120 | mrproper - can be called to clean source |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 121 | config - called to configure for a board |
| 122 | build - the main make invocation - it does the build |
| 123 | args: A list of arguments to pass to 'make' |
| 124 | kwargs: A list of keyword arguments to pass to command.RunPipe() |
| 125 | |
| 126 | Returns: |
| 127 | CommandResult object |
| 128 | """ |
| 129 | return self.builder.do_make(commit, brd, stage, cwd, *args, |
| 130 | **kwargs) |
| 131 | |
Simon Glass | b55b57d | 2016-11-16 14:09:25 -0700 | [diff] [blame] | 132 | def RunCommit(self, commit_upto, brd, work_dir, do_config, config_only, |
Simon Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 133 | force_build, force_build_failures, work_in_output): |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 134 | """Build a particular commit. |
| 135 | |
| 136 | If the build is already done, and we are not forcing a build, we skip |
| 137 | the build and just return the previously-saved results. |
| 138 | |
| 139 | Args: |
| 140 | commit_upto: Commit number to build (0...n-1) |
| 141 | brd: Board object to build |
| 142 | work_dir: Directory to which the source will be checked out |
| 143 | do_config: True to run a make <board>_defconfig on the source |
Simon Glass | b55b57d | 2016-11-16 14:09:25 -0700 | [diff] [blame] | 144 | config_only: Only configure the source, do not build it |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 145 | force_build: Force a build even if one was previously done |
| 146 | force_build_failures: Force a bulid if the previous result showed |
| 147 | failure |
Simon Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 148 | work_in_output: Use the output directory as the work directory and |
| 149 | don't write to a separate output directory. |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 150 | |
| 151 | Returns: |
| 152 | tuple containing: |
| 153 | - CommandResult object containing the results of the build |
| 154 | - boolean indicating whether 'make config' is still needed |
| 155 | """ |
| 156 | # Create a default result - it will be overwritte by the call to |
| 157 | # self.Make() below, in the event that we do a build. |
| 158 | result = command.CommandResult() |
| 159 | result.return_code = 0 |
Simon Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 160 | if work_in_output or self.builder.in_tree: |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 161 | out_dir = work_dir |
| 162 | else: |
Stephen Warren | 97c9690 | 2016-04-11 10:48:44 -0600 | [diff] [blame] | 163 | if self.per_board_out_dir: |
| 164 | out_rel_dir = os.path.join('..', brd.target) |
| 165 | else: |
| 166 | out_rel_dir = 'build' |
| 167 | out_dir = os.path.join(work_dir, out_rel_dir) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 168 | |
| 169 | # Check if the job was already completed last time |
| 170 | done_file = self.builder.GetDoneFile(commit_upto, brd.target) |
| 171 | result.already_done = os.path.exists(done_file) |
| 172 | will_build = (force_build or force_build_failures or |
| 173 | not result.already_done) |
Simon Glass | 09f1fe5 | 2014-09-05 19:00:17 -0600 | [diff] [blame] | 174 | if result.already_done: |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 175 | # Get the return code from that build and use it |
| 176 | with open(done_file, 'r') as fd: |
Simon Glass | 4345a6d | 2018-12-10 09:05:23 -0700 | [diff] [blame] | 177 | try: |
| 178 | result.return_code = int(fd.readline()) |
| 179 | except ValueError: |
| 180 | # The file may be empty due to running out of disk space. |
| 181 | # Try a rebuild |
| 182 | result.return_code = RETURN_CODE_RETRY |
Simon Glass | fd3eea1 | 2015-02-05 22:06:13 -0700 | [diff] [blame] | 183 | |
| 184 | # Check the signal that the build needs to be retried |
| 185 | if result.return_code == RETURN_CODE_RETRY: |
| 186 | will_build = True |
| 187 | elif will_build: |
Simon Glass | 09f1fe5 | 2014-09-05 19:00:17 -0600 | [diff] [blame] | 188 | err_file = self.builder.GetErrFile(commit_upto, brd.target) |
| 189 | if os.path.exists(err_file) and os.stat(err_file).st_size: |
| 190 | result.stderr = 'bad' |
| 191 | elif not force_build: |
| 192 | # The build passed, so no need to build it again |
| 193 | will_build = False |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 194 | |
| 195 | if will_build: |
| 196 | # We are going to have to build it. First, get a toolchain |
| 197 | if not self.toolchain: |
| 198 | try: |
| 199 | self.toolchain = self.builder.toolchains.Select(brd.arch) |
| 200 | except ValueError as err: |
| 201 | result.return_code = 10 |
| 202 | result.stdout = '' |
| 203 | result.stderr = str(err) |
| 204 | # TODO(sjg@chromium.org): This gets swallowed, but needs |
| 205 | # to be reported. |
| 206 | |
| 207 | if self.toolchain: |
| 208 | # Checkout the right commit |
| 209 | if self.builder.commits: |
| 210 | commit = self.builder.commits[commit_upto] |
| 211 | if self.builder.checkout: |
| 212 | git_dir = os.path.join(work_dir, '.git') |
| 213 | gitutil.Checkout(commit.hash, git_dir, work_dir, |
| 214 | force=True) |
| 215 | else: |
| 216 | commit = 'current' |
| 217 | |
| 218 | # Set up the environment and command line |
Simon Glass | d48a46c | 2014-12-01 17:34:00 -0700 | [diff] [blame] | 219 | env = self.toolchain.MakeEnvironment(self.builder.full_path) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 220 | Mkdir(out_dir) |
| 221 | args = [] |
| 222 | cwd = work_dir |
Simon Glass | e309625 | 2014-08-28 09:43:42 -0600 | [diff] [blame] | 223 | src_dir = os.path.realpath(work_dir) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 224 | if not self.builder.in_tree: |
| 225 | if commit_upto is None: |
| 226 | # In this case we are building in the original source |
| 227 | # directory (i.e. the current directory where buildman |
| 228 | # is invoked. The output directory is set to this |
| 229 | # thread's selected work directory. |
| 230 | # |
| 231 | # Symlinks can confuse U-Boot's Makefile since |
| 232 | # we may use '..' in our path, so remove them. |
Stephen Warren | 97c9690 | 2016-04-11 10:48:44 -0600 | [diff] [blame] | 233 | out_dir = os.path.realpath(out_dir) |
| 234 | args.append('O=%s' % out_dir) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 235 | cwd = None |
Simon Glass | e309625 | 2014-08-28 09:43:42 -0600 | [diff] [blame] | 236 | src_dir = os.getcwd() |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 237 | else: |
Stephen Warren | 97c9690 | 2016-04-11 10:48:44 -0600 | [diff] [blame] | 238 | args.append('O=%s' % out_rel_dir) |
Tom Rini | f06dd81 | 2015-04-01 07:47:41 -0400 | [diff] [blame] | 239 | if self.builder.verbose_build: |
| 240 | args.append('V=1') |
| 241 | else: |
Simon Glass | 655b610 | 2014-12-01 17:34:07 -0700 | [diff] [blame] | 242 | args.append('-s') |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 243 | if self.builder.num_jobs is not None: |
| 244 | args.extend(['-j', str(self.builder.num_jobs)]) |
Daniel Schwierzeck | 20e2ea9 | 2018-01-26 16:31:05 +0100 | [diff] [blame] | 245 | if self.builder.warnings_as_errors: |
| 246 | args.append('KCFLAGS=-Werror') |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 247 | config_args = ['%s_defconfig' % brd.target] |
| 248 | config_out = '' |
| 249 | args.extend(self.builder.toolchains.GetMakeArguments(brd)) |
Simon Glass | f77ca5b | 2019-01-07 16:44:20 -0700 | [diff] [blame] | 250 | args.extend(self.toolchain.MakeArgs()) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 251 | |
Simon Glass | e0f1971 | 2020-12-16 17:24:17 -0700 | [diff] [blame] | 252 | # Remove any output targets. Since we use a build directory that |
| 253 | # was previously used by another board, it may have produced an |
| 254 | # SPL image. If we don't remove it (i.e. see do_config and |
| 255 | # self.mrproper below) then it will appear to be the output of |
| 256 | # this build, even if it does not produce SPL images. |
| 257 | build_dir = self.builder.GetBuildDir(commit_upto, brd.target) |
| 258 | for elf in BASE_ELF_FILENAMES: |
| 259 | fname = os.path.join(out_dir, elf) |
| 260 | if os.path.exists(fname): |
| 261 | os.remove(fname) |
| 262 | |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 263 | # If we need to reconfigure, do that now |
| 264 | if do_config: |
Stephen Warren | 97c9690 | 2016-04-11 10:48:44 -0600 | [diff] [blame] | 265 | config_out = '' |
Simon Glass | 6029af1 | 2020-04-09 15:08:51 -0600 | [diff] [blame] | 266 | if self.mrproper: |
Stephen Warren | 97c9690 | 2016-04-11 10:48:44 -0600 | [diff] [blame] | 267 | result = self.Make(commit, brd, 'mrproper', cwd, |
| 268 | 'mrproper', *args, env=env) |
| 269 | config_out += result.combined |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 270 | result = self.Make(commit, brd, 'config', cwd, |
| 271 | *(args + config_args), env=env) |
Simon Glass | 413f91a | 2015-02-05 22:06:12 -0700 | [diff] [blame] | 272 | config_out += result.combined |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 273 | do_config = False # No need to configure next time |
| 274 | if result.return_code == 0: |
Simon Glass | b55b57d | 2016-11-16 14:09:25 -0700 | [diff] [blame] | 275 | if config_only: |
Simon Glass | 739e851 | 2016-11-13 14:25:51 -0700 | [diff] [blame] | 276 | args.append('cfg') |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 277 | result = self.Make(commit, brd, 'build', cwd, *args, |
| 278 | env=env) |
Simon Glass | e309625 | 2014-08-28 09:43:42 -0600 | [diff] [blame] | 279 | result.stderr = result.stderr.replace(src_dir + '/', '') |
Simon Glass | 413f91a | 2015-02-05 22:06:12 -0700 | [diff] [blame] | 280 | if self.builder.verbose_build: |
| 281 | result.stdout = config_out + result.stdout |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 282 | else: |
| 283 | result.return_code = 1 |
| 284 | result.stderr = 'No tool chain for %s\n' % brd.arch |
| 285 | result.already_done = False |
| 286 | |
| 287 | result.toolchain = self.toolchain |
| 288 | result.brd = brd |
| 289 | result.commit_upto = commit_upto |
| 290 | result.out_dir = out_dir |
| 291 | return result, do_config |
| 292 | |
Simon Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 293 | def _WriteResult(self, result, keep_outputs, work_in_output): |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 294 | """Write a built result to the output directory. |
| 295 | |
| 296 | Args: |
| 297 | result: CommandResult object containing result to write |
| 298 | keep_outputs: True to store the output binaries, False |
| 299 | to delete them |
Simon Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 300 | work_in_output: Use the output directory as the work directory and |
| 301 | don't write to a separate output directory. |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 302 | """ |
Simon Glass | fd3eea1 | 2015-02-05 22:06:13 -0700 | [diff] [blame] | 303 | # If we think this might have been aborted with Ctrl-C, record the |
| 304 | # failure but not that we are 'done' with this board. A retry may fix |
| 305 | # it. |
Simon Glass | 2e73745 | 2021-10-19 21:43:23 -0600 | [diff] [blame] | 306 | maybe_aborted = result.stderr and 'No child processes' in result.stderr |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 307 | |
Simon Glass | 2e73745 | 2021-10-19 21:43:23 -0600 | [diff] [blame] | 308 | if result.return_code >= 0 and result.already_done: |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 309 | return |
| 310 | |
| 311 | # Write the output and stderr |
| 312 | output_dir = self.builder._GetOutputDir(result.commit_upto) |
| 313 | Mkdir(output_dir) |
| 314 | build_dir = self.builder.GetBuildDir(result.commit_upto, |
| 315 | result.brd.target) |
| 316 | Mkdir(build_dir) |
| 317 | |
| 318 | outfile = os.path.join(build_dir, 'log') |
| 319 | with open(outfile, 'w') as fd: |
| 320 | if result.stdout: |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 321 | fd.write(result.stdout) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 322 | |
| 323 | errfile = self.builder.GetErrFile(result.commit_upto, |
| 324 | result.brd.target) |
| 325 | if result.stderr: |
| 326 | with open(errfile, 'w') as fd: |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 327 | fd.write(result.stderr) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 328 | elif os.path.exists(errfile): |
| 329 | os.remove(errfile) |
| 330 | |
Simon Glass | 2e73745 | 2021-10-19 21:43:23 -0600 | [diff] [blame] | 331 | # Fatal error |
| 332 | if result.return_code < 0: |
| 333 | return |
| 334 | |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 335 | if result.toolchain: |
| 336 | # Write the build result and toolchain information. |
| 337 | done_file = self.builder.GetDoneFile(result.commit_upto, |
| 338 | result.brd.target) |
| 339 | with open(done_file, 'w') as fd: |
Simon Glass | fd3eea1 | 2015-02-05 22:06:13 -0700 | [diff] [blame] | 340 | if maybe_aborted: |
| 341 | # Special code to indicate we need to retry |
| 342 | fd.write('%s' % RETURN_CODE_RETRY) |
| 343 | else: |
| 344 | fd.write('%s' % result.return_code) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 345 | with open(os.path.join(build_dir, 'toolchain'), 'w') as fd: |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 346 | print('gcc', result.toolchain.gcc, file=fd) |
| 347 | print('path', result.toolchain.path, file=fd) |
| 348 | print('cross', result.toolchain.cross, file=fd) |
| 349 | print('arch', result.toolchain.arch, file=fd) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 350 | fd.write('%s' % result.return_code) |
| 351 | |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 352 | # Write out the image and function size information and an objdump |
Simon Glass | d48a46c | 2014-12-01 17:34:00 -0700 | [diff] [blame] | 353 | env = result.toolchain.MakeEnvironment(self.builder.full_path) |
Simon Glass | 93008e2 | 2021-04-11 16:27:28 +1200 | [diff] [blame] | 354 | with open(os.path.join(build_dir, 'out-env'), 'wb') as fd: |
Simon Glass | 9e90d31 | 2019-01-07 16:44:23 -0700 | [diff] [blame] | 355 | for var in sorted(env.keys()): |
Simon Glass | 93008e2 | 2021-04-11 16:27:28 +1200 | [diff] [blame] | 356 | fd.write(b'%s="%s"' % (var, env[var])) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 357 | lines = [] |
Simon Glass | e0f1971 | 2020-12-16 17:24:17 -0700 | [diff] [blame] | 358 | for fname in BASE_ELF_FILENAMES: |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 359 | cmd = ['%snm' % self.toolchain.cross, '--size-sort', fname] |
| 360 | nm_result = command.RunPipe([cmd], capture=True, |
| 361 | capture_stderr=True, cwd=result.out_dir, |
| 362 | raise_on_error=False, env=env) |
| 363 | if nm_result.stdout: |
| 364 | nm = self.builder.GetFuncSizesFile(result.commit_upto, |
| 365 | result.brd.target, fname) |
| 366 | with open(nm, 'w') as fd: |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 367 | print(nm_result.stdout, end=' ', file=fd) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 368 | |
| 369 | cmd = ['%sobjdump' % self.toolchain.cross, '-h', fname] |
| 370 | dump_result = command.RunPipe([cmd], capture=True, |
| 371 | capture_stderr=True, cwd=result.out_dir, |
| 372 | raise_on_error=False, env=env) |
| 373 | rodata_size = '' |
| 374 | if dump_result.stdout: |
| 375 | objdump = self.builder.GetObjdumpFile(result.commit_upto, |
| 376 | result.brd.target, fname) |
| 377 | with open(objdump, 'w') as fd: |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 378 | print(dump_result.stdout, end=' ', file=fd) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 379 | for line in dump_result.stdout.splitlines(): |
| 380 | fields = line.split() |
| 381 | if len(fields) > 5 and fields[1] == '.rodata': |
| 382 | rodata_size = fields[2] |
| 383 | |
| 384 | cmd = ['%ssize' % self.toolchain.cross, fname] |
| 385 | size_result = command.RunPipe([cmd], capture=True, |
| 386 | capture_stderr=True, cwd=result.out_dir, |
| 387 | raise_on_error=False, env=env) |
| 388 | if size_result.stdout: |
| 389 | lines.append(size_result.stdout.splitlines()[1] + ' ' + |
| 390 | rodata_size) |
| 391 | |
Alex Kiernan | f07ed23 | 2018-05-31 04:48:33 +0000 | [diff] [blame] | 392 | # Extract the environment from U-Boot and dump it out |
| 393 | cmd = ['%sobjcopy' % self.toolchain.cross, '-O', 'binary', |
| 394 | '-j', '.rodata.default_environment', |
| 395 | 'env/built-in.o', 'uboot.env'] |
| 396 | command.RunPipe([cmd], capture=True, |
| 397 | capture_stderr=True, cwd=result.out_dir, |
| 398 | raise_on_error=False, env=env) |
| 399 | ubootenv = os.path.join(result.out_dir, 'uboot.env') |
Simon Glass | e3c85ab | 2020-04-17 17:51:34 -0600 | [diff] [blame] | 400 | if not work_in_output: |
| 401 | self.CopyFiles(result.out_dir, build_dir, '', ['uboot.env']) |
Alex Kiernan | f07ed23 | 2018-05-31 04:48:33 +0000 | [diff] [blame] | 402 | |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 403 | # Write out the image sizes file. This is similar to the output |
| 404 | # of binutil's 'size' utility, but it omits the header line and |
| 405 | # adds an additional hex value at the end of each line for the |
| 406 | # rodata size |
| 407 | if len(lines): |
| 408 | sizes = self.builder.GetSizesFile(result.commit_upto, |
| 409 | result.brd.target) |
| 410 | with open(sizes, 'w') as fd: |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 411 | print('\n'.join(lines), file=fd) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 412 | |
Simon Glass | e3c85ab | 2020-04-17 17:51:34 -0600 | [diff] [blame] | 413 | if not work_in_output: |
| 414 | # Write out the configuration files, with a special case for SPL |
| 415 | for dirname in ['', 'spl', 'tpl']: |
| 416 | self.CopyFiles( |
| 417 | result.out_dir, build_dir, dirname, |
| 418 | ['u-boot.cfg', 'spl/u-boot-spl.cfg', 'tpl/u-boot-tpl.cfg', |
| 419 | '.config', 'include/autoconf.mk', |
| 420 | 'include/generated/autoconf.h']) |
Simon Glass | 4b14c53 | 2015-02-05 22:06:14 -0700 | [diff] [blame] | 421 | |
Simon Glass | e3c85ab | 2020-04-17 17:51:34 -0600 | [diff] [blame] | 422 | # Now write the actual build output |
| 423 | if keep_outputs: |
| 424 | self.CopyFiles( |
| 425 | result.out_dir, build_dir, '', |
| 426 | ['u-boot*', '*.bin', '*.map', '*.img', 'MLO', 'SPL', |
| 427 | 'include/autoconf.mk', 'spl/u-boot-spl*']) |
Simon Glass | 4b14c53 | 2015-02-05 22:06:14 -0700 | [diff] [blame] | 428 | |
| 429 | def CopyFiles(self, out_dir, build_dir, dirname, patterns): |
| 430 | """Copy files from the build directory to the output. |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 431 | |
Simon Glass | 4b14c53 | 2015-02-05 22:06:14 -0700 | [diff] [blame] | 432 | Args: |
| 433 | out_dir: Path to output directory containing the files |
| 434 | build_dir: Place to copy the files |
| 435 | dirname: Source directory, '' for normal U-Boot, 'spl' for SPL |
| 436 | patterns: A list of filenames (strings) to copy, each relative |
| 437 | to the build directory |
| 438 | """ |
| 439 | for pattern in patterns: |
| 440 | file_list = glob.glob(os.path.join(out_dir, dirname, pattern)) |
| 441 | for fname in file_list: |
| 442 | target = os.path.basename(fname) |
| 443 | if dirname: |
| 444 | base, ext = os.path.splitext(target) |
| 445 | if ext: |
| 446 | target = '%s-%s%s' % (base, dirname, ext) |
| 447 | shutil.copy(fname, os.path.join(build_dir, target)) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 448 | |
Simon Glass | a3d0144 | 2021-04-11 16:27:26 +1200 | [diff] [blame] | 449 | def _SendResult(self, result): |
| 450 | """Send a result to the builder for processing |
| 451 | |
| 452 | Args: |
| 453 | result: CommandResult object containing the results of the build |
Simon Glass | 9bf9a72 | 2021-04-11 16:27:27 +1200 | [diff] [blame] | 454 | |
| 455 | Raises: |
| 456 | ValueError if self.test_exception is true (for testing) |
Simon Glass | a3d0144 | 2021-04-11 16:27:26 +1200 | [diff] [blame] | 457 | """ |
Simon Glass | 9bf9a72 | 2021-04-11 16:27:27 +1200 | [diff] [blame] | 458 | if self.test_exception: |
| 459 | raise ValueError('test exception') |
Simon Glass | a3d0144 | 2021-04-11 16:27:26 +1200 | [diff] [blame] | 460 | if self.thread_num != -1: |
| 461 | self.builder.out_queue.put(result) |
| 462 | else: |
| 463 | self.builder.ProcessResult(result) |
| 464 | |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 465 | def RunJob(self, job): |
| 466 | """Run a single job |
| 467 | |
| 468 | A job consists of a building a list of commits for a particular board. |
| 469 | |
| 470 | Args: |
| 471 | job: Job to build |
Simon Glass | c635d89 | 2021-01-30 22:17:46 -0700 | [diff] [blame] | 472 | |
| 473 | Returns: |
| 474 | List of Result objects |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 475 | """ |
| 476 | brd = job.board |
| 477 | work_dir = self.builder.GetThreadDir(self.thread_num) |
| 478 | self.toolchain = None |
| 479 | if job.commits: |
| 480 | # Run 'make board_defconfig' on the first commit |
| 481 | do_config = True |
| 482 | commit_upto = 0 |
| 483 | force_build = False |
| 484 | for commit_upto in range(0, len(job.commits), job.step): |
| 485 | result, request_config = self.RunCommit(commit_upto, brd, |
Simon Glass | b55b57d | 2016-11-16 14:09:25 -0700 | [diff] [blame] | 486 | work_dir, do_config, self.builder.config_only, |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 487 | force_build or self.builder.force_build, |
Simon Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 488 | self.builder.force_build_failures, |
| 489 | work_in_output=job.work_in_output) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 490 | failed = result.return_code or result.stderr |
| 491 | did_config = do_config |
| 492 | if failed and not do_config: |
| 493 | # If our incremental build failed, try building again |
| 494 | # with a reconfig. |
| 495 | if self.builder.force_config_on_failure: |
| 496 | result, request_config = self.RunCommit(commit_upto, |
Simon Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 497 | brd, work_dir, True, False, True, False, |
| 498 | work_in_output=job.work_in_output) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 499 | did_config = True |
| 500 | if not self.builder.force_reconfig: |
| 501 | do_config = request_config |
| 502 | |
| 503 | # If we built that commit, then config is done. But if we got |
| 504 | # an warning, reconfig next time to force it to build the same |
| 505 | # files that created warnings this time. Otherwise an |
| 506 | # incremental build may not build the same file, and we will |
| 507 | # think that the warning has gone away. |
| 508 | # We could avoid this by using -Werror everywhere... |
| 509 | # For errors, the problem doesn't happen, since presumably |
| 510 | # the build stopped and didn't generate output, so will retry |
| 511 | # that file next time. So we could detect warnings and deal |
| 512 | # with them specially here. For now, we just reconfigure if |
| 513 | # anything goes work. |
| 514 | # Of course this is substantially slower if there are build |
| 515 | # errors/warnings (e.g. 2-3x slower even if only 10% of builds |
| 516 | # have problems). |
| 517 | if (failed and not result.already_done and not did_config and |
| 518 | self.builder.force_config_on_failure): |
| 519 | # If this build failed, try the next one with a |
| 520 | # reconfigure. |
| 521 | # Sometimes if the board_config.h file changes it can mess |
| 522 | # with dependencies, and we get: |
| 523 | # make: *** No rule to make target `include/autoconf.mk', |
| 524 | # needed by `depend'. |
| 525 | do_config = True |
| 526 | force_build = True |
| 527 | else: |
| 528 | force_build = False |
| 529 | if self.builder.force_config_on_failure: |
| 530 | if failed: |
| 531 | do_config = True |
| 532 | result.commit_upto = commit_upto |
| 533 | if result.return_code < 0: |
| 534 | raise ValueError('Interrupt') |
| 535 | |
| 536 | # We have the build results, so output the result |
Simon Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 537 | self._WriteResult(result, job.keep_outputs, job.work_in_output) |
Simon Glass | a3d0144 | 2021-04-11 16:27:26 +1200 | [diff] [blame] | 538 | self._SendResult(result) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 539 | else: |
| 540 | # Just build the currently checked-out build |
| 541 | result, request_config = self.RunCommit(None, brd, work_dir, True, |
Simon Glass | b55b57d | 2016-11-16 14:09:25 -0700 | [diff] [blame] | 542 | self.builder.config_only, True, |
Simon Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 543 | self.builder.force_build_failures, |
| 544 | work_in_output=job.work_in_output) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 545 | result.commit_upto = 0 |
Simon Glass | b6eb8cf | 2020-03-18 09:42:42 -0600 | [diff] [blame] | 546 | self._WriteResult(result, job.keep_outputs, job.work_in_output) |
Simon Glass | a3d0144 | 2021-04-11 16:27:26 +1200 | [diff] [blame] | 547 | self._SendResult(result) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 548 | |
| 549 | def run(self): |
| 550 | """Our thread's run function |
| 551 | |
| 552 | This thread picks a job from the queue, runs it, and then goes to the |
| 553 | next job. |
| 554 | """ |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 555 | while True: |
| 556 | job = self.builder.queue.get() |
Simon Glass | 9bf9a72 | 2021-04-11 16:27:27 +1200 | [diff] [blame] | 557 | try: |
| 558 | self.RunJob(job) |
| 559 | except Exception as e: |
| 560 | print('Thread exception:', e) |
| 561 | self.builder.thread_exceptions.append(e) |
Simon Glass | 4a1e88b | 2014-08-09 15:33:00 -0600 | [diff] [blame] | 562 | self.builder.queue.task_done() |