• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2022 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import optparse
17import os
18import sys
19
20from util import build_utils
21
22
23def parse_args(args):
24    args = build_utils.expand_file_args(args)
25
26    parser = optparse.OptionParser()
27    build_utils.add_depfile_option(parser)
28
29    parser.add_option('--idl-path', help='path to idl')
30    parser.add_option('--libcxx-path', help='path to libc++.so')
31    parser.add_option('--output-archive-path', help='path to output archive')
32    parser.add_option(
33        '--genenrated-cpp-files',
34        action='append',
35        help='generated cpp files'
36    )
37    parser.add_option(
38        '--generated-src-directory',
39        help='directory that stores generated source code'
40    )
41    parser.add_option(
42        '--gen-type', help='generate type code'
43    )
44    options, paths = parser.parse_args(args)
45    return options, paths
46
47
48def idl_compile(options, paths, cmd):
49    my_env = None
50    ld_library = os.path.dirname(options.libcxx_path)
51    if 'LD_LIBRARY_PATH' in os.environ:
52        ld_library = '{}.{}'.format(
53            ld_library,
54            os.environ.get('LD_LIBRARY_PATH').strip(':')
55        )
56    my_env = {'LD_LIBRARY_PATH': ld_library}
57
58    with build_utils.temp_dir() as tmp_dir:
59        for f in paths:
60            cmd.extend(['-c', f, '-d', tmp_dir])
61            build_utils.check_output(cmd, env=my_env)
62        build_utils.zip_dir(options.output_archive_path, tmp_dir)
63        os.makedirs(options.generated_src_directory, exist_ok=True)
64        build_utils.extract_all(options.output_archive_path,
65                                options.generated_src_directory)
66
67
68def main(args):
69    args = build_utils.expand_file_args(args)
70    options, paths = parse_args(args)
71    cmd = [options.idl_path]
72    if options.gen_type == "cpp":
73        cmd.extend(['-gen-cpp'])
74    elif options.gen_type == "ts":
75        cmd.extend(['-gen-ts'])
76    elif options.gen_type == "rust":
77        cmd.extend(['-gen-rust'])
78    else:
79        raise Exception("Unknow gen type: {}".format(options.gen_type))
80
81    outputs = [options.output_archive_path]
82
83    build_utils.call_and_write_depfile_if_stale(
84        lambda: idl_compile(options, paths, cmd),
85        options,
86        depfile_deps=([options.idl_path]),
87        input_paths=(paths + [options.idl_path]),
88        output_paths=(outputs),
89        input_strings=cmd,
90        force=False,
91        add_pydeps=False
92    )
93
94if __name__ == '__main__':
95    sys.exit(main(sys.argv[1:]))
96