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