1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2023 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 18 19from scripts.util.file_utils import write_json_file # noqa: E402 20from . import subsystem_scan # noqa: E402 21from resources.config import Config 22 23 24def _output_subsystem_configs(output_dir, subsystem_configs): 25 build_config_file_name = "subsystem_build_config.json" 26 build_config_file = os.path.join(output_dir, 'subsystem_info', 27 build_config_file_name) 28 write_json_file(build_config_file, subsystem_configs) 29 30 src_output_file_name = "src_subsystem_info.json" 31 no_src_output_file_name = "no_src_subsystem_info.json" 32 33 src_subsystem = {} 34 for key, val in subsystem_configs.get('subsystem').items(): 35 src_subsystem[key] = val.get('path') 36 src_output_file = os.path.join(output_dir, 'subsystem_info', 37 src_output_file_name) 38 write_json_file(src_output_file, src_subsystem) 39 40 no_src_output_file = os.path.join(output_dir, 'subsystem_info', 41 no_src_output_file_name) 42 write_json_file(no_src_output_file, 43 subsystem_configs.get('no_src_subsystem')) 44 45 46def merge_subsystem_overlay(subsystem_configs, subsystem_config_overlay, key): 47 subsystem_info = subsystem_configs[key] 48 overlay_info = subsystem_config_overlay[key] 49 50 for subsystem in overlay_info: 51 if subsystem in subsystem_info: 52 overlay_path = subsystem_config_overlay['subsystem'][subsystem]['path'] 53 for path in overlay_path: 54 if path.find("vendor") != -1 or path.find("device") != -1: 55 subsystem_configs['subsystem'][subsystem]['path'] += \ 56 subsystem_config_overlay['subsystem'][subsystem]['path'] 57 subsystem_configs['subsystem'][subsystem]['build_files'] += \ 58 subsystem_config_overlay['subsystem'][subsystem]['build_files'] 59 else: 60 continue 61 else: 62 subsystem_configs.setdefault(subsystem, subsystem_config_overlay[subsystem]) 63 64def get_subsystem_info(subsystem_config_file, example_subsystem_file, 65 source_root_dir, config_output_path, os_level): 66 if not subsystem_config_file: 67 subsystem_config_file = 'build/subsystem_config.json' 68 69 subsystem_configs = {} 70 output_dir_realpath = os.path.join(source_root_dir, config_output_path) 71 subsystem_configs = subsystem_scan.scan(subsystem_config_file, 72 example_subsystem_file, 73 source_root_dir) 74 config = Config() 75 subsystem_config_overlay_file = os.path.join( 76 config.product_path, "subsystem_config_overlay.json") 77 if os.path.isfile(subsystem_config_overlay_file): 78 subsystem_config_overlay = {} 79 subsystem_config_overlay = subsystem_scan.scan(subsystem_config_overlay_file, 80 example_subsystem_file, 81 source_root_dir) 82 merge_subsystem_overlay(subsystem_configs, subsystem_config_overlay, 'subsystem') 83 merge_subsystem_overlay(subsystem_configs, subsystem_config_overlay, 'no_src_subsystem') 84 85 _output_subsystem_configs(output_dir_realpath, subsystem_configs) 86 return subsystem_configs.get('subsystem') 87