blob: dc6788d9663c50bbe667510a97666968024b0b27 [file] [log] [blame]
Alex Klein9ec04452020-05-18 15:08:24 -06001# Copyright 2020 The Chromium OS Authors. All rights reserved.
2# 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
12import os
13
14from chromite.lib import commandline
15from chromite.lib import cros_build_lib
16from chromite.lib import osutils
17from chromite.lib import portage_util
Alex Klein18a60af2020-06-11 12:08:47 -060018from chromite.lib.parser import package_info
Alex Klein9ec04452020-05-18 15:08:24 -060019
Mike Frysinger807d8282022-04-28 22:45:17 -040020
Alex Klein9ec04452020-05-18 15:08:24 -060021if cros_build_lib.IsInsideChroot():
22 from chromite.lib import depgraph
23
24
25def GetParser():
26 """Build the argument parser."""
27 parser = commandline.ArgumentParser(description=__doc__)
28
29 parser.add_argument(
30 '-b',
31 '--build-target',
Alex Klein952ceee2020-09-02 10:13:18 -060032 dest='build_target_name',
Alex Klein9ec04452020-05-18 15:08:24 -060033 help='The build target that is being checked.')
34 parser.add_argument(
35 '--output',
36 type='path',
37 required=True,
38 help='The file path where the result json should be stored.')
39 parser.add_argument(
40 'packages',
41 nargs='+',
42 help='The package atoms that are being checked.')
43
44 return parser
45
46
47def _ParseArguments(argv):
48 """Parse and validate arguments."""
49 parser = GetParser()
50 opts = parser.parse_args(argv)
51
52 if not os.path.exists(os.path.dirname(opts.output)):
53 parser.error('Path containing the output file does not exist.')
54
55 # Manually parse the packages as CPVs.
56 packages = []
57 for pkg in opts.packages:
Alex Klein18a60af2020-06-11 12:08:47 -060058 cpv = package_info.parse(pkg)
59 if not cpv.atom:
Alex Klein9ec04452020-05-18 15:08:24 -060060 parser.error('Invalid package atom: %s' % pkg)
61
62 packages.append(cpv)
63 opts.packages = packages
64
65 opts.Freeze()
66 return opts
67
68
69def main(argv):
70 opts = _ParseArguments(argv)
71 cros_build_lib.AssertInsideChroot()
72
Alex Klein952ceee2020-09-02 10:13:18 -060073 board = opts.build_target_name
Alex Klein9ec04452020-05-18 15:08:24 -060074 bests = {}
75 for cpv in opts.packages:
Alex Klein18a60af2020-06-11 12:08:47 -060076 bests[cpv.atom] = portage_util.PortageqBestVisible(cpv.atom, board=board)
Alex Klein9ec04452020-05-18 15:08:24 -060077
78 # Emerge args:
79 # g: use binpkgs (needed to find if we have one)
80 # u: update packages to latest version (want updates to invalidate binpkgs)
81 # D: deep -- consider full tree rather that just immediate deps
82 # (changes in dependencies and transitive deps can invalidate a binpkg)
83 # N: Packages with changed use flags should be considered
84 # (changes in dependencies and transitive deps can invalidate a binpkg)
85 # q: quiet (simplifies output)
86 # p: pretend (don't actually install it)
87 args = ['-guDNqp', '--with-bdeps=y', '--color=n']
88 if board:
89 args.append('--board=%s' % board)
Alex Klein5caab872021-09-10 11:44:37 -060090 args.extend('=%s' % best.cpvr for best in bests.values())
Alex Klein9ec04452020-05-18 15:08:24 -060091
92 generator = depgraph.DepGraphGenerator()
93 generator.Initialize(args)
94
95 results = {}
96 for atom, best in bests.items():
Alex Klein5caab872021-09-10 11:44:37 -060097 results[atom] = generator.HasPrebuilt(best.cpvr)
Alex Klein9ec04452020-05-18 15:08:24 -060098
99 osutils.WriteFile(opts.output, json.dumps(results))