1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4# 5# Copyright (c) 2023 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17 18 19import os 20import sys 21import platform 22 23sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 24from .global_var import CURRENT_OHOS_ROOT 25from .global_var import BUILD_CONFIG_FILE 26from .global_var import ROOT_CONFIG_FILE 27from exceptions.ohos_exception import OHOSException 28from helper.singleton import Singleton 29from util.io_util import IoUtil 30from containers.status import throw_exception 31 32 33def get_config_path(): 34 if not os.path.exists(ROOT_CONFIG_FILE): 35 IoUtil.copy_file(BUILD_CONFIG_FILE, ROOT_CONFIG_FILE) 36 return ROOT_CONFIG_FILE 37 38 39class Config(metaclass=Singleton): 40 def __init__(self): 41 self.config_json = "" 42 self._root_path = "" 43 self._board = "" 44 self._kernel = "" 45 self._product = "" 46 self._product_path = "" 47 self._device_path = "" 48 self._device_company = "" 49 self._patch_cache = "" 50 self._version = "" 51 self._os_level = "" 52 self._product_json = "" 53 self._target_os = "" 54 self._target_cpu = "" 55 self._out_path = "" 56 self._compile_config = "" 57 self._component_type = "" 58 self._device_config_path = "" 59 self._product_config_path = "" 60 self._subsystem_config_json = "" 61 self._subsystem_config_overlay_json = "" 62 self._support_cpu = "" 63 self._log_mode = "" 64 self.fs_attr = set() 65 self.platform = platform.system() 66 self.__post__init() 67 68 def __post__init(self): 69 self.config_json = get_config_path() 70 config_content = IoUtil.read_json_file(self.config_json) 71 self.root_path = CURRENT_OHOS_ROOT 72 self.board = config_content.get('board', None) 73 self.kernel = config_content.get('kernel', None) 74 self.product = config_content.get('product', None) 75 self.product_path = config_content.get('product_path', None) 76 self.device_path = config_content.get('device_path', None) 77 self.device_company = config_content.get('device_company', None) 78 self.patch_cache = config_content.get('patch_cache', None) 79 self.version = config_content.get('version', '3.0') 80 self.os_level = config_content.get('os_level', 'small') 81 self.product_json = config_content.get('product_json', None) 82 self.target_os = config_content.get('target_os', None) 83 self.target_cpu = config_content.get('target_cpu', None) 84 self.out_path = config_content.get('out_path', None) 85 self.compile_config = config_content.get('compile_config', None) 86 self.component_type = config_content.get('component_type', None) 87 self.device_config_path = config_content.get('device_config_path', 88 None) 89 self.product_config_path = config_content.get('product_config_path', 90 None) 91 self.subsystem_config_json = config_content.get( 92 'subsystem_config_json', None) 93 self.support_cpu = config_content.get('support_cpu', None) 94 self.fs_attr = set() 95 self.platform = platform.system() 96 97 @property 98 def component_type(self): 99 return self._component_type 100 101 @component_type.setter 102 def component_type(self, value: str): 103 self._component_type = value 104 self.config_update('component_type', self._component_type) 105 106 @property 107 def target_os(self): 108 return self._target_os 109 110 @target_os.setter 111 def target_os(self, value: str): 112 self._target_os = value 113 self.config_update('target_os', self._target_os) 114 115 @property 116 def target_cpu(self): 117 return self._target_cpu 118 119 @target_cpu.setter 120 def target_cpu(self, value: str): 121 self._target_cpu = value 122 self.config_update('target_cpu', self._target_cpu) 123 124 @property 125 def version(self): 126 return self._version 127 128 @version.setter 129 def version(self, value: str): 130 self._version = value 131 self.config_update('version', self._version) 132 133 @property 134 def compile_config(self): 135 return self._compile_config 136 137 @compile_config.setter 138 def compile_config(self, value: str): 139 self._compile_config = value 140 self.config_update('compile_config', self._compile_config) 141 142 @property 143 def os_level(self): 144 return self._os_level 145 146 @os_level.setter 147 def os_level(self, value: str): 148 self._os_level = value 149 self.config_update('os_level', self._os_level) 150 151 @property 152 def product_json(self): 153 return self._product_json 154 155 @product_json.setter 156 def product_json(self, value: str): 157 self._product_json = value 158 self.config_update('product_json', self._product_json) 159 160 @property 161 @throw_exception 162 def root_path(self): 163 if self._root_path is None: 164 raise OHOSException('Failed to init compile config', '0019') 165 166 return self._root_path 167 168 @root_path.setter 169 def root_path(self, value: str): 170 self._root_path = os.path.abspath(value) 171 self.config_update('root_path', self._root_path) 172 173 @property 174 def board(self): 175 if self._board is None: 176 raise OHOSException('Failed to init compile config', '0019') 177 return self._board 178 179 @board.setter 180 def board(self, value: str): 181 self._board = value 182 self.config_update('board', self._board) 183 184 @property 185 def device_company(self): 186 if self._device_company is None: 187 raise OHOSException('Failed to init compile config', '0019') 188 return self._device_company 189 190 @device_company.setter 191 def device_company(self, value: str): 192 self._device_company = value 193 self.config_update('device_company', self._device_company) 194 195 @property 196 def kernel(self): 197 return self._kernel 198 199 @kernel.setter 200 def kernel(self, value: str): 201 self._kernel = value 202 self.config_update('kernel', self._kernel) 203 204 @property 205 def product(self): 206 if self._product is None: 207 raise OHOSException('Failed to init compile config', '0019') 208 return self._product 209 210 @product.setter 211 def product(self, value: str): 212 self._product = value 213 self.config_update('product', self._product) 214 215 @property 216 def product_path(self): 217 if self._product_path is None: 218 raise OHOSException('Failed to init compile config', '0019') 219 return self._product_path 220 221 @product_path.setter 222 def product_path(self, value: str): 223 self._product_path = value 224 self.config_update('product_path', self._product_path) 225 226 @property 227 def gn_product_path(self): 228 return self.product_path.replace(self.root_path, '/') 229 230 @property 231 def device_path(self): 232 if self._device_path is None: 233 raise OHOSException('Failed to init compile config', '0019') 234 return self._device_path 235 236 @device_path.setter 237 def device_path(self, value: str): 238 self._device_path = value 239 self.config_update('device_path', self._device_path) 240 241 @property 242 def gn_device_path(self): 243 return self.device_path.replace(self.root_path, '/') 244 245 @property 246 def build_path(self): 247 _build_path = os.path.join(self.root_path, 'build', 'lite') 248 if not os.path.isdir(_build_path): 249 raise OHOSException(f'Invalid build path: {_build_path}') 250 return _build_path 251 252 @property 253 def out_path(self): 254 return self._out_path 255 256 @out_path.setter 257 def out_path(self, value: str): 258 self._out_path = value 259 self.config_update('out_path', self._out_path) 260 261 @property 262 def device_config_path(self): 263 return self._device_config_path 264 265 @device_config_path.setter 266 def device_config_path(self, value: str): 267 self._device_config_path = value 268 self.config_update('device_config_path', self._device_config_path) 269 270 @property 271 def product_config_path(self): 272 return self._product_config_path 273 274 @product_config_path.setter 275 def product_config_path(self, value: str): 276 self._product_config_path = value 277 self.config_update('product_config_path', self._product_config_path) 278 279 @property 280 def subsystem_config_json(self): 281 return self._subsystem_config_json 282 283 @subsystem_config_json.setter 284 def subsystem_config_json(self, value: str): 285 self._subsystem_config_json = value 286 self.config_update('subsystem_config_json', 287 self._subsystem_config_json) 288 289 @property 290 def subsystem_config_overlay_json(self): 291 return self._subsystem_config_overlay_json 292 293 @subsystem_config_overlay_json.setter 294 def subsystem_config_overlay_json(self, value: str): 295 self._subsystem_config_overlay_json = value 296 self.config_update('subsystem_config_overlay_json', 297 self._subsystem_config_overlay_json) 298 299 @property 300 def support_cpu(self): 301 return self._support_cpu 302 303 @property 304 def log_mode(self): 305 return self._log_mode 306 307 @log_mode.setter 308 def log_mode(self, value): 309 self._log_mode = value 310 self.config_update('log_mode', self._log_mode) 311 312 @support_cpu.setter 313 def support_cpu(self, value: str): 314 self._support_cpu = value 315 self.config_update('support_cpu', self._support_cpu) 316 317 @property 318 def log_path(self): 319 if self.out_path is not None: 320 return os.path.join(self.out_path, 'build.log') 321 else: 322 raise OHOSException(f'Failed to get log_path') 323 324 @property 325 def vendor_path(self): 326 _vendor_path = os.path.join(self.root_path, 'vendor') 327 if not os.path.isdir(_vendor_path): 328 _vendor_path = '' 329 return _vendor_path 330 331 @property 332 def built_in_product_path(self): 333 _built_in_product_path = os.path.join(self.root_path, 334 'productdefine/common/products') 335 if not os.path.isdir(_built_in_product_path): 336 raise OHOSException( 337 f'Invalid built-in product path: {_built_in_product_path}') 338 return _built_in_product_path 339 340 @property 341 def built_in_product_path_for_llvm(self): 342 _built_in_product_path_for_llvm = os.path.join(self.root_path, 343 'toolchain/llvm-project/llvm_products') 344 return _built_in_product_path_for_llvm 345 346 @property 347 def build_tools_path(self): 348 try: 349 tools_path = IoUtil.read_json_file( 350 'build_tools/build_tools_config.json')[self.platform]['build_tools_path'] 351 return os.path.join(self.root_path, tools_path) 352 except KeyError: 353 raise OHOSException(f'unidentified platform: {self.platform}') 354 355 @property 356 def gn_path(self): 357 repo_gn_path = os.path.join(self.build_tools_path, 'gn') 358 # gn exist. 359 if os.path.isfile(repo_gn_path): 360 return repo_gn_path 361 else: 362 raise OHOSException('There is no clang executable file at {}, \ 363 please execute build/prebuilts_download.sh'.format(repo_gn_path)) 364 365 @property 366 def ninja_path(self): 367 repo_ninja_path = os.path.join(self.build_tools_path, 'ninja') 368 # ninja exist. 369 if os.path.isfile(repo_ninja_path): 370 return repo_ninja_path 371 else: 372 raise OHOSException('There is no clang executable file at {}, \ 373 please execute build/prebuilts_download.sh'.format(repo_ninja_path)) 374 375 @property 376 def clang_path(self): 377 repo_clang_path = os.path.join('prebuilts', 'clang', 'ohos', 378 'linux-x86_64', 'llvm') 379 if not os.path.exists(repo_clang_path): 380 repo_clang_path = os.path.join('out', 'llvm-install') 381 # clang exist 382 if os.path.isdir(repo_clang_path): 383 return f'//{repo_clang_path}' 384 # clang installed manually or auto download 385 else: 386 raise OHOSException('There is no clang executable file at {}, \ 387 please execute build/prebuilts_download.sh'.format(repo_clang_path)) 388 389 @property 390 def patch_cache(self): 391 return self._patch_cache 392 393 @patch_cache.setter 394 def patch_cache(self, value: str): 395 self._patch_cache = value 396 self.config_update('patch_cache', self._patch_cache) 397 398 def config_update(self, key: str, value: str): 399 config_content = IoUtil.read_json_file(self.config_json) 400 config_content[key] = value 401 IoUtil.dump_json_file(self.config_json, config_content) 402