Allen Webb | 3e498aa | 2023-09-05 14:40:49 +0000 | [diff] [blame] | 1 | # Copyright 2023 The ChromiumOS Authors |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Check whether a package links libraries not in RDEPEND. |
| 6 | |
| 7 | If no argument is provided it will check all installed packages. It takes the |
| 8 | BOARD environment variable into account. |
| 9 | |
| 10 | Example: |
| 11 | package_hash_missing_deps.py --board=amd64-generic --match \ |
| 12 | chromeos-base/cryptohome |
| 13 | """ |
| 14 | |
| 15 | import argparse |
| 16 | import collections |
| 17 | import os |
Allen Webb | e8c1da0 | 2023-09-08 18:25:22 +0000 | [diff] [blame] | 18 | from pathlib import Path |
Allen Webb | 3e498aa | 2023-09-05 14:40:49 +0000 | [diff] [blame] | 19 | import pprint |
| 20 | import sys |
Allen Webb | e8c1da0 | 2023-09-08 18:25:22 +0000 | [diff] [blame] | 21 | from typing import List, Optional, Set, Union |
Allen Webb | 3e498aa | 2023-09-05 14:40:49 +0000 | [diff] [blame] | 22 | |
| 23 | from chromite.lib import build_target_lib |
| 24 | from chromite.lib import chroot_lib |
| 25 | from chromite.lib import commandline |
| 26 | from chromite.lib import cros_build_lib |
| 27 | from chromite.lib import parallel |
| 28 | from chromite.lib import portage_util |
| 29 | from chromite.lib.parser import package_info |
| 30 | |
| 31 | |
Allen Webb | 7cc7cd9 | 2023-09-11 16:16:33 +0000 | [diff] [blame] | 32 | VIRTUALS = { |
Allen Webb | 4c582aa | 2023-09-12 17:20:49 +0000 | [diff] [blame] | 33 | "virtual/acl": ("sys-apps/acl", "media-libs/img-ddk-bin"), |
| 34 | "virtual/arc-opengles": ( |
| 35 | "media-libs/arc-img-ddk", |
| 36 | "media-libs/arc-mesa-img", |
| 37 | "media-libs/arc-mali-drivers", |
| 38 | "media-libs/arc-mali-drivers-bifrost", |
| 39 | "media-libs/arc-mali-drivers-bifrost-bin", |
| 40 | "media-libs/arc-mali-drivers-valhall", |
| 41 | "media-libs/arc-mali-drivers-valhall-bin", |
| 42 | "media-libs/arc-mesa", |
| 43 | "media-libs/arc-mesa-amd", |
| 44 | "media-libs/arc-mesa-freedreno", |
| 45 | "media-libs/arc-mesa-iris", |
| 46 | "media-libs/arc-mesa-virgl", |
| 47 | "x11-drivers/opengles-headers", |
| 48 | ), |
| 49 | "virtual/cros-camera-hal": ( |
| 50 | "media-libs/cros-camera-hal-intel-ipu3", |
| 51 | "media-libs/cros-camera-hal-intel-ipu6", |
| 52 | "media-libs/cros-camera-hal-mtk", |
| 53 | "media-libs/cros-camera-hal-qti", |
| 54 | "media-libs/cros-camera-hal-rockchip-isp1", |
| 55 | "media-libs/cros-camera-hal-usb", |
| 56 | "media-libs/qti-7c-camera-tuning", |
| 57 | ), |
| 58 | "virtual/img-ddk": ("media-libs/img-ddk", "media-libs/img-ddk-bin"), |
| 59 | "virtual/jpeg": ("media-libs/libjpeg-turbo", "media-libs/jpeg"), |
| 60 | "virtual/krb5": ("app-crypt/mit-krb5", "app-crypt/heimdal"), |
Allen Webb | 7cc7cd9 | 2023-09-11 16:16:33 +0000 | [diff] [blame] | 61 | "virtual/libcrypt": ("sys-libs/libxcrypt",), |
| 62 | "virtual/libelf": ("dev-libs/elfutils", "sys-freebsd/freebsd-lib"), |
| 63 | "virtual/libiconv": ("dev-libs/libiconv",), |
| 64 | "virtual/libintl": ("dev-libs/libintl",), |
| 65 | "virtual/libudev": ( |
| 66 | "sys-apps/systemd-utils", |
| 67 | "sys-fs/udev", |
| 68 | "sys-fs/eudev", |
| 69 | "sys-apps/systemd", |
| 70 | ), |
| 71 | "virtual/libusb": ("dev-libs/libusb", "sys-freebsd/freebsd-lib"), |
Allen Webb | 4c582aa | 2023-09-12 17:20:49 +0000 | [diff] [blame] | 72 | "virtual/opengles": ( |
| 73 | "media-libs/img-ddk", |
| 74 | "media-libs/img-ddk-bin", |
| 75 | "media-libs/mali-drivers-bin", |
| 76 | "media-libs/mali-drivers-bifrost", |
| 77 | "media-libs/mali-drivers-bifrost-bin", |
| 78 | "media-libs/mali-drivers-valhall", |
| 79 | "media-libs/mali-drivers-valhall-bin", |
| 80 | "media-libs/mesa", |
| 81 | "media-libs/mesa-amd", |
| 82 | "media-libs/mesa-freedreno", |
| 83 | "media-libs/mesa-iris", |
| 84 | "media-libs/mesa-llvmpipe", |
| 85 | "media-libs/mesa-panfrost", |
| 86 | "media-libs/mesa-reven", |
| 87 | "x11-drivers/opengles-headers", |
| 88 | ), |
| 89 | "virtual/vulkan-icd": ( |
| 90 | "media-libs/img-ddk", |
| 91 | "media-libs/img-ddk-bin", |
| 92 | "media-libs/mali-drivers-bifrost", |
| 93 | "media-libs/mali-drivers-bifrost-bin", |
| 94 | "media-libs/mali-drivers-valhall", |
| 95 | "media-libs/mali-drivers-valhall-bin", |
| 96 | "media-libs/mesa", |
| 97 | "media-libs/mesa-freedreno", |
| 98 | "media-libs/mesa-iris", |
| 99 | "media-libs/mesa-llvmpipe", |
| 100 | "media-libs/mesa-radv", |
| 101 | "media-libs/vulkan-loader", |
| 102 | ), |
Allen Webb | 7cc7cd9 | 2023-09-11 16:16:33 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | |
Allen Webb | 3e498aa | 2023-09-05 14:40:49 +0000 | [diff] [blame] | 106 | def env_to_libs(var: str) -> List[str]: |
| 107 | """Converts value of REQUIRES to a list of .so files. |
| 108 | |
| 109 | For example: |
| 110 | "arm_32: libRSSupport.so libblasV8.so libc.so ..." |
| 111 | Becomes: |
| 112 | ["libRSSupport.so", "libblasV8.so", "libc.so", ...] |
| 113 | """ |
| 114 | return [x for x in var.split() if not x.endswith(":")] |
| 115 | |
| 116 | |
| 117 | class DotSoResolver: |
| 118 | """Provides shared library related dependency operations.""" |
| 119 | |
| 120 | def __init__( |
| 121 | self, |
| 122 | board: Optional[str] = None, |
| 123 | root: Union[os.PathLike, str] = "/", |
| 124 | chroot: Optional[chroot_lib.Chroot] = None, |
| 125 | ): |
| 126 | self.board = board |
| 127 | self.chroot = chroot if chroot else chroot_lib.Chroot() |
| 128 | |
| 129 | self.sdk_db = portage_util.PortageDB() |
| 130 | self.db = self.sdk_db if root == "/" else portage_util.PortageDB(root) |
| 131 | self.provided_libs_cache = {} |
| 132 | |
Allen Webb | e8c1da0 | 2023-09-08 18:25:22 +0000 | [diff] [blame] | 133 | # Lazy initialize since it might not be needed. |
| 134 | self.lib_to_package_map = None |
| 135 | |
Allen Webb | 3e498aa | 2023-09-05 14:40:49 +0000 | [diff] [blame] | 136 | def get_package( |
| 137 | self, query: str, from_sdk=False |
| 138 | ) -> Optional[portage_util.InstalledPackage]: |
| 139 | """Try to find an InstalledPackage for the provided package string""" |
| 140 | packages = (self.sdk_db if from_sdk else self.db).InstalledPackages() |
| 141 | info = package_info.parse(query) |
| 142 | for package in packages: |
| 143 | if info.package != package.package: |
| 144 | continue |
| 145 | if info.category != package.category: |
| 146 | continue |
| 147 | dep_info = package.package_info |
| 148 | if info.revision and info.revision != dep_info.revision: |
| 149 | continue |
| 150 | if info.pv and info.pv != dep_info.pv: |
| 151 | continue |
| 152 | return package |
| 153 | return None |
| 154 | |
| 155 | def get_required_libs(self, package) -> Set[str]: |
| 156 | """Return a set of required .so files.""" |
Allen Webb | 8fc0bba | 2023-09-11 14:37:25 +0000 | [diff] [blame] | 157 | requires = package.requires |
| 158 | if requires is not None: |
| 159 | return set(env_to_libs(package.requires)) |
| 160 | # Fallback to needed if requires is not available. |
| 161 | aggregate = set() |
| 162 | needed = package.needed |
| 163 | if needed is not None: |
| 164 | for libs in needed.values(): |
| 165 | aggregate.update(libs) |
| 166 | return aggregate |
Allen Webb | 3e498aa | 2023-09-05 14:40:49 +0000 | [diff] [blame] | 167 | |
Allen Webb | 7cc7cd9 | 2023-09-11 16:16:33 +0000 | [diff] [blame] | 168 | def get_deps(self, package) -> List[portage_util.InstalledPackage]: |
| 169 | """Return a list of dependencies. |
| 170 | |
| 171 | This expands the virtuals listed below. |
| 172 | """ |
Allen Webb | 3e498aa | 2023-09-05 14:40:49 +0000 | [diff] [blame] | 173 | cpvr = f"{package.category}/{package.pf}" |
Allen Webb | 7cc7cd9 | 2023-09-11 16:16:33 +0000 | [diff] [blame] | 174 | expanded = [] |
| 175 | deps = [] |
| 176 | for dep in portage_util.GetFlattenedDepsForPackage( |
Allen Webb | 3e498aa | 2023-09-05 14:40:49 +0000 | [diff] [blame] | 177 | cpvr, board=self.board, depth=1 |
Allen Webb | 7cc7cd9 | 2023-09-11 16:16:33 +0000 | [diff] [blame] | 178 | ): |
| 179 | info = package_info.parse(dep) |
| 180 | if not info: |
| 181 | continue |
| 182 | |
| 183 | cp = info.cp |
| 184 | if cp in VIRTUALS: |
| 185 | expanded += VIRTUALS[cp] |
| 186 | continue |
| 187 | |
| 188 | pkg = self.db.GetInstalledPackage(info.category, info.pvr) |
| 189 | if pkg: |
| 190 | deps.append(pkg) |
| 191 | |
| 192 | for dep in expanded: |
| 193 | pkg = self.get_package(dep) |
| 194 | if pkg: |
| 195 | deps.append(pkg) |
| 196 | |
| 197 | return deps |
Allen Webb | 3e498aa | 2023-09-05 14:40:49 +0000 | [diff] [blame] | 198 | |
| 199 | def get_implicit_libs(self): |
| 200 | """Return a set of .so files that are provided by the system.""" |
| 201 | implicit_libs = set() |
| 202 | for dep, from_sdk in ( |
| 203 | ("cross-aarch64-cros-linux-gnu/glibc", True), |
| 204 | ("cross-armv7a-cros-linux-gnueabihf/glibc", True), |
| 205 | ("cross-i686-cros-linux-gnu/glibc", True), |
| 206 | ("cross-x86_64-cros-linux-gnu/glibc", True), |
| 207 | ("sys-libs/glibc", False), |
| 208 | ("sys-libs/libcxx", False), |
| 209 | ("sys-libs/llvm-libunwind", False), |
| 210 | ): |
| 211 | pkg = self.get_package(dep, from_sdk) |
| 212 | if not pkg: |
| 213 | continue |
| 214 | implicit_libs.update(self.provided_libs(pkg)) |
| 215 | return implicit_libs |
| 216 | |
| 217 | def provided_libs(self, package: portage_util.InstalledPackage) -> Set[str]: |
| 218 | """Return a set of .so files provided by |package|.""" |
| 219 | cpvr = f"{package.category}/{package.pf}" |
| 220 | if cpvr in self.provided_libs_cache: |
| 221 | return self.provided_libs_cache[cpvr] |
| 222 | |
| 223 | libs = set() |
| 224 | contents = package.ListContents() |
| 225 | # Keep only the .so files |
| 226 | for typ, path in contents: |
| 227 | if typ == package.DIR: |
| 228 | continue |
| 229 | filename = os.path.basename(path) |
| 230 | if filename.endswith(".so") or ".so." in filename: |
| 231 | libs.add(filename) |
| 232 | self.provided_libs_cache[cpvr] = libs |
| 233 | return libs |
| 234 | |
Allen Webb | 8fc0bba | 2023-09-11 14:37:25 +0000 | [diff] [blame] | 235 | def cache_libs_from_build( |
| 236 | self, package: portage_util.InstalledPackage, image_dir: Path |
| 237 | ): |
| 238 | """Populate the provided_libs_cache for the package from the image dir. |
| 239 | |
| 240 | When using build-info, CONTENTS might not be available yet. so provide |
| 241 | alternative using the destination directory of the ebuild. |
| 242 | """ |
| 243 | |
| 244 | cpvr = f"{package.category}/{package.pf}" |
| 245 | libs = set() |
| 246 | for _, _, files in os.walk(image_dir): |
| 247 | for file in files: |
| 248 | if file.endswith(".so") or ".so." in file: |
| 249 | libs.add(os.path.basename(file)) |
| 250 | self.provided_libs_cache[cpvr] = libs |
| 251 | |
Allen Webb | 3e498aa | 2023-09-05 14:40:49 +0000 | [diff] [blame] | 252 | def get_provided_from_all_deps( |
| 253 | self, package: portage_util.InstalledPackage |
| 254 | ) -> Set[str]: |
| 255 | """Return a set of .so files provided by the immediate dependencies.""" |
| 256 | provided_libs = set() |
Allen Webb | 8fc0bba | 2023-09-11 14:37:25 +0000 | [diff] [blame] | 257 | # |package| may not actually be installed yet so manually add it to the |
| 258 | # since a package can depend on its own libs. |
| 259 | provided_libs.update(self.provided_libs(package)) |
Allen Webb | 7cc7cd9 | 2023-09-11 16:16:33 +0000 | [diff] [blame] | 260 | for pkg in self.get_deps(package): |
| 261 | provided_libs.update(self.provided_libs(pkg)) |
Allen Webb | 3e498aa | 2023-09-05 14:40:49 +0000 | [diff] [blame] | 262 | return provided_libs |
| 263 | |
Allen Webb | e8c1da0 | 2023-09-08 18:25:22 +0000 | [diff] [blame] | 264 | def lib_to_package(self, lib_filename: str = None) -> Set[str]: |
| 265 | """Return a set of packages that contain the library.""" |
| 266 | if self.lib_to_package_map is None: |
| 267 | lookup = collections.defaultdict(set) |
| 268 | for pkg in self.db.InstalledPackages(): |
| 269 | cpvr = f"{pkg.category}/{pkg.pf}" |
| 270 | # Packages with bundled libs for internal use and/or standaline |
| 271 | # binary packages. |
| 272 | if f"{pkg.category}/{pkg.package}" in ( |
| 273 | "app-emulation/qemu", |
| 274 | "chromeos-base/aosp-frameworks-ml-nn-vts", |
| 275 | "chromeos-base/factory", |
| 276 | "chromeos-base/signingtools-bin", |
| 277 | "sys-devel/gcc-bin", |
| 278 | ): |
| 279 | continue |
| 280 | for lib in set(self.provided_libs(pkg)): |
| 281 | lookup[lib].add(cpvr) |
| 282 | self.lib_to_package_map = lookup |
| 283 | else: |
| 284 | lookup = self.lib_to_package_map |
| 285 | if not lib_filename: |
| 286 | return set() |
| 287 | try: |
| 288 | return lookup[lib_filename] |
| 289 | except KeyError: |
| 290 | return set() |
Allen Webb | 3e498aa | 2023-09-05 14:40:49 +0000 | [diff] [blame] | 291 | |
| 292 | |
| 293 | def get_parser() -> commandline.ArgumentParser: |
| 294 | """Build the argument parser.""" |
| 295 | parser = commandline.ArgumentParser(description=__doc__) |
| 296 | |
| 297 | parser.add_argument("package", nargs="*", help="package atom") |
| 298 | |
| 299 | parser.add_argument( |
| 300 | "-b", |
| 301 | "--board", |
| 302 | "--build-target", |
| 303 | default=cros_build_lib.GetDefaultBoard(), |
| 304 | help="ChromeOS board (Uses the SDK if not specified)", |
| 305 | ) |
| 306 | |
| 307 | parser.add_argument( |
Allen Webb | e8c1da0 | 2023-09-08 18:25:22 +0000 | [diff] [blame] | 308 | "-i", |
| 309 | "--build-info", |
| 310 | default=None, |
| 311 | type=Path, |
| 312 | help="Path to build-info folder post src_install", |
| 313 | ) |
| 314 | |
| 315 | parser.add_argument( |
Allen Webb | 8fc0bba | 2023-09-11 14:37:25 +0000 | [diff] [blame] | 316 | "-x", |
| 317 | "--image", |
| 318 | default=None, |
| 319 | type=Path, |
| 320 | help="Path to image folder post src_install (${D} if unspecified)", |
| 321 | ) |
| 322 | |
| 323 | parser.add_argument( |
Allen Webb | 3e498aa | 2023-09-05 14:40:49 +0000 | [diff] [blame] | 324 | "--match", |
| 325 | default=False, |
| 326 | action="store_true", |
| 327 | help="Try to match missing libraries", |
| 328 | ) |
| 329 | |
| 330 | parser.add_argument( |
| 331 | "-j", |
| 332 | "--jobs", |
| 333 | default=None, |
| 334 | type=int, |
| 335 | help="Number of parallel processes", |
| 336 | ) |
| 337 | |
| 338 | return parser |
| 339 | |
| 340 | |
| 341 | def parse_arguments(argv: List[str]) -> argparse.Namespace: |
| 342 | """Parse and validate arguments.""" |
| 343 | parser = get_parser() |
| 344 | opts = parser.parse_args(argv) |
Allen Webb | e8c1da0 | 2023-09-08 18:25:22 +0000 | [diff] [blame] | 345 | if opts.build_info and opts.package: |
Allen Webb | 8fc0bba | 2023-09-11 14:37:25 +0000 | [diff] [blame] | 346 | parser.error("Do not specify a package when setting --board-info") |
| 347 | if opts.image and not opts.build_info: |
| 348 | parser.error("--image requires --board-info") |
Allen Webb | e8c1da0 | 2023-09-08 18:25:22 +0000 | [diff] [blame] | 349 | if opts.build_info or len(opts.package) == 1: |
Allen Webb | 3e498aa | 2023-09-05 14:40:49 +0000 | [diff] [blame] | 350 | opts.jobs = 1 |
| 351 | return opts |
| 352 | |
| 353 | |
| 354 | def check_package( |
Allen Webb | e8c1da0 | 2023-09-08 18:25:22 +0000 | [diff] [blame] | 355 | package: portage_util.InstalledPackage, |
Allen Webb | 3e498aa | 2023-09-05 14:40:49 +0000 | [diff] [blame] | 356 | implicit: Set[str], |
| 357 | resolver: DotSoResolver, |
| 358 | match: bool, |
| 359 | debug: bool, |
| 360 | ) -> bool: |
| 361 | """Returns false if the package has missing dependencies""" |
| 362 | if not package: |
| 363 | print("missing package") |
| 364 | return False |
| 365 | |
| 366 | provided = resolver.get_provided_from_all_deps(package) |
| 367 | if debug: |
| 368 | print("provided") |
| 369 | pprint.pprint(provided) |
| 370 | |
| 371 | available = provided.union(implicit) |
| 372 | required = resolver.get_required_libs(package) |
| 373 | if debug: |
| 374 | print("required") |
| 375 | pprint.pprint(required) |
| 376 | unsatisfied = required - available |
| 377 | if unsatisfied: |
| 378 | cpvr = package.package_info.cpvr |
| 379 | print(f"'{cpvr}' missing deps for: ", end="") |
| 380 | pprint.pprint(unsatisfied) |
| 381 | if match: |
| 382 | missing = set() |
| 383 | for lib in unsatisfied: |
Allen Webb | e8c1da0 | 2023-09-08 18:25:22 +0000 | [diff] [blame] | 384 | missing.update(resolver.lib_to_package(lib)) |
Allen Webb | 3e498aa | 2023-09-05 14:40:49 +0000 | [diff] [blame] | 385 | if missing: |
| 386 | print(f"'{cpvr}' needs: ", end="") |
| 387 | pprint.pprint(missing) |
| 388 | return False |
| 389 | return True |
| 390 | |
| 391 | |
| 392 | def main(argv: Optional[List[str]]): |
| 393 | """Main.""" |
| 394 | opts = parse_arguments(argv) |
| 395 | opts.Freeze() |
| 396 | |
| 397 | board = opts.board |
| 398 | root = build_target_lib.get_default_sysroot_path(board) |
| 399 | if board: |
| 400 | os.environ["PORTAGE_CONFIGROOT"] = root |
| 401 | os.environ["SYSROOT"] = root |
| 402 | os.environ["ROOT"] = root |
| 403 | |
| 404 | failed = False |
| 405 | resolver = DotSoResolver(board, root) |
Allen Webb | 3e498aa | 2023-09-05 14:40:49 +0000 | [diff] [blame] | 406 | |
| 407 | if not opts.package: |
Allen Webb | e8c1da0 | 2023-09-08 18:25:22 +0000 | [diff] [blame] | 408 | if opts.build_info: |
Allen Webb | 8fc0bba | 2023-09-11 14:37:25 +0000 | [diff] [blame] | 409 | pkg = portage_util.InstalledPackage(resolver.db, opts.build_info) |
| 410 | image_path = opts.image or os.environ.get("D") |
| 411 | if image_path: |
| 412 | resolver.cache_libs_from_build(pkg, Path(image_path)) |
| 413 | packages = [pkg] |
Allen Webb | e8c1da0 | 2023-09-08 18:25:22 +0000 | [diff] [blame] | 414 | else: |
| 415 | packages = resolver.db.InstalledPackages() |
Allen Webb | 3e498aa | 2023-09-05 14:40:49 +0000 | [diff] [blame] | 416 | else: |
| 417 | packages = [resolver.get_package(p) for p in opts.package] |
| 418 | |
| 419 | implicit = resolver.get_implicit_libs() |
| 420 | if opts.debug: |
| 421 | print("implicit") |
| 422 | pprint.pprint(implicit) |
| 423 | |
| 424 | if opts.jobs == 1: |
| 425 | for package in packages: |
| 426 | if not check_package( |
| 427 | package, |
Allen Webb | 3e498aa | 2023-09-05 14:40:49 +0000 | [diff] [blame] | 428 | implicit, |
| 429 | resolver, |
| 430 | opts.match, |
| 431 | opts.debug, |
| 432 | ): |
| 433 | failed = True |
| 434 | else: |
Allen Webb | e8c1da0 | 2023-09-08 18:25:22 +0000 | [diff] [blame] | 435 | if opts.match: |
| 436 | # Pre initialize the map before starting jobs. |
| 437 | resolver.lib_to_package() |
Allen Webb | 3e498aa | 2023-09-05 14:40:49 +0000 | [diff] [blame] | 438 | for ret in parallel.RunTasksInProcessPool( |
| 439 | lambda p: check_package( |
Allen Webb | e8c1da0 | 2023-09-08 18:25:22 +0000 | [diff] [blame] | 440 | p, implicit, resolver, opts.match, opts.debug |
Allen Webb | 3e498aa | 2023-09-05 14:40:49 +0000 | [diff] [blame] | 441 | ), |
| 442 | [[p] for p in packages], |
| 443 | opts.jobs, |
| 444 | ): |
| 445 | if not ret: |
| 446 | failed = True |
| 447 | |
| 448 | if failed: |
| 449 | sys.exit(1) |
| 450 | |
| 451 | |
| 452 | if __name__ == "__main__": |
| 453 | main(sys.argv[1:]) |