blob: 127d7af1bfc5b669c8505f9047c659c82e5346b2 [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
16from chromite.service import sdk
17
18
Alex Klein0996b462023-06-15 15:26:37 -060019if TYPE_CHECKING:
20 from chromite.api import api_config
21 from chromite.api.gen.chromite.api import sdk_pb2
22
23
Alex Klein076841b2019-08-29 15:19:39 -060024def _ChrootVersionResponse(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -060025 """Add a fake chroot version to a successful response."""
26 output_proto.version.version = 168
Alex Klein076841b2019-08-29 15:19:39 -060027
28
Bob Haarman171d4092023-01-10 16:44:19 +000029def _BinhostCLs(_input_proto, output_proto, _config):
30 """Add fake CL identifiers to a successful response."""
Bob Haarmanc0082602022-09-20 16:12:43 -070031 output_proto.cls = [
Bob Haarman171d4092023-01-10 16:44:19 +000032 "fakecl:1",
33 "fakecl:2",
Bob Haarmanc0082602022-09-20 16:12:43 -070034 ]
35
36
Bob Haarman0853ce92022-12-13 18:11:53 +000037def _BuildSdkTarballResponse(_input_proto, output_proto, _config):
38 """Populate a fake BuildSdkTarballResponse."""
39 output_proto.sdk_tarball_path.path = "/fake/sdk/tarball.tar.gz"
40 output_proto.sdk_tarball_path.location = common_pb2.Path.OUTSIDE
41
42
43@faux.success(_BuildSdkTarballResponse)
44@validate.require("chroot")
45@validate.validation_complete
46def BuildSdkTarball(
Alex Klein0996b462023-06-15 15:26:37 -060047 input_proto: "sdk_pb2.BuildSdkTarballRequest",
48 output_proto: "sdk_pb2.BuildSdkTarballResponse",
Bob Haarman0853ce92022-12-13 18:11:53 +000049 _config: "api_config.ApiConfig",
50) -> None:
51 chroot = controller_util.ParseChroot(input_proto.chroot)
52 output_proto.sdk_tarball_path.path = str(sdk.BuildSdkTarball(chroot))
53 output_proto.sdk_tarball_path.location = common_pb2.Path.OUTSIDE
54
55
Greg Edelston01ae5942023-01-30 16:26:54 -070056def _CreateManifestFromSdkResponse(_input_proto, output_proto, _config):
57 """Populate a fake CreateManifestFromSdkResponse."""
58 output_proto.manifest_path.path = "/fake/sdk/tarball.tar.gz.Manifest"
59 output_proto.manifest_path.location = common_pb2.Path.Location.INSIDE
60
61
62@faux.success(_CreateManifestFromSdkResponse)
63@validate.require("chroot")
64@validate.require("sdk_path")
65@validate.require("dest_dir")
66@validate.validation_complete
67def CreateManifestFromSdk(
Alex Klein0996b462023-06-15 15:26:37 -060068 input_proto: "sdk_pb2.CreateManifestFromSdkRequest",
69 output_proto: "sdk_pb2.CreateManifestFromSdkResponse",
Greg Edelston01ae5942023-01-30 16:26:54 -070070 _config: "api_config.ApiConfig",
71) -> None:
72 """Create a manifest file showing the ebuilds in an SDK."""
73
74 def _assert_path_is_absolute(path: str, name: str):
75 """Raise an exception if the given path is not absolute."""
76 if not os.path.isabs(path):
77 cros_build_lib.Die(f"The {name} must be absolute; got {path}")
78
79 _assert_path_is_absolute(input_proto.chroot.path, "chroot path")
80 _assert_path_is_absolute(input_proto.sdk_path.path, "SDK path")
81 _assert_path_is_absolute(input_proto.dest_dir.path, "destination directory")
82
Greg Edelston1f5deb62023-03-31 14:22:08 -060083 sdk_path = controller_util.pb2_path_to_pathlib_path(
Greg Edelston01ae5942023-01-30 16:26:54 -070084 input_proto.sdk_path, input_proto.chroot
85 )
Greg Edelston1f5deb62023-03-31 14:22:08 -060086 dest_dir = controller_util.pb2_path_to_pathlib_path(
Greg Edelston01ae5942023-01-30 16:26:54 -070087 input_proto.dest_dir, input_proto.chroot
88 )
89
90 manifest_path = sdk.CreateManifestFromSdk(sdk_path, dest_dir)
91 output_proto.manifest_path.path = str(manifest_path)
92 output_proto.manifest_path.location = common_pb2.Path.Location.OUTSIDE
93
94
Alex Klein076841b2019-08-29 15:19:39 -060095@faux.success(_ChrootVersionResponse)
96@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -060097def Create(
Alex Klein0996b462023-06-15 15:26:37 -060098 input_proto: "sdk_pb2.CreateRequest",
99 output_proto: "sdk_pb2.CreateResponse",
Alex Klein1699fab2022-09-08 08:46:06 -0600100 config: "api_config.ApiConfig",
101) -> Union[int, None]:
102 """Chroot creation, includes support for replacing an existing chroot.
Alex Klein19c4cc42019-02-27 14:47:57 -0700103
Alex Klein1699fab2022-09-08 08:46:06 -0600104 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600105 input_proto: The input proto.
106 output_proto: The output proto.
107 config: The API call config.
Kevin Shelton50dabff2022-04-09 11:29:53 -0700108
Alex Klein1699fab2022-09-08 08:46:06 -0600109 Returns:
Alex Klein611dddd2022-10-11 17:02:01 -0600110 An error code, None otherwise.
Alex Klein1699fab2022-09-08 08:46:06 -0600111 """
112 replace = not input_proto.flags.no_replace
113 bootstrap = input_proto.flags.bootstrap
Brian Norris67374d82023-05-01 12:31:12 -0700114 chroot = controller_util.ParseChroot(input_proto.chroot)
Alex Klein19c4cc42019-02-27 14:47:57 -0700115
Alex Klein1699fab2022-09-08 08:46:06 -0600116 sdk_version = input_proto.sdk_version
117 skip_chroot_upgrade = input_proto.skip_chroot_upgrade
George Burgess IV6e04d602023-08-16 15:08:32 -0600118 ccache_disable = input_proto.ccache_disable
Alex Klein19c4cc42019-02-27 14:47:57 -0700119
Alex Klein1699fab2022-09-08 08:46:06 -0600120 if config.validate_only:
121 return controller.RETURN_CODE_VALID_INPUT
Alex Klein231d2da2019-07-22 16:44:45 -0600122
Alex Klein1699fab2022-09-08 08:46:06 -0600123 args = sdk.CreateArguments(
124 replace=replace,
125 bootstrap=bootstrap,
Brian Norris67374d82023-05-01 12:31:12 -0700126 chroot=chroot,
Alex Klein1699fab2022-09-08 08:46:06 -0600127 sdk_version=sdk_version,
128 skip_chroot_upgrade=skip_chroot_upgrade,
George Burgess IV6e04d602023-08-16 15:08:32 -0600129 ccache_disable=ccache_disable,
Alex Klein1699fab2022-09-08 08:46:06 -0600130 )
Alex Klein19c4cc42019-02-27 14:47:57 -0700131
Alex Klein1699fab2022-09-08 08:46:06 -0600132 version = sdk.Create(args)
Alex Klein19c4cc42019-02-27 14:47:57 -0700133
Alex Klein1699fab2022-09-08 08:46:06 -0600134 if version:
135 output_proto.version.version = version
136 else:
137 # This should be very rare, if ever used, but worth noting.
138 cros_build_lib.Die(
139 "No chroot version could be found. There was likely an"
140 "error creating the chroot that was not detected."
141 )
Alex Kleinaa5c4172019-02-27 17:12:20 -0700142
143
Alex Klein076841b2019-08-29 15:19:39 -0600144@faux.success(_ChrootVersionResponse)
145@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600146@validate.require_each("toolchain_targets", ["name"])
Alex Klein231d2da2019-07-22 16:44:45 -0600147@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -0600148def Update(
Alex Klein0996b462023-06-15 15:26:37 -0600149 input_proto: "sdk_pb2.UpdateRequest",
150 output_proto: "sdk_pb2.UpdateResponse",
Alex Klein1699fab2022-09-08 08:46:06 -0600151 _config: "api_config.ApiConfig",
152):
153 """Update the chroot.
Alex Kleinaa5c4172019-02-27 17:12:20 -0700154
Alex Klein1699fab2022-09-08 08:46:06 -0600155 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600156 input_proto: The input proto.
157 output_proto: The output proto.
158 _config: The API call config.
Alex Klein1699fab2022-09-08 08:46:06 -0600159 """
160 build_source = input_proto.flags.build_source
161 targets = [target.name for target in input_proto.toolchain_targets]
162 toolchain_changed = input_proto.flags.toolchain_changed
Alex Kleinaa5c4172019-02-27 17:12:20 -0700163
Alex Klein1699fab2022-09-08 08:46:06 -0600164 args = sdk.UpdateArguments(
165 build_source=build_source,
166 toolchain_targets=targets,
167 toolchain_changed=toolchain_changed,
168 )
Chris McDonald68faa2a2020-01-13 12:23:05 -0700169
Alex Klein1699fab2022-09-08 08:46:06 -0600170 version = sdk.Update(args)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700171
Alex Klein1699fab2022-09-08 08:46:06 -0600172 if version:
173 output_proto.version.version = version
174 else:
175 # This should be very rare, if ever used, but worth noting.
176 cros_build_lib.Die(
177 "No chroot version could be found. There was likely an"
178 "error creating the chroot that was not detected."
179 )
Alex Klein730cf552019-10-16 11:28:22 -0600180
181
182@faux.all_empty
Greg Edelston6733dc52023-02-15 15:20:07 -0700183@validate.require("binhost_gs_bucket")
Greg Edelston0382ca42023-05-04 14:00:15 -0600184@validate.require("toolchain_tarball_template")
Greg Edelston6733dc52023-02-15 15:20:07 -0700185@validate.validation_complete
186def Uprev(input_proto, output_proto, _config):
Greg Edelston40aea812023-03-27 16:34:35 -0600187 """Update SDK version file and prebuilt files to point to the latest SDK.
188
189 Files will be changed locally, but not committed.
190 """
191 # If the UprevRequest did not specify a target version,
192 # check the remote SDK version file on Google Cloud Storage for the latest
193 # uprev target.
Greg Edelstond401e5a2023-04-28 15:29:11 -0600194 target_version = (
195 input_proto.version or sdk.get_latest_uprev_target_version()
196 )
Greg Edelston40aea812023-03-27 16:34:35 -0600197
198 # The main uprev logic occurs in service/sdk.py.
Greg Edelstond401e5a2023-04-28 15:29:11 -0600199 modified_files = sdk.uprev_sdk_and_prebuilts(
Greg Edelston6733dc52023-02-15 15:20:07 -0700200 binhost_gs_bucket=input_proto.binhost_gs_bucket,
Greg Edelston8bf6d4c2023-06-30 17:14:39 -0600201 sdk_version=target_version,
Greg Edelston0382ca42023-05-04 14:00:15 -0600202 toolchain_tarball_template=input_proto.toolchain_tarball_template,
Greg Edelston6733dc52023-02-15 15:20:07 -0700203 )
Greg Edelston40aea812023-03-27 16:34:35 -0600204
205 # Populate the UprevResponse object with the modified files.
Greg Edelston6733dc52023-02-15 15:20:07 -0700206 for modified_file in modified_files:
Greg Edelston40aea812023-03-27 16:34:35 -0600207 proto_path = output_proto.modified_files.add()
208 proto_path.path = str(modified_file)
209 proto_path.location = common_pb2.Path.OUTSIDE
Greg Edelston6733dc52023-02-15 15:20:07 -0700210 output_proto.version = target_version
211
212
213@faux.all_empty
Alex Klein730cf552019-10-16 11:28:22 -0600214@validate.validation_complete
215def Delete(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600216 """Delete a chroot."""
217 chroot = controller_util.ParseChroot(input_proto.chroot)
218 sdk.Delete(chroot, force=True)
Chris McDonald53ad5442020-01-17 14:11:55 -0700219
220
221@faux.all_empty
222@validate.validation_complete
Brian Norrisf624bf42023-03-02 12:57:49 -0800223def Unmount(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600224 """Unmount a chroot"""
Brian Norrisf624bf42023-03-02 12:57:49 -0800225 # Deprecated. Do nothing.
Chris McDonaldf48ea202020-01-29 13:19:23 -0700226
227
228@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600229@validate.require("path.path")
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600230@validate.validation_complete
231def UnmountPath(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600232 """Unmount a path"""
233 sdk.UnmountPath(input_proto.path.path)
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600234
235
236@faux.all_empty
Chris McDonaldf48ea202020-01-29 13:19:23 -0700237@validate.validation_complete
238def Clean(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600239 """Clean unneeded files from a chroot."""
240 chroot = controller_util.ParseChroot(input_proto.chroot)
George Engelbrecht86cfae62023-04-05 10:57:41 -0600241
242 # Default (flagless) call sets 'safe' and 'sysroots'.
243 if not (
244 input_proto.safe
245 or input_proto.images
246 or input_proto.sysroots
247 or input_proto.tmp
248 or input_proto.cache
249 or input_proto.logs
250 or input_proto.workdirs
251 or input_proto.incrementals
252 ):
253 sdk.Clean(chroot, safe=True, sysroots=True)
254 else:
255 sdk.Clean(
256 chroot,
257 safe=input_proto.safe,
258 images=input_proto.images,
259 sysroots=input_proto.sysroots,
260 tmp=input_proto.tmp,
261 cache=input_proto.cache,
262 logs=input_proto.logs,
263 workdirs=input_proto.workdirs,
264 incrementals=input_proto.incrementals,
265 )
Chris McDonald9d486802020-01-29 15:57:22 -0700266
267
268@faux.all_empty
269@validate.validation_complete
Bob Haarmand1225ea2022-01-19 22:01:42 +0000270def BuildPrebuilts(input_proto, _output_proto, _config):
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700271 """Build the binary packages that comprise the Chromium OS SDK."""
Alex Klein1699fab2022-09-08 08:46:06 -0600272 chroot = controller_util.ParseChroot(input_proto.chroot)
Greg Edelston199c5362023-01-04 12:29:07 -0700273 sdk.BuildPrebuilts(chroot, board=input_proto.build_target.name)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000274
275
Bob Haarman171d4092023-01-10 16:44:19 +0000276@faux.success(_BinhostCLs)
Bob Haarmanc0082602022-09-20 16:12:43 -0700277@faux.empty_error
Bob Haarman0853ce92022-12-13 18:11:53 +0000278@validate.require(
279 "prepend_version", "version", "upload_location", "sdk_tarball_template"
280)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000281@validate.validation_complete
Bob Haarmanc0082602022-09-20 16:12:43 -0700282def CreateBinhostCLs(
Alex Klein0996b462023-06-15 15:26:37 -0600283 input_proto: "sdk_pb2.CreateBinhostCLsRequest",
284 output_proto: "sdk_pb2.CreateBinhostCLsResponse",
Bob Haarmanc0082602022-09-20 16:12:43 -0700285 _config: "api_config.ApiConfig",
286) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600287 """Create CLs to update the binhost to point at uploaded prebuilts."""
Bob Haarman171d4092023-01-10 16:44:19 +0000288 output_proto.cls.extend(
289 sdk.CreateBinhostCLs(
290 input_proto.prepend_version,
291 input_proto.version,
292 input_proto.upload_location,
293 input_proto.sdk_tarball_template,
294 )
Alex Klein1699fab2022-09-08 08:46:06 -0600295 )
Bob Haarman338f8b72022-02-04 01:02:46 +0000296
297
298@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600299@validate.require("prepend_version", "version", "upload_location")
Bob Haarman338f8b72022-02-04 01:02:46 +0000300@validate.validation_complete
Bob Haarmand1225ea2022-01-19 22:01:42 +0000301def UploadPrebuiltPackages(input_proto, _output_proto, _config):
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700302 """Upload prebuilt packages."""
Alex Klein1699fab2022-09-08 08:46:06 -0600303 sdk.UploadPrebuiltPackages(
304 controller_util.ParseChroot(input_proto.chroot),
305 input_proto.prepend_version,
306 input_proto.version,
307 input_proto.upload_location,
308 )
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700309
310
311@faux.all_empty
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700312@validate.validation_complete
313def BuildSdkToolchain(input_proto, output_proto, _config):
314 """Build cross-compiler packages for the SDK."""
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700315 extra_env: Dict[str, str] = {}
316 if input_proto.use_flags:
317 extra_env["USE"] = " ".join(use.flag for use in input_proto.use_flags)
Greg Edelstone265a172023-06-26 17:20:05 -0600318 generated_files = sdk.BuildSdkToolchain(extra_env=extra_env)
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700319 output_proto.generated_files.extend(generated_files)