blob: d4c2ace2d5d0493b1b1ab2a923712fc464cc2ff9 [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
Alex Klein19c4cc42019-02-27 14:47:57 -0700109
Alex Klein1699fab2022-09-08 08:46:06 -0600110 chroot_path = input_proto.chroot.path
111 cache_dir = input_proto.chroot.cache_dir
112 sdk_version = input_proto.sdk_version
113 skip_chroot_upgrade = input_proto.skip_chroot_upgrade
Alex Klein19c4cc42019-02-27 14:47:57 -0700114
Alex Klein1699fab2022-09-08 08:46:06 -0600115 if chroot_path and not os.path.isabs(chroot_path):
116 cros_build_lib.Die("The chroot path must be absolute.")
Alex Klein19c4cc42019-02-27 14:47:57 -0700117
Alex Klein1699fab2022-09-08 08:46:06 -0600118 if config.validate_only:
119 return controller.RETURN_CODE_VALID_INPUT
Alex Klein231d2da2019-07-22 16:44:45 -0600120
Alex Klein1699fab2022-09-08 08:46:06 -0600121 args = sdk.CreateArguments(
122 replace=replace,
123 bootstrap=bootstrap,
Alex Klein1699fab2022-09-08 08:46:06 -0600124 cache_dir=cache_dir,
125 chroot_path=chroot_path,
126 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(
147 input_proto: "UpdateRequest",
148 output_proto: "UpdateResponse",
149 _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")
182@validate.validation_complete
183def Uprev(input_proto, output_proto, _config):
Greg Edelston40aea812023-03-27 16:34:35 -0600184 """Update SDK version file and prebuilt files to point to the latest SDK.
185
186 Files will be changed locally, but not committed.
187 """
188 # If the UprevRequest did not specify a target version,
189 # check the remote SDK version file on Google Cloud Storage for the latest
190 # uprev target.
Greg Edelstond401e5a2023-04-28 15:29:11 -0600191 target_version = (
192 input_proto.version or sdk.get_latest_uprev_target_version()
193 )
Greg Edelston40aea812023-03-27 16:34:35 -0600194
195 # The main uprev logic occurs in service/sdk.py.
Greg Edelstond401e5a2023-04-28 15:29:11 -0600196 modified_files = sdk.uprev_sdk_and_prebuilts(
Greg Edelston6733dc52023-02-15 15:20:07 -0700197 binhost_gs_bucket=input_proto.binhost_gs_bucket,
198 version=target_version,
199 )
Greg Edelston40aea812023-03-27 16:34:35 -0600200
201 # Populate the UprevResponse object with the modified files.
Greg Edelston6733dc52023-02-15 15:20:07 -0700202 for modified_file in modified_files:
Greg Edelston40aea812023-03-27 16:34:35 -0600203 proto_path = output_proto.modified_files.add()
204 proto_path.path = str(modified_file)
205 proto_path.location = common_pb2.Path.OUTSIDE
Greg Edelston6733dc52023-02-15 15:20:07 -0700206 output_proto.version = target_version
207
208
209@faux.all_empty
Alex Klein730cf552019-10-16 11:28:22 -0600210@validate.validation_complete
211def Delete(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600212 """Delete a chroot."""
213 chroot = controller_util.ParseChroot(input_proto.chroot)
214 sdk.Delete(chroot, force=True)
Chris McDonald53ad5442020-01-17 14:11:55 -0700215
216
217@faux.all_empty
218@validate.validation_complete
Brian Norrisf624bf42023-03-02 12:57:49 -0800219def Unmount(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600220 """Unmount a chroot"""
Brian Norrisf624bf42023-03-02 12:57:49 -0800221 # Deprecated. Do nothing.
Chris McDonaldf48ea202020-01-29 13:19:23 -0700222
223
224@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600225@validate.require("path.path")
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600226@validate.validation_complete
227def UnmountPath(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600228 """Unmount a path"""
229 sdk.UnmountPath(input_proto.path.path)
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600230
231
232@faux.all_empty
Chris McDonaldf48ea202020-01-29 13:19:23 -0700233@validate.validation_complete
234def Clean(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600235 """Clean unneeded files from a chroot."""
236 chroot = controller_util.ParseChroot(input_proto.chroot)
George Engelbrecht86cfae62023-04-05 10:57:41 -0600237
238 # Default (flagless) call sets 'safe' and 'sysroots'.
239 if not (
240 input_proto.safe
241 or input_proto.images
242 or input_proto.sysroots
243 or input_proto.tmp
244 or input_proto.cache
245 or input_proto.logs
246 or input_proto.workdirs
247 or input_proto.incrementals
248 ):
249 sdk.Clean(chroot, safe=True, sysroots=True)
250 else:
251 sdk.Clean(
252 chroot,
253 safe=input_proto.safe,
254 images=input_proto.images,
255 sysroots=input_proto.sysroots,
256 tmp=input_proto.tmp,
257 cache=input_proto.cache,
258 logs=input_proto.logs,
259 workdirs=input_proto.workdirs,
260 incrementals=input_proto.incrementals,
261 )
Chris McDonald9d486802020-01-29 15:57:22 -0700262
263
264@faux.all_empty
265@validate.validation_complete
Bob Haarmand1225ea2022-01-19 22:01:42 +0000266def BuildPrebuilts(input_proto, _output_proto, _config):
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700267 """Build the binary packages that comprise the Chromium OS SDK."""
Alex Klein1699fab2022-09-08 08:46:06 -0600268 chroot = controller_util.ParseChroot(input_proto.chroot)
Greg Edelston199c5362023-01-04 12:29:07 -0700269 sdk.BuildPrebuilts(chroot, board=input_proto.build_target.name)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000270
271
Bob Haarman171d4092023-01-10 16:44:19 +0000272@faux.success(_BinhostCLs)
Bob Haarmanc0082602022-09-20 16:12:43 -0700273@faux.empty_error
Bob Haarman0853ce92022-12-13 18:11:53 +0000274@validate.require(
275 "prepend_version", "version", "upload_location", "sdk_tarball_template"
276)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000277@validate.validation_complete
Bob Haarmanc0082602022-09-20 16:12:43 -0700278def CreateBinhostCLs(
279 input_proto: "CreateBinhostCLsRequest",
280 output_proto: "CreateBinhostCLsResponse",
281 _config: "api_config.ApiConfig",
282) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600283 """Create CLs to update the binhost to point at uploaded prebuilts."""
Bob Haarman171d4092023-01-10 16:44:19 +0000284 output_proto.cls.extend(
285 sdk.CreateBinhostCLs(
286 input_proto.prepend_version,
287 input_proto.version,
288 input_proto.upload_location,
289 input_proto.sdk_tarball_template,
290 )
Alex Klein1699fab2022-09-08 08:46:06 -0600291 )
Bob Haarman338f8b72022-02-04 01:02:46 +0000292
293
294@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600295@validate.require("prepend_version", "version", "upload_location")
Bob Haarman338f8b72022-02-04 01:02:46 +0000296@validate.validation_complete
Bob Haarmand1225ea2022-01-19 22:01:42 +0000297def UploadPrebuiltPackages(input_proto, _output_proto, _config):
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700298 """Upload prebuilt packages."""
Alex Klein1699fab2022-09-08 08:46:06 -0600299 sdk.UploadPrebuiltPackages(
300 controller_util.ParseChroot(input_proto.chroot),
301 input_proto.prepend_version,
302 input_proto.version,
303 input_proto.upload_location,
304 )
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700305
306
307@faux.all_empty
308@validate.require("chroot")
309@validate.validation_complete
310def BuildSdkToolchain(input_proto, output_proto, _config):
311 """Build cross-compiler packages for the SDK."""
312 chroot = controller_util.ParseChroot(input_proto.chroot)
313 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)
316 generated_files = sdk.BuildSdkToolchain(chroot, extra_env=extra_env)
317 output_proto.generated_files.extend(generated_files)