blob: 77dccf7fedb4e681e360da31693bae3dfca98908 [file] [log] [blame]
Tiancong Wangaf050172019-07-10 11:52:03 -07001# -*- coding: utf-8 -*-
2# Copyright 2019 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Toolchain-related operations."""
7
8from __future__ import print_function
9
LaMont Jonesb20b3d92019-11-23 11:47:48 -070010import collections
11
LaMont Jones5d2edcb2019-12-23 11:32:03 -070012from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060013from chromite.api import faux
Alex Klein231d2da2019-07-22 16:44:45 -060014from chromite.api import validate
LaMont Jones5d2edcb2019-12-23 11:32:03 -070015from chromite.api.controller import controller_util
Tiancong Wang24a3df72019-08-20 15:48:51 -070016from chromite.api.gen.chromite.api import toolchain_pb2
LaMont Jonesb20b3d92019-11-23 11:47:48 -070017from chromite.api.gen.chromiumos.builder_config_pb2 import BuilderConfig
LaMont Jones5d2edcb2019-12-23 11:32:03 -070018from chromite.lib import cros_logging as logging
19from chromite.lib import toolchain_util
LaMont Jonesfd68cb12020-04-29 16:43:06 -060020from chromite.api.gen.chromite.api.artifacts_pb2 import PrepareForBuildResponse
Tiancong Wangaf050172019-07-10 11:52:03 -070021
LaMont Jonesb20b3d92019-11-23 11:47:48 -070022_Handlers = collections.namedtuple('_Handlers', ['name', 'prepare', 'bundle'])
23_TOOLCHAIN_ARTIFACT_HANDLERS = {
LaMont Jonescd1503d2020-03-04 09:09:59 -070024 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE:
LaMont Jonesd3944582020-03-04 10:37:05 -070025 _Handlers('UnverifiedChromeLlvmOrderfile',
LaMont Jones5d2edcb2019-12-23 11:32:03 -070026 toolchain_util.PrepareForBuild,
27 toolchain_util.BundleArtifacts),
LaMont Jonescd1503d2020-03-04 09:09:59 -070028 BuilderConfig.Artifacts.VERIFIED_CHROME_LLVM_ORDERFILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070029 _Handlers('VerifiedChromeLlvmOrderfile', toolchain_util.PrepareForBuild,
LaMont Jones5d2edcb2019-12-23 11:32:03 -070030 toolchain_util.BundleArtifacts),
LaMont Jonesb20b3d92019-11-23 11:47:48 -070031 BuilderConfig.Artifacts.CHROME_CLANG_WARNINGS_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070032 _Handlers('ChromeClangWarningsFile', toolchain_util.PrepareForBuild,
LaMont Jones90bab632020-01-27 15:58:26 -070033 toolchain_util.BundleArtifacts),
LaMont Jonesb20b3d92019-11-23 11:47:48 -070034 BuilderConfig.Artifacts.UNVERIFIED_LLVM_PGO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070035 _Handlers('UnverifiedLlvmPgoFile', toolchain_util.PrepareForBuild,
LaMont Jones90bab632020-01-27 15:58:26 -070036 toolchain_util.BundleArtifacts),
LaMont Jones45ca6c42020-02-05 09:39:09 -070037 BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE:
38 _Handlers('UnverifiedChromeBenchmarkAfdoFile',
LaMont Jones90bab632020-01-27 15:58:26 -070039 toolchain_util.PrepareForBuild,
40 toolchain_util.BundleArtifacts),
LaMont Jones3fed7f22020-03-04 10:15:11 -070041 BuilderConfig.Artifacts.CHROME_DEBUG_BINARY:
Tiancong Wangba2a1c22021-01-19 10:45:06 -080042 _Handlers('ChromeDebugBinary', toolchain_util.PrepareForBuild,
LaMont Jones3fed7f22020-03-04 10:15:11 -070043 toolchain_util.BundleArtifacts),
LaMont Jones53bddd02020-03-12 15:02:54 -060044 BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_PERF_FILE:
45 _Handlers('UnverifiedChromeBenchmarkPerfFile',
46 toolchain_util.PrepareForBuild,
47 toolchain_util.BundleArtifacts),
LaMont Jones45ca6c42020-02-05 09:39:09 -070048 BuilderConfig.Artifacts.VERIFIED_CHROME_BENCHMARK_AFDO_FILE:
49 _Handlers('VerifiedChromeBenchmarkAfdoFile',
LaMont Jones90bab632020-01-27 15:58:26 -070050 toolchain_util.PrepareForBuild,
51 toolchain_util.BundleArtifacts),
LaMont Jones45ca6c42020-02-05 09:39:09 -070052 BuilderConfig.Artifacts.UNVERIFIED_KERNEL_CWP_AFDO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070053 _Handlers('UnverifiedKernelCwpAfdoFile', toolchain_util.PrepareForBuild,
LaMont Jones45ca6c42020-02-05 09:39:09 -070054 toolchain_util.BundleArtifacts),
55 BuilderConfig.Artifacts.VERIFIED_KERNEL_CWP_AFDO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070056 _Handlers('VerifiedKernelCwpAfdoFile', toolchain_util.PrepareForBuild,
LaMont Jones45ca6c42020-02-05 09:39:09 -070057 toolchain_util.BundleArtifacts),
58 BuilderConfig.Artifacts.UNVERIFIED_CHROME_CWP_AFDO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070059 _Handlers('UnverifiedChromeCwpAfdoFile', toolchain_util.PrepareForBuild,
LaMont Jones45ca6c42020-02-05 09:39:09 -070060 toolchain_util.BundleArtifacts),
61 BuilderConfig.Artifacts.VERIFIED_CHROME_CWP_AFDO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070062 _Handlers('VerifiedChromeCwpAfdoFile', toolchain_util.PrepareForBuild,
LaMont Jones45ca6c42020-02-05 09:39:09 -070063 toolchain_util.BundleArtifacts),
64 BuilderConfig.Artifacts.VERIFIED_RELEASE_AFDO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070065 _Handlers('VerifiedReleaseAfdoFile', toolchain_util.PrepareForBuild,
LaMont Jones90bab632020-01-27 15:58:26 -070066 toolchain_util.BundleArtifacts),
Tiancong Wang91cf1dd2020-05-05 10:30:22 -070067 BuilderConfig.Artifacts.TOOLCHAIN_WARNING_LOGS:
68 _Handlers('ToolchainWarningLogs', toolchain_util.PrepareForBuild,
69 toolchain_util.BundleArtifacts),
Tiancong Wangfe3dbd22020-06-12 15:45:55 -070070 BuilderConfig.Artifacts.CHROME_AFDO_PROFILE_FOR_ANDROID_LINUX:
71 _Handlers('ChromeAFDOProfileForAndroidLinux',
72 toolchain_util.PrepareForBuild,
73 toolchain_util.BundleArtifacts),
Jian Cai6190cc82020-06-12 16:24:32 -070074 BuilderConfig.Artifacts.CLANG_CRASH_DIAGNOSES:
75 _Handlers('ClangCrashDiagnoses', toolchain_util.PrepareForBuild,
76 toolchain_util.BundleArtifacts),
Ryan Beltran0be7dcf2020-12-09 18:31:53 +000077 BuilderConfig.Artifacts.COMPILER_RUSAGE_LOG:
78 _Handlers('CompilerRusageLogs', toolchain_util.PrepareForBuild,
79 toolchain_util.BundleArtifacts),
LaMont Jonesb20b3d92019-11-23 11:47:48 -070080}
81
82
LaMont Jonese7821672020-04-09 08:56:26 -060083def _GetProfileInfoDict(profile_info):
84 """Convert profile_info to a dict.
85
86 Args:
87 profile_info (ArtifactProfileInfo): The artifact profile_info.
88
89 Returns:
90 A dictionary containing profile info.
91 """
92 ret = {}
93 which = profile_info.WhichOneof('artifact_profile_info')
94 if which:
95 value = getattr(profile_info, which)
96 # If it is a message, then use the contents of the message. This works as
97 # long as simple types do not have a 'DESCRIPTOR' attribute. (And protobuf
98 # messages do.)
99 if getattr(value, 'DESCRIPTOR', None):
100 ret.update({k.name: v for k, v in value.ListFields()})
101 else:
102 ret[which] = value
103 return ret
104
105
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700106# TODO(crbug/1031213): When @faux is expanded to have more than success/failure,
107# this should be changed.
108@faux.all_empty
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700109@validate.require('artifact_types')
110# Note: chroot and sysroot are unspecified the first time that the build_target
111# recipe calls PrepareForBuild. The second time, they are specified. No
112# validation check because "all" values are valid.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700113@validate.validation_complete
114def PrepareForBuild(input_proto, output_proto, _config):
115 """Prepare to build toolchain artifacts.
116
117 The handlers (from _TOOLCHAIN_ARTIFACT_HANDLERS above) are called with:
118 artifact_name (str): name of the artifact type.
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700119 chroot (chroot_lib.Chroot): chroot. Will be None if the chroot has not
120 yet been created.
121 sysroot_path (str): sysroot path inside the chroot (e.g., /build/atlas).
122 Will be an empty string if the sysroot has not yet been created.
123 build_target_name (str): name of the build target (e.g., atlas). Will be
124 an empty string if the sysroot has not yet been created.
125 input_artifacts ({(str) name:[str gs_locations]}): locations for possible
126 input artifacts. The handler is expected to know which keys it should
127 be using, and ignore any keys that it does not understand.
LaMont Jonese7821672020-04-09 08:56:26 -0600128 profile_info ({(str) name: (str) value}) Dictionary containing profile
129 information.
LaMont Jonesa215f1e2019-12-06 10:18:58 -0700130
131 They locate and modify any ebuilds and/or source required for the artifact
132 being created, then return a value from toolchain_util.PrepareForBuildReturn.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700133
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700134 This function sets output_proto.build_relevance to the result.
135
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700136 Args:
137 input_proto (PrepareForToolchainBuildRequest): The input proto
138 output_proto (PrepareForToolchainBuildResponse): The output proto
139 _config (api_config.ApiConfig): The API call config.
140 """
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700141 if input_proto.chroot.path:
142 chroot = controller_util.ParseChroot(input_proto.chroot)
143 else:
144 chroot = None
LaMont Jones4579e8c2019-12-06 14:20:37 -0700145
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700146 input_artifacts = collections.defaultdict(list)
147 for art in input_proto.input_artifacts:
148 item = _TOOLCHAIN_ARTIFACT_HANDLERS.get(art.input_artifact_type)
149 if item:
150 input_artifacts[item.name].extend(
151 ['gs://%s' % str(x) for x in art.input_artifact_gs_locations])
152
LaMont Jonese7821672020-04-09 08:56:26 -0600153 profile_info = _GetProfileInfoDict(input_proto.profile_info)
LaMont Jones45ca6c42020-02-05 09:39:09 -0700154
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700155 results = set()
156 sysroot_path = input_proto.sysroot.path
157 build_target = input_proto.sysroot.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700158 for artifact_type in input_proto.artifact_types:
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700159 # Unknown artifact_types are an error.
160 handler = _TOOLCHAIN_ARTIFACT_HANDLERS[artifact_type]
161 if handler.prepare:
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800162 results.add(
163 handler.prepare(handler.name, chroot, sysroot_path, build_target,
164 input_artifacts, profile_info))
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700165
166 # Translate the returns from the handlers we called.
167 # If any NEEDED => NEEDED
168 # elif any UNKNOWN => UNKNOWN
169 # elif any POINTLESS => POINTLESS
170 # else UNKNOWN.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700171 if toolchain_util.PrepareForBuildReturn.NEEDED in results:
LaMont Jonesfd68cb12020-04-29 16:43:06 -0600172 output_proto.build_relevance = PrepareForBuildResponse.NEEDED
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700173 elif toolchain_util.PrepareForBuildReturn.UNKNOWN in results:
LaMont Jonesfd68cb12020-04-29 16:43:06 -0600174 output_proto.build_relevance = PrepareForBuildResponse.UNKNOWN
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700175 elif toolchain_util.PrepareForBuildReturn.POINTLESS in results:
LaMont Jonesfd68cb12020-04-29 16:43:06 -0600176 output_proto.build_relevance = PrepareForBuildResponse.POINTLESS
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700177 else:
LaMont Jonesfd68cb12020-04-29 16:43:06 -0600178 output_proto.build_relevance = PrepareForBuildResponse.UNKNOWN
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700179 return controller.RETURN_CODE_SUCCESS
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700180
181
182# TODO(crbug/1031213): When @faux is expanded to have more than success/failure,
183# this should be changed.
184@faux.all_empty
LaMont Jonese911df02020-04-16 12:40:17 -0600185@validate.require('chroot.path', 'output_dir', 'artifact_types')
LaMont Jones4579e8c2019-12-06 14:20:37 -0700186@validate.exists('output_dir')
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700187@validate.validation_complete
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700188def BundleArtifacts(input_proto, output_proto, _config):
189 """Bundle toolchain artifacts.
190
191 The handlers (from _TOOLCHAIN_ARTIFACT_HANDLERS above) are called with:
LaMont Jonesa215f1e2019-12-06 10:18:58 -0700192 artifact_name (str): name of the artifact type
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700193 chroot (chroot_lib.Chroot): chroot
LaMont Jonese911df02020-04-16 12:40:17 -0600194 sysroot_path (str): sysroot path inside the chroot (e.g., /build/atlas),
195 or None.
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700196 chrome_root (str): path to chrome root. (e.g., /b/s/w/ir/k/chrome)
LaMont Jonese911df02020-04-16 12:40:17 -0600197 build_target_name (str): name of the build target (e.g., atlas), or None.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700198 output_dir (str): absolute path where artifacts are being bundled.
199 (e.g., /b/s/w/ir/k/recipe_cleanup/artifactssptfMU)
LaMont Jonese7821672020-04-09 08:56:26 -0600200 profile_info ({(str) name: (str) value}) Dictionary containing profile
201 information.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700202
203 Note: the actual upload to GS is done by CI, not here.
204
205 Args:
206 input_proto (BundleToolchainRequest): The input proto
207 output_proto (BundleToolchainResponse): The output proto
208 _config (api_config.ApiConfig): The API call config.
209 """
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700210 chroot = controller_util.ParseChroot(input_proto.chroot)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700211
LaMont Jonese7821672020-04-09 08:56:26 -0600212 profile_info = _GetProfileInfoDict(input_proto.profile_info)
LaMont Jones45ca6c42020-02-05 09:39:09 -0700213
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700214 for artifact_type in input_proto.artifact_types:
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700215 if artifact_type not in _TOOLCHAIN_ARTIFACT_HANDLERS:
216 logging.error('%s not understood', artifact_type)
217 return controller.RETURN_CODE_UNRECOVERABLE
218 handler = _TOOLCHAIN_ARTIFACT_HANDLERS[artifact_type]
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700219 if handler and handler.bundle:
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800220 artifacts = handler.bundle(handler.name, chroot, input_proto.sysroot.path,
221 input_proto.sysroot.build_target.name,
222 input_proto.output_dir, profile_info)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700223 if artifacts:
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700224 art_info = output_proto.artifacts_info.add()
225 art_info.artifact_type = artifact_type
226 for artifact in artifacts:
227 art_info.artifacts.add().path = artifact
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700228
229
230# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
231_NAMES_FOR_AFDO_ARTIFACTS = {
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700232 toolchain_pb2.ORDERFILE: 'orderfile',
233 toolchain_pb2.KERNEL_AFDO: 'kernel_afdo',
234 toolchain_pb2.CHROME_AFDO: 'chrome_afdo'
235}
236
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800237
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700238# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700239# Using a function instead of a dict because we need to mock these
240# functions in unittest, and mock doesn't play well with a dict definition.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700241def _GetMethodForUpdatingAFDOArtifacts(artifact_type):
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700242 return {
243 toolchain_pb2.ORDERFILE: toolchain_util.OrderfileUpdateChromeEbuild,
244 toolchain_pb2.KERNEL_AFDO: toolchain_util.AFDOUpdateKernelEbuild,
245 toolchain_pb2.CHROME_AFDO: toolchain_util.AFDOUpdateChromeEbuild
246 }[artifact_type]
Tiancong Wang24a3df72019-08-20 15:48:51 -0700247
Tiancong Wangaf050172019-07-10 11:52:03 -0700248
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700249# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700250def _UpdateEbuildWithAFDOArtifactsResponse(_input_proto, output_proto, _config):
251 """Add successful status to the faux response."""
252 output_proto.status = True
253
254
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700255# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700256@faux.success(_UpdateEbuildWithAFDOArtifactsResponse)
257@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -0600258@validate.require('build_target.name')
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700259@validate.is_in('artifact_type', _NAMES_FOR_AFDO_ARTIFACTS)
Alex Klein231d2da2019-07-22 16:44:45 -0600260@validate.validation_complete
Tiancong Wang24a3df72019-08-20 15:48:51 -0700261def UpdateEbuildWithAFDOArtifacts(input_proto, output_proto, _config):
262 """Update Chrome or kernel ebuild with most recent unvetted artifacts.
Tiancong Wangaf050172019-07-10 11:52:03 -0700263
264 Args:
Tiancong Wang24a3df72019-08-20 15:48:51 -0700265 input_proto (VerifyAFDOArtifactsRequest): The input proto
266 output_proto (VerifyAFDOArtifactsResponse): The output proto
Alex Klein231d2da2019-07-22 16:44:45 -0600267 _config (api_config.ApiConfig): The API call config.
Tiancong Wangaf050172019-07-10 11:52:03 -0700268 """
Tiancong Wangaf050172019-07-10 11:52:03 -0700269 board = input_proto.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700270 update_method = _GetMethodForUpdatingAFDOArtifacts(input_proto.artifact_type)
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700271 output_proto.status = update_method(board)
Tiancong Wangaf050172019-07-10 11:52:03 -0700272
273
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700274# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700275def _UploadVettedAFDOArtifactsResponse(_input_proto, output_proto, _config):
276 """Add successful status to the faux response."""
277 output_proto.status = True
278
279
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700280# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700281@faux.success(_UploadVettedAFDOArtifactsResponse)
282@faux.empty_error
Tiancong Wang24a3df72019-08-20 15:48:51 -0700283@validate.require('build_target.name')
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700284@validate.is_in('artifact_type', _NAMES_FOR_AFDO_ARTIFACTS)
Alex Klein231d2da2019-07-22 16:44:45 -0600285@validate.validation_complete
Tiancong Wang24a3df72019-08-20 15:48:51 -0700286def UploadVettedAFDOArtifacts(input_proto, output_proto, _config):
Tiancong Wangaf050172019-07-10 11:52:03 -0700287 """Upload a vetted orderfile to GS bucket.
288
289 Args:
Tiancong Wang24a3df72019-08-20 15:48:51 -0700290 input_proto (VerifyAFDOArtifactsRequest): The input proto
291 output_proto (VerifyAFDOArtifactsResponse): The output proto
Alex Klein231d2da2019-07-22 16:44:45 -0600292 _config (api_config.ApiConfig): The API call config.
Tiancong Wangaf050172019-07-10 11:52:03 -0700293 """
Tiancong Wang24a3df72019-08-20 15:48:51 -0700294 board = input_proto.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700295 artifact_type = _NAMES_FOR_AFDO_ARTIFACTS[input_proto.artifact_type]
Tiancong Wang24a3df72019-08-20 15:48:51 -0700296 output_proto.status = toolchain_util.UploadAndPublishVettedAFDOArtifacts(
297 artifact_type, board)