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, updater_base_dir, 62 updater_vendor_base_dir, sys_prod_base_dir, chip_prod_base_dir, 63 eng_system_base_dir, eng_chipset_base_dir, source_file_name, install_images, 64 module_install_dir, relative_install_dir, module_type): 65 """Generate module install dir by user config.""" 66 dests = [] 67 if module_type == "none": 68 return dests 69 dest = '' 70 for image in install_images: 71 if image == 'system': 72 dest = _gen_install_dest(system_base_dir, module_install_dir, 73 relative_install_dir, module_type) 74 elif image == 'ramdisk': 75 dest = _gen_install_dest(ramdisk_base_dir, module_install_dir, 76 relative_install_dir, module_type) 77 elif image == 'vendor': 78 dest = _gen_install_dest(vendor_base_dir, module_install_dir, 79 relative_install_dir, module_type) 80 elif image == 'updater': 81 dest = _gen_install_dest(updater_base_dir, module_install_dir, 82 relative_install_dir, module_type) 83 elif image == 'updater_vendor': 84 dest = _gen_install_dest(updater_vendor_base_dir, module_install_dir, 85 relative_install_dir, module_type) 86 elif image == 'sys_prod': 87 dest = _gen_install_dest(sys_prod_base_dir, module_install_dir, 88 relative_install_dir, module_type) 89 elif image == 'chip_prod': 90 dest = _gen_install_dest(chip_prod_base_dir, module_install_dir, 91 relative_install_dir, module_type) 92 elif image == 'eng_system': 93 dest = _gen_install_dest(eng_system_base_dir, module_install_dir, 94 relative_install_dir, module_type) 95 elif image == 'eng_chipset': 96 dest = _gen_install_dest(eng_chipset_base_dir, module_install_dir, 97 relative_install_dir, module_type) 98 dests.append(os.path.join(dest, source_file_name)) 99 return dests 100 101 102def gen_module_info(module_type, module_label, module_name, source_dir, 103 module_source, module_alt_source, install_dests, 104 symlink_target, install_enable, collect, notice, args): 105 """Generate module install info.""" 106 source = os.path.join(source_dir, module_source) 107 data = { 108 'type': module_type, 109 'label': module_label, 110 'label_name': module_name, 111 'source': source, 112 'dest': install_dests, 113 'collect': collect, 114 'install_enable': install_enable 115 } 116 if notice: 117 data['notice'] = notice 118 if args.part_name: 119 data['part_name'] = args.part_name 120 if args.subsystem_name: 121 data['subsystem_name'] = args.subsystem_name 122 if args.shlib_type: 123 data['shlib_type'] = args.shlib_type 124 if args.innerapi_tags: 125 data['innerapi_tags'] = args.innerapi_tags 126 if args.version_script: 127 data['version_script'] = args.version_script 128 if module_type == 'java_library': 129 data['alternative_source'] = os.path.join(source_dir, 130 module_alt_source) 131 if symlink_target: 132 data['symlink'] = symlink_target 133 return data 134 135 136def main(): 137 parser = argparse.ArgumentParser() 138 parser.add_argument('--system-base-dir', required=True) 139 parser.add_argument('--ramdisk-base-dir', required=True) 140 parser.add_argument('--vendor-base-dir', required=True) 141 parser.add_argument('--updater-base-dir', required=True) 142 parser.add_argument('--updater_vendor-base-dir', required=True) 143 parser.add_argument('--eng_system-base-dir', required=True) 144 parser.add_argument('--eng_chipset-base-dir', required=True) 145 parser.add_argument('--sys_prod-base-dir', required=True) 146 parser.add_argument('--chip_prod-base-dir', required=True) 147 parser.add_argument('--label-name', help='module name', required=True) 148 parser.add_argument('--target-label', help='target label', required=True) 149 parser.add_argument('--type', help='module type', required=True) 150 parser.add_argument('--source-dir', help='', required=True) 151 parser.add_argument('--install-images', nargs='+', help='') 152 parser.add_argument('--install-name', help='', required=False, default='') 153 parser.add_argument('--suffix', help='', required=False) 154 parser.add_argument('--alternative-suffix', 155 help='alternative extension for java library targets', 156 required=False) 157 parser.add_argument('--symlink-target', nargs='+', help='', required=False) 158 parser.add_argument('--output-file', help='', required=True) 159 parser.add_argument('--prebuilt', dest='prebuilt', action='store_true') 160 parser.add_argument('--no-prebuilt', dest='prebuilt', action='store_false') 161 parser.set_defaults(prebuilt=False) 162 parser.add_argument('--module-source', help='', required=False) 163 parser.add_argument('--install-enable', 164 dest='install_enable', 165 action='store_false') 166 parser.set_defaults(install_enable=True) 167 parser.add_argument('--collect', dest='collect', action='store_true') 168 parser.add_argument('--notice', help='path to notice') 169 parser.set_defaults(collect=False) 170 parser.add_argument('--module-install-dir', help='', required=False, default='') 171 parser.add_argument('--relative-install-dir', help='', required=False, default='') 172 parser.add_argument('--part-name', help='', required=False, default='') 173 parser.add_argument('--subsystem-name', help='', required=False, default='') 174 parser.add_argument('--shlib-type', help='', required=False, default='') 175 parser.add_argument('--innerapi-tags', nargs='+', help='', required=False, default='') 176 parser.add_argument('--version_script', help='', required=False, default='') 177 parser.add_argument('--prefix-override', 178 dest='prefix_override', 179 action='store_true') 180 parser.add_argument('--no-prefix-override', 181 dest='prefix_override', 182 action='store_false') 183 parser.set_defaults(prefix_override=False) 184 args = parser.parse_args() 185 186 module_source = '' 187 module_alt_source = '' 188 source_file_name = '' 189 if args.prebuilt: 190 source_file_name = os.path.basename(args.module_source) 191 module_source = args.module_source 192 if module_source.startswith("//"): 193 module_source = module_source[2:] 194 elif args.type == "app" and not args.suffix: 195 source_file_name = "" 196 alt_source_file_name = "" 197 module_source = args.install_name 198 module_alt_source == "" 199 else: 200 source_file_name, alt_source_file_name = get_source_name( 201 args.type, args.install_name, args.prefix_override, args.suffix, 202 args.alternative_suffix) 203 module_source = source_file_name 204 module_alt_source = alt_source_file_name 205 206 install_dests = [] 207 if args.install_images: 208 install_dests = gen_install_dests( 209 args.system_base_dir, args.ramdisk_base_dir, args.vendor_base_dir, args.updater_base_dir, 210 args.updater_vendor_base_dir, args.sys_prod_base_dir, args.chip_prod_base_dir, 211 args.eng_system_base_dir, args.eng_chipset_base_dir, source_file_name, 212 args.install_images, args.module_install_dir, args.relative_install_dir, args.type) 213 214 module_info_data = gen_module_info(args.type, args.target_label, 215 args.label_name, args.source_dir, 216 module_source, module_alt_source, 217 install_dests, args.symlink_target, 218 args.install_enable, args.collect, 219 args.notice, args) 220 221 # write module info file 222 write_json_file(args.output_file, module_info_data) 223 return 0 224 225 226if __name__ == '__main__': 227 sys.exit(main()) 228