blob: 8417dfa7c3b16bf4f936fe64db89bbec232deb3a [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Alex Klein19c4cc42019-02-27 14:47:57 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""SDK chroot operations."""
6
Alex Klein19c4cc42019-02-27 14:47:57 -07007import os
Alex Klein0996b462023-06-15 15:26:37 -06008from typing import Dict, TYPE_CHECKING, Union
Alex Klein19c4cc42019-02-27 14:47:57 -07009
Alex Klein231d2da2019-07-22 16:44:45 -060010from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060011from chromite.api import faux
Alex Klein231d2da2019-07-22 16:44:45 -060012from chromite.api import validate
Alex Klein71a9a1d2019-10-28 15:45:10 -060013from chromite.api.controller import controller_util
Bob Haarman0853ce92022-12-13 18:11:53 +000014from chromite.api.gen.chromiumos import common_pb2
Alex Klein19c4cc42019-02-27 14:47:57 -070015from chromite.lib import cros_build_lib
Navil Pereze73734a2023-08-11 13:07:41 +000016from chromite.lib import sysroot_lib
Alex Klein19c4cc42019-02-27 14:47:57 -070017from chromite.service import sdk
18
19
Alex Klein0996b462023-06-15 15:26:37 -060020if TYPE_CHECKING:
21 from chromite.api import api_config
22 from chromite.api.gen.chromite.api import sdk_pb2
23
24
Alex Klein076841b2019-08-29 15:19:39 -060025def _ChrootVersionResponse(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -060026 """Add a fake chroot version to a successful response."""
27 output_proto.version.version = 168
Alex Klein076841b2019-08-29 15:19:39 -060028
29
Bob Haarman171d4092023-01-10 16:44:19 +000030def _BinhostCLs(_input_proto, output_proto, _config):
31 """Add fake CL identifiers to a successful response."""
Bob Haarmanc0082602022-09-20 16:12:43 -070032 output_proto.cls = [
Bob Haarman171d4092023-01-10 16:44:19 +000033 "fakecl:1",
34 "fakecl:2",
Bob Haarmanc0082602022-09-20 16:12:43 -070035 ]
36
37
Bob Haarman0853ce92022-12-13 18:11:53 +000038def _BuildSdkTarballResponse(_input_proto, output_proto, _config):
39 """Populate a fake BuildSdkTarballResponse."""
40 output_proto.sdk_tarball_path.path = "/fake/sdk/tarball.tar.gz"
41 output_proto.sdk_tarball_path.location = common_pb2.Path.OUTSIDE
42
43
44@faux.success(_BuildSdkTarballResponse)
45@validate.require("chroot")
46@validate.validation_complete
47def BuildSdkTarball(
Alex Klein0996b462023-06-15 15:26:37 -060048 input_proto: "sdk_pb2.BuildSdkTarballRequest",
49 output_proto: "sdk_pb2.BuildSdkTarballResponse",
Bob Haarman0853ce92022-12-13 18:11:53 +000050 _config: "api_config.ApiConfig",
51) -> None:
52 chroot = controller_util.ParseChroot(input_proto.chroot)
53 output_proto.sdk_tarball_path.path = str(sdk.BuildSdkTarball(chroot))
54 output_proto.sdk_tarball_path.location = common_pb2.Path.OUTSIDE
55
56
Greg Edelston01ae5942023-01-30 16:26:54 -070057def _CreateManifestFromSdkResponse(_input_proto, output_proto, _config):
58 """Populate a fake CreateManifestFromSdkResponse."""
59 output_proto.manifest_path.path = "/fake/sdk/tarball.tar.gz.Manifest"
60 output_proto.manifest_path.location = common_pb2.Path.Location.INSIDE
61
62
63@faux.success(_CreateManifestFromSdkResponse)
64@validate.require("chroot")
65@validate.require("sdk_path")
66@validate.require("dest_dir")
67@validate.validation_complete
68def CreateManifestFromSdk(
Alex Klein0996b462023-06-15 15:26:37 -060069 input_proto: "sdk_pb2.CreateManifestFromSdkRequest",
70 output_proto: "sdk_pb2.CreateManifestFromSdkResponse",
Greg Edelston01ae5942023-01-30 16:26:54 -070071 _config: "api_config.ApiConfig",
72) -> None:
73 """Create a manifest file showing the ebuilds in an SDK."""
74
75 def _assert_path_is_absolute(path: str, name: str):
76 """Raise an exception if the given path is not absolute."""
77 if not os.path.isabs(path):
78 cros_build_lib.Die(f"The {name} must be absolute; got {path}")
79
80 _assert_path_is_absolute(input_proto.chroot.path, "chroot path")
81 _assert_path_is_absolute(input_proto.sdk_path.path, "SDK path")
82 _assert_path_is_absolute(input_proto.dest_dir.path, "destination directory")
83
Greg Edelston1f5deb62023-03-31 14:22:08 -060084 sdk_path = controller_util.pb2_path_to_pathlib_path(
Greg Edelston01ae5942023-01-30 16:26:54 -070085 input_proto.sdk_path, input_proto.chroot
86 )
Greg Edelston1f5deb62023-03-31 14:22:08 -060087 dest_dir = controller_util.pb2_path_to_pathlib_path(
Greg Edelston01ae5942023-01-30 16:26:54 -070088 input_proto.dest_dir, input_proto.chroot
89 )
90
91 manifest_path = sdk.CreateManifestFromSdk(sdk_path, dest_dir)
92 output_proto.manifest_path.path = str(manifest_path)
93 output_proto.manifest_path.location = common_pb2.Path.Location.OUTSIDE
94
95
Alex Klein076841b2019-08-29 15:19:39 -060096@faux.success(_ChrootVersionResponse)
97@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -060098def Create(
Alex Klein0996b462023-06-15 15:26:37 -060099 input_proto: "sdk_pb2.CreateRequest",
100 output_proto: "sdk_pb2.CreateResponse",
Alex Klein1699fab2022-09-08 08:46:06 -0600101 config: "api_config.ApiConfig",
102) -> Union[int, None]:
103 """Chroot creation, includes support for replacing an existing chroot.
Alex Klein19c4cc42019-02-27 14:47:57 -0700104
Alex Klein1699fab2022-09-08 08:46:06 -0600105 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600106 input_proto: The input proto.
107 output_proto: The output proto.
108 config: The API call config.
Kevin Shelton50dabff2022-04-09 11:29:53 -0700109
Alex Klein1699fab2022-09-08 08:46:06 -0600110 Returns:
Alex Klein611dddd2022-10-11 17:02:01 -0600111 An error code, None otherwise.
Alex Klein1699fab2022-09-08 08:46:06 -0600112 """
113 replace = not input_proto.flags.no_replace
114 bootstrap = input_proto.flags.bootstrap
Brian Norris67374d82023-05-01 12:31:12 -0700115 chroot = controller_util.ParseChroot(input_proto.chroot)
Alex Klein19c4cc42019-02-27 14:47:57 -0700116
Alex Klein1699fab2022-09-08 08:46:06 -0600117 sdk_version = input_proto.sdk_version
118 skip_chroot_upgrade = input_proto.skip_chroot_upgrade
George Burgess IV6e04d602023-08-16 15:08:32 -0600119 ccache_disable = input_proto.ccache_disable
Alex Klein19c4cc42019-02-27 14:47:57 -0700120
Alex Klein1699fab2022-09-08 08:46:06 -0600121 if config.validate_only:
122 return controller.RETURN_CODE_VALID_INPUT
Alex Klein231d2da2019-07-22 16:44:45 -0600123
Alex Klein1699fab2022-09-08 08:46:06 -0600124 args = sdk.CreateArguments(
125 replace=replace,
126 bootstrap=bootstrap,
Brian Norris67374d82023-05-01 12:31:12 -0700127 chroot=chroot,
Alex Klein1699fab2022-09-08 08:46:06 -0600128 sdk_version=sdk_version,
129 skip_chroot_upgrade=skip_chroot_upgrade,
George Burgess IV6e04d602023-08-16 15:08:32 -0600130 ccache_disable=ccache_disable,
Alex Klein1699fab2022-09-08 08:46:06 -0600131 )
Alex Klein19c4cc42019-02-27 14:47:57 -0700132
Alex Klein1699fab2022-09-08 08:46:06 -0600133 version = sdk.Create(args)
Alex Klein19c4cc42019-02-27 14:47:57 -0700134
Alex Klein1699fab2022-09-08 08:46:06 -0600135 if version:
136 output_proto.version.version = version
137 else:
138 # This should be very rare, if ever used, but worth noting.
139 cros_build_lib.Die(
140 "No chroot version could be found. There was likely an"
141 "error creating the chroot that was not detected."
142 )
Alex Kleinaa5c4172019-02-27 17:12:20 -0700143
144
Alex Klein076841b2019-08-29 15:19:39 -0600145@faux.success(_ChrootVersionResponse)
146@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600147@validate.require_each("toolchain_targets", ["name"])
Alex Klein231d2da2019-07-22 16:44:45 -0600148@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -0600149def Update(
Alex Klein0996b462023-06-15 15:26:37 -0600150 input_proto: "sdk_pb2.UpdateRequest",
151 output_proto: "sdk_pb2.UpdateResponse",
Alex Klein1699fab2022-09-08 08:46:06 -0600152 _config: "api_config.ApiConfig",
153):
154 """Update the chroot.
Alex Kleinaa5c4172019-02-27 17:12:20 -0700155
Alex Klein1699fab2022-09-08 08:46:06 -0600156 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600157 input_proto: The input proto.
158 output_proto: The output proto.
159 _config: The API call config.
Alex Klein1699fab2022-09-08 08:46:06 -0600160 """
161 build_source = input_proto.flags.build_source
162 targets = [target.name for target in input_proto.toolchain_targets]
163 toolchain_changed = input_proto.flags.toolchain_changed
Alex Kleinaa5c4172019-02-27 17:12:20 -0700164
Alex Klein1699fab2022-09-08 08:46:06 -0600165 args = sdk.UpdateArguments(
166 build_source=build_source,
167 toolchain_targets=targets,
168 toolchain_changed=toolchain_changed,
169 )
Chris McDonald68faa2a2020-01-13 12:23:05 -0700170
Navil Pereze73734a2023-08-11 13:07:41 +0000171 result = sdk.Update(args)
172 if result.success:
173 output_proto.version.version = result.version
174 elif result.failed_pkgs:
175 sysroot = sysroot_lib.Sysroot("/")
176 controller_util.retrieve_package_log_paths(
177 result.failed_pkgs, output_proto, sysroot
Alex Klein1699fab2022-09-08 08:46:06 -0600178 )
Navil Pereze73734a2023-08-11 13:07:41 +0000179 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
180 else:
181 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Alex Klein730cf552019-10-16 11:28:22 -0600182
183
184@faux.all_empty
Greg Edelston6733dc52023-02-15 15:20:07 -0700185@validate.require("binhost_gs_bucket")
Greg Edelston0382ca42023-05-04 14:00:15 -0600186@validate.require("toolchain_tarball_template")
Greg Edelston6733dc52023-02-15 15:20:07 -0700187@validate.validation_complete
188def Uprev(input_proto, output_proto, _config):
Greg Edelston40aea812023-03-27 16:34:35 -0600189 """Update SDK version file and prebuilt files to point to the latest SDK.
190
191 Files will be changed locally, but not committed.
192 """
193 # If the UprevRequest did not specify a target version,
194 # check the remote SDK version file on Google Cloud Storage for the latest
195 # uprev target.
Greg Edelstond401e5a2023-04-28 15:29:11 -0600196 target_version = (
197 input_proto.version or sdk.get_latest_uprev_target_version()
198 )
Greg Edelston40aea812023-03-27 16:34:35 -0600199
200 # The main uprev logic occurs in service/sdk.py.
Greg Edelstond401e5a2023-04-28 15:29:11 -0600201 modified_files = sdk.uprev_sdk_and_prebuilts(
Greg Edelston6733dc52023-02-15 15:20:07 -0700202 binhost_gs_bucket=input_proto.binhost_gs_bucket,
Greg Edelston8bf6d4c2023-06-30 17:14:39 -0600203 sdk_version=target_version,
Greg Edelston0382ca42023-05-04 14:00:15 -0600204 toolchain_tarball_template=input_proto.toolchain_tarball_template,
Greg Edelston6733dc52023-02-15 15:20:07 -0700205 )
Greg Edelston40aea812023-03-27 16:34:35 -0600206
207 # Populate the UprevResponse object with the modified files.
Greg Edelston6733dc52023-02-15 15:20:07 -0700208 for modified_file in modified_files:
Greg Edelston40aea812023-03-27 16:34:35 -0600209 proto_path = output_proto.modified_files.add()
210 proto_path.path = str(modified_file)
211 proto_path.location = common_pb2.Path.OUTSIDE
Greg Edelston6733dc52023-02-15 15:20:07 -0700212 output_proto.version = target_version
213
214
215@faux.all_empty
Alex Klein730cf552019-10-16 11:28:22 -0600216@validate.validation_complete
217def Delete(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600218 """Delete a chroot."""
219 chroot = controller_util.ParseChroot(input_proto.chroot)
220 sdk.Delete(chroot, force=True)
Chris McDonald53ad5442020-01-17 14:11:55 -0700221
222
223@faux.all_empty
224@validate.validation_complete
Brian Norrisf624bf42023-03-02 12:57:49 -0800225def Unmount(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600226 """Unmount a chroot"""
Brian Norrisf624bf42023-03-02 12:57:49 -0800227 # Deprecated. Do nothing.
Chris McDonaldf48ea202020-01-29 13:19:23 -0700228
229
230@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600231@validate.require("path.path")
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600232@validate.validation_complete
233def UnmountPath(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600234 """Unmount a path"""
235 sdk.UnmountPath(input_proto.path.path)
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600236
237
238@faux.all_empty
Chris McDonaldf48ea202020-01-29 13:19:23 -0700239@validate.validation_complete
240def Clean(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600241 """Clean unneeded files from a chroot."""
242 chroot = controller_util.ParseChroot(input_proto.chroot)
George Engelbrecht86cfae62023-04-05 10:57:41 -0600243
244 # Default (flagless) call sets 'safe' and 'sysroots'.
245 if not (
246 input_proto.safe
247 or input_proto.images
248 or input_proto.sysroots
249 or input_proto.tmp
250 or input_proto.cache
251 or input_proto.logs
252 or input_proto.workdirs
253 or input_proto.incrementals
254 ):
255 sdk.Clean(chroot, safe=True, sysroots=True)
256 else:
257 sdk.Clean(
258 chroot,
259 safe=input_proto.safe,
260 images=input_proto.images,
261 sysroots=input_proto.sysroots,
262 tmp=input_proto.tmp,
263 cache=input_proto.cache,
264 logs=input_proto.logs,
265 workdirs=input_proto.workdirs,
266 incrementals=input_proto.incrementals,
267 )
Chris McDonald9d486802020-01-29 15:57:22 -0700268
269
270@faux.all_empty
271@validate.validation_complete
Bob Haarmand1225ea2022-01-19 22:01:42 +0000272def BuildPrebuilts(input_proto, _output_proto, _config):
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700273 """Build the binary packages that comprise the Chromium OS SDK."""
Alex Klein1699fab2022-09-08 08:46:06 -0600274 chroot = controller_util.ParseChroot(input_proto.chroot)
Greg Edelston199c5362023-01-04 12:29:07 -0700275 sdk.BuildPrebuilts(chroot, board=input_proto.build_target.name)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000276
277
Bob Haarman171d4092023-01-10 16:44:19 +0000278@faux.success(_BinhostCLs)
Bob Haarmanc0082602022-09-20 16:12:43 -0700279@faux.empty_error
Bob Haarman0853ce92022-12-13 18:11:53 +0000280@validate.require(
281 "prepend_version", "version", "upload_location", "sdk_tarball_template"
282)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000283@validate.validation_complete
Bob Haarmanc0082602022-09-20 16:12:43 -0700284def CreateBinhostCLs(
Alex Klein0996b462023-06-15 15:26:37 -0600285 input_proto: "sdk_pb2.CreateBinhostCLsRequest",
286 output_proto: "sdk_pb2.CreateBinhostCLsResponse",
Bob Haarmanc0082602022-09-20 16:12:43 -0700287 _config: "api_config.ApiConfig",
288) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600289 """Create CLs to update the binhost to point at uploaded prebuilts."""
Bob Haarman171d4092023-01-10 16:44:19 +0000290 output_proto.cls.extend(
291 sdk.CreateBinhostCLs(
292 input_proto.prepend_version,
293 input_proto.version,
294 input_proto.upload_location,
295 input_proto.sdk_tarball_template,
296 )
Alex Klein1699fab2022-09-08 08:46:06 -0600297 )
Bob Haarman338f8b72022-02-04 01:02:46 +0000298
299
300@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600301@validate.require("prepend_version", "version", "upload_location")
Bob Haarman338f8b72022-02-04 01:02:46 +0000302@validate.validation_complete
Bob Haarmand1225ea2022-01-19 22:01:42 +0000303def UploadPrebuiltPackages(input_proto, _output_proto, _config):
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700304 """Upload prebuilt packages."""
Alex Klein1699fab2022-09-08 08:46:06 -0600305 sdk.UploadPrebuiltPackages(
306 controller_util.ParseChroot(input_proto.chroot),
307 input_proto.prepend_version,
308 input_proto.version,
309 input_proto.upload_location,
310 )
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700311
312
313@faux.all_empty
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700314@validate.validation_complete
315def BuildSdkToolchain(input_proto, output_proto, _config):
316 """Build cross-compiler packages for the SDK."""
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700317 extra_env: Dict[str, str] = {}
318 if input_proto.use_flags:
319 extra_env["USE"] = " ".join(use.flag for use in input_proto.use_flags)
Greg Edelstone265a172023-06-26 17:20:05 -0600320 generated_files = sdk.BuildSdkToolchain(extra_env=extra_env)
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700321 output_proto.generated_files.extend(generated_files)