blob: ffabfd6a7267bdc1c2119a2111397b5de3010ea8 [file] [log] [blame]
Tiancong Wangaf050172019-07-10 11:52:03 -07001# Copyright 2019 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Toolchain-related operations."""
6
LaMont Jonesb20b3d92019-11-23 11:47:48 -07007import collections
Chris McDonald1672ddb2021-07-21 11:48:23 -06008import logging
LaMont Jonesb20b3d92019-11-23 11:47:48 -07009
LaMont Jones5d2edcb2019-12-23 11:32:03 -070010from 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
LaMont Jones5d2edcb2019-12-23 11:32:03 -070013from chromite.api.controller import controller_util
Tiancong Wang24a3df72019-08-20 15:48:51 -070014from chromite.api.gen.chromite.api import toolchain_pb2
LaMont Jonesfd68cb12020-04-29 16:43:06 -060015from chromite.api.gen.chromite.api.artifacts_pb2 import PrepareForBuildResponse
Chris McDonald1672ddb2021-07-21 11:48:23 -060016from chromite.api.gen.chromiumos.builder_config_pb2 import BuilderConfig
17from chromite.lib import chroot_util
18from chromite.lib import cros_build_lib
19from chromite.lib import toolchain_util
Ryan Beltran40e4ad12021-05-17 19:55:03 +000020from chromite.scripts import tricium_cargo_clippy
Tiancong Wangaf050172019-07-10 11:52:03 -070021
Chris McDonald1672ddb2021-07-21 11:48:23 -060022
LaMont Jonesb20b3d92019-11-23 11:47:48 -070023_Handlers = collections.namedtuple('_Handlers', ['name', 'prepare', 'bundle'])
24_TOOLCHAIN_ARTIFACT_HANDLERS = {
LaMont Jonescd1503d2020-03-04 09:09:59 -070025 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE:
LaMont Jonesd3944582020-03-04 10:37:05 -070026 _Handlers('UnverifiedChromeLlvmOrderfile',
LaMont Jones5d2edcb2019-12-23 11:32:03 -070027 toolchain_util.PrepareForBuild,
28 toolchain_util.BundleArtifacts),
LaMont Jonescd1503d2020-03-04 09:09:59 -070029 BuilderConfig.Artifacts.VERIFIED_CHROME_LLVM_ORDERFILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070030 _Handlers('VerifiedChromeLlvmOrderfile', toolchain_util.PrepareForBuild,
LaMont Jones5d2edcb2019-12-23 11:32:03 -070031 toolchain_util.BundleArtifacts),
LaMont Jonesb20b3d92019-11-23 11:47:48 -070032 BuilderConfig.Artifacts.CHROME_CLANG_WARNINGS_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070033 _Handlers('ChromeClangWarningsFile', toolchain_util.PrepareForBuild,
LaMont Jones90bab632020-01-27 15:58:26 -070034 toolchain_util.BundleArtifacts),
LaMont Jonesb20b3d92019-11-23 11:47:48 -070035 BuilderConfig.Artifacts.UNVERIFIED_LLVM_PGO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070036 _Handlers('UnverifiedLlvmPgoFile', toolchain_util.PrepareForBuild,
LaMont Jones90bab632020-01-27 15:58:26 -070037 toolchain_util.BundleArtifacts),
LaMont Jones45ca6c42020-02-05 09:39:09 -070038 BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE:
39 _Handlers('UnverifiedChromeBenchmarkAfdoFile',
LaMont Jones90bab632020-01-27 15:58:26 -070040 toolchain_util.PrepareForBuild,
41 toolchain_util.BundleArtifacts),
LaMont Jones3fed7f22020-03-04 10:15:11 -070042 BuilderConfig.Artifacts.CHROME_DEBUG_BINARY:
Tiancong Wangba2a1c22021-01-19 10:45:06 -080043 _Handlers('ChromeDebugBinary', toolchain_util.PrepareForBuild,
LaMont Jones3fed7f22020-03-04 10:15:11 -070044 toolchain_util.BundleArtifacts),
LaMont Jones53bddd02020-03-12 15:02:54 -060045 BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_PERF_FILE:
46 _Handlers('UnverifiedChromeBenchmarkPerfFile',
47 toolchain_util.PrepareForBuild,
48 toolchain_util.BundleArtifacts),
LaMont Jones45ca6c42020-02-05 09:39:09 -070049 BuilderConfig.Artifacts.VERIFIED_CHROME_BENCHMARK_AFDO_FILE:
50 _Handlers('VerifiedChromeBenchmarkAfdoFile',
LaMont Jones90bab632020-01-27 15:58:26 -070051 toolchain_util.PrepareForBuild,
52 toolchain_util.BundleArtifacts),
LaMont Jones45ca6c42020-02-05 09:39:09 -070053 BuilderConfig.Artifacts.UNVERIFIED_KERNEL_CWP_AFDO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070054 _Handlers('UnverifiedKernelCwpAfdoFile', toolchain_util.PrepareForBuild,
LaMont Jones45ca6c42020-02-05 09:39:09 -070055 toolchain_util.BundleArtifacts),
56 BuilderConfig.Artifacts.VERIFIED_KERNEL_CWP_AFDO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070057 _Handlers('VerifiedKernelCwpAfdoFile', toolchain_util.PrepareForBuild,
LaMont Jones45ca6c42020-02-05 09:39:09 -070058 toolchain_util.BundleArtifacts),
59 BuilderConfig.Artifacts.UNVERIFIED_CHROME_CWP_AFDO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070060 _Handlers('UnverifiedChromeCwpAfdoFile', toolchain_util.PrepareForBuild,
LaMont Jones45ca6c42020-02-05 09:39:09 -070061 toolchain_util.BundleArtifacts),
62 BuilderConfig.Artifacts.VERIFIED_CHROME_CWP_AFDO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070063 _Handlers('VerifiedChromeCwpAfdoFile', toolchain_util.PrepareForBuild,
LaMont Jones45ca6c42020-02-05 09:39:09 -070064 toolchain_util.BundleArtifacts),
65 BuilderConfig.Artifacts.VERIFIED_RELEASE_AFDO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070066 _Handlers('VerifiedReleaseAfdoFile', toolchain_util.PrepareForBuild,
LaMont Jones90bab632020-01-27 15:58:26 -070067 toolchain_util.BundleArtifacts),
Tiancong Wang91cf1dd2020-05-05 10:30:22 -070068 BuilderConfig.Artifacts.TOOLCHAIN_WARNING_LOGS:
69 _Handlers('ToolchainWarningLogs', toolchain_util.PrepareForBuild,
70 toolchain_util.BundleArtifacts),
Tiancong Wangfe3dbd22020-06-12 15:45:55 -070071 BuilderConfig.Artifacts.CHROME_AFDO_PROFILE_FOR_ANDROID_LINUX:
72 _Handlers('ChromeAFDOProfileForAndroidLinux',
73 toolchain_util.PrepareForBuild,
74 toolchain_util.BundleArtifacts),
Jian Cai6190cc82020-06-12 16:24:32 -070075 BuilderConfig.Artifacts.CLANG_CRASH_DIAGNOSES:
76 _Handlers('ClangCrashDiagnoses', toolchain_util.PrepareForBuild,
77 toolchain_util.BundleArtifacts),
Ryan Beltran0be7dcf2020-12-09 18:31:53 +000078 BuilderConfig.Artifacts.COMPILER_RUSAGE_LOG:
79 _Handlers('CompilerRusageLogs', toolchain_util.PrepareForBuild,
80 toolchain_util.BundleArtifacts),
LaMont Jonesb20b3d92019-11-23 11:47:48 -070081}
82
Tiancong Wangd5214132021-01-12 10:43:57 -080083_TOOLCHAIN_COMMIT_HANDLERS = {
84 BuilderConfig.Artifacts.VERIFIED_KERNEL_CWP_AFDO_FILE:
85 'VerifiedKernelCwpAfdoFile'
86}
87
LaMont Jonesb20b3d92019-11-23 11:47:48 -070088
LaMont Jonese7821672020-04-09 08:56:26 -060089def _GetProfileInfoDict(profile_info):
90 """Convert profile_info to a dict.
91
92 Args:
93 profile_info (ArtifactProfileInfo): The artifact profile_info.
94
95 Returns:
96 A dictionary containing profile info.
97 """
98 ret = {}
99 which = profile_info.WhichOneof('artifact_profile_info')
100 if which:
101 value = getattr(profile_info, which)
102 # If it is a message, then use the contents of the message. This works as
103 # long as simple types do not have a 'DESCRIPTOR' attribute. (And protobuf
104 # messages do.)
105 if getattr(value, 'DESCRIPTOR', None):
106 ret.update({k.name: v for k, v in value.ListFields()})
107 else:
108 ret[which] = value
109 return ret
110
111
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700112# TODO(crbug/1031213): When @faux is expanded to have more than success/failure,
113# this should be changed.
114@faux.all_empty
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700115@validate.require('artifact_types')
116# Note: chroot and sysroot are unspecified the first time that the build_target
117# recipe calls PrepareForBuild. The second time, they are specified. No
118# validation check because "all" values are valid.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700119@validate.validation_complete
120def PrepareForBuild(input_proto, output_proto, _config):
121 """Prepare to build toolchain artifacts.
122
123 The handlers (from _TOOLCHAIN_ARTIFACT_HANDLERS above) are called with:
124 artifact_name (str): name of the artifact type.
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700125 chroot (chroot_lib.Chroot): chroot. Will be None if the chroot has not
126 yet been created.
127 sysroot_path (str): sysroot path inside the chroot (e.g., /build/atlas).
128 Will be an empty string if the sysroot has not yet been created.
129 build_target_name (str): name of the build target (e.g., atlas). Will be
130 an empty string if the sysroot has not yet been created.
131 input_artifacts ({(str) name:[str gs_locations]}): locations for possible
132 input artifacts. The handler is expected to know which keys it should
133 be using, and ignore any keys that it does not understand.
LaMont Jonese7821672020-04-09 08:56:26 -0600134 profile_info ({(str) name: (str) value}) Dictionary containing profile
135 information.
LaMont Jonesa215f1e2019-12-06 10:18:58 -0700136
137 They locate and modify any ebuilds and/or source required for the artifact
138 being created, then return a value from toolchain_util.PrepareForBuildReturn.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700139
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700140 This function sets output_proto.build_relevance to the result.
141
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700142 Args:
143 input_proto (PrepareForToolchainBuildRequest): The input proto
144 output_proto (PrepareForToolchainBuildResponse): The output proto
145 _config (api_config.ApiConfig): The API call config.
146 """
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700147 if input_proto.chroot.path:
148 chroot = controller_util.ParseChroot(input_proto.chroot)
149 else:
150 chroot = None
LaMont Jones4579e8c2019-12-06 14:20:37 -0700151
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700152 input_artifacts = collections.defaultdict(list)
153 for art in input_proto.input_artifacts:
154 item = _TOOLCHAIN_ARTIFACT_HANDLERS.get(art.input_artifact_type)
155 if item:
156 input_artifacts[item.name].extend(
157 ['gs://%s' % str(x) for x in art.input_artifact_gs_locations])
158
LaMont Jonese7821672020-04-09 08:56:26 -0600159 profile_info = _GetProfileInfoDict(input_proto.profile_info)
LaMont Jones45ca6c42020-02-05 09:39:09 -0700160
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700161 results = set()
162 sysroot_path = input_proto.sysroot.path
163 build_target = input_proto.sysroot.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700164 for artifact_type in input_proto.artifact_types:
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700165 # Unknown artifact_types are an error.
166 handler = _TOOLCHAIN_ARTIFACT_HANDLERS[artifact_type]
167 if handler.prepare:
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800168 results.add(
169 handler.prepare(handler.name, chroot, sysroot_path, build_target,
170 input_artifacts, profile_info))
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700171
172 # Translate the returns from the handlers we called.
173 # If any NEEDED => NEEDED
174 # elif any UNKNOWN => UNKNOWN
175 # elif any POINTLESS => POINTLESS
176 # else UNKNOWN.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700177 if toolchain_util.PrepareForBuildReturn.NEEDED in results:
LaMont Jonesfd68cb12020-04-29 16:43:06 -0600178 output_proto.build_relevance = PrepareForBuildResponse.NEEDED
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700179 elif toolchain_util.PrepareForBuildReturn.UNKNOWN in results:
LaMont Jonesfd68cb12020-04-29 16:43:06 -0600180 output_proto.build_relevance = PrepareForBuildResponse.UNKNOWN
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700181 elif toolchain_util.PrepareForBuildReturn.POINTLESS in results:
LaMont Jonesfd68cb12020-04-29 16:43:06 -0600182 output_proto.build_relevance = PrepareForBuildResponse.POINTLESS
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700183 else:
LaMont Jonesfd68cb12020-04-29 16:43:06 -0600184 output_proto.build_relevance = PrepareForBuildResponse.UNKNOWN
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700185 return controller.RETURN_CODE_SUCCESS
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700186
187
188# TODO(crbug/1031213): When @faux is expanded to have more than success/failure,
189# this should be changed.
190@faux.all_empty
LaMont Jonese911df02020-04-16 12:40:17 -0600191@validate.require('chroot.path', 'output_dir', 'artifact_types')
LaMont Jones4579e8c2019-12-06 14:20:37 -0700192@validate.exists('output_dir')
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700193@validate.validation_complete
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700194def BundleArtifacts(input_proto, output_proto, _config):
195 """Bundle toolchain artifacts.
196
197 The handlers (from _TOOLCHAIN_ARTIFACT_HANDLERS above) are called with:
LaMont Jonesa215f1e2019-12-06 10:18:58 -0700198 artifact_name (str): name of the artifact type
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700199 chroot (chroot_lib.Chroot): chroot
LaMont Jonese911df02020-04-16 12:40:17 -0600200 sysroot_path (str): sysroot path inside the chroot (e.g., /build/atlas),
201 or None.
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700202 chrome_root (str): path to chrome root. (e.g., /b/s/w/ir/k/chrome)
LaMont Jonese911df02020-04-16 12:40:17 -0600203 build_target_name (str): name of the build target (e.g., atlas), or None.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700204 output_dir (str): absolute path where artifacts are being bundled.
205 (e.g., /b/s/w/ir/k/recipe_cleanup/artifactssptfMU)
LaMont Jonese7821672020-04-09 08:56:26 -0600206 profile_info ({(str) name: (str) value}) Dictionary containing profile
207 information.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700208
209 Note: the actual upload to GS is done by CI, not here.
210
211 Args:
212 input_proto (BundleToolchainRequest): The input proto
213 output_proto (BundleToolchainResponse): The output proto
214 _config (api_config.ApiConfig): The API call config.
215 """
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700216 chroot = controller_util.ParseChroot(input_proto.chroot)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700217
LaMont Jonese7821672020-04-09 08:56:26 -0600218 profile_info = _GetProfileInfoDict(input_proto.profile_info)
LaMont Jones45ca6c42020-02-05 09:39:09 -0700219
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700220 for artifact_type in input_proto.artifact_types:
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700221 if artifact_type not in _TOOLCHAIN_ARTIFACT_HANDLERS:
222 logging.error('%s not understood', artifact_type)
223 return controller.RETURN_CODE_UNRECOVERABLE
224 handler = _TOOLCHAIN_ARTIFACT_HANDLERS[artifact_type]
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700225 if handler and handler.bundle:
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800226 artifacts = handler.bundle(handler.name, chroot, input_proto.sysroot.path,
227 input_proto.sysroot.build_target.name,
228 input_proto.output_dir, profile_info)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700229 if artifacts:
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700230 art_info = output_proto.artifacts_info.add()
231 art_info.artifact_type = artifact_type
232 for artifact in artifacts:
233 art_info.artifacts.add().path = artifact
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700234
235
Tiancong Wangd5214132021-01-12 10:43:57 -0800236def _GetUpdatedFilesResponse(_input_proto, output_proto, _config):
237 """Add successful status to the faux response."""
238 file_info = output_proto.updated_files.add()
239 file_info.path = '/any/modified/file'
240 output_proto.commit_message = 'Commit message'
241
242
243@faux.empty_error
244@faux.success(_GetUpdatedFilesResponse)
245@validate.require('uploaded_artifacts')
246@validate.validation_complete
247def GetUpdatedFiles(input_proto, output_proto, _config):
248 """Use uploaded artifacts to update some updates in a chromeos checkout.
249
250 The function will call toolchain_util.GetUpdatedFiles using the type of
251 uploaded artifacts to make some changes in a checkout, and return the list
252 of change files together with commit message.
253 updated_artifacts: A list of UpdatedArtifacts type which contains a tuple
254 of artifact info and profile info.
255 Note: the actual creation of the commit is done by CI, not here.
256
257 Args:
258 input_proto (GetUpdatedFilesRequest): The input proto
259 output_proto (GetUpdatedFilesResponse): The output proto
260 _config (api_config.ApiConfig): The API call config.
261 """
262 commit_message = ''
263 for artifact in input_proto.uploaded_artifacts:
264 artifact_type = artifact.artifact_info.artifact_type
265 if artifact_type not in _TOOLCHAIN_COMMIT_HANDLERS:
266 logging.error('%s not understood', artifact_type)
267 return controller.RETURN_CODE_UNRECOVERABLE
268 artifact_name = _TOOLCHAIN_COMMIT_HANDLERS[artifact_type]
269 if artifact_name:
270 assert len(artifact.artifact_info.artifacts) == 1, (
271 'Only one file to update per each artifact')
272 updated_files, message = toolchain_util.GetUpdatedFiles(
273 artifact_name, artifact.artifact_info.artifacts[0].path,
274 _GetProfileInfoDict(artifact.profile_info))
275 for f in updated_files:
276 file_info = output_proto.updated_files.add()
277 file_info.path = f
278
279 commit_message += message + '\n'
280 output_proto.commit_message = commit_message
281 # No commit footer is added for now. Can add more here if needed
282
283
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700284# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
285_NAMES_FOR_AFDO_ARTIFACTS = {
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700286 toolchain_pb2.ORDERFILE: 'orderfile',
287 toolchain_pb2.KERNEL_AFDO: 'kernel_afdo',
288 toolchain_pb2.CHROME_AFDO: 'chrome_afdo'
289}
290
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800291
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700292# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700293# Using a function instead of a dict because we need to mock these
294# functions in unittest, and mock doesn't play well with a dict definition.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700295def _GetMethodForUpdatingAFDOArtifacts(artifact_type):
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700296 return {
297 toolchain_pb2.ORDERFILE: toolchain_util.OrderfileUpdateChromeEbuild,
298 toolchain_pb2.KERNEL_AFDO: toolchain_util.AFDOUpdateKernelEbuild,
299 toolchain_pb2.CHROME_AFDO: toolchain_util.AFDOUpdateChromeEbuild
300 }[artifact_type]
Tiancong Wang24a3df72019-08-20 15:48:51 -0700301
Tiancong Wangaf050172019-07-10 11:52:03 -0700302
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700303# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700304def _UpdateEbuildWithAFDOArtifactsResponse(_input_proto, output_proto, _config):
305 """Add successful status to the faux response."""
306 output_proto.status = True
307
308
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700309# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700310@faux.success(_UpdateEbuildWithAFDOArtifactsResponse)
311@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -0600312@validate.require('build_target.name')
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700313@validate.is_in('artifact_type', _NAMES_FOR_AFDO_ARTIFACTS)
Alex Klein231d2da2019-07-22 16:44:45 -0600314@validate.validation_complete
Tiancong Wang24a3df72019-08-20 15:48:51 -0700315def UpdateEbuildWithAFDOArtifacts(input_proto, output_proto, _config):
316 """Update Chrome or kernel ebuild with most recent unvetted artifacts.
Tiancong Wangaf050172019-07-10 11:52:03 -0700317
318 Args:
Tiancong Wang24a3df72019-08-20 15:48:51 -0700319 input_proto (VerifyAFDOArtifactsRequest): The input proto
320 output_proto (VerifyAFDOArtifactsResponse): The output proto
Alex Klein231d2da2019-07-22 16:44:45 -0600321 _config (api_config.ApiConfig): The API call config.
Tiancong Wangaf050172019-07-10 11:52:03 -0700322 """
Tiancong Wangaf050172019-07-10 11:52:03 -0700323 board = input_proto.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700324 update_method = _GetMethodForUpdatingAFDOArtifacts(input_proto.artifact_type)
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700325 output_proto.status = update_method(board)
Tiancong Wangaf050172019-07-10 11:52:03 -0700326
327
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700328# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700329def _UploadVettedAFDOArtifactsResponse(_input_proto, output_proto, _config):
330 """Add successful status to the faux response."""
331 output_proto.status = True
332
333
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700334# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700335@faux.success(_UploadVettedAFDOArtifactsResponse)
336@faux.empty_error
Tiancong Wang24a3df72019-08-20 15:48:51 -0700337@validate.require('build_target.name')
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700338@validate.is_in('artifact_type', _NAMES_FOR_AFDO_ARTIFACTS)
Alex Klein231d2da2019-07-22 16:44:45 -0600339@validate.validation_complete
Tiancong Wang24a3df72019-08-20 15:48:51 -0700340def UploadVettedAFDOArtifacts(input_proto, output_proto, _config):
Tiancong Wangaf050172019-07-10 11:52:03 -0700341 """Upload a vetted orderfile to GS bucket.
342
343 Args:
Tiancong Wang24a3df72019-08-20 15:48:51 -0700344 input_proto (VerifyAFDOArtifactsRequest): The input proto
345 output_proto (VerifyAFDOArtifactsResponse): The output proto
Alex Klein231d2da2019-07-22 16:44:45 -0600346 _config (api_config.ApiConfig): The API call config.
Tiancong Wangaf050172019-07-10 11:52:03 -0700347 """
Tiancong Wang24a3df72019-08-20 15:48:51 -0700348 board = input_proto.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700349 artifact_type = _NAMES_FOR_AFDO_ARTIFACTS[input_proto.artifact_type]
Tiancong Wang24a3df72019-08-20 15:48:51 -0700350 output_proto.status = toolchain_util.UploadAndPublishVettedAFDOArtifacts(
351 artifact_type, board)
Ryan Beltran40e4ad12021-05-17 19:55:03 +0000352
353
354def _fetch_clippy_lints():
355 """Get lints created by Cargo Clippy during emerge."""
356 lints_dir = '/tmp/cargo_clippy'
357 # TODO(b/188578936): determine relevant files for file filter
358 file_filter = '/**/*'
359 findings = tricium_cargo_clippy.parse_files(lints_dir)
360 findings = tricium_cargo_clippy.filter_diagnostics(findings, file_filter)
361 findings_protos = []
362 for finding in findings:
363 location_protos = []
364 for location in finding.locations:
365 location_protos.append(
366 toolchain_pb2.LinterFindingLocation(
367 filepath=location.file_path,
368 line_start=location.line_start,
369 line_end=location.line_end
370 )
371 )
372 findings_protos.append(
373 toolchain_pb2.LinterFinding(
374 message=finding.message,
375 locations=location_protos
376 )
377 )
378 return findings_protos
379
380
Ryan Beltran55a84832021-06-30 23:01:21 +0000381@validate.exists('sysroot.path')
382@validate.require('packages')
383@validate.validation_complete
Ryan Beltran40e4ad12021-05-17 19:55:03 +0000384def GetClippyLints(input_proto, output_proto, _config):
385 """Emerges the given packages and retrieves any findings from Cargo Clippy."""
Ryan Beltranb8909ae2021-06-30 19:59:12 +0000386 emerge_cmd = chroot_util.GetEmergeCommand(input_proto.sysroot.path)
387 package_names = [package.package_name for package in input_proto.packages]
388 cros_build_lib.sudo_run(
389 emerge_cmd + package_names,
390 preserve_env=True,
391 extra_env={'ENABLE_RUST_CLIPPY':1}
Ryan Beltran40e4ad12021-05-17 19:55:03 +0000392 )
393 output_proto.findings.extend(_fetch_clippy_lints())