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