1 # Copyright (c) 2013 The Chromium OS Authors.
3 # See file CREDITS for list of people who contributed to this
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.
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.
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,
22 import multiprocessing
28 from builder import Builder
35 """Returns a plural 's' if count is not 1"""
36 return 's' if count != 1 else ''
38 def GetActionSummary(is_summary, count, selected, options):
39 """Return a string summarising the intended action.
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),
48 str += ' (%d thread%s, %d job%s per thread)' % (options.threads,
49 GetPlural(options.threads), options.jobs, GetPlural(options.jobs))
52 def ShowActions(series, why_selected, boards_selected, builder, options):
53 """Display a list of actions that we would take, if not a dry run.
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,
64 builder: The builder that will be used to build the commits
65 options: Command line options object
67 col = terminal.Color()
68 print 'Dry run, so not doing much. But I would do this:'
70 print GetActionSummary(False, len(series.commits), boards_selected,
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),
78 for arg in why_selected:
80 print arg, ': %d boards' % why_selected[arg]
81 print ('Total boards to build for each commit: %d\n' %
84 def DoBuildman(options, args):
85 """The main control code for buildman
88 options: Command line options object
89 args: Command line arguments (list of strings)
94 options.git_dir = os.path.join(options.git, '.git')
96 toolchains = toolchain.Toolchains()
97 toolchains.Scan(options.list_tool_chains)
98 if options.list_tool_chains:
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
109 if not options.branch:
110 str = 'Please use -b to specify a branch to build'
111 print col.Color(col.RED, str)
113 count = gitutil.CountCommitsInBranch(options.git_dir, options.branch)
115 str = "Branch '%s' not found or has no upstream" % options.branch
116 print col.Color(col.RED, str)
118 count += 1 # Build upstream commit also
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)
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')
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
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,
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,
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))
157 options.jobs = max(1, (multiprocessing.cpu_count() +
158 len(selected) - 1) / len(selected))
161 options.step = len(series.commits) - 1
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
170 # For a dry run, just show our actions as a sanity check
172 ShowActions(series, why_selected, selected, builder, options)
174 builder.force_build = options.force_build
176 # Work out which boards to build
177 board_selected = boards.GetSelectedDict()
179 print GetActionSummary(options.summary, count, board_selected, options)
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)
189 builder.BuildBoards(series.commits, board_selected,
190 options.show_errors, options.keep_outputs)