blob: 00b412a34d4aef8d2d39c35e6085243fc03f607d [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
8
LaMont Jones5d2edcb2019-12-23 11:32:03 -07009from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060010from chromite.api import faux
Alex Klein231d2da2019-07-22 16:44:45 -060011from chromite.api import validate
LaMont Jones5d2edcb2019-12-23 11:32:03 -070012from chromite.api.controller import controller_util
Tiancong Wang24a3df72019-08-20 15:48:51 -070013from chromite.api.gen.chromite.api import toolchain_pb2
LaMont Jonesb20b3d92019-11-23 11:47:48 -070014from chromite.api.gen.chromiumos.builder_config_pb2 import BuilderConfig
LaMont Jones5d2edcb2019-12-23 11:32:03 -070015from chromite.lib import cros_logging as logging
16from chromite.lib import toolchain_util
Ryan Beltran40e4ad12021-05-17 19:55:03 +000017from chromite.lib import chroot_util
LaMont Jonesfd68cb12020-04-29 16:43:06 -060018from chromite.api.gen.chromite.api.artifacts_pb2 import PrepareForBuildResponse
Ryan Beltran40e4ad12021-05-17 19:55:03 +000019from chromite.scripts import tricium_cargo_clippy
Tiancong Wangaf050172019-07-10 11:52:03 -070020
LaMont Jonesb20b3d92019-11-23 11:47:48 -070021_Handlers = collections.namedtuple('_Handlers', ['name', 'prepare', 'bundle'])
22_TOOLCHAIN_ARTIFACT_HANDLERS = {
LaMont Jonescd1503d2020-03-04 09:09:59 -070023 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE:
LaMont Jonesd3944582020-03-04 10:37:05 -070024 _Handlers('UnverifiedChromeLlvmOrderfile',
LaMont Jones5d2edcb2019-12-23 11:32:03 -070025 toolchain_util.PrepareForBuild,
26 toolchain_util.BundleArtifacts),
LaMont Jonescd1503d2020-03-04 09:09:59 -070027 BuilderConfig.Artifacts.VERIFIED_CHROME_LLVM_ORDERFILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070028 _Handlers('VerifiedChromeLlvmOrderfile', toolchain_util.PrepareForBuild,
LaMont Jones5d2edcb2019-12-23 11:32:03 -070029 toolchain_util.BundleArtifacts),
LaMont Jonesb20b3d92019-11-23 11:47:48 -070030 BuilderConfig.Artifacts.CHROME_CLANG_WARNINGS_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070031 _Handlers('ChromeClangWarningsFile', toolchain_util.PrepareForBuild,
LaMont Jones90bab632020-01-27 15:58:26 -070032 toolchain_util.BundleArtifacts),
LaMont Jonesb20b3d92019-11-23 11:47:48 -070033 BuilderConfig.Artifacts.UNVERIFIED_LLVM_PGO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070034 _Handlers('UnverifiedLlvmPgoFile', toolchain_util.PrepareForBuild,
LaMont Jones90bab632020-01-27 15:58:26 -070035 toolchain_util.BundleArtifacts),
LaMont Jones45ca6c42020-02-05 09:39:09 -070036 BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE:
37 _Handlers('UnverifiedChromeBenchmarkAfdoFile',
LaMont Jones90bab632020-01-27 15:58:26 -070038 toolchain_util.PrepareForBuild,
39 toolchain_util.BundleArtifacts),
LaMont Jones3fed7f22020-03-04 10:15:11 -070040 BuilderConfig.Artifacts.CHROME_DEBUG_BINARY:
Tiancong Wangba2a1c22021-01-19 10:45:06 -080041 _Handlers('ChromeDebugBinary', toolchain_util.PrepareForBuild,
LaMont Jones3fed7f22020-03-04 10:15:11 -070042 toolchain_util.BundleArtifacts),
LaMont Jones53bddd02020-03-12 15:02:54 -060043 BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_PERF_FILE:
44 _Handlers('UnverifiedChromeBenchmarkPerfFile',
45 toolchain_util.PrepareForBuild,
46 toolchain_util.BundleArtifacts),
LaMont Jones45ca6c42020-02-05 09:39:09 -070047 BuilderConfig.Artifacts.VERIFIED_CHROME_BENCHMARK_AFDO_FILE:
48 _Handlers('VerifiedChromeBenchmarkAfdoFile',
LaMont Jones90bab632020-01-27 15:58:26 -070049 toolchain_util.PrepareForBuild,
50 toolchain_util.BundleArtifacts),
LaMont Jones45ca6c42020-02-05 09:39:09 -070051 BuilderConfig.Artifacts.UNVERIFIED_KERNEL_CWP_AFDO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070052 _Handlers('UnverifiedKernelCwpAfdoFile', toolchain_util.PrepareForBuild,
LaMont Jones45ca6c42020-02-05 09:39:09 -070053 toolchain_util.BundleArtifacts),
54 BuilderConfig.Artifacts.VERIFIED_KERNEL_CWP_AFDO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070055 _Handlers('VerifiedKernelCwpAfdoFile', toolchain_util.PrepareForBuild,
LaMont Jones45ca6c42020-02-05 09:39:09 -070056 toolchain_util.BundleArtifacts),
57 BuilderConfig.Artifacts.UNVERIFIED_CHROME_CWP_AFDO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070058 _Handlers('UnverifiedChromeCwpAfdoFile', toolchain_util.PrepareForBuild,
LaMont Jones45ca6c42020-02-05 09:39:09 -070059 toolchain_util.BundleArtifacts),
60 BuilderConfig.Artifacts.VERIFIED_CHROME_CWP_AFDO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070061 _Handlers('VerifiedChromeCwpAfdoFile', toolchain_util.PrepareForBuild,
LaMont Jones45ca6c42020-02-05 09:39:09 -070062 toolchain_util.BundleArtifacts),
63 BuilderConfig.Artifacts.VERIFIED_RELEASE_AFDO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070064 _Handlers('VerifiedReleaseAfdoFile', toolchain_util.PrepareForBuild,
LaMont Jones90bab632020-01-27 15:58:26 -070065 toolchain_util.BundleArtifacts),
Tiancong Wang91cf1dd2020-05-05 10:30:22 -070066 BuilderConfig.Artifacts.TOOLCHAIN_WARNING_LOGS:
67 _Handlers('ToolchainWarningLogs', toolchain_util.PrepareForBuild,
68 toolchain_util.BundleArtifacts),
Tiancong Wangfe3dbd22020-06-12 15:45:55 -070069 BuilderConfig.Artifacts.CHROME_AFDO_PROFILE_FOR_ANDROID_LINUX:
70 _Handlers('ChromeAFDOProfileForAndroidLinux',
71 toolchain_util.PrepareForBuild,
72 toolchain_util.BundleArtifacts),
Jian Cai6190cc82020-06-12 16:24:32 -070073 BuilderConfig.Artifacts.CLANG_CRASH_DIAGNOSES:
74 _Handlers('ClangCrashDiagnoses', toolchain_util.PrepareForBuild,
75 toolchain_util.BundleArtifacts),
Ryan Beltran0be7dcf2020-12-09 18:31:53 +000076 BuilderConfig.Artifacts.COMPILER_RUSAGE_LOG:
77 _Handlers('CompilerRusageLogs', toolchain_util.PrepareForBuild,
78 toolchain_util.BundleArtifacts),
LaMont Jonesb20b3d92019-11-23 11:47:48 -070079}
80
Tiancong Wangd5214132021-01-12 10:43:57 -080081_TOOLCHAIN_COMMIT_HANDLERS = {
82 BuilderConfig.Artifacts.VERIFIED_KERNEL_CWP_AFDO_FILE:
83 'VerifiedKernelCwpAfdoFile'
84}
85
LaMont Jonesb20b3d92019-11-23 11:47:48 -070086
LaMont Jonese7821672020-04-09 08:56:26 -060087def _GetProfileInfoDict(profile_info):
88 """Convert profile_info to a dict.
89
90 Args:
91 profile_info (ArtifactProfileInfo): The artifact profile_info.
92
93 Returns:
94 A dictionary containing profile info.
95 """
96 ret = {}
97 which = profile_info.WhichOneof('artifact_profile_info')
98 if which:
99 value = getattr(profile_info, which)
100 # If it is a message, then use the contents of the message. This works as
101 # long as simple types do not have a 'DESCRIPTOR' attribute. (And protobuf
102 # messages do.)
103 if getattr(value, 'DESCRIPTOR', None):
104 ret.update({k.name: v for k, v in value.ListFields()})
105 else:
106 ret[which] = value
107 return ret
108
109
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700110# TODO(crbug/1031213): When @faux is expanded to have more than success/failure,
111# this should be changed.
112@faux.all_empty
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700113@validate.require('artifact_types')
114# Note: chroot and sysroot are unspecified the first time that the build_target
115# recipe calls PrepareForBuild. The second time, they are specified. No
116# validation check because "all" values are valid.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700117@validate.validation_complete
118def PrepareForBuild(input_proto, output_proto, _config):
119 """Prepare to build toolchain artifacts.
120
121 The handlers (from _TOOLCHAIN_ARTIFACT_HANDLERS above) are called with:
122 artifact_name (str): name of the artifact type.
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700123 chroot (chroot_lib.Chroot): chroot. Will be None if the chroot has not
124 yet been created.
125 sysroot_path (str): sysroot path inside the chroot (e.g., /build/atlas).
126 Will be an empty string if the sysroot has not yet been created.
127 build_target_name (str): name of the build target (e.g., atlas). Will be
128 an empty string if the sysroot has not yet been created.
129 input_artifacts ({(str) name:[str gs_locations]}): locations for possible
130 input artifacts. The handler is expected to know which keys it should
131 be using, and ignore any keys that it does not understand.
LaMont Jonese7821672020-04-09 08:56:26 -0600132 profile_info ({(str) name: (str) value}) Dictionary containing profile
133 information.
LaMont Jonesa215f1e2019-12-06 10:18:58 -0700134
135 They locate and modify any ebuilds and/or source required for the artifact
136 being created, then return a value from toolchain_util.PrepareForBuildReturn.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700137
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700138 This function sets output_proto.build_relevance to the result.
139
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700140 Args:
141 input_proto (PrepareForToolchainBuildRequest): The input proto
142 output_proto (PrepareForToolchainBuildResponse): The output proto
143 _config (api_config.ApiConfig): The API call config.
144 """
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700145 if input_proto.chroot.path:
146 chroot = controller_util.ParseChroot(input_proto.chroot)
147 else:
148 chroot = None
LaMont Jones4579e8c2019-12-06 14:20:37 -0700149
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700150 input_artifacts = collections.defaultdict(list)
151 for art in input_proto.input_artifacts:
152 item = _TOOLCHAIN_ARTIFACT_HANDLERS.get(art.input_artifact_type)
153 if item:
154 input_artifacts[item.name].extend(
155 ['gs://%s' % str(x) for x in art.input_artifact_gs_locations])
156
LaMont Jonese7821672020-04-09 08:56:26 -0600157 profile_info = _GetProfileInfoDict(input_proto.profile_info)
LaMont Jones45ca6c42020-02-05 09:39:09 -0700158
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700159 results = set()
160 sysroot_path = input_proto.sysroot.path
161 build_target = input_proto.sysroot.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700162 for artifact_type in input_proto.artifact_types:
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700163 # Unknown artifact_types are an error.
164 handler = _TOOLCHAIN_ARTIFACT_HANDLERS[artifact_type]
165 if handler.prepare:
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800166 results.add(
167 handler.prepare(handler.name, chroot, sysroot_path, build_target,
168 input_artifacts, profile_info))
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700169
170 # Translate the returns from the handlers we called.
171 # If any NEEDED => NEEDED
172 # elif any UNKNOWN => UNKNOWN
173 # elif any POINTLESS => POINTLESS
174 # else UNKNOWN.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700175 if toolchain_util.PrepareForBuildReturn.NEEDED in results:
LaMont Jonesfd68cb12020-04-29 16:43:06 -0600176 output_proto.build_relevance = PrepareForBuildResponse.NEEDED
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700177 elif toolchain_util.PrepareForBuildReturn.UNKNOWN in results:
LaMont Jonesfd68cb12020-04-29 16:43:06 -0600178 output_proto.build_relevance = PrepareForBuildResponse.UNKNOWN
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700179 elif toolchain_util.PrepareForBuildReturn.POINTLESS in results:
LaMont Jonesfd68cb12020-04-29 16:43:06 -0600180 output_proto.build_relevance = PrepareForBuildResponse.POINTLESS
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700181 else:
LaMont Jonesfd68cb12020-04-29 16:43:06 -0600182 output_proto.build_relevance = PrepareForBuildResponse.UNKNOWN
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700183 return controller.RETURN_CODE_SUCCESS
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700184
185
186# TODO(crbug/1031213): When @faux is expanded to have more than success/failure,
187# this should be changed.
188@faux.all_empty
LaMont Jonese911df02020-04-16 12:40:17 -0600189@validate.require('chroot.path', 'output_dir', 'artifact_types')
LaMont Jones4579e8c2019-12-06 14:20:37 -0700190@validate.exists('output_dir')
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700191@validate.validation_complete
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700192def BundleArtifacts(input_proto, output_proto, _config):
193 """Bundle toolchain artifacts.
194
195 The handlers (from _TOOLCHAIN_ARTIFACT_HANDLERS above) are called with:
LaMont Jonesa215f1e2019-12-06 10:18:58 -0700196 artifact_name (str): name of the artifact type
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700197 chroot (chroot_lib.Chroot): chroot
LaMont Jonese911df02020-04-16 12:40:17 -0600198 sysroot_path (str): sysroot path inside the chroot (e.g., /build/atlas),
199 or None.
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700200 chrome_root (str): path to chrome root. (e.g., /b/s/w/ir/k/chrome)
LaMont Jonese911df02020-04-16 12:40:17 -0600201 build_target_name (str): name of the build target (e.g., atlas), or None.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700202 output_dir (str): absolute path where artifacts are being bundled.
203 (e.g., /b/s/w/ir/k/recipe_cleanup/artifactssptfMU)
LaMont Jonese7821672020-04-09 08:56:26 -0600204 profile_info ({(str) name: (str) value}) Dictionary containing profile
205 information.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700206
207 Note: the actual upload to GS is done by CI, not here.
208
209 Args:
210 input_proto (BundleToolchainRequest): The input proto
211 output_proto (BundleToolchainResponse): The output proto
212 _config (api_config.ApiConfig): The API call config.
213 """
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700214 chroot = controller_util.ParseChroot(input_proto.chroot)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700215
LaMont Jonese7821672020-04-09 08:56:26 -0600216 profile_info = _GetProfileInfoDict(input_proto.profile_info)
LaMont Jones45ca6c42020-02-05 09:39:09 -0700217
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700218 for artifact_type in input_proto.artifact_types:
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700219 if artifact_type not in _TOOLCHAIN_ARTIFACT_HANDLERS:
220 logging.error('%s not understood', artifact_type)
221 return controller.RETURN_CODE_UNRECOVERABLE
222 handler = _TOOLCHAIN_ARTIFACT_HANDLERS[artifact_type]
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700223 if handler and handler.bundle:
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800224 artifacts = handler.bundle(handler.name, chroot, input_proto.sysroot.path,
225 input_proto.sysroot.build_target.name,
226 input_proto.output_dir, profile_info)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700227 if artifacts:
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700228 art_info = output_proto.artifacts_info.add()
229 art_info.artifact_type = artifact_type
230 for artifact in artifacts:
231 art_info.artifacts.add().path = artifact
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700232
233
Tiancong Wangd5214132021-01-12 10:43:57 -0800234def _GetUpdatedFilesResponse(_input_proto, output_proto, _config):
235 """Add successful status to the faux response."""
236 file_info = output_proto.updated_files.add()
237 file_info.path = '/any/modified/file'
238 output_proto.commit_message = 'Commit message'
239
240
241@faux.empty_error
242@faux.success(_GetUpdatedFilesResponse)
243@validate.require('uploaded_artifacts')
244@validate.validation_complete
245def GetUpdatedFiles(input_proto, output_proto, _config):
246 """Use uploaded artifacts to update some updates in a chromeos checkout.
247
248 The function will call toolchain_util.GetUpdatedFiles using the type of
249 uploaded artifacts to make some changes in a checkout, and return the list
250 of change files together with commit message.
251 updated_artifacts: A list of UpdatedArtifacts type which contains a tuple
252 of artifact info and profile info.
253 Note: the actual creation of the commit is done by CI, not here.
254
255 Args:
256 input_proto (GetUpdatedFilesRequest): The input proto
257 output_proto (GetUpdatedFilesResponse): The output proto
258 _config (api_config.ApiConfig): The API call config.
259 """
260 commit_message = ''
261 for artifact in input_proto.uploaded_artifacts:
262 artifact_type = artifact.artifact_info.artifact_type
263 if artifact_type not in _TOOLCHAIN_COMMIT_HANDLERS:
264 logging.error('%s not understood', artifact_type)
265 return controller.RETURN_CODE_UNRECOVERABLE
266 artifact_name = _TOOLCHAIN_COMMIT_HANDLERS[artifact_type]
267 if artifact_name:
268 assert len(artifact.artifact_info.artifacts) == 1, (
269 'Only one file to update per each artifact')
270 updated_files, message = toolchain_util.GetUpdatedFiles(
271 artifact_name, artifact.artifact_info.artifacts[0].path,
272 _GetProfileInfoDict(artifact.profile_info))
273 for f in updated_files:
274 file_info = output_proto.updated_files.add()
275 file_info.path = f
276
277 commit_message += message + '\n'
278 output_proto.commit_message = commit_message
279 # No commit footer is added for now. Can add more here if needed
280
281
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700282# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
283_NAMES_FOR_AFDO_ARTIFACTS = {
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700284 toolchain_pb2.ORDERFILE: 'orderfile',
285 toolchain_pb2.KERNEL_AFDO: 'kernel_afdo',
286 toolchain_pb2.CHROME_AFDO: 'chrome_afdo'
287}
288
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800289
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700290# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700291# Using a function instead of a dict because we need to mock these
292# functions in unittest, and mock doesn't play well with a dict definition.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700293def _GetMethodForUpdatingAFDOArtifacts(artifact_type):
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700294 return {
295 toolchain_pb2.ORDERFILE: toolchain_util.OrderfileUpdateChromeEbuild,
296 toolchain_pb2.KERNEL_AFDO: toolchain_util.AFDOUpdateKernelEbuild,
297 toolchain_pb2.CHROME_AFDO: toolchain_util.AFDOUpdateChromeEbuild
298 }[artifact_type]
Tiancong Wang24a3df72019-08-20 15:48:51 -0700299
Tiancong Wangaf050172019-07-10 11:52:03 -0700300
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700301# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700302def _UpdateEbuildWithAFDOArtifactsResponse(_input_proto, output_proto, _config):
303 """Add successful status to the faux response."""
304 output_proto.status = True
305
306
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700307# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700308@faux.success(_UpdateEbuildWithAFDOArtifactsResponse)
309@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -0600310@validate.require('build_target.name')
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700311@validate.is_in('artifact_type', _NAMES_FOR_AFDO_ARTIFACTS)
Alex Klein231d2da2019-07-22 16:44:45 -0600312@validate.validation_complete
Tiancong Wang24a3df72019-08-20 15:48:51 -0700313def UpdateEbuildWithAFDOArtifacts(input_proto, output_proto, _config):
314 """Update Chrome or kernel ebuild with most recent unvetted artifacts.
Tiancong Wangaf050172019-07-10 11:52:03 -0700315
316 Args:
Tiancong Wang24a3df72019-08-20 15:48:51 -0700317 input_proto (VerifyAFDOArtifactsRequest): The input proto
318 output_proto (VerifyAFDOArtifactsResponse): The output proto
Alex Klein231d2da2019-07-22 16:44:45 -0600319 _config (api_config.ApiConfig): The API call config.
Tiancong Wangaf050172019-07-10 11:52:03 -0700320 """
Tiancong Wangaf050172019-07-10 11:52:03 -0700321 board = input_proto.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700322 update_method = _GetMethodForUpdatingAFDOArtifacts(input_proto.artifact_type)
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700323 output_proto.status = update_method(board)
Tiancong Wangaf050172019-07-10 11:52:03 -0700324
325
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700326# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700327def _UploadVettedAFDOArtifactsResponse(_input_proto, output_proto, _config):
328 """Add successful status to the faux response."""
329 output_proto.status = True
330
331
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700332# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700333@faux.success(_UploadVettedAFDOArtifactsResponse)
334@faux.empty_error
Tiancong Wang24a3df72019-08-20 15:48:51 -0700335@validate.require('build_target.name')
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700336@validate.is_in('artifact_type', _NAMES_FOR_AFDO_ARTIFACTS)
Alex Klein231d2da2019-07-22 16:44:45 -0600337@validate.validation_complete
Tiancong Wang24a3df72019-08-20 15:48:51 -0700338def UploadVettedAFDOArtifacts(input_proto, output_proto, _config):
Tiancong Wangaf050172019-07-10 11:52:03 -0700339 """Upload a vetted orderfile to GS bucket.
340
341 Args:
Tiancong Wang24a3df72019-08-20 15:48:51 -0700342 input_proto (VerifyAFDOArtifactsRequest): The input proto
343 output_proto (VerifyAFDOArtifactsResponse): The output proto
Alex Klein231d2da2019-07-22 16:44:45 -0600344 _config (api_config.ApiConfig): The API call config.
Tiancong Wangaf050172019-07-10 11:52:03 -0700345 """
Tiancong Wang24a3df72019-08-20 15:48:51 -0700346 board = input_proto.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700347 artifact_type = _NAMES_FOR_AFDO_ARTIFACTS[input_proto.artifact_type]
Tiancong Wang24a3df72019-08-20 15:48:51 -0700348 output_proto.status = toolchain_util.UploadAndPublishVettedAFDOArtifacts(
349 artifact_type, board)
Ryan Beltran40e4ad12021-05-17 19:55:03 +0000350
351
352def _fetch_clippy_lints():
353 """Get lints created by Cargo Clippy during emerge."""
354 lints_dir = '/tmp/cargo_clippy'
355 # TODO(b/188578936): determine relevant files for file filter
356 file_filter = '/**/*'
357 findings = tricium_cargo_clippy.parse_files(lints_dir)
358 findings = tricium_cargo_clippy.filter_diagnostics(findings, file_filter)
359 findings_protos = []
360 for finding in findings:
361 location_protos = []
362 for location in finding.locations:
363 location_protos.append(
364 toolchain_pb2.LinterFindingLocation(
365 filepath=location.file_path,
366 line_start=location.line_start,
367 line_end=location.line_end
368 )
369 )
370 findings_protos.append(
371 toolchain_pb2.LinterFinding(
372 message=finding.message,
373 locations=location_protos
374 )
375 )
376 return findings_protos
377
378
Ryan Beltran55a84832021-06-30 23:01:21 +0000379@validate.exists('sysroot.path')
380@validate.require('packages')
381@validate.validation_complete
Ryan Beltran40e4ad12021-05-17 19:55:03 +0000382def GetClippyLints(input_proto, output_proto, _config):
383 """Emerges the given packages and retrieves any findings from Cargo Clippy."""
384 chroot_util.Emerge(
385 [package.package_name for package in input_proto.packages],
386 sysroot=input_proto.sysroot.path,
387 with_deps=False,
388 rebuild_deps=True,
389 # TODO(b/188590586): consider setting jobs to emerge in parallel
390 )
391 output_proto.findings.extend(_fetch_clippy_lints())