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 19from modules.interface.indep_build_module_interface import IndepBuildModuleInterface 20from resolver.interface.args_resolver_interface import ArgsResolverInterface 21from exceptions.ohos_exception import OHOSException 22from services.interface.build_file_generator_interface import BuildFileGeneratorInterface 23from util.log_util import LogUtil 24from containers.status import throw_exception 25from resolver.indep_build_args_resolver import get_part_name 26from containers.arg import BuildPhase 27import argparse 28import sys 29 30 31class OHOSIndepBuildModule(IndepBuildModuleInterface): 32 _instance = None 33 34 def __init__(self, args_dict: dict, args_resolver: ArgsResolverInterface, prebuilts: BuildFileGeneratorInterface, hpm: BuildFileGeneratorInterface, 35 indep_build: BuildFileGeneratorInterface): 36 super().__init__(args_dict, args_resolver) 37 self._prebuilts = prebuilts 38 self._hpm = hpm 39 self._indep_build = indep_build 40 OHOSIndepBuildModule._instance = self 41 42 @property 43 def prebuilts(self): 44 return self._prebuilts 45 46 @property 47 def hpm(self): 48 return self._hpm 49 50 @property 51 def indep_build(self): 52 return self._indep_build 53 54 @staticmethod 55 def get_instance(): 56 if OHOSIndepBuildModule._instance is not None: 57 return OHOSIndepBuildModule._instance 58 else: 59 raise OHOSException( 60 'OHOSIndepBuildModule has not been instantiated', '0000') 61 62 @staticmethod 63 def parse_build_args(args): 64 parser = argparse.ArgumentParser(add_help=False) 65 parser.add_argument('-i', action='store_true', help='Src option') 66 parser.add_argument('-t', '--test', action='store_true', help='Test option') 67 parsed, _ = parser.parse_known_args(args) 68 return parsed 69 70 @throw_exception 71 def run(self): 72 try: 73 super().run() 74 except OHOSException as exception: 75 raise exception 76 else: 77 if len(sys.argv) < 2: 78 LogUtil.hb_error("Insufficient arguments") 79 return 80 arg_value = "onlysrc" 81 if sys.argv[1] == 'build': 82 build_args = sys.argv[2:] if len(sys.argv) > 2 else [] 83 parsed = self.parse_build_args(build_args) 84 85 has_i = parsed.i 86 has_test = parsed.test 87 88 if has_i and has_test: 89 arg_value = "both" 90 elif has_test and not has_i: 91 arg_value = "onlytest" 92 message = { 93 'both': 'build src and test success', 94 'onlytest': 'build test success', 95 'onlysrc': 'build src success' 96 }[arg_value] 97 LogUtil.hb_info(f'{",".join(get_part_name())} {message}') 98 99 def _target_compilation(self): 100 self._run_prebuilts() 101 self._run_hpm() 102 self._run_indep_build() 103 104 def _run_prebuilts(self): 105 self._run_phase(BuildPhase.PRE_BUILD) 106 self._prebuilts.run() 107 108 def _run_hpm(self): 109 self._run_phase(BuildPhase.HPM_DOWNLOAD) 110 self.hpm.run() 111 112 def _run_indep_build(self): 113 self._run_phase(BuildPhase.INDEP_COMPILATION) 114 self.indep_build.run() 115 116 def _run_phase(self, phase: BuildPhase): 117 '''Description: Traverse all registered parameters in build process and 118 execute the resolver function of the corresponding phase 119 @parameter: [phase]: Build phase corresponding to parameter 120 @return :none 121 ''' 122 for arg in self.args_dict.values(): 123 if isinstance(arg.arg_phase, list): 124 if phase in arg.arg_phase: 125 self.args_resolver.resolve_arg(arg, self) 126 else: 127 if phase == arg.arg_phase: 128 self.args_resolver.resolve_arg(arg, self) 129