config.py 14.0 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# Copyright (c) 2020 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
L
lubinglun 已提交
19
import sys
G
gzyang 已提交
20
import platform
M
mamingshuai 已提交
21
from distutils.spawn import find_executable
L
lubinglun 已提交
22
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
23 24 25 26 27 28 29 30 31
from hb_internal import CONFIG_JSON
from hb_internal import CONFIG_STRUCT
from hb_internal import BUILD_TOOLS_CFG
from hb_internal.common.utils import read_json_file
from hb_internal.common.utils import dump_json_file
from hb_internal.common.utils import Singleton
from hb_internal.common.utils import OHOSException
from hb_internal.common.utils import download_tool
from hb_internal.common.utils import makedirs
M
mamingshuai 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44


class Config(metaclass=Singleton):
    def __init__(self):
        self.config_json = CONFIG_JSON

        config_content = read_json_file(self.config_json)
        self._root_path = config_content.get('root_path', None)
        self._board = config_content.get('board', None)
        self._kernel = config_content.get('kernel', None)
        self._product = config_content.get('product', None)
        self._product_path = config_content.get('product_path', None)
        self._device_path = config_content.get('device_path', None)
45
        self._device_company = config_content.get('device_company', None)
46
        self._patch_cache = config_content.get('patch_cache', None)
47 48 49 50 51 52
        self._version = config_content.get('version', '3.0')
        self._os_level = config_content.get('os_level', 'small')
        self._product_json = config_content.get('product_json', None)
        self._target_cpu = config_content.get('target_cpu', None)
        self._target_os = config_content.get('target_os', None)
        self._out_path = config_content.get('out_path', None)
53
        self._compile_config = config_content.get('compile_config', None)
H
split  
hongguo 已提交
54
        self._component_type = config_content.get('component_type', None)
55 56 57 58 59 60
        self._device_config_path = config_content.get('device_config_path',
                                                      None)
        self._product_config_path = config_content.get('product_config_path',
                                                       None)
        self._subsystem_config_json = config_content.get(
            'subsystem_config_json', None)
61 62 63 64 65 66
        subsystem_config_overlay_path = os.path.join(self._root_path,
            'build/subsystem_config_overlay.json')
        if os.path.isfile(subsystem_config_overlay_path):
            self._subsystem_config_overlay_json = 'build/subsystem_config_overlay.json'
        else:
            self._subsystem_config_overlay_json = ''
P
pilipala195 已提交
67
        self.fs_attr = set()
68
        self.platform = platform.system()
M
mamingshuai 已提交
69

H
split  
hongguo 已提交
70 71 72 73 74 75 76 77 78
    @property
    def component_type(self):
        return self._component_type

    @component_type.setter
    def component_type(self, value):
        self._component_type = value
        self.config_update('component_type', self._component_type)

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    @property
    def target_os(self):
        return self._target_os

    @target_os.setter
    def target_os(self, value):
        self._target_os = value
        self.config_update('target_os', self._target_os)

    @property
    def target_cpu(self):
        return self._target_cpu

    @target_cpu.setter
    def target_cpu(self, value):
        self._target_cpu = value
        self.config_update('target_cpu', self._target_cpu)

    @property
    def version(self):
        return self._version

    @version.setter
    def version(self, value):
        self._version = value
        self.config_update('version', self._version)
105 106

    @property
107 108 109 110 111 112
    def compile_config(self):
        return self._compile_config

    @compile_config.setter
    def compile_config(self, value):
        self._compile_config = value
113
        self.config_update('compile_config', self._compile_config)
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

    @property
    def os_level(self):
        return self._os_level

    @os_level.setter
    def os_level(self, value):
        self._os_level = value
        self.config_update('os_level', self._os_level)

    @property
    def product_json(self):
        return self._product_json

    @product_json.setter
    def product_json(self, value):
        self._product_json = value
        self.config_update('product_json', self._product_json)

M
mamingshuai 已提交
133 134 135
    @property
    def root_path(self):
        if self._root_path is None:
136 137
            raise OHOSException('Failed to get root_path. '
                                'Please run command "hb set" to '
G
gzyang 已提交
138
                                'init OHOS development environment')
M
mamingshuai 已提交
139 140 141 142 143 144 145

        return self._root_path

    @root_path.setter
    def root_path(self, value):
        self._root_path = os.path.abspath(value)
        if not os.path.isdir(self._root_path):
G
gzyang 已提交
146
            raise OHOSException(f'{self._root_path} is not a valid path')
M
mamingshuai 已提交
147 148 149 150 151 152 153 154 155

        config_path = os.path.join(self._root_path, 'ohos_config.json')
        if not os.path.isfile(config_path):
            self.config_create(config_path)
        self.config_update('root_path', self._root_path)

    @property
    def board(self):
        if self._board is None:
156 157
            raise OHOSException('Failed to get board. '
                                'Please run command "hb set" to '
G
gzyang 已提交
158
                                'init OHOS development environment')
M
mamingshuai 已提交
159 160 161 162 163 164 165
        return self._board

    @board.setter
    def board(self, value):
        self._board = value
        self.config_update('board', self._board)

166 167 168
    @property
    def device_company(self):
        if self._device_company is None:
169 170
            raise OHOSException('Failed to get device_company. '
                                'Please run command "hb set" to '
171 172 173 174 175 176 177 178
                                'init OHOS development environment')
        return self._device_company

    @device_company.setter
    def device_company(self, value):
        self._device_company = value
        self.config_update('device_company', self._device_company)

M
mamingshuai 已提交
179 180 181 182 183 184 185 186 187 188 189 190
    @property
    def kernel(self):
        return self._kernel

    @kernel.setter
    def kernel(self, value):
        self._kernel = value
        self.config_update('kernel', self._kernel)

    @property
    def product(self):
        if self._product is None:
191 192
            raise OHOSException('Failed to get product. '
                                'Please run command "hb set" to '
G
gzyang 已提交
193
                                'init OHOS development environment')
M
mamingshuai 已提交
194 195 196 197 198 199 200 201 202 203
        return self._product

    @product.setter
    def product(self, value):
        self._product = value
        self.config_update('product', self._product)

    @property
    def product_path(self):
        if self._product_path is None:
204 205
            raise OHOSException('Failed to get product_path. '
                                'Please run command "hb set" to '
G
gzyang 已提交
206
                                'init OHOS development environment')
M
mamingshuai 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220
        return self._product_path

    @product_path.setter
    def product_path(self, value):
        self._product_path = value
        self.config_update('product_path', self._product_path)

    @property
    def gn_product_path(self):
        return self.product_path.replace(self.root_path, '/')

    @property
    def device_path(self):
        if self._device_path is None:
221 222
            raise OHOSException('Failed to get device_path. '
                                'Please run command "hb set" to '
G
gzyang 已提交
223
                                'init OHOS development environment')
M
mamingshuai 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236
        return self._device_path

    @device_path.setter
    def device_path(self, value):
        self._device_path = value
        self.config_update('device_path', self._device_path)

    @property
    def gn_device_path(self):
        return self.device_path.replace(self.root_path, '/')

    @property
    def build_path(self):
P
pilipala195 已提交
237 238
        _build_path = os.path.join(self.root_path, 'build', 'lite')
        if not os.path.isdir(_build_path):
G
gzyang 已提交
239
            raise OHOSException(f'Invalid build path: {_build_path}')
P
pilipala195 已提交
240
        return _build_path
M
mamingshuai 已提交
241 242 243 244 245 246 247 248

    @property
    def out_path(self):
        return self._out_path

    @out_path.setter
    def out_path(self, value):
        self._out_path = value
249
        self.config_update('out_path', self._out_path)
M
mamingshuai 已提交
250

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    @property
    def device_config_path(self):
        return self._device_config_path

    @device_config_path.setter
    def device_config_path(self, value):
        self._device_config_path = value
        self.config_update('device_config_path', self._device_config_path)

    @property
    def product_config_path(self):
        return self._product_config_path

    @product_config_path.setter
    def product_config_path(self, value):
        self._product_config_path = value
        self.config_update('product_config_path', self._product_config_path)

    @property
    def subsystem_config_json(self):
        return self._subsystem_config_json

    @subsystem_config_json.setter
    def subsystem_config_json(self, value):
        self._subsystem_config_json = value
        self.config_update('subsystem_config_json',
                           self._subsystem_config_json)

279 280 281 282 283 284 285 286 287 288
    @property
    def subsystem_config_overlay_json(self):
        return self._subsystem_config_overlay_json

    @subsystem_config_overlay_json.setter
    def subsystem_config_overlay_json(self, value):
        self._subsystem_config_overlay_json = value
        self.config_update('subsystem_config_overlay_json',
                           self._subsystem_config_overlay_json)

M
mamingshuai 已提交
289 290
    @property
    def log_path(self):
291 292 293 294
        if self.out_path is not None:
            return os.path.join(self.out_path, 'build.log')
        else:
            raise OHOSException(f'Failed to get log_path')
M
mamingshuai 已提交
295 296 297

    @property
    def vendor_path(self):
P
pilipala195 已提交
298 299
        _vendor_path = os.path.join(self.root_path, 'vendor')
        if not os.path.isdir(_vendor_path):
G
gzyang 已提交
300
            raise OHOSException(f'Invalid vendor path: {_vendor_path}')
P
pilipala195 已提交
301
        return _vendor_path
M
mamingshuai 已提交
302

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
    @property
    def built_in_product_path(self):
        _built_in_product_path = os.path.join(self.root_path,
                                              'productdefine/common/products')
        if not os.path.isdir(_built_in_product_path):
            raise OHOSException(
                f'Invalid built-in product path: {_built_in_product_path}')
        return _built_in_product_path

    @property
    def built_in_device_path(self):
        _built_in_device_path = os.path.join(self.root_path,
                                             'productdefine/common/device')
        if not os.path.isdir(_built_in_device_path):
            raise OHOSException(
                f'Invalid built-in product path: {_built_in_device_path}')
        return _built_in_device_path

G
gzyang 已提交
321 322
    @property
    def build_tools_path(self):
323 324 325 326 327
        try:
            tools_path = BUILD_TOOLS_CFG[self.platform]['build_tools_path']
            return os.path.join(self.root_path, tools_path)
        except KeyError:
            raise OHOSException(f'unidentified platform: {self.platform}')
G
gzyang 已提交
328

M
mamingshuai 已提交
329 330
    @property
    def gn_path(self):
G
gzyang 已提交
331
        repo_gn_path = os.path.join(self.build_tools_path, 'gn')
332
        # gn exist.
M
mamingshuai 已提交
333 334 335
        if os.path.isfile(repo_gn_path):
            return repo_gn_path

336
        # gn not install, download and extract it.
337 338 339 340 341 342
        makedirs(self.build_tools_path, exist_ok=True)

        gn_url = BUILD_TOOLS_CFG[self.platform].get('gn')
        gn_dst = os.path.join(self.build_tools_path, 'gn_pkg')
        download_tool(gn_url, gn_dst, tgt_dir=self.build_tools_path)

343
        return repo_gn_path
M
mamingshuai 已提交
344 345 346

    @property
    def ninja_path(self):
G
gzyang 已提交
347
        repo_ninja_path = os.path.join(self.build_tools_path, 'ninja')
348
        # ninja exist.
M
mamingshuai 已提交
349 350 351
        if os.path.isfile(repo_ninja_path):
            return repo_ninja_path

352
        # ninja not install, download and extract.
Y
yangming_ha 已提交
353 354
        makedirs(self.build_tools_path, exist_ok=True)

355 356 357 358
        ninja_url = BUILD_TOOLS_CFG[self.platform].get('ninja')
        ninja_dst = os.path.join(self.build_tools_path, 'ninja_pkg')
        download_tool(ninja_url, ninja_dst, tgt_dir=self.build_tools_path)

359
        return repo_ninja_path
M
mamingshuai 已提交
360 361 362

    @property
    def clang_path(self):
363 364
        repo_clang_path = os.path.join('prebuilts', 'clang', 'ohos',
                                       'linux-x86_64', 'llvm')
365
        # clang exist
M
mamingshuai 已提交
366 367
        if os.path.isdir(repo_clang_path):
            return f'//{repo_clang_path}'
368 369 370 371 372
        # clang installed manually or auto download
        else:
            # already installed manually
            env_clang_bin_path = find_executable('clang')
            if env_clang_bin_path is not None:
373 374
                env_clang_path = os.path.abspath(
                    os.path.join(env_clang_bin_path, os.pardir, os.pardir))
375 376 377 378 379

                if os.path.basename(env_clang_path) == 'llvm':
                    return env_clang_path

            # need auto download and extract clang.
380 381
            clang_path = os.path.abspath(
                os.path.join(repo_clang_path, os.pardir))
382 383 384 385 386
            makedirs(clang_path, exist_ok=True)

            clang_url = BUILD_TOOLS_CFG[self.platform].get('clang')
            clang_dst = os.path.join(clang_path, 'clang_pkg')
            download_tool(clang_url, clang_dst, tgt_dir=clang_path)
387
            return f'//{repo_clang_path}'
M
mamingshuai 已提交
388

389 390 391 392 393 394 395 396 397
    @property
    def patch_cache(self):
        return self._patch_cache

    @patch_cache.setter
    def patch_cache(self, value):
        self._patch_cache = value
        self.config_update('patch_cache', self._patch_cache)

M
mamingshuai 已提交
398 399 400 401 402 403 404 405
    def config_create(self, config_path):
        dump_json_file(config_path, CONFIG_STRUCT)
        self.config_json = config_path

    def config_update(self, key, value):
        config_content = read_json_file(self.config_json)
        config_content[key] = value
        dump_json_file(self.config_json, config_content)