Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 1 | # Copyright 2020 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. |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 4 | |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 5 | """Firmware builder controller. |
| 6 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 7 | Handle all firmware builder related functionality. Currently no service module |
| 8 | exists: all of the work is done here. |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 9 | """ |
| 10 | |
| 11 | import os |
| 12 | import tempfile |
| 13 | |
Mike Frysinger | 2c02406 | 2021-05-22 15:43:22 -0400 | [diff] [blame] | 14 | from chromite.third_party.google.protobuf import json_format |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 15 | |
| 16 | from chromite.api import controller |
| 17 | from chromite.api import faux |
| 18 | from chromite.api import validate |
| 19 | from chromite.api.gen.chromite.api import firmware_pb2 |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 20 | from chromite.api.gen.chromiumos import common_pb2 |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 21 | from chromite.lib import constants |
| 22 | from chromite.lib import cros_build_lib |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 23 | from chromite.lib import osutils |
| 24 | |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 25 | |
LaMont Jones | b7be76b | 2021-02-16 16:22:14 -0700 | [diff] [blame] | 26 | def _call_entry(fw_loc, metric_proto, subcmd, *args, **kwargs): |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 27 | """Calls into firmware_builder.py with the specified subcmd.""" |
| 28 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 29 | if fw_loc == common_pb2.PLATFORM_EC: |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 30 | fw_path = 'src/platform/ec/' |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 31 | elif fw_loc == common_pb2.PLATFORM_ZEPHYR: |
Jack Rosenthal | d56a9b2 | 2021-03-25 15:36:42 -0600 | [diff] [blame] | 32 | fw_path = 'src/platform/ec/zephyr/' |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 33 | elif fw_loc == common_pb2.PLATFORM_TI50: |
Andrew Luo | 60439f3 | 2021-01-26 15:19:28 -0800 | [diff] [blame] | 34 | fw_path = 'src/platform/ti50/common/' |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 35 | else: |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 36 | cros_build_lib.Die(f'Unknown firmware location {fw_loc}.') |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 37 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 38 | entry_point = os.path.join(constants.SOURCE_ROOT, fw_path, |
| 39 | 'firmware_builder.py') |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 40 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 41 | with tempfile.NamedTemporaryFile() as tmpfile: |
LaMont Jones | b7be76b | 2021-02-16 16:22:14 -0700 | [diff] [blame] | 42 | cmd = [entry_point, '--metrics', tmpfile.name] + list(args) |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 43 | for key, value in kwargs.items(): |
| 44 | cmd += [f'--{key.replace("_", "-")}', value] |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 45 | cmd += [subcmd] |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 46 | |
| 47 | result = cros_build_lib.run(cmd, check=False) |
| 48 | with open(tmpfile.name, 'r') as f: |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 49 | response = f.read() |
| 50 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 51 | if metric_proto: |
| 52 | # Parse the entire metric file as our metric proto (as a passthru). |
| 53 | # TODO(b/177907747): BundleFirmwareArtifacts doesn't use this (yet?), but |
| 54 | # firmware_builder.py requires it. |
| 55 | json_format.Parse(response, metric_proto) |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 56 | |
| 57 | if result.returncode == 0: |
| 58 | return controller.RETURN_CODE_SUCCESS |
| 59 | else: |
| 60 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
| 61 | |
| 62 | |
| 63 | def _BuildAllTotFirmwareResponse(_input_proto, output_proto, _config): |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 64 | """Add a fw region metric to a successful response.""" |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 65 | |
| 66 | metric = output_proto.success.value.add() |
| 67 | metric.target_name = 'foo' |
| 68 | metric.platform_name = 'bar' |
| 69 | fw_section = metric.fw_section.add() |
Andrew Luo | d08045d | 2021-06-17 12:06:44 -0700 | [diff] [blame^] | 70 | fw_section.region = 'EC_RO' |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 71 | fw_section.used = 100 |
| 72 | fw_section.total = 150 |
| 73 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 74 | |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 75 | @faux.success(_BuildAllTotFirmwareResponse) |
| 76 | @faux.empty_completed_unsuccessfully_error |
| 77 | @validate.require('firmware_location') |
| 78 | @validate.validation_complete |
| 79 | def BuildAllTotFirmware(input_proto, output_proto, _config): |
| 80 | """Build all of the firmware targets at the specified location.""" |
| 81 | |
LaMont Jones | b7be76b | 2021-02-16 16:22:14 -0700 | [diff] [blame] | 82 | args = ['--code-coverage'] if input_proto.code_coverage else [] |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 83 | return _call_entry(input_proto.firmware_location, output_proto.metrics, |
LaMont Jones | b7be76b | 2021-02-16 16:22:14 -0700 | [diff] [blame] | 84 | 'build', *args) |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 85 | |
| 86 | |
| 87 | def _TestAllTotFirmwareResponse(_input_proto, output_proto, _config): |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 88 | """Add a fw region metric to a successful response.""" |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 89 | |
| 90 | metric = output_proto.success.value.add() |
| 91 | metric.name = 'foo-test' |
| 92 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 93 | |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 94 | @faux.success(_TestAllTotFirmwareResponse) |
| 95 | @faux.empty_completed_unsuccessfully_error |
| 96 | @validate.require('firmware_location') |
| 97 | @validate.validation_complete |
| 98 | def TestAllTotFirmware(input_proto, output_proto, _config): |
| 99 | """Runs all of the firmware tests at the specified location.""" |
| 100 | |
LaMont Jones | b7be76b | 2021-02-16 16:22:14 -0700 | [diff] [blame] | 101 | args = ['--code-coverage'] if input_proto.code_coverage else [] |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 102 | return _call_entry(input_proto.firmware_location, output_proto.metrics, |
LaMont Jones | b7be76b | 2021-02-16 16:22:14 -0700 | [diff] [blame] | 103 | 'test', *args) |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 104 | |
| 105 | |
| 106 | def _BuildAllFirmwareResponse(_input_proto, output_proto, _config): |
| 107 | """Add a fw region metric to a successful response.""" |
| 108 | |
Michael Mortensen | 515c889 | 2021-02-26 15:37:59 -0700 | [diff] [blame] | 109 | metric = output_proto.metrics.value.add() |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 110 | metric.target_name = 'foo' |
| 111 | metric.platform_name = 'bar' |
| 112 | fw_section = metric.fw_section.add() |
Andrew Luo | d08045d | 2021-06-17 12:06:44 -0700 | [diff] [blame^] | 113 | fw_section.region = 'EC_RO' |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 114 | fw_section.used = 100 |
| 115 | fw_section.total = 150 |
| 116 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 117 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 118 | @faux.success(_BuildAllFirmwareResponse) |
| 119 | @faux.empty_completed_unsuccessfully_error |
| 120 | @validate.require('firmware_location') |
| 121 | @validate.validation_complete |
| 122 | def BuildAllFirmware(input_proto, output_proto, _config): |
| 123 | """Build all of the firmware targets at the specified location.""" |
| 124 | |
LaMont Jones | b7be76b | 2021-02-16 16:22:14 -0700 | [diff] [blame] | 125 | args = ['--code-coverage'] if input_proto.code_coverage else [] |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 126 | return _call_entry(input_proto.firmware_location, output_proto.metrics, |
LaMont Jones | b7be76b | 2021-02-16 16:22:14 -0700 | [diff] [blame] | 127 | 'build', *args) |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 128 | |
| 129 | |
| 130 | def _TestAllFirmwareResponse(_input_proto, output_proto, _config): |
| 131 | """Add a fw region metric to a successful response.""" |
| 132 | |
| 133 | metric = output_proto.success.value.add() |
| 134 | metric.name = 'foo-test' |
| 135 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 136 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 137 | @faux.success(_TestAllFirmwareResponse) |
| 138 | @faux.empty_completed_unsuccessfully_error |
| 139 | @validate.require('firmware_location') |
| 140 | @validate.validation_complete |
| 141 | def TestAllFirmware(input_proto, output_proto, _config): |
| 142 | """Runs all of the firmware tests at the specified location.""" |
| 143 | |
LaMont Jones | b7be76b | 2021-02-16 16:22:14 -0700 | [diff] [blame] | 144 | args = ['--code-coverage'] if input_proto.code_coverage else [] |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 145 | return _call_entry(input_proto.firmware_location, output_proto.metrics, |
LaMont Jones | b7be76b | 2021-02-16 16:22:14 -0700 | [diff] [blame] | 146 | 'test', *args) |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 147 | |
| 148 | |
| 149 | def _BundleFirmwareArtifactsResponse(_input_proto, output_proto, _config): |
| 150 | """Add a fw region metric to a successful response.""" |
| 151 | |
| 152 | metric = output_proto.success.value.add() |
| 153 | metric.name = 'foo-test' |
| 154 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 155 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 156 | @faux.success(_BundleFirmwareArtifactsResponse) |
| 157 | @faux.empty_completed_unsuccessfully_error |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 158 | @validate.validation_complete |
| 159 | def BundleFirmwareArtifacts(input_proto, output_proto, _config): |
| 160 | """Runs all of the firmware tests at the specified location.""" |
| 161 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 162 | if len(input_proto.artifacts.output_artifacts) > 1: |
LaMont Jones | 9a0594d | 2021-03-17 15:51:20 -0600 | [diff] [blame] | 163 | raise ValueError('Must have exactly one output_artifact entry') |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 164 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 165 | with osutils.TempDir(delete=False) as tmpdir: |
| 166 | info = input_proto.artifacts.output_artifacts[0] |
| 167 | metadata_path = os.path.join(tmpdir, 'firmware_metadata.jsonpb') |
LaMont Jones | b7be76b | 2021-02-16 16:22:14 -0700 | [diff] [blame] | 168 | args = [] |
| 169 | if input_proto.artifacts.FIRMWARE_LCOV in info.artifact_types: |
| 170 | args += ['--code-coverage'] |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 171 | resp = _call_entry( |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 172 | info.location, |
| 173 | None, |
| 174 | 'bundle', |
LaMont Jones | b7be76b | 2021-02-16 16:22:14 -0700 | [diff] [blame] | 175 | *args, |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 176 | output_dir=tmpdir, |
| 177 | metadata=metadata_path) |
LaMont Jones | 9a0594d | 2021-03-17 15:51:20 -0600 | [diff] [blame] | 178 | file_paths = [] |
| 179 | if os.path.exists(metadata_path): |
LaMont Jones | 80dc8bc | 2021-02-09 16:29:30 -0700 | [diff] [blame] | 180 | with open(metadata_path, 'r') as f: |
| 181 | metadata = json_format.Parse(f.read(), |
| 182 | firmware_pb2.FirmwareArtifactInfo()) |
LaMont Jones | 9a0594d | 2021-03-17 15:51:20 -0600 | [diff] [blame] | 183 | else: |
| 184 | metadata = firmware_pb2.FirmwareArtifactInfo() |
| 185 | if input_proto.artifacts.FIRMWARE_TARBALL_INFO in info.artifact_types: |
| 186 | output_proto.artifacts.artifacts.add( |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 187 | artifact_type=input_proto.artifacts.FIRMWARE_TARBALL_INFO, |
LaMont Jones | 9a0594d | 2021-03-17 15:51:20 -0600 | [diff] [blame] | 188 | location=info.location, paths=[ |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 189 | common_pb2.Path( |
| 190 | path=metadata_path, location=common_pb2.Path.INSIDE) |
| 191 | ]) |
LaMont Jones | 9a0594d | 2021-03-17 15:51:20 -0600 | [diff] [blame] | 192 | |
| 193 | full_path = lambda x: common_pb2.Path( |
| 194 | path=os.path.join(tmpdir, x.file_name), |
| 195 | location=common_pb2.Path.INSIDE) |
| 196 | |
| 197 | for typ, name in ( |
| 198 | (input_proto.artifacts.FIRMWARE_TARBALL, 'tarball_info'), |
| 199 | (input_proto.artifacts.FIRMWARE_LCOV, 'lcov_info')): |
| 200 | file_paths = [ |
| 201 | full_path(x) for x in metadata.objects |
| 202 | if x.WhichOneof('firmware_object_info') == name |
LaMont Jones | 80dc8bc | 2021-02-09 16:29:30 -0700 | [diff] [blame] | 203 | ] |
LaMont Jones | 9a0594d | 2021-03-17 15:51:20 -0600 | [diff] [blame] | 204 | if (file_paths and typ in info.artifact_types): |
| 205 | output_proto.artifacts.artifacts.add( |
| 206 | artifact_type=typ, paths=file_paths, location=info.location) |
| 207 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 208 | return resp |