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 16 17import os 18 19from resources.config import Config 20from util.log_util import LogUtil 21from util.io_util import IoUtil 22from util.preloader.parse_vendor_product_config import get_vendor_parts_list 23 24 25class Outputs: 26 27 def __init__(self, output_dir): 28 self.__post_init__(output_dir) 29 30 def __post_init__(self, output_dir): 31 os.makedirs(output_dir, exist_ok=True) 32 self.build_prop = os.path.join(output_dir, 'build.prop') 33 self.build_config_json = os.path.join(output_dir, 'build_config.json') 34 self.parts_json = os.path.join(output_dir, 'parts.json') 35 self.parts_config_json = os.path.join(output_dir, 'parts_config.json') 36 self.build_gnargs_prop = os.path.join(output_dir, 'build_gnargs.prop') 37 self.features_json = os.path.join(output_dir, 'features.json') 38 self.syscap_json = os.path.join(output_dir, 'syscap.json') 39 self.exclusion_modules_json = os.path.join(output_dir, 40 'exclusion_modules.json') 41 self.subsystem_config_json = os.path.join(output_dir, 42 'subsystem_config.json') 43 self.subsystem_config_overlay_json = os.path.join(output_dir, 44 'subsystem_config_overlay.json') 45 self.platforms_build = os.path.join(output_dir, 'platforms.build') 46 self.systemcapability_json = os.path.join( 47 output_dir, 'SystemCapability.json') 48 self.compile_standard_whitelist_json = os.path.join(output_dir, 'compile_standard_whitelist.json') 49 self.compile_env_allowlist_json = os.path.join(output_dir, 'compile_env_allowlist.json') 50 51 52class Dirs: 53 54 def __init__(self, config): 55 self.__post_init__(config) 56 57 def __post_init__(self, config): 58 self.source_root_dir = config.root_path 59 self.built_in_product_dir = config.built_in_product_path 60 self.productdefine_dir = os.path.join( 61 self.source_root_dir, 'productdefine/common') 62 self.built_in_base_dir = os.path.join(self.productdefine_dir, 'base') 63 64 # Configs of vendor specified products are stored in ${vendor_dir} directory. 65 self.vendor_dir = config.vendor_path 66 # Configs of device specified products are stored in ${device_dir} directory. 67 self.device_dir = os.path.join(config.root_path, 'device') 68 69 self.subsystem_config_json = os.path.join( 70 config.root_path, config.subsystem_config_json) 71 self.subsystem_config_overlay_json = os.path.join(config.product_path, 72 'subsystem_config_overlay.json') 73 self.lite_components_dir = os.path.join( 74 config.root_path, 'build/lite/components') 75 76 self.preloader_output_dir = os.path.join( 77 config.root_path, 'out/preloader', config.product) 78 79 80class Product(): 81 82 def __init__(self, config_dirs: Dirs, ohos_config: Config): 83 self._ohos_config = None 84 self._dirs = None 85 self._name = "" 86 self._config = {} 87 self._build_vars = {} 88 self._parts = {} 89 self._syscap_info = {} 90 self._device_name = "" 91 self._device_info = {} 92 self._config_file = "" 93 self._version = '' 94 self.__post_init__(config_dirs, ohos_config) 95 96 def __post_init__(self, config_dirs: Dirs, config: Config): 97 self._ohos_config = config 98 self._dirs = config_dirs 99 self._name = config.product 100 self._config_file = config.product_json 101 self._config = self._get_full_product_config() 102 self._version = self._config.get('version', '3.0') 103 self._do_parse() 104 105 # parse product configuration, then generate parts list and build vars 106 def _do_parse(self): 107 self._update_syscap_info() 108 self._update_device() 109 self._update_parts() 110 self._update_build_vars() 111 self._remove_excluded_components() 112 113# update and remove 114 115 # Update the syscap info 116 def _update_syscap_info(self): 117 product_name = self._config.get('product_name') 118 if product_name is None: 119 product_name = "" 120 os_level = self._config.get('type') 121 if os_level is None: 122 os_level = "" 123 api_version = self._config.get('api_version') 124 if api_version is None: 125 api_version = 0 126 manufacturer_id = self._config.get('manufacturer_id') 127 if manufacturer_id is None: 128 manufacturer_id = 0 129 self._syscap_info = {'product': product_name, 'api_version': api_version, 130 'system_type': os_level, 'manufacturer_id': manufacturer_id} 131 132 # Update the _device_name and _device_info based on the product configuration in the vendor warehouse 133 def _update_device(self): 134 if self._version == "2.0": 135 device_name = self._config.get('product_device') 136 if device_name: 137 self._device_name = device_name 138 self._device_info = self._get_device_info_v2( 139 device_name, self._dirs.built_in_device_dir) 140 else: 141 device_name = self._config.get('board') 142 if device_name: 143 self._device_name = device_name 144 self._device_info = self._get_device_info_v3(self._config) 145 if self._ohos_config.target_cpu: 146 self._device_info["target_cpu"] = self._ohos_config.target_cpu 147 if self._ohos_config.target_os: 148 self._device_info["target_os"] = self._ohos_config.target_os 149 if self._ohos_config.compile_config: 150 self._device_info[self._ohos_config["compile_config"]] = True 151 152 # Update the _parts based on the product configuration in the vendor warehouse 153 def _update_parts(self): 154 if self._version == "1.0": 155 _parts = {} 156 self._parts = _parts 157 else: 158 # 1. inherit parts information from base config 159 if self._version == "2.0": 160 os_level = self._config.get("type", "standard") 161 else: 162 os_level = self._config.get("type", "mini") 163 # 2. product config based on default minimum system 164 based_on_mininum_system = self._config.get( 165 'based_on_mininum_system') 166 if based_on_mininum_system == "true": 167 self._parts = self._get_base_parts( 168 self._dirs.built_in_base_dir, os_level) 169 # 3. inherit parts information from inherit config 170 inherit = self._config.get('inherit') 171 if inherit: 172 self._parts.update( 173 self._get_inherit_parts(inherit, self._dirs.source_root_dir)) 174 175 # 4. chipset products relate system parts config 176 sys_info_path = self._config.get('system_component') 177 if sys_info_path: 178 sys_parts = self._get_sys_relate_parts( 179 sys_info_path, self._parts, self._dirs.source_root_dir) 180 self._parts.update(sys_parts) 181 all_parts = {} 182 if self._version == "2.0": 183 current_product_parts = self._config.get("parts") 184 if current_product_parts: 185 all_parts.update(current_product_parts) 186 else: 187 all_parts.update(get_vendor_parts_list(self._config)) 188 all_parts.update(self._get_product_specific_parts()) 189 190 device_name = self._config.get('board') 191 if device_name: 192 all_parts.update(self._get_device_specific_parts()) 193 self._parts.update(all_parts) 194 195 # Update the _build_vars based on the product configuration in the vendor warehouse 196 def _update_build_vars(self): 197 config = self._config 198 build_vars = {} 199 if self._version == "1.0": 200 build_vars = {"os_level": 'large'} 201 else: 202 if self._version == "2.0": 203 build_vars['os_level'] = config.get("type", "standard") 204 device_name = config.get('product_device') 205 if device_name: 206 build_vars['device_name'] = device_name 207 else: 208 build_vars['device_name'] = '' 209 build_vars['product_company'] = config.get('product_company') 210 else: 211 build_vars['os_level'] = config.get('type', 'mini') 212 build_vars['device_name'] = config.get('board') 213 if config.get('product_company'): 214 build_vars['product_company'] = config.get( 215 'product_company') 216 elif os.path.dirname(self._config_file) != self._dirs.built_in_product_dir: 217 relpath = os.path.relpath( 218 self._config_file, self._dirs.vendor_dir) 219 build_vars['product_company'] = relpath.split('/')[0] 220 else: 221 build_vars['product_company'] = config.get( 222 'device_company') 223 build_vars['product_name'] = config.get('product_name') 224 if 'ext_root_proc_conf_path' in config: 225 ext_root_proc_conf_path = os.path.join( 226 self._dirs.source_root_dir, config.get('ext_root_proc_conf_path')) 227 if os.path.exists(ext_root_proc_conf_path): 228 build_vars['ext_root_proc_conf_path'] = ext_root_proc_conf_path 229 if 'ext_critical_proc_conf_path' in config: 230 ext_critical_proc_conf_path = os.path.join( 231 self._dirs.source_root_dir, config.get('ext_critical_proc_conf_path')) 232 if os.path.exists(ext_critical_proc_conf_path): 233 build_vars['ext_critical_proc_conf_path'] = ext_critical_proc_conf_path 234 if 'ext_sanitizer_check_list_path' in config: 235 ext_sanitizer_check_list_path = os.path.join( 236 self._dirs.source_root_dir, config.get('ext_sanitizer_check_list_path')) 237 if os.path.exists(ext_sanitizer_check_list_path): 238 build_vars['ext_sanitizer_check_list_path'] = ext_sanitizer_check_list_path 239 _global_ext_var_file = os.path.join( 240 self._dirs.source_root_dir, "out/products_ext", "global_ext_var_file.gni") 241 if os.path.exists(_global_ext_var_file): 242 build_vars['global_ext_var_file'] = _global_ext_var_file 243 if 'enable_ramdisk' in config: 244 build_vars['enable_ramdisk'] = config.get('enable_ramdisk') 245 if 'enable_absystem' in config: 246 build_vars['enable_absystem'] = config.get('enable_absystem') 247 if 'build_selinux' in config: 248 build_vars['build_selinux'] = config.get('build_selinux') 249 if 'build_seccomp' in config: 250 build_vars['build_seccomp'] = config.get('build_seccomp') 251 if 'support_jsapi' in config: 252 build_vars['support_jsapi'] = config.get('support_jsapi') 253 if 'chipprod_config_path' in config: 254 chipprod_config_path = os.path.join( 255 self._dirs.source_root_dir, config.get('chipprod_config_path')) 256 if os.path.exists(chipprod_config_path): 257 build_vars['chipprod_config_path'] = chipprod_config_path 258 if 'ext_sdk_config_file' in config: 259 ext_sdk_config_file = os.path.join( 260 self._dirs.source_root_dir, config.get('ext_sdk_config_file')) 261 if os.path.exists(ext_sdk_config_file): 262 build_vars['ext_sdk_config_file'] = ext_sdk_config_file 263 if 'ext_ndk_config_file' in config: 264 ext_ndk_config_file = os.path.join( 265 self._dirs.source_root_dir, config.get('ext_ndk_config_file')) 266 if os.path.exists(ext_ndk_config_file): 267 build_vars['ext_ndk_config_file'] = ext_ndk_config_file 268 if 'ext_sign_hap_py_path' in config: 269 path = os.path.join( 270 self._dirs.source_root_dir, config.get('ext_sign_hap_py_path')) 271 if os.path.exists(path): 272 build_vars['ext_sign_hap_py_path'] = path 273 274 build_vars.update(self._device_info) 275 if build_vars['os_level'] == 'mini' or build_vars['os_level'] == 'small': 276 toolchain_label = "" 277 else: 278 toolchain_label = '//build/toolchain/{0}:{0}_clang_{1}'.format( 279 self._device_info.get('target_os'), self._device_info.get('target_cpu')) 280 build_vars['product_toolchain_label'] = toolchain_label 281 self._build_vars = build_vars 282 283 # Remove excluded components 284 def _remove_excluded_components(self): 285 items_to_remove = [] 286 for part, val in self._parts.items(): 287 if "exclude" in val and val["exclude"] == "true": 288 items_to_remove.append(part) 289 for item in items_to_remove: 290 del self._parts[item] 291 292# get method 293 294 # Generate build_info needed for V2 configuration 295 def _get_device_info_v2(self, device_name, config_dir) -> dict: 296 device_config_file = os.path.join(config_dir, 297 '{}.json'.format(device_name)) 298 device_info = IoUtil.read_json_file(device_config_file) 299 if device_info and device_info.get('device_name') != device_name: 300 raise Exception("device name configuration incorrect in '{}'".format( 301 device_config_file)) 302 return device_info 303 304 # Generate build_info needed for V3 configuration 305 def _get_device_info_v3(self, config) -> dict: 306 # NOTE: 307 # Product_name, device_company are necessary for 308 # config.json, DON NOT use .get to replace [] 309 device_info = { 310 'device_name': config['board'], 311 'device_company': config['device_company'] 312 } 313 if config.get('target_os'): 314 device_info['target_os'] = config.get('target_os') 315 else: 316 device_info['target_os'] = 'ohos' 317 if config.get('target_cpu'): 318 device_info['target_cpu'] = config['target_cpu'] 319 else: 320 # Target cpu is used to set default toolchain for standard system. 321 LogUtil.hb_warning( 322 "The target_cpu needs to be specified, default target_cpu=arm") 323 device_info['target_cpu'] = 'arm' 324 if config.get('kernel_version'): 325 device_info['kernel_version'] = config.get('kernel_version') 326 if config.get('device_build_path'): 327 device_info['device_build_path'] = config.get('device_build_path') 328 else: 329 device_build_path = os.path.join(self._dirs.device_dir, 330 config['device_company'], 331 config['board']) 332 if not os.path.exists(device_build_path): 333 device_build_path = os.path.join(self._dirs.device_dir, 334 'board', 335 config['device_company'], 336 config['board']) 337 device_info['device_build_path'] = device_build_path 338 return device_info 339 340 def _get_device_specific_parts(self) -> dict: 341 info = {} 342 if self._device_info and self._device_info.get('device_build_path'): 343 subsystem_name = 'device_{}'.format(self._device_name) 344 part_name = subsystem_name 345 info['{}:{}'.format(subsystem_name, part_name)] = {} 346 return info 347 348 def _get_device_specific_subsystem(self) -> dict: 349 info = {} 350 subsystem_name = 'device_{}'.format(self._device_name) 351 if self._device_info and self._device_info.get('device_build_path'): 352 info[subsystem_name] = { 353 'name': subsystem_name, 354 'path': self._device_info.get('device_build_path') 355 } 356 return info 357 358 def _get_base_parts(self, base_config_dir, os_level) -> dict: 359 system_base_config_file = os.path.join(base_config_dir, 360 '{}_system.json'.format(os_level)) 361 if not os.path.exists(system_base_config_file): 362 raise Exception("product configuration '{}' doesn't exist.".format( 363 system_base_config_file)) 364 return IoUtil.read_json_file(system_base_config_file) 365 366 def _get_inherit_parts(self, inherit, source_root_dir) -> dict: 367 inherit_parts = {} 368 for _config in inherit: 369 _file = os.path.join(source_root_dir, _config) 370 _info = IoUtil.read_json_file(_file) 371 parts = _info.get('parts') 372 if parts: 373 inherit_parts.update(parts) 374 else: 375 inherit_parts.update(get_vendor_parts_list(_info)) 376 return inherit_parts 377 378 def _get_sys_relate_parts(self, system_component_info, _parts, source_root_dir) -> dict: 379 _info = IoUtil.read_json_file(os.path.join( 380 source_root_dir, system_component_info)) 381 ret = {} 382 parts = _info.get('parts') 383 if not parts: 384 parts = get_vendor_parts_list(_info) 385 for part, featrue in parts.items(): 386 if not _parts.get(part): 387 ret[part] = featrue 388 return ret 389 390 def _get_product_specific_parts(self) -> dict: 391 part_name = 'product_{}'.format(self._name) 392 subsystem_name = part_name 393 info = {} 394 info['{}:{}'.format(subsystem_name, part_name)] = {} 395 return info 396 397 def _get_product_specific_subsystem(self) -> dict: 398 info = {} 399 subsystem_name = 'product_{}'.format(self._name) 400 product_build_path = self._config.get('product_build_path') 401 if product_build_path: 402 info[subsystem_name] = { 403 'name': subsystem_name, 404 'path': product_build_path 405 } 406 return info 407 408 def _get_full_product_config(self) -> dict: 409 config = IoUtil.read_json_file(self._config_file) 410 if config.get("version") != '2.0': 411 if os.path.dirname(self._config_file) != self._dirs.built_in_product_dir \ 412 and not hasattr(self._config, 'product_build_path'): 413 config['product_build_path'] = os.path.relpath( 414 os.path.dirname(self._config_file), self._dirs.source_root_dir) 415 return config 416