Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 1 | # -*- 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 | |
| 8 | from __future__ import print_function |
| 9 | |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 10 | import collections |
| 11 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 12 | from chromite.api import faux |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 13 | from chromite.api import validate |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 14 | from chromite.api.gen.chromite.api import toolchain_pb2 |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 15 | from chromite.lib import toolchain_util |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 16 | from chromite.api.gen.chromiumos.builder_config_pb2 import BuilderConfig |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 17 | |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 18 | # 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 |
| 43 | def 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. |
LaMont Jones | a215f1e | 2019-12-06 10:18:58 -0700 | [diff] [blame] | 48 | chroot_path (str): chroot path (absolute path) |
| 49 | sysroot_path (str): sysroot path inside the chroot (e.g., /build/atlas) |
| 50 | build_target_name (str): name of the build target (e.g., atlas) |
| 51 | |
| 52 | They locate and modify any ebuilds and/or source required for the artifact |
| 53 | being created, then return a value from toolchain_util.PrepareForBuildReturn. |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 54 | |
| 55 | Args: |
| 56 | input_proto (PrepareForToolchainBuildRequest): The input proto |
| 57 | output_proto (PrepareForToolchainBuildResponse): The output proto |
| 58 | _config (api_config.ApiConfig): The API call config. |
| 59 | """ |
| 60 | results = set() |
| 61 | for artifact_type in input_proto.artifact_types: |
| 62 | # Ignore any artifact_types not handled. |
| 63 | handler = _TOOLCHAIN_ARTIFACT_HANDLERS.get(artifact_type) |
| 64 | if handler and handler.prepare: |
LaMont Jones | a215f1e | 2019-12-06 10:18:58 -0700 | [diff] [blame] | 65 | results.add(handler.prepare( |
| 66 | handler.name, input_proto.chroot.path, input_proto.sysroot.path, |
| 67 | input_proto.sysroot.build_target.name)) |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 68 | |
| 69 | # Translate the returns from the handlers we called. |
| 70 | # If any NEEDED => NEEDED |
| 71 | # elif any UNKNOWN => UNKNOWN |
| 72 | # elif any POINTLESS => POINTLESS |
| 73 | # else UNKNOWN. |
| 74 | proto_resp = toolchain_pb2.PrepareForToolchainBuildResponse |
| 75 | if toolchain_util.PrepareForBuildReturn.NEEDED in results: |
| 76 | output_proto.build_relevance = proto_resp.NEEDED |
| 77 | elif toolchain_util.PrepareForBuildReturn.UNKNOWN in results: |
| 78 | output_proto.build_relevance = proto_resp.UNKNOWN |
| 79 | elif toolchain_util.PrepareForBuildReturn.POINTLESS in results: |
| 80 | output_proto.build_relevance = proto_resp.POINTLESS |
| 81 | else: |
| 82 | output_proto.build_relevance = proto_resp.UNKNOWN |
| 83 | |
| 84 | |
| 85 | # TODO(crbug/1031213): When @faux is expanded to have more than success/failure, |
| 86 | # this should be changed. |
| 87 | @faux.all_empty |
| 88 | @validate.require('artifact_types') |
| 89 | @validate.require('output_dir') |
| 90 | def BundleArtifacts(input_proto, output_proto, _config): |
| 91 | """Bundle toolchain artifacts. |
| 92 | |
| 93 | The handlers (from _TOOLCHAIN_ARTIFACT_HANDLERS above) are called with: |
LaMont Jones | a215f1e | 2019-12-06 10:18:58 -0700 | [diff] [blame] | 94 | artifact_name (str): name of the artifact type |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 95 | chroot_path (str): chroot path (absolute path) |
| 96 | sysroot_path (str): sysroot path inside the chroot (e.g., /build/atlas) |
| 97 | build_target_name (str): name of the build target (e.g., atlas) |
| 98 | output_dir (str): absolute path where artifacts are being bundled. |
| 99 | (e.g., /b/s/w/ir/k/recipe_cleanup/artifactssptfMU) |
| 100 | |
| 101 | Note: the actual upload to GS is done by CI, not here. |
| 102 | |
| 103 | Args: |
| 104 | input_proto (BundleToolchainRequest): The input proto |
| 105 | output_proto (BundleToolchainResponse): The output proto |
| 106 | _config (api_config.ApiConfig): The API call config. |
| 107 | """ |
LaMont Jones | a215f1e | 2019-12-06 10:18:58 -0700 | [diff] [blame] | 108 | # TODO(crbug/1019868): This is moving, handle both cases. |
| 109 | resp_artifact = ( |
| 110 | getattr(toolchain_pb2.BundleToolchainResponse, 'ArtifactInfo', None) or |
| 111 | getattr(toolchain_pb2, 'ArtifactInfo')) |
| 112 | # resp_artifact = toolchain_pb2.ArtifactInfo |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 113 | |
| 114 | for artifact_type in input_proto.artifact_types: |
| 115 | # Ignore any artifact_types not handled. |
| 116 | handler = _TOOLCHAIN_ARTIFACT_HANDLERS.get(artifact_type) |
| 117 | if handler and handler.bundle: |
| 118 | artifacts = handler.bundle( |
| 119 | handler.name, input_proto.chroot.path, input_proto.sysroot.path, |
| 120 | input_proto.sysroot.build_target.name, input_proto.output_dir) |
| 121 | if artifacts: |
| 122 | output_proto.artifacts_info.append( |
| 123 | resp_artifact(artifact_type=artifact_type, artifacts=artifacts)) |
| 124 | |
| 125 | |
| 126 | # TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone. |
| 127 | _NAMES_FOR_AFDO_ARTIFACTS = { |
Tiancong Wang | f9c736c | 2019-08-26 13:54:38 -0700 | [diff] [blame] | 128 | toolchain_pb2.ORDERFILE: 'orderfile', |
| 129 | toolchain_pb2.KERNEL_AFDO: 'kernel_afdo', |
| 130 | toolchain_pb2.CHROME_AFDO: 'chrome_afdo' |
| 131 | } |
| 132 | |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 133 | # TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone. |
Tiancong Wang | f9c736c | 2019-08-26 13:54:38 -0700 | [diff] [blame] | 134 | # Using a function instead of a dict because we need to mock these |
| 135 | # functions in unittest, and mock doesn't play well with a dict definition. |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 136 | def _GetMethodForUpdatingAFDOArtifacts(artifact_type): |
Tiancong Wang | f9c736c | 2019-08-26 13:54:38 -0700 | [diff] [blame] | 137 | return { |
| 138 | toolchain_pb2.ORDERFILE: toolchain_util.OrderfileUpdateChromeEbuild, |
| 139 | toolchain_pb2.KERNEL_AFDO: toolchain_util.AFDOUpdateKernelEbuild, |
| 140 | toolchain_pb2.CHROME_AFDO: toolchain_util.AFDOUpdateChromeEbuild |
| 141 | }[artifact_type] |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 142 | |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 143 | |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 144 | # TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone. |
Michael Mortensen | 54bd70a | 2019-11-21 14:45:38 -0700 | [diff] [blame] | 145 | def _UpdateEbuildWithAFDOArtifactsResponse(_input_proto, output_proto, _config): |
| 146 | """Add successful status to the faux response.""" |
| 147 | output_proto.status = True |
| 148 | |
| 149 | |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 150 | # TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone. |
Michael Mortensen | 54bd70a | 2019-11-21 14:45:38 -0700 | [diff] [blame] | 151 | @faux.success(_UpdateEbuildWithAFDOArtifactsResponse) |
| 152 | @faux.empty_error |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 153 | @validate.require('build_target.name') |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 154 | @validate.is_in('artifact_type', _NAMES_FOR_AFDO_ARTIFACTS) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 155 | @validate.validation_complete |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 156 | def UpdateEbuildWithAFDOArtifacts(input_proto, output_proto, _config): |
| 157 | """Update Chrome or kernel ebuild with most recent unvetted artifacts. |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 158 | |
| 159 | Args: |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 160 | input_proto (VerifyAFDOArtifactsRequest): The input proto |
| 161 | output_proto (VerifyAFDOArtifactsResponse): The output proto |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 162 | _config (api_config.ApiConfig): The API call config. |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 163 | """ |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 164 | board = input_proto.build_target.name |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 165 | update_method = _GetMethodForUpdatingAFDOArtifacts(input_proto.artifact_type) |
Tiancong Wang | f9c736c | 2019-08-26 13:54:38 -0700 | [diff] [blame] | 166 | output_proto.status = update_method(board) |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 167 | |
| 168 | |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 169 | # TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone. |
Michael Mortensen | 54bd70a | 2019-11-21 14:45:38 -0700 | [diff] [blame] | 170 | def _UploadVettedAFDOArtifactsResponse(_input_proto, output_proto, _config): |
| 171 | """Add successful status to the faux response.""" |
| 172 | output_proto.status = True |
| 173 | |
| 174 | |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 175 | # TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone. |
Michael Mortensen | 54bd70a | 2019-11-21 14:45:38 -0700 | [diff] [blame] | 176 | @faux.success(_UploadVettedAFDOArtifactsResponse) |
| 177 | @faux.empty_error |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 178 | @validate.require('build_target.name') |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 179 | @validate.is_in('artifact_type', _NAMES_FOR_AFDO_ARTIFACTS) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 180 | @validate.validation_complete |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 181 | def UploadVettedAFDOArtifacts(input_proto, output_proto, _config): |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 182 | """Upload a vetted orderfile to GS bucket. |
| 183 | |
| 184 | Args: |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 185 | input_proto (VerifyAFDOArtifactsRequest): The input proto |
| 186 | output_proto (VerifyAFDOArtifactsResponse): The output proto |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 187 | _config (api_config.ApiConfig): The API call config. |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 188 | """ |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 189 | board = input_proto.build_target.name |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 190 | artifact_type = _NAMES_FOR_AFDO_ARTIFACTS[input_proto.artifact_type] |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 191 | output_proto.status = toolchain_util.UploadAndPublishVettedAFDOArtifacts( |
| 192 | artifact_type, board) |