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