blob: c4ed316b7b35dfd804c7d87662e5c1e98d4eb766 [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 Edelston9dcdc8a2023-01-11 17:07:10 -07008from typing import Dict, 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 Klein076841b2019-08-29 15:19:39 -060019def _ChrootVersionResponse(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -060020 """Add a fake chroot version to a successful response."""
21 output_proto.version.version = 168
Alex Klein076841b2019-08-29 15:19:39 -060022
23
Bob Haarman171d4092023-01-10 16:44:19 +000024def _BinhostCLs(_input_proto, output_proto, _config):
25 """Add fake CL identifiers to a successful response."""
Bob Haarmanc0082602022-09-20 16:12:43 -070026 output_proto.cls = [
Bob Haarman171d4092023-01-10 16:44:19 +000027 "fakecl:1",
28 "fakecl:2",
Bob Haarmanc0082602022-09-20 16:12:43 -070029 ]
30
31
Bob Haarman0853ce92022-12-13 18:11:53 +000032def _BuildSdkTarballResponse(_input_proto, output_proto, _config):
33 """Populate a fake BuildSdkTarballResponse."""
34 output_proto.sdk_tarball_path.path = "/fake/sdk/tarball.tar.gz"
35 output_proto.sdk_tarball_path.location = common_pb2.Path.OUTSIDE
36
37
38@faux.success(_BuildSdkTarballResponse)
39@validate.require("chroot")
40@validate.validation_complete
41def BuildSdkTarball(
42 input_proto: "BuildSdkTarballRequest",
43 output_proto: "BuildSdkTarballResponse",
44 _config: "api_config.ApiConfig",
45) -> None:
46 chroot = controller_util.ParseChroot(input_proto.chroot)
47 output_proto.sdk_tarball_path.path = str(sdk.BuildSdkTarball(chroot))
48 output_proto.sdk_tarball_path.location = common_pb2.Path.OUTSIDE
49
50
Greg Edelston01ae5942023-01-30 16:26:54 -070051def _CreateManifestFromSdkResponse(_input_proto, output_proto, _config):
52 """Populate a fake CreateManifestFromSdkResponse."""
53 output_proto.manifest_path.path = "/fake/sdk/tarball.tar.gz.Manifest"
54 output_proto.manifest_path.location = common_pb2.Path.Location.INSIDE
55
56
57@faux.success(_CreateManifestFromSdkResponse)
58@validate.require("chroot")
59@validate.require("sdk_path")
60@validate.require("dest_dir")
61@validate.validation_complete
62def CreateManifestFromSdk(
63 input_proto: "CreateManifestFromSdkRequest",
64 output_proto: "CreateManifestFromSdkResponse",
65 _config: "api_config.ApiConfig",
66) -> None:
67 """Create a manifest file showing the ebuilds in an SDK."""
68
69 def _assert_path_is_absolute(path: str, name: str):
70 """Raise an exception if the given path is not absolute."""
71 if not os.path.isabs(path):
72 cros_build_lib.Die(f"The {name} must be absolute; got {path}")
73
74 _assert_path_is_absolute(input_proto.chroot.path, "chroot path")
75 _assert_path_is_absolute(input_proto.sdk_path.path, "SDK path")
76 _assert_path_is_absolute(input_proto.dest_dir.path, "destination directory")
77
Greg Edelston1f5deb62023-03-31 14:22:08 -060078 sdk_path = controller_util.pb2_path_to_pathlib_path(
Greg Edelston01ae5942023-01-30 16:26:54 -070079 input_proto.sdk_path, input_proto.chroot
80 )
Greg Edelston1f5deb62023-03-31 14:22:08 -060081 dest_dir = controller_util.pb2_path_to_pathlib_path(
Greg Edelston01ae5942023-01-30 16:26:54 -070082 input_proto.dest_dir, input_proto.chroot
83 )
84
85 manifest_path = sdk.CreateManifestFromSdk(sdk_path, dest_dir)
86 output_proto.manifest_path.path = str(manifest_path)
87 output_proto.manifest_path.location = common_pb2.Path.Location.OUTSIDE
88
89
Alex Klein076841b2019-08-29 15:19:39 -060090@faux.success(_ChrootVersionResponse)
91@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -060092def Create(
93 input_proto: "CreateRequest",
94 output_proto: "CreateResponse",
95 config: "api_config.ApiConfig",
96) -> Union[int, None]:
97 """Chroot creation, includes support for replacing an existing chroot.
Alex Klein19c4cc42019-02-27 14:47:57 -070098
Alex Klein1699fab2022-09-08 08:46:06 -060099 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600100 input_proto: The input proto.
101 output_proto: The output proto.
102 config: The API call config.
Kevin Shelton50dabff2022-04-09 11:29:53 -0700103
Alex Klein1699fab2022-09-08 08:46:06 -0600104 Returns:
Alex Klein611dddd2022-10-11 17:02:01 -0600105 An error code, None otherwise.
Alex Klein1699fab2022-09-08 08:46:06 -0600106 """
107 replace = not input_proto.flags.no_replace
108 bootstrap = input_proto.flags.bootstrap
Brian Norris67374d82023-05-01 12:31:12 -0700109 chroot = controller_util.ParseChroot(input_proto.chroot)
Alex Klein19c4cc42019-02-27 14:47:57 -0700110
Alex Klein1699fab2022-09-08 08:46:06 -0600111 sdk_version = input_proto.sdk_version
112 skip_chroot_upgrade = input_proto.skip_chroot_upgrade
Alex Klein19c4cc42019-02-27 14:47:57 -0700113
Alex Klein1699fab2022-09-08 08:46:06 -0600114 if config.validate_only:
115 return controller.RETURN_CODE_VALID_INPUT
Alex Klein231d2da2019-07-22 16:44:45 -0600116
Alex Klein1699fab2022-09-08 08:46:06 -0600117 args = sdk.CreateArguments(
118 replace=replace,
119 bootstrap=bootstrap,
Brian Norris67374d82023-05-01 12:31:12 -0700120 chroot=chroot,
Alex Klein1699fab2022-09-08 08:46:06 -0600121 sdk_version=sdk_version,
122 skip_chroot_upgrade=skip_chroot_upgrade,
123 )
Alex Klein19c4cc42019-02-27 14:47:57 -0700124
Alex Klein1699fab2022-09-08 08:46:06 -0600125 version = sdk.Create(args)
Alex Klein19c4cc42019-02-27 14:47:57 -0700126
Alex Klein1699fab2022-09-08 08:46:06 -0600127 if version:
128 output_proto.version.version = version
129 else:
130 # This should be very rare, if ever used, but worth noting.
131 cros_build_lib.Die(
132 "No chroot version could be found. There was likely an"
133 "error creating the chroot that was not detected."
134 )
Alex Kleinaa5c4172019-02-27 17:12:20 -0700135
136
Alex Klein076841b2019-08-29 15:19:39 -0600137@faux.success(_ChrootVersionResponse)
138@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600139@validate.require_each("toolchain_targets", ["name"])
Alex Klein231d2da2019-07-22 16:44:45 -0600140@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -0600141def Update(
142 input_proto: "UpdateRequest",
143 output_proto: "UpdateResponse",
144 _config: "api_config.ApiConfig",
145):
146 """Update the chroot.
Alex Kleinaa5c4172019-02-27 17:12:20 -0700147
Alex Klein1699fab2022-09-08 08:46:06 -0600148 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600149 input_proto: The input proto.
150 output_proto: The output proto.
151 _config: The API call config.
Alex Klein1699fab2022-09-08 08:46:06 -0600152 """
153 build_source = input_proto.flags.build_source
154 targets = [target.name for target in input_proto.toolchain_targets]
155 toolchain_changed = input_proto.flags.toolchain_changed
Alex Kleinaa5c4172019-02-27 17:12:20 -0700156
Alex Klein1699fab2022-09-08 08:46:06 -0600157 args = sdk.UpdateArguments(
158 build_source=build_source,
159 toolchain_targets=targets,
160 toolchain_changed=toolchain_changed,
161 )
Chris McDonald68faa2a2020-01-13 12:23:05 -0700162
Alex Klein1699fab2022-09-08 08:46:06 -0600163 version = sdk.Update(args)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700164
Alex Klein1699fab2022-09-08 08:46:06 -0600165 if version:
166 output_proto.version.version = version
167 else:
168 # This should be very rare, if ever used, but worth noting.
169 cros_build_lib.Die(
170 "No chroot version could be found. There was likely an"
171 "error creating the chroot that was not detected."
172 )
Alex Klein730cf552019-10-16 11:28:22 -0600173
174
175@faux.all_empty
Greg Edelston6733dc52023-02-15 15:20:07 -0700176@validate.require("binhost_gs_bucket")
Greg Edelston0382ca42023-05-04 14:00:15 -0600177@validate.require("toolchain_tarball_template")
Greg Edelston6733dc52023-02-15 15:20:07 -0700178@validate.validation_complete
179def Uprev(input_proto, output_proto, _config):
Greg Edelston40aea812023-03-27 16:34:35 -0600180 """Update SDK version file and prebuilt files to point to the latest SDK.
181
182 Files will be changed locally, but not committed.
183 """
184 # If the UprevRequest did not specify a target version,
185 # check the remote SDK version file on Google Cloud Storage for the latest
186 # uprev target.
Greg Edelstond401e5a2023-04-28 15:29:11 -0600187 target_version = (
188 input_proto.version or sdk.get_latest_uprev_target_version()
189 )
Greg Edelston40aea812023-03-27 16:34:35 -0600190
191 # The main uprev logic occurs in service/sdk.py.
Greg Edelstond401e5a2023-04-28 15:29:11 -0600192 modified_files = sdk.uprev_sdk_and_prebuilts(
Greg Edelston6733dc52023-02-15 15:20:07 -0700193 binhost_gs_bucket=input_proto.binhost_gs_bucket,
194 version=target_version,
Greg Edelston0382ca42023-05-04 14:00:15 -0600195 toolchain_tarball_template=input_proto.toolchain_tarball_template,
Greg Edelston6733dc52023-02-15 15:20:07 -0700196 )
Greg Edelston40aea812023-03-27 16:34:35 -0600197
198 # Populate the UprevResponse object with the modified files.
Greg Edelston6733dc52023-02-15 15:20:07 -0700199 for modified_file in modified_files:
Greg Edelston40aea812023-03-27 16:34:35 -0600200 proto_path = output_proto.modified_files.add()
201 proto_path.path = str(modified_file)
202 proto_path.location = common_pb2.Path.OUTSIDE
Greg Edelston6733dc52023-02-15 15:20:07 -0700203 output_proto.version = target_version
204
205
206@faux.all_empty
Alex Klein730cf552019-10-16 11:28:22 -0600207@validate.validation_complete
208def Delete(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600209 """Delete a chroot."""
210 chroot = controller_util.ParseChroot(input_proto.chroot)
211 sdk.Delete(chroot, force=True)
Chris McDonald53ad5442020-01-17 14:11:55 -0700212
213
214@faux.all_empty
215@validate.validation_complete
Brian Norrisf624bf42023-03-02 12:57:49 -0800216def Unmount(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600217 """Unmount a chroot"""
Brian Norrisf624bf42023-03-02 12:57:49 -0800218 # Deprecated. Do nothing.
Chris McDonaldf48ea202020-01-29 13:19:23 -0700219
220
221@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600222@validate.require("path.path")
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600223@validate.validation_complete
224def UnmountPath(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600225 """Unmount a path"""
226 sdk.UnmountPath(input_proto.path.path)
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600227
228
229@faux.all_empty
Chris McDonaldf48ea202020-01-29 13:19:23 -0700230@validate.validation_complete
231def Clean(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600232 """Clean unneeded files from a chroot."""
233 chroot = controller_util.ParseChroot(input_proto.chroot)
George Engelbrecht86cfae62023-04-05 10:57:41 -0600234
235 # Default (flagless) call sets 'safe' and 'sysroots'.
236 if not (
237 input_proto.safe
238 or input_proto.images
239 or input_proto.sysroots
240 or input_proto.tmp
241 or input_proto.cache
242 or input_proto.logs
243 or input_proto.workdirs
244 or input_proto.incrementals
245 ):
246 sdk.Clean(chroot, safe=True, sysroots=True)
247 else:
248 sdk.Clean(
249 chroot,
250 safe=input_proto.safe,
251 images=input_proto.images,
252 sysroots=input_proto.sysroots,
253 tmp=input_proto.tmp,
254 cache=input_proto.cache,
255 logs=input_proto.logs,
256 workdirs=input_proto.workdirs,
257 incrementals=input_proto.incrementals,
258 )
Chris McDonald9d486802020-01-29 15:57:22 -0700259
260
261@faux.all_empty
262@validate.validation_complete
Bob Haarmand1225ea2022-01-19 22:01:42 +0000263def BuildPrebuilts(input_proto, _output_proto, _config):
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700264 """Build the binary packages that comprise the Chromium OS SDK."""
Alex Klein1699fab2022-09-08 08:46:06 -0600265 chroot = controller_util.ParseChroot(input_proto.chroot)
Greg Edelston199c5362023-01-04 12:29:07 -0700266 sdk.BuildPrebuilts(chroot, board=input_proto.build_target.name)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000267
268
Bob Haarman171d4092023-01-10 16:44:19 +0000269@faux.success(_BinhostCLs)
Bob Haarmanc0082602022-09-20 16:12:43 -0700270@faux.empty_error
Bob Haarman0853ce92022-12-13 18:11:53 +0000271@validate.require(
272 "prepend_version", "version", "upload_location", "sdk_tarball_template"
273)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000274@validate.validation_complete
Bob Haarmanc0082602022-09-20 16:12:43 -0700275def CreateBinhostCLs(
276 input_proto: "CreateBinhostCLsRequest",
277 output_proto: "CreateBinhostCLsResponse",
278 _config: "api_config.ApiConfig",
279) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600280 """Create CLs to update the binhost to point at uploaded prebuilts."""
Bob Haarman171d4092023-01-10 16:44:19 +0000281 output_proto.cls.extend(
282 sdk.CreateBinhostCLs(
283 input_proto.prepend_version,
284 input_proto.version,
285 input_proto.upload_location,
286 input_proto.sdk_tarball_template,
287 )
Alex Klein1699fab2022-09-08 08:46:06 -0600288 )
Bob Haarman338f8b72022-02-04 01:02:46 +0000289
290
291@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600292@validate.require("prepend_version", "version", "upload_location")
Bob Haarman338f8b72022-02-04 01:02:46 +0000293@validate.validation_complete
Bob Haarmand1225ea2022-01-19 22:01:42 +0000294def UploadPrebuiltPackages(input_proto, _output_proto, _config):
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700295 """Upload prebuilt packages."""
Alex Klein1699fab2022-09-08 08:46:06 -0600296 sdk.UploadPrebuiltPackages(
297 controller_util.ParseChroot(input_proto.chroot),
298 input_proto.prepend_version,
299 input_proto.version,
300 input_proto.upload_location,
301 )
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700302
303
304@faux.all_empty
305@validate.require("chroot")
306@validate.validation_complete
307def BuildSdkToolchain(input_proto, output_proto, _config):
308 """Build cross-compiler packages for the SDK."""
309 chroot = controller_util.ParseChroot(input_proto.chroot)
310 extra_env: Dict[str, str] = {}
311 if input_proto.use_flags:
312 extra_env["USE"] = " ".join(use.flag for use in input_proto.use_flags)
313 generated_files = sdk.BuildSdkToolchain(chroot, extra_env=extra_env)
314 output_proto.generated_files.extend(generated_files)