• 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
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
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    # 根据目标编译测试用例
52    # project_root_path 工程根目录
53    # product_form 产品形态,指令第一步选择的产品
54    # build_target 编译目标
55    @classmethod
56    def _compile_test_cases_by_target(cls, project_root_path, product_form,
57                                      build_target):
58        if BuildTestcases(project_root_path).build_testcases(product_form,
59                                                             build_target):
60            LOG.info("Test case compilation successed.")
61            build_result = True
62        else:
63            LOG.info("Test case compilation failed, please modify.")
64            build_result = False
65        return build_result
66
67    # 根据目标编译acts测试用例
68    # project_root_path 工程根目录
69    # para 指令参数
70    @classmethod
71    def _compile_acts_test_cases(cls, project_root_path, para):
72        if BuildTestcases(project_root_path).build_acts_testcases(para):
73            LOG.info("Acts test case compilation successed.")
74            build_result = True
75        else:
76            LOG.info("Acts test compilation failed, please modify.")
77            build_result = False
78        return build_result
79
80    @classmethod
81    def _compile_fuzz_test_case(cls, project_root_path, para):
82        build_result = BuildTestcases(
83            project_root_path).build_fuzz_testcases(para)
84        if build_result:
85            LOG.info("Test case compilation successed.")
86        else:
87            LOG.info("Test case compilation failed, please modify.")
88        return build_result
89
90    # 编译入口
91    def _compile_testcases(self, project_root_path, para):
92        # 获取所有支持的产品,3.1Release版本为["DAYU","Hi3516DV300","ohos-arm64","ohos-sdk","rk3568"]
93        all_product_list = scan_support_product()
94        product_list = FrameworkConfigManager().get_framework_config("productform")
95        if para.productform in product_list and para.productform.find("wifiiot") == -1:
96            all_product_list.append(para.productform)
97        if para.productform not in all_product_list:
98            from core.build.build_lite_manager import BuildLiteManager
99            build_lite_manager = BuildLiteManager(project_root_path)
100            return build_lite_manager.build_testcases(para)
101
102        # 如果测试集不为空,build_target为测试集
103        if para.testsuit != "":
104            return self._compile_test_cases_by_target(
105                project_root_path,
106                para.productform,
107                para.testsuit)
108
109        # 如果测试集为空,部件列表为空,模块列表为空,测试类型中含有“ALL”,build_target为"make_test"
110        if (len(para.partname_list) == 0 and para.testmodule == "" and
111                "ALL" in para.testtype):
112            return self._compile_test_cases_by_target(
113                project_root_path,
114                para.productform,
115                "make_test")
116
117        # 如果测试集为空,三个条件(部件列表为空,模块列表为空,测试类型中含有“ALL”)不同时成立
118        target_list = SelectTargets(
119            project_root_path).filter_build_targets(para)
120        if len(target_list) == 0:
121            LOG.warning("No build target found.")
122            return False
123
124        # 路径拼接 build_cfg_filepath = OpenHarmony/test/testfwk/developer_test/BUILD.gn
125        build_cfg_filepath = os.path.join(project_root_path,
126                                          "test",
127                                          "testfwk",
128                                          "developer_test",
129                                          "BUILD.gn")
130
131        self._make_gn_file(build_cfg_filepath, target_list)
132        if "fuzztest" in para.testtype:
133            Gen().gen_fuzzer_list_file(target_list)
134            build_result = self._compile_fuzz_test_case(
135                project_root_path, para)
136        else:
137            build_result = self._compile_test_cases_by_target(
138                project_root_path,
139                para.productform,
140                "make_temp_test")
141        self._make_gn_file(build_cfg_filepath, [])
142
143        return build_result
144
145    @classmethod
146    def build_version(cls, project_root_path, product_form):
147        if BuildTestcases(project_root_path).build_version(product_form):
148            LOG.info("The version compiled successfully.")
149            build_result = True
150        else:
151            LOG.info("The version compilation failed, please modify.")
152            build_result = False
153        return build_result
154
155    @classmethod
156    def build_gn_file(cls, project_root_path, product_form):
157        if BuildTestcases(project_root_path).build_gn_file(product_form):
158            LOG.info("The gn compiled successfully.")
159            build_result = True
160        else:
161            LOG.info("The gn compilation failed, please modify.")
162            build_result = False
163        return build_result
164
165    def build_testcases(self, project_root_path, param):
166        if not os.path.exists(project_root_path):
167            LOG.error("%s is not exists." % project_root_path)
168            return False
169
170        LOG.info("--------------------------------------------------")
171        LOG.info("Building parameter:")
172        LOG.info("productform   = %s" % param.productform)
173        LOG.info("testtype      = %s" % str(param.testtype))
174        LOG.info("partname_list = %s" % str(param.partname_list))
175        LOG.info("testmodule    = %s" % param.testmodule)
176        LOG.info("testsuit      = %s" % param.testsuit)
177        LOG.info("testcase      = %s" % param.testcase)
178        LOG.info("--------------------------------------------------")
179
180        LOG.info("")
181        LOG.info("**************************************************")
182        LOG.info("*************** Start build testcases ************")
183        LOG.info("**************************************************")
184        LOG.info("")
185
186        build_acts_result = True
187        build_result = True
188        if "actstest" in param.testtype:
189            LOG.info("**********************Start build acts testcases****************************")
190            build_acts_result = self._compile_acts_test_cases(project_root_path, param)
191        else:
192            LOG.info("**********************Start build subsystem testcases****************************")
193            build_result = self._compile_testcases(project_root_path, param)
194
195        LOG.info("")
196        LOG.info("**************************************************")
197        LOG.info("*************** Ended build testcases ************")
198        LOG.info("**************************************************")
199        LOG.info("")
200
201        return build_result and build_acts_result
202
203
204##############################################################################
205##############################################################################
206