1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2014 Google, Inc
15 RETURN_CODE_RETRY = -1
17 def Mkdir(dirname, parents = False):
18 """Make a directory if it doesn't already exist.
21 dirname: Directory to create
28 except OSError as err:
29 if err.errno == errno.EEXIST:
30 if os.path.realpath('.') == os.path.realpath(dirname):
31 print("Cannot create the current working directory '%s'!" % dirname)
38 """Holds information about a job to be performed by a thread
41 board: Board object to build
42 commits: List of Commit objects to build
43 keep_outputs: True to save build output files
44 step: 1 to process every commit, n to process every nth commit
45 work_in_output: Use the output directory as the work directory and
46 don't write to a separate output directory.
51 self.keep_outputs = False
53 self.work_in_output = False
56 class ResultThread(threading.Thread):
57 """This thread processes results from builder threads.
59 It simply passes the results on to the builder. There is only one
60 result thread, and this helps to serialise the build output.
62 def __init__(self, builder):
63 """Set up a new result thread
66 builder: Builder which will be sent each result
68 threading.Thread.__init__(self)
69 self.builder = builder
72 """Called to start up the result thread.
74 We collect the next result job and pass it on to the build.
77 result = self.builder.out_queue.get()
78 self.builder.ProcessResult(result)
79 self.builder.out_queue.task_done()
82 class BuilderThread(threading.Thread):
83 """This thread builds U-Boot for a particular board.
85 An input queue provides each new job. We run 'make' to build U-Boot
86 and then pass the results on to the output queue.
89 builder: The builder which contains information we might need
90 thread_num: Our thread number (0-n-1), used to decide on a
93 def __init__(self, builder, thread_num, incremental, per_board_out_dir):
94 """Set up a new builder thread"""
95 threading.Thread.__init__(self)
96 self.builder = builder
97 self.thread_num = thread_num
98 self.incremental = incremental
99 self.per_board_out_dir = per_board_out_dir
101 def Make(self, commit, brd, stage, cwd, *args, **kwargs):
102 """Run 'make' on a particular commit and board.
104 The source code will already be checked out, so the 'commit'
105 argument is only for information.
108 commit: Commit object that is being built
109 brd: Board object that is being built
110 stage: Stage of the build. Valid stages are:
111 mrproper - can be called to clean source
112 config - called to configure for a board
113 build - the main make invocation - it does the build
114 args: A list of arguments to pass to 'make'
115 kwargs: A list of keyword arguments to pass to command.RunPipe()
120 return self.builder.do_make(commit, brd, stage, cwd, *args,
123 def RunCommit(self, commit_upto, brd, work_dir, do_config, config_only,
124 force_build, force_build_failures, work_in_output):
125 """Build a particular commit.
127 If the build is already done, and we are not forcing a build, we skip
128 the build and just return the previously-saved results.
131 commit_upto: Commit number to build (0...n-1)
132 brd: Board object to build
133 work_dir: Directory to which the source will be checked out
134 do_config: True to run a make <board>_defconfig on the source
135 config_only: Only configure the source, do not build it
136 force_build: Force a build even if one was previously done
137 force_build_failures: Force a bulid if the previous result showed
139 work_in_output: Use the output directory as the work directory and
140 don't write to a separate output directory.
144 - CommandResult object containing the results of the build
145 - boolean indicating whether 'make config' is still needed
147 # Create a default result - it will be overwritte by the call to
148 # self.Make() below, in the event that we do a build.
149 result = command.CommandResult()
150 result.return_code = 0
151 if work_in_output or self.builder.in_tree:
154 if self.per_board_out_dir:
155 out_rel_dir = os.path.join('..', brd.target)
157 out_rel_dir = 'build'
158 out_dir = os.path.join(work_dir, out_rel_dir)
160 # Check if the job was already completed last time
161 done_file = self.builder.GetDoneFile(commit_upto, brd.target)
162 result.already_done = os.path.exists(done_file)
163 will_build = (force_build or force_build_failures or
164 not result.already_done)
165 if result.already_done:
166 # Get the return code from that build and use it
167 with open(done_file, 'r') as fd:
169 result.return_code = int(fd.readline())
171 # The file may be empty due to running out of disk space.
173 result.return_code = RETURN_CODE_RETRY
175 # Check the signal that the build needs to be retried
176 if result.return_code == RETURN_CODE_RETRY:
179 err_file = self.builder.GetErrFile(commit_upto, brd.target)
180 if os.path.exists(err_file) and os.stat(err_file).st_size:
181 result.stderr = 'bad'
182 elif not force_build:
183 # The build passed, so no need to build it again
187 # We are going to have to build it. First, get a toolchain
188 if not self.toolchain:
190 self.toolchain = self.builder.toolchains.Select(brd.arch)
191 except ValueError as err:
192 result.return_code = 10
194 result.stderr = str(err)
195 # TODO(sjg@chromium.org): This gets swallowed, but needs
199 # Checkout the right commit
200 if self.builder.commits:
201 commit = self.builder.commits[commit_upto]
202 if self.builder.checkout:
203 git_dir = os.path.join(work_dir, '.git')
204 gitutil.Checkout(commit.hash, git_dir, work_dir,
209 # Set up the environment and command line
210 env = self.toolchain.MakeEnvironment(self.builder.full_path)
214 src_dir = os.path.realpath(work_dir)
215 if not self.builder.in_tree:
216 if commit_upto is None:
217 # In this case we are building in the original source
218 # directory (i.e. the current directory where buildman
219 # is invoked. The output directory is set to this
220 # thread's selected work directory.
222 # Symlinks can confuse U-Boot's Makefile since
223 # we may use '..' in our path, so remove them.
224 out_dir = os.path.realpath(out_dir)
225 args.append('O=%s' % out_dir)
227 src_dir = os.getcwd()
229 args.append('O=%s' % out_rel_dir)
230 if self.builder.verbose_build:
234 if self.builder.num_jobs is not None:
235 args.extend(['-j', str(self.builder.num_jobs)])
236 if self.builder.warnings_as_errors:
237 args.append('KCFLAGS=-Werror')
238 config_args = ['%s_defconfig' % brd.target]
240 args.extend(self.builder.toolchains.GetMakeArguments(brd))
241 args.extend(self.toolchain.MakeArgs())
243 # If we need to reconfigure, do that now
246 if not self.incremental:
247 result = self.Make(commit, brd, 'mrproper', cwd,
248 'mrproper', *args, env=env)
249 config_out += result.combined
250 result = self.Make(commit, brd, 'config', cwd,
251 *(args + config_args), env=env)
252 config_out += result.combined
253 do_config = False # No need to configure next time
254 if result.return_code == 0:
257 result = self.Make(commit, brd, 'build', cwd, *args,
259 result.stderr = result.stderr.replace(src_dir + '/', '')
260 if self.builder.verbose_build:
261 result.stdout = config_out + result.stdout
263 result.return_code = 1
264 result.stderr = 'No tool chain for %s\n' % brd.arch
265 result.already_done = False
267 result.toolchain = self.toolchain
269 result.commit_upto = commit_upto
270 result.out_dir = out_dir
271 return result, do_config
273 def _WriteResult(self, result, keep_outputs, work_in_output):
274 """Write a built result to the output directory.
277 result: CommandResult object containing result to write
278 keep_outputs: True to store the output binaries, False
280 work_in_output: Use the output directory as the work directory and
281 don't write to a separate output directory.
286 if result.return_code < 0:
289 # If we think this might have been aborted with Ctrl-C, record the
290 # failure but not that we are 'done' with this board. A retry may fix
292 maybe_aborted = result.stderr and 'No child processes' in result.stderr
294 if result.already_done:
297 # Write the output and stderr
298 output_dir = self.builder._GetOutputDir(result.commit_upto)
300 build_dir = self.builder.GetBuildDir(result.commit_upto,
304 outfile = os.path.join(build_dir, 'log')
305 with open(outfile, 'w') as fd:
307 fd.write(result.stdout)
309 errfile = self.builder.GetErrFile(result.commit_upto,
312 with open(errfile, 'w') as fd:
313 fd.write(result.stderr)
314 elif os.path.exists(errfile):
318 # Write the build result and toolchain information.
319 done_file = self.builder.GetDoneFile(result.commit_upto,
321 with open(done_file, 'w') as fd:
323 # Special code to indicate we need to retry
324 fd.write('%s' % RETURN_CODE_RETRY)
326 fd.write('%s' % result.return_code)
327 with open(os.path.join(build_dir, 'toolchain'), 'w') as fd:
328 print('gcc', result.toolchain.gcc, file=fd)
329 print('path', result.toolchain.path, file=fd)
330 print('cross', result.toolchain.cross, file=fd)
331 print('arch', result.toolchain.arch, file=fd)
332 fd.write('%s' % result.return_code)
334 # Write out the image and function size information and an objdump
335 env = result.toolchain.MakeEnvironment(self.builder.full_path)
336 with open(os.path.join(build_dir, 'env'), 'w') as fd:
337 for var in sorted(env.keys()):
338 print('%s="%s"' % (var, env[var]), file=fd)
340 for fname in ['u-boot', 'spl/u-boot-spl']:
341 cmd = ['%snm' % self.toolchain.cross, '--size-sort', fname]
342 nm_result = command.RunPipe([cmd], capture=True,
343 capture_stderr=True, cwd=result.out_dir,
344 raise_on_error=False, env=env)
346 nm = self.builder.GetFuncSizesFile(result.commit_upto,
347 result.brd.target, fname)
348 with open(nm, 'w') as fd:
349 print(nm_result.stdout, end=' ', file=fd)
351 cmd = ['%sobjdump' % self.toolchain.cross, '-h', fname]
352 dump_result = command.RunPipe([cmd], capture=True,
353 capture_stderr=True, cwd=result.out_dir,
354 raise_on_error=False, env=env)
356 if dump_result.stdout:
357 objdump = self.builder.GetObjdumpFile(result.commit_upto,
358 result.brd.target, fname)
359 with open(objdump, 'w') as fd:
360 print(dump_result.stdout, end=' ', file=fd)
361 for line in dump_result.stdout.splitlines():
362 fields = line.split()
363 if len(fields) > 5 and fields[1] == '.rodata':
364 rodata_size = fields[2]
366 cmd = ['%ssize' % self.toolchain.cross, fname]
367 size_result = command.RunPipe([cmd], capture=True,
368 capture_stderr=True, cwd=result.out_dir,
369 raise_on_error=False, env=env)
370 if size_result.stdout:
371 lines.append(size_result.stdout.splitlines()[1] + ' ' +
374 # Extract the environment from U-Boot and dump it out
375 cmd = ['%sobjcopy' % self.toolchain.cross, '-O', 'binary',
376 '-j', '.rodata.default_environment',
377 'env/built-in.o', 'uboot.env']
378 command.RunPipe([cmd], capture=True,
379 capture_stderr=True, cwd=result.out_dir,
380 raise_on_error=False, env=env)
381 ubootenv = os.path.join(result.out_dir, 'uboot.env')
382 self.CopyFiles(result.out_dir, build_dir, '', ['uboot.env'])
384 # Write out the image sizes file. This is similar to the output
385 # of binutil's 'size' utility, but it omits the header line and
386 # adds an additional hex value at the end of each line for the
389 sizes = self.builder.GetSizesFile(result.commit_upto,
391 with open(sizes, 'w') as fd:
392 print('\n'.join(lines), file=fd)
394 # Write out the configuration files, with a special case for SPL
395 for dirname in ['', 'spl', 'tpl']:
396 self.CopyFiles(result.out_dir, build_dir, dirname, ['u-boot.cfg',
397 'spl/u-boot-spl.cfg', 'tpl/u-boot-tpl.cfg', '.config',
398 'include/autoconf.mk', 'include/generated/autoconf.h'])
400 # Now write the actual build output
402 self.CopyFiles(result.out_dir, build_dir, '', ['u-boot*', '*.bin',
403 '*.map', '*.img', 'MLO', 'SPL', 'include/autoconf.mk',
406 def CopyFiles(self, out_dir, build_dir, dirname, patterns):
407 """Copy files from the build directory to the output.
410 out_dir: Path to output directory containing the files
411 build_dir: Place to copy the files
412 dirname: Source directory, '' for normal U-Boot, 'spl' for SPL
413 patterns: A list of filenames (strings) to copy, each relative
414 to the build directory
416 for pattern in patterns:
417 file_list = glob.glob(os.path.join(out_dir, dirname, pattern))
418 for fname in file_list:
419 target = os.path.basename(fname)
421 base, ext = os.path.splitext(target)
423 target = '%s-%s%s' % (base, dirname, ext)
424 shutil.copy(fname, os.path.join(build_dir, target))
426 def RunJob(self, job):
429 A job consists of a building a list of commits for a particular board.
435 work_dir = self.builder.GetThreadDir(self.thread_num)
436 self.toolchain = None
438 # Run 'make board_defconfig' on the first commit
442 for commit_upto in range(0, len(job.commits), job.step):
443 result, request_config = self.RunCommit(commit_upto, brd,
444 work_dir, do_config, self.builder.config_only,
445 force_build or self.builder.force_build,
446 self.builder.force_build_failures,
447 work_in_output=job.work_in_output)
448 failed = result.return_code or result.stderr
449 did_config = do_config
450 if failed and not do_config:
451 # If our incremental build failed, try building again
453 if self.builder.force_config_on_failure:
454 result, request_config = self.RunCommit(commit_upto,
455 brd, work_dir, True, False, True, False,
456 work_in_output=job.work_in_output)
458 if not self.builder.force_reconfig:
459 do_config = request_config
461 # If we built that commit, then config is done. But if we got
462 # an warning, reconfig next time to force it to build the same
463 # files that created warnings this time. Otherwise an
464 # incremental build may not build the same file, and we will
465 # think that the warning has gone away.
466 # We could avoid this by using -Werror everywhere...
467 # For errors, the problem doesn't happen, since presumably
468 # the build stopped and didn't generate output, so will retry
469 # that file next time. So we could detect warnings and deal
470 # with them specially here. For now, we just reconfigure if
471 # anything goes work.
472 # Of course this is substantially slower if there are build
473 # errors/warnings (e.g. 2-3x slower even if only 10% of builds
475 if (failed and not result.already_done and not did_config and
476 self.builder.force_config_on_failure):
477 # If this build failed, try the next one with a
479 # Sometimes if the board_config.h file changes it can mess
480 # with dependencies, and we get:
481 # make: *** No rule to make target `include/autoconf.mk',
482 # needed by `depend'.
487 if self.builder.force_config_on_failure:
490 result.commit_upto = commit_upto
491 if result.return_code < 0:
492 raise ValueError('Interrupt')
494 # We have the build results, so output the result
495 self._WriteResult(result, job.keep_outputs, job.work_in_output)
496 self.builder.out_queue.put(result)
498 # Just build the currently checked-out build
499 result, request_config = self.RunCommit(None, brd, work_dir, True,
500 self.builder.config_only, True,
501 self.builder.force_build_failures,
502 work_in_output=job.work_in_output)
503 result.commit_upto = 0
504 self._WriteResult(result, job.keep_outputs, job.work_in_output)
505 self.builder.out_queue.put(result)
508 """Our thread's run function
510 This thread picks a job from the queue, runs it, and then goes to the
514 job = self.builder.queue.get()
516 self.builder.queue.task_done()