blob: fe023ab965e29bc663af5468670a495b0dce09c2 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2012 The ChromiumOS Authors
David James4a697fd2012-02-11 15:27:53 -08002# 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
David James4a697fd2012-02-11 15:27:53 -080013import os
14import sys
15
Mike Frysingera67f8d92016-09-01 00:40:01 -040016from chromite.lib import commandline
Alex Kleinf78f3072018-09-05 09:49:05 -060017from chromite.lib import portage_util
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():
Alex Klein1699fab2022-09-08 08:46:06 -060021 """Find candidate local boards to grab prebuilts from."""
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.
25 yield path.replace(portageq_prefix, "")
David James4a697fd2012-02-11 15:27:53 -080026
27
28def SummarizeCompatibility(board):
Alex Klein1699fab2022-09-08 08:46:06 -060029 """Returns a string that will be the same for compatible boards."""
30 result = portage_util.PortageqEnvvars(["ARCH", "CFLAGS"], board=board)
31 return "%s %s" % (result["ARCH"], result["CFLAGS"])
David James4a697fd2012-02-11 15:27:53 -080032
33
34def GenerateBinhostLine(build_root, compatible_boards):
Alex Klein1699fab2022-09-08 08:46:06 -060035 """Generate a binhost line pulling binaries from the specified boards."""
36 # TODO(davidjames): Prioritize binhosts with more matching use flags.
37 local_binhosts = " ".join(
38 [
39 "file://localhost" + os.path.join(build_root, x, "packages")
40 for x in sorted(compatible_boards)
41 ]
42 )
43 return "LOCAL_BINHOST='%s'" % local_binhosts
David James4a697fd2012-02-11 15:27:53 -080044
45
Mike Frysingera67f8d92016-09-01 00:40:01 -040046def GetParser():
Alex Klein1699fab2022-09-08 08:46:06 -060047 """Return a command line parser."""
48 parser = commandline.ArgumentParser(description=__doc__)
49 parser.add_argument(
50 "--build_root",
51 default="/build",
52 help="Location of boards (normally %(default)s)",
53 )
54 parser.add_argument("--board", required=True, help="Board name")
55 return parser
Mike Frysingera67f8d92016-09-01 00:40:01 -040056
57
David James4a697fd2012-02-11 15:27:53 -080058def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060059 parser = GetParser()
60 flags = parser.parse_args(argv)
David James4a697fd2012-02-11 15:27:53 -080061
Alex Klein1699fab2022-09-08 08:46:06 -060062 by_compatibility = collections.defaultdict(set)
63 compatible_boards = None
64 for other_board in FindCandidateBoards():
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)
David James4a697fd2012-02-11 15:27:53 -080070
Alex Klein1699fab2022-09-08 08:46:06 -060071 if compatible_boards is None:
72 print("Missing portageq wrapper for %s" % flags.board, file=sys.stderr)
73 sys.exit(1)
David James4a697fd2012-02-11 15:27:53 -080074
Alex Klein1699fab2022-09-08 08:46:06 -060075 print("# Generated by cros_generate_local_binhosts.")
76 print(GenerateBinhostLine(flags.build_root, compatible_boards))