Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2012 The ChromiumOS Authors |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 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 |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 13 | import os |
| 14 | import sys |
| 15 | |
Mike Frysinger | a67f8d9 | 2016-09-01 00:40:01 -0400 | [diff] [blame] | 16 | from chromite.lib import commandline |
Alex Klein | f78f307 | 2018-09-05 09:49:05 -0600 | [diff] [blame] | 17 | from chromite.lib import portage_util |
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(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 21 | """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 James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 26 | |
| 27 | |
| 28 | def SummarizeCompatibility(board): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 29 | """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 James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 32 | |
| 33 | |
| 34 | def GenerateBinhostLine(build_root, compatible_boards): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 35 | """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 James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 44 | |
| 45 | |
Mike Frysinger | a67f8d9 | 2016-09-01 00:40:01 -0400 | [diff] [blame] | 46 | def GetParser(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 47 | """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 Frysinger | a67f8d9 | 2016-09-01 00:40:01 -0400 | [diff] [blame] | 56 | |
| 57 | |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 58 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 59 | parser = GetParser() |
| 60 | flags = parser.parse_args(argv) |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 61 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | 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 James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 70 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 71 | if compatible_boards is None: |
| 72 | print("Missing portageq wrapper for %s" % flags.board, file=sys.stderr) |
| 73 | sys.exit(1) |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 74 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 75 | print("# Generated by cros_generate_local_binhosts.") |
| 76 | print(GenerateBinhostLine(flags.build_root, compatible_boards)) |