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