blob: ff144afda606c6a7f0a533e9047478c29cb65fb5 [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# TODO(crbug/1019868): Add handlers as needed.
27_Handlers = collections.namedtuple('_Handlers', ['name', 'prepare', 'bundle'])
28_TOOLCHAIN_ARTIFACT_HANDLERS = {
LaMont Jonescd1503d2020-03-04 09:09:59 -070029 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE:
LaMont Jones5d2edcb2019-12-23 11:32:03 -070030 _Handlers('UnverifiedOrderingFile',
31 toolchain_util.PrepareForBuild,
32 toolchain_util.BundleArtifacts),
LaMont Jonescd1503d2020-03-04 09:09:59 -070033 BuilderConfig.Artifacts.VERIFIED_CHROME_LLVM_ORDERFILE:
LaMont Jones5d2edcb2019-12-23 11:32:03 -070034 _Handlers('VerifiedOrderingFile',
35 toolchain_util.PrepareForBuild,
36 toolchain_util.BundleArtifacts),
LaMont Jonesb20b3d92019-11-23 11:47:48 -070037 BuilderConfig.Artifacts.CHROME_CLANG_WARNINGS_FILE:
LaMont Jones5d2edcb2019-12-23 11:32:03 -070038 _Handlers('ChromeClangWarningsFile',
LaMont Jones90bab632020-01-27 15:58:26 -070039 toolchain_util.PrepareForBuild,
40 toolchain_util.BundleArtifacts),
LaMont Jonesb20b3d92019-11-23 11:47:48 -070041 BuilderConfig.Artifacts.UNVERIFIED_LLVM_PGO_FILE:
LaMont Jones5d2edcb2019-12-23 11:32:03 -070042 _Handlers('UnverifiedLlvmPgoFile',
LaMont Jones90bab632020-01-27 15:58:26 -070043 toolchain_util.PrepareForBuild,
44 toolchain_util.BundleArtifacts),
LaMont Jones45ca6c42020-02-05 09:39:09 -070045 BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE:
46 _Handlers('UnverifiedChromeBenchmarkAfdoFile',
LaMont Jones90bab632020-01-27 15:58:26 -070047 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:
54 _Handlers('UnverifiedKernelCwpAfdoFile',
55 toolchain_util.PrepareForBuild,
56 toolchain_util.BundleArtifacts),
57 BuilderConfig.Artifacts.VERIFIED_KERNEL_CWP_AFDO_FILE:
58 _Handlers('VerifiedKernelCwpAfdoFile',
59 toolchain_util.PrepareForBuild,
60 toolchain_util.BundleArtifacts),
61 BuilderConfig.Artifacts.UNVERIFIED_CHROME_CWP_AFDO_FILE:
62 _Handlers('UnverifiedChromeCwpAfdoFile',
63 toolchain_util.PrepareForBuild,
64 toolchain_util.BundleArtifacts),
65 BuilderConfig.Artifacts.VERIFIED_CHROME_CWP_AFDO_FILE:
66 _Handlers('VerifiedChromeCwpAfdoFile',
67 toolchain_util.PrepareForBuild,
68 toolchain_util.BundleArtifacts),
69 BuilderConfig.Artifacts.VERIFIED_RELEASE_AFDO_FILE:
70 _Handlers('VerifiedReleaseAfdoFile',
LaMont Jones90bab632020-01-27 15:58:26 -070071 toolchain_util.PrepareForBuild,
72 toolchain_util.BundleArtifacts),
LaMont Jonesb20b3d92019-11-23 11:47:48 -070073}
74
75
76# TODO(crbug/1031213): When @faux is expanded to have more than success/failure,
77# this should be changed.
78@faux.all_empty
LaMont Jones5d2edcb2019-12-23 11:32:03 -070079@validate.require('artifact_types')
80# Note: chroot and sysroot are unspecified the first time that the build_target
81# recipe calls PrepareForBuild. The second time, they are specified. No
82# validation check because "all" values are valid.
LaMont Jonesb20b3d92019-11-23 11:47:48 -070083@validate.validation_complete
84def PrepareForBuild(input_proto, output_proto, _config):
85 """Prepare to build toolchain artifacts.
86
87 The handlers (from _TOOLCHAIN_ARTIFACT_HANDLERS above) are called with:
88 artifact_name (str): name of the artifact type.
LaMont Jones5d2edcb2019-12-23 11:32:03 -070089 chroot (chroot_lib.Chroot): chroot. Will be None if the chroot has not
90 yet been created.
91 sysroot_path (str): sysroot path inside the chroot (e.g., /build/atlas).
92 Will be an empty string if the sysroot has not yet been created.
93 build_target_name (str): name of the build target (e.g., atlas). Will be
94 an empty string if the sysroot has not yet been created.
95 input_artifacts ({(str) name:[str gs_locations]}): locations for possible
96 input artifacts. The handler is expected to know which keys it should
97 be using, and ignore any keys that it does not understand.
LaMont Jones45ca6c42020-02-05 09:39:09 -070098 additional_args ({(str) name: (str) value}) Dictionary of additional
99 arguments.
LaMont Jonesa215f1e2019-12-06 10:18:58 -0700100
101 They locate and modify any ebuilds and/or source required for the artifact
102 being created, then return a value from toolchain_util.PrepareForBuildReturn.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700103
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700104 This function sets output_proto.build_relevance to the result.
105
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700106 Args:
107 input_proto (PrepareForToolchainBuildRequest): The input proto
108 output_proto (PrepareForToolchainBuildResponse): The output proto
109 _config (api_config.ApiConfig): The API call config.
110 """
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700111 if input_proto.chroot.path:
112 chroot = controller_util.ParseChroot(input_proto.chroot)
113 else:
114 chroot = None
LaMont Jones4579e8c2019-12-06 14:20:37 -0700115
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700116 input_artifacts = collections.defaultdict(list)
117 for art in input_proto.input_artifacts:
118 item = _TOOLCHAIN_ARTIFACT_HANDLERS.get(art.input_artifact_type)
119 if item:
120 input_artifacts[item.name].extend(
121 ['gs://%s' % str(x) for x in art.input_artifact_gs_locations])
122
LaMont Jones45ca6c42020-02-05 09:39:09 -0700123 # Pass along any additional args.
124 additional_args = {}
125 which = input_proto.additional_args.WhichOneof('prepare_for_build_args')
126 if which:
127 # All of the additional arguments we understand are strings, so we can
128 # copy whichever argument we got without processing it.
129 additional_args[which] = getattr(input_proto.additional_args, which)
130
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700131 results = set()
132 sysroot_path = input_proto.sysroot.path
133 build_target = input_proto.sysroot.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700134 for artifact_type in input_proto.artifact_types:
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700135 # Unknown artifact_types are an error.
136 handler = _TOOLCHAIN_ARTIFACT_HANDLERS[artifact_type]
137 if handler.prepare:
LaMont Jonesa215f1e2019-12-06 10:18:58 -0700138 results.add(handler.prepare(
LaMont Jones45ca6c42020-02-05 09:39:09 -0700139 handler.name, chroot, sysroot_path, build_target, input_artifacts,
140 additional_args))
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700141
142 # Translate the returns from the handlers we called.
143 # If any NEEDED => NEEDED
144 # elif any UNKNOWN => UNKNOWN
145 # elif any POINTLESS => POINTLESS
146 # else UNKNOWN.
147 proto_resp = toolchain_pb2.PrepareForToolchainBuildResponse
148 if toolchain_util.PrepareForBuildReturn.NEEDED in results:
149 output_proto.build_relevance = proto_resp.NEEDED
150 elif toolchain_util.PrepareForBuildReturn.UNKNOWN in results:
151 output_proto.build_relevance = proto_resp.UNKNOWN
152 elif toolchain_util.PrepareForBuildReturn.POINTLESS in results:
153 output_proto.build_relevance = proto_resp.POINTLESS
154 else:
155 output_proto.build_relevance = proto_resp.UNKNOWN
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700156 return controller.RETURN_CODE_SUCCESS
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700157
158
159# TODO(crbug/1031213): When @faux is expanded to have more than success/failure,
160# this should be changed.
161@faux.all_empty
LaMont Jones4579e8c2019-12-06 14:20:37 -0700162@validate.require('chroot.path', 'sysroot.path', 'sysroot.build_target.name',
163 'output_dir', 'artifact_types')
164@validate.exists('output_dir')
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700165@validate.validation_complete
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700166def BundleArtifacts(input_proto, output_proto, _config):
167 """Bundle toolchain artifacts.
168
169 The handlers (from _TOOLCHAIN_ARTIFACT_HANDLERS above) are called with:
LaMont Jonesa215f1e2019-12-06 10:18:58 -0700170 artifact_name (str): name of the artifact type
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700171 chroot (chroot_lib.Chroot): chroot
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700172 sysroot_path (str): sysroot path inside the chroot (e.g., /build/atlas)
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700173 chrome_root (str): path to chrome root. (e.g., /b/s/w/ir/k/chrome)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700174 build_target_name (str): name of the build target (e.g., atlas)
175 output_dir (str): absolute path where artifacts are being bundled.
176 (e.g., /b/s/w/ir/k/recipe_cleanup/artifactssptfMU)
177
178 Note: the actual upload to GS is done by CI, not here.
179
180 Args:
181 input_proto (BundleToolchainRequest): The input proto
182 output_proto (BundleToolchainResponse): The output proto
183 _config (api_config.ApiConfig): The API call config.
184 """
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700185 chroot = controller_util.ParseChroot(input_proto.chroot)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700186
LaMont Jones45ca6c42020-02-05 09:39:09 -0700187 # Pass along any additional args.
188 additional_args = {}
189 which = input_proto.additional_args.WhichOneof('prepare_for_build_args')
190 if which:
191 # All of the additional arguments we understand are strings, so we can
192 # copy whichever argument we got without processing it.
193 additional_args[which] = getattr(input_proto.additional_args, which)
194
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700195 for artifact_type in input_proto.artifact_types:
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700196 if artifact_type not in _TOOLCHAIN_ARTIFACT_HANDLERS:
197 logging.error('%s not understood', artifact_type)
198 return controller.RETURN_CODE_UNRECOVERABLE
199 handler = _TOOLCHAIN_ARTIFACT_HANDLERS[artifact_type]
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700200 if handler and handler.bundle:
201 artifacts = handler.bundle(
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700202 handler.name, chroot, input_proto.sysroot.path,
LaMont Jones45ca6c42020-02-05 09:39:09 -0700203 input_proto.sysroot.build_target.name, input_proto.output_dir,
204 additional_args)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700205 if artifacts:
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700206 art_info = output_proto.artifacts_info.add()
207 art_info.artifact_type = artifact_type
208 for artifact in artifacts:
209 art_info.artifacts.add().path = artifact
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700210
211
212# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
213_NAMES_FOR_AFDO_ARTIFACTS = {
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700214 toolchain_pb2.ORDERFILE: 'orderfile',
215 toolchain_pb2.KERNEL_AFDO: 'kernel_afdo',
216 toolchain_pb2.CHROME_AFDO: 'chrome_afdo'
217}
218
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700219# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700220# Using a function instead of a dict because we need to mock these
221# functions in unittest, and mock doesn't play well with a dict definition.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700222def _GetMethodForUpdatingAFDOArtifacts(artifact_type):
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700223 return {
224 toolchain_pb2.ORDERFILE: toolchain_util.OrderfileUpdateChromeEbuild,
225 toolchain_pb2.KERNEL_AFDO: toolchain_util.AFDOUpdateKernelEbuild,
226 toolchain_pb2.CHROME_AFDO: toolchain_util.AFDOUpdateChromeEbuild
227 }[artifact_type]
Tiancong Wang24a3df72019-08-20 15:48:51 -0700228
Tiancong Wangaf050172019-07-10 11:52:03 -0700229
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700230# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700231def _UpdateEbuildWithAFDOArtifactsResponse(_input_proto, output_proto, _config):
232 """Add successful status to the faux response."""
233 output_proto.status = True
234
235
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700236# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700237@faux.success(_UpdateEbuildWithAFDOArtifactsResponse)
238@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -0600239@validate.require('build_target.name')
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700240@validate.is_in('artifact_type', _NAMES_FOR_AFDO_ARTIFACTS)
Alex Klein231d2da2019-07-22 16:44:45 -0600241@validate.validation_complete
Tiancong Wang24a3df72019-08-20 15:48:51 -0700242def UpdateEbuildWithAFDOArtifacts(input_proto, output_proto, _config):
243 """Update Chrome or kernel ebuild with most recent unvetted artifacts.
Tiancong Wangaf050172019-07-10 11:52:03 -0700244
245 Args:
Tiancong Wang24a3df72019-08-20 15:48:51 -0700246 input_proto (VerifyAFDOArtifactsRequest): The input proto
247 output_proto (VerifyAFDOArtifactsResponse): The output proto
Alex Klein231d2da2019-07-22 16:44:45 -0600248 _config (api_config.ApiConfig): The API call config.
Tiancong Wangaf050172019-07-10 11:52:03 -0700249 """
Tiancong Wangaf050172019-07-10 11:52:03 -0700250 board = input_proto.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700251 update_method = _GetMethodForUpdatingAFDOArtifacts(input_proto.artifact_type)
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700252 output_proto.status = update_method(board)
Tiancong Wangaf050172019-07-10 11:52:03 -0700253
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 -0700256def _UploadVettedAFDOArtifactsResponse(_input_proto, output_proto, _config):
257 """Add successful status to the faux response."""
258 output_proto.status = True
259
260
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700261# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700262@faux.success(_UploadVettedAFDOArtifactsResponse)
263@faux.empty_error
Tiancong Wang24a3df72019-08-20 15:48:51 -0700264@validate.require('build_target.name')
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700265@validate.is_in('artifact_type', _NAMES_FOR_AFDO_ARTIFACTS)
Alex Klein231d2da2019-07-22 16:44:45 -0600266@validate.validation_complete
Tiancong Wang24a3df72019-08-20 15:48:51 -0700267def UploadVettedAFDOArtifacts(input_proto, output_proto, _config):
Tiancong Wangaf050172019-07-10 11:52:03 -0700268 """Upload a vetted orderfile to GS bucket.
269
270 Args:
Tiancong Wang24a3df72019-08-20 15:48:51 -0700271 input_proto (VerifyAFDOArtifactsRequest): The input proto
272 output_proto (VerifyAFDOArtifactsResponse): The output proto
Alex Klein231d2da2019-07-22 16:44:45 -0600273 _config (api_config.ApiConfig): The API call config.
Tiancong Wangaf050172019-07-10 11:52:03 -0700274 """
Tiancong Wang24a3df72019-08-20 15:48:51 -0700275 board = input_proto.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700276 artifact_type = _NAMES_FOR_AFDO_ARTIFACTS[input_proto.artifact_type]
Tiancong Wang24a3df72019-08-20 15:48:51 -0700277 output_proto.status = toolchain_util.UploadAndPublishVettedAFDOArtifacts(
278 artifact_type, board)