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