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