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