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."""
11 from chromite.buildbot import constants
12 from chromite.buildbot import portage_utilities
13 from chromite.lib import cros_build_lib
14 from chromite.lib import gs
15 from chromite.lib import osutils
17 if cros_build_lib.IsInsideChroot():
18 # Only import portage after we've checked that we're inside the chroot.
19 # Outside may not have portage, in which case the above may not happen.
20 # We'll check in main() if the operation needs portage.
22 # pylint: disable=F0401
27 """Returns compiler tuple for the host system."""
28 # pylint: disable=E1101
29 return portage.settings['CHOST']
32 def GetTuplesForOverlays(overlays):
33 """Returns a set of tuples for a given set of overlays."""
41 for overlay in overlays:
42 config = os.path.join(overlay, 'toolchain.conf')
43 if os.path.exists(config):
47 for line in osutils.ReadFile(config).splitlines():
48 # Split by hash sign so that comments are ignored.
49 # Then split the line to get the tuple and its options.
50 line = line.split('#', 1)[0].split()
56 if target not in targets:
57 targets[target] = copy.copy(default_settings)
59 targets[target].update(json.loads(' '.join(line[1:])))
60 if targets[target]['default']:
63 # If the user has not explicitly marked a toolchain as default,
64 # automatically select the first tuple that we saw in the conf.
65 if not seen_default and first_target:
66 targets[first_target]['default'] = True
71 # Tree interface functions. They help with retrieving data about the current
74 """Get the complete list of targets.
77 The list of cross targets for the current tree
79 targets = GetToolchainsForBoard('all')
81 # Remove the host target as that is not a cross-target. Replace with 'host'.
82 del targets[GetHostTuple()]
86 def GetToolchainsForBoard(board, buildroot=constants.SOURCE_ROOT):
87 """Get a list of toolchain tuples for a given board name
90 The list of toolchain tuples for the given board
92 overlays = portage_utilities.FindOverlays(
93 constants.BOTH_OVERLAYS, None if board in ('all', 'sdk') else board,
95 toolchains = GetTuplesForOverlays(overlays)
97 toolchains = FilterToolchains(toolchains, 'sdk', True)
101 def FilterToolchains(targets, key, value):
102 """Filter out targets based on their attributes.
105 targets: dict of toolchains
106 key: metadata to examine
107 value: expected value for metadata
110 dict where all targets whose metadata |key| does not match |value|
113 return dict((k, v) for k, v in targets.iteritems() if v[key] == value)
116 def GetSdkURL(for_gsutil=False, suburl=''):
117 """Construct a Google Storage URL for accessing SDK related archives
120 for_gsutil: Do you want a URL for passing to `gsutil`?
121 suburl: A url fragment to tack onto the end
124 The fully constructed URL
126 return gs.GetGsURL(constants.SDK_GS_BUCKET, for_gsutil=for_gsutil,