blob: dd9e1d44bf1dc52d74082a72b249ef55c84d1d55 [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
Mike Frysingeref94e4c2020-02-10 23:59:54 -050011import sys
LaMont Jonesb20b3d92019-11-23 11:47:48 -070012
LaMont Jones5d2edcb2019-12-23 11:32:03 -070013from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060014from chromite.api import faux
Alex Klein231d2da2019-07-22 16:44:45 -060015from chromite.api import validate
LaMont Jones5d2edcb2019-12-23 11:32:03 -070016from chromite.api.controller import controller_util
Tiancong Wang24a3df72019-08-20 15:48:51 -070017from chromite.api.gen.chromite.api import toolchain_pb2
LaMont Jonesb20b3d92019-11-23 11:47:48 -070018from chromite.api.gen.chromiumos.builder_config_pb2 import BuilderConfig
LaMont Jones5d2edcb2019-12-23 11:32:03 -070019from chromite.lib import cros_logging as logging
20from chromite.lib import toolchain_util
Tiancong Wangaf050172019-07-10 11:52:03 -070021
Mike Frysingeref94e4c2020-02-10 23:59:54 -050022
23assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
24
25
LaMont Jonesb20b3d92019-11-23 11:47:48 -070026_Handlers = collections.namedtuple('_Handlers', ['name', 'prepare', 'bundle'])
27_TOOLCHAIN_ARTIFACT_HANDLERS = {
LaMont Jonescd1503d2020-03-04 09:09:59 -070028 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE:
LaMont Jonesd3944582020-03-04 10:37:05 -070029 _Handlers('UnverifiedChromeLlvmOrderfile',
LaMont Jones5d2edcb2019-12-23 11:32:03 -070030 toolchain_util.PrepareForBuild,
31 toolchain_util.BundleArtifacts),
LaMont Jonescd1503d2020-03-04 09:09:59 -070032 BuilderConfig.Artifacts.VERIFIED_CHROME_LLVM_ORDERFILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070033 _Handlers('VerifiedChromeLlvmOrderfile', toolchain_util.PrepareForBuild,
LaMont Jones5d2edcb2019-12-23 11:32:03 -070034 toolchain_util.BundleArtifacts),
LaMont Jonesb20b3d92019-11-23 11:47:48 -070035 BuilderConfig.Artifacts.CHROME_CLANG_WARNINGS_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070036 _Handlers('ChromeClangWarningsFile', toolchain_util.PrepareForBuild,
LaMont Jones90bab632020-01-27 15:58:26 -070037 toolchain_util.BundleArtifacts),
LaMont Jonesb20b3d92019-11-23 11:47:48 -070038 BuilderConfig.Artifacts.UNVERIFIED_LLVM_PGO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070039 _Handlers('UnverifiedLlvmPgoFile', toolchain_util.PrepareForBuild,
LaMont Jones90bab632020-01-27 15:58:26 -070040 toolchain_util.BundleArtifacts),
LaMont Jones45ca6c42020-02-05 09:39:09 -070041 BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE:
42 _Handlers('UnverifiedChromeBenchmarkAfdoFile',
LaMont Jones90bab632020-01-27 15:58:26 -070043 toolchain_util.PrepareForBuild,
44 toolchain_util.BundleArtifacts),
LaMont Jones3fed7f22020-03-04 10:15:11 -070045 BuilderConfig.Artifacts.CHROME_DEBUG_BINARY:
46 _Handlers('ChromeDebugBinary',
47 toolchain_util.PrepareForBuild,
48 toolchain_util.BundleArtifacts),
LaMont Jones53bddd02020-03-12 15:02:54 -060049 BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_PERF_FILE:
50 _Handlers('UnverifiedChromeBenchmarkPerfFile',
51 toolchain_util.PrepareForBuild,
52 toolchain_util.BundleArtifacts),
LaMont Jones45ca6c42020-02-05 09:39:09 -070053 BuilderConfig.Artifacts.VERIFIED_CHROME_BENCHMARK_AFDO_FILE:
54 _Handlers('VerifiedChromeBenchmarkAfdoFile',
LaMont Jones90bab632020-01-27 15:58:26 -070055 toolchain_util.PrepareForBuild,
56 toolchain_util.BundleArtifacts),
LaMont Jones45ca6c42020-02-05 09:39:09 -070057 BuilderConfig.Artifacts.UNVERIFIED_KERNEL_CWP_AFDO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070058 _Handlers('UnverifiedKernelCwpAfdoFile', toolchain_util.PrepareForBuild,
LaMont Jones45ca6c42020-02-05 09:39:09 -070059 toolchain_util.BundleArtifacts),
60 BuilderConfig.Artifacts.VERIFIED_KERNEL_CWP_AFDO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070061 _Handlers('VerifiedKernelCwpAfdoFile', toolchain_util.PrepareForBuild,
LaMont Jones45ca6c42020-02-05 09:39:09 -070062 toolchain_util.BundleArtifacts),
63 BuilderConfig.Artifacts.UNVERIFIED_CHROME_CWP_AFDO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070064 _Handlers('UnverifiedChromeCwpAfdoFile', toolchain_util.PrepareForBuild,
LaMont Jones45ca6c42020-02-05 09:39:09 -070065 toolchain_util.BundleArtifacts),
66 BuilderConfig.Artifacts.VERIFIED_CHROME_CWP_AFDO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070067 _Handlers('VerifiedChromeCwpAfdoFile', toolchain_util.PrepareForBuild,
LaMont Jones45ca6c42020-02-05 09:39:09 -070068 toolchain_util.BundleArtifacts),
69 BuilderConfig.Artifacts.VERIFIED_RELEASE_AFDO_FILE:
LaMont Jones3fed7f22020-03-04 10:15:11 -070070 _Handlers('VerifiedReleaseAfdoFile', toolchain_util.PrepareForBuild,
LaMont Jones90bab632020-01-27 15:58:26 -070071 toolchain_util.BundleArtifacts),
LaMont Jonesb20b3d92019-11-23 11:47:48 -070072}
73
74
LaMont Jonese7821672020-04-09 08:56:26 -060075def _GetProfileInfoDict(profile_info):
76 """Convert profile_info to a dict.
77
78 Args:
79 profile_info (ArtifactProfileInfo): The artifact profile_info.
80
81 Returns:
82 A dictionary containing profile info.
83 """
84 ret = {}
85 which = profile_info.WhichOneof('artifact_profile_info')
86 if which:
87 value = getattr(profile_info, which)
88 # If it is a message, then use the contents of the message. This works as
89 # long as simple types do not have a 'DESCRIPTOR' attribute. (And protobuf
90 # messages do.)
91 if getattr(value, 'DESCRIPTOR', None):
92 ret.update({k.name: v for k, v in value.ListFields()})
93 else:
94 ret[which] = value
95 return ret
96
97
LaMont Jonesb20b3d92019-11-23 11:47:48 -070098# TODO(crbug/1031213): When @faux is expanded to have more than success/failure,
99# this should be changed.
100@faux.all_empty
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700101@validate.require('artifact_types')
102# Note: chroot and sysroot are unspecified the first time that the build_target
103# recipe calls PrepareForBuild. The second time, they are specified. No
104# validation check because "all" values are valid.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700105@validate.validation_complete
106def PrepareForBuild(input_proto, output_proto, _config):
107 """Prepare to build toolchain artifacts.
108
109 The handlers (from _TOOLCHAIN_ARTIFACT_HANDLERS above) are called with:
110 artifact_name (str): name of the artifact type.
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700111 chroot (chroot_lib.Chroot): chroot. Will be None if the chroot has not
112 yet been created.
113 sysroot_path (str): sysroot path inside the chroot (e.g., /build/atlas).
114 Will be an empty string if the sysroot has not yet been created.
115 build_target_name (str): name of the build target (e.g., atlas). Will be
116 an empty string if the sysroot has not yet been created.
117 input_artifacts ({(str) name:[str gs_locations]}): locations for possible
118 input artifacts. The handler is expected to know which keys it should
119 be using, and ignore any keys that it does not understand.
LaMont Jonese7821672020-04-09 08:56:26 -0600120 profile_info ({(str) name: (str) value}) Dictionary containing profile
121 information.
LaMont Jonesa215f1e2019-12-06 10:18:58 -0700122
123 They locate and modify any ebuilds and/or source required for the artifact
124 being created, then return a value from toolchain_util.PrepareForBuildReturn.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700125
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700126 This function sets output_proto.build_relevance to the result.
127
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700128 Args:
129 input_proto (PrepareForToolchainBuildRequest): The input proto
130 output_proto (PrepareForToolchainBuildResponse): The output proto
131 _config (api_config.ApiConfig): The API call config.
132 """
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700133 if input_proto.chroot.path:
134 chroot = controller_util.ParseChroot(input_proto.chroot)
135 else:
136 chroot = None
LaMont Jones4579e8c2019-12-06 14:20:37 -0700137
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700138 input_artifacts = collections.defaultdict(list)
139 for art in input_proto.input_artifacts:
140 item = _TOOLCHAIN_ARTIFACT_HANDLERS.get(art.input_artifact_type)
141 if item:
142 input_artifacts[item.name].extend(
143 ['gs://%s' % str(x) for x in art.input_artifact_gs_locations])
144
LaMont Jonese7821672020-04-09 08:56:26 -0600145 profile_info = _GetProfileInfoDict(input_proto.profile_info)
LaMont Jones45ca6c42020-02-05 09:39:09 -0700146
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700147 results = set()
148 sysroot_path = input_proto.sysroot.path
149 build_target = input_proto.sysroot.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700150 for artifact_type in input_proto.artifact_types:
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700151 # Unknown artifact_types are an error.
152 handler = _TOOLCHAIN_ARTIFACT_HANDLERS[artifact_type]
153 if handler.prepare:
LaMont Jonesa215f1e2019-12-06 10:18:58 -0700154 results.add(handler.prepare(
LaMont Jones45ca6c42020-02-05 09:39:09 -0700155 handler.name, chroot, sysroot_path, build_target, input_artifacts,
LaMont Jonese7821672020-04-09 08:56:26 -0600156 profile_info))
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700157
158 # Translate the returns from the handlers we called.
159 # If any NEEDED => NEEDED
160 # elif any UNKNOWN => UNKNOWN
161 # elif any POINTLESS => POINTLESS
162 # else UNKNOWN.
163 proto_resp = toolchain_pb2.PrepareForToolchainBuildResponse
164 if toolchain_util.PrepareForBuildReturn.NEEDED in results:
165 output_proto.build_relevance = proto_resp.NEEDED
166 elif toolchain_util.PrepareForBuildReturn.UNKNOWN in results:
167 output_proto.build_relevance = proto_resp.UNKNOWN
168 elif toolchain_util.PrepareForBuildReturn.POINTLESS in results:
169 output_proto.build_relevance = proto_resp.POINTLESS
170 else:
171 output_proto.build_relevance = proto_resp.UNKNOWN
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700172 return controller.RETURN_CODE_SUCCESS
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700173
174
175# TODO(crbug/1031213): When @faux is expanded to have more than success/failure,
176# this should be changed.
177@faux.all_empty
LaMont Jones4579e8c2019-12-06 14:20:37 -0700178@validate.require('chroot.path', 'sysroot.path', 'sysroot.build_target.name',
179 'output_dir', 'artifact_types')
180@validate.exists('output_dir')
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700181@validate.validation_complete
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700182def BundleArtifacts(input_proto, output_proto, _config):
183 """Bundle toolchain artifacts.
184
185 The handlers (from _TOOLCHAIN_ARTIFACT_HANDLERS above) are called with:
LaMont Jonesa215f1e2019-12-06 10:18:58 -0700186 artifact_name (str): name of the artifact type
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700187 chroot (chroot_lib.Chroot): chroot
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700188 sysroot_path (str): sysroot path inside the chroot (e.g., /build/atlas)
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700189 chrome_root (str): path to chrome root. (e.g., /b/s/w/ir/k/chrome)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700190 build_target_name (str): name of the build target (e.g., atlas)
191 output_dir (str): absolute path where artifacts are being bundled.
192 (e.g., /b/s/w/ir/k/recipe_cleanup/artifactssptfMU)
LaMont Jonese7821672020-04-09 08:56:26 -0600193 profile_info ({(str) name: (str) value}) Dictionary containing profile
194 information.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700195
196 Note: the actual upload to GS is done by CI, not here.
197
198 Args:
199 input_proto (BundleToolchainRequest): The input proto
200 output_proto (BundleToolchainResponse): The output proto
201 _config (api_config.ApiConfig): The API call config.
202 """
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700203 chroot = controller_util.ParseChroot(input_proto.chroot)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700204
LaMont Jonese7821672020-04-09 08:56:26 -0600205 profile_info = _GetProfileInfoDict(input_proto.profile_info)
LaMont Jones45ca6c42020-02-05 09:39:09 -0700206
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700207 for artifact_type in input_proto.artifact_types:
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700208 if artifact_type not in _TOOLCHAIN_ARTIFACT_HANDLERS:
209 logging.error('%s not understood', artifact_type)
210 return controller.RETURN_CODE_UNRECOVERABLE
211 handler = _TOOLCHAIN_ARTIFACT_HANDLERS[artifact_type]
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700212 if handler and handler.bundle:
213 artifacts = handler.bundle(
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700214 handler.name, chroot, input_proto.sysroot.path,
LaMont Jones45ca6c42020-02-05 09:39:09 -0700215 input_proto.sysroot.build_target.name, input_proto.output_dir,
LaMont Jonese7821672020-04-09 08:56:26 -0600216 profile_info)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700217 if artifacts:
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700218 art_info = output_proto.artifacts_info.add()
219 art_info.artifact_type = artifact_type
220 for artifact in artifacts:
221 art_info.artifacts.add().path = artifact
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700222
223
224# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
225_NAMES_FOR_AFDO_ARTIFACTS = {
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700226 toolchain_pb2.ORDERFILE: 'orderfile',
227 toolchain_pb2.KERNEL_AFDO: 'kernel_afdo',
228 toolchain_pb2.CHROME_AFDO: 'chrome_afdo'
229}
230
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700231# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700232# Using a function instead of a dict because we need to mock these
233# functions in unittest, and mock doesn't play well with a dict definition.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700234def _GetMethodForUpdatingAFDOArtifacts(artifact_type):
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700235 return {
236 toolchain_pb2.ORDERFILE: toolchain_util.OrderfileUpdateChromeEbuild,
237 toolchain_pb2.KERNEL_AFDO: toolchain_util.AFDOUpdateKernelEbuild,
238 toolchain_pb2.CHROME_AFDO: toolchain_util.AFDOUpdateChromeEbuild
239 }[artifact_type]
Tiancong Wang24a3df72019-08-20 15:48:51 -0700240
Tiancong Wangaf050172019-07-10 11:52:03 -0700241
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700242# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700243def _UpdateEbuildWithAFDOArtifactsResponse(_input_proto, output_proto, _config):
244 """Add successful status to the faux response."""
245 output_proto.status = True
246
247
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700248# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700249@faux.success(_UpdateEbuildWithAFDOArtifactsResponse)
250@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -0600251@validate.require('build_target.name')
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700252@validate.is_in('artifact_type', _NAMES_FOR_AFDO_ARTIFACTS)
Alex Klein231d2da2019-07-22 16:44:45 -0600253@validate.validation_complete
Tiancong Wang24a3df72019-08-20 15:48:51 -0700254def UpdateEbuildWithAFDOArtifacts(input_proto, output_proto, _config):
255 """Update Chrome or kernel ebuild with most recent unvetted artifacts.
Tiancong Wangaf050172019-07-10 11:52:03 -0700256
257 Args:
Tiancong Wang24a3df72019-08-20 15:48:51 -0700258 input_proto (VerifyAFDOArtifactsRequest): The input proto
259 output_proto (VerifyAFDOArtifactsResponse): The output proto
Alex Klein231d2da2019-07-22 16:44:45 -0600260 _config (api_config.ApiConfig): The API call config.
Tiancong Wangaf050172019-07-10 11:52:03 -0700261 """
Tiancong Wangaf050172019-07-10 11:52:03 -0700262 board = input_proto.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700263 update_method = _GetMethodForUpdatingAFDOArtifacts(input_proto.artifact_type)
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700264 output_proto.status = update_method(board)
Tiancong Wangaf050172019-07-10 11:52:03 -0700265
266
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700267# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700268def _UploadVettedAFDOArtifactsResponse(_input_proto, output_proto, _config):
269 """Add successful status to the faux response."""
270 output_proto.status = True
271
272
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700273# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700274@faux.success(_UploadVettedAFDOArtifactsResponse)
275@faux.empty_error
Tiancong Wang24a3df72019-08-20 15:48:51 -0700276@validate.require('build_target.name')
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700277@validate.is_in('artifact_type', _NAMES_FOR_AFDO_ARTIFACTS)
Alex Klein231d2da2019-07-22 16:44:45 -0600278@validate.validation_complete
Tiancong Wang24a3df72019-08-20 15:48:51 -0700279def UploadVettedAFDOArtifacts(input_proto, output_proto, _config):
Tiancong Wangaf050172019-07-10 11:52:03 -0700280 """Upload a vetted orderfile to GS bucket.
281
282 Args:
Tiancong Wang24a3df72019-08-20 15:48:51 -0700283 input_proto (VerifyAFDOArtifactsRequest): The input proto
284 output_proto (VerifyAFDOArtifactsResponse): The output proto
Alex Klein231d2da2019-07-22 16:44:45 -0600285 _config (api_config.ApiConfig): The API call config.
Tiancong Wangaf050172019-07-10 11:52:03 -0700286 """
Tiancong Wang24a3df72019-08-20 15:48:51 -0700287 board = input_proto.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700288 artifact_type = _NAMES_FOR_AFDO_ARTIFACTS[input_proto.artifact_type]
Tiancong Wang24a3df72019-08-20 15:48:51 -0700289 output_proto.status = toolchain_util.UploadAndPublishVettedAFDOArtifacts(
290 artifact_type, board)