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