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 |
Alex Klein | c33c191 | 2019-02-15 10:29:13 -0700 | [diff] [blame^] | 13 | from chromite.lib import constants |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 14 | from chromite.lib import cros_build_lib |
Alex Klein | 2900d34 | 2019-01-23 14:03:25 -0700 | [diff] [blame] | 15 | from chromite.lib import cros_logging as logging |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 16 | |
| 17 | |
| 18 | def GetParser(): |
| 19 | parser = commandline.ArgumentParser(description=__doc__) |
| 20 | return parser |
| 21 | |
| 22 | |
| 23 | def _ParseArguments(argv): |
| 24 | """Parse and validate arguments.""" |
| 25 | parser = GetParser() |
| 26 | opts = parser.parse_args(argv) |
| 27 | |
| 28 | opts.Freeze() |
| 29 | return opts |
| 30 | |
| 31 | |
| 32 | def main(argv): |
| 33 | _opts = _ParseArguments(argv) |
| 34 | |
Alex Klein | c33c191 | 2019-02-15 10:29:13 -0700 | [diff] [blame^] | 35 | base_dir = os.path.join(constants.CHROOT_SOURCE_ROOT, 'chromite', 'api') |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 36 | output = os.path.join(base_dir, 'gen') |
| 37 | source = os.path.join(base_dir, 'proto') |
| 38 | targets = os.path.join(source, '*.proto') |
| 39 | |
Alex Klein | c33c191 | 2019-02-15 10:29:13 -0700 | [diff] [blame^] | 40 | version = cros_build_lib.RunCommand(['protoc', '--version'], print_cmd=False, |
| 41 | enter_chroot=True, capture_output=True, |
| 42 | error_code_ok=True) |
| 43 | if version.returncode != 0: |
| 44 | cros_build_lib.Die('protoc not found in your chroot.') |
| 45 | elif '3.3.0' in version.output: |
| 46 | # This is the old chroot version, just needs to have update_chroot run. |
| 47 | cros_build_lib.Die('Old protoc version detected. Please update your chroot' |
| 48 | 'and try again: `cros_sdk -- ./update_chroot`') |
| 49 | elif '3.6.1' not in version.output: |
| 50 | # Note: We know some lower versions have some compiling backwards |
| 51 | # compatibility problems. One would hope new versions would be ok, |
| 52 | # but we would have said that with earlier versions too. |
| 53 | logging.warning('Unsupported protoc version found in your chroot.\n' |
| 54 | "libprotoc 3.6.1 is supported. Found '%s'.\n" |
| 55 | 'protoc will still be run, but be cautious.', |
| 56 | version.output.strip()) |
Alex Klein | 2900d34 | 2019-01-23 14:03:25 -0700 | [diff] [blame] | 57 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 58 | cmd = ('protoc --python_out %(output)s --proto_path %(source)s %(targets)s' |
| 59 | % {'output': output, 'source': source, 'targets': targets}) |
Alex Klein | c33c191 | 2019-02-15 10:29:13 -0700 | [diff] [blame^] | 60 | result = cros_build_lib.RunCommand(cmd, enter_chroot=True, shell=True, |
| 61 | error_code_ok=True) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 62 | return result.returncode |