blob: ddf3dc3631ecfaaf7fcbba2d2988e45dc4c29a11 [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
Greg Edelstoncd70b202023-09-14 14:04:12 -06008from pathlib import Path
Alex Klein0996b462023-06-15 15:26:37 -06009from typing import Dict, TYPE_CHECKING, Union
Alex Klein19c4cc42019-02-27 14:47:57 -070010
Alex Klein231d2da2019-07-22 16:44:45 -060011from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060012from chromite.api import faux
Alex Klein231d2da2019-07-22 16:44:45 -060013from chromite.api import validate
Alex Klein71a9a1d2019-10-28 15:45:10 -060014from chromite.api.controller import controller_util
Bob Haarman0853ce92022-12-13 18:11:53 +000015from chromite.api.gen.chromiumos import common_pb2
Alex Klein19c4cc42019-02-27 14:47:57 -070016from chromite.lib import cros_build_lib
Greg Edelstoncd70b202023-09-14 14:04:12 -060017from chromite.lib import path_util
Navil Pereze73734a2023-08-11 13:07:41 +000018from chromite.lib import sysroot_lib
Alex Klein19c4cc42019-02-27 14:47:57 -070019from chromite.service import sdk
20
21
Alex Klein0996b462023-06-15 15:26:37 -060022if TYPE_CHECKING:
23 from chromite.api import api_config
24 from chromite.api.gen.chromite.api import sdk_pb2
25
26
Alex Klein076841b2019-08-29 15:19:39 -060027def _ChrootVersionResponse(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -060028 """Add a fake chroot version to a successful response."""
29 output_proto.version.version = 168
Alex Klein076841b2019-08-29 15:19:39 -060030
31
Bob Haarman171d4092023-01-10 16:44:19 +000032def _BinhostCLs(_input_proto, output_proto, _config):
33 """Add fake CL identifiers to a successful response."""
Bob Haarmanc0082602022-09-20 16:12:43 -070034 output_proto.cls = [
Bob Haarman171d4092023-01-10 16:44:19 +000035 "fakecl:1",
36 "fakecl:2",
Bob Haarmanc0082602022-09-20 16:12:43 -070037 ]
38
39
Bob Haarman0853ce92022-12-13 18:11:53 +000040def _BuildSdkTarballResponse(_input_proto, output_proto, _config):
41 """Populate a fake BuildSdkTarballResponse."""
42 output_proto.sdk_tarball_path.path = "/fake/sdk/tarball.tar.gz"
43 output_proto.sdk_tarball_path.location = common_pb2.Path.OUTSIDE
44
45
46@faux.success(_BuildSdkTarballResponse)
47@validate.require("chroot")
48@validate.validation_complete
49def BuildSdkTarball(
Alex Klein0996b462023-06-15 15:26:37 -060050 input_proto: "sdk_pb2.BuildSdkTarballRequest",
51 output_proto: "sdk_pb2.BuildSdkTarballResponse",
Bob Haarman0853ce92022-12-13 18:11:53 +000052 _config: "api_config.ApiConfig",
53) -> None:
54 chroot = controller_util.ParseChroot(input_proto.chroot)
55 output_proto.sdk_tarball_path.path = str(sdk.BuildSdkTarball(chroot))
56 output_proto.sdk_tarball_path.location = common_pb2.Path.OUTSIDE
57
58
Greg Edelston01ae5942023-01-30 16:26:54 -070059def _CreateManifestFromSdkResponse(_input_proto, output_proto, _config):
60 """Populate a fake CreateManifestFromSdkResponse."""
61 output_proto.manifest_path.path = "/fake/sdk/tarball.tar.gz.Manifest"
62 output_proto.manifest_path.location = common_pb2.Path.Location.INSIDE
63
64
65@faux.success(_CreateManifestFromSdkResponse)
66@validate.require("chroot")
67@validate.require("sdk_path")
68@validate.require("dest_dir")
69@validate.validation_complete
70def CreateManifestFromSdk(
Alex Klein0996b462023-06-15 15:26:37 -060071 input_proto: "sdk_pb2.CreateManifestFromSdkRequest",
72 output_proto: "sdk_pb2.CreateManifestFromSdkResponse",
Greg Edelston01ae5942023-01-30 16:26:54 -070073 _config: "api_config.ApiConfig",
74) -> None:
75 """Create a manifest file showing the ebuilds in an SDK."""
76
77 def _assert_path_is_absolute(path: str, name: str):
78 """Raise an exception if the given path is not absolute."""
79 if not os.path.isabs(path):
80 cros_build_lib.Die(f"The {name} must be absolute; got {path}")
81
82 _assert_path_is_absolute(input_proto.chroot.path, "chroot path")
83 _assert_path_is_absolute(input_proto.sdk_path.path, "SDK path")
84 _assert_path_is_absolute(input_proto.dest_dir.path, "destination directory")
85
Greg Edelston1f5deb62023-03-31 14:22:08 -060086 sdk_path = controller_util.pb2_path_to_pathlib_path(
Greg Edelston01ae5942023-01-30 16:26:54 -070087 input_proto.sdk_path, input_proto.chroot
88 )
Greg Edelston1f5deb62023-03-31 14:22:08 -060089 dest_dir = controller_util.pb2_path_to_pathlib_path(
Greg Edelston01ae5942023-01-30 16:26:54 -070090 input_proto.dest_dir, input_proto.chroot
91 )
92
93 manifest_path = sdk.CreateManifestFromSdk(sdk_path, dest_dir)
94 output_proto.manifest_path.path = str(manifest_path)
95 output_proto.manifest_path.location = common_pb2.Path.Location.OUTSIDE
96
97
Alex Klein076841b2019-08-29 15:19:39 -060098@faux.success(_ChrootVersionResponse)
99@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600100def Create(
Alex Klein0996b462023-06-15 15:26:37 -0600101 input_proto: "sdk_pb2.CreateRequest",
102 output_proto: "sdk_pb2.CreateResponse",
Alex Klein1699fab2022-09-08 08:46:06 -0600103 config: "api_config.ApiConfig",
104) -> Union[int, None]:
105 """Chroot creation, includes support for replacing an existing chroot.
Alex Klein19c4cc42019-02-27 14:47:57 -0700106
Alex Klein1699fab2022-09-08 08:46:06 -0600107 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600108 input_proto: The input proto.
109 output_proto: The output proto.
110 config: The API call config.
Kevin Shelton50dabff2022-04-09 11:29:53 -0700111
Alex Klein1699fab2022-09-08 08:46:06 -0600112 Returns:
Alex Klein611dddd2022-10-11 17:02:01 -0600113 An error code, None otherwise.
Alex Klein1699fab2022-09-08 08:46:06 -0600114 """
115 replace = not input_proto.flags.no_replace
116 bootstrap = input_proto.flags.bootstrap
Brian Norris67374d82023-05-01 12:31:12 -0700117 chroot = controller_util.ParseChroot(input_proto.chroot)
Alex Klein19c4cc42019-02-27 14:47:57 -0700118
Alex Klein1699fab2022-09-08 08:46:06 -0600119 sdk_version = input_proto.sdk_version
120 skip_chroot_upgrade = input_proto.skip_chroot_upgrade
George Burgess IV6e04d602023-08-16 15:08:32 -0600121 ccache_disable = input_proto.ccache_disable
Alex Klein19c4cc42019-02-27 14:47:57 -0700122
Alex Klein1699fab2022-09-08 08:46:06 -0600123 if config.validate_only:
124 return controller.RETURN_CODE_VALID_INPUT
Alex Klein231d2da2019-07-22 16:44:45 -0600125
Alex Klein1699fab2022-09-08 08:46:06 -0600126 args = sdk.CreateArguments(
127 replace=replace,
128 bootstrap=bootstrap,
Brian Norris67374d82023-05-01 12:31:12 -0700129 chroot=chroot,
Alex Klein1699fab2022-09-08 08:46:06 -0600130 sdk_version=sdk_version,
131 skip_chroot_upgrade=skip_chroot_upgrade,
George Burgess IV6e04d602023-08-16 15:08:32 -0600132 ccache_disable=ccache_disable,
Alex Klein1699fab2022-09-08 08:46:06 -0600133 )
Alex Klein19c4cc42019-02-27 14:47:57 -0700134
Alex Klein1699fab2022-09-08 08:46:06 -0600135 version = sdk.Create(args)
Alex Klein19c4cc42019-02-27 14:47:57 -0700136
Alex Klein1699fab2022-09-08 08:46:06 -0600137 if version:
138 output_proto.version.version = version
139 else:
140 # This should be very rare, if ever used, but worth noting.
141 cros_build_lib.Die(
142 "No chroot version could be found. There was likely an"
143 "error creating the chroot that was not detected."
144 )
Alex Kleinaa5c4172019-02-27 17:12:20 -0700145
146
Alex Klein076841b2019-08-29 15:19:39 -0600147@faux.success(_ChrootVersionResponse)
148@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600149@validate.require_each("toolchain_targets", ["name"])
Alex Klein231d2da2019-07-22 16:44:45 -0600150@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -0600151def Update(
Alex Klein0996b462023-06-15 15:26:37 -0600152 input_proto: "sdk_pb2.UpdateRequest",
153 output_proto: "sdk_pb2.UpdateResponse",
Alex Klein1699fab2022-09-08 08:46:06 -0600154 _config: "api_config.ApiConfig",
155):
156 """Update the chroot.
Alex Kleinaa5c4172019-02-27 17:12:20 -0700157
Alex Klein1699fab2022-09-08 08:46:06 -0600158 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600159 input_proto: The input proto.
160 output_proto: The output proto.
161 _config: The API call config.
Alex Klein1699fab2022-09-08 08:46:06 -0600162 """
163 build_source = input_proto.flags.build_source
164 targets = [target.name for target in input_proto.toolchain_targets]
165 toolchain_changed = input_proto.flags.toolchain_changed
Alex Kleinaa5c4172019-02-27 17:12:20 -0700166
Alex Klein1699fab2022-09-08 08:46:06 -0600167 args = sdk.UpdateArguments(
168 build_source=build_source,
169 toolchain_targets=targets,
170 toolchain_changed=toolchain_changed,
171 )
Chris McDonald68faa2a2020-01-13 12:23:05 -0700172
Navil Pereze73734a2023-08-11 13:07:41 +0000173 result = sdk.Update(args)
174 if result.success:
175 output_proto.version.version = result.version
176 elif result.failed_pkgs:
177 sysroot = sysroot_lib.Sysroot("/")
178 controller_util.retrieve_package_log_paths(
179 result.failed_pkgs, output_proto, sysroot
Alex Klein1699fab2022-09-08 08:46:06 -0600180 )
Navil Pereze73734a2023-08-11 13:07:41 +0000181 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
182 else:
183 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Alex Klein730cf552019-10-16 11:28:22 -0600184
185
186@faux.all_empty
Greg Edelston6733dc52023-02-15 15:20:07 -0700187@validate.require("binhost_gs_bucket")
Greg Edelston0382ca42023-05-04 14:00:15 -0600188@validate.require("toolchain_tarball_template")
Greg Edelston6733dc52023-02-15 15:20:07 -0700189@validate.validation_complete
190def Uprev(input_proto, output_proto, _config):
Greg Edelston40aea812023-03-27 16:34:35 -0600191 """Update SDK version file and prebuilt files to point to the latest SDK.
192
193 Files will be changed locally, but not committed.
194 """
195 # If the UprevRequest did not specify a target version,
196 # check the remote SDK version file on Google Cloud Storage for the latest
197 # uprev target.
Greg Edelstond401e5a2023-04-28 15:29:11 -0600198 target_version = (
199 input_proto.version or sdk.get_latest_uprev_target_version()
200 )
Greg Edelston40aea812023-03-27 16:34:35 -0600201
202 # The main uprev logic occurs in service/sdk.py.
Greg Edelstond401e5a2023-04-28 15:29:11 -0600203 modified_files = sdk.uprev_sdk_and_prebuilts(
Greg Edelston6733dc52023-02-15 15:20:07 -0700204 binhost_gs_bucket=input_proto.binhost_gs_bucket,
Greg Edelston8bf6d4c2023-06-30 17:14:39 -0600205 sdk_version=target_version,
Greg Edelston0382ca42023-05-04 14:00:15 -0600206 toolchain_tarball_template=input_proto.toolchain_tarball_template,
Greg Edelston6733dc52023-02-15 15:20:07 -0700207 )
Greg Edelston40aea812023-03-27 16:34:35 -0600208
209 # Populate the UprevResponse object with the modified files.
Greg Edelston6733dc52023-02-15 15:20:07 -0700210 for modified_file in modified_files:
Greg Edelston40aea812023-03-27 16:34:35 -0600211 proto_path = output_proto.modified_files.add()
212 proto_path.path = str(modified_file)
213 proto_path.location = common_pb2.Path.OUTSIDE
Greg Edelston6733dc52023-02-15 15:20:07 -0700214 output_proto.version = target_version
215
216
217@faux.all_empty
Alex Klein730cf552019-10-16 11:28:22 -0600218@validate.validation_complete
219def Delete(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600220 """Delete a chroot."""
221 chroot = controller_util.ParseChroot(input_proto.chroot)
222 sdk.Delete(chroot, force=True)
Chris McDonald53ad5442020-01-17 14:11:55 -0700223
224
225@faux.all_empty
226@validate.validation_complete
Brian Norrisf624bf42023-03-02 12:57:49 -0800227def Unmount(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600228 """Unmount a chroot"""
Brian Norrisf624bf42023-03-02 12:57:49 -0800229 # Deprecated. Do nothing.
Chris McDonaldf48ea202020-01-29 13:19:23 -0700230
231
232@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600233@validate.require("path.path")
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600234@validate.validation_complete
235def UnmountPath(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600236 """Unmount a path"""
237 sdk.UnmountPath(input_proto.path.path)
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600238
239
240@faux.all_empty
Chris McDonaldf48ea202020-01-29 13:19:23 -0700241@validate.validation_complete
242def Clean(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600243 """Clean unneeded files from a chroot."""
244 chroot = controller_util.ParseChroot(input_proto.chroot)
George Engelbrecht86cfae62023-04-05 10:57:41 -0600245
246 # Default (flagless) call sets 'safe' and 'sysroots'.
247 if not (
248 input_proto.safe
249 or input_proto.images
250 or input_proto.sysroots
251 or input_proto.tmp
252 or input_proto.cache
253 or input_proto.logs
254 or input_proto.workdirs
255 or input_proto.incrementals
256 ):
257 sdk.Clean(chroot, safe=True, sysroots=True)
258 else:
259 sdk.Clean(
260 chroot,
261 safe=input_proto.safe,
262 images=input_proto.images,
263 sysroots=input_proto.sysroots,
264 tmp=input_proto.tmp,
265 cache=input_proto.cache,
266 logs=input_proto.logs,
267 workdirs=input_proto.workdirs,
268 incrementals=input_proto.incrementals,
269 )
Chris McDonald9d486802020-01-29 15:57:22 -0700270
271
272@faux.all_empty
273@validate.validation_complete
Greg Edelstoncd70b202023-09-14 14:04:12 -0600274def BuildPrebuilts(input_proto, output_proto, _config):
275 """Build the binary packages that comprise the Chromium OS SDK."""
276 chroot = controller_util.ParseChroot(input_proto.chroot)
277 host_path, target_path = sdk.BuildPrebuilts(
278 chroot,
279 board=input_proto.build_target.name,
280 )
281 # Convert paths to OUTSIDE, rather than using the ResultPath, to avoid
282 # unnecessary copying of several-gigabyte directories, and because
283 # ResultPath doesn't support returning multiple directories.
284 chroot_path_resolver = path_util.ChrootPathResolver(
285 chroot_path=Path(input_proto.chroot.path),
286 out_path=Path(input_proto.chroot.out_path),
287 )
288 output_proto.host_prebuilts_path.path = str(
289 chroot_path_resolver.FromChroot(host_path),
290 )
291 output_proto.host_prebuilts_path.location = common_pb2.Path.OUTSIDE
292 output_proto.target_prebuilts_path.path = str(
293 chroot_path_resolver.FromChroot(target_path),
294 )
295 output_proto.target_prebuilts_path.location = common_pb2.Path.OUTSIDE
Bob Haarmand1225ea2022-01-19 22:01:42 +0000296
297
Bob Haarman171d4092023-01-10 16:44:19 +0000298@faux.success(_BinhostCLs)
Bob Haarmanc0082602022-09-20 16:12:43 -0700299@faux.empty_error
Bob Haarman0853ce92022-12-13 18:11:53 +0000300@validate.require(
301 "prepend_version", "version", "upload_location", "sdk_tarball_template"
302)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000303@validate.validation_complete
Bob Haarmanc0082602022-09-20 16:12:43 -0700304def CreateBinhostCLs(
Alex Klein0996b462023-06-15 15:26:37 -0600305 input_proto: "sdk_pb2.CreateBinhostCLsRequest",
306 output_proto: "sdk_pb2.CreateBinhostCLsResponse",
Bob Haarmanc0082602022-09-20 16:12:43 -0700307 _config: "api_config.ApiConfig",
308) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600309 """Create CLs to update the binhost to point at uploaded prebuilts."""
Bob Haarman171d4092023-01-10 16:44:19 +0000310 output_proto.cls.extend(
311 sdk.CreateBinhostCLs(
312 input_proto.prepend_version,
313 input_proto.version,
314 input_proto.upload_location,
315 input_proto.sdk_tarball_template,
316 )
Alex Klein1699fab2022-09-08 08:46:06 -0600317 )
Bob Haarman338f8b72022-02-04 01:02:46 +0000318
319
320@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600321@validate.require("prepend_version", "version", "upload_location")
Bob Haarman338f8b72022-02-04 01:02:46 +0000322@validate.validation_complete
Bob Haarmand1225ea2022-01-19 22:01:42 +0000323def UploadPrebuiltPackages(input_proto, _output_proto, _config):
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700324 """Upload prebuilt packages."""
Alex Klein1699fab2022-09-08 08:46:06 -0600325 sdk.UploadPrebuiltPackages(
326 controller_util.ParseChroot(input_proto.chroot),
327 input_proto.prepend_version,
328 input_proto.version,
329 input_proto.upload_location,
330 )
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700331
332
333@faux.all_empty
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700334@validate.validation_complete
335def BuildSdkToolchain(input_proto, output_proto, _config):
336 """Build cross-compiler packages for the SDK."""
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700337 extra_env: Dict[str, str] = {}
338 if input_proto.use_flags:
339 extra_env["USE"] = " ".join(use.flag for use in input_proto.use_flags)
Greg Edelstone265a172023-06-26 17:20:05 -0600340 generated_files = sdk.BuildSdkToolchain(extra_env=extra_env)
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700341 output_proto.generated_files.extend(generated_files)