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