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