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 22def parse_args(args): 23 args = build_utils.expand_file_args(args) 24 25 parser = optparse.OptionParser() 26 build_utils.add_depfile_option(parser) 27 28 parser.add_option('--idl-path', help='path to idl') 29 parser.add_option('--libcxx-path', help='path to libc++.so') 30 parser.add_option('--output-archive-path', help='path to output archive') 31 parser.add_option( 32 '--genenrated-cpp-files', 33 action='append', 34 help='generated cpp files' 35 ) 36 parser.add_option( 37 '--generated-src-directory', 38 help='directory that stores generated source code' 39 ) 40 parser.add_option( 41 '--gen-type', help='generate type code' 42 ) 43 options, paths = parser.parse_args(args) 44 return options, paths 45 46def idl_compile(options, paths, cmd): 47 my_env = None 48 ld_library = os.path.dirname(options.libcxx_path) 49 if 'LD_LIBRARY_PATH' in os.environ: 50 ld_library = '{}.{}'.format( 51 ld_library, 52 os.environ.get('LD_LIBRARY_PATH').strip(':') 53 ) 54 my_env = {'LD_LIBRARY_PATH': ld_library} 55 56 with build_utils.temp_dir() as tmp_dir: 57 for f in paths: 58 cmd.extend(['-c', f, '-d', tmp_dir]) 59 build_utils.check_output(cmd, env=my_env) 60 build_utils.zip_dir(options.output_archive_path, tmp_dir) 61 os.makedirs(options.generated_src_directory, exist_ok=True) 62 build_utils.extract_all(options.output_archive_path, 63 options.generated_src_directory) 64 65def main(args): 66 args = build_utils.expand_file_args(args) 67 options, paths = parse_args(args) 68 cmd = [options.idl_path] 69 if options.gen_type == "cpp": 70 cmd.extend(['-gen-cpp']) 71 elif options.gen_type == "ts": 72 cmd.extend(['-gen-ts']) 73 74 outputs = [options.output_archive_path] 75 76 build_utils.call_and_write_depfile_if_stale( 77 lambda: idl_compile(options, paths, cmd), 78 options, 79 depfile_deps=([options.idl_path]), 80 input_paths=(paths + [options.idl_path]), 81 output_paths=(outputs), 82 input_strings=cmd, 83 force=False, 84 add_pydeps=False 85 ) 86 87if __name__ == '__main__': 88 sys.exit(main(sys.argv[1:])) 89