Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2018 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 | |
| 6 | """Compile the Build API's proto.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import os |
| 11 | |
| 12 | from chromite.lib import commandline |
| 13 | from chromite.lib import cros_build_lib |
Alex Klein | 2900d34 | 2019-01-23 14:03:25 -0700 | [diff] [blame^] | 14 | from chromite.lib import cros_logging as logging |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 15 | |
| 16 | |
| 17 | def GetParser(): |
| 18 | parser = commandline.ArgumentParser(description=__doc__) |
| 19 | return parser |
| 20 | |
| 21 | |
| 22 | def _ParseArguments(argv): |
| 23 | """Parse and validate arguments.""" |
| 24 | parser = GetParser() |
| 25 | opts = parser.parse_args(argv) |
| 26 | |
| 27 | opts.Freeze() |
| 28 | return opts |
| 29 | |
| 30 | |
| 31 | def main(argv): |
| 32 | _opts = _ParseArguments(argv) |
| 33 | |
| 34 | base_dir = os.path.abspath(os.path.join(__file__, '..')) |
| 35 | output = os.path.join(base_dir, 'gen') |
| 36 | source = os.path.join(base_dir, 'proto') |
| 37 | targets = os.path.join(source, '*.proto') |
| 38 | |
Alex Klein | 2900d34 | 2019-01-23 14:03:25 -0700 | [diff] [blame^] | 39 | # TODO(crbug.com/924660) Update compile to run in the chroot and remove |
| 40 | # the warning. |
| 41 | protoc_version = ['protoc', '--version'] |
| 42 | result = cros_build_lib.RunCommand(protoc_version, print_cmd=False, |
| 43 | redirect_stdout=True, |
| 44 | combine_stdout_stderr=True, |
| 45 | error_code_ok=True) |
| 46 | if not result.returncode == 0 or not '3.6.1' in result.output: |
| 47 | logging.warning('You must have libprotoc 3.6.1 installed locally to ' |
| 48 | 'compile the protobuf correctly.') |
| 49 | logging.warning('This will be run in the chroot in the future ' |
| 50 | '(see crbug.com/924660).') |
| 51 | if not result.returncode == 0: |
| 52 | logging.warning('protoc could not be found on your system.') |
| 53 | else: |
| 54 | logging.warning('"%s" was found on your system.', result.output.strip()) |
| 55 | |
| 56 | logging.warning("We won't stop you from running it for now, but be very " |
| 57 | "weary of your changes.") |
| 58 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 59 | cmd = ('protoc --python_out %(output)s --proto_path %(source)s %(targets)s' |
| 60 | % {'output': output, 'source': source, 'targets': targets}) |
| 61 | |
| 62 | result = cros_build_lib.RunCommand(cmd, shell=True, error_code_ok=True) |
| 63 | return result.returncode |