1 # Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Utilities for managing the toolchains in the chroot."""
7 from __future__ import print_function
13 from chromite.cbuildbot import constants
14 from chromite.lib import cros_build_lib
15 from chromite.lib import gs
16 from chromite.lib import osutils
17 from chromite.lib import portage_util
19 if cros_build_lib.IsInsideChroot():
20 # Only import portage after we've checked that we're inside the chroot.
21 # Outside may not have portage, in which case the above may not happen.
22 # We'll check in main() if the operation needs portage.
24 # pylint: disable=F0401
29 """Returns compiler tuple for the host system."""
30 # pylint: disable=E1101
31 return portage.settings['CHOST']
34 def GetTuplesForOverlays(overlays):
35 """Returns a set of tuples for a given set of overlays."""
43 for overlay in overlays:
44 config = os.path.join(overlay, 'toolchain.conf')
45 if os.path.exists(config):
49 for line in osutils.ReadFile(config).splitlines():
50 # Split by hash sign so that comments are ignored.
51 # Then split the line to get the tuple and its options.
52 line = line.split('#', 1)[0].split()
58 if target not in targets:
59 targets[target] = copy.copy(default_settings)
61 targets[target].update(json.loads(' '.join(line[1:])))
62 if targets[target]['default']:
65 # If the user has not explicitly marked a toolchain as default,
66 # automatically select the first tuple that we saw in the conf.
67 if not seen_default and first_target:
68 targets[first_target]['default'] = True
73 # Tree interface functions. They help with retrieving data about the current
76 """Get the complete list of targets.
79 The list of cross targets for the current tree
81 targets = GetToolchainsForBoard('all')
83 # Remove the host target as that is not a cross-target. Replace with 'host'.
84 del targets[GetHostTuple()]
88 def GetToolchainsForBoard(board, buildroot=constants.SOURCE_ROOT):
89 """Get a list of toolchain tuples for a given board name
92 The list of toolchain tuples for the given board
94 overlays = portage_util.FindOverlays(
95 constants.BOTH_OVERLAYS, None if board in ('all', 'sdk') else board,
97 toolchains = GetTuplesForOverlays(overlays)
99 toolchains = FilterToolchains(toolchains, 'sdk', True)
103 def FilterToolchains(targets, key, value):
104 """Filter out targets based on their attributes.
107 targets: dict of toolchains
108 key: metadata to examine
109 value: expected value for metadata
112 dict where all targets whose metadata |key| does not match |value|
115 return dict((k, v) for k, v in targets.iteritems() if v[key] == value)
118 def GetSdkURL(for_gsutil=False, suburl=''):
119 """Construct a Google Storage URL for accessing SDK related archives
122 for_gsutil: Do you want a URL for passing to `gsutil`?
123 suburl: A url fragment to tack onto the end
126 The fully constructed URL
128 return gs.GetGsURL(constants.SDK_GS_BUCKET, for_gsutil=for_gsutil,