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