• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
20import sys
21import json
22import shutil
23import subprocess
24import platform
25from xdevice import platform_logger
26from core.utils import get_build_output_path
27from core.utils import scan_support_product
28from core.common import is_open_source_product
29from core.config.config_manager import UserConfigManager
30
31BUILD_FILEPATH = "./build.sh"
32BUILD_LITE = "build/lite/build.py"
33BUILD_TARGET_PLATFORM = "build_platform=\"%s\""
34LOG = platform_logger("BuildTestcases")
35
36
37##############################################################################
38##############################################################################
39
40class BuildTestcases(object):
41    def __init__(self, project_rootpath):
42        self.project_rootpath = project_rootpath
43
44        user_manager = UserConfigManager()
45        self.is_build_example = user_manager.get_user_config_flag(
46            "build", "example")
47        self.build_parameter_dic = user_manager.get_user_config(
48            "build", "parameter")
49
50    @classmethod
51    def _copy_folder(cls, source_dir, target_dir):
52        if not os.path.exists(target_dir):
53            os.makedirs(target_dir)
54
55        if platform.system() == 'Windows':
56            command = ["xcopy", "/f", "/s", "/e", "/y",
57                       "/b", "/q", source_dir, target_dir]
58        else:
59            command = ["cp", "-rf", source_dir, target_dir]
60
61        LOG.info("command: %s" % str(command))
62        return subprocess.call(command) == 0
63
64    @classmethod
65    def _get_testcase_outname_by_productform(cls, productform):
66        if productform == "phone" or is_open_source_product(productform):
67            return ""
68
69        testcase_outpath = ""
70        toolchain_filepath = os.path.join(
71            get_build_output_path(productform),
72            "build_configs",
73            "platforms_info",
74            "toolchain_to_variant.json")
75        if os.path.exists(toolchain_filepath):
76            data_dic = []
77            with open(toolchain_filepath, 'r') as toolchain_file:
78                data_dic = json.load(toolchain_file)
79                if not data_dic:
80                    LOG.warning("The %s file load error." %
81                                toolchain_filepath)
82                    data_dic = []
83            paltform_toolchain_dic = data_dic.get("platform_toolchain")
84            testcase_outpath = paltform_toolchain_dic.get(productform, "")
85            pos = testcase_outpath.rfind(':') + 1
86            testcase_outpath = testcase_outpath[pos:len(testcase_outpath)]
87        return testcase_outpath
88
89    def _delete_testcase_dir(self, productform):
90        if is_open_source_product(productform):
91            package_out_dir = os.path.join(
92                get_build_output_path(productform),
93                "packages",
94                "phone",
95                "tests")
96        else:
97            package_out_dir = os.path.join(
98                get_build_output_path(productform),
99                "packages",
100                productform,
101                "tests")
102
103        LOG.info("package_out_dir=%s" % package_out_dir)
104        if os.path.exists(package_out_dir):
105            shutil.rmtree(package_out_dir)
106
107        phone_out_dir = os.path.join(
108            self.project_rootpath, "out", "release", "tests")
109        LOG.info("phone_out_dir=%s" % phone_out_dir)
110        if os.path.exists(phone_out_dir):
111            shutil.rmtree(phone_out_dir)
112
113        curr_productform_outname = self._get_testcase_outname_by_productform(
114            productform)
115        if curr_productform_outname == "":
116            return
117
118        curr_productform_outdir = os.path.join(
119            get_build_output_path(productform),
120            curr_productform_outname,
121            "tests")
122        LOG.info("curr_productform_outdir=%s" % curr_productform_outdir)
123        if os.path.exists(curr_productform_outdir):
124            shutil.rmtree(curr_productform_outdir)
125
126    def _merge_testcase_dir(self, productform):
127        if is_open_source_product(productform):
128            package_out_dir = os.path.join(
129                get_build_output_path(productform),
130                "packages",
131                "phone")
132        else:
133            package_out_dir = os.path.join(
134                get_build_output_path(productform),
135                "packages",
136                productform)
137        if platform.system() == 'Windows':
138            package_out_dir = os.path.join(package_out_dir, "tests")
139
140        phone_out_dir = os.path.join(get_build_output_path(productform),
141                                     "tests")
142        if os.path.exists(phone_out_dir):
143            self._copy_folder(phone_out_dir, package_out_dir)
144
145        curr_productform_outname = self._get_testcase_outname_by_productform(
146            productform)
147        if curr_productform_outname == "":
148            return
149
150        curr_productform_outdir = os.path.join(
151            get_build_output_path(productform),
152            curr_productform_outname,
153            "tests")
154        LOG.info("curr_productform_outdir=%s" % curr_productform_outdir)
155        if os.path.exists(curr_productform_outdir):
156            self._copy_folder(curr_productform_outdir, package_out_dir)
157
158    def _execute_build_command(self, productform, command):
159        build_result = False
160        current_path = os.getcwd()
161        os.chdir(self.project_rootpath)
162
163        command.append("--product-name")
164        command.append(productform)
165
166        if os.path.exists(BUILD_FILEPATH):
167            build_command = [BUILD_FILEPATH]
168            build_command.extend(command)
169            LOG.info("build_command: %s" % str(build_command))
170            if subprocess.call(build_command) == 0:
171                build_result = True
172            else:
173                build_result = False
174        else:
175            LOG.warning("Error: The %s is not exist" % BUILD_FILEPATH)
176
177        os.chdir(current_path)
178        return build_result
179
180    def build_fuzz_testcases(self, para):
181        self._delete_testcase_dir(para.productform)
182        helper_path = os.path.join("..", "libs", "fuzzlib", "fuzzer_helper.py")
183        command = [sys.executable, helper_path, 'make',
184                   'make_temp_test', para.productform]
185        if subprocess.call(command, shell=False) == 0:
186            build_result = True
187        else:
188            build_result = False
189        self._merge_testcase_dir(para.productform)
190        return build_result
191
192    def build_testcases(self, productform, target):
193        command = []
194        if self.is_build_example:
195            command.append("--gn-args")
196            command.append("build_example=true")
197        command.append("--build-target")
198        command.append(target)
199        command.append("--gn-args")
200        command.append(BUILD_TARGET_PLATFORM % productform)
201        self._delete_testcase_dir(productform)
202        build_result = self._execute_build_command(productform, command)
203        self._merge_testcase_dir(productform)
204        return build_result
205
206    def build_gn_file(self, productform):
207        command = []
208        if self.is_build_example:
209            command.append("--gn-args")
210            command.append("build_example=true")
211        command.append("--build-only-gn")
212        command.append("--gn-args")
213        command.append(BUILD_TARGET_PLATFORM % productform)
214        return self._execute_build_command(productform, command)
215
216    def build_version(self, productform):
217        command = []
218        command.append("--build-target")
219        command.append("make_all")
220        command.append("--gn-args")
221        command.append(BUILD_TARGET_PLATFORM % productform)
222        return self._execute_build_command(productform, command)
223
224
225##############################################################################
226##############################################################################
227