blob: f0d9704fff88a26ebc7f08675d1daf7901f337bd [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
Alex Klein19c4cc42019-02-27 14:47:57 -0700118
Alex Klein1699fab2022-09-08 08:46:06 -0600119 if config.validate_only:
120 return controller.RETURN_CODE_VALID_INPUT
Alex Klein231d2da2019-07-22 16:44:45 -0600121
Alex Klein1699fab2022-09-08 08:46:06 -0600122 args = sdk.CreateArguments(
123 replace=replace,
124 bootstrap=bootstrap,
Brian Norris67374d82023-05-01 12:31:12 -0700125 chroot=chroot,
Alex Klein1699fab2022-09-08 08:46:06 -0600126 sdk_version=sdk_version,
127 skip_chroot_upgrade=skip_chroot_upgrade,
128 )
Alex Klein19c4cc42019-02-27 14:47:57 -0700129
Alex Klein1699fab2022-09-08 08:46:06 -0600130 version = sdk.Create(args)
Alex Klein19c4cc42019-02-27 14:47:57 -0700131
Alex Klein1699fab2022-09-08 08:46:06 -0600132 if version:
133 output_proto.version.version = version
134 else:
135 # This should be very rare, if ever used, but worth noting.
136 cros_build_lib.Die(
137 "No chroot version could be found. There was likely an"
138 "error creating the chroot that was not detected."
139 )
Alex Kleinaa5c4172019-02-27 17:12:20 -0700140
141
Alex Klein076841b2019-08-29 15:19:39 -0600142@faux.success(_ChrootVersionResponse)
143@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600144@validate.require_each("toolchain_targets", ["name"])
Alex Klein231d2da2019-07-22 16:44:45 -0600145@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -0600146def Update(
Alex Klein0996b462023-06-15 15:26:37 -0600147 input_proto: "sdk_pb2.UpdateRequest",
148 output_proto: "sdk_pb2.UpdateResponse",
Alex Klein1699fab2022-09-08 08:46:06 -0600149 _config: "api_config.ApiConfig",
150):
151 """Update the chroot.
Alex Kleinaa5c4172019-02-27 17:12:20 -0700152
Alex Klein1699fab2022-09-08 08:46:06 -0600153 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600154 input_proto: The input proto.
155 output_proto: The output proto.
156 _config: The API call config.
Alex Klein1699fab2022-09-08 08:46:06 -0600157 """
158 build_source = input_proto.flags.build_source
159 targets = [target.name for target in input_proto.toolchain_targets]
160 toolchain_changed = input_proto.flags.toolchain_changed
Alex Kleinaa5c4172019-02-27 17:12:20 -0700161
Alex Klein1699fab2022-09-08 08:46:06 -0600162 args = sdk.UpdateArguments(
163 build_source=build_source,
164 toolchain_targets=targets,
165 toolchain_changed=toolchain_changed,
166 )
Chris McDonald68faa2a2020-01-13 12:23:05 -0700167
Alex Klein1699fab2022-09-08 08:46:06 -0600168 version = sdk.Update(args)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700169
Alex Klein1699fab2022-09-08 08:46:06 -0600170 if version:
171 output_proto.version.version = version
172 else:
173 # This should be very rare, if ever used, but worth noting.
174 cros_build_lib.Die(
175 "No chroot version could be found. There was likely an"
176 "error creating the chroot that was not detected."
177 )
Alex Klein730cf552019-10-16 11:28:22 -0600178
179
180@faux.all_empty
Greg Edelston6733dc52023-02-15 15:20:07 -0700181@validate.require("binhost_gs_bucket")
Greg Edelston0382ca42023-05-04 14:00:15 -0600182@validate.require("toolchain_tarball_template")
Greg Edelston6733dc52023-02-15 15:20:07 -0700183@validate.validation_complete
184def Uprev(input_proto, output_proto, _config):
Greg Edelston40aea812023-03-27 16:34:35 -0600185 """Update SDK version file and prebuilt files to point to the latest SDK.
186
187 Files will be changed locally, but not committed.
188 """
189 # If the UprevRequest did not specify a target version,
190 # check the remote SDK version file on Google Cloud Storage for the latest
191 # uprev target.
Greg Edelstond401e5a2023-04-28 15:29:11 -0600192 target_version = (
193 input_proto.version or sdk.get_latest_uprev_target_version()
194 )
Greg Edelston40aea812023-03-27 16:34:35 -0600195
196 # The main uprev logic occurs in service/sdk.py.
Greg Edelstond401e5a2023-04-28 15:29:11 -0600197 modified_files = sdk.uprev_sdk_and_prebuilts(
Greg Edelston6733dc52023-02-15 15:20:07 -0700198 binhost_gs_bucket=input_proto.binhost_gs_bucket,
199 version=target_version,
Greg Edelston0382ca42023-05-04 14:00:15 -0600200 toolchain_tarball_template=input_proto.toolchain_tarball_template,
Greg Edelston6733dc52023-02-15 15:20:07 -0700201 )
Greg Edelston40aea812023-03-27 16:34:35 -0600202
203 # Populate the UprevResponse object with the modified files.
Greg Edelston6733dc52023-02-15 15:20:07 -0700204 for modified_file in modified_files:
Greg Edelston40aea812023-03-27 16:34:35 -0600205 proto_path = output_proto.modified_files.add()
206 proto_path.path = str(modified_file)
207 proto_path.location = common_pb2.Path.OUTSIDE
Greg Edelston6733dc52023-02-15 15:20:07 -0700208 output_proto.version = target_version
209
210
211@faux.all_empty
Alex Klein730cf552019-10-16 11:28:22 -0600212@validate.validation_complete
213def Delete(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600214 """Delete a chroot."""
215 chroot = controller_util.ParseChroot(input_proto.chroot)
216 sdk.Delete(chroot, force=True)
Chris McDonald53ad5442020-01-17 14:11:55 -0700217
218
219@faux.all_empty
220@validate.validation_complete
Brian Norrisf624bf42023-03-02 12:57:49 -0800221def Unmount(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600222 """Unmount a chroot"""
Brian Norrisf624bf42023-03-02 12:57:49 -0800223 # Deprecated. Do nothing.
Chris McDonaldf48ea202020-01-29 13:19:23 -0700224
225
226@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600227@validate.require("path.path")
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600228@validate.validation_complete
229def UnmountPath(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600230 """Unmount a path"""
231 sdk.UnmountPath(input_proto.path.path)
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600232
233
234@faux.all_empty
Chris McDonaldf48ea202020-01-29 13:19:23 -0700235@validate.validation_complete
236def Clean(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600237 """Clean unneeded files from a chroot."""
238 chroot = controller_util.ParseChroot(input_proto.chroot)
George Engelbrecht86cfae62023-04-05 10:57:41 -0600239
240 # Default (flagless) call sets 'safe' and 'sysroots'.
241 if not (
242 input_proto.safe
243 or input_proto.images
244 or input_proto.sysroots
245 or input_proto.tmp
246 or input_proto.cache
247 or input_proto.logs
248 or input_proto.workdirs
249 or input_proto.incrementals
250 ):
251 sdk.Clean(chroot, safe=True, sysroots=True)
252 else:
253 sdk.Clean(
254 chroot,
255 safe=input_proto.safe,
256 images=input_proto.images,
257 sysroots=input_proto.sysroots,
258 tmp=input_proto.tmp,
259 cache=input_proto.cache,
260 logs=input_proto.logs,
261 workdirs=input_proto.workdirs,
262 incrementals=input_proto.incrementals,
263 )
Chris McDonald9d486802020-01-29 15:57:22 -0700264
265
266@faux.all_empty
267@validate.validation_complete
Bob Haarmand1225ea2022-01-19 22:01:42 +0000268def BuildPrebuilts(input_proto, _output_proto, _config):
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700269 """Build the binary packages that comprise the Chromium OS SDK."""
Alex Klein1699fab2022-09-08 08:46:06 -0600270 chroot = controller_util.ParseChroot(input_proto.chroot)
Greg Edelston199c5362023-01-04 12:29:07 -0700271 sdk.BuildPrebuilts(chroot, board=input_proto.build_target.name)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000272
273
Bob Haarman171d4092023-01-10 16:44:19 +0000274@faux.success(_BinhostCLs)
Bob Haarmanc0082602022-09-20 16:12:43 -0700275@faux.empty_error
Bob Haarman0853ce92022-12-13 18:11:53 +0000276@validate.require(
277 "prepend_version", "version", "upload_location", "sdk_tarball_template"
278)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000279@validate.validation_complete
Bob Haarmanc0082602022-09-20 16:12:43 -0700280def CreateBinhostCLs(
Alex Klein0996b462023-06-15 15:26:37 -0600281 input_proto: "sdk_pb2.CreateBinhostCLsRequest",
282 output_proto: "sdk_pb2.CreateBinhostCLsResponse",
Bob Haarmanc0082602022-09-20 16:12:43 -0700283 _config: "api_config.ApiConfig",
284) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600285 """Create CLs to update the binhost to point at uploaded prebuilts."""
Bob Haarman171d4092023-01-10 16:44:19 +0000286 output_proto.cls.extend(
287 sdk.CreateBinhostCLs(
288 input_proto.prepend_version,
289 input_proto.version,
290 input_proto.upload_location,
291 input_proto.sdk_tarball_template,
292 )
Alex Klein1699fab2022-09-08 08:46:06 -0600293 )
Bob Haarman338f8b72022-02-04 01:02:46 +0000294
295
296@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600297@validate.require("prepend_version", "version", "upload_location")
Bob Haarman338f8b72022-02-04 01:02:46 +0000298@validate.validation_complete
Bob Haarmand1225ea2022-01-19 22:01:42 +0000299def UploadPrebuiltPackages(input_proto, _output_proto, _config):
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700300 """Upload prebuilt packages."""
Alex Klein1699fab2022-09-08 08:46:06 -0600301 sdk.UploadPrebuiltPackages(
302 controller_util.ParseChroot(input_proto.chroot),
303 input_proto.prepend_version,
304 input_proto.version,
305 input_proto.upload_location,
306 )
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700307
308
309@faux.all_empty
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700310@validate.validation_complete
311def BuildSdkToolchain(input_proto, output_proto, _config):
312 """Build cross-compiler packages for the SDK."""
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700313 extra_env: Dict[str, str] = {}
314 if input_proto.use_flags:
315 extra_env["USE"] = " ".join(use.flag for use in input_proto.use_flags)
Greg Edelstone265a172023-06-26 17:20:05 -0600316 generated_files = sdk.BuildSdkToolchain(extra_env=extra_env)
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700317 output_proto.generated_files.extend(generated_files)