Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2020 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 | """Firmware builder controller. |
| 6 | |
| 7 | Handle all firmware builder related functionality. |
| 8 | """ |
| 9 | |
| 10 | import os |
| 11 | import tempfile |
| 12 | |
| 13 | from google.protobuf import json_format |
| 14 | |
| 15 | from chromite.api import controller |
| 16 | from chromite.api import faux |
| 17 | from chromite.api import validate |
| 18 | from chromite.api.gen.chromite.api import firmware_pb2 |
| 19 | from chromite.lib import constants |
| 20 | from chromite.lib import cros_build_lib |
| 21 | |
| 22 | def _call_entry(fw_loc, metric_proto, subcmd): |
| 23 | """Calls into firmware_builder.py with the specified subcmd.""" |
| 24 | |
| 25 | if fw_loc == firmware_pb2.PLATFORM_EC: |
| 26 | fw_path = 'src/platform/ec/' |
| 27 | elif fw_loc == firmware_pb2.PLATFORM_ZEPHYR: |
| 28 | fw_path = 'src/platform/zephyr-chrome/' |
Andrew Luo | a841935 | 2020-12-28 12:56:20 -0800 | [diff] [blame] | 29 | elif fw_loc == firmware_pb2.PLATFORM_TI50: |
Andrew Luo | 60439f3 | 2021-01-26 15:19:28 -0800 | [diff] [blame^] | 30 | fw_path = 'src/platform/ti50/common/' |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 31 | else: |
| 32 | cros_build_lib.Die(f'Unknown firmware location {fw_loc}!') |
| 33 | |
| 34 | entry_point = os.path.join(constants.SOURCE_ROOT, |
| 35 | fw_path, 'firmware_builder.py') |
| 36 | |
| 37 | with tempfile.NamedTemporaryFile() as file: |
| 38 | result = cros_build_lib.run([entry_point, '--metrics', file.name, subcmd], |
| 39 | check=False) |
| 40 | with open(file.name, 'r') as f: |
| 41 | response = f.read() |
| 42 | |
| 43 | # Parse the entire metric file as our metric proto (as a passthru) |
| 44 | json_format.Parse(response, metric_proto) |
| 45 | |
| 46 | if result.returncode == 0: |
| 47 | return controller.RETURN_CODE_SUCCESS |
| 48 | else: |
| 49 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
| 50 | |
| 51 | |
| 52 | def _BuildAllTotFirmwareResponse(_input_proto, output_proto, _config): |
| 53 | """Add a fw region metric to a successful repose.""" |
| 54 | |
| 55 | metric = output_proto.success.value.add() |
| 56 | metric.target_name = 'foo' |
| 57 | metric.platform_name = 'bar' |
| 58 | fw_section = metric.fw_section.add() |
| 59 | fw_section.region = firmware_pb2.FwBuildMetric.FwSection.EC_RO |
| 60 | fw_section.used = 100 |
| 61 | fw_section.total = 150 |
| 62 | |
| 63 | @faux.success(_BuildAllTotFirmwareResponse) |
| 64 | @faux.empty_completed_unsuccessfully_error |
| 65 | @validate.require('firmware_location') |
| 66 | @validate.validation_complete |
| 67 | def BuildAllTotFirmware(input_proto, output_proto, _config): |
| 68 | """Build all of the firmware targets at the specified location.""" |
| 69 | |
| 70 | return _call_entry(input_proto.firmware_location, output_proto.metrics, |
| 71 | 'build') |
| 72 | |
| 73 | |
| 74 | def _TestAllTotFirmwareResponse(_input_proto, output_proto, _config): |
| 75 | """Add a fw region metric to a successful repose.""" |
| 76 | |
| 77 | metric = output_proto.success.value.add() |
| 78 | metric.name = 'foo-test' |
| 79 | |
| 80 | @faux.success(_TestAllTotFirmwareResponse) |
| 81 | @faux.empty_completed_unsuccessfully_error |
| 82 | @validate.require('firmware_location') |
| 83 | @validate.validation_complete |
| 84 | def TestAllTotFirmware(input_proto, output_proto, _config): |
| 85 | """Runs all of the firmware tests at the specified location.""" |
| 86 | |
| 87 | return _call_entry(input_proto.firmware_location, output_proto.metrics, |
| 88 | 'test') |