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