xref: /openbmc/u-boot/tools/buildman/control.py (revision 983c72f4)
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        if count is None:
115            str = "Branch '%s' not found or has no upstream" % options.branch
116            print col.Color(col.RED, str)
117            sys.exit(1)
118        count += 1   # Build upstream commit also
119
120    if not count:
121        str = ("No commits found to process in branch '%s': "
122               "set branch's upstream or use -c flag" % options.branch)
123        print col.Color(col.RED, str)
124        sys.exit(1)
125
126    # Work out what subset of the boards we are building
127    boards = board.Boards()
128    boards.ReadBoards(os.path.join(options.git, 'boards.cfg'))
129    why_selected = boards.SelectBoards(args)
130    selected = boards.GetSelected()
131    if not len(selected):
132        print col.Color(col.RED, 'No matching boards found')
133        sys.exit(1)
134
135    # Read the metadata from the commits. First look at the upstream commit,
136    # then the ones in the branch. We would like to do something like
137    # upstream/master~..branch but that isn't possible if upstream/master is
138    # a merge commit (it will list all the commits that form part of the
139    # merge)
140    range_expr = gitutil.GetRangeInBranch(options.git_dir, options.branch)
141    upstream_commit = gitutil.GetUpstream(options.git_dir, options.branch)
142    series = patchstream.GetMetaDataForList(upstream_commit, options.git_dir,
143            1)
144    # Conflicting tags are not a problem for buildman, since it does not use
145    # them. For example, Series-version is not useful for buildman. On the
146    # other hand conflicting tags will cause an error. So allow later tags
147    # to overwrite earlier ones.
148    series.allow_overwrite = True
149    series = patchstream.GetMetaDataForList(range_expr, options.git_dir, None,
150            series)
151
152    # By default we have one thread per CPU. But if there are not enough jobs
153    # we can have fewer threads and use a high '-j' value for make.
154    if not options.threads:
155        options.threads = min(multiprocessing.cpu_count(), len(selected))
156    if not options.jobs:
157        options.jobs = max(1, (multiprocessing.cpu_count() +
158                len(selected) - 1) / len(selected))
159
160    if not options.step:
161        options.step = len(series.commits) - 1
162
163    # Create a new builder with the selected options
164    output_dir = os.path.join('..', options.branch)
165    builder = Builder(toolchains, output_dir, options.git_dir,
166            options.threads, options.jobs, checkout=True,
167            show_unknown=options.show_unknown, step=options.step)
168    builder.force_config_on_failure = not options.quick
169
170    # For a dry run, just show our actions as a sanity check
171    if options.dry_run:
172        ShowActions(series, why_selected, selected, builder, options)
173    else:
174        builder.force_build = options.force_build
175
176        # Work out which boards to build
177        board_selected = boards.GetSelectedDict()
178
179        print GetActionSummary(options.summary, count, board_selected, options)
180
181        if options.summary:
182            # We can't show function sizes without board details at present
183            if options.show_bloat:
184                options.show_detail = True
185            builder.ShowSummary(series.commits, board_selected,
186                    options.show_errors, options.show_sizes,
187                    options.show_detail, options.show_bloat)
188        else:
189            builder.BuildBoards(series.commits, board_selected,
190                    options.show_errors, options.keep_outputs)
191