• 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            LogUtil.hb_info('Cost time:  {}'.format(self.build_time))
72
73    def _prebuild(self):
74        self._run_phase(BuildPhase.PRE_BUILD)
75
76    def _preload(self):
77        self._run_phase(BuildPhase.PRE_LOAD)
78        if self.args_dict.get('fast_rebuild', None) and not self.args_dict.get('fast_rebuild').arg_value:
79            self.preloader.run()
80
81    def _load(self):
82        self._run_phase(BuildPhase.LOAD)
83        if self.args_dict.get('fast_rebuild', None) and not self.args_dict.get('fast_rebuild').arg_value:
84            self.loader.run()
85
86    def _pre_target_generate(self):
87        self._run_phase(BuildPhase.PRE_TARGET_GENERATE)
88
89    def _target_generate(self):
90        self._run_phase(BuildPhase.TARGET_GENERATE)
91        if self.args_dict.get('fast_rebuild', None) and not self.args_dict.get("fast_rebuild").arg_value:
92            self.target_generator.run()
93
94    def _post_target_generate(self):
95        self._run_phase(BuildPhase.POST_TARGET_GENERATE)
96
97    def _pre_target_compilation(self):
98        self._run_phase(BuildPhase.PRE_TARGET_COMPILATION)
99
100    def _target_compilation(self):
101        self._run_phase(BuildPhase.TARGET_COMPILATION)
102        if self.args_dict.get('build_only_gn', None) and not self.args_dict.get("build_only_gn").arg_value:
103            self.target_compiler.run()
104
105    def _post_target_compilation(self):
106        self._run_phase(BuildPhase.POST_TARGET_COMPILATION)
107
108    def _post_build(self):
109        self._run_phase(BuildPhase.POST_BUILD)
110
111    def _run_phase(self, phase: BuildPhase):
112        '''Description: Traverse all registered parameters in build process and
113            execute the resolver function of the corresponding phase
114        @parameter: [phase]:  Build phase corresponding to parameter
115        @return :none
116        '''
117        for phase_arg in [arg for arg in self.args_dict.values()if arg.arg_phase == phase]:
118            self.args_resolver.resolve_arg(phase_arg, self)
119