blob: 6f9ab403e32f26ef8a9e915a47434af282ebe701 [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 Edelston4907c8c2023-03-27 14:53:31 -06008import pathlib
Greg Edelston9dcdc8a2023-01-11 17:07:10 -07009from typing import Dict, 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 Edelston01ae5942023-01-30 16:26:54 -070017from chromite.lib import path_util
Alex Klein19c4cc42019-02-27 14:47:57 -070018from chromite.service import sdk
19
20
Alex Klein076841b2019-08-29 15:19:39 -060021def _ChrootVersionResponse(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -060022 """Add a fake chroot version to a successful response."""
23 output_proto.version.version = 168
Alex Klein076841b2019-08-29 15:19:39 -060024
25
Bob Haarman171d4092023-01-10 16:44:19 +000026def _BinhostCLs(_input_proto, output_proto, _config):
27 """Add fake CL identifiers to a successful response."""
Bob Haarmanc0082602022-09-20 16:12:43 -070028 output_proto.cls = [
Bob Haarman171d4092023-01-10 16:44:19 +000029 "fakecl:1",
30 "fakecl:2",
Bob Haarmanc0082602022-09-20 16:12:43 -070031 ]
32
33
Bob Haarman0853ce92022-12-13 18:11:53 +000034def _BuildSdkTarballResponse(_input_proto, output_proto, _config):
35 """Populate a fake BuildSdkTarballResponse."""
36 output_proto.sdk_tarball_path.path = "/fake/sdk/tarball.tar.gz"
37 output_proto.sdk_tarball_path.location = common_pb2.Path.OUTSIDE
38
39
40@faux.success(_BuildSdkTarballResponse)
41@validate.require("chroot")
42@validate.validation_complete
43def BuildSdkTarball(
44 input_proto: "BuildSdkTarballRequest",
45 output_proto: "BuildSdkTarballResponse",
46 _config: "api_config.ApiConfig",
47) -> None:
48 chroot = controller_util.ParseChroot(input_proto.chroot)
49 output_proto.sdk_tarball_path.path = str(sdk.BuildSdkTarball(chroot))
50 output_proto.sdk_tarball_path.location = common_pb2.Path.OUTSIDE
51
52
Greg Edelston01ae5942023-01-30 16:26:54 -070053def _CreateManifestFromSdkResponse(_input_proto, output_proto, _config):
54 """Populate a fake CreateManifestFromSdkResponse."""
55 output_proto.manifest_path.path = "/fake/sdk/tarball.tar.gz.Manifest"
56 output_proto.manifest_path.location = common_pb2.Path.Location.INSIDE
57
58
59@faux.success(_CreateManifestFromSdkResponse)
60@validate.require("chroot")
61@validate.require("sdk_path")
62@validate.require("dest_dir")
63@validate.validation_complete
64def CreateManifestFromSdk(
65 input_proto: "CreateManifestFromSdkRequest",
66 output_proto: "CreateManifestFromSdkResponse",
67 _config: "api_config.ApiConfig",
68) -> None:
69 """Create a manifest file showing the ebuilds in an SDK."""
70
71 def _assert_path_is_absolute(path: str, name: str):
72 """Raise an exception if the given path is not absolute."""
73 if not os.path.isabs(path):
74 cros_build_lib.Die(f"The {name} must be absolute; got {path}")
75
76 _assert_path_is_absolute(input_proto.chroot.path, "chroot path")
77 _assert_path_is_absolute(input_proto.sdk_path.path, "SDK path")
78 _assert_path_is_absolute(input_proto.dest_dir.path, "destination directory")
79
80 sdk_path = path_util.ProtoPathToPathlibPath(
81 input_proto.sdk_path, input_proto.chroot
82 )
83 dest_dir = path_util.ProtoPathToPathlibPath(
84 input_proto.dest_dir, input_proto.chroot
85 )
86
87 manifest_path = sdk.CreateManifestFromSdk(sdk_path, dest_dir)
88 output_proto.manifest_path.path = str(manifest_path)
89 output_proto.manifest_path.location = common_pb2.Path.Location.OUTSIDE
90
91
Alex Klein076841b2019-08-29 15:19:39 -060092@faux.success(_ChrootVersionResponse)
93@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -060094def Create(
95 input_proto: "CreateRequest",
96 output_proto: "CreateResponse",
97 config: "api_config.ApiConfig",
98) -> Union[int, None]:
99 """Chroot creation, includes support for replacing an existing chroot.
Alex Klein19c4cc42019-02-27 14:47:57 -0700100
Alex Klein1699fab2022-09-08 08:46:06 -0600101 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600102 input_proto: The input proto.
103 output_proto: The output proto.
104 config: The API call config.
Kevin Shelton50dabff2022-04-09 11:29:53 -0700105
Alex Klein1699fab2022-09-08 08:46:06 -0600106 Returns:
Alex Klein611dddd2022-10-11 17:02:01 -0600107 An error code, None otherwise.
Alex Klein1699fab2022-09-08 08:46:06 -0600108 """
109 replace = not input_proto.flags.no_replace
110 bootstrap = input_proto.flags.bootstrap
Alex Klein19c4cc42019-02-27 14:47:57 -0700111
Alex Klein1699fab2022-09-08 08:46:06 -0600112 chroot_path = input_proto.chroot.path
113 cache_dir = input_proto.chroot.cache_dir
114 sdk_version = input_proto.sdk_version
115 skip_chroot_upgrade = input_proto.skip_chroot_upgrade
Alex Klein19c4cc42019-02-27 14:47:57 -0700116
Alex Klein1699fab2022-09-08 08:46:06 -0600117 if chroot_path and not os.path.isabs(chroot_path):
118 cros_build_lib.Die("The chroot path must be absolute.")
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,
Alex Klein1699fab2022-09-08 08:46:06 -0600126 cache_dir=cache_dir,
127 chroot_path=chroot_path,
128 sdk_version=sdk_version,
129 skip_chroot_upgrade=skip_chroot_upgrade,
130 )
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(
149 input_proto: "UpdateRequest",
150 output_proto: "UpdateResponse",
151 _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 Edelston4907c8c2023-03-27 14:53:31 -0600183@validate.require("source_root")
Greg Edelston6733dc52023-02-15 15:20:07 -0700184@validate.require("binhost_gs_bucket")
Greg Edelston40aea812023-03-27 16:34:35 -0600185@validate.eq("source_root.location", common_pb2.Path.Location.OUTSIDE)
Greg Edelston6733dc52023-02-15 15:20:07 -0700186@validate.validation_complete
187def Uprev(input_proto, output_proto, _config):
Greg Edelston40aea812023-03-27 16:34:35 -0600188 """Update SDK version file and prebuilt files to point to the latest SDK.
189
190 Files will be changed locally, but not committed.
191 """
192 # If the UprevRequest did not specify a target version,
193 # check the remote SDK version file on Google Cloud Storage for the latest
194 # uprev target.
195 target_version = input_proto.version or sdk.GetLatestUprevTargetVersion()
196
197 # The main uprev logic occurs in service/sdk.py.
Greg Edelston6733dc52023-02-15 15:20:07 -0700198 modified_files = sdk.UprevSdkAndPrebuilts(
Greg Edelston4907c8c2023-03-27 14:53:31 -0600199 pathlib.Path(input_proto.source_root.path),
Greg Edelston6733dc52023-02-15 15:20:07 -0700200 binhost_gs_bucket=input_proto.binhost_gs_bucket,
201 version=target_version,
202 )
Greg Edelston40aea812023-03-27 16:34:35 -0600203
204 # Populate the UprevResponse object with the modified files.
Greg Edelston6733dc52023-02-15 15:20:07 -0700205 for modified_file in modified_files:
Greg Edelston40aea812023-03-27 16:34:35 -0600206 proto_path = output_proto.modified_files.add()
207 proto_path.path = str(modified_file)
208 proto_path.location = common_pb2.Path.OUTSIDE
Greg Edelston6733dc52023-02-15 15:20:07 -0700209 output_proto.version = target_version
210
211
212@faux.all_empty
Alex Klein730cf552019-10-16 11:28:22 -0600213@validate.validation_complete
214def Delete(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600215 """Delete a chroot."""
216 chroot = controller_util.ParseChroot(input_proto.chroot)
217 sdk.Delete(chroot, force=True)
Chris McDonald53ad5442020-01-17 14:11:55 -0700218
219
220@faux.all_empty
221@validate.validation_complete
Brian Norrisf624bf42023-03-02 12:57:49 -0800222def Unmount(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600223 """Unmount a chroot"""
Brian Norrisf624bf42023-03-02 12:57:49 -0800224 # Deprecated. Do nothing.
Chris McDonaldf48ea202020-01-29 13:19:23 -0700225
226
227@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600228@validate.require("path.path")
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600229@validate.validation_complete
230def UnmountPath(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600231 """Unmount a path"""
232 sdk.UnmountPath(input_proto.path.path)
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600233
234
235@faux.all_empty
Chris McDonaldf48ea202020-01-29 13:19:23 -0700236@validate.validation_complete
237def Clean(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600238 """Clean unneeded files from a chroot."""
239 chroot = controller_util.ParseChroot(input_proto.chroot)
240 sdk.Clean(chroot, safe=True, sysroots=True)
Chris McDonald9d486802020-01-29 15:57:22 -0700241
242
243@faux.all_empty
244@validate.validation_complete
Bob Haarmand1225ea2022-01-19 22:01:42 +0000245def BuildPrebuilts(input_proto, _output_proto, _config):
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700246 """Build the binary packages that comprise the Chromium OS SDK."""
Alex Klein1699fab2022-09-08 08:46:06 -0600247 chroot = controller_util.ParseChroot(input_proto.chroot)
Greg Edelston199c5362023-01-04 12:29:07 -0700248 sdk.BuildPrebuilts(chroot, board=input_proto.build_target.name)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000249
250
Bob Haarman171d4092023-01-10 16:44:19 +0000251@faux.success(_BinhostCLs)
Bob Haarmanc0082602022-09-20 16:12:43 -0700252@faux.empty_error
Bob Haarman0853ce92022-12-13 18:11:53 +0000253@validate.require(
254 "prepend_version", "version", "upload_location", "sdk_tarball_template"
255)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000256@validate.validation_complete
Bob Haarmanc0082602022-09-20 16:12:43 -0700257def CreateBinhostCLs(
258 input_proto: "CreateBinhostCLsRequest",
259 output_proto: "CreateBinhostCLsResponse",
260 _config: "api_config.ApiConfig",
261) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600262 """Create CLs to update the binhost to point at uploaded prebuilts."""
Bob Haarman171d4092023-01-10 16:44:19 +0000263 output_proto.cls.extend(
264 sdk.CreateBinhostCLs(
265 input_proto.prepend_version,
266 input_proto.version,
267 input_proto.upload_location,
268 input_proto.sdk_tarball_template,
269 )
Alex Klein1699fab2022-09-08 08:46:06 -0600270 )
Bob Haarman338f8b72022-02-04 01:02:46 +0000271
272
273@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600274@validate.require("prepend_version", "version", "upload_location")
Bob Haarman338f8b72022-02-04 01:02:46 +0000275@validate.validation_complete
Bob Haarmand1225ea2022-01-19 22:01:42 +0000276def UploadPrebuiltPackages(input_proto, _output_proto, _config):
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700277 """Upload prebuilt packages."""
Alex Klein1699fab2022-09-08 08:46:06 -0600278 sdk.UploadPrebuiltPackages(
279 controller_util.ParseChroot(input_proto.chroot),
280 input_proto.prepend_version,
281 input_proto.version,
282 input_proto.upload_location,
283 )
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700284
285
286@faux.all_empty
287@validate.require("chroot")
288@validate.validation_complete
289def BuildSdkToolchain(input_proto, output_proto, _config):
290 """Build cross-compiler packages for the SDK."""
291 chroot = controller_util.ParseChroot(input_proto.chroot)
292 extra_env: Dict[str, str] = {}
293 if input_proto.use_flags:
294 extra_env["USE"] = " ".join(use.flag for use in input_proto.use_flags)
295 generated_files = sdk.BuildSdkToolchain(chroot, extra_env=extra_env)
296 output_proto.generated_files.extend(generated_files)