xref: /openbmc/u-boot/tools/buildman/control.py (revision b1e6c4c3)
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    series = patchstream.GetMetaDataForList(range_expr, options.git_dir, None,
141            series)
142
143    # By default we have one thread per CPU. But if there are not enough jobs
144    # we can have fewer threads and use a high '-j' value for make.
145    if not options.threads:
146        options.threads = min(multiprocessing.cpu_count(), len(selected))
147    if not options.jobs:
148        options.jobs = max(1, (multiprocessing.cpu_count() +
149                len(selected) - 1) / len(selected))
150
151    if not options.step:
152        options.step = len(series.commits) - 1
153
154    # Create a new builder with the selected options
155    output_dir = os.path.join('..', options.branch)
156    builder = Builder(toolchains, output_dir, options.git_dir,
157            options.threads, options.jobs, checkout=True,
158            show_unknown=options.show_unknown, step=options.step)
159    builder.force_config_on_failure = not options.quick
160
161    # For a dry run, just show our actions as a sanity check
162    if options.dry_run:
163        ShowActions(series, why_selected, selected, builder, options)
164    else:
165        builder.force_build = options.force_build
166
167        # Work out which boards to build
168        board_selected = boards.GetSelectedDict()
169
170        print GetActionSummary(options.summary, count, board_selected, options)
171
172        if options.summary:
173            # We can't show function sizes without board details at present
174            if options.show_bloat:
175                options.show_detail = True
176            builder.ShowSummary(series.commits, board_selected,
177                    options.show_errors, options.show_sizes,
178                    options.show_detail, options.show_bloat)
179        else:
180            builder.BuildBoards(series.commits, board_selected,
181                    options.show_errors, options.keep_outputs)
182