xref: /openbmc/u-boot/tools/buildman/control.py (revision 9458f6d8)
1# Copyright (c) 2013 The Chromium OS Authors.
2#
3# See file CREDITS for list of people who contributed to this
4# project.
5#
6# This program is free software; you can redistribute it and/or
7# modify it under the terms of the GNU General Public License as
8# published by the Free Software Foundation; either version 2 of
9# the License, or (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19# MA 02111-1307 USA
20#
21
22import multiprocessing
23import os
24import sys
25
26import board
27import bsettings
28from builder import Builder
29import gitutil
30import patchstream
31import terminal
32import toolchain
33
34def GetPlural(count):
35    """Returns a plural 's' if count is not 1"""
36    return 's' if count != 1 else ''
37
38def GetActionSummary(is_summary, count, selected, options):
39    """Return a string summarising the intended action.
40
41    Returns:
42        Summary string.
43    """
44    count = (count + options.step - 1) / options.step
45    str = '%s %d commit%s for %d boards' % (
46        'Summary of' if is_summary else 'Building', count, GetPlural(count),
47        len(selected))
48    str += ' (%d thread%s, %d job%s per thread)' % (options.threads,
49            GetPlural(options.threads), options.jobs, GetPlural(options.jobs))
50    return str
51
52def ShowActions(series, why_selected, boards_selected, builder, options):
53    """Display a list of actions that we would take, if not a dry run.
54
55    Args:
56        series: Series object
57        why_selected: Dictionary where each key is a buildman argument
58                provided by the user, and the value is the boards brought
59                in by that argument. For example, 'arm' might bring in
60                400 boards, so in this case the key would be 'arm' and
61                the value would be a list of board names.
62        boards_selected: Dict of selected boards, key is target name,
63                value is Board object
64        builder: The builder that will be used to build the commits
65        options: Command line options object
66    """
67    col = terminal.Color()
68    print 'Dry run, so not doing much. But I would do this:'
69    print
70    print GetActionSummary(False, len(series.commits), boards_selected,
71            options)
72    print 'Build directory: %s' % builder.base_dir
73    for upto in range(0, len(series.commits), options.step):
74        commit = series.commits[upto]
75        print '   ', col.Color(col.YELLOW, commit.hash, bright=False),
76        print commit.subject
77    print
78    for arg in why_selected:
79        if arg != 'all':
80            print arg, ': %d boards' % why_selected[arg]
81    print ('Total boards to build for each commit: %d\n' %
82            why_selected['all'])
83
84def DoBuildman(options, args):
85    """The main control code for buildman
86
87    Args:
88        options: Command line options object
89        args: Command line arguments (list of strings)
90    """
91    gitutil.Setup()
92
93    bsettings.Setup()
94    options.git_dir = os.path.join(options.git, '.git')
95
96    toolchains = toolchain.Toolchains()
97    toolchains.Scan(options.list_tool_chains)
98    if options.list_tool_chains:
99        toolchains.List()
100        print
101        return
102
103    # Work out how many commits to build. We want to build everything on the
104    # branch. We also build the upstream commit as a control so we can see
105    # problems introduced by the first commit on the branch.
106    col = terminal.Color()
107    count = options.count
108    if count == -1:
109        if not options.branch:
110            str = 'Please use -b to specify a branch to build'
111            print col.Color(col.RED, str)
112            sys.exit(1)
113        count = gitutil.CountCommitsInBranch(options.git_dir, options.branch)
114        count += 1   # Build upstream commit also
115
116    if not count:
117        str = ("No commits found to process in branch '%s': "
118               "set branch's upstream or use -c flag" % options.branch)
119        print col.Color(col.RED, str)
120        sys.exit(1)
121
122    # Work out what subset of the boards we are building
123    boards = board.Boards()
124    boards.ReadBoards(os.path.join(options.git, 'boards.cfg'))
125    why_selected = boards.SelectBoards(args)
126    selected = boards.GetSelected()
127    if not len(selected):
128        print col.Color(col.RED, 'No matching boards found')
129        sys.exit(1)
130
131    # Read the metadata from the commits. First look at the upstream commit,
132    # then the ones in the branch. We would like to do something like
133    # upstream/master~..branch but that isn't possible if upstream/master is
134    # a merge commit (it will list all the commits that form part of the
135    # merge)
136    range_expr = gitutil.GetRangeInBranch(options.git_dir, options.branch)
137    upstream_commit = gitutil.GetUpstream(options.git_dir, options.branch)
138    series = patchstream.GetMetaDataForList(upstream_commit, options.git_dir,
139            1)
140    # Conflicting tags are not a problem for buildman, since it does not use
141    # them. For example, Series-version is not useful for buildman. On the
142    # other hand conflicting tags will cause an error. So allow later tags
143    # to overwrite earlier ones.
144    series.allow_overwrite = True
145    series = patchstream.GetMetaDataForList(range_expr, options.git_dir, None,
146            series)
147
148    # By default we have one thread per CPU. But if there are not enough jobs
149    # we can have fewer threads and use a high '-j' value for make.
150    if not options.threads:
151        options.threads = min(multiprocessing.cpu_count(), len(selected))
152    if not options.jobs:
153        options.jobs = max(1, (multiprocessing.cpu_count() +
154                len(selected) - 1) / len(selected))
155
156    if not options.step:
157        options.step = len(series.commits) - 1
158
159    # Create a new builder with the selected options
160    output_dir = os.path.join('..', options.branch)
161    builder = Builder(toolchains, output_dir, options.git_dir,
162            options.threads, options.jobs, checkout=True,
163            show_unknown=options.show_unknown, step=options.step)
164    builder.force_config_on_failure = not options.quick
165
166    # For a dry run, just show our actions as a sanity check
167    if options.dry_run:
168        ShowActions(series, why_selected, selected, builder, options)
169    else:
170        builder.force_build = options.force_build
171
172        # Work out which boards to build
173        board_selected = boards.GetSelectedDict()
174
175        print GetActionSummary(options.summary, count, board_selected, options)
176
177        if options.summary:
178            # We can't show function sizes without board details at present
179            if options.show_bloat:
180                options.show_detail = True
181            builder.ShowSummary(series.commits, board_selected,
182                    options.show_errors, options.show_sizes,
183                    options.show_detail, options.show_bloat)
184        else:
185            builder.BuildBoards(series.commits, board_selected,
186                    options.show_errors, options.keep_outputs)
187