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    parser.add_argument("--protos-dir", required=True,
30                        help="protos in dir.")
31    parser.add_argument("--cpp-out",
32                        help="Output directory for standard C++ generator.")
33    parser.add_argument("protos", nargs="+",
34                        help="Input protobuf definition file(s).")
35    options = parser.parse_args(argv)
36
37    protos_dir = os.path.relpath(options.protos_dir)
38    proto_files = options.protos
39
40    # Generate protoc cmd.
41    protoc_cmd = [os.path.realpath(options.protoc)]
42    if options.cpp_out:
43        cpp_out = options.cpp_out
44        protoc_cmd += ["--cpp_out", cpp_out]
45
46    protoc_cmd += ["--proto_path", protos_dir]
47    protoc_cmd += [os.path.join(protos_dir, name) for name in proto_files]
48
49    ret = subprocess.call(protoc_cmd)
50    if ret != 0:
51        raise RuntimeError("Protoc failed.")
52
53if __name__ == "__main__":
54    main(sys.argv[1:])
55