Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 1 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Toolchain-related operations.""" |
| 6 | |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 7 | import collections |
Chris McDonald | 1672ddb | 2021-07-21 11:48:23 -0600 | [diff] [blame] | 8 | import logging |
Alex Klein | cd03a5e | 2021-10-18 13:23:47 -0600 | [diff] [blame] | 9 | from pathlib import Path |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 10 | from typing import TYPE_CHECKING |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 11 | |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 12 | from chromite.api import controller |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 13 | from chromite.api import faux |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 14 | from chromite.api import validate |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 15 | from chromite.api.controller import controller_util |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 16 | from chromite.api.gen.chromite.api import toolchain_pb2 |
LaMont Jones | fd68cb1 | 2020-04-29 16:43:06 -0600 | [diff] [blame] | 17 | from chromite.api.gen.chromite.api.artifacts_pb2 import PrepareForBuildResponse |
Chris McDonald | 1672ddb | 2021-07-21 11:48:23 -0600 | [diff] [blame] | 18 | from chromite.api.gen.chromiumos.builder_config_pb2 import BuilderConfig |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 19 | from chromite.lib import toolchain as toolchain_lib |
Chris McDonald | 1672ddb | 2021-07-21 11:48:23 -0600 | [diff] [blame] | 20 | from chromite.lib import toolchain_util |
Ryan Beltran | f2a5dcc | 2022-04-19 20:34:00 +0000 | [diff] [blame] | 21 | from chromite.service import toolchain |
Ryan Beltran | 7d19180 | 2021-11-24 00:08:17 +0000 | [diff] [blame] | 22 | |
Mike Frysinger | ea11fdd | 2022-05-06 22:59:33 -0400 | [diff] [blame] | 23 | |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 24 | if TYPE_CHECKING: |
| 25 | from chromite.api import api_config |
| 26 | |
Ryan Beltran | f2a5dcc | 2022-04-19 20:34:00 +0000 | [diff] [blame] | 27 | # TODO(b/229665884): Move the implementation details for most/all endpoints to: |
| 28 | # chromite/services/toolchain.py |
| 29 | # This migration has been done for linting endpoints but not yet for others. |
Chris McDonald | 1672ddb | 2021-07-21 11:48:23 -0600 | [diff] [blame] | 30 | |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 31 | _Handlers = collections.namedtuple('_Handlers', ['name', 'prepare', 'bundle']) |
| 32 | _TOOLCHAIN_ARTIFACT_HANDLERS = { |
LaMont Jones | cd1503d | 2020-03-04 09:09:59 -0700 | [diff] [blame] | 33 | BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE: |
LaMont Jones | d394458 | 2020-03-04 10:37:05 -0700 | [diff] [blame] | 34 | _Handlers('UnverifiedChromeLlvmOrderfile', |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 35 | toolchain_util.PrepareForBuild, |
| 36 | toolchain_util.BundleArtifacts), |
LaMont Jones | cd1503d | 2020-03-04 09:09:59 -0700 | [diff] [blame] | 37 | BuilderConfig.Artifacts.VERIFIED_CHROME_LLVM_ORDERFILE: |
LaMont Jones | 3fed7f2 | 2020-03-04 10:15:11 -0700 | [diff] [blame] | 38 | _Handlers('VerifiedChromeLlvmOrderfile', toolchain_util.PrepareForBuild, |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 39 | toolchain_util.BundleArtifacts), |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 40 | BuilderConfig.Artifacts.CHROME_CLANG_WARNINGS_FILE: |
LaMont Jones | 3fed7f2 | 2020-03-04 10:15:11 -0700 | [diff] [blame] | 41 | _Handlers('ChromeClangWarningsFile', toolchain_util.PrepareForBuild, |
LaMont Jones | 90bab63 | 2020-01-27 15:58:26 -0700 | [diff] [blame] | 42 | toolchain_util.BundleArtifacts), |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 43 | BuilderConfig.Artifacts.UNVERIFIED_LLVM_PGO_FILE: |
LaMont Jones | 3fed7f2 | 2020-03-04 10:15:11 -0700 | [diff] [blame] | 44 | _Handlers('UnverifiedLlvmPgoFile', toolchain_util.PrepareForBuild, |
LaMont Jones | 90bab63 | 2020-01-27 15:58:26 -0700 | [diff] [blame] | 45 | toolchain_util.BundleArtifacts), |
LaMont Jones | 45ca6c4 | 2020-02-05 09:39:09 -0700 | [diff] [blame] | 46 | BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE: |
| 47 | _Handlers('UnverifiedChromeBenchmarkAfdoFile', |
LaMont Jones | 90bab63 | 2020-01-27 15:58:26 -0700 | [diff] [blame] | 48 | toolchain_util.PrepareForBuild, |
| 49 | toolchain_util.BundleArtifacts), |
LaMont Jones | 3fed7f2 | 2020-03-04 10:15:11 -0700 | [diff] [blame] | 50 | BuilderConfig.Artifacts.CHROME_DEBUG_BINARY: |
Tiancong Wang | ba2a1c2 | 2021-01-19 10:45:06 -0800 | [diff] [blame] | 51 | _Handlers('ChromeDebugBinary', toolchain_util.PrepareForBuild, |
LaMont Jones | 3fed7f2 | 2020-03-04 10:15:11 -0700 | [diff] [blame] | 52 | toolchain_util.BundleArtifacts), |
LaMont Jones | 53bddd0 | 2020-03-12 15:02:54 -0600 | [diff] [blame] | 53 | BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_PERF_FILE: |
| 54 | _Handlers('UnverifiedChromeBenchmarkPerfFile', |
| 55 | toolchain_util.PrepareForBuild, |
| 56 | toolchain_util.BundleArtifacts), |
LaMont Jones | 45ca6c4 | 2020-02-05 09:39:09 -0700 | [diff] [blame] | 57 | BuilderConfig.Artifacts.VERIFIED_CHROME_BENCHMARK_AFDO_FILE: |
| 58 | _Handlers('VerifiedChromeBenchmarkAfdoFile', |
LaMont Jones | 90bab63 | 2020-01-27 15:58:26 -0700 | [diff] [blame] | 59 | toolchain_util.PrepareForBuild, |
| 60 | toolchain_util.BundleArtifacts), |
LaMont Jones | 45ca6c4 | 2020-02-05 09:39:09 -0700 | [diff] [blame] | 61 | BuilderConfig.Artifacts.UNVERIFIED_KERNEL_CWP_AFDO_FILE: |
LaMont Jones | 3fed7f2 | 2020-03-04 10:15:11 -0700 | [diff] [blame] | 62 | _Handlers('UnverifiedKernelCwpAfdoFile', toolchain_util.PrepareForBuild, |
LaMont Jones | 45ca6c4 | 2020-02-05 09:39:09 -0700 | [diff] [blame] | 63 | toolchain_util.BundleArtifacts), |
| 64 | BuilderConfig.Artifacts.VERIFIED_KERNEL_CWP_AFDO_FILE: |
LaMont Jones | 3fed7f2 | 2020-03-04 10:15:11 -0700 | [diff] [blame] | 65 | _Handlers('VerifiedKernelCwpAfdoFile', toolchain_util.PrepareForBuild, |
LaMont Jones | 45ca6c4 | 2020-02-05 09:39:09 -0700 | [diff] [blame] | 66 | toolchain_util.BundleArtifacts), |
| 67 | BuilderConfig.Artifacts.UNVERIFIED_CHROME_CWP_AFDO_FILE: |
LaMont Jones | 3fed7f2 | 2020-03-04 10:15:11 -0700 | [diff] [blame] | 68 | _Handlers('UnverifiedChromeCwpAfdoFile', toolchain_util.PrepareForBuild, |
LaMont Jones | 45ca6c4 | 2020-02-05 09:39:09 -0700 | [diff] [blame] | 69 | toolchain_util.BundleArtifacts), |
| 70 | BuilderConfig.Artifacts.VERIFIED_CHROME_CWP_AFDO_FILE: |
LaMont Jones | 3fed7f2 | 2020-03-04 10:15:11 -0700 | [diff] [blame] | 71 | _Handlers('VerifiedChromeCwpAfdoFile', toolchain_util.PrepareForBuild, |
LaMont Jones | 45ca6c4 | 2020-02-05 09:39:09 -0700 | [diff] [blame] | 72 | toolchain_util.BundleArtifacts), |
| 73 | BuilderConfig.Artifacts.VERIFIED_RELEASE_AFDO_FILE: |
LaMont Jones | 3fed7f2 | 2020-03-04 10:15:11 -0700 | [diff] [blame] | 74 | _Handlers('VerifiedReleaseAfdoFile', toolchain_util.PrepareForBuild, |
LaMont Jones | 90bab63 | 2020-01-27 15:58:26 -0700 | [diff] [blame] | 75 | toolchain_util.BundleArtifacts), |
Tiancong Wang | 91cf1dd | 2020-05-05 10:30:22 -0700 | [diff] [blame] | 76 | BuilderConfig.Artifacts.TOOLCHAIN_WARNING_LOGS: |
| 77 | _Handlers('ToolchainWarningLogs', toolchain_util.PrepareForBuild, |
| 78 | toolchain_util.BundleArtifacts), |
Tiancong Wang | fe3dbd2 | 2020-06-12 15:45:55 -0700 | [diff] [blame] | 79 | BuilderConfig.Artifacts.CHROME_AFDO_PROFILE_FOR_ANDROID_LINUX: |
| 80 | _Handlers('ChromeAFDOProfileForAndroidLinux', |
| 81 | toolchain_util.PrepareForBuild, |
| 82 | toolchain_util.BundleArtifacts), |
Jian Cai | 6190cc8 | 2020-06-12 16:24:32 -0700 | [diff] [blame] | 83 | BuilderConfig.Artifacts.CLANG_CRASH_DIAGNOSES: |
| 84 | _Handlers('ClangCrashDiagnoses', toolchain_util.PrepareForBuild, |
| 85 | toolchain_util.BundleArtifacts), |
Ryan Beltran | 0be7dcf | 2020-12-09 18:31:53 +0000 | [diff] [blame] | 86 | BuilderConfig.Artifacts.COMPILER_RUSAGE_LOG: |
| 87 | _Handlers('CompilerRusageLogs', toolchain_util.PrepareForBuild, |
| 88 | toolchain_util.BundleArtifacts), |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Tiancong Wang | d521413 | 2021-01-12 10:43:57 -0800 | [diff] [blame] | 91 | _TOOLCHAIN_COMMIT_HANDLERS = { |
| 92 | BuilderConfig.Artifacts.VERIFIED_KERNEL_CWP_AFDO_FILE: |
| 93 | 'VerifiedKernelCwpAfdoFile' |
| 94 | } |
| 95 | |
LaMont Jones | e782167 | 2020-04-09 08:56:26 -0600 | [diff] [blame] | 96 | |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 97 | # TODO(crbug/1031213): When @faux is expanded to have more than success/failure, |
| 98 | # this should be changed. |
| 99 | @faux.all_empty |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 100 | @validate.require('artifact_types') |
| 101 | # Note: chroot and sysroot are unspecified the first time that the build_target |
| 102 | # recipe calls PrepareForBuild. The second time, they are specified. No |
| 103 | # validation check because "all" values are valid. |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 104 | @validate.validation_complete |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 105 | def PrepareForBuild( |
| 106 | input_proto: 'toolchain_pb2.PrepareForToolchainBuildRequest', |
| 107 | output_proto: 'toolchain_pb2.PrepareForToolchainBuildResponse', |
| 108 | _config: 'api_config.ApiConfig'): |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 109 | """Prepare to build toolchain artifacts. |
| 110 | |
| 111 | The handlers (from _TOOLCHAIN_ARTIFACT_HANDLERS above) are called with: |
| 112 | artifact_name (str): name of the artifact type. |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 113 | chroot (chroot_lib.Chroot): chroot. Will be None if the chroot has not |
| 114 | yet been created. |
| 115 | sysroot_path (str): sysroot path inside the chroot (e.g., /build/atlas). |
| 116 | Will be an empty string if the sysroot has not yet been created. |
| 117 | build_target_name (str): name of the build target (e.g., atlas). Will be |
| 118 | an empty string if the sysroot has not yet been created. |
| 119 | input_artifacts ({(str) name:[str gs_locations]}): locations for possible |
| 120 | input artifacts. The handler is expected to know which keys it should |
| 121 | be using, and ignore any keys that it does not understand. |
LaMont Jones | e782167 | 2020-04-09 08:56:26 -0600 | [diff] [blame] | 122 | profile_info ({(str) name: (str) value}) Dictionary containing profile |
| 123 | information. |
LaMont Jones | a215f1e | 2019-12-06 10:18:58 -0700 | [diff] [blame] | 124 | |
| 125 | They locate and modify any ebuilds and/or source required for the artifact |
| 126 | being created, then return a value from toolchain_util.PrepareForBuildReturn. |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 127 | |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 128 | This function sets output_proto.build_relevance to the result. |
| 129 | |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 130 | Args: |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 131 | input_proto: The input proto |
| 132 | output_proto: The output proto |
| 133 | _config): The API call config. |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 134 | """ |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 135 | if input_proto.chroot.path: |
| 136 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 137 | else: |
| 138 | chroot = None |
LaMont Jones | 4579e8c | 2019-12-06 14:20:37 -0700 | [diff] [blame] | 139 | |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 140 | input_artifacts = collections.defaultdict(list) |
| 141 | for art in input_proto.input_artifacts: |
| 142 | item = _TOOLCHAIN_ARTIFACT_HANDLERS.get(art.input_artifact_type) |
| 143 | if item: |
| 144 | input_artifacts[item.name].extend( |
| 145 | ['gs://%s' % str(x) for x in art.input_artifact_gs_locations]) |
| 146 | |
LaMont Jones | e782167 | 2020-04-09 08:56:26 -0600 | [diff] [blame] | 147 | profile_info = _GetProfileInfoDict(input_proto.profile_info) |
LaMont Jones | 45ca6c4 | 2020-02-05 09:39:09 -0700 | [diff] [blame] | 148 | |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 149 | results = set() |
| 150 | sysroot_path = input_proto.sysroot.path |
| 151 | build_target = input_proto.sysroot.build_target.name |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 152 | for artifact_type in input_proto.artifact_types: |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 153 | # Unknown artifact_types are an error. |
| 154 | handler = _TOOLCHAIN_ARTIFACT_HANDLERS[artifact_type] |
| 155 | if handler.prepare: |
Tiancong Wang | ba2a1c2 | 2021-01-19 10:45:06 -0800 | [diff] [blame] | 156 | results.add( |
| 157 | handler.prepare(handler.name, chroot, sysroot_path, build_target, |
| 158 | input_artifacts, profile_info)) |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 159 | |
| 160 | # Translate the returns from the handlers we called. |
| 161 | # If any NEEDED => NEEDED |
| 162 | # elif any UNKNOWN => UNKNOWN |
| 163 | # elif any POINTLESS => POINTLESS |
| 164 | # else UNKNOWN. |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 165 | if toolchain_util.PrepareForBuildReturn.NEEDED in results: |
LaMont Jones | fd68cb1 | 2020-04-29 16:43:06 -0600 | [diff] [blame] | 166 | output_proto.build_relevance = PrepareForBuildResponse.NEEDED |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 167 | elif toolchain_util.PrepareForBuildReturn.UNKNOWN in results: |
LaMont Jones | fd68cb1 | 2020-04-29 16:43:06 -0600 | [diff] [blame] | 168 | output_proto.build_relevance = PrepareForBuildResponse.UNKNOWN |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 169 | elif toolchain_util.PrepareForBuildReturn.POINTLESS in results: |
LaMont Jones | fd68cb1 | 2020-04-29 16:43:06 -0600 | [diff] [blame] | 170 | output_proto.build_relevance = PrepareForBuildResponse.POINTLESS |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 171 | else: |
LaMont Jones | fd68cb1 | 2020-04-29 16:43:06 -0600 | [diff] [blame] | 172 | output_proto.build_relevance = PrepareForBuildResponse.UNKNOWN |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 173 | return controller.RETURN_CODE_SUCCESS |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 174 | |
| 175 | |
| 176 | # TODO(crbug/1031213): When @faux is expanded to have more than success/failure, |
| 177 | # this should be changed. |
| 178 | @faux.all_empty |
LaMont Jones | e911df0 | 2020-04-16 12:40:17 -0600 | [diff] [blame] | 179 | @validate.require('chroot.path', 'output_dir', 'artifact_types') |
LaMont Jones | 4579e8c | 2019-12-06 14:20:37 -0700 | [diff] [blame] | 180 | @validate.exists('output_dir') |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 181 | @validate.validation_complete |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 182 | def BundleArtifacts(input_proto: 'toolchain_pb2.BundleToolchainRequest', |
| 183 | output_proto: 'toolchain_pb2.BundleToolchainResponse', |
| 184 | _config: 'api_config.ApiConfig'): |
Alex Klein | cd03a5e | 2021-10-18 13:23:47 -0600 | [diff] [blame] | 185 | """Bundle valid toolchain artifacts. |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 186 | |
| 187 | The handlers (from _TOOLCHAIN_ARTIFACT_HANDLERS above) are called with: |
LaMont Jones | a215f1e | 2019-12-06 10:18:58 -0700 | [diff] [blame] | 188 | artifact_name (str): name of the artifact type |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 189 | chroot (chroot_lib.Chroot): chroot |
LaMont Jones | e911df0 | 2020-04-16 12:40:17 -0600 | [diff] [blame] | 190 | sysroot_path (str): sysroot path inside the chroot (e.g., /build/atlas), |
| 191 | or None. |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 192 | chrome_root (str): path to chrome root. (e.g., /b/s/w/ir/k/chrome) |
LaMont Jones | e911df0 | 2020-04-16 12:40:17 -0600 | [diff] [blame] | 193 | build_target_name (str): name of the build target (e.g., atlas), or None. |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 194 | output_dir (str): absolute path where artifacts are being bundled. |
| 195 | (e.g., /b/s/w/ir/k/recipe_cleanup/artifactssptfMU) |
LaMont Jones | e782167 | 2020-04-09 08:56:26 -0600 | [diff] [blame] | 196 | profile_info ({(str) name: (str) value}) Dictionary containing profile |
| 197 | information. |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 198 | |
| 199 | Note: the actual upload to GS is done by CI, not here. |
| 200 | |
| 201 | Args: |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 202 | input_proto: The input proto |
| 203 | output_proto: The output proto |
| 204 | _config: The API call config. |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 205 | """ |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 206 | chroot = controller_util.ParseChroot(input_proto.chroot) |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 207 | |
LaMont Jones | e782167 | 2020-04-09 08:56:26 -0600 | [diff] [blame] | 208 | profile_info = _GetProfileInfoDict(input_proto.profile_info) |
LaMont Jones | 45ca6c4 | 2020-02-05 09:39:09 -0700 | [diff] [blame] | 209 | |
Alex Klein | cd03a5e | 2021-10-18 13:23:47 -0600 | [diff] [blame] | 210 | output_path = Path(input_proto.output_dir) |
| 211 | |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 212 | for artifact_type in input_proto.artifact_types: |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 213 | if artifact_type not in _TOOLCHAIN_ARTIFACT_HANDLERS: |
| 214 | logging.error('%s not understood', artifact_type) |
| 215 | return controller.RETURN_CODE_UNRECOVERABLE |
Alex Klein | cd03a5e | 2021-10-18 13:23:47 -0600 | [diff] [blame] | 216 | |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 217 | handler = _TOOLCHAIN_ARTIFACT_HANDLERS[artifact_type] |
Alex Klein | cd03a5e | 2021-10-18 13:23:47 -0600 | [diff] [blame] | 218 | if not handler or not handler.bundle: |
| 219 | logging.warning('%s does not have a handler with a bundle function.', |
| 220 | artifact_type) |
| 221 | continue |
| 222 | |
| 223 | artifacts = handler.bundle(handler.name, chroot, input_proto.sysroot.path, |
| 224 | input_proto.sysroot.build_target.name, |
| 225 | input_proto.output_dir, profile_info) |
| 226 | if not artifacts: |
| 227 | continue |
| 228 | |
| 229 | # Filter out artifacts that do not exist or are empty. |
| 230 | usable_artifacts = [] |
| 231 | for artifact in artifacts: |
| 232 | artifact_path = output_path / artifact |
| 233 | if not artifact_path.exists(): |
| 234 | logging.warning('%s is not in the output directory.', artifact) |
| 235 | elif not artifact_path.stat().st_size: |
| 236 | logging.warning('%s is empty.', artifact) |
| 237 | else: |
| 238 | usable_artifacts.append(artifact) |
| 239 | |
| 240 | if not usable_artifacts: |
| 241 | logging.warning('No usable artifacts for artifact type %s', artifact_type) |
| 242 | continue |
| 243 | |
| 244 | # Add all usable artifacts. |
| 245 | art_info = output_proto.artifacts_info.add() |
| 246 | art_info.artifact_type = artifact_type |
| 247 | for artifact in usable_artifacts: |
| 248 | art_info.artifacts.add().path = artifact |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 249 | |
| 250 | |
Tiancong Wang | d521413 | 2021-01-12 10:43:57 -0800 | [diff] [blame] | 251 | def _GetUpdatedFilesResponse(_input_proto, output_proto, _config): |
| 252 | """Add successful status to the faux response.""" |
| 253 | file_info = output_proto.updated_files.add() |
| 254 | file_info.path = '/any/modified/file' |
| 255 | output_proto.commit_message = 'Commit message' |
| 256 | |
| 257 | |
| 258 | @faux.empty_error |
| 259 | @faux.success(_GetUpdatedFilesResponse) |
| 260 | @validate.require('uploaded_artifacts') |
| 261 | @validate.validation_complete |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 262 | def GetUpdatedFiles(input_proto: 'toolchain_pb2.GetUpdatedFilesRequest', |
| 263 | output_proto: 'toolchain_pb2.GetUpdatedFilesResponse', |
| 264 | _config: 'api_config.ApiConfig'): |
Tiancong Wang | d521413 | 2021-01-12 10:43:57 -0800 | [diff] [blame] | 265 | """Use uploaded artifacts to update some updates in a chromeos checkout. |
| 266 | |
| 267 | The function will call toolchain_util.GetUpdatedFiles using the type of |
| 268 | uploaded artifacts to make some changes in a checkout, and return the list |
| 269 | of change files together with commit message. |
| 270 | updated_artifacts: A list of UpdatedArtifacts type which contains a tuple |
| 271 | of artifact info and profile info. |
| 272 | Note: the actual creation of the commit is done by CI, not here. |
| 273 | |
| 274 | Args: |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 275 | input_proto: The input proto |
| 276 | output_proto: The output proto |
| 277 | _config: The API call config. |
Tiancong Wang | d521413 | 2021-01-12 10:43:57 -0800 | [diff] [blame] | 278 | """ |
| 279 | commit_message = '' |
| 280 | for artifact in input_proto.uploaded_artifacts: |
| 281 | artifact_type = artifact.artifact_info.artifact_type |
| 282 | if artifact_type not in _TOOLCHAIN_COMMIT_HANDLERS: |
| 283 | logging.error('%s not understood', artifact_type) |
| 284 | return controller.RETURN_CODE_UNRECOVERABLE |
| 285 | artifact_name = _TOOLCHAIN_COMMIT_HANDLERS[artifact_type] |
| 286 | if artifact_name: |
| 287 | assert len(artifact.artifact_info.artifacts) == 1, ( |
| 288 | 'Only one file to update per each artifact') |
| 289 | updated_files, message = toolchain_util.GetUpdatedFiles( |
| 290 | artifact_name, artifact.artifact_info.artifacts[0].path, |
| 291 | _GetProfileInfoDict(artifact.profile_info)) |
| 292 | for f in updated_files: |
| 293 | file_info = output_proto.updated_files.add() |
| 294 | file_info.path = f |
| 295 | |
| 296 | commit_message += message + '\n' |
| 297 | output_proto.commit_message = commit_message |
| 298 | # No commit footer is added for now. Can add more here if needed |
| 299 | |
| 300 | |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 301 | def _GetProfileInfoDict(profile_info: 'toolchain_pb2.ArtifactProfileInfo'): |
Ryan Beltran | f2a5dcc | 2022-04-19 20:34:00 +0000 | [diff] [blame] | 302 | """Convert profile_info to a dict. |
| 303 | |
| 304 | Args: |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 305 | profile_info: The artifact profile_info. |
Ryan Beltran | f2a5dcc | 2022-04-19 20:34:00 +0000 | [diff] [blame] | 306 | |
| 307 | Returns: |
| 308 | A dictionary containing profile info. |
| 309 | """ |
| 310 | ret = {} |
| 311 | which = profile_info.WhichOneof('artifact_profile_info') |
| 312 | if which: |
| 313 | value = getattr(profile_info, which) |
| 314 | # If it is a message, then use the contents of the message. This works as |
| 315 | # long as simple types do not have a 'DESCRIPTOR' attribute. (And protobuf |
| 316 | # messages do.) |
| 317 | if getattr(value, 'DESCRIPTOR', None): |
| 318 | ret.update({k.name: v for k, v in value.ListFields()}) |
| 319 | else: |
| 320 | ret[which] = value |
| 321 | return ret |
| 322 | |
| 323 | |
| 324 | LINTER_CODES = { |
| 325 | 'clang_tidy': toolchain_pb2.LinterFinding.CLANG_TIDY, |
Uwem Wilson | 21404d6 | 2022-07-08 18:51:08 +0000 | [diff] [blame] | 326 | 'cargo_clippy': toolchain_pb2.LinterFinding.CARGO_CLIPPY, |
| 327 | 'go_lint': toolchain_pb2.LinterFinding.GO_LINT |
Ryan Beltran | f2a5dcc | 2022-04-19 20:34:00 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | |
Ryan Beltran | 0df7fb0 | 2021-11-10 20:58:51 +0000 | [diff] [blame] | 331 | @faux.all_empty |
| 332 | @validate.exists('sysroot.path') |
| 333 | @validate.require('packages') |
| 334 | @validate.validation_complete |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 335 | def EmergeWithLinting(input_proto: 'toolchain_pb2.LinterRequest', |
| 336 | output_proto: 'toolchain_pb2.LinterResponse', |
| 337 | _config: 'api_config.ApiConfig'): |
Ryan Beltran | 0df7fb0 | 2021-11-10 20:58:51 +0000 | [diff] [blame] | 338 | """Emerge packages with linter features enabled and retrieves all findings. |
| 339 | |
| 340 | Args: |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 341 | input_proto: The nput proto with package and sysroot info. |
| 342 | output_proto: The output proto where findings are stored. |
| 343 | _config: The API call config (unused). |
Ryan Beltran | 0df7fb0 | 2021-11-10 20:58:51 +0000 | [diff] [blame] | 344 | """ |
| 345 | packages = [ |
Ryan Beltran | f2a5dcc | 2022-04-19 20:34:00 +0000 | [diff] [blame] | 346 | controller_util.deserialize_package_info(package) |
| 347 | for package in input_proto.packages |
| 348 | ] |
Ryan Beltran | 7d19180 | 2021-11-24 00:08:17 +0000 | [diff] [blame] | 349 | |
Ryan Beltran | f2a5dcc | 2022-04-19 20:34:00 +0000 | [diff] [blame] | 350 | build_linter = toolchain.BuildLinter( |
| 351 | packages, |
| 352 | input_proto.sysroot.path, |
| 353 | differential=input_proto.filter_modified) |
Ryan Beltran | 7d19180 | 2021-11-24 00:08:17 +0000 | [diff] [blame] | 354 | |
Ryan Beltran | 4425d5f | 2022-07-20 18:34:33 +0000 | [diff] [blame] | 355 | use_clippy = ( |
| 356 | toolchain_pb2.LinterFinding.CARGO_CLIPPY |
| 357 | not in input_proto.disabled_linters) |
| 358 | use_tidy = ( |
| 359 | toolchain_pb2.LinterFinding.CLANG_TIDY |
| 360 | not in input_proto.disabled_linters) |
| 361 | use_golint = ( |
| 362 | toolchain_pb2.LinterFinding.GO_LINT |
| 363 | not in input_proto.disabled_linters) |
| 364 | |
| 365 | findings = build_linter.emerge_with_linting(use_clippy=use_clippy, |
| 366 | use_tidy=use_tidy, |
| 367 | use_golint=use_golint) |
Ryan Beltran | f9a86f4 | 2022-04-13 20:58:18 +0000 | [diff] [blame] | 368 | |
Ryan Beltran | 7d19180 | 2021-11-24 00:08:17 +0000 | [diff] [blame] | 369 | for finding in findings: |
Ryan Beltran | f2a5dcc | 2022-04-19 20:34:00 +0000 | [diff] [blame] | 370 | locations = [] |
Ryan Beltran | 40e4ad1 | 2021-05-17 19:55:03 +0000 | [diff] [blame] | 371 | for location in finding.locations: |
Ryan Beltran | f2a5dcc | 2022-04-19 20:34:00 +0000 | [diff] [blame] | 372 | locations.append( |
Ryan Beltran | 40e4ad1 | 2021-05-17 19:55:03 +0000 | [diff] [blame] | 373 | toolchain_pb2.LinterFindingLocation( |
Ryan Beltran | f2a5dcc | 2022-04-19 20:34:00 +0000 | [diff] [blame] | 374 | filepath=location.filepath, |
Ryan Beltran | 40e4ad1 | 2021-05-17 19:55:03 +0000 | [diff] [blame] | 375 | line_start=location.line_start, |
Ryan Beltran | f2a5dcc | 2022-04-19 20:34:00 +0000 | [diff] [blame] | 376 | line_end=location.line_end)) |
| 377 | output_proto.findings.append( |
Ryan Beltran | 40e4ad1 | 2021-05-17 19:55:03 +0000 | [diff] [blame] | 378 | toolchain_pb2.LinterFinding( |
| 379 | message=finding.message, |
Ryan Beltran | f2a5dcc | 2022-04-19 20:34:00 +0000 | [diff] [blame] | 380 | locations=locations, |
| 381 | linter=LINTER_CODES[finding.linter])) |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 382 | |
| 383 | |
| 384 | @faux.all_empty |
| 385 | @validate.require('board') |
| 386 | @validate.validation_complete |
| 387 | def GetToolchainsForBoard(input_proto: 'toolchain_pb2.ToolchainsRequest', |
| 388 | output_proto: 'toolchain_pb2.ToolchainsReponse', |
| 389 | _config: 'api_config.ApiConfig'): |
| 390 | """Gets the default and non-default toolchains for a board. |
| 391 | |
| 392 | Args: |
| 393 | input_proto: The input proto with board and sysroot info. |
| 394 | output_proto: The output proto where findings are stored. |
| 395 | _config: The API call config (unused). |
| 396 | """ |
| 397 | toolchains = toolchain_lib.GetToolchainsForBoard(input_proto.board) |
| 398 | output_proto.default_toolchains.extend( |
| 399 | list(toolchain_lib.FilterToolchains(toolchains, 'default', True))) |
| 400 | output_proto.nondefault_toolchains.extend( |
| 401 | list(toolchain_lib.FilterToolchains(toolchains, 'default', False))) |