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(
215f9996aaSopenharmony_ci    os.path.dirname(os.path.dirname(os.path.dirname(
225f9996aaSopenharmony_ci        os.path.abspath(__file__)))))
235f9996aaSopenharmony_cifrom scripts.util.file_utils import write_json_file  # noqa: E402
245f9996aaSopenharmony_ci
255f9996aaSopenharmony_ci
265f9996aaSopenharmony_cidef get_source_name(module_type, name, prefix_override, suffix,
275f9996aaSopenharmony_ci                    alternative_suffix):
285f9996aaSopenharmony_ci    """Generate source file name by type."""
295f9996aaSopenharmony_ci    if (module_type == 'lib'
305f9996aaSopenharmony_ci            or module_type == 'lib64') and not prefix_override:
315f9996aaSopenharmony_ci        if not name.startswith('lib'):
325f9996aaSopenharmony_ci            name = 'lib' + name
335f9996aaSopenharmony_ci    alias = ''
345f9996aaSopenharmony_ci    if module_type == 'java_library' and alternative_suffix:
355f9996aaSopenharmony_ci        alias = '%s%s' % (name, alternative_suffix)
365f9996aaSopenharmony_ci    if suffix:
375f9996aaSopenharmony_ci        name = '%s%s' % (name, suffix)
385f9996aaSopenharmony_ci    if module_type == 'none':
395f9996aaSopenharmony_ci        name = ''
405f9996aaSopenharmony_ci    return name, alias
415f9996aaSopenharmony_ci
425f9996aaSopenharmony_ci
435f9996aaSopenharmony_cidef get_type_dir(module_type):
445f9996aaSopenharmony_ci    """Get the install directory by type."""
455f9996aaSopenharmony_ci    return module_type
465f9996aaSopenharmony_ci
475f9996aaSopenharmony_ci
485f9996aaSopenharmony_cidef _gen_install_dest(base_dir, module_install_dir, relative_install_dir,
495f9996aaSopenharmony_ci                      module_type):
505f9996aaSopenharmony_ci    """Generate module install dir by user config."""
515f9996aaSopenharmony_ci    if module_install_dir != '':
525f9996aaSopenharmony_ci        _install_dir = os.path.join(base_dir, module_install_dir)
535f9996aaSopenharmony_ci    elif relative_install_dir != '':
545f9996aaSopenharmony_ci        _install_dir = os.path.join(base_dir, get_type_dir(module_type),
555f9996aaSopenharmony_ci                                    relative_install_dir)
565f9996aaSopenharmony_ci    else:
575f9996aaSopenharmony_ci        _install_dir = os.path.join(base_dir, get_type_dir(module_type))
585f9996aaSopenharmony_ci    return _install_dir
595f9996aaSopenharmony_ci
605f9996aaSopenharmony_ci
615f9996aaSopenharmony_cidef gen_install_dests(system_base_dir, ramdisk_base_dir, vendor_base_dir, updater_base_dir,
625f9996aaSopenharmony_ci                      updater_vendor_base_dir, sys_prod_base_dir, chip_prod_base_dir,
635f9996aaSopenharmony_ci                      eng_system_base_dir, eng_chipset_base_dir, source_file_name, install_images,
645f9996aaSopenharmony_ci                      module_install_dir, relative_install_dir, module_type):
655f9996aaSopenharmony_ci    """Generate module install dir by user config."""
665f9996aaSopenharmony_ci    dests = []
675f9996aaSopenharmony_ci    if module_type == "none":
685f9996aaSopenharmony_ci        return dests
695f9996aaSopenharmony_ci    dest = ''
705f9996aaSopenharmony_ci    for image in install_images:
715f9996aaSopenharmony_ci        if image == 'system':
725f9996aaSopenharmony_ci            dest = _gen_install_dest(system_base_dir, module_install_dir,
735f9996aaSopenharmony_ci                                     relative_install_dir, module_type)
745f9996aaSopenharmony_ci        elif image == 'ramdisk':
755f9996aaSopenharmony_ci            dest = _gen_install_dest(ramdisk_base_dir, module_install_dir,
765f9996aaSopenharmony_ci                                     relative_install_dir, module_type)
775f9996aaSopenharmony_ci        elif image == 'vendor':
785f9996aaSopenharmony_ci            dest = _gen_install_dest(vendor_base_dir, module_install_dir,
795f9996aaSopenharmony_ci                                     relative_install_dir, module_type)
805f9996aaSopenharmony_ci        elif image == 'updater':
815f9996aaSopenharmony_ci            dest = _gen_install_dest(updater_base_dir, module_install_dir,
825f9996aaSopenharmony_ci                                     relative_install_dir, module_type)
835f9996aaSopenharmony_ci        elif image == 'updater_vendor':
845f9996aaSopenharmony_ci            dest = _gen_install_dest(updater_vendor_base_dir, module_install_dir,
855f9996aaSopenharmony_ci                                     relative_install_dir, module_type)
865f9996aaSopenharmony_ci        elif image == 'sys_prod':
875f9996aaSopenharmony_ci            dest = _gen_install_dest(sys_prod_base_dir, module_install_dir,
885f9996aaSopenharmony_ci                                     relative_install_dir, module_type)
895f9996aaSopenharmony_ci        elif image == 'chip_prod':
905f9996aaSopenharmony_ci            dest = _gen_install_dest(chip_prod_base_dir, module_install_dir,
915f9996aaSopenharmony_ci                                     relative_install_dir, module_type)
925f9996aaSopenharmony_ci        elif image == 'eng_system':
935f9996aaSopenharmony_ci            dest = _gen_install_dest(eng_system_base_dir, module_install_dir,
945f9996aaSopenharmony_ci                                     relative_install_dir, module_type)
955f9996aaSopenharmony_ci        elif image == 'eng_chipset':
965f9996aaSopenharmony_ci            dest = _gen_install_dest(eng_chipset_base_dir, module_install_dir,
975f9996aaSopenharmony_ci                                     relative_install_dir, module_type)
985f9996aaSopenharmony_ci        dests.append(os.path.join(dest, source_file_name))
995f9996aaSopenharmony_ci    return dests
1005f9996aaSopenharmony_ci
1015f9996aaSopenharmony_ci
1025f9996aaSopenharmony_cidef gen_module_info(module_type, module_label, module_name, source_dir,
1035f9996aaSopenharmony_ci                    module_source, module_alt_source, install_dests,
1045f9996aaSopenharmony_ci                    symlink_target, install_enable, collect, notice, args):
1055f9996aaSopenharmony_ci    """Generate module install info."""
1065f9996aaSopenharmony_ci    source = os.path.join(source_dir, module_source)
1075f9996aaSopenharmony_ci    data = {
1085f9996aaSopenharmony_ci        'type': module_type,
1095f9996aaSopenharmony_ci        'label': module_label,
1105f9996aaSopenharmony_ci        'label_name': module_name,
1115f9996aaSopenharmony_ci        'source': source,
1125f9996aaSopenharmony_ci        'dest': install_dests,
1135f9996aaSopenharmony_ci        'collect': collect,
1145f9996aaSopenharmony_ci        'install_enable': install_enable
1155f9996aaSopenharmony_ci    }
1165f9996aaSopenharmony_ci    if notice:
1175f9996aaSopenharmony_ci        data['notice'] = notice
1185f9996aaSopenharmony_ci    if args.part_name:
1195f9996aaSopenharmony_ci        data['part_name'] = args.part_name
1205f9996aaSopenharmony_ci    if args.subsystem_name:
1215f9996aaSopenharmony_ci        data['subsystem_name'] = args.subsystem_name
1225f9996aaSopenharmony_ci    if args.shlib_type:
1235f9996aaSopenharmony_ci        data['shlib_type'] = args.shlib_type
1245f9996aaSopenharmony_ci    if args.innerapi_tags:
1255f9996aaSopenharmony_ci        data['innerapi_tags'] = args.innerapi_tags
1265f9996aaSopenharmony_ci    if args.version_script:
1275f9996aaSopenharmony_ci        data['version_script'] = args.version_script
1285f9996aaSopenharmony_ci    if module_type == 'java_library':
1295f9996aaSopenharmony_ci        data['alternative_source'] = os.path.join(source_dir,
1305f9996aaSopenharmony_ci                                                  module_alt_source)
1315f9996aaSopenharmony_ci    if args.symlink_ext:
1325f9996aaSopenharmony_ci        data['symlink_ext'] = args.symlink_ext
1335f9996aaSopenharmony_ci    if args.symlink_path:
1345f9996aaSopenharmony_ci        data['symlink_path'] = args.symlink_path
1355f9996aaSopenharmony_ci    if symlink_target:
1365f9996aaSopenharmony_ci        data['symlink'] = symlink_target
1375f9996aaSopenharmony_ci    return data
1385f9996aaSopenharmony_ci
1395f9996aaSopenharmony_ci
1405f9996aaSopenharmony_cidef main():
1415f9996aaSopenharmony_ci    parser = argparse.ArgumentParser()
1425f9996aaSopenharmony_ci    parser.add_argument('--system-base-dir', required=True)
1435f9996aaSopenharmony_ci    parser.add_argument('--ramdisk-base-dir', required=True)
1445f9996aaSopenharmony_ci    parser.add_argument('--vendor-base-dir', required=True)
1455f9996aaSopenharmony_ci    parser.add_argument('--updater-base-dir', required=True)
1465f9996aaSopenharmony_ci    parser.add_argument('--updater_vendor-base-dir', required=True)
1475f9996aaSopenharmony_ci    parser.add_argument('--eng_system-base-dir', required=True)
1485f9996aaSopenharmony_ci    parser.add_argument('--eng_chipset-base-dir', required=True)
1495f9996aaSopenharmony_ci    parser.add_argument('--sys_prod-base-dir', required=True)
1505f9996aaSopenharmony_ci    parser.add_argument('--chip_prod-base-dir', required=True)
1515f9996aaSopenharmony_ci    parser.add_argument('--label-name', help='module name', required=True)
1525f9996aaSopenharmony_ci    parser.add_argument('--target-label', help='target label', required=True)
1535f9996aaSopenharmony_ci    parser.add_argument('--type', help='module type', required=True)
1545f9996aaSopenharmony_ci    parser.add_argument('--source-dir', help='', required=True)
1555f9996aaSopenharmony_ci    parser.add_argument('--install-images', nargs='+', help='')
1565f9996aaSopenharmony_ci    parser.add_argument('--install-name', help='', required=False, default='')
1575f9996aaSopenharmony_ci    parser.add_argument('--suffix', help='', required=False)
1585f9996aaSopenharmony_ci    parser.add_argument('--alternative-suffix',
1595f9996aaSopenharmony_ci                        help='alternative extension for java library targets',
1605f9996aaSopenharmony_ci                        required=False)
1615f9996aaSopenharmony_ci    parser.add_argument('--symlink-target', nargs='+', help='', required=False)
1625f9996aaSopenharmony_ci    parser.add_argument('--output-file', help='', required=True)
1635f9996aaSopenharmony_ci    parser.add_argument('--prebuilt', dest='prebuilt', action='store_true')
1645f9996aaSopenharmony_ci    parser.add_argument('--no-prebuilt', dest='prebuilt', action='store_false')
1655f9996aaSopenharmony_ci    parser.set_defaults(prebuilt=False)
1665f9996aaSopenharmony_ci    parser.add_argument('--module-source', help='', required=False)
1675f9996aaSopenharmony_ci    parser.add_argument('--install-enable',
1685f9996aaSopenharmony_ci                        dest='install_enable',
1695f9996aaSopenharmony_ci                        action='store_false')
1705f9996aaSopenharmony_ci    parser.set_defaults(install_enable=True)
1715f9996aaSopenharmony_ci    parser.add_argument('--collect', dest='collect', action='store_true')
1725f9996aaSopenharmony_ci    parser.add_argument('--notice', help='path to notice')
1735f9996aaSopenharmony_ci    parser.set_defaults(collect=False)
1745f9996aaSopenharmony_ci    parser.add_argument('--module-install-dir', help='', required=False, default='')
1755f9996aaSopenharmony_ci    parser.add_argument('--relative-install-dir', help='', required=False, default='')
1765f9996aaSopenharmony_ci    parser.add_argument('--part-name', help='', required=False, default='')
1775f9996aaSopenharmony_ci    parser.add_argument('--subsystem-name', help='', required=False, default='')
1785f9996aaSopenharmony_ci    parser.add_argument('--shlib-type', help='', required=False, default='')
1795f9996aaSopenharmony_ci    parser.add_argument('--innerapi-tags', nargs='+', help='', required=False, default='')
1805f9996aaSopenharmony_ci    parser.add_argument('--symlink-ext', nargs='+', help='', required=False, default='')
1815f9996aaSopenharmony_ci    parser.add_argument('--symlink-path', help='', required=False, default='')
1825f9996aaSopenharmony_ci    parser.add_argument('--version_script', help='', required=False, default='')
1835f9996aaSopenharmony_ci    parser.add_argument('--prefix-override',
1845f9996aaSopenharmony_ci                        dest='prefix_override',
1855f9996aaSopenharmony_ci                        action='store_true')
1865f9996aaSopenharmony_ci    parser.add_argument('--no-prefix-override',
1875f9996aaSopenharmony_ci                        dest='prefix_override',
1885f9996aaSopenharmony_ci                        action='store_false')
1895f9996aaSopenharmony_ci    parser.set_defaults(prefix_override=False)
1905f9996aaSopenharmony_ci    args = parser.parse_args()
1915f9996aaSopenharmony_ci
1925f9996aaSopenharmony_ci    module_source = ''
1935f9996aaSopenharmony_ci    module_alt_source = ''
1945f9996aaSopenharmony_ci    source_file_name = ''
1955f9996aaSopenharmony_ci    if args.prebuilt:
1965f9996aaSopenharmony_ci        source_file_name = os.path.basename(args.module_source)
1975f9996aaSopenharmony_ci        module_source = args.module_source
1985f9996aaSopenharmony_ci        if module_source.startswith("//"):
1995f9996aaSopenharmony_ci            module_source = module_source[2:]
2005f9996aaSopenharmony_ci    # when the app is compiled by hvigor, that means module_type == "app" and suffix == "".
2015f9996aaSopenharmony_ci    elif args.type == "app" and not args.suffix:
2025f9996aaSopenharmony_ci        source_file_name = ""
2035f9996aaSopenharmony_ci        alt_source_file_name = ""
2045f9996aaSopenharmony_ci        module_source = args.install_name
2055f9996aaSopenharmony_ci        module_alt_source = ""
2065f9996aaSopenharmony_ci    else:
2075f9996aaSopenharmony_ci        source_file_name, alt_source_file_name = get_source_name(
2085f9996aaSopenharmony_ci            args.type, args.install_name, args.prefix_override, args.suffix,
2095f9996aaSopenharmony_ci            args.alternative_suffix)
2105f9996aaSopenharmony_ci        module_source = source_file_name
2115f9996aaSopenharmony_ci        module_alt_source = alt_source_file_name
2125f9996aaSopenharmony_ci
2135f9996aaSopenharmony_ci    install_dests = []
2145f9996aaSopenharmony_ci    if args.install_images:
2155f9996aaSopenharmony_ci        install_dests = gen_install_dests(
2165f9996aaSopenharmony_ci            args.system_base_dir, args.ramdisk_base_dir, args.vendor_base_dir, args.updater_base_dir,
2175f9996aaSopenharmony_ci            args.updater_vendor_base_dir, args.sys_prod_base_dir, args.chip_prod_base_dir,
2185f9996aaSopenharmony_ci            args.eng_system_base_dir, args.eng_chipset_base_dir, source_file_name,
2195f9996aaSopenharmony_ci            args.install_images, args.module_install_dir, args.relative_install_dir, args.type)
2205f9996aaSopenharmony_ci
2215f9996aaSopenharmony_ci    module_info_data = gen_module_info(args.type, args.target_label,
2225f9996aaSopenharmony_ci                                       args.label_name, args.source_dir,
2235f9996aaSopenharmony_ci                                       module_source, module_alt_source,
2245f9996aaSopenharmony_ci                                       install_dests, args.symlink_target,
2255f9996aaSopenharmony_ci                                       args.install_enable, args.collect,
2265f9996aaSopenharmony_ci                                       args.notice, args)
2275f9996aaSopenharmony_ci
2285f9996aaSopenharmony_ci    # write module info file
2295f9996aaSopenharmony_ci    write_json_file(args.output_file, module_info_data)
2305f9996aaSopenharmony_ci    return 0
2315f9996aaSopenharmony_ci
2325f9996aaSopenharmony_ci
2335f9996aaSopenharmony_ciif __name__ == '__main__':
2345f9996aaSopenharmony_ci    sys.exit(main())
235