David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
David James | 7c3a727 | 2014-02-06 18:24:35 -0800 | [diff] [blame] | 5 | """Script for calculating compatible binhosts. |
| 6 | |
| 7 | 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] | 8 | other compatible boards in this buildroot. |
| 9 | """ |
| 10 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame^] | 11 | from __future__ import print_function |
| 12 | |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 13 | import collections |
| 14 | import glob |
| 15 | import optparse |
| 16 | import os |
| 17 | import sys |
| 18 | |
Brian Harring | 1b8c4c8 | 2012-05-29 23:03:04 -0700 | [diff] [blame] | 19 | from chromite.lib import cros_build_lib |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 20 | |
Mike Frysinger | cc83883 | 2014-05-24 13:10:30 -0400 | [diff] [blame] | 21 | |
David James | 7c3a727 | 2014-02-06 18:24:35 -0800 | [diff] [blame] | 22 | def FindCandidateBoards(): |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 23 | """Find candidate local boards to grab prebuilts from.""" |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 24 | portageq_prefix = "/usr/local/bin/portageq-" |
| 25 | for path in sorted(glob.glob("%s*" % portageq_prefix)): |
| 26 | # Strip off the portageq prefix, leaving only the board. |
David James | 7c3a727 | 2014-02-06 18:24:35 -0800 | [diff] [blame] | 27 | yield path.replace(portageq_prefix, "") |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 28 | |
| 29 | |
| 30 | def SummarizeCompatibility(board): |
| 31 | """Returns a string that will be the same for compatible boards.""" |
| 32 | cmd = ["portageq-%s" % board, "envvar", "ARCH", "CFLAGS"] |
Brian Harring | 1b8c4c8 | 2012-05-29 23:03:04 -0700 | [diff] [blame] | 33 | return cros_build_lib.RunCommand(cmd, redirect_stdout=True, |
| 34 | print_cmd=False).output.rstrip() |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 35 | |
| 36 | |
| 37 | def GenerateBinhostLine(build_root, compatible_boards): |
| 38 | """Generate a binhost line pulling binaries from the specified boards.""" |
David James | 602934f | 2014-03-31 10:10:11 -0700 | [diff] [blame] | 39 | # TODO(davidjames): Prioritize binhosts with more matching use flags. |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 40 | local_binhosts = " ".join([ |
| 41 | "file://localhost" + os.path.join(build_root, x, "packages") |
| 42 | for x in sorted(compatible_boards)]) |
| 43 | return "LOCAL_BINHOST='%s'" % local_binhosts |
| 44 | |
| 45 | |
| 46 | def main(argv): |
| 47 | parser = optparse.OptionParser(usage="USAGE: ./%prog --board=board [options]") |
| 48 | |
| 49 | parser.add_option("--build_root", default="/build", |
| 50 | dest="build_root", |
| 51 | help="Location of boards (normally /build)") |
| 52 | parser.add_option("--board", default=None, |
| 53 | dest="board", |
| 54 | help="Board name (required).") |
| 55 | |
| 56 | flags, remaining_arguments = parser.parse_args(argv) |
| 57 | |
| 58 | if remaining_arguments or not flags.board: |
| 59 | parser.print_help() |
| 60 | sys.exit(1) |
| 61 | |
| 62 | by_compatibility = collections.defaultdict(set) |
| 63 | compatible_boards = None |
David James | 7c3a727 | 2014-02-06 18:24:35 -0800 | [diff] [blame] | 64 | for other_board in FindCandidateBoards(): |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 65 | compat_id = SummarizeCompatibility(other_board) |
| 66 | if other_board == flags.board: |
| 67 | compatible_boards = by_compatibility[compat_id] |
| 68 | else: |
| 69 | by_compatibility[compat_id].add(other_board) |
| 70 | |
| 71 | if compatible_boards is None: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame^] | 72 | print('Missing portageq wrapper for %s' % flags.board, file=sys.stderr) |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 73 | sys.exit(1) |
| 74 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame^] | 75 | print('# Generated by cros_generate_local_binhosts.') |
| 76 | print(GenerateBinhostLine(flags.build_root, compatible_boards)) |