1#!/usr/bin/env python3 2# coding=utf-8 3 4# 5# Copyright (c) 2020-2022 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, FrameworkConfigManager 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 27from core.command.run import Run 28 29LOG = platform_logger("BuildManager") 30 31 32############################################################################## 33############################################################################## 34 35class BuildManager(object): 36 37 @classmethod 38 def _make_gn_file(cls, filepath, target_list): 39 LOG.info("The gn file path: %s" % filepath) 40 with open(filepath, "w") as gn_file: 41 gn_file.write("group(\"make_temp_test\") {\n") 42 gn_file.write(" testonly = true\n") 43 gn_file.write(" deps = []\n") 44 if target_list: 45 gn_file.write(" deps += [\n") 46 for target in target_list: 47 if target: 48 gn_file.write(" \"%s\",\n" % target) 49 gn_file.write(" ]\n") 50 gn_file.write("}\n") 51 52 # 根据目标编译测试用例 53 # project_root_path 工程根目录 54 # product_form 产品形态,指令第一步选择的产品 55 # build_target 编译目标 56 @classmethod 57 def _compile_test_cases_by_target(cls, project_root_path, product_form, 58 build_target): 59 if BuildTestcases(project_root_path).build_testcases(product_form, 60 build_target): 61 LOG.info("Test case compilation successed.") 62 build_result = True 63 else: 64 LOG.info("Test case compilation failed, please modify.") 65 build_result = False 66 return build_result 67 68 # 根据json生成部件间依赖关系 69 # para 指令参数 70 # 编译代码生成中间文件,只执行gn阶段,并打开check_deps属性 71 72 @classmethod 73 def _compile_deps_files(cls, project_root_path, para): 74 build_deps_files_result = False 75 if BuildTestcases(project_root_path).build_deps_files(para.productform): 76 LOG.info("Deps files compilation successed.") 77 build_deps_files_result = True 78 else: 79 LOG.info("Deps files compilation failed, please modify.") 80 return build_deps_files_result 81 82 # 运行脚本,生成part_deps_info.json部件间依赖关系文件 83 @classmethod 84 def _compile_part_deps(cls, project_root_path, para): 85 build_part_deps_result = False 86 if BuildTestcases(project_root_path).build_part_deps(para): 87 LOG.info("Part deps info compilation successed.") 88 build_part_deps_result = True 89 else: 90 LOG.info("Part deps info compilation failed, please modify.") 91 return build_part_deps_result 92 93 # 根据目标编译xts测试用例 94 # project_root_path 工程根目录 95 # para 指令参数 96 @classmethod 97 def _compile_xts_test_cases(cls, project_root_path, para): 98 if BuildTestcases(project_root_path).build_xts_testcases(para): 99 LOG.info("XTS test case compilation successed.") 100 build_result = True 101 else: 102 LOG.info("XTS test compilation failed, please modify.") 103 build_result = False 104 return build_result 105 106 @classmethod 107 def _compile_fuzz_test_case(cls, project_root_path, para): 108 build_result = BuildTestcases( 109 project_root_path).build_fuzz_testcases(para) 110 if build_result: 111 LOG.info("Test case compilation successed.") 112 else: 113 LOG.info("Test case compilation failed, please modify.") 114 return build_result 115 116 # 编译入口 117 def _compile_testcases(self, project_root_path, para): 118 # 获取所有支持的产品,3.1Release版本为["DAYU","Hi3516DV300","ohos-arm64","ohos-sdk","rk3568"] 119 all_product_list = scan_support_product() 120 product_list = FrameworkConfigManager().get_framework_config("productform") 121 if para.productform in product_list and para.productform.find("wifiiot") == -1: 122 all_product_list.append(para.productform) 123 if para.productform not in all_product_list: 124 from core.build.build_lite_manager import BuildLiteManager 125 build_lite_manager = BuildLiteManager(project_root_path) 126 return build_lite_manager.build_testcases(para) 127 128 # 如果测试集不为空或测试部件不为空,build_target为测试集或测试部件 129 # 如果测试集不为空,build_target为测试集 130 if para.testsuit != "": 131 return self._compile_test_cases_by_target( 132 project_root_path, 133 para.productform, 134 para.testsuit) 135 if para.partname_list != "": 136 if "partdeps" == para.partdeps: 137 LOG.info("External deps part build.") 138 external_deps_part_list = Run.get_part_deps_list(para.productform, para.testpart) 139 external_deps_part_list.append(para.testpart[0]) 140 return self._compile_test_cases_by_target( 141 project_root_path, 142 para.productform, 143 external_deps_part_list) 144 else: 145 LOG.info("Multi testpart build.") 146 return self._compile_test_cases_by_target( 147 project_root_path, 148 para.productform, 149 para.partname_list) 150 151 # 如果测试集为空,部件列表为空,模块列表为空,测试类型中含有“ALL”,build_target为"make_test" 152 if (len(para.partname_list) == 0 and para.testmodule == "" and 153 "ALL" in para.testtype): 154 return self._compile_test_cases_by_target( 155 project_root_path, 156 para.productform, 157 "make_test") 158 159 # 如果测试集为空,三个条件(部件列表为空,模块列表为空,测试类型中含有“ALL”)不同时成立 160 target_list = SelectTargets( 161 project_root_path).filter_build_targets(para) 162 if len(target_list) == 0: 163 LOG.warning("No build target found.") 164 return False 165 166 # 路径拼接 build_cfg_filepath = OpenHarmony/test/testfwk/developer_test/BUILD.gn 167 build_cfg_filepath = os.path.join(project_root_path, 168 "test", 169 "testfwk", 170 "developer_test", 171 "BUILD.gn") 172 173 self._make_gn_file(build_cfg_filepath, target_list) 174 if "fuzztest" in para.testtype: 175 Gen().gen_fuzzer_list_file(target_list) 176 build_result = self._compile_fuzz_test_case( 177 project_root_path, para) 178 else: 179 build_result = self._compile_test_cases_by_target( 180 project_root_path, 181 para.productform, 182 "make_temp_test") 183 self._make_gn_file(build_cfg_filepath, []) 184 185 return build_result 186 187 @classmethod 188 def build_version(cls, project_root_path, product_form): 189 if BuildTestcases(project_root_path).build_version(product_form): 190 LOG.info("The version compiled successfully.") 191 build_result = True 192 else: 193 LOG.info("The version compilation failed, please modify.") 194 build_result = False 195 return build_result 196 197 @classmethod 198 def build_gn_file(cls, project_root_path, product_form): 199 if BuildTestcases(project_root_path).build_gn_file(product_form): 200 LOG.info("The gn compiled successfully.") 201 build_result = True 202 else: 203 LOG.info("The gn compilation failed, please modify.") 204 build_result = False 205 return build_result 206 207 def build_testcases(self, project_root_path, param): 208 if not os.path.exists(project_root_path): 209 LOG.error("%s is not exists." % project_root_path) 210 return False 211 212 LOG.info("--------------------------------------------------") 213 LOG.info("Building parameter:") 214 LOG.info("productform = %s" % param.productform) 215 LOG.info("testtype = %s" % str(param.testtype)) 216 LOG.info("partname_list = %s" % str(param.partname_list)) 217 LOG.info("testmodule = %s" % param.testmodule) 218 LOG.info("testsuit = %s" % param.testsuit) 219 LOG.info("testcase = %s" % param.testcase) 220 LOG.info("--------------------------------------------------") 221 222 LOG.info("") 223 LOG.info("**************************************************") 224 LOG.info("*************** Start build testcases ************") 225 LOG.info("**************************************************") 226 LOG.info("") 227 228 build_xts_result = True 229 build_result = True 230 if "partdeps" == param.partdeps: 231 LOG.info("**********************Start prebuild testcases****************************") 232 build_deps_files_result = self._compile_deps_files(project_root_path, param) 233 if build_deps_files_result: 234 self._compile_part_deps(project_root_path, param) 235 236 if "acts" in param.testtype or "hats" in param.testtype or "hits" in param.testtype: 237 LOG.info("**********************Start build xts testcases****************************") 238 build_xts_result = self._compile_xts_test_cases(project_root_path, param) 239 else: 240 LOG.info("**********************Start build subsystem testcases****************************") 241 build_result = self._compile_testcases(project_root_path, param) 242 243 LOG.info("") 244 LOG.info("**************************************************") 245 LOG.info("*************** Ended build testcases ************") 246 LOG.info("**************************************************") 247 LOG.info("") 248 249 return build_result and build_xts_result 250 251 252############################################################################## 253############################################################################## 254