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