15f9996aaSopenharmony_ci#!/usr/bin/env python
25f9996aaSopenharmony_ci# -*- coding: utf-8 -*-
35f9996aaSopenharmony_ci# Copyright (c) 2021 Huawei Device Co., Ltd.
45f9996aaSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
55f9996aaSopenharmony_ci# you may not use this file except in compliance with the License.
65f9996aaSopenharmony_ci# You may obtain a copy of the License at
75f9996aaSopenharmony_ci#
85f9996aaSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
95f9996aaSopenharmony_ci#
105f9996aaSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
115f9996aaSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
125f9996aaSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135f9996aaSopenharmony_ci# See the License for the specific language governing permissions and
145f9996aaSopenharmony_ci# limitations under the License.
155f9996aaSopenharmony_ci
165f9996aaSopenharmony_ciimport sys
175f9996aaSopenharmony_ciimport argparse
185f9996aaSopenharmony_ciimport os
195f9996aaSopenharmony_ci
205f9996aaSopenharmony_cisys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
215f9996aaSopenharmony_cifrom scripts.util.file_utils import read_json_file, write_json_file  # noqa: E402 E501
225f9996aaSopenharmony_ci
235f9996aaSopenharmony_ci
245f9996aaSopenharmony_ci# read subsystem module, generate subsystem install list and deps list.
255f9996aaSopenharmony_cidef gen_output_file(part_name: str, origin_part_name: str, all_modules_file: str,
265f9996aaSopenharmony_ci                    sdk_modules_info_file: str, install_modules_file: str,
275f9996aaSopenharmony_ci                    dep_modules_file: str, output_host_file: str,
285f9996aaSopenharmony_ci                    current_toolchain: str, host_toolchain: str):
295f9996aaSopenharmony_ci    # read subsystem module info
305f9996aaSopenharmony_ci    all_module_info = read_json_file(all_modules_file)
315f9996aaSopenharmony_ci    if all_module_info is None:
325f9996aaSopenharmony_ci        raise Exception(
335f9996aaSopenharmony_ci            "read part '{}' modules info failed.".format(part_name))
345f9996aaSopenharmony_ci
355f9996aaSopenharmony_ci    # merge sdk install modules, may be repeat
365f9996aaSopenharmony_ci    if os.path.exists(sdk_modules_info_file):
375f9996aaSopenharmony_ci        sdk_modules_info = read_json_file(sdk_modules_info_file)
385f9996aaSopenharmony_ci        if sdk_modules_info is not None:
395f9996aaSopenharmony_ci            all_module_info.extend(sdk_modules_info)
405f9996aaSopenharmony_ci
415f9996aaSopenharmony_ci    # thirdparty subsystem part module list
425f9996aaSopenharmony_ci    thirdparty_module_list = []
435f9996aaSopenharmony_ci
445f9996aaSopenharmony_ci    # driver/interface part moudle list
455f9996aaSopenharmony_ci    hdi_module_list = []
465f9996aaSopenharmony_ci
475f9996aaSopenharmony_ci    # driver/interface and thirdparty subsystem part module list
485f9996aaSopenharmony_ci    external_module_list = []
495f9996aaSopenharmony_ci
505f9996aaSopenharmony_ci    # Generate a list of modules by part
515f9996aaSopenharmony_ci    modules_info_dict = {}
525f9996aaSopenharmony_ci    modules_def = {}  # remove duplicates
535f9996aaSopenharmony_ci    for info in all_module_info:
545f9996aaSopenharmony_ci        module_def = info.get('module_def')
555f9996aaSopenharmony_ci        if module_def in modules_def:
565f9996aaSopenharmony_ci            continue
575f9996aaSopenharmony_ci
585f9996aaSopenharmony_ci        modules_def[module_def] = ''
595f9996aaSopenharmony_ci        thirdparty = info.get('subsystem_name')
605f9996aaSopenharmony_ci        if thirdparty == 'thirdparty':
615f9996aaSopenharmony_ci            thirdparty_module_list.append(info)
625f9996aaSopenharmony_ci            external_module_list.append(info)
635f9996aaSopenharmony_ci        if str(module_def).startswith("//drivers/interface"):
645f9996aaSopenharmony_ci            hdi_module_list.append(info)
655f9996aaSopenharmony_ci            external_module_list.append(info)
665f9996aaSopenharmony_ci        _module_part_name = info.get('part_name')
675f9996aaSopenharmony_ci        if _module_part_name not in modules_info_dict:
685f9996aaSopenharmony_ci            modules_info_dict[_module_part_name] = []
695f9996aaSopenharmony_ci        modules_info_dict[_module_part_name] += [info]
705f9996aaSopenharmony_ci
715f9996aaSopenharmony_ci    # Current part module list
725f9996aaSopenharmony_ci    part_module_list = []
735f9996aaSopenharmony_ci    if origin_part_name in modules_info_dict:
745f9996aaSopenharmony_ci        part_module_list = modules_info_dict.pop(origin_part_name)
755f9996aaSopenharmony_ci
765f9996aaSopenharmony_ci    # Current part dependent module list
775f9996aaSopenharmony_ci    part_install_modules = []
785f9996aaSopenharmony_ci    part_no_install_modules = []
795f9996aaSopenharmony_ci    part_host_modules = []
805f9996aaSopenharmony_ci    for install_module in part_module_list:
815f9996aaSopenharmony_ci        toolchain = install_module.get('toolchain')
825f9996aaSopenharmony_ci        if toolchain == '' or toolchain == current_toolchain:
835f9996aaSopenharmony_ci            part_install_modules.append(install_module)
845f9996aaSopenharmony_ci        else:
855f9996aaSopenharmony_ci            if toolchain == host_toolchain:
865f9996aaSopenharmony_ci                part_host_modules.append(install_module)
875f9996aaSopenharmony_ci            part_no_install_modules.append(install_module)
885f9996aaSopenharmony_ci
895f9996aaSopenharmony_ci    for install_module in external_module_list:
905f9996aaSopenharmony_ci        toolchain = install_module.get('toolchain')
915f9996aaSopenharmony_ci        if toolchain == host_toolchain:
925f9996aaSopenharmony_ci            part_host_modules.append(install_module)
935f9996aaSopenharmony_ci
945f9996aaSopenharmony_ci    # Depended thirdparty modules and driver/interface modules
955f9996aaSopenharmony_ci    # are installed by default
965f9996aaSopenharmony_ci    # rather than configured in product config.
975f9996aaSopenharmony_ci    part_install_modules.extend(thirdparty_module_list)
985f9996aaSopenharmony_ci    part_install_modules.extend(hdi_module_list)
995f9996aaSopenharmony_ci    # write install modules file
1005f9996aaSopenharmony_ci    write_json_file(install_modules_file, part_install_modules)
1015f9996aaSopenharmony_ci
1025f9996aaSopenharmony_ci    # add no install modules to dict, example: host target
1035f9996aaSopenharmony_ci    modules_info_dict[part_name] = part_no_install_modules
1045f9996aaSopenharmony_ci    # write dep modules file
1055f9996aaSopenharmony_ci    write_json_file(dep_modules_file, modules_info_dict)
1065f9996aaSopenharmony_ci    write_json_file(output_host_file, part_host_modules)
1075f9996aaSopenharmony_ci
1085f9996aaSopenharmony_ci
1095f9996aaSopenharmony_cidef main():
1105f9996aaSopenharmony_ci    parser = argparse.ArgumentParser()
1115f9996aaSopenharmony_ci    parser.add_argument('--part-name', required=True)
1125f9996aaSopenharmony_ci    parser.add_argument('--origin-part-name', required=True)
1135f9996aaSopenharmony_ci    parser.add_argument('--input-file', help='', required=True)
1145f9996aaSopenharmony_ci    parser.add_argument('--sdk-modules-info-file', help='', required=True)
1155f9996aaSopenharmony_ci    parser.add_argument('--output-install-file', help='', required=True)
1165f9996aaSopenharmony_ci    parser.add_argument('--output-deps-file', help='', required=True)
1175f9996aaSopenharmony_ci    parser.add_argument('--output-host-file', help='', required=True)
1185f9996aaSopenharmony_ci    parser.add_argument('--current-toolchain', help='', required=True)
1195f9996aaSopenharmony_ci    parser.add_argument('--host-toolchain', help='', required=True)
1205f9996aaSopenharmony_ci    parser.add_argument('--depfile', required=False)
1215f9996aaSopenharmony_ci    args = parser.parse_args()
1225f9996aaSopenharmony_ci    gen_output_file(args.part_name, args.origin_part_name, args.input_file,
1235f9996aaSopenharmony_ci                    args.sdk_modules_info_file, args.output_install_file,
1245f9996aaSopenharmony_ci                    args.output_deps_file, args.output_host_file,
1255f9996aaSopenharmony_ci                    args.current_toolchain, args.host_toolchain)
1265f9996aaSopenharmony_ci    return 0
1275f9996aaSopenharmony_ci
1285f9996aaSopenharmony_ci
1295f9996aaSopenharmony_ciif __name__ == '__main__':
1305f9996aaSopenharmony_ci    sys.exit(main())
131