blob: c85995a59e4e19b9b2396dfbe7e692951607bf7c [file] [log] [blame]
David James4a697fd2012-02-11 15:27:53 -08001# 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 James7c3a7272014-02-06 18:24:35 -08005"""Script for calculating compatible binhosts.
6
7Generates a file that sets the specified board's binhosts to include all of the
David James4a697fd2012-02-11 15:27:53 -08008other compatible boards in this buildroot.
9"""
10
Mike Frysinger383367e2014-09-16 15:06:17 -040011from __future__ import print_function
12
David James4a697fd2012-02-11 15:27:53 -080013import collections
14import glob
15import optparse
16import os
17import sys
18
Brian Harring1b8c4c82012-05-29 23:03:04 -070019from chromite.lib import cros_build_lib
David James4a697fd2012-02-11 15:27:53 -080020
Mike Frysingercc838832014-05-24 13:10:30 -040021
David James7c3a7272014-02-06 18:24:35 -080022def FindCandidateBoards():
David James4a697fd2012-02-11 15:27:53 -080023 """Find candidate local boards to grab prebuilts from."""
David James4a697fd2012-02-11 15:27:53 -080024 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 James7c3a7272014-02-06 18:24:35 -080027 yield path.replace(portageq_prefix, "")
David James4a697fd2012-02-11 15:27:53 -080028
29
30def SummarizeCompatibility(board):
31 """Returns a string that will be the same for compatible boards."""
32 cmd = ["portageq-%s" % board, "envvar", "ARCH", "CFLAGS"]
Brian Harring1b8c4c82012-05-29 23:03:04 -070033 return cros_build_lib.RunCommand(cmd, redirect_stdout=True,
34 print_cmd=False).output.rstrip()
David James4a697fd2012-02-11 15:27:53 -080035
36
37def GenerateBinhostLine(build_root, compatible_boards):
38 """Generate a binhost line pulling binaries from the specified boards."""
David James602934f2014-03-31 10:10:11 -070039 # TODO(davidjames): Prioritize binhosts with more matching use flags.
David James4a697fd2012-02-11 15:27:53 -080040 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
46def 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 James7c3a7272014-02-06 18:24:35 -080064 for other_board in FindCandidateBoards():
David James4a697fd2012-02-11 15:27:53 -080065 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 Frysinger383367e2014-09-16 15:06:17 -040072 print('Missing portageq wrapper for %s' % flags.board, file=sys.stderr)
David James4a697fd2012-02-11 15:27:53 -080073 sys.exit(1)
74
Mike Frysinger383367e2014-09-16 15:06:17 -040075 print('# Generated by cros_generate_local_binhosts.')
76 print(GenerateBinhostLine(flags.build_root, compatible_boards))