Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
David James | 7c3a727 | 2014-02-06 18:24:35 -0800 | [diff] [blame] | 6 | """Script for calculating compatible binhosts. |
| 7 | |
| 8 | Generates a file that sets the specified board's binhosts to include all of the |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 9 | other compatible boards in this buildroot. |
| 10 | """ |
| 11 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 12 | from __future__ import print_function |
| 13 | |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 14 | import collections |
| 15 | import glob |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 16 | import os |
| 17 | import sys |
| 18 | |
Mike Frysinger | a67f8d9 | 2016-09-01 00:40:01 -0400 | [diff] [blame] | 19 | from chromite.lib import commandline |
Brian Harring | 1b8c4c8 | 2012-05-29 23:03:04 -0700 | [diff] [blame] | 20 | from chromite.lib import cros_build_lib |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 21 | |
Mike Frysinger | cc83883 | 2014-05-24 13:10:30 -0400 | [diff] [blame] | 22 | |
David James | 7c3a727 | 2014-02-06 18:24:35 -0800 | [diff] [blame] | 23 | def FindCandidateBoards(): |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 24 | """Find candidate local boards to grab prebuilts from.""" |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 25 | portageq_prefix = "/usr/local/bin/portageq-" |
| 26 | for path in sorted(glob.glob("%s*" % portageq_prefix)): |
| 27 | # Strip off the portageq prefix, leaving only the board. |
David James | 7c3a727 | 2014-02-06 18:24:35 -0800 | [diff] [blame] | 28 | yield path.replace(portageq_prefix, "") |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 29 | |
| 30 | |
| 31 | def SummarizeCompatibility(board): |
| 32 | """Returns a string that will be the same for compatible boards.""" |
| 33 | cmd = ["portageq-%s" % board, "envvar", "ARCH", "CFLAGS"] |
Yunlian Jiang | 0aad8ad | 2015-06-11 14:28:12 -0700 | [diff] [blame] | 34 | summary = cros_build_lib.RunCommand(cmd, redirect_stdout=True, |
| 35 | print_cmd=False).output.rstrip() |
| 36 | # We will add -clang-syntax to falco and nyan board. So we need to |
| 37 | # filter out -clang-syntax to make the flags from PFQ are the same as |
| 38 | # the release-board. See crbug.com/499115 |
| 39 | # TODO(yunlian): Remove this when all the boards are build with -clang-syntax |
| 40 | return summary.replace(" -clang-syntax", "") |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 41 | |
| 42 | |
| 43 | def GenerateBinhostLine(build_root, compatible_boards): |
| 44 | """Generate a binhost line pulling binaries from the specified boards.""" |
David James | 602934f | 2014-03-31 10:10:11 -0700 | [diff] [blame] | 45 | # TODO(davidjames): Prioritize binhosts with more matching use flags. |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 46 | local_binhosts = " ".join([ |
| 47 | "file://localhost" + os.path.join(build_root, x, "packages") |
| 48 | for x in sorted(compatible_boards)]) |
| 49 | return "LOCAL_BINHOST='%s'" % local_binhosts |
| 50 | |
| 51 | |
Mike Frysinger | a67f8d9 | 2016-09-01 00:40:01 -0400 | [diff] [blame] | 52 | def GetParser(): |
| 53 | """Return a command line parser.""" |
| 54 | parser = commandline.ArgumentParser(description=__doc__) |
| 55 | parser.add_argument('--build_root', default='/build', |
| 56 | help='Location of boards (normally %(default)s)') |
| 57 | parser.add_argument('--board', required=True, |
| 58 | help='Board name') |
| 59 | return parser |
| 60 | |
| 61 | |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 62 | def main(argv): |
Mike Frysinger | a67f8d9 | 2016-09-01 00:40:01 -0400 | [diff] [blame] | 63 | parser = GetParser() |
| 64 | flags = parser.parse_args(argv) |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 65 | |
| 66 | by_compatibility = collections.defaultdict(set) |
| 67 | compatible_boards = None |
David James | 7c3a727 | 2014-02-06 18:24:35 -0800 | [diff] [blame] | 68 | for other_board in FindCandidateBoards(): |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 69 | compat_id = SummarizeCompatibility(other_board) |
| 70 | if other_board == flags.board: |
| 71 | compatible_boards = by_compatibility[compat_id] |
| 72 | else: |
| 73 | by_compatibility[compat_id].add(other_board) |
| 74 | |
| 75 | if compatible_boards is None: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 76 | print('Missing portageq wrapper for %s' % flags.board, file=sys.stderr) |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 77 | sys.exit(1) |
| 78 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 79 | print('# Generated by cros_generate_local_binhosts.') |
| 80 | print(GenerateBinhostLine(flags.build_root, compatible_boards)) |