blob: e5e02796235b6c8be83d379cad1f59e7ae133b8c [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 Jonesd3944582020-03-04 10:37:05 -070033 _Handlers('VerifiedChromeLlvmOrderfile',
LaMont Jones5d2edcb2019-12-23 11:32:03 -070034 toolchain_util.PrepareForBuild,
35 toolchain_util.BundleArtifacts),
LaMont Jonesb20b3d92019-11-23 11:47:48 -070036 BuilderConfig.Artifacts.CHROME_CLANG_WARNINGS_FILE:
LaMont Jones5d2edcb2019-12-23 11:32:03 -070037 _Handlers('ChromeClangWarningsFile',
LaMont Jones90bab632020-01-27 15:58:26 -070038 toolchain_util.PrepareForBuild,
39 toolchain_util.BundleArtifacts),
LaMont Jonesb20b3d92019-11-23 11:47:48 -070040 BuilderConfig.Artifacts.UNVERIFIED_LLVM_PGO_FILE:
LaMont Jones5d2edcb2019-12-23 11:32:03 -070041 _Handlers('UnverifiedLlvmPgoFile',
LaMont Jones90bab632020-01-27 15:58:26 -070042 toolchain_util.PrepareForBuild,
43 toolchain_util.BundleArtifacts),
LaMont Jones45ca6c42020-02-05 09:39:09 -070044 BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE:
45 _Handlers('UnverifiedChromeBenchmarkAfdoFile',
LaMont Jones90bab632020-01-27 15:58:26 -070046 toolchain_util.PrepareForBuild,
47 toolchain_util.BundleArtifacts),
LaMont Jones53bddd02020-03-12 15:02:54 -060048 BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_PERF_FILE:
49 _Handlers('UnverifiedChromeBenchmarkPerfFile',
50 toolchain_util.PrepareForBuild,
51 toolchain_util.BundleArtifacts),
LaMont Jones45ca6c42020-02-05 09:39:09 -070052 BuilderConfig.Artifacts.VERIFIED_CHROME_BENCHMARK_AFDO_FILE:
53 _Handlers('VerifiedChromeBenchmarkAfdoFile',
LaMont Jones90bab632020-01-27 15:58:26 -070054 toolchain_util.PrepareForBuild,
55 toolchain_util.BundleArtifacts),
LaMont Jones45ca6c42020-02-05 09:39:09 -070056 BuilderConfig.Artifacts.UNVERIFIED_KERNEL_CWP_AFDO_FILE:
57 _Handlers('UnverifiedKernelCwpAfdoFile',
58 toolchain_util.PrepareForBuild,
59 toolchain_util.BundleArtifacts),
60 BuilderConfig.Artifacts.VERIFIED_KERNEL_CWP_AFDO_FILE:
61 _Handlers('VerifiedKernelCwpAfdoFile',
62 toolchain_util.PrepareForBuild,
63 toolchain_util.BundleArtifacts),
64 BuilderConfig.Artifacts.UNVERIFIED_CHROME_CWP_AFDO_FILE:
65 _Handlers('UnverifiedChromeCwpAfdoFile',
66 toolchain_util.PrepareForBuild,
67 toolchain_util.BundleArtifacts),
68 BuilderConfig.Artifacts.VERIFIED_CHROME_CWP_AFDO_FILE:
69 _Handlers('VerifiedChromeCwpAfdoFile',
70 toolchain_util.PrepareForBuild,
71 toolchain_util.BundleArtifacts),
72 BuilderConfig.Artifacts.VERIFIED_RELEASE_AFDO_FILE:
73 _Handlers('VerifiedReleaseAfdoFile',
LaMont Jones90bab632020-01-27 15:58:26 -070074 toolchain_util.PrepareForBuild,
75 toolchain_util.BundleArtifacts),
LaMont Jonesb20b3d92019-11-23 11:47:48 -070076}
77
78
79# TODO(crbug/1031213): When @faux is expanded to have more than success/failure,
80# this should be changed.
81@faux.all_empty
LaMont Jones5d2edcb2019-12-23 11:32:03 -070082@validate.require('artifact_types')
83# Note: chroot and sysroot are unspecified the first time that the build_target
84# recipe calls PrepareForBuild. The second time, they are specified. No
85# validation check because "all" values are valid.
LaMont Jonesb20b3d92019-11-23 11:47:48 -070086@validate.validation_complete
87def PrepareForBuild(input_proto, output_proto, _config):
88 """Prepare to build toolchain artifacts.
89
90 The handlers (from _TOOLCHAIN_ARTIFACT_HANDLERS above) are called with:
91 artifact_name (str): name of the artifact type.
LaMont Jones5d2edcb2019-12-23 11:32:03 -070092 chroot (chroot_lib.Chroot): chroot. Will be None if the chroot has not
93 yet been created.
94 sysroot_path (str): sysroot path inside the chroot (e.g., /build/atlas).
95 Will be an empty string if the sysroot has not yet been created.
96 build_target_name (str): name of the build target (e.g., atlas). Will be
97 an empty string if the sysroot has not yet been created.
98 input_artifacts ({(str) name:[str gs_locations]}): locations for possible
99 input artifacts. The handler is expected to know which keys it should
100 be using, and ignore any keys that it does not understand.
LaMont Jones45ca6c42020-02-05 09:39:09 -0700101 additional_args ({(str) name: (str) value}) Dictionary of additional
102 arguments.
LaMont Jonesa215f1e2019-12-06 10:18:58 -0700103
104 They locate and modify any ebuilds and/or source required for the artifact
105 being created, then return a value from toolchain_util.PrepareForBuildReturn.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700106
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700107 This function sets output_proto.build_relevance to the result.
108
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700109 Args:
110 input_proto (PrepareForToolchainBuildRequest): The input proto
111 output_proto (PrepareForToolchainBuildResponse): The output proto
112 _config (api_config.ApiConfig): The API call config.
113 """
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700114 if input_proto.chroot.path:
115 chroot = controller_util.ParseChroot(input_proto.chroot)
116 else:
117 chroot = None
LaMont Jones4579e8c2019-12-06 14:20:37 -0700118
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700119 input_artifacts = collections.defaultdict(list)
120 for art in input_proto.input_artifacts:
121 item = _TOOLCHAIN_ARTIFACT_HANDLERS.get(art.input_artifact_type)
122 if item:
123 input_artifacts[item.name].extend(
124 ['gs://%s' % str(x) for x in art.input_artifact_gs_locations])
125
LaMont Jones45ca6c42020-02-05 09:39:09 -0700126 # Pass along any additional args.
127 additional_args = {}
128 which = input_proto.additional_args.WhichOneof('prepare_for_build_args')
129 if which:
130 # All of the additional arguments we understand are strings, so we can
131 # copy whichever argument we got without processing it.
132 additional_args[which] = getattr(input_proto.additional_args, which)
133
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700134 results = set()
135 sysroot_path = input_proto.sysroot.path
136 build_target = input_proto.sysroot.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700137 for artifact_type in input_proto.artifact_types:
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700138 # Unknown artifact_types are an error.
139 handler = _TOOLCHAIN_ARTIFACT_HANDLERS[artifact_type]
140 if handler.prepare:
LaMont Jonesa215f1e2019-12-06 10:18:58 -0700141 results.add(handler.prepare(
LaMont Jones45ca6c42020-02-05 09:39:09 -0700142 handler.name, chroot, sysroot_path, build_target, input_artifacts,
143 additional_args))
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700144
145 # Translate the returns from the handlers we called.
146 # If any NEEDED => NEEDED
147 # elif any UNKNOWN => UNKNOWN
148 # elif any POINTLESS => POINTLESS
149 # else UNKNOWN.
150 proto_resp = toolchain_pb2.PrepareForToolchainBuildResponse
151 if toolchain_util.PrepareForBuildReturn.NEEDED in results:
152 output_proto.build_relevance = proto_resp.NEEDED
153 elif toolchain_util.PrepareForBuildReturn.UNKNOWN in results:
154 output_proto.build_relevance = proto_resp.UNKNOWN
155 elif toolchain_util.PrepareForBuildReturn.POINTLESS in results:
156 output_proto.build_relevance = proto_resp.POINTLESS
157 else:
158 output_proto.build_relevance = proto_resp.UNKNOWN
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700159 return controller.RETURN_CODE_SUCCESS
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700160
161
162# TODO(crbug/1031213): When @faux is expanded to have more than success/failure,
163# this should be changed.
164@faux.all_empty
LaMont Jones4579e8c2019-12-06 14:20:37 -0700165@validate.require('chroot.path', 'sysroot.path', 'sysroot.build_target.name',
166 'output_dir', 'artifact_types')
167@validate.exists('output_dir')
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700168@validate.validation_complete
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700169def BundleArtifacts(input_proto, output_proto, _config):
170 """Bundle toolchain artifacts.
171
172 The handlers (from _TOOLCHAIN_ARTIFACT_HANDLERS above) are called with:
LaMont Jonesa215f1e2019-12-06 10:18:58 -0700173 artifact_name (str): name of the artifact type
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700174 chroot (chroot_lib.Chroot): chroot
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700175 sysroot_path (str): sysroot path inside the chroot (e.g., /build/atlas)
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700176 chrome_root (str): path to chrome root. (e.g., /b/s/w/ir/k/chrome)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700177 build_target_name (str): name of the build target (e.g., atlas)
178 output_dir (str): absolute path where artifacts are being bundled.
179 (e.g., /b/s/w/ir/k/recipe_cleanup/artifactssptfMU)
180
181 Note: the actual upload to GS is done by CI, not here.
182
183 Args:
184 input_proto (BundleToolchainRequest): The input proto
185 output_proto (BundleToolchainResponse): The output proto
186 _config (api_config.ApiConfig): The API call config.
187 """
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700188 chroot = controller_util.ParseChroot(input_proto.chroot)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700189
LaMont Jones45ca6c42020-02-05 09:39:09 -0700190 # Pass along any additional args.
191 additional_args = {}
192 which = input_proto.additional_args.WhichOneof('prepare_for_build_args')
193 if which:
194 # All of the additional arguments we understand are strings, so we can
195 # copy whichever argument we got without processing it.
196 additional_args[which] = getattr(input_proto.additional_args, which)
197
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700198 for artifact_type in input_proto.artifact_types:
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700199 if artifact_type not in _TOOLCHAIN_ARTIFACT_HANDLERS:
200 logging.error('%s not understood', artifact_type)
201 return controller.RETURN_CODE_UNRECOVERABLE
202 handler = _TOOLCHAIN_ARTIFACT_HANDLERS[artifact_type]
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700203 if handler and handler.bundle:
204 artifacts = handler.bundle(
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700205 handler.name, chroot, input_proto.sysroot.path,
LaMont Jones45ca6c42020-02-05 09:39:09 -0700206 input_proto.sysroot.build_target.name, input_proto.output_dir,
207 additional_args)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700208 if artifacts:
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700209 art_info = output_proto.artifacts_info.add()
210 art_info.artifact_type = artifact_type
211 for artifact in artifacts:
212 art_info.artifacts.add().path = artifact
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700213
214
215# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
216_NAMES_FOR_AFDO_ARTIFACTS = {
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700217 toolchain_pb2.ORDERFILE: 'orderfile',
218 toolchain_pb2.KERNEL_AFDO: 'kernel_afdo',
219 toolchain_pb2.CHROME_AFDO: 'chrome_afdo'
220}
221
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700222# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700223# Using a function instead of a dict because we need to mock these
224# functions in unittest, and mock doesn't play well with a dict definition.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700225def _GetMethodForUpdatingAFDOArtifacts(artifact_type):
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700226 return {
227 toolchain_pb2.ORDERFILE: toolchain_util.OrderfileUpdateChromeEbuild,
228 toolchain_pb2.KERNEL_AFDO: toolchain_util.AFDOUpdateKernelEbuild,
229 toolchain_pb2.CHROME_AFDO: toolchain_util.AFDOUpdateChromeEbuild
230 }[artifact_type]
Tiancong Wang24a3df72019-08-20 15:48:51 -0700231
Tiancong Wangaf050172019-07-10 11:52:03 -0700232
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700233# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700234def _UpdateEbuildWithAFDOArtifactsResponse(_input_proto, output_proto, _config):
235 """Add successful status to the faux response."""
236 output_proto.status = True
237
238
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700239# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700240@faux.success(_UpdateEbuildWithAFDOArtifactsResponse)
241@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -0600242@validate.require('build_target.name')
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700243@validate.is_in('artifact_type', _NAMES_FOR_AFDO_ARTIFACTS)
Alex Klein231d2da2019-07-22 16:44:45 -0600244@validate.validation_complete
Tiancong Wang24a3df72019-08-20 15:48:51 -0700245def UpdateEbuildWithAFDOArtifacts(input_proto, output_proto, _config):
246 """Update Chrome or kernel ebuild with most recent unvetted artifacts.
Tiancong Wangaf050172019-07-10 11:52:03 -0700247
248 Args:
Tiancong Wang24a3df72019-08-20 15:48:51 -0700249 input_proto (VerifyAFDOArtifactsRequest): The input proto
250 output_proto (VerifyAFDOArtifactsResponse): The output proto
Alex Klein231d2da2019-07-22 16:44:45 -0600251 _config (api_config.ApiConfig): The API call config.
Tiancong Wangaf050172019-07-10 11:52:03 -0700252 """
Tiancong Wangaf050172019-07-10 11:52:03 -0700253 board = input_proto.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700254 update_method = _GetMethodForUpdatingAFDOArtifacts(input_proto.artifact_type)
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700255 output_proto.status = update_method(board)
Tiancong Wangaf050172019-07-10 11:52:03 -0700256
257
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700258# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700259def _UploadVettedAFDOArtifactsResponse(_input_proto, output_proto, _config):
260 """Add successful status to the faux response."""
261 output_proto.status = True
262
263
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700264# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700265@faux.success(_UploadVettedAFDOArtifactsResponse)
266@faux.empty_error
Tiancong Wang24a3df72019-08-20 15:48:51 -0700267@validate.require('build_target.name')
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700268@validate.is_in('artifact_type', _NAMES_FOR_AFDO_ARTIFACTS)
Alex Klein231d2da2019-07-22 16:44:45 -0600269@validate.validation_complete
Tiancong Wang24a3df72019-08-20 15:48:51 -0700270def UploadVettedAFDOArtifacts(input_proto, output_proto, _config):
Tiancong Wangaf050172019-07-10 11:52:03 -0700271 """Upload a vetted orderfile to GS bucket.
272
273 Args:
Tiancong Wang24a3df72019-08-20 15:48:51 -0700274 input_proto (VerifyAFDOArtifactsRequest): The input proto
275 output_proto (VerifyAFDOArtifactsResponse): The output proto
Alex Klein231d2da2019-07-22 16:44:45 -0600276 _config (api_config.ApiConfig): The API call config.
Tiancong Wangaf050172019-07-10 11:52:03 -0700277 """
Tiancong Wang24a3df72019-08-20 15:48:51 -0700278 board = input_proto.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700279 artifact_type = _NAMES_FOR_AFDO_ARTIFACTS[input_proto.artifact_type]
Tiancong Wang24a3df72019-08-20 15:48:51 -0700280 output_proto.status = toolchain_util.UploadAndPublishVettedAFDOArtifacts(
281 artifact_type, board)