blob: 810d34af8c182ca1ae3f90aba5c4b997e0312e93 [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
17from chromite.buildbot import cbuildbot_config
Brian Harring1b8c4c82012-05-29 23:03:04 -070018from chromite.lib import cros_build_lib
David James4a697fd2012-02-11 15:27:53 -080019
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."""
37 local_binhosts = " ".join([
38 "file://localhost" + os.path.join(build_root, x, "packages")
39 for x in sorted(compatible_boards)])
40 return "LOCAL_BINHOST='%s'" % local_binhosts
41
42
43def main(argv):
44 parser = optparse.OptionParser(usage="USAGE: ./%prog --board=board [options]")
45
46 parser.add_option("--build_root", default="/build",
47 dest="build_root",
48 help="Location of boards (normally /build)")
49 parser.add_option("--board", default=None,
50 dest="board",
51 help="Board name (required).")
52
53 flags, remaining_arguments = parser.parse_args(argv)
54
55 if remaining_arguments or not flags.board:
56 parser.print_help()
57 sys.exit(1)
58
59 by_compatibility = collections.defaultdict(set)
60 compatible_boards = None
David James7c3a7272014-02-06 18:24:35 -080061 for other_board in FindCandidateBoards():
David James4a697fd2012-02-11 15:27:53 -080062 compat_id = SummarizeCompatibility(other_board)
63 if other_board == flags.board:
64 compatible_boards = by_compatibility[compat_id]
65 else:
66 by_compatibility[compat_id].add(other_board)
67
68 if compatible_boards is None:
David James43e14f42012-12-14 13:44:16 -080069 print >> sys.stderr, "Missing portageq wrapper for %s" % flags.board
David James4a697fd2012-02-11 15:27:53 -080070 sys.exit(1)
71
72 # If a non-generic prebuilt is available, ignore the generic ones,
73 # since the non-generic ones will be better matches.
74 compatible_specific_boards = \
75 compatible_boards - set(cbuildbot_config.generic_boards)
76 if compatible_specific_boards:
77 compatible_boards = compatible_specific_boards
78
79 print "# Generated by cros_generate_local_binhosts."
80 print GenerateBinhostLine(flags.build_root, compatible_boards)