1#!/usr/bin/env python3 2# coding=utf-8 3 4# 5# Copyright (c) 2020-2021 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 20from xdevice import platform_logger 21from core.utils import scan_support_product 22from core.config.config_manager import UserConfigManager 23from core.build.select_targets import SelectTargets 24from core.build.pretreat_targets import PretreatTargets 25from core.build.build_testcases import BuildTestcases 26from core.command.gen import Gen 27 28LOG = platform_logger("BuildManager") 29 30 31############################################################################## 32############################################################################## 33 34class BuildManager(object): 35 36 @classmethod 37 def _make_gn_file(cls, filepath, target_list): 38 LOG.info("The gn file path: %s" % filepath) 39 with open(filepath, "w") as gn_file: 40 gn_file.write("group(\"make_temp_test\") {\n") 41 gn_file.write(" testonly = true\n") 42 gn_file.write(" deps = []\n") 43 if target_list: 44 gn_file.write(" deps += [\n") 45 for target in target_list: 46 if target: 47 gn_file.write(" \"%s\",\n" % target) 48 gn_file.write(" ]\n") 49 gn_file.write("}\n") 50 51 @classmethod 52 def _compile_test_cases_by_target(cls, project_root_path, product_form, 53 build_target): 54 if BuildTestcases(project_root_path).build_testcases(product_form, 55 build_target): 56 LOG.info("Test case compilation successed.") 57 build_result = True 58 else: 59 LOG.info("Test case compilation failed, please modify.") 60 build_result = False 61 return build_result 62 63 @classmethod 64 def _compile_fuzz_test_case(cls, project_root_path, para): 65 build_result = BuildTestcases( 66 project_root_path).build_fuzz_testcases(para) 67 if build_result: 68 LOG.info("Test case compilation successed.") 69 else: 70 LOG.info("Test case compilation failed, please modify.") 71 return build_result 72 73 def _compile_testcases(self, project_root_path, para): 74 all_product_list = scan_support_product() 75 if para.productform not in all_product_list: 76 from core.build.build_lite_manager import BuildLiteManager 77 build_lite_manager = BuildLiteManager(project_root_path) 78 return build_lite_manager.build_testcases(para) 79 80 if para.testsuit != "": 81 return self._compile_test_cases_by_target( 82 project_root_path, 83 para.productform, 84 para.testsuit) 85 86 if (len(para.partname_list) == 0 and para.testmodule == "" and 87 "ALL" in para.testtype): 88 return self._compile_test_cases_by_target( 89 project_root_path, 90 para.productform, 91 "make_test") 92 93 target_list = SelectTargets( 94 project_root_path).filter_build_targets(para) 95 if len(target_list) == 0: 96 LOG.warning("No build target found.") 97 return False 98 99 build_cfg_filepath = os.path.join(project_root_path, 100 "test", 101 "developertest", 102 "BUILD.gn") 103 104 self._make_gn_file(build_cfg_filepath, target_list) 105 if "fuzztest" in para.testtype: 106 Gen().gen_fuzzer_list_file(target_list) 107 build_result = self._compile_fuzz_test_case( 108 project_root_path, para) 109 else: 110 build_result = self._compile_test_cases_by_target( 111 project_root_path, 112 para.productform, 113 "make_temp_test") 114 self._make_gn_file(build_cfg_filepath, []) 115 116 return build_result 117 118 @classmethod 119 def build_version(cls, project_root_path, product_form): 120 if BuildTestcases(project_root_path).build_version(product_form): 121 LOG.info("The version compiled successfully.") 122 build_result = True 123 else: 124 LOG.info("The version compilation failed, please modify.") 125 build_result = False 126 return build_result 127 128 @classmethod 129 def build_gn_file(cls, project_root_path, product_form): 130 if BuildTestcases(project_root_path).build_gn_file(product_form): 131 LOG.info("The gn compiled successfully.") 132 build_result = True 133 else: 134 LOG.info("The gn compilation failed, please modify.") 135 build_result = False 136 return build_result 137 138 def build_testcases(self, project_root_path, param): 139 if not os.path.exists(project_root_path): 140 LOG.error("%s is not exists." % project_root_path) 141 return False 142 143 LOG.info("--------------------------------------------------") 144 LOG.info("Building parameter:") 145 LOG.info("productform = %s" % param.productform) 146 LOG.info("testtype = %s" % str(param.testtype)) 147 LOG.info("partname_list = %s" % str(param.partname_list)) 148 LOG.info("testmodule = %s" % param.testmodule) 149 LOG.info("testsuit = %s" % param.testsuit) 150 LOG.info("--------------------------------------------------") 151 152 LOG.info("") 153 LOG.info("**************************************************") 154 LOG.info("*************** Start build testcases ************") 155 LOG.info("**************************************************") 156 LOG.info("") 157 158 build_result = self._compile_testcases(project_root_path, param) 159 160 LOG.info("") 161 LOG.info("**************************************************") 162 LOG.info("*************** Ended build testcases ************") 163 LOG.info("**************************************************") 164 LOG.info("") 165 166 return build_result 167 168 169############################################################################## 170############################################################################## 171