blob: eeaccf857a8c5e5db8555f29aeb6febe98596aa3 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
David James4a697fd2012-02-11 15:27:53 -08002# 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 James7c3a7272014-02-06 18:24:35 -08006"""Script for calculating compatible binhosts.
7
8Generates a file that sets the specified board's binhosts to include all of the
David James4a697fd2012-02-11 15:27:53 -08009other compatible boards in this buildroot.
10"""
11
Mike Frysinger383367e2014-09-16 15:06:17 -040012from __future__ import print_function
13
David James4a697fd2012-02-11 15:27:53 -080014import collections
15import glob
David James4a697fd2012-02-11 15:27:53 -080016import os
17import sys
18
Mike Frysingera67f8d92016-09-01 00:40:01 -040019from chromite.lib import commandline
Alex Kleinf78f3072018-09-05 09:49:05 -060020from chromite.lib import portage_util
David James4a697fd2012-02-11 15:27:53 -080021
Mike Frysingercc838832014-05-24 13:10:30 -040022
David James7c3a7272014-02-06 18:24:35 -080023def FindCandidateBoards():
David James4a697fd2012-02-11 15:27:53 -080024 """Find candidate local boards to grab prebuilts from."""
David James4a697fd2012-02-11 15:27:53 -080025 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 James7c3a7272014-02-06 18:24:35 -080028 yield path.replace(portageq_prefix, "")
David James4a697fd2012-02-11 15:27:53 -080029
30
31def SummarizeCompatibility(board):
32 """Returns a string that will be the same for compatible boards."""
Alex Kleinf78f3072018-09-05 09:49:05 -060033 result = portage_util.PortageqEnvvars(['ARCH', 'CFLAGS'], board=board)
34 return '%s %s' % (result['ARCH'], result['CFLAGS'])
David James4a697fd2012-02-11 15:27:53 -080035
36
37def GenerateBinhostLine(build_root, compatible_boards):
38 """Generate a binhost line pulling binaries from the specified boards."""
David James602934f2014-03-31 10:10:11 -070039 # TODO(davidjames): Prioritize binhosts with more matching use flags.
David James4a697fd2012-02-11 15:27:53 -080040 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 Frysingera67f8d92016-09-01 00:40:01 -040046def 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 James4a697fd2012-02-11 15:27:53 -080056def main(argv):
Mike Frysingera67f8d92016-09-01 00:40:01 -040057 parser = GetParser()
58 flags = parser.parse_args(argv)
David James4a697fd2012-02-11 15:27:53 -080059
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:
Mike Frysinger383367e2014-09-16 15:06:17 -040070 print('Missing portageq wrapper for %s' % flags.board, file=sys.stderr)
David James4a697fd2012-02-11 15:27:53 -080071 sys.exit(1)
72
Mike Frysinger383367e2014-09-16 15:06:17 -040073 print('# Generated by cros_generate_local_binhosts.')
74 print(GenerateBinhostLine(flags.build_root, compatible_boards))