blob: 6ce8b79fc7fbe13fbac1241c533205f3868e5616 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2020 The ChromiumOS Authors
Alex Klein9ec04452020-05-18 15:08:24 -06002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Script to check if the package(s) have prebuilts.
6
7The script must be run inside the chroot. The output is a json dict mapping the
8package atoms to a boolean for whether a prebuilt exists.
9"""
10
Alex Klein9ec04452020-05-18 15:08:24 -060011import json
Mike Frysinger065bb3d2023-06-21 14:25:15 -040012import logging
Alex Klein9ec04452020-05-18 15:08:24 -060013import os
14
15from chromite.lib import commandline
16from chromite.lib import cros_build_lib
17from chromite.lib import osutils
18from chromite.lib import portage_util
Alex Klein18a60af2020-06-11 12:08:47 -060019from chromite.lib.parser import package_info
Alex Klein9ec04452020-05-18 15:08:24 -060020
Mike Frysinger807d8282022-04-28 22:45:17 -040021
Alex Klein9ec04452020-05-18 15:08:24 -060022if cros_build_lib.IsInsideChroot():
Alex Klein1699fab2022-09-08 08:46:06 -060023 from chromite.lib import depgraph
Alex Klein9ec04452020-05-18 15:08:24 -060024
25
26def GetParser():
Alex Klein1699fab2022-09-08 08:46:06 -060027 """Build the argument parser."""
28 parser = commandline.ArgumentParser(description=__doc__)
Alex Klein9ec04452020-05-18 15:08:24 -060029
Alex Klein1699fab2022-09-08 08:46:06 -060030 parser.add_argument(
31 "-b",
32 "--build-target",
33 dest="build_target_name",
34 help="The build target that is being checked.",
35 )
36 parser.add_argument(
Mike Frysinger043f6052023-10-04 00:00:26 -040037 "--depgraph-root-packages",
38 action="split_extend",
39 help="Search the depgraph starting at these packages.",
40 )
41 parser.add_argument(
Alex Klein1699fab2022-09-08 08:46:06 -060042 "--output",
43 type="path",
44 required=True,
45 help="The file path where the result json should be stored.",
46 )
47 parser.add_argument(
48 "packages", nargs="+", help="The package atoms that are being checked."
49 )
Alex Klein9ec04452020-05-18 15:08:24 -060050
Alex Klein1699fab2022-09-08 08:46:06 -060051 return parser
Alex Klein9ec04452020-05-18 15:08:24 -060052
53
54def _ParseArguments(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060055 """Parse and validate arguments."""
56 parser = GetParser()
57 opts = parser.parse_args(argv)
Alex Klein9ec04452020-05-18 15:08:24 -060058
Alex Klein1699fab2022-09-08 08:46:06 -060059 if not os.path.exists(os.path.dirname(opts.output)):
60 parser.error("Path containing the output file does not exist.")
Alex Klein9ec04452020-05-18 15:08:24 -060061
Alex Klein1699fab2022-09-08 08:46:06 -060062 # Manually parse the packages as CPVs.
63 packages = []
64 for pkg in opts.packages:
65 cpv = package_info.parse(pkg)
66 if not cpv.atom:
67 parser.error("Invalid package atom: %s" % pkg)
Alex Klein9ec04452020-05-18 15:08:24 -060068
Alex Klein1699fab2022-09-08 08:46:06 -060069 packages.append(cpv)
70 opts.packages = packages
Alex Klein9ec04452020-05-18 15:08:24 -060071
Alex Klein1699fab2022-09-08 08:46:06 -060072 opts.Freeze()
73 return opts
Alex Klein9ec04452020-05-18 15:08:24 -060074
75
76def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060077 opts = _ParseArguments(argv)
78 cros_build_lib.AssertInsideChroot()
Alex Klein9ec04452020-05-18 15:08:24 -060079
Alex Klein1699fab2022-09-08 08:46:06 -060080 board = opts.build_target_name
Cindy Lin397a0b12023-06-29 22:09:17 +000081
82 portage_binhost = portage_util.PortageqEnvvar("PORTAGE_BINHOST", board)
83 logging.info("PORTAGE_BINHOST: %s", portage_binhost)
84
Mike Frysingerfbb98992023-06-21 14:34:54 -040085 results = {}
Alex Klein1699fab2022-09-08 08:46:06 -060086 bests = {}
87 for cpv in opts.packages:
Mike Frysinger3917d982023-06-21 14:38:34 -040088 query = cpv.cpvr or cpv.atom
Mike Frysingerfbb98992023-06-21 14:34:54 -040089 try:
Mike Frysinger3917d982023-06-21 14:38:34 -040090 bests[query] = portage_util.PortageqBestVisible(query, board=board)
91 logging.debug("Resolved %s best visible to %s", query, bests[query])
Mike Frysingerfbb98992023-06-21 14:34:54 -040092 except portage_util.NoVisiblePackageError:
Mike Frysinger3917d982023-06-21 14:38:34 -040093 results[query] = False
Alex Klein9ec04452020-05-18 15:08:24 -060094
Mike Frysingerfbb98992023-06-21 14:34:54 -040095 if bests:
Mike Frysingerf264c172023-06-21 14:46:05 -040096 args = [
97 # Fetch remote binpkg databases.
98 "--getbinpkg",
99 # Update packages to the latest version (we want updates to
100 # invalidate installed packages).
101 "--update",
102 # Consider full tree rather than just immediate deps (changes in
103 # dependencies and transitive deps can invalidate a binpkg).
104 "--deep",
105 # Packages with changed USE flags should be considered (changes in
106 # dependencies and transitive deps can invalidate a binpkg).
107 "--newuse",
108 # Simplifies output.
109 "--quiet",
110 # Don't actually install it :).
111 "--pretend",
Mike Frysinger8569dd02023-06-21 14:55:42 -0400112 "--selective=n",
Mike Frysinger516f5de2023-06-28 13:41:41 -0400113 # We run build_packages by default with these flags which can
114 # trigger rebuilds (and ignoring of binpkgs) in more cases.
115 "--newrepo",
116 "--with-test-deps=y",
Mike Frysingerf264c172023-06-21 14:46:05 -0400117 ]
Mike Frysingerfbb98992023-06-21 14:34:54 -0400118 if board:
119 args.append("--board=%s" % board)
Mike Frysinger043f6052023-10-04 00:00:26 -0400120 if opts.depgraph_root_packages:
121 args += [f"--reinstall-atoms={x.cp}" for x in bests.values()]
122 args += opts.depgraph_root_packages
123 else:
124 args.extend("=%s" % best.cpvr for best in bests.values())
Alex Klein9ec04452020-05-18 15:08:24 -0600125
Mike Frysingerfbb98992023-06-21 14:34:54 -0400126 generator = depgraph.DepGraphGenerator()
127 logging.debug(
128 "Initializing depgraph with: %s", cros_build_lib.CmdToStr(args)
129 )
130 generator.Initialize(args)
Alex Klein9ec04452020-05-18 15:08:24 -0600131
Mike Frysingerfbb98992023-06-21 14:34:54 -0400132 for atom, best in bests.items():
133 results[atom] = generator.HasPrebuilt(best.cpvr)
Alex Klein9ec04452020-05-18 15:08:24 -0600134
Alex Klein1699fab2022-09-08 08:46:06 -0600135 osutils.WriteFile(opts.output, json.dumps(results))