xref: /rk3399_rockchip-uboot/tools/buildman/control.py (revision 3cf4ae6f8672cd10ddba4b18bf82e4d03aeb15e6)
1fc3fe1c2SSimon Glass# Copyright (c) 2013 The Chromium OS Authors.
2fc3fe1c2SSimon Glass#
31a459660SWolfgang Denk# SPDX-License-Identifier:	GPL-2.0+
4fc3fe1c2SSimon Glass#
5fc3fe1c2SSimon Glass
6fc3fe1c2SSimon Glassimport multiprocessing
7fc3fe1c2SSimon Glassimport os
8fc3fe1c2SSimon Glassimport sys
9fc3fe1c2SSimon Glass
10fc3fe1c2SSimon Glassimport board
11fc3fe1c2SSimon Glassimport bsettings
12fc3fe1c2SSimon Glassfrom builder import Builder
13fc3fe1c2SSimon Glassimport gitutil
14fc3fe1c2SSimon Glassimport patchstream
15fc3fe1c2SSimon Glassimport terminal
16fc3fe1c2SSimon Glassimport toolchain
1799796923SMasahiro Yamadaimport command
1873f30b9bSMasahiro Yamadaimport subprocess
19fc3fe1c2SSimon Glass
20fc3fe1c2SSimon Glassdef GetPlural(count):
21fc3fe1c2SSimon Glass    """Returns a plural 's' if count is not 1"""
22fc3fe1c2SSimon Glass    return 's' if count != 1 else ''
23fc3fe1c2SSimon Glass
24fea5858eSSimon Glassdef GetActionSummary(is_summary, commits, selected, options):
25fc3fe1c2SSimon Glass    """Return a string summarising the intended action.
26fc3fe1c2SSimon Glass
27fc3fe1c2SSimon Glass    Returns:
28fc3fe1c2SSimon Glass        Summary string.
29fc3fe1c2SSimon Glass    """
30fea5858eSSimon Glass    if commits:
31fea5858eSSimon Glass        count = len(commits)
32fc3fe1c2SSimon Glass        count = (count + options.step - 1) / options.step
33fea5858eSSimon Glass        commit_str = '%d commit%s' % (count, GetPlural(count))
34fea5858eSSimon Glass    else:
35fea5858eSSimon Glass        commit_str = 'current source'
36fea5858eSSimon Glass    str = '%s %s for %d boards' % (
37fea5858eSSimon Glass        'Summary of' if is_summary else 'Building', commit_str,
38fc3fe1c2SSimon Glass        len(selected))
39fc3fe1c2SSimon Glass    str += ' (%d thread%s, %d job%s per thread)' % (options.threads,
40fc3fe1c2SSimon Glass            GetPlural(options.threads), options.jobs, GetPlural(options.jobs))
41fc3fe1c2SSimon Glass    return str
42fc3fe1c2SSimon Glass
43fc3fe1c2SSimon Glassdef ShowActions(series, why_selected, boards_selected, builder, options):
44fc3fe1c2SSimon Glass    """Display a list of actions that we would take, if not a dry run.
45fc3fe1c2SSimon Glass
46fc3fe1c2SSimon Glass    Args:
47fc3fe1c2SSimon Glass        series: Series object
48fc3fe1c2SSimon Glass        why_selected: Dictionary where each key is a buildman argument
49fc3fe1c2SSimon Glass                provided by the user, and the value is the boards brought
50fc3fe1c2SSimon Glass                in by that argument. For example, 'arm' might bring in
51fc3fe1c2SSimon Glass                400 boards, so in this case the key would be 'arm' and
52fc3fe1c2SSimon Glass                the value would be a list of board names.
53fc3fe1c2SSimon Glass        boards_selected: Dict of selected boards, key is target name,
54fc3fe1c2SSimon Glass                value is Board object
55fc3fe1c2SSimon Glass        builder: The builder that will be used to build the commits
56fc3fe1c2SSimon Glass        options: Command line options object
57fc3fe1c2SSimon Glass    """
58fc3fe1c2SSimon Glass    col = terminal.Color()
59fc3fe1c2SSimon Glass    print 'Dry run, so not doing much. But I would do this:'
60fc3fe1c2SSimon Glass    print
61fea5858eSSimon Glass    if series:
62fea5858eSSimon Glass        commits = series.commits
63fea5858eSSimon Glass    else:
64fea5858eSSimon Glass        commits = None
65fea5858eSSimon Glass    print GetActionSummary(False, commits, boards_selected,
66fc3fe1c2SSimon Glass            options)
67fc3fe1c2SSimon Glass    print 'Build directory: %s' % builder.base_dir
68fea5858eSSimon Glass    if commits:
69fc3fe1c2SSimon Glass        for upto in range(0, len(series.commits), options.step):
70fc3fe1c2SSimon Glass            commit = series.commits[upto]
71fc3fe1c2SSimon Glass            print '   ', col.Color(col.YELLOW, commit.hash, bright=False),
72fc3fe1c2SSimon Glass            print commit.subject
73fc3fe1c2SSimon Glass    print
74fc3fe1c2SSimon Glass    for arg in why_selected:
75fc3fe1c2SSimon Glass        if arg != 'all':
76fc3fe1c2SSimon Glass            print arg, ': %d boards' % why_selected[arg]
77fc3fe1c2SSimon Glass    print ('Total boards to build for each commit: %d\n' %
78fc3fe1c2SSimon Glass            why_selected['all'])
79fc3fe1c2SSimon Glass
80fc3fe1c2SSimon Glassdef DoBuildman(options, args):
81fc3fe1c2SSimon Glass    """The main control code for buildman
82fc3fe1c2SSimon Glass
83fc3fe1c2SSimon Glass    Args:
84fc3fe1c2SSimon Glass        options: Command line options object
85fc3fe1c2SSimon Glass        args: Command line arguments (list of strings)
86fc3fe1c2SSimon Glass    """
87fc3fe1c2SSimon Glass    gitutil.Setup()
88fc3fe1c2SSimon Glass
890f7c9ddaSSimon Glass    bsettings.Setup(options.config_file)
90fc3fe1c2SSimon Glass    options.git_dir = os.path.join(options.git, '.git')
91fc3fe1c2SSimon Glass
92fc3fe1c2SSimon Glass    toolchains = toolchain.Toolchains()
93fc3fe1c2SSimon Glass    toolchains.Scan(options.list_tool_chains)
94fc3fe1c2SSimon Glass    if options.list_tool_chains:
95fc3fe1c2SSimon Glass        toolchains.List()
96fc3fe1c2SSimon Glass        print
972c3deb97SSimon Glass        return 0
98fc3fe1c2SSimon Glass
99fc3fe1c2SSimon Glass    # Work out how many commits to build. We want to build everything on the
100fc3fe1c2SSimon Glass    # branch. We also build the upstream commit as a control so we can see
101fc3fe1c2SSimon Glass    # problems introduced by the first commit on the branch.
102fc3fe1c2SSimon Glass    col = terminal.Color()
103fc3fe1c2SSimon Glass    count = options.count
104fc3fe1c2SSimon Glass    if count == -1:
105fc3fe1c2SSimon Glass        if not options.branch:
106fea5858eSSimon Glass            count = 1
107fea5858eSSimon Glass        else:
108fea5858eSSimon Glass            count = gitutil.CountCommitsInBranch(options.git_dir,
109fea5858eSSimon Glass                                                 options.branch)
110cce717a9SSimon Glass            if count is None:
111fea5858eSSimon Glass                str = ("Branch '%s' not found or has no upstream" %
112fea5858eSSimon Glass                       options.branch)
11331e2141dSMasahiro Yamada                sys.exit(col.Color(col.RED, str))
114fc3fe1c2SSimon Glass            count += 1   # Build upstream commit also
115fc3fe1c2SSimon Glass
116fc3fe1c2SSimon Glass    if not count:
117fc3fe1c2SSimon Glass        str = ("No commits found to process in branch '%s': "
118fc3fe1c2SSimon Glass               "set branch's upstream or use -c flag" % options.branch)
11931e2141dSMasahiro Yamada        sys.exit(col.Color(col.RED, str))
120fc3fe1c2SSimon Glass
121fc3fe1c2SSimon Glass    # Work out what subset of the boards we are building
12273f30b9bSMasahiro Yamada    board_file = os.path.join(options.git, 'boards.cfg')
12373f30b9bSMasahiro Yamada    status = subprocess.call([os.path.join(options.git,
12473f30b9bSMasahiro Yamada                                           'tools/genboardscfg.py')])
12573f30b9bSMasahiro Yamada    if status != 0:
12631e2141dSMasahiro Yamada        sys.exit("Failed to generate boards.cfg")
12773f30b9bSMasahiro Yamada
128fc3fe1c2SSimon Glass    boards = board.Boards()
129fc3fe1c2SSimon Glass    boards.ReadBoards(os.path.join(options.git, 'boards.cfg'))
130*3cf4ae6fSSimon Glass
131*3cf4ae6fSSimon Glass    exclude = []
132*3cf4ae6fSSimon Glass    if options.exclude:
133*3cf4ae6fSSimon Glass        for arg in options.exclude:
134*3cf4ae6fSSimon Glass            exclude += arg.split(',')
135*3cf4ae6fSSimon Glass
136*3cf4ae6fSSimon Glass    why_selected = boards.SelectBoards(args, exclude)
137fc3fe1c2SSimon Glass    selected = boards.GetSelected()
138fc3fe1c2SSimon Glass    if not len(selected):
13931e2141dSMasahiro Yamada        sys.exit(col.Color(col.RED, 'No matching boards found'))
140fc3fe1c2SSimon Glass
141fc3fe1c2SSimon Glass    # Read the metadata from the commits. First look at the upstream commit,
142fc3fe1c2SSimon Glass    # then the ones in the branch. We would like to do something like
143fc3fe1c2SSimon Glass    # upstream/master~..branch but that isn't possible if upstream/master is
144fc3fe1c2SSimon Glass    # a merge commit (it will list all the commits that form part of the
145fc3fe1c2SSimon Glass    # merge)
146fea5858eSSimon Glass    if options.branch:
1473b74ba5fSSimon Glass        if count == -1:
1483b74ba5fSSimon Glass            range_expr = gitutil.GetRangeInBranch(options.git_dir,
1493b74ba5fSSimon Glass                                                  options.branch)
1503b74ba5fSSimon Glass            upstream_commit = gitutil.GetUpstream(options.git_dir,
1513b74ba5fSSimon Glass                                                  options.branch)
152fea5858eSSimon Glass            series = patchstream.GetMetaDataForList(upstream_commit,
153fea5858eSSimon Glass                options.git_dir, 1)
154fea5858eSSimon Glass
1553b74ba5fSSimon Glass            # Conflicting tags are not a problem for buildman, since it does
1563b74ba5fSSimon Glass            # not use them. For example, Series-version is not useful for
1573b74ba5fSSimon Glass            # buildman. On the other hand conflicting tags will cause an
1583b74ba5fSSimon Glass            # error. So allow later tags to overwrite earlier ones.
159f0b739f1SSimon Glass            series.allow_overwrite = True
1603b74ba5fSSimon Glass            series = patchstream.GetMetaDataForList(range_expr,
1613b74ba5fSSimon Glass                                              options.git_dir, None, series)
1623b74ba5fSSimon Glass        else:
1633b74ba5fSSimon Glass            # Honour the count
1643b74ba5fSSimon Glass            series = patchstream.GetMetaDataForList(options.branch,
1653b74ba5fSSimon Glass                                                    options.git_dir, count)
166fea5858eSSimon Glass    else:
167fea5858eSSimon Glass        series = None
168e5a0e5d8SSimon Glass        options.verbose = True
169e5a0e5d8SSimon Glass        options.show_errors = True
170fc3fe1c2SSimon Glass
171fc3fe1c2SSimon Glass    # By default we have one thread per CPU. But if there are not enough jobs
172fc3fe1c2SSimon Glass    # we can have fewer threads and use a high '-j' value for make.
173fc3fe1c2SSimon Glass    if not options.threads:
174fc3fe1c2SSimon Glass        options.threads = min(multiprocessing.cpu_count(), len(selected))
175fc3fe1c2SSimon Glass    if not options.jobs:
176fc3fe1c2SSimon Glass        options.jobs = max(1, (multiprocessing.cpu_count() +
177fc3fe1c2SSimon Glass                len(selected) - 1) / len(selected))
178fc3fe1c2SSimon Glass
179fc3fe1c2SSimon Glass    if not options.step:
180fc3fe1c2SSimon Glass        options.step = len(series.commits) - 1
181fc3fe1c2SSimon Glass
18299796923SMasahiro Yamada    gnu_make = command.Output(os.path.join(options.git,
18399796923SMasahiro Yamada                                           'scripts/show-gnu-make')).rstrip()
18499796923SMasahiro Yamada    if not gnu_make:
18531e2141dSMasahiro Yamada        sys.exit('GNU Make not found')
18699796923SMasahiro Yamada
187fc3fe1c2SSimon Glass    # Create a new builder with the selected options
188fea5858eSSimon Glass    if options.branch:
189fea5858eSSimon Glass        dirname = options.branch
190fea5858eSSimon Glass    else:
191fea5858eSSimon Glass        dirname = 'current'
192fea5858eSSimon Glass    output_dir = os.path.join(options.output_dir, dirname)
193fc3fe1c2SSimon Glass    builder = Builder(toolchains, output_dir, options.git_dir,
19499796923SMasahiro Yamada            options.threads, options.jobs, gnu_make=gnu_make, checkout=True,
195fc3fe1c2SSimon Glass            show_unknown=options.show_unknown, step=options.step)
196fc3fe1c2SSimon Glass    builder.force_config_on_failure = not options.quick
197fc3fe1c2SSimon Glass
198fc3fe1c2SSimon Glass    # For a dry run, just show our actions as a sanity check
199fc3fe1c2SSimon Glass    if options.dry_run:
200fc3fe1c2SSimon Glass        ShowActions(series, why_selected, selected, builder, options)
201fc3fe1c2SSimon Glass    else:
202fc3fe1c2SSimon Glass        builder.force_build = options.force_build
2034266dc28SSimon Glass        builder.force_build_failures = options.force_build_failures
20497e91526SSimon Glass        builder.force_reconfig = options.force_reconfig
205189a4968SSimon Glass        builder.in_tree = options.in_tree
206fc3fe1c2SSimon Glass
207fc3fe1c2SSimon Glass        # Work out which boards to build
208fc3fe1c2SSimon Glass        board_selected = boards.GetSelectedDict()
209fc3fe1c2SSimon Glass
210fea5858eSSimon Glass        if series:
211fea5858eSSimon Glass            commits = series.commits
212fea5858eSSimon Glass        else:
213fea5858eSSimon Glass            commits = None
214fea5858eSSimon Glass
215fea5858eSSimon Glass        print GetActionSummary(options.summary, commits, board_selected,
216fea5858eSSimon Glass                               options)
217fc3fe1c2SSimon Glass
218b2ea7ab2SSimon Glass        builder.SetDisplayOptions(options.show_errors, options.show_sizes,
219b2ea7ab2SSimon Glass                                  options.show_detail, options.show_bloat)
220fc3fe1c2SSimon Glass        if options.summary:
221fc3fe1c2SSimon Glass            # We can't show function sizes without board details at present
222fc3fe1c2SSimon Glass            if options.show_bloat:
223fc3fe1c2SSimon Glass                options.show_detail = True
224b2ea7ab2SSimon Glass            builder.ShowSummary(commits, board_selected)
225fc3fe1c2SSimon Glass        else:
2262c3deb97SSimon Glass            fail, warned = builder.BuildBoards(commits, board_selected,
227e5a0e5d8SSimon Glass                                options.keep_outputs, options.verbose)
2282c3deb97SSimon Glass            if fail:
2292c3deb97SSimon Glass                return 128
2302c3deb97SSimon Glass            elif warned:
2312c3deb97SSimon Glass                return 129
2322c3deb97SSimon Glass    return 0
233