1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#
4# Copyright (c) 2022 Huawei Device Co., Ltd.
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18
19from __future__ import print_function
20import argparse
21import os.path
22import subprocess
23import sys
24
25def main(argv):
26  parser = argparse.ArgumentParser()
27  parser.add_argument("--protoc", required=True,
28                      help="Relative path to compiler.")
29
30  parser.add_argument("--protos-dir", required=True,
31                      help="protos in dir.")
32  parser.add_argument("--cpp-out",
33                      help="Output directory for standard C++ generator.")
34  parser.add_argument("protos", nargs="+",
35                      help="Input protobuf definition file(s).")
36
37  options = parser.parse_args(argv)
38
39  protos_dir = os.path.relpath(options.protos_dir)
40  proto_files = options.protos
41
42  # Generate protoc cmd.
43  protoc_cmd = [os.path.realpath(options.protoc)]
44  if options.cpp_out:
45    cpp_out = options.cpp_out
46    protoc_cmd += ["--cpp_out", cpp_out]
47
48  protoc_cmd += ["--proto_path", protos_dir]
49  protoc_cmd += [os.path.join(protos_dir, name) for name in proto_files]
50
51  ret = subprocess.call(protoc_cmd)
52  if ret != 0:
53    raise RuntimeError("Protoc failed.")
54
55if __name__ == "__main__":
56    main(sys.argv[1:])
57