Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 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. |
| 4 | |
Mike Frysinger | 750c5f5 | 2014-09-16 16:16:57 -0400 | [diff] [blame] | 5 | """This script manages the installed toolchains in the chroot.""" |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 6 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 9 | import copy |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 10 | import glob |
Mike Frysinger | 7ccee99 | 2012-06-01 21:27:59 -0400 | [diff] [blame] | 11 | import json |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 12 | import os |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 13 | |
Don Garrett | 88b8d78 | 2014-05-13 17:30:55 -0700 | [diff] [blame] | 14 | from chromite.cbuildbot import constants |
Mike Frysinger | 506e75f | 2012-12-17 14:21:13 -0500 | [diff] [blame] | 15 | from chromite.lib import commandline |
Brian Harring | 503f3ab | 2012-03-09 21:39:41 -0800 | [diff] [blame] | 16 | from chromite.lib import cros_build_lib |
Ralph Nathan | 0304728 | 2015-03-23 11:09:32 -0700 | [diff] [blame] | 17 | from chromite.lib import cros_logging as logging |
Brian Harring | af019fb | 2012-05-10 15:06:13 -0700 | [diff] [blame] | 18 | from chromite.lib import osutils |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 19 | from chromite.lib import parallel |
David James | 27ac4ae | 2012-12-03 23:16:15 -0800 | [diff] [blame] | 20 | from chromite.lib import toolchain |
Bertrand SIMONNET | 3c77bc9 | 2015-03-20 14:28:54 -0700 | [diff] [blame] | 21 | from chromite.lib import workspace_lib |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 22 | |
| 23 | # Needs to be after chromite imports. |
| 24 | import lddtree |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 25 | |
Mike Frysinger | 3159600 | 2012-12-03 23:54:24 -0500 | [diff] [blame] | 26 | if cros_build_lib.IsInsideChroot(): |
| 27 | # Only import portage after we've checked that we're inside the chroot. |
| 28 | # Outside may not have portage, in which case the above may not happen. |
| 29 | # We'll check in main() if the operation needs portage. |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 30 | # pylint: disable=F0401 |
Mike Frysinger | 3159600 | 2012-12-03 23:54:24 -0500 | [diff] [blame] | 31 | import portage |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 32 | |
| 33 | |
Matt Tennant | f1e3097 | 2012-03-02 16:30:07 -0800 | [diff] [blame] | 34 | EMERGE_CMD = os.path.join(constants.CHROMITE_BIN_DIR, 'parallel_emerge') |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 35 | PACKAGE_STABLE = '[stable]' |
| 36 | PACKAGE_NONE = '[none]' |
| 37 | SRC_ROOT = os.path.realpath(constants.SOURCE_ROOT) |
Zdenek Behan | 4eb6fd2 | 2012-03-12 17:00:56 +0100 | [diff] [blame] | 38 | |
| 39 | CHROMIUMOS_OVERLAY = '/usr/local/portage/chromiumos' |
| 40 | STABLE_OVERLAY = '/usr/local/portage/stable' |
| 41 | CROSSDEV_OVERLAY = '/usr/local/portage/crossdev' |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 42 | |
| 43 | |
| 44 | # TODO: The versions are stored here very much like in setup_board. |
| 45 | # The goal for future is to differentiate these using a config file. |
| 46 | # This is done essentially by messing with GetDesiredPackageVersions() |
| 47 | DEFAULT_VERSION = PACKAGE_STABLE |
| 48 | DEFAULT_TARGET_VERSION_MAP = { |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 49 | } |
| 50 | TARGET_VERSION_MAP = { |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 51 | 'host' : { |
| 52 | 'gdb' : PACKAGE_NONE, |
| 53 | }, |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 54 | } |
| 55 | # Overrides for {gcc,binutils}-config, pick a package with particular suffix. |
| 56 | CONFIG_TARGET_SUFFIXES = { |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 57 | 'binutils' : { |
Han Shen | 43b8442 | 2015-02-19 11:38:13 -0800 | [diff] [blame] | 58 | 'armv7a-cros-linux-gnueabi': '-gold', |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 59 | 'i686-pc-linux-gnu' : '-gold', |
| 60 | 'x86_64-cros-linux-gnu' : '-gold', |
| 61 | }, |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 62 | } |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 63 | # Global per-run cache that will be filled ondemand in by GetPackageMap() |
| 64 | # function as needed. |
| 65 | target_version_map = { |
| 66 | } |
| 67 | |
| 68 | |
David James | 66a09c4 | 2012-11-05 13:31:38 -0800 | [diff] [blame] | 69 | class Crossdev(object): |
| 70 | """Class for interacting with crossdev and caching its output.""" |
| 71 | |
| 72 | _CACHE_FILE = os.path.join(CROSSDEV_OVERLAY, '.configured.json') |
| 73 | _CACHE = {} |
| 74 | |
| 75 | @classmethod |
| 76 | def Load(cls, reconfig): |
| 77 | """Load crossdev cache from disk.""" |
David James | 90239b9 | 2012-11-05 15:31:34 -0800 | [diff] [blame] | 78 | crossdev_version = GetStablePackageVersion('sys-devel/crossdev', True) |
| 79 | cls._CACHE = {'crossdev_version': crossdev_version} |
David James | 66a09c4 | 2012-11-05 13:31:38 -0800 | [diff] [blame] | 80 | if os.path.exists(cls._CACHE_FILE) and not reconfig: |
| 81 | with open(cls._CACHE_FILE) as f: |
| 82 | data = json.load(f) |
David James | 90239b9 | 2012-11-05 15:31:34 -0800 | [diff] [blame] | 83 | if crossdev_version == data.get('crossdev_version'): |
David James | 66a09c4 | 2012-11-05 13:31:38 -0800 | [diff] [blame] | 84 | cls._CACHE = data |
| 85 | |
| 86 | @classmethod |
| 87 | def Save(cls): |
| 88 | """Store crossdev cache on disk.""" |
| 89 | # Save the cache from the successful run. |
| 90 | with open(cls._CACHE_FILE, 'w') as f: |
| 91 | json.dump(cls._CACHE, f) |
| 92 | |
| 93 | @classmethod |
| 94 | def GetConfig(cls, target): |
| 95 | """Returns a map of crossdev provided variables about a tuple.""" |
| 96 | CACHE_ATTR = '_target_tuple_map' |
| 97 | |
| 98 | val = cls._CACHE.setdefault(CACHE_ATTR, {}) |
| 99 | if not target in val: |
| 100 | # Find out the crossdev tuple. |
| 101 | target_tuple = target |
| 102 | if target == 'host': |
David James | 27ac4ae | 2012-12-03 23:16:15 -0800 | [diff] [blame] | 103 | target_tuple = toolchain.GetHostTuple() |
David James | 66a09c4 | 2012-11-05 13:31:38 -0800 | [diff] [blame] | 104 | # Catch output of crossdev. |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 105 | out = cros_build_lib.RunCommand( |
| 106 | ['crossdev', '--show-target-cfg', '--ex-gdb', target_tuple], |
| 107 | print_cmd=False, redirect_stdout=True).output.splitlines() |
David James | 66a09c4 | 2012-11-05 13:31:38 -0800 | [diff] [blame] | 108 | # List of tuples split at the first '=', converted into dict. |
| 109 | val[target] = dict([x.split('=', 1) for x in out]) |
| 110 | return val[target] |
| 111 | |
| 112 | @classmethod |
| 113 | def UpdateTargets(cls, targets, usepkg, config_only=False): |
| 114 | """Calls crossdev to initialize a cross target. |
| 115 | |
| 116 | Args: |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 117 | targets: The list of targets to initialize using crossdev. |
| 118 | usepkg: Copies the commandline opts. |
| 119 | config_only: Just update. |
David James | 66a09c4 | 2012-11-05 13:31:38 -0800 | [diff] [blame] | 120 | """ |
| 121 | configured_targets = cls._CACHE.setdefault('configured_targets', []) |
| 122 | |
| 123 | cmdbase = ['crossdev', '--show-fail-log'] |
| 124 | cmdbase.extend(['--env', 'FEATURES=splitdebug']) |
| 125 | # Pick stable by default, and override as necessary. |
| 126 | cmdbase.extend(['-P', '--oneshot']) |
| 127 | if usepkg: |
| 128 | cmdbase.extend(['-P', '--getbinpkg', |
| 129 | '-P', '--usepkgonly', |
| 130 | '--without-headers']) |
| 131 | |
| 132 | overlays = '%s %s' % (CHROMIUMOS_OVERLAY, STABLE_OVERLAY) |
| 133 | cmdbase.extend(['--overlays', overlays]) |
| 134 | cmdbase.extend(['--ov-output', CROSSDEV_OVERLAY]) |
| 135 | |
| 136 | for target in targets: |
| 137 | if config_only and target in configured_targets: |
| 138 | continue |
| 139 | |
| 140 | cmd = cmdbase + ['-t', target] |
| 141 | |
| 142 | for pkg in GetTargetPackages(target): |
| 143 | if pkg == 'gdb': |
| 144 | # Gdb does not have selectable versions. |
| 145 | cmd.append('--ex-gdb') |
| 146 | continue |
| 147 | # The first of the desired versions is the "primary" one. |
| 148 | version = GetDesiredPackageVersions(target, pkg)[0] |
| 149 | cmd.extend(['--%s' % pkg, version]) |
| 150 | |
| 151 | cmd.extend(targets[target]['crossdev'].split()) |
| 152 | if config_only: |
| 153 | # In this case we want to just quietly reinit |
| 154 | cmd.append('--init-target') |
| 155 | cros_build_lib.RunCommand(cmd, print_cmd=False, redirect_stdout=True) |
| 156 | else: |
| 157 | cros_build_lib.RunCommand(cmd) |
| 158 | |
| 159 | configured_targets.append(target) |
| 160 | |
Zdenek Behan | 4eb6fd2 | 2012-03-12 17:00:56 +0100 | [diff] [blame] | 161 | |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 162 | def GetPackageMap(target): |
| 163 | """Compiles a package map for the given target from the constants. |
| 164 | |
| 165 | Uses a cache in target_version_map, that is dynamically filled in as needed, |
| 166 | since here everything is static data and the structuring is for ease of |
| 167 | configurability only. |
| 168 | |
Mike Frysinger | dc649ae | 2014-11-26 19:29:24 -0500 | [diff] [blame] | 169 | Args: |
| 170 | target: The target for which to return a version map |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 171 | |
Mike Frysinger | dc649ae | 2014-11-26 19:29:24 -0500 | [diff] [blame] | 172 | Returns: |
| 173 | A map between packages and desired versions in internal format |
| 174 | (using the PACKAGE_* constants) |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 175 | """ |
| 176 | if target in target_version_map: |
| 177 | return target_version_map[target] |
| 178 | |
| 179 | # Start from copy of the global defaults. |
| 180 | result = copy.copy(DEFAULT_TARGET_VERSION_MAP) |
| 181 | |
Zdenek Behan | f4d18a0 | 2012-03-22 15:45:05 +0100 | [diff] [blame] | 182 | for pkg in GetTargetPackages(target): |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 183 | # prefer any specific overrides |
| 184 | if pkg in TARGET_VERSION_MAP.get(target, {}): |
| 185 | result[pkg] = TARGET_VERSION_MAP[target][pkg] |
| 186 | else: |
| 187 | # finally, if not already set, set a sane default |
| 188 | result.setdefault(pkg, DEFAULT_VERSION) |
| 189 | target_version_map[target] = result |
| 190 | return result |
| 191 | |
| 192 | |
Zdenek Behan | f4d18a0 | 2012-03-22 15:45:05 +0100 | [diff] [blame] | 193 | def GetTargetPackages(target): |
| 194 | """Returns a list of packages for a given target.""" |
David James | 66a09c4 | 2012-11-05 13:31:38 -0800 | [diff] [blame] | 195 | conf = Crossdev.GetConfig(target) |
Zdenek Behan | f4d18a0 | 2012-03-22 15:45:05 +0100 | [diff] [blame] | 196 | # Undesired packages are denoted by empty ${pkg}_pn variable. |
| 197 | return [x for x in conf['crosspkgs'].strip("'").split() if conf[x+'_pn']] |
| 198 | |
| 199 | |
Zdenek Behan | 4eb6fd2 | 2012-03-12 17:00:56 +0100 | [diff] [blame] | 200 | # Portage helper functions: |
| 201 | def GetPortagePackage(target, package): |
| 202 | """Returns a package name for the given target.""" |
David James | 66a09c4 | 2012-11-05 13:31:38 -0800 | [diff] [blame] | 203 | conf = Crossdev.GetConfig(target) |
Zdenek Behan | 4eb6fd2 | 2012-03-12 17:00:56 +0100 | [diff] [blame] | 204 | # Portage category: |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 205 | if target == 'host': |
Zdenek Behan | 4eb6fd2 | 2012-03-12 17:00:56 +0100 | [diff] [blame] | 206 | category = conf[package + '_category'] |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 207 | else: |
Zdenek Behan | 4eb6fd2 | 2012-03-12 17:00:56 +0100 | [diff] [blame] | 208 | category = conf['category'] |
| 209 | # Portage package: |
| 210 | pn = conf[package + '_pn'] |
| 211 | # Final package name: |
Mike Frysinger | 422faf4 | 2014-11-12 23:16:48 -0500 | [diff] [blame] | 212 | assert category |
| 213 | assert pn |
Zdenek Behan | 4eb6fd2 | 2012-03-12 17:00:56 +0100 | [diff] [blame] | 214 | return '%s/%s' % (category, pn) |
| 215 | |
| 216 | |
Zdenek Behan | 4eb6fd2 | 2012-03-12 17:00:56 +0100 | [diff] [blame] | 217 | def IsPackageDisabled(target, package): |
| 218 | """Returns if the given package is not used for the target.""" |
| 219 | return GetDesiredPackageVersions(target, package) == [PACKAGE_NONE] |
| 220 | |
Liam McLoughlin | f54a078 | 2012-05-17 23:36:52 +0100 | [diff] [blame] | 221 | |
David James | 66a09c4 | 2012-11-05 13:31:38 -0800 | [diff] [blame] | 222 | def GetInstalledPackageVersions(atom): |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 223 | """Extracts the list of current versions of a target, package pair. |
| 224 | |
Mike Frysinger | dc649ae | 2014-11-26 19:29:24 -0500 | [diff] [blame] | 225 | Args: |
| 226 | atom: The atom to operate on (e.g. sys-devel/gcc) |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 227 | |
Mike Frysinger | dc649ae | 2014-11-26 19:29:24 -0500 | [diff] [blame] | 228 | Returns: |
| 229 | The list of versions of the package currently installed. |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 230 | """ |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 231 | versions = [] |
Mike Frysinger | 506e75f | 2012-12-17 14:21:13 -0500 | [diff] [blame] | 232 | # pylint: disable=E1101 |
David James | 90239b9 | 2012-11-05 15:31:34 -0800 | [diff] [blame] | 233 | for pkg in portage.db['/']['vartree'].dbapi.match(atom, use_cache=0): |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 234 | version = portage.versions.cpv_getversion(pkg) |
| 235 | versions.append(version) |
| 236 | return versions |
| 237 | |
| 238 | |
David James | 90239b9 | 2012-11-05 15:31:34 -0800 | [diff] [blame] | 239 | def GetStablePackageVersion(atom, installed): |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 240 | """Extracts the current stable version for a given package. |
| 241 | |
Mike Frysinger | dc649ae | 2014-11-26 19:29:24 -0500 | [diff] [blame] | 242 | Args: |
| 243 | atom: The target/package to operate on eg. i686-pc-linux-gnu,gcc |
| 244 | installed: Whether we want installed packages or ebuilds |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 245 | |
Mike Frysinger | dc649ae | 2014-11-26 19:29:24 -0500 | [diff] [blame] | 246 | Returns: |
| 247 | A string containing the latest version. |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 248 | """ |
David James | 90239b9 | 2012-11-05 15:31:34 -0800 | [diff] [blame] | 249 | pkgtype = 'vartree' if installed else 'porttree' |
Mike Frysinger | 506e75f | 2012-12-17 14:21:13 -0500 | [diff] [blame] | 250 | # pylint: disable=E1101 |
David James | 90239b9 | 2012-11-05 15:31:34 -0800 | [diff] [blame] | 251 | cpv = portage.best(portage.db['/'][pkgtype].dbapi.match(atom, use_cache=0)) |
| 252 | return portage.versions.cpv_getversion(cpv) if cpv else None |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 253 | |
| 254 | |
Zdenek Behan | 699ddd3 | 2012-04-13 07:14:08 +0200 | [diff] [blame] | 255 | def VersionListToNumeric(target, package, versions, installed): |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 256 | """Resolves keywords in a given version list for a particular package. |
| 257 | |
| 258 | Resolving means replacing PACKAGE_STABLE with the actual number. |
| 259 | |
Mike Frysinger | dc649ae | 2014-11-26 19:29:24 -0500 | [diff] [blame] | 260 | Args: |
| 261 | target: The target to operate on (e.g. i686-pc-linux-gnu) |
| 262 | package: The target/package to operate on (e.g. gcc) |
| 263 | versions: List of versions to resolve |
| 264 | installed: Query installed packages |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 265 | |
Mike Frysinger | dc649ae | 2014-11-26 19:29:24 -0500 | [diff] [blame] | 266 | Returns: |
| 267 | List of purely numeric versions equivalent to argument |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 268 | """ |
| 269 | resolved = [] |
David James | 90239b9 | 2012-11-05 15:31:34 -0800 | [diff] [blame] | 270 | atom = GetPortagePackage(target, package) |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 271 | for version in versions: |
| 272 | if version == PACKAGE_STABLE: |
David James | 90239b9 | 2012-11-05 15:31:34 -0800 | [diff] [blame] | 273 | resolved.append(GetStablePackageVersion(atom, installed)) |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 274 | elif version != PACKAGE_NONE: |
| 275 | resolved.append(version) |
| 276 | return resolved |
| 277 | |
| 278 | |
| 279 | def GetDesiredPackageVersions(target, package): |
| 280 | """Produces the list of desired versions for each target, package pair. |
| 281 | |
| 282 | The first version in the list is implicitly treated as primary, ie. |
| 283 | the version that will be initialized by crossdev and selected. |
| 284 | |
| 285 | If the version is PACKAGE_STABLE, it really means the current version which |
| 286 | is emerged by using the package atom with no particular version key. |
| 287 | Since crossdev unmasks all packages by default, this will actually |
| 288 | mean 'unstable' in most cases. |
| 289 | |
Mike Frysinger | dc649ae | 2014-11-26 19:29:24 -0500 | [diff] [blame] | 290 | Args: |
| 291 | target: The target to operate on (e.g. i686-pc-linux-gnu) |
| 292 | package: The target/package to operate on (e.g. gcc) |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 293 | |
Mike Frysinger | dc649ae | 2014-11-26 19:29:24 -0500 | [diff] [blame] | 294 | Returns: |
| 295 | A list composed of either a version string, PACKAGE_STABLE |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 296 | """ |
| 297 | packagemap = GetPackageMap(target) |
| 298 | |
| 299 | versions = [] |
| 300 | if package in packagemap: |
| 301 | versions.append(packagemap[package]) |
| 302 | |
| 303 | return versions |
| 304 | |
| 305 | |
| 306 | def TargetIsInitialized(target): |
| 307 | """Verifies if the given list of targets has been correctly initialized. |
| 308 | |
| 309 | This determines whether we have to call crossdev while emerging |
| 310 | toolchain packages or can do it using emerge. Emerge is naturally |
| 311 | preferred, because all packages can be updated in a single pass. |
| 312 | |
Mike Frysinger | dc649ae | 2014-11-26 19:29:24 -0500 | [diff] [blame] | 313 | Args: |
| 314 | target: The target to operate on (e.g. i686-pc-linux-gnu) |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 315 | |
Mike Frysinger | dc649ae | 2014-11-26 19:29:24 -0500 | [diff] [blame] | 316 | Returns: |
| 317 | True if |target| is completely initialized, else False |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 318 | """ |
| 319 | # Check if packages for the given target all have a proper version. |
| 320 | try: |
Zdenek Behan | f4d18a0 | 2012-03-22 15:45:05 +0100 | [diff] [blame] | 321 | for package in GetTargetPackages(target): |
David James | 66a09c4 | 2012-11-05 13:31:38 -0800 | [diff] [blame] | 322 | atom = GetPortagePackage(target, package) |
Zdenek Behan | 4eb6fd2 | 2012-03-12 17:00:56 +0100 | [diff] [blame] | 323 | # Do we even want this package && is it initialized? |
David James | 90239b9 | 2012-11-05 15:31:34 -0800 | [diff] [blame] | 324 | if not IsPackageDisabled(target, package) and not ( |
| 325 | GetStablePackageVersion(atom, True) and |
| 326 | GetStablePackageVersion(atom, False)): |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 327 | return False |
| 328 | return True |
| 329 | except cros_build_lib.RunCommandError: |
| 330 | # Fails - The target has likely never been initialized before. |
| 331 | return False |
| 332 | |
| 333 | |
| 334 | def RemovePackageMask(target): |
| 335 | """Removes a package.mask file for the given platform. |
| 336 | |
| 337 | The pre-existing package.mask files can mess with the keywords. |
| 338 | |
Mike Frysinger | dc649ae | 2014-11-26 19:29:24 -0500 | [diff] [blame] | 339 | Args: |
| 340 | target: The target to operate on (e.g. i686-pc-linux-gnu) |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 341 | """ |
| 342 | maskfile = os.path.join('/etc/portage/package.mask', 'cross-' + target) |
Brian Harring | af019fb | 2012-05-10 15:06:13 -0700 | [diff] [blame] | 343 | osutils.SafeUnlink(maskfile) |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 344 | |
| 345 | |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 346 | # Main functions performing the actual update steps. |
Mike Frysinger | c880a96 | 2013-11-08 13:59:06 -0500 | [diff] [blame] | 347 | def RebuildLibtool(): |
| 348 | """Rebuild libtool as needed |
| 349 | |
| 350 | Libtool hardcodes full paths to internal gcc files, so whenever we upgrade |
| 351 | gcc, libtool will break. We can't use binary packages either as those will |
| 352 | most likely be compiled against the previous version of gcc. |
| 353 | """ |
| 354 | needs_update = False |
| 355 | with open('/usr/bin/libtool') as f: |
| 356 | for line in f: |
| 357 | # Look for a line like: |
| 358 | # sys_lib_search_path_spec="..." |
| 359 | # It'll be a list of paths and gcc will be one of them. |
| 360 | if line.startswith('sys_lib_search_path_spec='): |
| 361 | line = line.rstrip() |
| 362 | for path in line.split('=', 1)[1].strip('"').split(): |
| 363 | if not os.path.exists(path): |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 364 | print('Rebuilding libtool after gcc upgrade') |
| 365 | print(' %s' % line) |
| 366 | print(' missing path: %s' % path) |
Mike Frysinger | c880a96 | 2013-11-08 13:59:06 -0500 | [diff] [blame] | 367 | needs_update = True |
| 368 | break |
| 369 | |
| 370 | if needs_update: |
| 371 | break |
| 372 | |
| 373 | if needs_update: |
| 374 | cmd = [EMERGE_CMD, '--oneshot', 'sys-devel/libtool'] |
| 375 | cros_build_lib.RunCommand(cmd) |
| 376 | |
| 377 | |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 378 | def UpdateTargets(targets, usepkg): |
| 379 | """Determines which packages need update/unmerge and defers to portage. |
| 380 | |
Mike Frysinger | dc649ae | 2014-11-26 19:29:24 -0500 | [diff] [blame] | 381 | Args: |
| 382 | targets: The list of targets to update |
| 383 | usepkg: Copies the commandline option |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 384 | """ |
David James | 90239b9 | 2012-11-05 15:31:34 -0800 | [diff] [blame] | 385 | # Remove keyword files created by old versions of cros_setup_toolchains. |
| 386 | osutils.SafeUnlink('/etc/portage/package.keywords/cross-host') |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 387 | |
| 388 | # For each target, we do two things. Figure out the list of updates, |
| 389 | # and figure out the appropriate keywords/masks. Crossdev will initialize |
| 390 | # these, but they need to be regenerated on every update. |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 391 | print('Determining required toolchain updates...') |
David James | 90239b9 | 2012-11-05 15:31:34 -0800 | [diff] [blame] | 392 | mergemap = {} |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 393 | for target in targets: |
| 394 | # Record the highest needed version for each target, for masking purposes. |
| 395 | RemovePackageMask(target) |
Zdenek Behan | f4d18a0 | 2012-03-22 15:45:05 +0100 | [diff] [blame] | 396 | for package in GetTargetPackages(target): |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 397 | # Portage name for the package |
Zdenek Behan | 4eb6fd2 | 2012-03-12 17:00:56 +0100 | [diff] [blame] | 398 | if IsPackageDisabled(target, package): |
| 399 | continue |
| 400 | pkg = GetPortagePackage(target, package) |
David James | 66a09c4 | 2012-11-05 13:31:38 -0800 | [diff] [blame] | 401 | current = GetInstalledPackageVersions(pkg) |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 402 | desired = GetDesiredPackageVersions(target, package) |
Zdenek Behan | 699ddd3 | 2012-04-13 07:14:08 +0200 | [diff] [blame] | 403 | desired_num = VersionListToNumeric(target, package, desired, False) |
Zdenek Behan | 4eb6fd2 | 2012-03-12 17:00:56 +0100 | [diff] [blame] | 404 | mergemap[pkg] = set(desired_num).difference(current) |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 405 | |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 406 | packages = [] |
| 407 | for pkg in mergemap: |
| 408 | for ver in mergemap[pkg]: |
Zdenek Behan | 677b6d8 | 2012-04-11 05:31:47 +0200 | [diff] [blame] | 409 | if ver != PACKAGE_NONE: |
David James | 90239b9 | 2012-11-05 15:31:34 -0800 | [diff] [blame] | 410 | packages.append(pkg) |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 411 | |
| 412 | if not packages: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 413 | print('Nothing to update!') |
David James | f8c672f | 2012-11-06 13:38:11 -0800 | [diff] [blame] | 414 | return False |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 415 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 416 | print('Updating packages:') |
| 417 | print(packages) |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 418 | |
Zdenek Behan | 7e33b4e | 2012-03-12 17:00:56 +0100 | [diff] [blame] | 419 | cmd = [EMERGE_CMD, '--oneshot', '--update'] |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 420 | if usepkg: |
| 421 | cmd.extend(['--getbinpkg', '--usepkgonly']) |
| 422 | |
| 423 | cmd.extend(packages) |
| 424 | cros_build_lib.RunCommand(cmd) |
David James | f8c672f | 2012-11-06 13:38:11 -0800 | [diff] [blame] | 425 | return True |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 426 | |
| 427 | |
| 428 | def CleanTargets(targets): |
| 429 | """Unmerges old packages that are assumed unnecessary.""" |
| 430 | unmergemap = {} |
| 431 | for target in targets: |
Zdenek Behan | f4d18a0 | 2012-03-22 15:45:05 +0100 | [diff] [blame] | 432 | for package in GetTargetPackages(target): |
Zdenek Behan | 4eb6fd2 | 2012-03-12 17:00:56 +0100 | [diff] [blame] | 433 | if IsPackageDisabled(target, package): |
| 434 | continue |
| 435 | pkg = GetPortagePackage(target, package) |
David James | 66a09c4 | 2012-11-05 13:31:38 -0800 | [diff] [blame] | 436 | current = GetInstalledPackageVersions(pkg) |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 437 | desired = GetDesiredPackageVersions(target, package) |
Zdenek Behan | 699ddd3 | 2012-04-13 07:14:08 +0200 | [diff] [blame] | 438 | desired_num = VersionListToNumeric(target, package, desired, True) |
| 439 | if not set(desired_num).issubset(current): |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 440 | print('Some packages have been held back, skipping clean!') |
Zdenek Behan | 699ddd3 | 2012-04-13 07:14:08 +0200 | [diff] [blame] | 441 | return |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 442 | unmergemap[pkg] = set(current).difference(desired_num) |
| 443 | |
| 444 | # Cleaning doesn't care about consistency and rebuilding package.* files. |
| 445 | packages = [] |
| 446 | for pkg, vers in unmergemap.iteritems(): |
| 447 | packages.extend('=%s-%s' % (pkg, ver) for ver in vers if ver != '9999') |
| 448 | |
| 449 | if packages: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 450 | print('Cleaning packages:') |
| 451 | print(packages) |
Zdenek Behan | 7e33b4e | 2012-03-12 17:00:56 +0100 | [diff] [blame] | 452 | cmd = [EMERGE_CMD, '--unmerge'] |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 453 | cmd.extend(packages) |
| 454 | cros_build_lib.RunCommand(cmd) |
| 455 | else: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 456 | print('Nothing to clean!') |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 457 | |
| 458 | |
| 459 | def SelectActiveToolchains(targets, suffixes): |
| 460 | """Runs gcc-config and binutils-config to select the desired. |
| 461 | |
Mike Frysinger | dc649ae | 2014-11-26 19:29:24 -0500 | [diff] [blame] | 462 | Args: |
| 463 | targets: The targets to select |
| 464 | suffixes: Optional target-specific hacks |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 465 | """ |
| 466 | for package in ['gcc', 'binutils']: |
| 467 | for target in targets: |
| 468 | # Pick the first version in the numbered list as the selected one. |
| 469 | desired = GetDesiredPackageVersions(target, package) |
Zdenek Behan | 699ddd3 | 2012-04-13 07:14:08 +0200 | [diff] [blame] | 470 | desired_num = VersionListToNumeric(target, package, desired, True) |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 471 | desired = desired_num[0] |
| 472 | # *-config does not play revisions, strip them, keep just PV. |
| 473 | desired = portage.versions.pkgsplit('%s-%s' % (package, desired))[1] |
| 474 | |
| 475 | if target == 'host': |
| 476 | # *-config is the only tool treating host identically (by tuple). |
David James | 27ac4ae | 2012-12-03 23:16:15 -0800 | [diff] [blame] | 477 | target = toolchain.GetHostTuple() |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 478 | |
| 479 | # And finally, attach target to it. |
| 480 | desired = '%s-%s' % (target, desired) |
| 481 | |
| 482 | # Target specific hacks |
| 483 | if package in suffixes: |
| 484 | if target in suffixes[package]: |
| 485 | desired += suffixes[package][target] |
| 486 | |
David James | 7ec5efc | 2012-11-06 09:39:49 -0800 | [diff] [blame] | 487 | extra_env = {'CHOST': target} |
| 488 | cmd = ['%s-config' % package, '-c', target] |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 489 | result = cros_build_lib.RunCommand( |
| 490 | cmd, print_cmd=False, redirect_stdout=True, extra_env=extra_env) |
| 491 | current = result.output.splitlines()[0] |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 492 | # Do not gcc-config when the current is live or nothing needs to be done. |
| 493 | if current != desired and current != '9999': |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 494 | cmd = [package + '-config', desired] |
Zdenek Behan | 7e33b4e | 2012-03-12 17:00:56 +0100 | [diff] [blame] | 495 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 496 | |
| 497 | |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 498 | def ExpandTargets(targets_wanted): |
| 499 | """Expand any possible toolchain aliases into full targets |
| 500 | |
| 501 | This will expand 'all' and 'sdk' into the respective toolchain tuples. |
| 502 | |
| 503 | Args: |
| 504 | targets_wanted: The targets specified by the user. |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 505 | |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 506 | Returns: |
Gilad Arnold | 8195b53 | 2015-04-07 10:56:30 +0300 | [diff] [blame] | 507 | Dictionary of concrete targets and their toolchain tuples. |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 508 | """ |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 509 | targets_wanted = set(targets_wanted) |
Gilad Arnold | 8195b53 | 2015-04-07 10:56:30 +0300 | [diff] [blame] | 510 | if targets_wanted in (set(['boards']), set(['bricks'])): |
| 511 | # Only pull targets from the included boards/bricks. |
| 512 | return {} |
| 513 | |
| 514 | all_targets = toolchain.GetAllTargets() |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 515 | if targets_wanted == set(['all']): |
Gilad Arnold | 8195b53 | 2015-04-07 10:56:30 +0300 | [diff] [blame] | 516 | return all_targets |
| 517 | if targets_wanted == set(['sdk']): |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 518 | # Filter out all the non-sdk toolchains as we don't want to mess |
| 519 | # with those in all of our builds. |
Gilad Arnold | 8195b53 | 2015-04-07 10:56:30 +0300 | [diff] [blame] | 520 | return toolchain.FilterToolchains(all_targets, 'sdk', True) |
| 521 | |
| 522 | # Verify user input. |
| 523 | nonexistent = targets_wanted.difference(all_targets) |
| 524 | if nonexistent: |
| 525 | raise ValueError('Invalid targets: %s', ','.join(nonexistent)) |
| 526 | return {t: all_targets[t] for t in targets_wanted} |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 527 | |
| 528 | |
David James | f8c672f | 2012-11-06 13:38:11 -0800 | [diff] [blame] | 529 | def UpdateToolchains(usepkg, deleteold, hostonly, reconfig, |
Gilad Arnold | 8195b53 | 2015-04-07 10:56:30 +0300 | [diff] [blame] | 530 | targets_wanted, boards_wanted, bricks_wanted): |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 531 | """Performs all steps to create a synchronized toolchain enviroment. |
| 532 | |
Mike Frysinger | dc649ae | 2014-11-26 19:29:24 -0500 | [diff] [blame] | 533 | Args: |
| 534 | usepkg: Use prebuilt packages |
| 535 | deleteold: Unmerge deprecated packages |
| 536 | hostonly: Only setup the host toolchain |
| 537 | reconfig: Reload crossdev config and reselect toolchains |
| 538 | targets_wanted: All the targets to update |
| 539 | boards_wanted: Load targets from these boards |
Gilad Arnold | 8195b53 | 2015-04-07 10:56:30 +0300 | [diff] [blame] | 540 | bricks_wanted: Load targets from these bricks |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 541 | """ |
David James | f8c672f | 2012-11-06 13:38:11 -0800 | [diff] [blame] | 542 | targets, crossdev_targets, reconfig_targets = {}, {}, {} |
Zdenek Behan | 7e33b4e | 2012-03-12 17:00:56 +0100 | [diff] [blame] | 543 | if not hostonly: |
| 544 | # For hostonly, we can skip most of the below logic, much of which won't |
| 545 | # work on bare systems where this is useful. |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 546 | targets = ExpandTargets(targets_wanted) |
Mike Frysinger | 7ccee99 | 2012-06-01 21:27:59 -0400 | [diff] [blame] | 547 | |
Gilad Arnold | 8195b53 | 2015-04-07 10:56:30 +0300 | [diff] [blame] | 548 | # Now re-add any targets that might be from this board/brick. This is to |
| 549 | # allow unofficial boards to declare their own toolchains. |
Mike Frysinger | 7ccee99 | 2012-06-01 21:27:59 -0400 | [diff] [blame] | 550 | for board in boards_wanted: |
David James | 27ac4ae | 2012-12-03 23:16:15 -0800 | [diff] [blame] | 551 | targets.update(toolchain.GetToolchainsForBoard(board)) |
Gilad Arnold | 8195b53 | 2015-04-07 10:56:30 +0300 | [diff] [blame] | 552 | for brick in bricks_wanted: |
| 553 | targets.update(toolchain.GetToolchainsForBrick(brick)) |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 554 | |
Zdenek Behan | 7e33b4e | 2012-03-12 17:00:56 +0100 | [diff] [blame] | 555 | # First check and initialize all cross targets that need to be. |
Mike Frysinger | 7ccee99 | 2012-06-01 21:27:59 -0400 | [diff] [blame] | 556 | for target in targets: |
| 557 | if TargetIsInitialized(target): |
| 558 | reconfig_targets[target] = targets[target] |
| 559 | else: |
| 560 | crossdev_targets[target] = targets[target] |
Zdenek Behan | 7e33b4e | 2012-03-12 17:00:56 +0100 | [diff] [blame] | 561 | if crossdev_targets: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 562 | print('The following targets need to be re-initialized:') |
| 563 | print(crossdev_targets) |
David James | 66a09c4 | 2012-11-05 13:31:38 -0800 | [diff] [blame] | 564 | Crossdev.UpdateTargets(crossdev_targets, usepkg) |
Zdenek Behan | 8be29ba | 2012-05-29 23:10:34 +0200 | [diff] [blame] | 565 | # Those that were not initialized may need a config update. |
David James | 66a09c4 | 2012-11-05 13:31:38 -0800 | [diff] [blame] | 566 | Crossdev.UpdateTargets(reconfig_targets, usepkg, config_only=True) |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 567 | |
Zdenek Behan | 7e33b4e | 2012-03-12 17:00:56 +0100 | [diff] [blame] | 568 | # We want host updated. |
Mike Frysinger | 7ccee99 | 2012-06-01 21:27:59 -0400 | [diff] [blame] | 569 | targets['host'] = {} |
Zdenek Behan | 7e33b4e | 2012-03-12 17:00:56 +0100 | [diff] [blame] | 570 | |
| 571 | # Now update all packages. |
David James | f8c672f | 2012-11-06 13:38:11 -0800 | [diff] [blame] | 572 | if UpdateTargets(targets, usepkg) or crossdev_targets or reconfig: |
| 573 | SelectActiveToolchains(targets, CONFIG_TARGET_SUFFIXES) |
David James | 7ec5efc | 2012-11-06 09:39:49 -0800 | [diff] [blame] | 574 | |
| 575 | if deleteold: |
| 576 | CleanTargets(targets) |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 577 | |
Mike Frysinger | c880a96 | 2013-11-08 13:59:06 -0500 | [diff] [blame] | 578 | # Now that we've cleared out old versions, see if we need to rebuild |
| 579 | # anything. Can't do this earlier as it might not be broken. |
| 580 | RebuildLibtool() |
| 581 | |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 582 | |
Bertrand SIMONNET | cae9d5f | 2015-03-09 15:58:01 -0700 | [diff] [blame] | 583 | def ShowConfig(name): |
| 584 | """Show the toolchain tuples used by |name| |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 585 | |
| 586 | Args: |
Bertrand SIMONNET | cae9d5f | 2015-03-09 15:58:01 -0700 | [diff] [blame] | 587 | name: The board name or brick locator to query. |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 588 | """ |
Bertrand SIMONNET | 3c77bc9 | 2015-03-20 14:28:54 -0700 | [diff] [blame] | 589 | if workspace_lib.IsLocator(name): |
Bertrand SIMONNET | cae9d5f | 2015-03-09 15:58:01 -0700 | [diff] [blame] | 590 | toolchains = toolchain.GetToolchainsForBrick(name) |
| 591 | else: |
| 592 | toolchains = toolchain.GetToolchainsForBoard(name) |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 593 | # Make sure we display the default toolchain first. |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 594 | print(','.join( |
David James | 27ac4ae | 2012-12-03 23:16:15 -0800 | [diff] [blame] | 595 | toolchain.FilterToolchains(toolchains, 'default', True).keys() + |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 596 | toolchain.FilterToolchains(toolchains, 'default', False).keys())) |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 597 | |
| 598 | |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 599 | def GeneratePathWrapper(root, wrappath, path): |
| 600 | """Generate a shell script to execute another shell script |
| 601 | |
| 602 | Since we can't symlink a wrapped ELF (see GenerateLdsoWrapper) because the |
| 603 | argv[0] won't be pointing to the correct path, generate a shell script that |
| 604 | just executes another program with its full path. |
| 605 | |
| 606 | Args: |
| 607 | root: The root tree to generate scripts inside of |
| 608 | wrappath: The full path (inside |root|) to create the wrapper |
| 609 | path: The target program which this wrapper will execute |
| 610 | """ |
| 611 | replacements = { |
| 612 | 'path': path, |
| 613 | 'relroot': os.path.relpath('/', os.path.dirname(wrappath)), |
| 614 | } |
| 615 | wrapper = """#!/bin/sh |
| 616 | base=$(realpath "$0") |
| 617 | basedir=${base%%/*} |
| 618 | exec "${basedir}/%(relroot)s%(path)s" "$@" |
| 619 | """ % replacements |
| 620 | root_wrapper = root + wrappath |
| 621 | if os.path.islink(root_wrapper): |
| 622 | os.unlink(root_wrapper) |
| 623 | else: |
| 624 | osutils.SafeMakedirs(os.path.dirname(root_wrapper)) |
| 625 | osutils.WriteFile(root_wrapper, wrapper) |
Mike Frysinger | 60ec101 | 2013-10-21 00:11:10 -0400 | [diff] [blame] | 626 | os.chmod(root_wrapper, 0o755) |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 627 | |
| 628 | |
| 629 | def FileIsCrosSdkElf(elf): |
| 630 | """Determine if |elf| is an ELF that we execute in the cros_sdk |
| 631 | |
| 632 | We don't need this to be perfect, just quick. It makes sure the ELF |
| 633 | is a 64bit LSB x86_64 ELF. That is the native type of cros_sdk. |
| 634 | |
| 635 | Args: |
| 636 | elf: The file to check |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 637 | |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 638 | Returns: |
| 639 | True if we think |elf| is a native ELF |
| 640 | """ |
| 641 | with open(elf) as f: |
| 642 | data = f.read(20) |
| 643 | # Check the magic number, EI_CLASS, EI_DATA, and e_machine. |
| 644 | return (data[0:4] == '\x7fELF' and |
| 645 | data[4] == '\x02' and |
| 646 | data[5] == '\x01' and |
| 647 | data[18] == '\x3e') |
| 648 | |
| 649 | |
| 650 | def IsPathPackagable(ptype, path): |
| 651 | """Should the specified file be included in a toolchain package? |
| 652 | |
| 653 | We only need to handle files as we'll create dirs as we need them. |
| 654 | |
| 655 | Further, trim files that won't be useful: |
| 656 | - non-english translations (.mo) since it'd require env vars |
| 657 | - debug files since these are for the host compiler itself |
| 658 | - info/man pages as they're big, and docs are online, and the |
| 659 | native docs should work fine for the most part (`man gcc`) |
| 660 | |
| 661 | Args: |
| 662 | ptype: A string describing the path type (i.e. 'file' or 'dir' or 'sym') |
| 663 | path: The full path to inspect |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 664 | |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 665 | Returns: |
| 666 | True if we want to include this path in the package |
| 667 | """ |
| 668 | return not (ptype in ('dir',) or |
| 669 | path.startswith('/usr/lib/debug/') or |
| 670 | os.path.splitext(path)[1] == '.mo' or |
| 671 | ('/man/' in path or '/info/' in path)) |
| 672 | |
| 673 | |
| 674 | def ReadlinkRoot(path, root): |
| 675 | """Like os.readlink(), but relative to a |root| |
| 676 | |
| 677 | Args: |
| 678 | path: The symlink to read |
| 679 | root: The path to use for resolving absolute symlinks |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 680 | |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 681 | Returns: |
| 682 | A fully resolved symlink path |
| 683 | """ |
| 684 | while os.path.islink(root + path): |
| 685 | path = os.path.join(os.path.dirname(path), os.readlink(root + path)) |
| 686 | return path |
| 687 | |
| 688 | |
| 689 | def _GetFilesForTarget(target, root='/'): |
| 690 | """Locate all the files to package for |target| |
| 691 | |
| 692 | This does not cover ELF dependencies. |
| 693 | |
| 694 | Args: |
| 695 | target: The toolchain target name |
| 696 | root: The root path to pull all packages from |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 697 | |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 698 | Returns: |
| 699 | A tuple of a set of all packable paths, and a set of all paths which |
| 700 | are also native ELFs |
| 701 | """ |
| 702 | paths = set() |
| 703 | elfs = set() |
| 704 | |
| 705 | # Find all the files owned by the packages for this target. |
| 706 | for pkg in GetTargetPackages(target): |
| 707 | # Ignore packages that are part of the target sysroot. |
| 708 | if pkg in ('kernel', 'libc'): |
| 709 | continue |
| 710 | |
| 711 | atom = GetPortagePackage(target, pkg) |
| 712 | cat, pn = atom.split('/') |
| 713 | ver = GetInstalledPackageVersions(atom)[0] |
Ralph Nathan | 0304728 | 2015-03-23 11:09:32 -0700 | [diff] [blame] | 714 | logging.info('packaging %s-%s', atom, ver) |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 715 | |
| 716 | # pylint: disable=E1101 |
| 717 | dblink = portage.dblink(cat, pn + '-' + ver, myroot=root, |
| 718 | settings=portage.settings) |
| 719 | contents = dblink.getcontents() |
| 720 | for obj in contents: |
| 721 | ptype = contents[obj][0] |
| 722 | if not IsPathPackagable(ptype, obj): |
| 723 | continue |
| 724 | |
| 725 | if ptype == 'obj': |
| 726 | # For native ELFs, we need to pull in their dependencies too. |
| 727 | if FileIsCrosSdkElf(obj): |
| 728 | elfs.add(obj) |
| 729 | paths.add(obj) |
| 730 | |
| 731 | return paths, elfs |
| 732 | |
| 733 | |
| 734 | def _BuildInitialPackageRoot(output_dir, paths, elfs, ldpaths, |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 735 | path_rewrite_func=lambda x: x, root='/'): |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 736 | """Link in all packable files and their runtime dependencies |
| 737 | |
| 738 | This also wraps up executable ELFs with helper scripts. |
| 739 | |
| 740 | Args: |
| 741 | output_dir: The output directory to store files |
| 742 | paths: All the files to include |
| 743 | elfs: All the files which are ELFs (a subset of |paths|) |
| 744 | ldpaths: A dict of static ldpath information |
| 745 | path_rewrite_func: User callback to rewrite paths in output_dir |
| 746 | root: The root path to pull all packages/files from |
| 747 | """ |
| 748 | # Link in all the files. |
| 749 | sym_paths = [] |
| 750 | for path in paths: |
| 751 | new_path = path_rewrite_func(path) |
| 752 | dst = output_dir + new_path |
| 753 | osutils.SafeMakedirs(os.path.dirname(dst)) |
| 754 | |
| 755 | # Is this a symlink which we have to rewrite or wrap? |
| 756 | # Delay wrap check until after we have created all paths. |
| 757 | src = root + path |
| 758 | if os.path.islink(src): |
| 759 | tgt = os.readlink(src) |
| 760 | if os.path.sep in tgt: |
| 761 | sym_paths.append((new_path, lddtree.normpath(ReadlinkRoot(src, root)))) |
| 762 | |
| 763 | # Rewrite absolute links to relative and then generate the symlink |
| 764 | # ourselves. All other symlinks can be hardlinked below. |
| 765 | if tgt[0] == '/': |
| 766 | tgt = os.path.relpath(tgt, os.path.dirname(new_path)) |
| 767 | os.symlink(tgt, dst) |
| 768 | continue |
| 769 | |
| 770 | os.link(src, dst) |
| 771 | |
| 772 | # Now see if any of the symlinks need to be wrapped. |
| 773 | for sym, tgt in sym_paths: |
| 774 | if tgt in elfs: |
| 775 | GeneratePathWrapper(output_dir, sym, tgt) |
| 776 | |
| 777 | # Locate all the dependencies for all the ELFs. Stick them all in the |
| 778 | # top level "lib" dir to make the wrapper simpler. This exact path does |
| 779 | # not matter since we execute ldso directly, and we tell the ldso the |
| 780 | # exact path to search for its libraries. |
| 781 | libdir = os.path.join(output_dir, 'lib') |
| 782 | osutils.SafeMakedirs(libdir) |
| 783 | donelibs = set() |
| 784 | for elf in elfs: |
Mike Frysinger | ea7688e | 2014-07-31 22:40:33 -0400 | [diff] [blame] | 785 | e = lddtree.ParseELF(elf, root=root, ldpaths=ldpaths) |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 786 | interp = e['interp'] |
| 787 | if interp: |
| 788 | # Generate a wrapper if it is executable. |
Mike Frysinger | c2ec090 | 2013-03-26 01:28:45 -0400 | [diff] [blame] | 789 | interp = os.path.join('/lib', os.path.basename(interp)) |
| 790 | lddtree.GenerateLdsoWrapper(output_dir, path_rewrite_func(elf), interp, |
| 791 | libpaths=e['rpath'] + e['runpath']) |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 792 | |
| 793 | for lib, lib_data in e['libs'].iteritems(): |
| 794 | if lib in donelibs: |
| 795 | continue |
| 796 | |
| 797 | src = path = lib_data['path'] |
| 798 | if path is None: |
Ralph Nathan | 446aee9 | 2015-03-23 14:44:56 -0700 | [diff] [blame] | 799 | logging.warning('%s: could not locate %s', elf, lib) |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 800 | continue |
| 801 | donelibs.add(lib) |
| 802 | |
| 803 | # Needed libs are the SONAME, but that is usually a symlink, not a |
| 804 | # real file. So link in the target rather than the symlink itself. |
| 805 | # We have to walk all the possible symlinks (SONAME could point to a |
| 806 | # symlink which points to a symlink), and we have to handle absolute |
| 807 | # ourselves (since we have a "root" argument). |
| 808 | dst = os.path.join(libdir, os.path.basename(path)) |
| 809 | src = ReadlinkRoot(src, root) |
| 810 | |
| 811 | os.link(root + src, dst) |
| 812 | |
| 813 | |
| 814 | def _EnvdGetVar(envd, var): |
| 815 | """Given a Gentoo env.d file, extract a var from it |
| 816 | |
| 817 | Args: |
| 818 | envd: The env.d file to load (may be a glob path) |
| 819 | var: The var to extract |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 820 | |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 821 | Returns: |
| 822 | The value of |var| |
| 823 | """ |
| 824 | envds = glob.glob(envd) |
| 825 | assert len(envds) == 1, '%s: should have exactly 1 env.d file' % envd |
| 826 | envd = envds[0] |
| 827 | return cros_build_lib.LoadKeyValueFile(envd)[var] |
| 828 | |
| 829 | |
| 830 | def _ProcessBinutilsConfig(target, output_dir): |
| 831 | """Do what binutils-config would have done""" |
| 832 | binpath = os.path.join('/bin', target + '-') |
Mike Frysinger | d4d40fd | 2014-11-06 17:30:57 -0500 | [diff] [blame] | 833 | |
| 834 | # Locate the bin dir holding the gold linker. |
| 835 | binutils_bin_path = os.path.join(output_dir, 'usr', toolchain.GetHostTuple(), |
| 836 | target, 'binutils-bin') |
| 837 | globpath = os.path.join(binutils_bin_path, '*-gold') |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 838 | srcpath = glob.glob(globpath) |
Mike Frysinger | d4d40fd | 2014-11-06 17:30:57 -0500 | [diff] [blame] | 839 | if not srcpath: |
| 840 | # Maybe this target doesn't support gold. |
| 841 | globpath = os.path.join(binutils_bin_path, '*') |
| 842 | srcpath = glob.glob(globpath) |
| 843 | assert len(srcpath) == 1, ('%s: matched more than one path (but not *-gold)' |
| 844 | % globpath) |
| 845 | srcpath = srcpath[0] |
| 846 | ld_path = os.path.join(srcpath, 'ld') |
| 847 | assert os.path.exists(ld_path), '%s: linker is missing!' % ld_path |
| 848 | ld_path = os.path.join(srcpath, 'ld.bfd') |
| 849 | assert os.path.exists(ld_path), '%s: linker is missing!' % ld_path |
| 850 | ld_path = os.path.join(srcpath, 'ld.gold') |
| 851 | assert not os.path.exists(ld_path), ('%s: exists, but gold dir does not!' |
| 852 | % ld_path) |
| 853 | |
| 854 | # Nope, no gold support to be found. |
| 855 | gold_supported = False |
Ralph Nathan | 446aee9 | 2015-03-23 14:44:56 -0700 | [diff] [blame] | 856 | logging.warning('%s: binutils lacks support for the gold linker', target) |
Mike Frysinger | d4d40fd | 2014-11-06 17:30:57 -0500 | [diff] [blame] | 857 | else: |
| 858 | assert len(srcpath) == 1, '%s: did not match exactly 1 path' % globpath |
| 859 | gold_supported = True |
Mike Frysinger | 78b7a81 | 2014-11-26 19:45:23 -0500 | [diff] [blame] | 860 | srcpath = srcpath[0] |
Mike Frysinger | d4d40fd | 2014-11-06 17:30:57 -0500 | [diff] [blame] | 861 | |
Mike Frysinger | 78b7a81 | 2014-11-26 19:45:23 -0500 | [diff] [blame] | 862 | srcpath = srcpath[len(output_dir):] |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 863 | gccpath = os.path.join('/usr', 'libexec', 'gcc') |
| 864 | for prog in os.listdir(output_dir + srcpath): |
| 865 | # Skip binaries already wrapped. |
| 866 | if not prog.endswith('.real'): |
| 867 | GeneratePathWrapper(output_dir, binpath + prog, |
| 868 | os.path.join(srcpath, prog)) |
| 869 | GeneratePathWrapper(output_dir, os.path.join(gccpath, prog), |
| 870 | os.path.join(srcpath, prog)) |
| 871 | |
David James | 27ac4ae | 2012-12-03 23:16:15 -0800 | [diff] [blame] | 872 | libpath = os.path.join('/usr', toolchain.GetHostTuple(), target, 'lib') |
Mike Frysinger | d4d40fd | 2014-11-06 17:30:57 -0500 | [diff] [blame] | 873 | envd = os.path.join(output_dir, 'etc', 'env.d', 'binutils', '*') |
| 874 | if gold_supported: |
| 875 | envd += '-gold' |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 876 | srcpath = _EnvdGetVar(envd, 'LIBPATH') |
| 877 | os.symlink(os.path.relpath(srcpath, os.path.dirname(libpath)), |
| 878 | output_dir + libpath) |
| 879 | |
| 880 | |
| 881 | def _ProcessGccConfig(target, output_dir): |
| 882 | """Do what gcc-config would have done""" |
| 883 | binpath = '/bin' |
| 884 | envd = os.path.join(output_dir, 'etc', 'env.d', 'gcc', '*') |
| 885 | srcpath = _EnvdGetVar(envd, 'GCC_PATH') |
| 886 | for prog in os.listdir(output_dir + srcpath): |
| 887 | # Skip binaries already wrapped. |
| 888 | if (not prog.endswith('.real') and |
| 889 | not prog.endswith('.elf') and |
| 890 | prog.startswith(target)): |
| 891 | GeneratePathWrapper(output_dir, os.path.join(binpath, prog), |
| 892 | os.path.join(srcpath, prog)) |
| 893 | return srcpath |
| 894 | |
| 895 | |
Frank Henigman | 179ec7c | 2015-02-06 03:01:09 -0500 | [diff] [blame] | 896 | def _ProcessSysrootWrappers(_target, output_dir, srcpath): |
| 897 | """Remove chroot-specific things from our sysroot wrappers""" |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 898 | # Disable ccache since we know it won't work outside of chroot. |
Frank Henigman | 179ec7c | 2015-02-06 03:01:09 -0500 | [diff] [blame] | 899 | for sysroot_wrapper in glob.glob(os.path.join( |
| 900 | output_dir + srcpath, 'sysroot_wrapper*')): |
| 901 | contents = osutils.ReadFile(sysroot_wrapper).splitlines() |
| 902 | for num in xrange(len(contents)): |
| 903 | if '@CCACHE_DEFAULT@' in contents[num]: |
| 904 | contents[num] = 'use_ccache = False' |
| 905 | break |
| 906 | # Can't update the wrapper in place since it's a hardlink to a file in /. |
| 907 | os.unlink(sysroot_wrapper) |
| 908 | osutils.WriteFile(sysroot_wrapper, '\n'.join(contents)) |
| 909 | os.chmod(sysroot_wrapper, 0o755) |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 910 | |
| 911 | |
| 912 | def _ProcessDistroCleanups(target, output_dir): |
| 913 | """Clean up the tree and remove all distro-specific requirements |
| 914 | |
| 915 | Args: |
| 916 | target: The toolchain target name |
| 917 | output_dir: The output directory to clean up |
| 918 | """ |
| 919 | _ProcessBinutilsConfig(target, output_dir) |
| 920 | gcc_path = _ProcessGccConfig(target, output_dir) |
Frank Henigman | 179ec7c | 2015-02-06 03:01:09 -0500 | [diff] [blame] | 921 | _ProcessSysrootWrappers(target, output_dir, gcc_path) |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 922 | |
| 923 | osutils.RmDir(os.path.join(output_dir, 'etc')) |
| 924 | |
| 925 | |
| 926 | def CreatePackagableRoot(target, output_dir, ldpaths, root='/'): |
| 927 | """Setup a tree from the packages for the specified target |
| 928 | |
| 929 | This populates a path with all the files from toolchain packages so that |
| 930 | a tarball can easily be generated from the result. |
| 931 | |
| 932 | Args: |
| 933 | target: The target to create a packagable root from |
| 934 | output_dir: The output directory to place all the files |
| 935 | ldpaths: A dict of static ldpath information |
| 936 | root: The root path to pull all packages/files from |
| 937 | """ |
| 938 | # Find all the files owned by the packages for this target. |
| 939 | paths, elfs = _GetFilesForTarget(target, root=root) |
| 940 | |
| 941 | # Link in all the package's files, any ELF dependencies, and wrap any |
| 942 | # executable ELFs with helper scripts. |
| 943 | def MoveUsrBinToBin(path): |
| 944 | """Move /usr/bin to /bin so people can just use that toplevel dir""" |
| 945 | return path[4:] if path.startswith('/usr/bin/') else path |
| 946 | _BuildInitialPackageRoot(output_dir, paths, elfs, ldpaths, |
| 947 | path_rewrite_func=MoveUsrBinToBin, root=root) |
| 948 | |
| 949 | # The packages, when part of the normal distro, have helper scripts |
| 950 | # that setup paths and such. Since we are making this standalone, we |
| 951 | # need to preprocess all that ourselves. |
| 952 | _ProcessDistroCleanups(target, output_dir) |
| 953 | |
| 954 | |
| 955 | def CreatePackages(targets_wanted, output_dir, root='/'): |
| 956 | """Create redistributable cross-compiler packages for the specified targets |
| 957 | |
| 958 | This creates toolchain packages that should be usable in conjunction with |
| 959 | a downloaded sysroot (created elsewhere). |
| 960 | |
| 961 | Tarballs (one per target) will be created in $PWD. |
| 962 | |
| 963 | Args: |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 964 | targets_wanted: The targets to package up. |
| 965 | output_dir: The directory to put the packages in. |
| 966 | root: The root path to pull all packages/files from. |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 967 | """ |
Ralph Nathan | 0304728 | 2015-03-23 11:09:32 -0700 | [diff] [blame] | 968 | logging.info('Writing tarballs to %s', output_dir) |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 969 | osutils.SafeMakedirs(output_dir) |
| 970 | ldpaths = lddtree.LoadLdpaths(root) |
| 971 | targets = ExpandTargets(targets_wanted) |
| 972 | |
David James | 4bc1370 | 2013-03-26 08:08:04 -0700 | [diff] [blame] | 973 | with osutils.TempDir() as tempdir: |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 974 | # We have to split the root generation from the compression stages. This is |
| 975 | # because we hardlink in all the files (to avoid overhead of reading/writing |
| 976 | # the copies multiple times). But tar gets angry if a file's hardlink count |
| 977 | # changes from when it starts reading a file to when it finishes. |
| 978 | with parallel.BackgroundTaskRunner(CreatePackagableRoot) as queue: |
| 979 | for target in targets: |
| 980 | output_target_dir = os.path.join(tempdir, target) |
| 981 | queue.put([target, output_target_dir, ldpaths, root]) |
| 982 | |
| 983 | # Build the tarball. |
| 984 | with parallel.BackgroundTaskRunner(cros_build_lib.CreateTarball) as queue: |
| 985 | for target in targets: |
| 986 | tar_file = os.path.join(output_dir, target + '.tar.xz') |
| 987 | queue.put([tar_file, os.path.join(tempdir, target)]) |
| 988 | |
| 989 | |
Brian Harring | 3067505 | 2012-02-29 12:18:22 -0800 | [diff] [blame] | 990 | def main(argv): |
Mike Frysinger | 0c80845 | 2014-11-06 17:30:23 -0500 | [diff] [blame] | 991 | parser = commandline.ArgumentParser(description=__doc__) |
| 992 | parser.add_argument('-u', '--nousepkg', |
| 993 | action='store_false', dest='usepkg', default=True, |
| 994 | help='Use prebuilt packages if possible') |
| 995 | parser.add_argument('-d', '--deleteold', |
| 996 | action='store_true', dest='deleteold', default=False, |
| 997 | help='Unmerge deprecated packages') |
| 998 | parser.add_argument('-t', '--targets', |
| 999 | dest='targets', default='sdk', |
Gilad Arnold | 8195b53 | 2015-04-07 10:56:30 +0300 | [diff] [blame] | 1000 | help="Comma separated list of tuples. Special keywords " |
| 1001 | "'host', 'sdk', 'boards', 'bricks' and 'all' are " |
| 1002 | "allowed. Defaults to 'sdk'.") |
| 1003 | parser.add_argument('--include-boards', default='', metavar='BOARDS', |
| 1004 | help='Comma separated list of boards whose toolchains we ' |
| 1005 | 'will always include. Default: none') |
| 1006 | parser.add_argument('--include-bricks', default='', metavar='BRICKS', |
| 1007 | help='Comma separated list of bricks whose toolchains we ' |
| 1008 | 'will always include. Default: none') |
Mike Frysinger | 0c80845 | 2014-11-06 17:30:23 -0500 | [diff] [blame] | 1009 | parser.add_argument('--hostonly', |
| 1010 | dest='hostonly', default=False, action='store_true', |
| 1011 | help='Only setup the host toolchain. ' |
| 1012 | 'Useful for bootstrapping chroot') |
Bertrand SIMONNET | cae9d5f | 2015-03-09 15:58:01 -0700 | [diff] [blame] | 1013 | parser.add_argument('--show-board-cfg', '--show-cfg', |
| 1014 | dest='cfg_name', default=None, |
| 1015 | help='Board or brick to list toolchains tuples for') |
Mike Frysinger | 0c80845 | 2014-11-06 17:30:23 -0500 | [diff] [blame] | 1016 | parser.add_argument('--create-packages', |
| 1017 | action='store_true', default=False, |
| 1018 | help='Build redistributable packages') |
| 1019 | parser.add_argument('--output-dir', default=os.getcwd(), type='path', |
| 1020 | help='Output directory') |
| 1021 | parser.add_argument('--reconfig', default=False, action='store_true', |
| 1022 | help='Reload crossdev config and reselect toolchains') |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 1023 | |
Mike Frysinger | 0c80845 | 2014-11-06 17:30:23 -0500 | [diff] [blame] | 1024 | options = parser.parse_args(argv) |
| 1025 | options.Freeze() |
Zdenek Behan | 508dcce | 2011-12-05 15:39:32 +0100 | [diff] [blame] | 1026 | |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 1027 | # Figure out what we're supposed to do and reject conflicting options. |
Bertrand SIMONNET | cae9d5f | 2015-03-09 15:58:01 -0700 | [diff] [blame] | 1028 | if options.cfg_name and options.create_packages: |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 1029 | parser.error('conflicting options: create-packages & show-board-cfg') |
Mike Frysinger | 984d062 | 2012-06-01 16:08:44 -0400 | [diff] [blame] | 1030 | |
Gilad Arnold | 8195b53 | 2015-04-07 10:56:30 +0300 | [diff] [blame] | 1031 | targets_wanted = set(options.targets.split(',')) |
| 1032 | boards_wanted = (set(options.include_boards.split(',')) |
| 1033 | if options.include_boards else set()) |
| 1034 | bricks_wanted = (set(options.include_bricks.split(',')) |
| 1035 | if options.include_bricks else set()) |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 1036 | |
Bertrand SIMONNET | cae9d5f | 2015-03-09 15:58:01 -0700 | [diff] [blame] | 1037 | if options.cfg_name: |
| 1038 | ShowConfig(options.cfg_name) |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 1039 | elif options.create_packages: |
| 1040 | cros_build_lib.AssertInsideChroot() |
| 1041 | Crossdev.Load(False) |
Gilad Arnold | 8195b53 | 2015-04-07 10:56:30 +0300 | [diff] [blame] | 1042 | CreatePackages(targets_wanted, options.output_dir) |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 1043 | else: |
| 1044 | cros_build_lib.AssertInsideChroot() |
| 1045 | # This has to be always run as root. |
| 1046 | if os.geteuid() != 0: |
| 1047 | cros_build_lib.Die('this script must be run as root') |
| 1048 | |
| 1049 | Crossdev.Load(options.reconfig) |
| 1050 | UpdateToolchains(options.usepkg, options.deleteold, options.hostonly, |
Gilad Arnold | 8195b53 | 2015-04-07 10:56:30 +0300 | [diff] [blame] | 1051 | options.reconfig, targets_wanted, boards_wanted, |
| 1052 | bricks_wanted) |
Mike Frysinger | 35247af | 2012-11-16 18:58:06 -0500 | [diff] [blame] | 1053 | Crossdev.Save() |
| 1054 | |
| 1055 | return 0 |