blob: fcb2b789503056da60e92f7aa6446c39f9f2c806 [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/'
Andrew Luoa8419352020-12-28 12:56:20 -080029 elif fw_loc == firmware_pb2.PLATFORM_TI50:
Andrew Luo60439f32021-01-26 15:19:28 -080030 fw_path = 'src/platform/ti50/common/'
Jett Rink17ed0f52020-09-25 17:14:31 -060031 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
52def _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
67def 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
74def _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
84def 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')