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