Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
David James | 7c3a727 | 2014-02-06 18:24:35 -0800 | [diff] [blame] | 6 | """Script for calculating compatible binhosts. |
| 7 | |
| 8 | 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] | 9 | other compatible boards in this buildroot. |
| 10 | """ |
| 11 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 12 | from __future__ import print_function |
| 13 | |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 14 | import collections |
| 15 | import glob |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 16 | import os |
| 17 | import sys |
| 18 | |
Mike Frysinger | a67f8d9 | 2016-09-01 00:40:01 -0400 | [diff] [blame] | 19 | from chromite.lib import commandline |
Alex Klein | f78f307 | 2018-09-05 09:49:05 -0600 | [diff] [blame^] | 20 | from chromite.lib import portage_util |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 21 | |
Mike Frysinger | cc83883 | 2014-05-24 13:10:30 -0400 | [diff] [blame] | 22 | |
David James | 7c3a727 | 2014-02-06 18:24:35 -0800 | [diff] [blame] | 23 | def FindCandidateBoards(): |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 24 | """Find candidate local boards to grab prebuilts from.""" |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 25 | portageq_prefix = "/usr/local/bin/portageq-" |
| 26 | for path in sorted(glob.glob("%s*" % portageq_prefix)): |
| 27 | # Strip off the portageq prefix, leaving only the board. |
David James | 7c3a727 | 2014-02-06 18:24:35 -0800 | [diff] [blame] | 28 | yield path.replace(portageq_prefix, "") |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 29 | |
| 30 | |
| 31 | def SummarizeCompatibility(board): |
| 32 | """Returns a string that will be the same for compatible boards.""" |
Alex Klein | f78f307 | 2018-09-05 09:49:05 -0600 | [diff] [blame^] | 33 | result = portage_util.PortageqEnvvars(['ARCH', 'CFLAGS'], board=board) |
| 34 | return '%s %s' % (result['ARCH'], result['CFLAGS']) |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 35 | |
| 36 | |
| 37 | def GenerateBinhostLine(build_root, compatible_boards): |
| 38 | """Generate a binhost line pulling binaries from the specified boards.""" |
David James | 602934f | 2014-03-31 10:10:11 -0700 | [diff] [blame] | 39 | # TODO(davidjames): Prioritize binhosts with more matching use flags. |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 40 | 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 | |
Mike Frysinger | a67f8d9 | 2016-09-01 00:40:01 -0400 | [diff] [blame] | 46 | def GetParser(): |
| 47 | """Return a command line parser.""" |
| 48 | parser = commandline.ArgumentParser(description=__doc__) |
| 49 | parser.add_argument('--build_root', default='/build', |
| 50 | help='Location of boards (normally %(default)s)') |
| 51 | parser.add_argument('--board', required=True, |
| 52 | help='Board name') |
| 53 | return parser |
| 54 | |
| 55 | |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 56 | def main(argv): |
Mike Frysinger | a67f8d9 | 2016-09-01 00:40:01 -0400 | [diff] [blame] | 57 | parser = GetParser() |
| 58 | flags = parser.parse_args(argv) |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 59 | |
| 60 | by_compatibility = collections.defaultdict(set) |
| 61 | compatible_boards = None |
David James | 7c3a727 | 2014-02-06 18:24:35 -0800 | [diff] [blame] | 62 | for other_board in FindCandidateBoards(): |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 63 | 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: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 70 | print('Missing portageq wrapper for %s' % flags.board, file=sys.stderr) |
David James | 4a697fd | 2012-02-11 15:27:53 -0800 | [diff] [blame] | 71 | sys.exit(1) |
| 72 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 73 | print('# Generated by cros_generate_local_binhosts.') |
| 74 | print(GenerateBinhostLine(flags.build_root, compatible_boards)) |