• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
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
19
20from modules.interface.build_module_interface import BuildModuleInterface
21from resolver.interface.args_resolver_interface import ArgsResolverInterface
22from services.interface.preload_interface import PreloadInterface
23from services.interface.load_interface import LoadInterface
24from services.interface.build_file_generator_interface import BuildFileGeneratorInterface
25from services.interface.build_executor_interface import BuildExecutorInterface
26from containers.arg import BuildPhase
27from exceptions.ohos_exception import OHOSException
28from util.system_util import SystemUtil
29from util.log_util import LogUtil
30from containers.status import throw_exception
31
32
33class OHOSBuildModule(BuildModuleInterface):
34
35    _instance = None
36
37    def __init__(self,
38                 args_dict: dict,
39                 args_resolver: ArgsResolverInterface,
40                 preloader: PreloadInterface,
41                 loader: LoadInterface,
42                 target_generator: BuildFileGeneratorInterface,
43                 target_compiler: BuildExecutorInterface):
44
45        super().__init__(args_dict, args_resolver, preloader,
46                         loader, target_generator, target_compiler)
47        OHOSBuildModule._instance = self
48        self._start_time = SystemUtil.get_current_time()
49
50    @property
51    def build_time(self):
52        return SystemUtil.get_current_time() - self._start_time
53
54    @staticmethod
55    def get_instance():
56        if OHOSBuildModule._instance is not None:
57            return OHOSBuildModule._instance
58        else:
59            raise OHOSException(
60                'OHOSBuildModule has not been instantiated', '0000')
61
62    @throw_exception
63    def run(self):
64        try:
65            super().run()
66        except OHOSException as exception:
67            raise exception
68        else:
69            LogUtil.hb_info('{} build success'.format(
70                self.args_dict.get('product_name').arg_value))
71
72    def _prebuild(self):
73        self._run_phase(BuildPhase.PRE_BUILD)
74
75    def _preload(self):
76        self._run_phase(BuildPhase.PRE_LOAD)
77        if self.args_dict.get('fast_rebuild', None) and not self.args_dict.get('fast_rebuild').arg_value:
78            self.preloader.run()
79
80    def _load(self):
81        self._run_phase(BuildPhase.LOAD)
82        if self.args_dict.get('fast_rebuild', None) and not self.args_dict.get('fast_rebuild').arg_value:
83            self.loader.run()
84
85    def _pre_target_generate(self):
86        self._run_phase(BuildPhase.PRE_TARGET_GENERATE)
87
88    def _target_generate(self):
89        self._run_phase(BuildPhase.TARGET_GENERATE)
90        if not self.args_dict.get("build_only_load").arg_value and not self.args_dict.get("fast_rebuild").arg_value:
91            self.target_generator.run()
92
93    def _post_target_generate(self):
94        self._run_phase(BuildPhase.POST_TARGET_GENERATE)
95
96    def _pre_target_compilation(self):
97        self._run_phase(BuildPhase.PRE_TARGET_COMPILATION)
98
99    def _target_compilation(self):
100        self._run_phase(BuildPhase.TARGET_COMPILATION)
101        if not self.args_dict.get("build_only_load").arg_value and not self.args_dict.get("build_only_gn").arg_value:
102            self.target_compiler.run()
103
104    def _post_target_compilation(self):
105        self._run_phase(BuildPhase.POST_TARGET_COMPILATION)
106
107    def _post_build(self):
108        self._run_phase(BuildPhase.POST_BUILD)
109
110    def _run_phase(self, phase: BuildPhase):
111        '''Description: Traverse all registered parameters in build process and
112            execute the resolver function of the corresponding phase
113        @parameter: [phase]:  Build phase corresponding to parameter
114        @return :none
115        '''
116        for phase_arg in [arg for arg in self.args_dict.values()if arg.arg_phase == phase]:
117            self.args_resolver.resolve_arg(phase_arg, self)
118