blob: bbd7d46eec30a235c602eafe9c0abf585bbce0d6 [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
11import collections
12import glob
13import optparse
14import os
15import sys
16
Brian Harring1b8c4c82012-05-29 23:03:04 -070017from chromite.lib import cros_build_lib
David James4a697fd2012-02-11 15:27:53 -080018
Mike Frysingercc838832014-05-24 13:10:30 -040019
David James7c3a7272014-02-06 18:24:35 -080020def FindCandidateBoards():
David James4a697fd2012-02-11 15:27:53 -080021 """Find candidate local boards to grab prebuilts from."""
David James4a697fd2012-02-11 15:27:53 -080022 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 James7c3a7272014-02-06 18:24:35 -080025 yield path.replace(portageq_prefix, "")
David James4a697fd2012-02-11 15:27:53 -080026
27
28def SummarizeCompatibility(board):
29 """Returns a string that will be the same for compatible boards."""
30 cmd = ["portageq-%s" % board, "envvar", "ARCH", "CFLAGS"]
Brian Harring1b8c4c82012-05-29 23:03:04 -070031 return cros_build_lib.RunCommand(cmd, redirect_stdout=True,
32 print_cmd=False).output.rstrip()
David James4a697fd2012-02-11 15:27:53 -080033
34
35def GenerateBinhostLine(build_root, compatible_boards):
36 """Generate a binhost line pulling binaries from the specified boards."""
David James602934f2014-03-31 10:10:11 -070037 # TODO(davidjames): Prioritize binhosts with more matching use flags.
David James4a697fd2012-02-11 15:27:53 -080038 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
44def 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 James7c3a7272014-02-06 18:24:35 -080062 for other_board in FindCandidateBoards():
David James4a697fd2012-02-11 15:27:53 -080063 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 James43e14f42012-12-14 13:44:16 -080070 print >> sys.stderr, "Missing portageq wrapper for %s" % flags.board
David James4a697fd2012-02-11 15:27:53 -080071 sys.exit(1)
72
David James4a697fd2012-02-11 15:27:53 -080073 print "# Generated by cros_generate_local_binhosts."
74 print GenerateBinhostLine(flags.build_root, compatible_boards)