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 sys 17import argparse 18import os 19 20sys.path.append( 21 os.path.dirname(os.path.dirname(os.path.dirname( 22 os.path.abspath(__file__))))) 23from scripts.util.file_utils import write_json_file # noqa: E402 24 25 26def get_source_name(module_type, name, prefix_override, suffix, 27 alternative_suffix): 28 """Generate source file name by type.""" 29 if (module_type == 'lib' 30 or module_type == 'lib64') and not prefix_override: 31 if not name.startswith('lib'): 32 name = 'lib' + name 33 alias = '' 34 if module_type == 'java_library' and alternative_suffix: 35 alias = '%s%s' % (name, alternative_suffix) 36 if suffix: 37 name = '%s%s' % (name, suffix) 38 if module_type == 'none': 39 name = '' 40 return name, alias 41 42 43def get_type_dir(module_type): 44 """Get the install directory by type.""" 45 return module_type 46 47 48def _gen_install_dest(base_dir, module_install_dir, relative_install_dir, 49 module_type): 50 """Generate module install dir by user config.""" 51 if module_install_dir != '': 52 _install_dir = os.path.join(base_dir, module_install_dir) 53 elif relative_install_dir != '': 54 _install_dir = os.path.join(base_dir, get_type_dir(module_type), 55 relative_install_dir) 56 else: 57 _install_dir = os.path.join(base_dir, get_type_dir(module_type)) 58 return _install_dir 59 60 61def gen_install_dests(system_base_dir, ramdisk_base_dir, vendor_base_dir, 62 updater_base_dir, sys_prod_base_dir, chip_prod_base_dir, source_file_name, install_images, 63 module_install_dir, relative_install_dir, module_type): 64 """Generate module install dir by user config.""" 65 dests = [] 66 if module_type == "none": 67 return dests 68 dest = '' 69 for image in install_images: 70 if image == 'system': 71 dest = _gen_install_dest(system_base_dir, module_install_dir, 72 relative_install_dir, module_type) 73 elif image == 'ramdisk': 74 dest = _gen_install_dest(ramdisk_base_dir, module_install_dir, 75 relative_install_dir, module_type) 76 elif image == 'vendor': 77 dest = _gen_install_dest(vendor_base_dir, module_install_dir, 78 relative_install_dir, module_type) 79 elif image == 'updater': 80 dest = _gen_install_dest(updater_base_dir, module_install_dir, 81 relative_install_dir, module_type) 82 elif image == 'sys_prod': 83 dest = _gen_install_dest(sys_prod_base_dir, module_install_dir, 84 relative_install_dir, module_type) 85 elif image == 'chip_prod': 86 dest = _gen_install_dest(chip_prod_base_dir, module_install_dir, 87 relative_install_dir, module_type) 88 dests.append(os.path.join(dest, source_file_name)) 89 return dests 90 91 92def gen_module_info(module_type, module_label, module_name, source_dir, 93 module_source, module_alt_source, install_dests, 94 symlink_target, install_enable, collect, notice): 95 """Generate module install info.""" 96 source = os.path.join(source_dir, module_source) 97 data = { 98 'type': module_type, 99 'label': module_label, 100 'label_name': module_name, 101 'source': source, 102 'dest': install_dests, 103 'collect': collect, 104 'install_enable': install_enable 105 } 106 if notice: 107 data['notice'] = notice 108 if module_type == 'java_library': 109 data['alternative_source'] = os.path.join(source_dir, 110 module_alt_source) 111 if symlink_target: 112 data['symlink'] = symlink_target 113 return data 114 115 116def main(): 117 parser = argparse.ArgumentParser() 118 parser.add_argument('--system-base-dir', required=True) 119 parser.add_argument('--ramdisk-base-dir', required=True) 120 parser.add_argument('--vendor-base-dir', required=True) 121 parser.add_argument('--updater-base-dir', required=True) 122 parser.add_argument('--sys_prod-base-dir', required=True) 123 parser.add_argument('--chip_prod-base-dir', required=True) 124 parser.add_argument('--label-name', help='module name', required=True) 125 parser.add_argument('--target-label', help='target label', required=True) 126 parser.add_argument('--type', help='module type', required=True) 127 parser.add_argument('--source-dir', help='', required=True) 128 parser.add_argument('--install-images', nargs='+', help='') 129 parser.add_argument('--install-name', help='', required=False) 130 parser.add_argument('--suffix', help='', required=False) 131 parser.add_argument('--alternative-suffix', 132 help='alternative extension for java library targets', 133 required=False) 134 parser.add_argument('--symlink-target', nargs='+', help='', required=False) 135 parser.add_argument('--output-file', help='', required=True) 136 parser.add_argument('--prebuilt', dest='prebuilt', action='store_true') 137 parser.add_argument('--no-prebuilt', dest='prebuilt', action='store_false') 138 parser.set_defaults(prebuilt=False) 139 parser.add_argument('--module-source', help='', required=False) 140 parser.add_argument('--install-enable', 141 dest='install_enable', 142 action='store_false') 143 parser.set_defaults(install_enable=True) 144 parser.add_argument('--collect', dest='collect', action='store_true') 145 parser.add_argument('--notice', help='path to notice') 146 parser.set_defaults(collect=False) 147 parser.add_argument('--module-install-dir', help='', required=False) 148 parser.add_argument('--relative-install-dir', help='', required=False) 149 parser.add_argument('--prefix-override', 150 dest='prefix_override', 151 action='store_true') 152 parser.add_argument('--no-prefix-override', 153 dest='prefix_override', 154 action='store_false') 155 parser.set_defaults(prefix_override=False) 156 args = parser.parse_args() 157 158 module_source = '' 159 module_alt_source = '' 160 source_file_name = '' 161 if args.prebuilt: 162 source_file_name = os.path.basename(args.module_source) 163 module_source = args.module_source 164 if module_source.startswith("//"): 165 module_source = module_source[2:] 166 else: 167 source_file_name, alt_source_file_name = get_source_name( 168 args.type, args.install_name, args.prefix_override, args.suffix, 169 args.alternative_suffix) 170 module_source = source_file_name 171 module_alt_source = alt_source_file_name 172 173 install_dests = [] 174 if args.install_images: 175 install_dests = gen_install_dests( 176 args.system_base_dir, args.ramdisk_base_dir, args.vendor_base_dir, 177 args.updater_base_dir, args.sys_prod_base_dir, args.chip_prod_base_dir, source_file_name, args.install_images, 178 args.module_install_dir, args.relative_install_dir, args.type) 179 180 module_info_data = gen_module_info(args.type, args.target_label, 181 args.label_name, args.source_dir, 182 module_source, module_alt_source, 183 install_dests, args.symlink_target, 184 args.install_enable, args.collect, 185 args.notice) 186 187 # write module info file 188 write_json_file(args.output_file, module_info_data) 189 return 0 190 191 192if __name__ == '__main__': 193 sys.exit(main()) 194