1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2021 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 os 17import sys 18import argparse 19import glob 20 21sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 22from scripts.util.file_utils import read_json_file, write_json_file # noqa: E402 E501 23 24_default_subsystem = {"common": "build/common"} 25 26 27def _read_config(subsystem_config_file, example_subsystem_file): 28 if not os.path.exists(subsystem_config_file): 29 raise Exception( 30 "config file '{}' doesn't exist.".format(subsystem_config_file)) 31 subsystem_config = read_json_file(subsystem_config_file) 32 if subsystem_config is None: 33 raise Exception("read file '{}' failed.".format(subsystem_config_file)) 34 35 # example subsystem 36 if example_subsystem_file: 37 example_subsystem_config = read_json_file(example_subsystem_file) 38 if example_subsystem_config is not None: 39 subsystem_config.update(example_subsystem_config) 40 41 subsystem_info = {} 42 for key, val in subsystem_config.items(): 43 if 'path' not in val: 44 raise Exception("subsystem '{}' not config path.".format(key)) 45 subsystem_info[key] = val.get('path') 46 return subsystem_info 47 48 49def _scan_build_file(subsystem_path): 50 build_config_file_name = "ohos.build" 51 search_str = "{}/**/{}".format(subsystem_path, build_config_file_name) 52 _files = glob.glob(search_str, recursive=True) 53 # campatibility bundle.json and ohos.build 54 bundle_file_name = 'bundle.json' 55 _bundle_files = glob.glob("{}/**/{}".format(subsystem_path, 56 bundle_file_name), 57 recursive=True) 58 if _bundle_files: 59 _files.extend(_bundle_files) 60 return _files 61 62 63def scan(subsystem_config_file, example_subsystem_file, source_root_dir): 64 subsystem_infos = _read_config(subsystem_config_file, 65 example_subsystem_file) 66 # add common subsystem info 67 subsystem_infos.update(_default_subsystem) 68 69 no_src_subsystem = {} 70 _build_configs = {} 71 for key, val in subsystem_infos.items(): 72 _info = {'path': val} 73 _subsystem_path = os.path.join(source_root_dir, val) 74 75 _build_config_files = _scan_build_file(_subsystem_path) 76 77 if _build_config_files: 78 _info['build_files'] = _build_config_files 79 _build_configs[key] = _info 80 else: 81 no_src_subsystem[key] = val 82 83 scan_result = { 84 'source_path': source_root_dir, 85 'subsystem': _build_configs, 86 'no_src_subsystem': no_src_subsystem 87 } 88 return scan_result 89 90 91def main(): 92 parser = argparse.ArgumentParser() 93 parser.add_argument('--subsystem-config-file', required=True) 94 parser.add_argument('--example-subsystem-file', required=False) 95 parser.add_argument('--source-root-dir', required=True) 96 parser.add_argument('--output-dir', required=True) 97 args = parser.parse_args() 98 99 build_configs = scan(args.subsystem_config_file, 100 args.example_subsystem_file, args.source_root_dir) 101 102 build_configs_file = os.path.join(args.output_dir, 103 "subsystem_build_config.json") 104 write_json_file(build_configs_file, build_configs) 105 return 0 106 107 108if __name__ == '__main__': 109 sys.exit(main()) 110