• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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        self.precise_branch = ""
97
98    @property
99    def component_type(self):
100        return self._component_type
101
102    @component_type.setter
103    def component_type(self, value: str):
104        self._component_type = value
105        self.config_update('component_type', self._component_type)
106
107    @property
108    def target_os(self):
109        return self._target_os
110
111    @target_os.setter
112    def target_os(self, value: str):
113        self._target_os = value
114        self.config_update('target_os', self._target_os)
115
116    @property
117    def target_cpu(self):
118        return self._target_cpu
119
120    @target_cpu.setter
121    def target_cpu(self, value: str):
122        self._target_cpu = value
123        self.config_update('target_cpu', self._target_cpu)
124
125    @property
126    def version(self):
127        return self._version
128
129    @version.setter
130    def version(self, value: str):
131        self._version = value
132        self.config_update('version', self._version)
133
134    @property
135    def compile_config(self):
136        return self._compile_config
137
138    @compile_config.setter
139    def compile_config(self, value: str):
140        self._compile_config = value
141        self.config_update('compile_config', self._compile_config)
142
143    @property
144    def os_level(self):
145        return self._os_level
146
147    @os_level.setter
148    def os_level(self, value: str):
149        self._os_level = value
150        self.config_update('os_level', self._os_level)
151
152    @property
153    def product_json(self):
154        return self._product_json
155
156    @product_json.setter
157    def product_json(self, value: str):
158        self._product_json = value
159        self.config_update('product_json', self._product_json)
160
161    @property
162    @throw_exception
163    def root_path(self):
164        if self._root_path is None:
165            raise OHOSException('Failed to init compile config', '0019')
166
167        return self._root_path
168
169    @root_path.setter
170    def root_path(self, value: str):
171        self._root_path = os.path.abspath(value)
172        self.config_update('root_path', self._root_path)
173
174    @property
175    def board(self):
176        if self._board is None:
177            raise OHOSException('Failed to init compile config', '0019')
178        return self._board
179
180    @board.setter
181    def board(self, value: str):
182        self._board = value
183        self.config_update('board', self._board)
184
185    @property
186    def device_company(self):
187        if self._device_company is None:
188            raise OHOSException('Failed to init compile config', '0019')
189        return self._device_company
190
191    @device_company.setter
192    def device_company(self, value: str):
193        self._device_company = value
194        self.config_update('device_company', self._device_company)
195
196    @property
197    def kernel(self):
198        return self._kernel
199
200    @kernel.setter
201    def kernel(self, value: str):
202        self._kernel = value
203        self.config_update('kernel', self._kernel)
204
205    @property
206    def product(self):
207        if self._product is None:
208            raise OHOSException('Failed to init compile config', '0019')
209        return self._product
210
211    @product.setter
212    def product(self, value: str):
213        self._product = value
214        self.config_update('product', self._product)
215
216    @property
217    def product_path(self):
218        if self._product_path is None:
219            raise OHOSException('Failed to init compile config', '0019')
220        return self._product_path
221
222    @product_path.setter
223    def product_path(self, value: str):
224        self._product_path = value
225        self.config_update('product_path', self._product_path)
226
227    @property
228    def gn_product_path(self):
229        return self.product_path.replace(self.root_path, '/')
230
231    @property
232    def device_path(self):
233        if self._device_path is None:
234            raise OHOSException('Failed to init compile config', '0019')
235        return self._device_path
236
237    @device_path.setter
238    def device_path(self, value: str):
239        self._device_path = value
240        self.config_update('device_path', self._device_path)
241
242    @property
243    def gn_device_path(self):
244        return self.device_path.replace(self.root_path, '/')
245
246    @property
247    def build_path(self):
248        _build_path = os.path.join(self.root_path, 'build', 'lite')
249        if not os.path.isdir(_build_path):
250            raise OHOSException(f'Invalid build path: {_build_path}')
251        return _build_path
252
253    @property
254    def out_path(self):
255        return self._out_path
256
257    @out_path.setter
258    def out_path(self, value: str):
259        self._out_path = value
260        self.config_update('out_path', self._out_path)
261
262    @property
263    def device_config_path(self):
264        return self._device_config_path
265
266    @device_config_path.setter
267    def device_config_path(self, value: str):
268        self._device_config_path = value
269        self.config_update('device_config_path', self._device_config_path)
270
271    @property
272    def product_config_path(self):
273        return self._product_config_path
274
275    @product_config_path.setter
276    def product_config_path(self, value: str):
277        self._product_config_path = value
278        self.config_update('product_config_path', self._product_config_path)
279
280    @property
281    def subsystem_config_json(self):
282        return self._subsystem_config_json
283
284    @subsystem_config_json.setter
285    def subsystem_config_json(self, value: str):
286        self._subsystem_config_json = value
287        self.config_update('subsystem_config_json',
288                           self._subsystem_config_json)
289
290    @property
291    def subsystem_config_overlay_json(self):
292        return self._subsystem_config_overlay_json
293
294    @subsystem_config_overlay_json.setter
295    def subsystem_config_overlay_json(self, value: str):
296        self._subsystem_config_overlay_json = value
297        self.config_update('subsystem_config_overlay_json',
298                           self._subsystem_config_overlay_json)
299
300    @property
301    def support_cpu(self):
302        return self._support_cpu
303
304    @property
305    def log_mode(self):
306        return self._log_mode
307
308    @log_mode.setter
309    def log_mode(self, value):
310        self._log_mode = value
311        self.config_update('log_mode', self._log_mode)
312
313    @support_cpu.setter
314    def support_cpu(self, value: str):
315        self._support_cpu = value
316        self.config_update('support_cpu', self._support_cpu)
317
318    @property
319    def log_path(self):
320        if self.out_path is not None:
321            return os.path.join(self.out_path, 'build.log')
322        else:
323            raise OHOSException(f'Failed to get log_path')
324
325    @property
326    def vendor_path(self):
327        _vendor_path = os.path.join(self.root_path, 'vendor')
328        if not os.path.isdir(_vendor_path):
329            _vendor_path = ''
330        return _vendor_path
331
332    @property
333    def built_in_product_path(self):
334        _built_in_product_path = os.path.join(self.root_path,
335                                              'productdefine/common/products')
336        if not os.path.isdir(_built_in_product_path):
337            raise OHOSException(
338                f'Invalid built-in product path: {_built_in_product_path}')
339        return _built_in_product_path
340
341    @property
342    def built_in_product_path_for_llvm(self):
343        _built_in_product_path_for_llvm = os.path.join(self.root_path,
344                                              'toolchain/llvm-project/llvm_products')
345        return _built_in_product_path_for_llvm
346
347    @property
348    def build_tools_path(self):
349        try:
350            tools_path = IoUtil.read_json_file(
351                'build_tools/build_tools_config.json')[self.platform]['build_tools_path']
352            return os.path.join(self.root_path, tools_path)
353        except KeyError:
354            raise OHOSException(f'unidentified platform: {self.platform}')
355
356    @property
357    def gn_path(self):
358        repo_gn_path = os.path.join(self.build_tools_path, 'gn')
359        # gn exist.
360        if os.path.isfile(repo_gn_path):
361            return repo_gn_path
362        else:
363            raise OHOSException('There is no clang executable file at {}, \
364                          please execute build/prebuilts_download.sh'.format(repo_gn_path))
365
366    @property
367    def ninja_path(self):
368        repo_ninja_path = os.path.join(self.build_tools_path, 'ninja')
369        # ninja exist.
370        if os.path.isfile(repo_ninja_path):
371            return repo_ninja_path
372        else:
373            raise OHOSException('There is no clang executable file at {}, \
374                          please execute build/prebuilts_download.sh'.format(repo_ninja_path))
375
376    @property
377    def clang_path(self):
378        repo_clang_path = os.path.join('prebuilts', 'clang', 'ohos',
379                                       'linux-x86_64', 'llvm')
380        if not os.path.exists(repo_clang_path):
381            repo_clang_path = os.path.join('out', 'llvm-install')
382        # clang exist
383        if os.path.isdir(repo_clang_path):
384            return f'//{repo_clang_path}'
385        # clang installed manually or auto download
386        else:
387            raise OHOSException('There is no clang executable file at {}, \
388                          please execute build/prebuilts_download.sh'.format(repo_clang_path))
389
390    @property
391    def patch_cache(self):
392        return self._patch_cache
393
394    @patch_cache.setter
395    def patch_cache(self, value: str):
396        self._patch_cache = value
397        self.config_update('patch_cache', self._patch_cache)
398
399    def config_update(self, key: str, value: str):
400        config_content = IoUtil.read_json_file(self.config_json)
401        config_content[key] = value
402        IoUtil.dump_json_file(self.config_json, config_content)
403