xref: /rk3399_rockchip-uboot/tools/buildman/builderthread.py (revision a9401b2bc93a750d1566174b18d23bcdc2a45b7b)
1190064b4SSimon Glass# Copyright (c) 2014 Google, Inc
2190064b4SSimon Glass#
3190064b4SSimon Glass# SPDX-License-Identifier:      GPL-2.0+
4190064b4SSimon Glass#
5190064b4SSimon Glass
6190064b4SSimon Glassimport errno
7190064b4SSimon Glassimport glob
8190064b4SSimon Glassimport os
9190064b4SSimon Glassimport shutil
10190064b4SSimon Glassimport threading
11190064b4SSimon Glass
12190064b4SSimon Glassimport command
13190064b4SSimon Glassimport gitutil
14190064b4SSimon Glass
1588c8dcf9SSimon GlassRETURN_CODE_RETRY = -1
1688c8dcf9SSimon Glass
17f3d015cbSThierry Redingdef Mkdir(dirname, parents = False):
18190064b4SSimon Glass    """Make a directory if it doesn't already exist.
19190064b4SSimon Glass
20190064b4SSimon Glass    Args:
21190064b4SSimon Glass        dirname: Directory to create
22190064b4SSimon Glass    """
23190064b4SSimon Glass    try:
24f3d015cbSThierry Reding        if parents:
25f3d015cbSThierry Reding            os.makedirs(dirname)
26f3d015cbSThierry Reding        else:
27190064b4SSimon Glass            os.mkdir(dirname)
28190064b4SSimon Glass    except OSError as err:
29190064b4SSimon Glass        if err.errno == errno.EEXIST:
30190064b4SSimon Glass            pass
31190064b4SSimon Glass        else:
32190064b4SSimon Glass            raise
33190064b4SSimon Glass
34190064b4SSimon Glassclass BuilderJob:
35190064b4SSimon Glass    """Holds information about a job to be performed by a thread
36190064b4SSimon Glass
37190064b4SSimon Glass    Members:
38190064b4SSimon Glass        board: Board object to build
39190064b4SSimon Glass        commits: List of commit options to build.
40190064b4SSimon Glass    """
41190064b4SSimon Glass    def __init__(self):
42190064b4SSimon Glass        self.board = None
43190064b4SSimon Glass        self.commits = []
44190064b4SSimon Glass
45190064b4SSimon Glass
46190064b4SSimon Glassclass ResultThread(threading.Thread):
47190064b4SSimon Glass    """This thread processes results from builder threads.
48190064b4SSimon Glass
49190064b4SSimon Glass    It simply passes the results on to the builder. There is only one
50190064b4SSimon Glass    result thread, and this helps to serialise the build output.
51190064b4SSimon Glass    """
52190064b4SSimon Glass    def __init__(self, builder):
53190064b4SSimon Glass        """Set up a new result thread
54190064b4SSimon Glass
55190064b4SSimon Glass        Args:
56190064b4SSimon Glass            builder: Builder which will be sent each result
57190064b4SSimon Glass        """
58190064b4SSimon Glass        threading.Thread.__init__(self)
59190064b4SSimon Glass        self.builder = builder
60190064b4SSimon Glass
61190064b4SSimon Glass    def run(self):
62190064b4SSimon Glass        """Called to start up the result thread.
63190064b4SSimon Glass
64190064b4SSimon Glass        We collect the next result job and pass it on to the build.
65190064b4SSimon Glass        """
66190064b4SSimon Glass        while True:
67190064b4SSimon Glass            result = self.builder.out_queue.get()
68190064b4SSimon Glass            self.builder.ProcessResult(result)
69190064b4SSimon Glass            self.builder.out_queue.task_done()
70190064b4SSimon Glass
71190064b4SSimon Glass
72190064b4SSimon Glassclass BuilderThread(threading.Thread):
73190064b4SSimon Glass    """This thread builds U-Boot for a particular board.
74190064b4SSimon Glass
75190064b4SSimon Glass    An input queue provides each new job. We run 'make' to build U-Boot
76190064b4SSimon Glass    and then pass the results on to the output queue.
77190064b4SSimon Glass
78190064b4SSimon Glass    Members:
79190064b4SSimon Glass        builder: The builder which contains information we might need
80190064b4SSimon Glass        thread_num: Our thread number (0-n-1), used to decide on a
81190064b4SSimon Glass                temporary directory
82190064b4SSimon Glass    """
83f79f1e0cSStephen Warren    def __init__(self, builder, thread_num, incremental, per_board_out_dir):
84190064b4SSimon Glass        """Set up a new builder thread"""
85190064b4SSimon Glass        threading.Thread.__init__(self)
86190064b4SSimon Glass        self.builder = builder
87190064b4SSimon Glass        self.thread_num = thread_num
88f79f1e0cSStephen Warren        self.incremental = incremental
89f79f1e0cSStephen Warren        self.per_board_out_dir = per_board_out_dir
90190064b4SSimon Glass
91190064b4SSimon Glass    def Make(self, commit, brd, stage, cwd, *args, **kwargs):
92190064b4SSimon Glass        """Run 'make' on a particular commit and board.
93190064b4SSimon Glass
94190064b4SSimon Glass        The source code will already be checked out, so the 'commit'
95190064b4SSimon Glass        argument is only for information.
96190064b4SSimon Glass
97190064b4SSimon Glass        Args:
98190064b4SSimon Glass            commit: Commit object that is being built
99190064b4SSimon Glass            brd: Board object that is being built
100190064b4SSimon Glass            stage: Stage of the build. Valid stages are:
101fd18a89eSRoger Meier                        mrproper - can be called to clean source
102190064b4SSimon Glass                        config - called to configure for a board
103190064b4SSimon Glass                        build - the main make invocation - it does the build
104190064b4SSimon Glass            args: A list of arguments to pass to 'make'
105190064b4SSimon Glass            kwargs: A list of keyword arguments to pass to command.RunPipe()
106190064b4SSimon Glass
107190064b4SSimon Glass        Returns:
108190064b4SSimon Glass            CommandResult object
109190064b4SSimon Glass        """
110190064b4SSimon Glass        return self.builder.do_make(commit, brd, stage, cwd, *args,
111190064b4SSimon Glass                **kwargs)
112190064b4SSimon Glass
113*a9401b2bSSimon Glass    def RunCommit(self, commit_upto, brd, work_dir, do_config, config_only,
114b50113f3SSimon Glass                  force_build, force_build_failures):
115190064b4SSimon Glass        """Build a particular commit.
116190064b4SSimon Glass
117190064b4SSimon Glass        If the build is already done, and we are not forcing a build, we skip
118190064b4SSimon Glass        the build and just return the previously-saved results.
119190064b4SSimon Glass
120190064b4SSimon Glass        Args:
121190064b4SSimon Glass            commit_upto: Commit number to build (0...n-1)
122190064b4SSimon Glass            brd: Board object to build
123190064b4SSimon Glass            work_dir: Directory to which the source will be checked out
124190064b4SSimon Glass            do_config: True to run a make <board>_defconfig on the source
125*a9401b2bSSimon Glass            config_only: Only configure the source, do not build it
126190064b4SSimon Glass            force_build: Force a build even if one was previously done
127190064b4SSimon Glass            force_build_failures: Force a bulid if the previous result showed
128190064b4SSimon Glass                failure
129190064b4SSimon Glass
130190064b4SSimon Glass        Returns:
131190064b4SSimon Glass            tuple containing:
132190064b4SSimon Glass                - CommandResult object containing the results of the build
133190064b4SSimon Glass                - boolean indicating whether 'make config' is still needed
134190064b4SSimon Glass        """
135190064b4SSimon Glass        # Create a default result - it will be overwritte by the call to
136190064b4SSimon Glass        # self.Make() below, in the event that we do a build.
137190064b4SSimon Glass        result = command.CommandResult()
138190064b4SSimon Glass        result.return_code = 0
139190064b4SSimon Glass        if self.builder.in_tree:
140190064b4SSimon Glass            out_dir = work_dir
141190064b4SSimon Glass        else:
142f79f1e0cSStephen Warren            if self.per_board_out_dir:
143f79f1e0cSStephen Warren                out_rel_dir = os.path.join('..', brd.target)
144f79f1e0cSStephen Warren            else:
145f79f1e0cSStephen Warren                out_rel_dir = 'build'
146f79f1e0cSStephen Warren            out_dir = os.path.join(work_dir, out_rel_dir)
147190064b4SSimon Glass
148190064b4SSimon Glass        # Check if the job was already completed last time
149190064b4SSimon Glass        done_file = self.builder.GetDoneFile(commit_upto, brd.target)
150190064b4SSimon Glass        result.already_done = os.path.exists(done_file)
151190064b4SSimon Glass        will_build = (force_build or force_build_failures or
152190064b4SSimon Glass            not result.already_done)
153fb3954f9SSimon Glass        if result.already_done:
154190064b4SSimon Glass            # Get the return code from that build and use it
155190064b4SSimon Glass            with open(done_file, 'r') as fd:
156190064b4SSimon Glass                result.return_code = int(fd.readline())
15788c8dcf9SSimon Glass
15888c8dcf9SSimon Glass            # Check the signal that the build needs to be retried
15988c8dcf9SSimon Glass            if result.return_code == RETURN_CODE_RETRY:
16088c8dcf9SSimon Glass                will_build = True
16188c8dcf9SSimon Glass            elif will_build:
162190064b4SSimon Glass                err_file = self.builder.GetErrFile(commit_upto, brd.target)
163190064b4SSimon Glass                if os.path.exists(err_file) and os.stat(err_file).st_size:
164190064b4SSimon Glass                    result.stderr = 'bad'
165190064b4SSimon Glass                elif not force_build:
166190064b4SSimon Glass                    # The build passed, so no need to build it again
167190064b4SSimon Glass                    will_build = False
168190064b4SSimon Glass
169190064b4SSimon Glass        if will_build:
170190064b4SSimon Glass            # We are going to have to build it. First, get a toolchain
171190064b4SSimon Glass            if not self.toolchain:
172190064b4SSimon Glass                try:
173190064b4SSimon Glass                    self.toolchain = self.builder.toolchains.Select(brd.arch)
174190064b4SSimon Glass                except ValueError as err:
175190064b4SSimon Glass                    result.return_code = 10
176190064b4SSimon Glass                    result.stdout = ''
177190064b4SSimon Glass                    result.stderr = str(err)
178190064b4SSimon Glass                    # TODO(sjg@chromium.org): This gets swallowed, but needs
179190064b4SSimon Glass                    # to be reported.
180190064b4SSimon Glass
181190064b4SSimon Glass            if self.toolchain:
182190064b4SSimon Glass                # Checkout the right commit
183190064b4SSimon Glass                if self.builder.commits:
184190064b4SSimon Glass                    commit = self.builder.commits[commit_upto]
185190064b4SSimon Glass                    if self.builder.checkout:
186190064b4SSimon Glass                        git_dir = os.path.join(work_dir, '.git')
187190064b4SSimon Glass                        gitutil.Checkout(commit.hash, git_dir, work_dir,
188190064b4SSimon Glass                                         force=True)
189190064b4SSimon Glass                else:
190190064b4SSimon Glass                    commit = 'current'
191190064b4SSimon Glass
192190064b4SSimon Glass                # Set up the environment and command line
193bb1501f2SSimon Glass                env = self.toolchain.MakeEnvironment(self.builder.full_path)
194190064b4SSimon Glass                Mkdir(out_dir)
195190064b4SSimon Glass                args = []
196190064b4SSimon Glass                cwd = work_dir
19748c1b6a8SSimon Glass                src_dir = os.path.realpath(work_dir)
198190064b4SSimon Glass                if not self.builder.in_tree:
199190064b4SSimon Glass                    if commit_upto is None:
200190064b4SSimon Glass                        # In this case we are building in the original source
201190064b4SSimon Glass                        # directory (i.e. the current directory where buildman
202190064b4SSimon Glass                        # is invoked. The output directory is set to this
203190064b4SSimon Glass                        # thread's selected work directory.
204190064b4SSimon Glass                        #
205190064b4SSimon Glass                        # Symlinks can confuse U-Boot's Makefile since
206190064b4SSimon Glass                        # we may use '..' in our path, so remove them.
207f79f1e0cSStephen Warren                        out_dir = os.path.realpath(out_dir)
208f79f1e0cSStephen Warren                        args.append('O=%s' % out_dir)
209190064b4SSimon Glass                        cwd = None
21048c1b6a8SSimon Glass                        src_dir = os.getcwd()
211190064b4SSimon Glass                    else:
212f79f1e0cSStephen Warren                        args.append('O=%s' % out_rel_dir)
213f5e5ece0STom Rini                if self.builder.verbose_build:
214f5e5ece0STom Rini                    args.append('V=1')
215f5e5ece0STom Rini                else:
216190064b4SSimon Glass                    args.append('-s')
217190064b4SSimon Glass                if self.builder.num_jobs is not None:
218190064b4SSimon Glass                    args.extend(['-j', str(self.builder.num_jobs)])
219190064b4SSimon Glass                config_args = ['%s_defconfig' % brd.target]
220190064b4SSimon Glass                config_out = ''
221190064b4SSimon Glass                args.extend(self.builder.toolchains.GetMakeArguments(brd))
222190064b4SSimon Glass
223190064b4SSimon Glass                # If we need to reconfigure, do that now
224190064b4SSimon Glass                if do_config:
225f79f1e0cSStephen Warren                    config_out = ''
226f79f1e0cSStephen Warren                    if not self.incremental:
227fd18a89eSRoger Meier                        result = self.Make(commit, brd, 'mrproper', cwd,
228fd18a89eSRoger Meier                                'mrproper', *args, env=env)
229f79f1e0cSStephen Warren                        config_out += result.combined
230190064b4SSimon Glass                    result = self.Make(commit, brd, 'config', cwd,
231190064b4SSimon Glass                            *(args + config_args), env=env)
23240f11fceSSimon Glass                    config_out += result.combined
233190064b4SSimon Glass                    do_config = False   # No need to configure next time
234190064b4SSimon Glass                if result.return_code == 0:
235*a9401b2bSSimon Glass                    if config_only:
236b50113f3SSimon Glass                        args.append('cfg')
237190064b4SSimon Glass                    result = self.Make(commit, brd, 'build', cwd, *args,
238190064b4SSimon Glass                            env=env)
23948c1b6a8SSimon Glass                result.stderr = result.stderr.replace(src_dir + '/', '')
24040f11fceSSimon Glass                if self.builder.verbose_build:
24140f11fceSSimon Glass                    result.stdout = config_out + result.stdout
242190064b4SSimon Glass            else:
243190064b4SSimon Glass                result.return_code = 1
244190064b4SSimon Glass                result.stderr = 'No tool chain for %s\n' % brd.arch
245190064b4SSimon Glass            result.already_done = False
246190064b4SSimon Glass
247190064b4SSimon Glass        result.toolchain = self.toolchain
248190064b4SSimon Glass        result.brd = brd
249190064b4SSimon Glass        result.commit_upto = commit_upto
250190064b4SSimon Glass        result.out_dir = out_dir
251190064b4SSimon Glass        return result, do_config
252190064b4SSimon Glass
253190064b4SSimon Glass    def _WriteResult(self, result, keep_outputs):
254190064b4SSimon Glass        """Write a built result to the output directory.
255190064b4SSimon Glass
256190064b4SSimon Glass        Args:
257190064b4SSimon Glass            result: CommandResult object containing result to write
258190064b4SSimon Glass            keep_outputs: True to store the output binaries, False
259190064b4SSimon Glass                to delete them
260190064b4SSimon Glass        """
261190064b4SSimon Glass        # Fatal error
262190064b4SSimon Glass        if result.return_code < 0:
263190064b4SSimon Glass            return
264190064b4SSimon Glass
26588c8dcf9SSimon Glass        # If we think this might have been aborted with Ctrl-C, record the
26688c8dcf9SSimon Glass        # failure but not that we are 'done' with this board. A retry may fix
26788c8dcf9SSimon Glass        # it.
26888c8dcf9SSimon Glass        maybe_aborted =  result.stderr and 'No child processes' in result.stderr
269190064b4SSimon Glass
270190064b4SSimon Glass        if result.already_done:
271190064b4SSimon Glass            return
272190064b4SSimon Glass
273190064b4SSimon Glass        # Write the output and stderr
274190064b4SSimon Glass        output_dir = self.builder._GetOutputDir(result.commit_upto)
275190064b4SSimon Glass        Mkdir(output_dir)
276190064b4SSimon Glass        build_dir = self.builder.GetBuildDir(result.commit_upto,
277190064b4SSimon Glass                result.brd.target)
278190064b4SSimon Glass        Mkdir(build_dir)
279190064b4SSimon Glass
280190064b4SSimon Glass        outfile = os.path.join(build_dir, 'log')
281190064b4SSimon Glass        with open(outfile, 'w') as fd:
282190064b4SSimon Glass            if result.stdout:
283190064b4SSimon Glass                fd.write(result.stdout)
284190064b4SSimon Glass
285190064b4SSimon Glass        errfile = self.builder.GetErrFile(result.commit_upto,
286190064b4SSimon Glass                result.brd.target)
287190064b4SSimon Glass        if result.stderr:
288190064b4SSimon Glass            with open(errfile, 'w') as fd:
289190064b4SSimon Glass                fd.write(result.stderr)
290190064b4SSimon Glass        elif os.path.exists(errfile):
291190064b4SSimon Glass            os.remove(errfile)
292190064b4SSimon Glass
293190064b4SSimon Glass        if result.toolchain:
294190064b4SSimon Glass            # Write the build result and toolchain information.
295190064b4SSimon Glass            done_file = self.builder.GetDoneFile(result.commit_upto,
296190064b4SSimon Glass                    result.brd.target)
297190064b4SSimon Glass            with open(done_file, 'w') as fd:
29888c8dcf9SSimon Glass                if maybe_aborted:
29988c8dcf9SSimon Glass                    # Special code to indicate we need to retry
30088c8dcf9SSimon Glass                    fd.write('%s' % RETURN_CODE_RETRY)
30188c8dcf9SSimon Glass                else:
302190064b4SSimon Glass                    fd.write('%s' % result.return_code)
303190064b4SSimon Glass            with open(os.path.join(build_dir, 'toolchain'), 'w') as fd:
304190064b4SSimon Glass                print >>fd, 'gcc', result.toolchain.gcc
305190064b4SSimon Glass                print >>fd, 'path', result.toolchain.path
306190064b4SSimon Glass                print >>fd, 'cross', result.toolchain.cross
307190064b4SSimon Glass                print >>fd, 'arch', result.toolchain.arch
308190064b4SSimon Glass                fd.write('%s' % result.return_code)
309190064b4SSimon Glass
310190064b4SSimon Glass            # Write out the image and function size information and an objdump
311bb1501f2SSimon Glass            env = result.toolchain.MakeEnvironment(self.builder.full_path)
312190064b4SSimon Glass            lines = []
313190064b4SSimon Glass            for fname in ['u-boot', 'spl/u-boot-spl']:
314190064b4SSimon Glass                cmd = ['%snm' % self.toolchain.cross, '--size-sort', fname]
315190064b4SSimon Glass                nm_result = command.RunPipe([cmd], capture=True,
316190064b4SSimon Glass                        capture_stderr=True, cwd=result.out_dir,
317190064b4SSimon Glass                        raise_on_error=False, env=env)
318190064b4SSimon Glass                if nm_result.stdout:
319190064b4SSimon Glass                    nm = self.builder.GetFuncSizesFile(result.commit_upto,
320190064b4SSimon Glass                                    result.brd.target, fname)
321190064b4SSimon Glass                    with open(nm, 'w') as fd:
322190064b4SSimon Glass                        print >>fd, nm_result.stdout,
323190064b4SSimon Glass
324190064b4SSimon Glass                cmd = ['%sobjdump' % self.toolchain.cross, '-h', fname]
325190064b4SSimon Glass                dump_result = command.RunPipe([cmd], capture=True,
326190064b4SSimon Glass                        capture_stderr=True, cwd=result.out_dir,
327190064b4SSimon Glass                        raise_on_error=False, env=env)
328190064b4SSimon Glass                rodata_size = ''
329190064b4SSimon Glass                if dump_result.stdout:
330190064b4SSimon Glass                    objdump = self.builder.GetObjdumpFile(result.commit_upto,
331190064b4SSimon Glass                                    result.brd.target, fname)
332190064b4SSimon Glass                    with open(objdump, 'w') as fd:
333190064b4SSimon Glass                        print >>fd, dump_result.stdout,
334190064b4SSimon Glass                    for line in dump_result.stdout.splitlines():
335190064b4SSimon Glass                        fields = line.split()
336190064b4SSimon Glass                        if len(fields) > 5 and fields[1] == '.rodata':
337190064b4SSimon Glass                            rodata_size = fields[2]
338190064b4SSimon Glass
339190064b4SSimon Glass                cmd = ['%ssize' % self.toolchain.cross, fname]
340190064b4SSimon Glass                size_result = command.RunPipe([cmd], capture=True,
341190064b4SSimon Glass                        capture_stderr=True, cwd=result.out_dir,
342190064b4SSimon Glass                        raise_on_error=False, env=env)
343190064b4SSimon Glass                if size_result.stdout:
344190064b4SSimon Glass                    lines.append(size_result.stdout.splitlines()[1] + ' ' +
345190064b4SSimon Glass                                 rodata_size)
346190064b4SSimon Glass
347190064b4SSimon Glass            # Write out the image sizes file. This is similar to the output
348190064b4SSimon Glass            # of binutil's 'size' utility, but it omits the header line and
349190064b4SSimon Glass            # adds an additional hex value at the end of each line for the
350190064b4SSimon Glass            # rodata size
351190064b4SSimon Glass            if len(lines):
352190064b4SSimon Glass                sizes = self.builder.GetSizesFile(result.commit_upto,
353190064b4SSimon Glass                                result.brd.target)
354190064b4SSimon Glass                with open(sizes, 'w') as fd:
355190064b4SSimon Glass                    print >>fd, '\n'.join(lines)
356190064b4SSimon Glass
357970f932aSSimon Glass        # Write out the configuration files, with a special case for SPL
358970f932aSSimon Glass        for dirname in ['', 'spl', 'tpl']:
359970f932aSSimon Glass            self.CopyFiles(result.out_dir, build_dir, dirname, ['u-boot.cfg',
360970f932aSSimon Glass                'spl/u-boot-spl.cfg', 'tpl/u-boot-tpl.cfg', '.config',
361970f932aSSimon Glass                'include/autoconf.mk', 'include/generated/autoconf.h'])
362970f932aSSimon Glass
363190064b4SSimon Glass        # Now write the actual build output
364190064b4SSimon Glass        if keep_outputs:
3650eb4c045STom Rini            self.CopyFiles(result.out_dir, build_dir, '', ['u-boot*', '*.bin',
366dd592110STom Rini                '*.map', '*.img', 'MLO', 'SPL', 'include/autoconf.mk',
3670eb4c045STom Rini                'spl/u-boot-spl*'])
368190064b4SSimon Glass
369970f932aSSimon Glass    def CopyFiles(self, out_dir, build_dir, dirname, patterns):
370970f932aSSimon Glass        """Copy files from the build directory to the output.
371970f932aSSimon Glass
372970f932aSSimon Glass        Args:
373970f932aSSimon Glass            out_dir: Path to output directory containing the files
374970f932aSSimon Glass            build_dir: Place to copy the files
375970f932aSSimon Glass            dirname: Source directory, '' for normal U-Boot, 'spl' for SPL
376970f932aSSimon Glass            patterns: A list of filenames (strings) to copy, each relative
377970f932aSSimon Glass               to the build directory
378970f932aSSimon Glass        """
379970f932aSSimon Glass        for pattern in patterns:
380970f932aSSimon Glass            file_list = glob.glob(os.path.join(out_dir, dirname, pattern))
381970f932aSSimon Glass            for fname in file_list:
382970f932aSSimon Glass                target = os.path.basename(fname)
383970f932aSSimon Glass                if dirname:
384970f932aSSimon Glass                    base, ext = os.path.splitext(target)
385970f932aSSimon Glass                    if ext:
386970f932aSSimon Glass                        target = '%s-%s%s' % (base, dirname, ext)
387970f932aSSimon Glass                shutil.copy(fname, os.path.join(build_dir, target))
388190064b4SSimon Glass
389190064b4SSimon Glass    def RunJob(self, job):
390190064b4SSimon Glass        """Run a single job
391190064b4SSimon Glass
392190064b4SSimon Glass        A job consists of a building a list of commits for a particular board.
393190064b4SSimon Glass
394190064b4SSimon Glass        Args:
395190064b4SSimon Glass            job: Job to build
396190064b4SSimon Glass        """
397190064b4SSimon Glass        brd = job.board
398190064b4SSimon Glass        work_dir = self.builder.GetThreadDir(self.thread_num)
399190064b4SSimon Glass        self.toolchain = None
400190064b4SSimon Glass        if job.commits:
401190064b4SSimon Glass            # Run 'make board_defconfig' on the first commit
402190064b4SSimon Glass            do_config = True
403190064b4SSimon Glass            commit_upto  = 0
404190064b4SSimon Glass            force_build = False
405190064b4SSimon Glass            for commit_upto in range(0, len(job.commits), job.step):
406190064b4SSimon Glass                result, request_config = self.RunCommit(commit_upto, brd,
407*a9401b2bSSimon Glass                        work_dir, do_config, self.builder.config_only,
408190064b4SSimon Glass                        force_build or self.builder.force_build,
409190064b4SSimon Glass                        self.builder.force_build_failures)
410190064b4SSimon Glass                failed = result.return_code or result.stderr
411190064b4SSimon Glass                did_config = do_config
412190064b4SSimon Glass                if failed and not do_config:
413190064b4SSimon Glass                    # If our incremental build failed, try building again
414190064b4SSimon Glass                    # with a reconfig.
415190064b4SSimon Glass                    if self.builder.force_config_on_failure:
416190064b4SSimon Glass                        result, request_config = self.RunCommit(commit_upto,
417b50113f3SSimon Glass                            brd, work_dir, True, False, True, False)
418190064b4SSimon Glass                        did_config = True
419190064b4SSimon Glass                if not self.builder.force_reconfig:
420190064b4SSimon Glass                    do_config = request_config
421190064b4SSimon Glass
422190064b4SSimon Glass                # If we built that commit, then config is done. But if we got
423190064b4SSimon Glass                # an warning, reconfig next time to force it to build the same
424190064b4SSimon Glass                # files that created warnings this time. Otherwise an
425190064b4SSimon Glass                # incremental build may not build the same file, and we will
426190064b4SSimon Glass                # think that the warning has gone away.
427190064b4SSimon Glass                # We could avoid this by using -Werror everywhere...
428190064b4SSimon Glass                # For errors, the problem doesn't happen, since presumably
429190064b4SSimon Glass                # the build stopped and didn't generate output, so will retry
430190064b4SSimon Glass                # that file next time. So we could detect warnings and deal
431190064b4SSimon Glass                # with them specially here. For now, we just reconfigure if
432190064b4SSimon Glass                # anything goes work.
433190064b4SSimon Glass                # Of course this is substantially slower if there are build
434190064b4SSimon Glass                # errors/warnings (e.g. 2-3x slower even if only 10% of builds
435190064b4SSimon Glass                # have problems).
436190064b4SSimon Glass                if (failed and not result.already_done and not did_config and
437190064b4SSimon Glass                        self.builder.force_config_on_failure):
438190064b4SSimon Glass                    # If this build failed, try the next one with a
439190064b4SSimon Glass                    # reconfigure.
440190064b4SSimon Glass                    # Sometimes if the board_config.h file changes it can mess
441190064b4SSimon Glass                    # with dependencies, and we get:
442190064b4SSimon Glass                    # make: *** No rule to make target `include/autoconf.mk',
443190064b4SSimon Glass                    #     needed by `depend'.
444190064b4SSimon Glass                    do_config = True
445190064b4SSimon Glass                    force_build = True
446190064b4SSimon Glass                else:
447190064b4SSimon Glass                    force_build = False
448190064b4SSimon Glass                    if self.builder.force_config_on_failure:
449190064b4SSimon Glass                        if failed:
450190064b4SSimon Glass                            do_config = True
451190064b4SSimon Glass                    result.commit_upto = commit_upto
452190064b4SSimon Glass                    if result.return_code < 0:
453190064b4SSimon Glass                        raise ValueError('Interrupt')
454190064b4SSimon Glass
455190064b4SSimon Glass                # We have the build results, so output the result
456190064b4SSimon Glass                self._WriteResult(result, job.keep_outputs)
457190064b4SSimon Glass                self.builder.out_queue.put(result)
458190064b4SSimon Glass        else:
459190064b4SSimon Glass            # Just build the currently checked-out build
460190064b4SSimon Glass            result, request_config = self.RunCommit(None, brd, work_dir, True,
461*a9401b2bSSimon Glass                        self.builder.config_only, True,
462b50113f3SSimon Glass                        self.builder.force_build_failures)
463190064b4SSimon Glass            result.commit_upto = 0
464190064b4SSimon Glass            self._WriteResult(result, job.keep_outputs)
465190064b4SSimon Glass            self.builder.out_queue.put(result)
466190064b4SSimon Glass
467190064b4SSimon Glass    def run(self):
468190064b4SSimon Glass        """Our thread's run function
469190064b4SSimon Glass
470190064b4SSimon Glass        This thread picks a job from the queue, runs it, and then goes to the
471190064b4SSimon Glass        next job.
472190064b4SSimon Glass        """
473190064b4SSimon Glass        while True:
474190064b4SSimon Glass            job = self.builder.queue.get()
475190064b4SSimon Glass            self.RunJob(job)
476190064b4SSimon Glass            self.builder.queue.task_done()
477