blob: 9730aa2c273451d5e87fca5229b4db9bc21603e1 [file] [log] [blame]
Tiancong Wangaf050172019-07-10 11:52:03 -07001# -*- coding: utf-8 -*-
2# Copyright 2019 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Toolchain-related operations."""
7
8from __future__ import print_function
9
LaMont Jonesb20b3d92019-11-23 11:47:48 -070010import collections
11
Alex Klein076841b2019-08-29 15:19:39 -060012from chromite.api import faux
Alex Klein231d2da2019-07-22 16:44:45 -060013from chromite.api import validate
Tiancong Wang24a3df72019-08-20 15:48:51 -070014from chromite.api.gen.chromite.api import toolchain_pb2
Tiancong Wangaf050172019-07-10 11:52:03 -070015from chromite.lib import toolchain_util
LaMont Jonesb20b3d92019-11-23 11:47:48 -070016from chromite.api.gen.chromiumos.builder_config_pb2 import BuilderConfig
Tiancong Wangaf050172019-07-10 11:52:03 -070017
LaMont Jonesb20b3d92019-11-23 11:47:48 -070018# TODO(crbug/1019868): Add handlers as needed.
19_Handlers = collections.namedtuple('_Handlers', ['name', 'prepare', 'bundle'])
20_TOOLCHAIN_ARTIFACT_HANDLERS = {
21 BuilderConfig.Artifacts.UNVERIFIED_ORDERING_FILE:
22 _Handlers('UnverifiedOrderingFile', None, None),
23 BuilderConfig.Artifacts.VERIFIED_ORDERING_FILE:
LaMont Jones4579e8c2019-12-06 14:20:37 -070024 _Handlers('VerifiedOrderingFile', None, None),
LaMont Jonesb20b3d92019-11-23 11:47:48 -070025 BuilderConfig.Artifacts.CHROME_CLANG_WARNINGS_FILE:
26 _Handlers('ChromeClangWarningsFile', None, None),
27 BuilderConfig.Artifacts.UNVERIFIED_LLVM_PGO_FILE:
28 _Handlers('UnverifiedLlvmPgoFile', None, None),
29 BuilderConfig.Artifacts.UNVERIFIED_CHROME_AFDO_FILE:
30 _Handlers('UnverifiedChromeAfdoFile', None, None),
31 BuilderConfig.Artifacts.VERIFIED_CHROME_AFDO_FILE:
32 _Handlers('VerifiedChromeAfdoFile', None, None),
33 BuilderConfig.Artifacts.VERIFIED_KERNEL_AFDO_FILE:
34 _Handlers('VerifiedKernelAfdoFile', None, None),
35}
36
37
38# TODO(crbug/1031213): When @faux is expanded to have more than success/failure,
39# this should be changed.
40@faux.all_empty
LaMont Jones4579e8c2019-12-06 14:20:37 -070041@validate.require('chroot.path', 'sysroot.path', 'sysroot.build_target.name',
42 'artifact_types')
LaMont Jonesb20b3d92019-11-23 11:47:48 -070043@validate.validation_complete
44def PrepareForBuild(input_proto, output_proto, _config):
45 """Prepare to build toolchain artifacts.
46
47 The handlers (from _TOOLCHAIN_ARTIFACT_HANDLERS above) are called with:
48 artifact_name (str): name of the artifact type.
LaMont Jonesa215f1e2019-12-06 10:18:58 -070049 chroot_path (str): chroot path (absolute path)
50 sysroot_path (str): sysroot path inside the chroot (e.g., /build/atlas)
51 build_target_name (str): name of the build target (e.g., atlas)
52
53 They locate and modify any ebuilds and/or source required for the artifact
54 being created, then return a value from toolchain_util.PrepareForBuildReturn.
LaMont Jonesb20b3d92019-11-23 11:47:48 -070055
56 Args:
57 input_proto (PrepareForToolchainBuildRequest): The input proto
58 output_proto (PrepareForToolchainBuildResponse): The output proto
59 _config (api_config.ApiConfig): The API call config.
60 """
61 results = set()
LaMont Jones4579e8c2019-12-06 14:20:37 -070062
LaMont Jonesb20b3d92019-11-23 11:47:48 -070063 for artifact_type in input_proto.artifact_types:
64 # Ignore any artifact_types not handled.
65 handler = _TOOLCHAIN_ARTIFACT_HANDLERS.get(artifact_type)
66 if handler and handler.prepare:
LaMont Jonesa215f1e2019-12-06 10:18:58 -070067 results.add(handler.prepare(
68 handler.name, input_proto.chroot.path, input_proto.sysroot.path,
69 input_proto.sysroot.build_target.name))
LaMont Jonesb20b3d92019-11-23 11:47:48 -070070
71 # Translate the returns from the handlers we called.
72 # If any NEEDED => NEEDED
73 # elif any UNKNOWN => UNKNOWN
74 # elif any POINTLESS => POINTLESS
75 # else UNKNOWN.
76 proto_resp = toolchain_pb2.PrepareForToolchainBuildResponse
77 if toolchain_util.PrepareForBuildReturn.NEEDED in results:
78 output_proto.build_relevance = proto_resp.NEEDED
79 elif toolchain_util.PrepareForBuildReturn.UNKNOWN in results:
80 output_proto.build_relevance = proto_resp.UNKNOWN
81 elif toolchain_util.PrepareForBuildReturn.POINTLESS in results:
82 output_proto.build_relevance = proto_resp.POINTLESS
83 else:
84 output_proto.build_relevance = proto_resp.UNKNOWN
85
86
87# TODO(crbug/1031213): When @faux is expanded to have more than success/failure,
88# this should be changed.
89@faux.all_empty
LaMont Jones4579e8c2019-12-06 14:20:37 -070090@validate.require('chroot.path', 'sysroot.path', 'sysroot.build_target.name',
91 'output_dir', 'artifact_types')
92@validate.exists('output_dir')
LaMont Jonesb20b3d92019-11-23 11:47:48 -070093def BundleArtifacts(input_proto, output_proto, _config):
94 """Bundle toolchain artifacts.
95
96 The handlers (from _TOOLCHAIN_ARTIFACT_HANDLERS above) are called with:
LaMont Jonesa215f1e2019-12-06 10:18:58 -070097 artifact_name (str): name of the artifact type
LaMont Jonesb20b3d92019-11-23 11:47:48 -070098 chroot_path (str): chroot path (absolute path)
99 sysroot_path (str): sysroot path inside the chroot (e.g., /build/atlas)
100 build_target_name (str): name of the build target (e.g., atlas)
101 output_dir (str): absolute path where artifacts are being bundled.
102 (e.g., /b/s/w/ir/k/recipe_cleanup/artifactssptfMU)
103
104 Note: the actual upload to GS is done by CI, not here.
105
106 Args:
107 input_proto (BundleToolchainRequest): The input proto
108 output_proto (BundleToolchainResponse): The output proto
109 _config (api_config.ApiConfig): The API call config.
110 """
LaMont Jones4579e8c2019-12-06 14:20:37 -0700111 resp_artifact = toolchain_pb2.ArtifactInfo
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700112
113 for artifact_type in input_proto.artifact_types:
114 # Ignore any artifact_types not handled.
115 handler = _TOOLCHAIN_ARTIFACT_HANDLERS.get(artifact_type)
116 if handler and handler.bundle:
117 artifacts = handler.bundle(
118 handler.name, input_proto.chroot.path, input_proto.sysroot.path,
119 input_proto.sysroot.build_target.name, input_proto.output_dir)
120 if artifacts:
121 output_proto.artifacts_info.append(
122 resp_artifact(artifact_type=artifact_type, artifacts=artifacts))
123
124
125# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
126_NAMES_FOR_AFDO_ARTIFACTS = {
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700127 toolchain_pb2.ORDERFILE: 'orderfile',
128 toolchain_pb2.KERNEL_AFDO: 'kernel_afdo',
129 toolchain_pb2.CHROME_AFDO: 'chrome_afdo'
130}
131
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700132# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700133# Using a function instead of a dict because we need to mock these
134# functions in unittest, and mock doesn't play well with a dict definition.
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700135def _GetMethodForUpdatingAFDOArtifacts(artifact_type):
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700136 return {
137 toolchain_pb2.ORDERFILE: toolchain_util.OrderfileUpdateChromeEbuild,
138 toolchain_pb2.KERNEL_AFDO: toolchain_util.AFDOUpdateKernelEbuild,
139 toolchain_pb2.CHROME_AFDO: toolchain_util.AFDOUpdateChromeEbuild
140 }[artifact_type]
Tiancong Wang24a3df72019-08-20 15:48:51 -0700141
Tiancong Wangaf050172019-07-10 11:52:03 -0700142
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700143# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700144def _UpdateEbuildWithAFDOArtifactsResponse(_input_proto, output_proto, _config):
145 """Add successful status to the faux response."""
146 output_proto.status = True
147
148
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700149# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700150@faux.success(_UpdateEbuildWithAFDOArtifactsResponse)
151@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -0600152@validate.require('build_target.name')
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700153@validate.is_in('artifact_type', _NAMES_FOR_AFDO_ARTIFACTS)
Alex Klein231d2da2019-07-22 16:44:45 -0600154@validate.validation_complete
Tiancong Wang24a3df72019-08-20 15:48:51 -0700155def UpdateEbuildWithAFDOArtifacts(input_proto, output_proto, _config):
156 """Update Chrome or kernel ebuild with most recent unvetted artifacts.
Tiancong Wangaf050172019-07-10 11:52:03 -0700157
158 Args:
Tiancong Wang24a3df72019-08-20 15:48:51 -0700159 input_proto (VerifyAFDOArtifactsRequest): The input proto
160 output_proto (VerifyAFDOArtifactsResponse): The output proto
Alex Klein231d2da2019-07-22 16:44:45 -0600161 _config (api_config.ApiConfig): The API call config.
Tiancong Wangaf050172019-07-10 11:52:03 -0700162 """
Tiancong Wangaf050172019-07-10 11:52:03 -0700163 board = input_proto.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700164 update_method = _GetMethodForUpdatingAFDOArtifacts(input_proto.artifact_type)
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700165 output_proto.status = update_method(board)
Tiancong Wangaf050172019-07-10 11:52:03 -0700166
167
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700168# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700169def _UploadVettedAFDOArtifactsResponse(_input_proto, output_proto, _config):
170 """Add successful status to the faux response."""
171 output_proto.status = True
172
173
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700174# TODO(crbug/1019868): Remove legacy code when cbuildbot builders are gone.
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700175@faux.success(_UploadVettedAFDOArtifactsResponse)
176@faux.empty_error
Tiancong Wang24a3df72019-08-20 15:48:51 -0700177@validate.require('build_target.name')
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700178@validate.is_in('artifact_type', _NAMES_FOR_AFDO_ARTIFACTS)
Alex Klein231d2da2019-07-22 16:44:45 -0600179@validate.validation_complete
Tiancong Wang24a3df72019-08-20 15:48:51 -0700180def UploadVettedAFDOArtifacts(input_proto, output_proto, _config):
Tiancong Wangaf050172019-07-10 11:52:03 -0700181 """Upload a vetted orderfile to GS bucket.
182
183 Args:
Tiancong Wang24a3df72019-08-20 15:48:51 -0700184 input_proto (VerifyAFDOArtifactsRequest): The input proto
185 output_proto (VerifyAFDOArtifactsResponse): The output proto
Alex Klein231d2da2019-07-22 16:44:45 -0600186 _config (api_config.ApiConfig): The API call config.
Tiancong Wangaf050172019-07-10 11:52:03 -0700187 """
Tiancong Wang24a3df72019-08-20 15:48:51 -0700188 board = input_proto.build_target.name
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700189 artifact_type = _NAMES_FOR_AFDO_ARTIFACTS[input_proto.artifact_type]
Tiancong Wang24a3df72019-08-20 15:48:51 -0700190 output_proto.status = toolchain_util.UploadAndPublishVettedAFDOArtifacts(
191 artifact_type, board)