blob: e14afdd46ed839bfbba3915c822b533ee46b42e9 [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
13from chromite.lib import cros_build_lib
Alex Klein2900d342019-01-23 14:03:25 -070014from chromite.lib import cros_logging as logging
Alex Kleinf4dc4f52018-12-05 13:55:12 -070015
16
17def GetParser():
18 parser = commandline.ArgumentParser(description=__doc__)
19 return parser
20
21
22def _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
31def 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 Klein2900d342019-01-23 14:03:25 -070039 # 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 Kleinf4dc4f52018-12-05 13:55:12 -070059 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