|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 4 | +# Use of this source code is governed by a BSD-style license that can be |
| 5 | +# found in the LICENSE file. |
| 6 | +"""Build and test all of the EC boards. |
| 7 | +
|
| 8 | +This is the entry point for the custom firmware builder workflow recipe. |
| 9 | +""" |
| 10 | + |
| 11 | +import argparse |
| 12 | +import multiprocessing |
| 13 | +import os |
| 14 | +import subprocess |
| 15 | +import sys |
| 16 | + |
| 17 | +from google.protobuf import json_format |
| 18 | + |
| 19 | +from chromite.api.gen.chromite.api import firmware_pb2 |
| 20 | + |
| 21 | +def build(opts): |
| 22 | + """Builds all EC firmware targets""" |
| 23 | + # TODO(b/169178847): Add appropriate metric information |
| 24 | + metrics = firmware_pb2.FwBuildMetricList() |
| 25 | + with open(opts.metrics, 'w') as f: |
| 26 | + f.write(json_format.MessageToJson(metrics)) |
| 27 | + return subprocess.run(['make', 'buildall_only', '-j{}'.format(opts.cpus)], |
| 28 | + cwd=os.path.dirname(__file__)).returncode |
| 29 | + |
| 30 | + |
| 31 | +def test(opts): |
| 32 | + """Runs all of the unit tests for EC firmware""" |
| 33 | + # TODO(b/169178847): Add appropriate metric information |
| 34 | + metrics = firmware_pb2.FwTestMetricList() |
| 35 | + with open(opts.metrics, 'w') as f: |
| 36 | + f.write(json_format.MessageToJson(metrics)) |
| 37 | + return subprocess.run(['make', 'runtests', '-j{}'.format(opts.cpus)], |
| 38 | + cwd=os.path.dirname(__file__)).returncode |
| 39 | + |
| 40 | + |
| 41 | +def main(args): |
| 42 | + """Builds and tests all of the EC targets and reports build metrics""" |
| 43 | + opts = parse_args(args) |
| 44 | + |
| 45 | + if not hasattr(opts, 'func'): |
| 46 | + print("Must select a valid sub command!") |
| 47 | + return -1 |
| 48 | + |
| 49 | + # Run selected sub command function |
| 50 | + return opts.func(opts) |
| 51 | + |
| 52 | + |
| 53 | +def parse_args(args): |
| 54 | + parser = argparse.ArgumentParser(description=__doc__) |
| 55 | + |
| 56 | + parser.add_argument( |
| 57 | + '--cpus', |
| 58 | + default=multiprocessing.cpu_count(), |
| 59 | + help='The number of cores to use.', |
| 60 | + ) |
| 61 | + |
| 62 | + parser.add_argument( |
| 63 | + '--metrics', |
| 64 | + dest='metrics', |
| 65 | + required=True, |
| 66 | + help='File to write the json-encoded MetricsList proto message.', |
| 67 | + ) |
| 68 | + |
| 69 | + # Would make this required=True, but not available until 3.7 |
| 70 | + sub_cmds = parser.add_subparsers() |
| 71 | + |
| 72 | + build_cmd = sub_cmds.add_parser('build', |
| 73 | + help='Builds all firmware targets') |
| 74 | + build_cmd.set_defaults(func=build) |
| 75 | + |
| 76 | + test_cmd = sub_cmds.add_parser('test', help='Runs all firmware unit tests') |
| 77 | + test_cmd.set_defaults(func=test) |
| 78 | + |
| 79 | + return parser.parse_args(args) |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == '__main__': |
| 83 | + sys.exit(main(sys.argv[1:])) |
0 commit comments