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.get('subsystem').get(subsystem)['path']
53            for path in overlay_path:
54                if not (path.find("vendor") != -1 or path.find("device") != -1):
55                    continue
56                else:
57                    subsystem_configs.get('subsystem').get(subsystem)['path'] += \
58                        subsystem_config_overlay.get('subsystem').get(subsystem)['path']
59                    subsystem_configs.get('subsystem').get(subsystem)['build_files'] += \
60                        subsystem_config_overlay.get('subsystem').get(subsystem)['build_files']
61        else:
62            subsystem_configs[key].setdefault(subsystem, subsystem_config_overlay[key][subsystem])
63
64
65def get_subsystem_info(subsystem_config_file, example_subsystem_file,
66                       source_root_dir, config_output_path, os_level):
67    if not subsystem_config_file:
68        subsystem_config_file = 'build/subsystem_config.json'
69
70    subsystem_configs = {}
71    output_dir_realpath = os.path.join(source_root_dir, config_output_path)
72    subsystem_configs = subsystem_scan.scan(subsystem_config_file,
73                                            example_subsystem_file,
74                                            source_root_dir)
75    config = Config()
76    subsystem_config_overlay_file = os.path.join(
77        config.product_path, "subsystem_config_overlay.json")
78    if os.path.isfile(subsystem_config_overlay_file):
79        subsystem_config_overlay = {}
80        subsystem_config_overlay = subsystem_scan.scan(subsystem_config_overlay_file,
81                                                       example_subsystem_file,
82                                                       source_root_dir)
83        merge_subsystem_overlay(subsystem_configs, subsystem_config_overlay, 'subsystem')
84        merge_subsystem_overlay(subsystem_configs, subsystem_config_overlay, 'no_src_subsystem')
85
86    _output_subsystem_configs(output_dir_realpath, subsystem_configs)
87    return subsystem_configs.get('subsystem')
88