blob: 5f3b8ab475645cd5e957947644bad6f23350d846 [file] [log] [blame]
Alex Kleinf4dc4f52018-12-05 13:55:12 -07001# -*- 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
8from __future__ import print_function
9
10import os
11
12from chromite.lib import commandline
Alex Kleinc33c1912019-02-15 10:29:13 -070013from chromite.lib import constants
Alex Kleinf4dc4f52018-12-05 13:55:12 -070014from chromite.lib import cros_build_lib
Alex Klein2900d342019-01-23 14:03:25 -070015from chromite.lib import cros_logging as logging
Alex Kleinf4dc4f52018-12-05 13:55:12 -070016
17
18def GetParser():
19 parser = commandline.ArgumentParser(description=__doc__)
20 return parser
21
22
23def _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
32def main(argv):
33 _opts = _ParseArguments(argv)
34
Alex Kleinc33c1912019-02-15 10:29:13 -070035 base_dir = os.path.join(constants.CHROOT_SOURCE_ROOT, 'chromite', 'api')
Alex Kleinf4dc4f52018-12-05 13:55:12 -070036 output = os.path.join(base_dir, 'gen')
37 source = os.path.join(base_dir, 'proto')
38 targets = os.path.join(source, '*.proto')
39
Alex Kleinc33c1912019-02-15 10:29:13 -070040 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 Klein2900d342019-01-23 14:03:25 -070057
Alex Kleinf4dc4f52018-12-05 13:55:12 -070058 cmd = ('protoc --python_out %(output)s --proto_path %(source)s %(targets)s'
59 % {'output': output, 'source': source, 'targets': targets})
Alex Kleinc33c1912019-02-15 10:29:13 -070060 result = cros_build_lib.RunCommand(cmd, enter_chroot=True, shell=True,
61 error_code_ok=True)
Alex Kleinf4dc4f52018-12-05 13:55:12 -070062 return result.returncode