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