blob: 7dc489cac7306cd99c104d60b6c07f5c308ec572 [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
Greg Edelston01ae5942023-01-30 16:26:54 -070016from chromite.lib import path_util
Alex Klein19c4cc42019-02-27 14:47:57 -070017from chromite.service import sdk
18
19
Alex Klein076841b2019-08-29 15:19:39 -060020def _ChrootVersionResponse(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -060021 """Add a fake chroot version to a successful response."""
22 output_proto.version.version = 168
Alex Klein076841b2019-08-29 15:19:39 -060023
24
Bob Haarman171d4092023-01-10 16:44:19 +000025def _BinhostCLs(_input_proto, output_proto, _config):
26 """Add fake CL identifiers to a successful response."""
Bob Haarmanc0082602022-09-20 16:12:43 -070027 output_proto.cls = [
Bob Haarman171d4092023-01-10 16:44:19 +000028 "fakecl:1",
29 "fakecl:2",
Bob Haarmanc0082602022-09-20 16:12:43 -070030 ]
31
32
Bob Haarman0853ce92022-12-13 18:11:53 +000033def _BuildSdkTarballResponse(_input_proto, output_proto, _config):
34 """Populate a fake BuildSdkTarballResponse."""
35 output_proto.sdk_tarball_path.path = "/fake/sdk/tarball.tar.gz"
36 output_proto.sdk_tarball_path.location = common_pb2.Path.OUTSIDE
37
38
39@faux.success(_BuildSdkTarballResponse)
40@validate.require("chroot")
41@validate.validation_complete
42def BuildSdkTarball(
43 input_proto: "BuildSdkTarballRequest",
44 output_proto: "BuildSdkTarballResponse",
45 _config: "api_config.ApiConfig",
46) -> None:
47 chroot = controller_util.ParseChroot(input_proto.chroot)
48 output_proto.sdk_tarball_path.path = str(sdk.BuildSdkTarball(chroot))
49 output_proto.sdk_tarball_path.location = common_pb2.Path.OUTSIDE
50
51
Greg Edelston01ae5942023-01-30 16:26:54 -070052def _CreateManifestFromSdkResponse(_input_proto, output_proto, _config):
53 """Populate a fake CreateManifestFromSdkResponse."""
54 output_proto.manifest_path.path = "/fake/sdk/tarball.tar.gz.Manifest"
55 output_proto.manifest_path.location = common_pb2.Path.Location.INSIDE
56
57
58@faux.success(_CreateManifestFromSdkResponse)
59@validate.require("chroot")
60@validate.require("sdk_path")
61@validate.require("dest_dir")
62@validate.validation_complete
63def CreateManifestFromSdk(
64 input_proto: "CreateManifestFromSdkRequest",
65 output_proto: "CreateManifestFromSdkResponse",
66 _config: "api_config.ApiConfig",
67) -> None:
68 """Create a manifest file showing the ebuilds in an SDK."""
69
70 def _assert_path_is_absolute(path: str, name: str):
71 """Raise an exception if the given path is not absolute."""
72 if not os.path.isabs(path):
73 cros_build_lib.Die(f"The {name} must be absolute; got {path}")
74
75 _assert_path_is_absolute(input_proto.chroot.path, "chroot path")
76 _assert_path_is_absolute(input_proto.sdk_path.path, "SDK path")
77 _assert_path_is_absolute(input_proto.dest_dir.path, "destination directory")
78
79 sdk_path = path_util.ProtoPathToPathlibPath(
80 input_proto.sdk_path, input_proto.chroot
81 )
82 dest_dir = path_util.ProtoPathToPathlibPath(
83 input_proto.dest_dir, input_proto.chroot
84 )
85
86 manifest_path = sdk.CreateManifestFromSdk(sdk_path, dest_dir)
87 output_proto.manifest_path.path = str(manifest_path)
88 output_proto.manifest_path.location = common_pb2.Path.Location.OUTSIDE
89
90
Alex Klein076841b2019-08-29 15:19:39 -060091@faux.success(_ChrootVersionResponse)
92@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -060093def Create(
94 input_proto: "CreateRequest",
95 output_proto: "CreateResponse",
96 config: "api_config.ApiConfig",
97) -> Union[int, None]:
98 """Chroot creation, includes support for replacing an existing chroot.
Alex Klein19c4cc42019-02-27 14:47:57 -070099
Alex Klein1699fab2022-09-08 08:46:06 -0600100 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600101 input_proto: The input proto.
102 output_proto: The output proto.
103 config: The API call config.
Kevin Shelton50dabff2022-04-09 11:29:53 -0700104
Alex Klein1699fab2022-09-08 08:46:06 -0600105 Returns:
Alex Klein611dddd2022-10-11 17:02:01 -0600106 An error code, None otherwise.
Alex Klein1699fab2022-09-08 08:46:06 -0600107 """
108 replace = not input_proto.flags.no_replace
109 bootstrap = input_proto.flags.bootstrap
Alex Klein19c4cc42019-02-27 14:47:57 -0700110
Alex Klein1699fab2022-09-08 08:46:06 -0600111 chroot_path = input_proto.chroot.path
112 cache_dir = input_proto.chroot.cache_dir
113 sdk_version = input_proto.sdk_version
114 skip_chroot_upgrade = input_proto.skip_chroot_upgrade
Alex Klein19c4cc42019-02-27 14:47:57 -0700115
Alex Klein1699fab2022-09-08 08:46:06 -0600116 if chroot_path and not os.path.isabs(chroot_path):
117 cros_build_lib.Die("The chroot path must be absolute.")
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,
Alex Klein1699fab2022-09-08 08:46:06 -0600125 cache_dir=cache_dir,
126 chroot_path=chroot_path,
127 sdk_version=sdk_version,
128 skip_chroot_upgrade=skip_chroot_upgrade,
129 )
Alex Klein19c4cc42019-02-27 14:47:57 -0700130
Alex Klein1699fab2022-09-08 08:46:06 -0600131 version = sdk.Create(args)
Alex Klein19c4cc42019-02-27 14:47:57 -0700132
Alex Klein1699fab2022-09-08 08:46:06 -0600133 if version:
134 output_proto.version.version = version
135 else:
136 # This should be very rare, if ever used, but worth noting.
137 cros_build_lib.Die(
138 "No chroot version could be found. There was likely an"
139 "error creating the chroot that was not detected."
140 )
Alex Kleinaa5c4172019-02-27 17:12:20 -0700141
142
Alex Klein076841b2019-08-29 15:19:39 -0600143@faux.success(_ChrootVersionResponse)
144@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600145@validate.require_each("toolchain_targets", ["name"])
Alex Klein231d2da2019-07-22 16:44:45 -0600146@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -0600147def Update(
148 input_proto: "UpdateRequest",
149 output_proto: "UpdateResponse",
150 _config: "api_config.ApiConfig",
151):
152 """Update the chroot.
Alex Kleinaa5c4172019-02-27 17:12:20 -0700153
Alex Klein1699fab2022-09-08 08:46:06 -0600154 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600155 input_proto: The input proto.
156 output_proto: The output proto.
157 _config: The API call config.
Alex Klein1699fab2022-09-08 08:46:06 -0600158 """
159 build_source = input_proto.flags.build_source
160 targets = [target.name for target in input_proto.toolchain_targets]
161 toolchain_changed = input_proto.flags.toolchain_changed
Alex Kleinaa5c4172019-02-27 17:12:20 -0700162
Alex Klein1699fab2022-09-08 08:46:06 -0600163 args = sdk.UpdateArguments(
164 build_source=build_source,
165 toolchain_targets=targets,
166 toolchain_changed=toolchain_changed,
167 )
Chris McDonald68faa2a2020-01-13 12:23:05 -0700168
Alex Klein1699fab2022-09-08 08:46:06 -0600169 version = sdk.Update(args)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700170
Alex Klein1699fab2022-09-08 08:46:06 -0600171 if version:
172 output_proto.version.version = version
173 else:
174 # This should be very rare, if ever used, but worth noting.
175 cros_build_lib.Die(
176 "No chroot version could be found. There was likely an"
177 "error creating the chroot that was not detected."
178 )
Alex Klein730cf552019-10-16 11:28:22 -0600179
180
181@faux.all_empty
Greg Edelston6733dc52023-02-15 15:20:07 -0700182@validate.require("chroot")
183@validate.require("binhost_gs_bucket")
184@validate.validation_complete
185def Uprev(input_proto, output_proto, _config):
186 """Update the SDK version and prebuilt files to point to the latest SDK."""
187 target_version = input_proto.version or sdk.GetLatestVersion()
188 modified_files = sdk.UprevSdkAndPrebuilts(
189 controller_util.ParseChroot(input_proto.chroot),
190 binhost_gs_bucket=input_proto.binhost_gs_bucket,
191 version=target_version,
192 )
193 for modified_file in modified_files:
194 output_proto.modified_files.add(
195 common_pb2.Path(
196 path=str(modified_file),
197 location=common_pb2.Path.OUTSIDE,
198 )
199 )
200 output_proto.version = target_version
201
202
203@faux.all_empty
Alex Klein730cf552019-10-16 11:28:22 -0600204@validate.validation_complete
205def Delete(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600206 """Delete a chroot."""
207 chroot = controller_util.ParseChroot(input_proto.chroot)
208 sdk.Delete(chroot, force=True)
Chris McDonald53ad5442020-01-17 14:11:55 -0700209
210
211@faux.all_empty
212@validate.validation_complete
Brian Norrisf624bf42023-03-02 12:57:49 -0800213def Unmount(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600214 """Unmount a chroot"""
Brian Norrisf624bf42023-03-02 12:57:49 -0800215 # Deprecated. Do nothing.
Chris McDonaldf48ea202020-01-29 13:19:23 -0700216
217
218@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600219@validate.require("path.path")
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600220@validate.validation_complete
221def UnmountPath(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600222 """Unmount a path"""
223 sdk.UnmountPath(input_proto.path.path)
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600224
225
226@faux.all_empty
Chris McDonaldf48ea202020-01-29 13:19:23 -0700227@validate.validation_complete
228def Clean(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600229 """Clean unneeded files from a chroot."""
230 chroot = controller_util.ParseChroot(input_proto.chroot)
231 sdk.Clean(chroot, safe=True, sysroots=True)
Chris McDonald9d486802020-01-29 15:57:22 -0700232
233
234@faux.all_empty
235@validate.validation_complete
Bob Haarmand1225ea2022-01-19 22:01:42 +0000236def BuildPrebuilts(input_proto, _output_proto, _config):
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700237 """Build the binary packages that comprise the Chromium OS SDK."""
Alex Klein1699fab2022-09-08 08:46:06 -0600238 chroot = controller_util.ParseChroot(input_proto.chroot)
Greg Edelston199c5362023-01-04 12:29:07 -0700239 sdk.BuildPrebuilts(chroot, board=input_proto.build_target.name)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000240
241
Bob Haarman171d4092023-01-10 16:44:19 +0000242@faux.success(_BinhostCLs)
Bob Haarmanc0082602022-09-20 16:12:43 -0700243@faux.empty_error
Bob Haarman0853ce92022-12-13 18:11:53 +0000244@validate.require(
245 "prepend_version", "version", "upload_location", "sdk_tarball_template"
246)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000247@validate.validation_complete
Bob Haarmanc0082602022-09-20 16:12:43 -0700248def CreateBinhostCLs(
249 input_proto: "CreateBinhostCLsRequest",
250 output_proto: "CreateBinhostCLsResponse",
251 _config: "api_config.ApiConfig",
252) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600253 """Create CLs to update the binhost to point at uploaded prebuilts."""
Bob Haarman171d4092023-01-10 16:44:19 +0000254 output_proto.cls.extend(
255 sdk.CreateBinhostCLs(
256 input_proto.prepend_version,
257 input_proto.version,
258 input_proto.upload_location,
259 input_proto.sdk_tarball_template,
260 )
Alex Klein1699fab2022-09-08 08:46:06 -0600261 )
Bob Haarman338f8b72022-02-04 01:02:46 +0000262
263
264@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600265@validate.require("prepend_version", "version", "upload_location")
Bob Haarman338f8b72022-02-04 01:02:46 +0000266@validate.validation_complete
Bob Haarmand1225ea2022-01-19 22:01:42 +0000267def UploadPrebuiltPackages(input_proto, _output_proto, _config):
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700268 """Upload prebuilt packages."""
Alex Klein1699fab2022-09-08 08:46:06 -0600269 sdk.UploadPrebuiltPackages(
270 controller_util.ParseChroot(input_proto.chroot),
271 input_proto.prepend_version,
272 input_proto.version,
273 input_proto.upload_location,
274 )
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700275
276
277@faux.all_empty
278@validate.require("chroot")
279@validate.validation_complete
280def BuildSdkToolchain(input_proto, output_proto, _config):
281 """Build cross-compiler packages for the SDK."""
282 chroot = controller_util.ParseChroot(input_proto.chroot)
283 extra_env: Dict[str, str] = {}
284 if input_proto.use_flags:
285 extra_env["USE"] = " ".join(use.flag for use in input_proto.use_flags)
286 generated_files = sdk.BuildSdkToolchain(chroot, extra_env=extra_env)
287 output_proto.generated_files.extend(generated_files)