1# Copyright (c) 2014 Google, Inc
2#
3# SPDX-License-Identifier:      GPL-2.0+
4#
5
6import errno
7import glob
8import os
9import shutil
10import threading
11
12import command
13import gitutil
14
15RETURN_CODE_RETRY = -1
16
17def Mkdir(dirname, parents = False):
18    """Make a directory if it doesn't already exist.
19
20    Args:
21        dirname: Directory to create
22    """
23    try:
24        if parents:
25            os.makedirs(dirname)
26        else:
27            os.mkdir(dirname)
28    except OSError as err:
29        if err.errno == errno.EEXIST:
30            pass
31        else:
32            raise
33
34class BuilderJob:
35    """Holds information about a job to be performed by a thread
36
37    Members:
38        board: Board object to build
39        commits: List of commit options to build.
40    """
41    def __init__(self):
42        self.board = None
43        self.commits = []
44
45
46class ResultThread(threading.Thread):
47    """This thread processes results from builder threads.
48
49    It simply passes the results on to the builder. There is only one
50    result thread, and this helps to serialise the build output.
51    """
52    def __init__(self, builder):
53        """Set up a new result thread
54
55        Args:
56            builder: Builder which will be sent each result
57        """
58        threading.Thread.__init__(self)
59        self.builder = builder
60
61    def run(self):
62        """Called to start up the result thread.
63
64        We collect the next result job and pass it on to the build.
65        """
66        while True:
67            result = self.builder.out_queue.get()
68            self.builder.ProcessResult(result)
69            self.builder.out_queue.task_done()
70
71
72class BuilderThread(threading.Thread):
73    """This thread builds U-Boot for a particular board.
74
75    An input queue provides each new job. We run 'make' to build U-Boot
76    and then pass the results on to the output queue.
77
78    Members:
79        builder: The builder which contains information we might need
80        thread_num: Our thread number (0-n-1), used to decide on a
81                temporary directory
82    """
83    def __init__(self, builder, thread_num, incremental, per_board_out_dir):
84        """Set up a new builder thread"""
85        threading.Thread.__init__(self)
86        self.builder = builder
87        self.thread_num = thread_num
88        self.incremental = incremental
89        self.per_board_out_dir = per_board_out_dir
90
91    def Make(self, commit, brd, stage, cwd, *args, **kwargs):
92        """Run 'make' on a particular commit and board.
93
94        The source code will already be checked out, so the 'commit'
95        argument is only for information.
96
97        Args:
98            commit: Commit object that is being built
99            brd: Board object that is being built
100            stage: Stage of the build. Valid stages are:
101                        mrproper - can be called to clean source
102                        config - called to configure for a board
103                        build - the main make invocation - it does the build
104            args: A list of arguments to pass to 'make'
105            kwargs: A list of keyword arguments to pass to command.RunPipe()
106
107        Returns:
108            CommandResult object
109        """
110        return self.builder.do_make(commit, brd, stage, cwd, *args,
111                **kwargs)
112
113    def RunCommit(self, commit_upto, brd, work_dir, do_config, force_build,
114                  force_build_failures):
115        """Build a particular commit.
116
117        If the build is already done, and we are not forcing a build, we skip
118        the build and just return the previously-saved results.
119
120        Args:
121            commit_upto: Commit number to build (0...n-1)
122            brd: Board object to build
123            work_dir: Directory to which the source will be checked out
124            do_config: True to run a make <board>_defconfig on the source
125            force_build: Force a build even if one was previously done
126            force_build_failures: Force a bulid if the previous result showed
127                failure
128
129        Returns:
130            tuple containing:
131                - CommandResult object containing the results of the build
132                - boolean indicating whether 'make config' is still needed
133        """
134        # Create a default result - it will be overwritte by the call to
135        # self.Make() below, in the event that we do a build.
136        result = command.CommandResult()
137        result.return_code = 0
138        if self.builder.in_tree:
139            out_dir = work_dir
140        else:
141            if self.per_board_out_dir:
142                out_rel_dir = os.path.join('..', brd.target)
143            else:
144                out_rel_dir = 'build'
145            out_dir = os.path.join(work_dir, out_rel_dir)
146
147        # Check if the job was already completed last time
148        done_file = self.builder.GetDoneFile(commit_upto, brd.target)
149        result.already_done = os.path.exists(done_file)
150        will_build = (force_build or force_build_failures or
151            not result.already_done)
152        if result.already_done:
153            # Get the return code from that build and use it
154            with open(done_file, 'r') as fd:
155                result.return_code = int(fd.readline())
156
157            # Check the signal that the build needs to be retried
158            if result.return_code == RETURN_CODE_RETRY:
159                will_build = True
160            elif will_build:
161                err_file = self.builder.GetErrFile(commit_upto, brd.target)
162                if os.path.exists(err_file) and os.stat(err_file).st_size:
163                    result.stderr = 'bad'
164                elif not force_build:
165                    # The build passed, so no need to build it again
166                    will_build = False
167
168        if will_build:
169            # We are going to have to build it. First, get a toolchain
170            if not self.toolchain:
171                try:
172                    self.toolchain = self.builder.toolchains.Select(brd.arch)
173                except ValueError as err:
174                    result.return_code = 10
175                    result.stdout = ''
176                    result.stderr = str(err)
177                    # TODO(sjg@chromium.org): This gets swallowed, but needs
178                    # to be reported.
179
180            if self.toolchain:
181                # Checkout the right commit
182                if self.builder.commits:
183                    commit = self.builder.commits[commit_upto]
184                    if self.builder.checkout:
185                        git_dir = os.path.join(work_dir, '.git')
186                        gitutil.Checkout(commit.hash, git_dir, work_dir,
187                                         force=True)
188                else:
189                    commit = 'current'
190
191                # Set up the environment and command line
192                env = self.toolchain.MakeEnvironment(self.builder.full_path)
193                Mkdir(out_dir)
194                args = []
195                cwd = work_dir
196                src_dir = os.path.realpath(work_dir)
197                if not self.builder.in_tree:
198                    if commit_upto is None:
199                        # In this case we are building in the original source
200                        # directory (i.e. the current directory where buildman
201                        # is invoked. The output directory is set to this
202                        # thread's selected work directory.
203                        #
204                        # Symlinks can confuse U-Boot's Makefile since
205                        # we may use '..' in our path, so remove them.
206                        out_dir = os.path.realpath(out_dir)
207                        args.append('O=%s' % out_dir)
208                        cwd = None
209                        src_dir = os.getcwd()
210                    else:
211                        args.append('O=%s' % out_rel_dir)
212                if self.builder.verbose_build:
213                    args.append('V=1')
214                else:
215                    args.append('-s')
216                if self.builder.num_jobs is not None:
217                    args.extend(['-j', str(self.builder.num_jobs)])
218                config_args = ['%s_defconfig' % brd.target]
219                config_out = ''
220                args.extend(self.builder.toolchains.GetMakeArguments(brd))
221
222                # If we need to reconfigure, do that now
223                if do_config:
224                    config_out = ''
225                    if not self.incremental:
226                        result = self.Make(commit, brd, 'mrproper', cwd,
227                                'mrproper', *args, env=env)
228                        config_out += result.combined
229                    result = self.Make(commit, brd, 'config', cwd,
230                            *(args + config_args), env=env)
231                    config_out += result.combined
232                    do_config = False   # No need to configure next time
233                if result.return_code == 0:
234                    result = self.Make(commit, brd, 'build', cwd, *args,
235                            env=env)
236                result.stderr = result.stderr.replace(src_dir + '/', '')
237                if self.builder.verbose_build:
238                    result.stdout = config_out + result.stdout
239            else:
240                result.return_code = 1
241                result.stderr = 'No tool chain for %s\n' % brd.arch
242            result.already_done = False
243
244        result.toolchain = self.toolchain
245        result.brd = brd
246        result.commit_upto = commit_upto
247        result.out_dir = out_dir
248        return result, do_config
249
250    def _WriteResult(self, result, keep_outputs):
251        """Write a built result to the output directory.
252
253        Args:
254            result: CommandResult object containing result to write
255            keep_outputs: True to store the output binaries, False
256                to delete them
257        """
258        # Fatal error
259        if result.return_code < 0:
260            return
261
262        # If we think this might have been aborted with Ctrl-C, record the
263        # failure but not that we are 'done' with this board. A retry may fix
264        # it.
265        maybe_aborted =  result.stderr and 'No child processes' in result.stderr
266
267        if result.already_done:
268            return
269
270        # Write the output and stderr
271        output_dir = self.builder._GetOutputDir(result.commit_upto)
272        Mkdir(output_dir)
273        build_dir = self.builder.GetBuildDir(result.commit_upto,
274                result.brd.target)
275        Mkdir(build_dir)
276
277        outfile = os.path.join(build_dir, 'log')
278        with open(outfile, 'w') as fd:
279            if result.stdout:
280                fd.write(result.stdout)
281
282        errfile = self.builder.GetErrFile(result.commit_upto,
283                result.brd.target)
284        if result.stderr:
285            with open(errfile, 'w') as fd:
286                fd.write(result.stderr)
287        elif os.path.exists(errfile):
288            os.remove(errfile)
289
290        if result.toolchain:
291            # Write the build result and toolchain information.
292            done_file = self.builder.GetDoneFile(result.commit_upto,
293                    result.brd.target)
294            with open(done_file, 'w') as fd:
295                if maybe_aborted:
296                    # Special code to indicate we need to retry
297                    fd.write('%s' % RETURN_CODE_RETRY)
298                else:
299                    fd.write('%s' % result.return_code)
300            with open(os.path.join(build_dir, 'toolchain'), 'w') as fd:
301                print >>fd, 'gcc', result.toolchain.gcc
302                print >>fd, 'path', result.toolchain.path
303                print >>fd, 'cross', result.toolchain.cross
304                print >>fd, 'arch', result.toolchain.arch
305                fd.write('%s' % result.return_code)
306
307            # Write out the image and function size information and an objdump
308            env = result.toolchain.MakeEnvironment(self.builder.full_path)
309            lines = []
310            for fname in ['u-boot', 'spl/u-boot-spl']:
311                cmd = ['%snm' % self.toolchain.cross, '--size-sort', fname]
312                nm_result = command.RunPipe([cmd], capture=True,
313                        capture_stderr=True, cwd=result.out_dir,
314                        raise_on_error=False, env=env)
315                if nm_result.stdout:
316                    nm = self.builder.GetFuncSizesFile(result.commit_upto,
317                                    result.brd.target, fname)
318                    with open(nm, 'w') as fd:
319                        print >>fd, nm_result.stdout,
320
321                cmd = ['%sobjdump' % self.toolchain.cross, '-h', fname]
322                dump_result = command.RunPipe([cmd], capture=True,
323                        capture_stderr=True, cwd=result.out_dir,
324                        raise_on_error=False, env=env)
325                rodata_size = ''
326                if dump_result.stdout:
327                    objdump = self.builder.GetObjdumpFile(result.commit_upto,
328                                    result.brd.target, fname)
329                    with open(objdump, 'w') as fd:
330                        print >>fd, dump_result.stdout,
331                    for line in dump_result.stdout.splitlines():
332                        fields = line.split()
333                        if len(fields) > 5 and fields[1] == '.rodata':
334                            rodata_size = fields[2]
335
336                cmd = ['%ssize' % self.toolchain.cross, fname]
337                size_result = command.RunPipe([cmd], capture=True,
338                        capture_stderr=True, cwd=result.out_dir,
339                        raise_on_error=False, env=env)
340                if size_result.stdout:
341                    lines.append(size_result.stdout.splitlines()[1] + ' ' +
342                                 rodata_size)
343
344            # Write out the image sizes file. This is similar to the output
345            # of binutil's 'size' utility, but it omits the header line and
346            # adds an additional hex value at the end of each line for the
347            # rodata size
348            if len(lines):
349                sizes = self.builder.GetSizesFile(result.commit_upto,
350                                result.brd.target)
351                with open(sizes, 'w') as fd:
352                    print >>fd, '\n'.join(lines)
353
354        # Write out the configuration files, with a special case for SPL
355        for dirname in ['', 'spl', 'tpl']:
356            self.CopyFiles(result.out_dir, build_dir, dirname, ['u-boot.cfg',
357                'spl/u-boot-spl.cfg', 'tpl/u-boot-tpl.cfg', '.config',
358                'include/autoconf.mk', 'include/generated/autoconf.h'])
359
360        # Now write the actual build output
361        if keep_outputs:
362            self.CopyFiles(result.out_dir, build_dir, '', ['u-boot*', '*.bin',
363                '*.map', '*.img', 'MLO', 'SPL', 'include/autoconf.mk',
364                'spl/u-boot-spl*'])
365
366    def CopyFiles(self, out_dir, build_dir, dirname, patterns):
367        """Copy files from the build directory to the output.
368
369        Args:
370            out_dir: Path to output directory containing the files
371            build_dir: Place to copy the files
372            dirname: Source directory, '' for normal U-Boot, 'spl' for SPL
373            patterns: A list of filenames (strings) to copy, each relative
374               to the build directory
375        """
376        for pattern in patterns:
377            file_list = glob.glob(os.path.join(out_dir, dirname, pattern))
378            for fname in file_list:
379                target = os.path.basename(fname)
380                if dirname:
381                    base, ext = os.path.splitext(target)
382                    if ext:
383                        target = '%s-%s%s' % (base, dirname, ext)
384                shutil.copy(fname, os.path.join(build_dir, target))
385
386    def RunJob(self, job):
387        """Run a single job
388
389        A job consists of a building a list of commits for a particular board.
390
391        Args:
392            job: Job to build
393        """
394        brd = job.board
395        work_dir = self.builder.GetThreadDir(self.thread_num)
396        self.toolchain = None
397        if job.commits:
398            # Run 'make board_defconfig' on the first commit
399            do_config = True
400            commit_upto  = 0
401            force_build = False
402            for commit_upto in range(0, len(job.commits), job.step):
403                result, request_config = self.RunCommit(commit_upto, brd,
404                        work_dir, do_config,
405                        force_build or self.builder.force_build,
406                        self.builder.force_build_failures)
407                failed = result.return_code or result.stderr
408                did_config = do_config
409                if failed and not do_config:
410                    # If our incremental build failed, try building again
411                    # with a reconfig.
412                    if self.builder.force_config_on_failure:
413                        result, request_config = self.RunCommit(commit_upto,
414                            brd, work_dir, True, True, False)
415                        did_config = True
416                if not self.builder.force_reconfig:
417                    do_config = request_config
418
419                # If we built that commit, then config is done. But if we got
420                # an warning, reconfig next time to force it to build the same
421                # files that created warnings this time. Otherwise an
422                # incremental build may not build the same file, and we will
423                # think that the warning has gone away.
424                # We could avoid this by using -Werror everywhere...
425                # For errors, the problem doesn't happen, since presumably
426                # the build stopped and didn't generate output, so will retry
427                # that file next time. So we could detect warnings and deal
428                # with them specially here. For now, we just reconfigure if
429                # anything goes work.
430                # Of course this is substantially slower if there are build
431                # errors/warnings (e.g. 2-3x slower even if only 10% of builds
432                # have problems).
433                if (failed and not result.already_done and not did_config and
434                        self.builder.force_config_on_failure):
435                    # If this build failed, try the next one with a
436                    # reconfigure.
437                    # Sometimes if the board_config.h file changes it can mess
438                    # with dependencies, and we get:
439                    # make: *** No rule to make target `include/autoconf.mk',
440                    #     needed by `depend'.
441                    do_config = True
442                    force_build = True
443                else:
444                    force_build = False
445                    if self.builder.force_config_on_failure:
446                        if failed:
447                            do_config = True
448                    result.commit_upto = commit_upto
449                    if result.return_code < 0:
450                        raise ValueError('Interrupt')
451
452                # We have the build results, so output the result
453                self._WriteResult(result, job.keep_outputs)
454                self.builder.out_queue.put(result)
455        else:
456            # Just build the currently checked-out build
457            result, request_config = self.RunCommit(None, brd, work_dir, True,
458                        True, self.builder.force_build_failures)
459            result.commit_upto = 0
460            self._WriteResult(result, job.keep_outputs)
461            self.builder.out_queue.put(result)
462
463    def run(self):
464        """Our thread's run function
465
466        This thread picks a job from the queue, runs it, and then goes to the
467        next job.
468        """
469        while True:
470            job = self.builder.queue.get()
471            self.RunJob(job)
472            self.builder.queue.task_done()
473