blob: 8238da959cce99693ba79f252815aa80c8e7fe85 [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
Brian Harring1b8c4c82012-05-29 23:03:04 -070020from chromite.lib import cros_build_lib
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."""
33 cmd = ["portageq-%s" % board, "envvar", "ARCH", "CFLAGS"]
Yunlian Jiang0aad8ad2015-06-11 14:28:12 -070034 summary = cros_build_lib.RunCommand(cmd, redirect_stdout=True,
35 print_cmd=False).output.rstrip()
36 # We will add -clang-syntax to falco and nyan board. So we need to
37 # filter out -clang-syntax to make the flags from PFQ are the same as
38 # the release-board. See crbug.com/499115
39 # TODO(yunlian): Remove this when all the boards are build with -clang-syntax
40 return summary.replace(" -clang-syntax", "")
David James4a697fd2012-02-11 15:27:53 -080041
42
43def GenerateBinhostLine(build_root, compatible_boards):
44 """Generate a binhost line pulling binaries from the specified boards."""
David James602934f2014-03-31 10:10:11 -070045 # TODO(davidjames): Prioritize binhosts with more matching use flags.
David James4a697fd2012-02-11 15:27:53 -080046 local_binhosts = " ".join([
47 "file://localhost" + os.path.join(build_root, x, "packages")
48 for x in sorted(compatible_boards)])
49 return "LOCAL_BINHOST='%s'" % local_binhosts
50
51
Mike Frysingera67f8d92016-09-01 00:40:01 -040052def GetParser():
53 """Return a command line parser."""
54 parser = commandline.ArgumentParser(description=__doc__)
55 parser.add_argument('--build_root', default='/build',
56 help='Location of boards (normally %(default)s)')
57 parser.add_argument('--board', required=True,
58 help='Board name')
59 return parser
60
61
David James4a697fd2012-02-11 15:27:53 -080062def main(argv):
Mike Frysingera67f8d92016-09-01 00:40:01 -040063 parser = GetParser()
64 flags = parser.parse_args(argv)
David James4a697fd2012-02-11 15:27:53 -080065
66 by_compatibility = collections.defaultdict(set)
67 compatible_boards = None
David James7c3a7272014-02-06 18:24:35 -080068 for other_board in FindCandidateBoards():
David James4a697fd2012-02-11 15:27:53 -080069 compat_id = SummarizeCompatibility(other_board)
70 if other_board == flags.board:
71 compatible_boards = by_compatibility[compat_id]
72 else:
73 by_compatibility[compat_id].add(other_board)
74
75 if compatible_boards is None:
Mike Frysinger383367e2014-09-16 15:06:17 -040076 print('Missing portageq wrapper for %s' % flags.board, file=sys.stderr)
David James4a697fd2012-02-11 15:27:53 -080077 sys.exit(1)
78
Mike Frysinger383367e2014-09-16 15:06:17 -040079 print('# Generated by cros_generate_local_binhosts.')
80 print(GenerateBinhostLine(flags.build_root, compatible_boards))