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