blob: 026cd100da7adc1a3c068df3d5bff7248a776767 [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
Alex Klein076841b2019-08-29 15:19:39 -060012from chromite.api import faux
Alex Klein231d2da2019-07-22 16:44:45 -060013from chromite.api import validate
Tiancong Wang24a3df72019-08-20 15:48:51 -070014from chromite.api.gen.chromite.api import toolchain_pb2
Tiancong Wangaf050172019-07-10 11:52:03 -070015from chromite.lib import toolchain_util
LaMont Jonesb20b3d92019-11-23 11:47:48 -070016from chromite.api.gen.chromiumos.builder_config_pb2 import BuilderConfig
Tiancong Wangaf050172019-07-10 11:52:03 -070017
LaMont Jonesb20b3d92019-11-23 11:47:48 -070018# TODO(crbug/1019868): Add handlers as needed.
19_Handlers = collections.namedtuple('_Handlers', ['name', 'prepare', 'bundle'])
20_TOOLCHAIN_ARTIFACT_HANDLERS = {
21 BuilderConfig.Artifacts.UNVERIFIED_ORDERING_FILE:
22 _Handlers('UnverifiedOrderingFile', None, None),
23 BuilderConfig.Artifacts.VERIFIED_ORDERING_FILE:
24 _Handlers('VerifedOrderingFile', None, None),
25 BuilderConfig.Artifacts.CHROME_CLANG_WARNINGS_FILE:
26 _Handlers('ChromeClangWarningsFile', None, None),
27 BuilderConfig.Artifacts.UNVERIFIED_LLVM_PGO_FILE:
28 _Handlers('UnverifiedLlvmPgoFile', None, None),
29 BuilderConfig.Artifacts.UNVERIFIED_CHROME_AFDO_FILE:
30 _Handlers('UnverifiedChromeAfdoFile', None, None),
31 BuilderConfig.Artifacts.VERIFIED_CHROME_AFDO_FILE:
32 _Handlers('VerifiedChromeAfdoFile', None, None),
33 BuilderConfig.Artifacts.VERIFIED_KERNEL_AFDO_FILE:
34 _Handlers('VerifiedKernelAfdoFile', None, None),
35}
36
37
38# TODO(crbug/1031213): When @faux is expanded to have more than success/failure,
39# this should be changed.
40@faux.all_empty
41@validate.require('artifact_types')
42@validate.validation_complete
43def PrepareForBuild(input_proto, output_proto, _config):
44 """Prepare to build toolchain artifacts.
45
46 The handlers (from _TOOLCHAIN_ARTIFACT_HANDLERS above) are called with:
47 artifact_name (str): name of the artifact type.
48 They use their knowledge of the source tree layout and __file__ to get locate
49 and modify any ebuilds and/or source required for the artifact being created,
50 then return a value from toolchain_util.PrepareForBuildReturn.
51
52 Args:
53 input_proto (PrepareForToolchainBuildRequest): The input proto
54 output_proto (PrepareForToolchainBuildResponse): The output proto
55 _config (api_config.ApiConfig): The API call config.
56 """
57 results = set()
58 for artifact_type in input_proto.artifact_types:
59 # Ignore any artifact_types not handled.
60 handler = _TOOLCHAIN_ARTIFACT_HANDLERS.get(artifact_type)
61 if handler and handler.prepare:
62 results.add(handler.prepare(handler.name))
63
64 # Translate the returns from the handlers we called.
65 # If any NEEDED => NEEDED
66 # elif any UNKNOWN => UNKNOWN
67 # elif any POINTLESS => POINTLESS
68 # else UNKNOWN.
69 proto_resp = toolchain_pb2.PrepareForToolchainBuildResponse
70 if toolchain_util.PrepareForBuildReturn.NEEDED in results:
71 output_proto.build_relevance = proto_resp.NEEDED
72 elif toolchain_util.PrepareForBuildReturn.UNKNOWN in results:
73 output_proto.build_relevance = proto_resp.UNKNOWN
74 elif toolchain_util.PrepareForBuildReturn.POINTLESS in results:
75 output_proto.build_relevance = proto_resp.POINTLESS
76 else:
77 output_proto.build_relevance = proto_resp.UNKNOWN
78
79
80# TODO(crbug/1031213): When @faux is expanded to have more than success/failure,
81# this should be changed.
82@faux.all_empty
83@validate.require('artifact_types')
84@validate.require('output_dir')
85def BundleArtifacts(input_proto, output_proto, _config):
86 """Bundle toolchain artifacts.
87
88 The handlers (from _TOOLCHAIN_ARTIFACT_HANDLERS above) are called with:
89 artifact_name (str): name of artifact type
90 chroot_path (str): chroot path (absolute path)
91 sysroot_path (str): sysroot path inside the chroot (e.g., /build/atlas)
92 build_target_name (str): name of the build target (e.g., atlas)
93 output_dir (str): absolute path where artifacts are being bundled.
94 (e.g., /b/s/w/ir/k/recipe_cleanup/artifactssptfMU)
95
96 Note: the actual upload to GS is done by CI, not here.
97
98 Args:
99 input_proto (BundleToolchainRequest): The input proto
100 output_proto (BundleToolchainResponse): The output proto
101 _config (api_config.ApiConfig): The API call config.
102 """
103 resp_artifact = toolchain_pb2.BundleToolchainResponse.ArtifactInfo
104
105 for artifact_type in input_proto.artifact_types:
106 # Ignore any artifact_types not handled.
107 handler = _TOOLCHAIN_ARTIFACT_HANDLERS.get(artifact_type)
108 if handler and handler.bundle:
109 artifacts = handler.bundle(
110 handler.name, input_proto.chroot.path, input_proto.sysroot.path,
111 input_proto.sysroot.build_target.name, input_proto.output_dir)
112 if artifacts:
113 output_proto.artifacts_info.append(
114 resp_artifact(artifact_type=artifact_type, artifacts=artifacts))
115
116
117# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
118_NAMES_FOR_AFDO_ARTIFACTS = {
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700119 toolchain_pb2.ORDERFILE: 'orderfile',
120 toolchain_pb2.KERNEL_AFDO: 'kernel_afdo',
121 toolchain_pb2.CHROME_AFDO: 'chrome_afdo'
122}
123
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700124# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700125# Using a function instead of a dict because we need to mock these
126# functions in unittest, and mock doesn't play well with a dict definition.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700127def _GetMethodForUpdatingAFDOArtifacts(artifact_type):
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700128 return {
129 toolchain_pb2.ORDERFILE: toolchain_util.OrderfileUpdateChromeEbuild,
130 toolchain_pb2.KERNEL_AFDO: toolchain_util.AFDOUpdateKernelEbuild,
131 toolchain_pb2.CHROME_AFDO: toolchain_util.AFDOUpdateChromeEbuild
132 }[artifact_type]
Tiancong Wang24a3df72019-08-20 15:48:51 -0700133
Tiancong Wangaf050172019-07-10 11:52:03 -0700134
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700135# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700136def _UpdateEbuildWithAFDOArtifactsResponse(_input_proto, output_proto, _config):
137 """Add successful status to the faux response."""
138 output_proto.status = True
139
140
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700141# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700142@faux.success(_UpdateEbuildWithAFDOArtifactsResponse)
143@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -0600144@validate.require('build_target.name')
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700145@validate.is_in('artifact_type', _NAMES_FOR_AFDO_ARTIFACTS)
Alex Klein231d2da2019-07-22 16:44:45 -0600146@validate.validation_complete
Tiancong Wang24a3df72019-08-20 15:48:51 -0700147def UpdateEbuildWithAFDOArtifacts(input_proto, output_proto, _config):
148 """Update Chrome or kernel ebuild with most recent unvetted artifacts.
Tiancong Wangaf050172019-07-10 11:52:03 -0700149
150 Args:
Tiancong Wang24a3df72019-08-20 15:48:51 -0700151 input_proto (VerifyAFDOArtifactsRequest): The input proto
152 output_proto (VerifyAFDOArtifactsResponse): The output proto
Alex Klein231d2da2019-07-22 16:44:45 -0600153 _config (api_config.ApiConfig): The API call config.
Tiancong Wangaf050172019-07-10 11:52:03 -0700154 """
Tiancong Wangaf050172019-07-10 11:52:03 -0700155 board = input_proto.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700156 update_method = _GetMethodForUpdatingAFDOArtifacts(input_proto.artifact_type)
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700157 output_proto.status = update_method(board)
Tiancong Wangaf050172019-07-10 11:52:03 -0700158
159
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700160# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700161def _UploadVettedAFDOArtifactsResponse(_input_proto, output_proto, _config):
162 """Add successful status to the faux response."""
163 output_proto.status = True
164
165
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700166# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700167@faux.success(_UploadVettedAFDOArtifactsResponse)
168@faux.empty_error
Tiancong Wang24a3df72019-08-20 15:48:51 -0700169@validate.require('build_target.name')
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700170@validate.is_in('artifact_type', _NAMES_FOR_AFDO_ARTIFACTS)
Alex Klein231d2da2019-07-22 16:44:45 -0600171@validate.validation_complete
Tiancong Wang24a3df72019-08-20 15:48:51 -0700172def UploadVettedAFDOArtifacts(input_proto, output_proto, _config):
Tiancong Wangaf050172019-07-10 11:52:03 -0700173 """Upload a vetted orderfile to GS bucket.
174
175 Args:
Tiancong Wang24a3df72019-08-20 15:48:51 -0700176 input_proto (VerifyAFDOArtifactsRequest): The input proto
177 output_proto (VerifyAFDOArtifactsResponse): The output proto
Alex Klein231d2da2019-07-22 16:44:45 -0600178 _config (api_config.ApiConfig): The API call config.
Tiancong Wangaf050172019-07-10 11:52:03 -0700179 """
Tiancong Wang24a3df72019-08-20 15:48:51 -0700180 board = input_proto.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700181 artifact_type = _NAMES_FOR_AFDO_ARTIFACTS[input_proto.artifact_type]
Tiancong Wang24a3df72019-08-20 15:48:51 -0700182 output_proto.status = toolchain_util.UploadAndPublishVettedAFDOArtifacts(
183 artifact_type, board)