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