1e509ee18Sopenharmony_ci#!/usr/bin/env python3
2e509ee18Sopenharmony_ci# -*- coding: utf-8 -*-
3e509ee18Sopenharmony_ci#
4e509ee18Sopenharmony_ci# Copyright (c) 2022-2024 Huawei Device Co., Ltd.
5e509ee18Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
6e509ee18Sopenharmony_ci# you may not use this file except in compliance with the License.
7e509ee18Sopenharmony_ci# You may obtain a copy of the License at
8e509ee18Sopenharmony_ci#
9e509ee18Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
10e509ee18Sopenharmony_ci#
11e509ee18Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
12e509ee18Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
13e509ee18Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14e509ee18Sopenharmony_ci# See the License for the specific language governing permissions and
15e509ee18Sopenharmony_ci# limitations under the License.
16e509ee18Sopenharmony_ci#
17e509ee18Sopenharmony_ci
18e509ee18Sopenharmony_cifrom __future__ import print_function
19e509ee18Sopenharmony_cifrom datetime import datetime
20e509ee18Sopenharmony_cifrom fnmatch import fnmatch
21e509ee18Sopenharmony_ciimport errno
22e509ee18Sopenharmony_ciimport json
23e509ee18Sopenharmony_ciimport os
24e509ee18Sopenharmony_ciimport platform
25e509ee18Sopenharmony_ciimport subprocess
26e509ee18Sopenharmony_ciimport sys
27e509ee18Sopenharmony_cifrom typing import List, Any, Tuple, Union, Optional
28e509ee18Sopenharmony_ci
29e509ee18Sopenharmony_ciCURRENT_FILENAME = os.path.basename(__file__)
30e509ee18Sopenharmony_ci
31e509ee18Sopenharmony_ci
32e509ee18Sopenharmony_cidef str_of_time_now() -> str:
33e509ee18Sopenharmony_ci    return datetime.now().strftime("%Y-%m-%d-%H-%M-%S-%f")[:-3]
34e509ee18Sopenharmony_ci
35e509ee18Sopenharmony_ci
36e509ee18Sopenharmony_cidef _call(cmd: str):
37e509ee18Sopenharmony_ci    print("# %s" % cmd)
38e509ee18Sopenharmony_ci    return subprocess.call(cmd, shell=True)
39e509ee18Sopenharmony_ci
40e509ee18Sopenharmony_ci
41e509ee18Sopenharmony_cidef _write(filename: str, content: str, mode: str):
42e509ee18Sopenharmony_ci    with open(filename, mode) as f:
43e509ee18Sopenharmony_ci        f.write(content)
44e509ee18Sopenharmony_ci
45e509ee18Sopenharmony_ci
46e509ee18Sopenharmony_cidef call_with_output(cmd: str, file: str):
47e509ee18Sopenharmony_ci    print("# %s" % cmd)
48e509ee18Sopenharmony_ci    host = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
49e509ee18Sopenharmony_ci    while True:
50e509ee18Sopenharmony_ci        try:
51e509ee18Sopenharmony_ci            build_data = host.stdout.readline().decode('utf-8')
52e509ee18Sopenharmony_ci            sys.stdout.flush()
53e509ee18Sopenharmony_ci            print(build_data)
54e509ee18Sopenharmony_ci            _write(file, build_data, "a")
55e509ee18Sopenharmony_ci        except OSError as error:
56e509ee18Sopenharmony_ci            if error == errno.ENOENT:
57e509ee18Sopenharmony_ci                print("no such file")
58e509ee18Sopenharmony_ci            elif error == errno.EPERM:
59e509ee18Sopenharmony_ci                print("permission denied")
60e509ee18Sopenharmony_ci            break
61e509ee18Sopenharmony_ci        if not build_data:
62e509ee18Sopenharmony_ci            break
63e509ee18Sopenharmony_ci    host.wait()
64e509ee18Sopenharmony_ci    return host.returncode
65e509ee18Sopenharmony_ci
66e509ee18Sopenharmony_ci
67e509ee18Sopenharmony_cidef enable_ccache():
68e509ee18Sopenharmony_ci    try:
69e509ee18Sopenharmony_ci        ccache_path = subprocess.check_output(['which', 'ccache']).strip().decode()
70e509ee18Sopenharmony_ci    except subprocess.CalledProcessError:
71e509ee18Sopenharmony_ci        print("Error: ccache not found.")
72e509ee18Sopenharmony_ci        return
73e509ee18Sopenharmony_ci    os.environ['CCACHE_EXEC'] = ccache_path
74e509ee18Sopenharmony_ci    os.environ['USE_CCACHE'] = "1"
75e509ee18Sopenharmony_ci
76e509ee18Sopenharmony_ci
77e509ee18Sopenharmony_cidef backup(file: str, mode: str):
78e509ee18Sopenharmony_ci    if os.path.exists(file):
79e509ee18Sopenharmony_ci        with open(file, 'r+') as src_file:
80e509ee18Sopenharmony_ci            src_content = src_file.read()
81e509ee18Sopenharmony_ci            src_file.seek(0)
82e509ee18Sopenharmony_ci            src_file.truncate()
83e509ee18Sopenharmony_ci
84e509ee18Sopenharmony_ci        with open(file[:-4] + "_last.log", mode) as dst_file:
85e509ee18Sopenharmony_ci            dst_file.write(src_content)
86e509ee18Sopenharmony_ci
87e509ee18Sopenharmony_ci
88e509ee18Sopenharmony_ciclass ArkPy:
89e509ee18Sopenharmony_ci    # constants determined by designer of this class
90e509ee18Sopenharmony_ci    NAME_OF_OUT_DIR_OF_FIRST_LEVEL = "out"
91e509ee18Sopenharmony_ci    DELIMITER_BETWEEN_OS_CPU_MODE_FOR_COMMAND = "."
92e509ee18Sopenharmony_ci    DELIMITER_FOR_SECOND_OUT_DIR_NAME = "."
93e509ee18Sopenharmony_ci    GN_TARGET_LOG_FILE_NAME = "build.log"
94e509ee18Sopenharmony_ci    UNITTEST_LOG_FILE_NAME = "unittest.log"
95e509ee18Sopenharmony_ci    TEST262_LOG_FILE_NAME = "test262.log"
96e509ee18Sopenharmony_ci    REGRESS_TEST_LOG_FILE_NAME = "regresstest.log"
97e509ee18Sopenharmony_ci    PREBUILTS_DOWNLOAD_CONFIG_FILE_PATH = \
98e509ee18Sopenharmony_ci        "./arkcompiler/toolchain/build/prebuilts_download/prebuilts_download_config.json"
99e509ee18Sopenharmony_ci    INDENTATION_STRING_PER_LEVEL = "  "  # for help message
100e509ee18Sopenharmony_ci    # In ARG_DICT, "flags" and "description" are must-keys for the leaf-dicts in it.
101e509ee18Sopenharmony_ci    # (Future designer need know.)
102e509ee18Sopenharmony_ci    ARG_DICT = {
103e509ee18Sopenharmony_ci        "os_cpu": {
104e509ee18Sopenharmony_ci            "linux_x64": {
105e509ee18Sopenharmony_ci                "flags": ["linux_x64", "x64"],
106e509ee18Sopenharmony_ci                "description":
107e509ee18Sopenharmony_ci                    "Build for arkcompiler target of target-operating-system linux and "
108e509ee18Sopenharmony_ci                    "target-central-processing-unit x64.",
109e509ee18Sopenharmony_ci                "gn_args": ["target_os=\"linux\"", "target_cpu=\"x64\""],
110e509ee18Sopenharmony_ci                "prefix_of_name_of_out_dir_of_second_level": "x64",
111e509ee18Sopenharmony_ci            },
112e509ee18Sopenharmony_ci            "linux_x86": {
113e509ee18Sopenharmony_ci                "flags": ["linux_x86", "x86"],
114e509ee18Sopenharmony_ci                "description":
115e509ee18Sopenharmony_ci                    "Build for arkcompiler target of target-operating-system linux and "
116e509ee18Sopenharmony_ci                    "target-central-processing-unit x86.",
117e509ee18Sopenharmony_ci                "gn_args": ["target_os=\"linux\"", "target_cpu=\"x86\""],
118e509ee18Sopenharmony_ci                "prefix_of_name_of_out_dir_of_second_level": "x86",
119e509ee18Sopenharmony_ci            },
120e509ee18Sopenharmony_ci            "ohos_arm": {
121e509ee18Sopenharmony_ci                "flags": ["ohos_arm", "arm"],
122e509ee18Sopenharmony_ci                "description":
123e509ee18Sopenharmony_ci                    "Build for arkcompiler target of target-operating-system ohos and "
124e509ee18Sopenharmony_ci                    "target-central-processing-unit arm.",
125e509ee18Sopenharmony_ci                "gn_args": ["target_os=\"ohos\"", "target_cpu=\"arm\""],
126e509ee18Sopenharmony_ci                "prefix_of_name_of_out_dir_of_second_level": "arm",
127e509ee18Sopenharmony_ci            },
128e509ee18Sopenharmony_ci            "ohos_arm64": {
129e509ee18Sopenharmony_ci                "flags": ["ohos_arm64", "arm64"],
130e509ee18Sopenharmony_ci                "description":
131e509ee18Sopenharmony_ci                    "Build for arkcompiler target of target-operating-system ohos and "
132e509ee18Sopenharmony_ci                    "target-central-processing-unit arm64.",
133e509ee18Sopenharmony_ci                "gn_args": ["target_os=\"ohos\"", "target_cpu=\"arm64\""],
134e509ee18Sopenharmony_ci                "prefix_of_name_of_out_dir_of_second_level": "arm64",
135e509ee18Sopenharmony_ci            },
136e509ee18Sopenharmony_ci            "android_arm64": {
137e509ee18Sopenharmony_ci                "flags": ["android_arm64"],
138e509ee18Sopenharmony_ci                "description":
139e509ee18Sopenharmony_ci                    "Build for arkcompiler target of target-operating-system android and "
140e509ee18Sopenharmony_ci                    "target-central-processing-unit arm64.",
141e509ee18Sopenharmony_ci                "gn_args": ["target_os=\"android\"", "target_cpu=\"arm64\""],
142e509ee18Sopenharmony_ci                "prefix_of_name_of_out_dir_of_second_level": "android_arm64",
143e509ee18Sopenharmony_ci            },
144e509ee18Sopenharmony_ci            "mingw_x86_64": {
145e509ee18Sopenharmony_ci                "flags": ["mingw_x86_64"],
146e509ee18Sopenharmony_ci                "description":
147e509ee18Sopenharmony_ci                    "Build for arkcompiler target of target-operating-system MinGW(Minimalist GNU on Windows) and "
148e509ee18Sopenharmony_ci                    "target-central-processing-unit x86_64.",
149e509ee18Sopenharmony_ci                "gn_args": ["target_os=\"mingw\"", "target_cpu=\"x86_64\""],
150e509ee18Sopenharmony_ci                "prefix_of_name_of_out_dir_of_second_level": "mingw_x86_64",
151e509ee18Sopenharmony_ci            },
152e509ee18Sopenharmony_ci            "ohos_mipsel": {
153e509ee18Sopenharmony_ci                "flags": ["ohos_mipsel", "mipsel"],
154e509ee18Sopenharmony_ci                "description":
155e509ee18Sopenharmony_ci                    "Build for arkcompiler target of target-operating-system ohos and "
156e509ee18Sopenharmony_ci                    "target-central-processing-unit mipsel(32-bit little-endian mips).",
157e509ee18Sopenharmony_ci                "gn_args": ["target_os=\"ohos\"", "target_cpu=\"mipsel\""],
158e509ee18Sopenharmony_ci                "prefix_of_name_of_out_dir_of_second_level": "mipsel",
159e509ee18Sopenharmony_ci            },
160e509ee18Sopenharmony_ci            "mac_arm64": {
161e509ee18Sopenharmony_ci                "flags": ["mac_arm64", "arm64"],
162e509ee18Sopenharmony_ci                "description":
163e509ee18Sopenharmony_ci                    "Build for arkcompiler target of target-operating-system linux and "
164e509ee18Sopenharmony_ci                    "target-central-processing-unit arm64.",
165e509ee18Sopenharmony_ci                "gn_args": ["target_os=\"mac\"", "target_cpu=\"arm64\""],
166e509ee18Sopenharmony_ci                "prefix_of_name_of_out_dir_of_second_level": "mac_arm64",
167e509ee18Sopenharmony_ci            },
168e509ee18Sopenharmony_ci            "mac_x86": {
169e509ee18Sopenharmony_ci                "flags": ["mac_x86", "x86"],
170e509ee18Sopenharmony_ci                "description":
171e509ee18Sopenharmony_ci                    "Build for arkcompiler target of target-operating-system mac and "
172e509ee18Sopenharmony_ci                    "target-central-processing-unit x86.",
173e509ee18Sopenharmony_ci                "gn_args": ["target_os=\"mac\"", "target_cpu=\"x86\""],
174e509ee18Sopenharmony_ci                "prefix_of_name_of_out_dir_of_second_level": "mac_x86",
175e509ee18Sopenharmony_ci            },
176e509ee18Sopenharmony_ci        },
177e509ee18Sopenharmony_ci        "mode": {
178e509ee18Sopenharmony_ci            "release": {
179e509ee18Sopenharmony_ci                "flags": ["release", "r"],
180e509ee18Sopenharmony_ci                "description": "Build for arkcompiler target(executables and libraries) for distribution.",
181e509ee18Sopenharmony_ci                "gn_args": ["is_debug=false"],
182e509ee18Sopenharmony_ci                "suffix_of_name_of_out_dir_of_second_level": "release",
183e509ee18Sopenharmony_ci            },
184e509ee18Sopenharmony_ci            "debug": {
185e509ee18Sopenharmony_ci                "flags": ["debug", "d"],
186e509ee18Sopenharmony_ci                "description": "Build for arkcompiler target(executables and libraries) for debugging.",
187e509ee18Sopenharmony_ci                "gn_args": ["is_debug=true"],
188e509ee18Sopenharmony_ci                "suffix_of_name_of_out_dir_of_second_level": "debug",
189e509ee18Sopenharmony_ci            },
190e509ee18Sopenharmony_ci            "fastverify": {
191e509ee18Sopenharmony_ci                "flags": ["fastverify", "fv"],
192e509ee18Sopenharmony_ci                "description": "Build for arkcompiler target(executables and libraries) for fastverify.",
193e509ee18Sopenharmony_ci                "gn_args": ["is_debug=true is_fastverify=true"],
194e509ee18Sopenharmony_ci                "suffix_of_name_of_out_dir_of_second_level": "fastverify",
195e509ee18Sopenharmony_ci            },
196e509ee18Sopenharmony_ci        },
197e509ee18Sopenharmony_ci        "target": {
198e509ee18Sopenharmony_ci            "test262": {
199e509ee18Sopenharmony_ci                "flags": ["test262", "test-262", "test_262", "262test", "262-test", "262_test", "262"],
200e509ee18Sopenharmony_ci                "description": "Compile arkcompiler target and run test262 with arkcompiler target.",
201e509ee18Sopenharmony_ci                "gn_targets_depend_on": ["default"],
202e509ee18Sopenharmony_ci                "arm64_gn_targets_depend_on": ["ark_js_packages"],
203e509ee18Sopenharmony_ci            },
204e509ee18Sopenharmony_ci            "unittest": {
205e509ee18Sopenharmony_ci                "flags": ["unittest", "ut"],
206e509ee18Sopenharmony_ci                "description":
207e509ee18Sopenharmony_ci                    "Compile and run unittest of arkcompiler target. "
208e509ee18Sopenharmony_ci                    "Add --keep-going=N to keep running unittest when errors occured less than N. "
209e509ee18Sopenharmony_ci                    "Add --gn-args=\"run_with_qemu=true\" timeout=\"1200\"\
210e509ee18Sopenharmony_ci                    \"disable_force_gc=true\" to command when running unittest of non-host type with qemu.",
211e509ee18Sopenharmony_ci                "gn_targets_depend_on": ["unittest_packages"],
212e509ee18Sopenharmony_ci            },
213e509ee18Sopenharmony_ci            "workload": {
214e509ee18Sopenharmony_ci                "flags": ["workload", "work-load", "work_load"],
215e509ee18Sopenharmony_ci                "description": "Compile arkcompiler target and run workload with arkcompiler target.",
216e509ee18Sopenharmony_ci                "gn_targets_depend_on": ["default"],
217e509ee18Sopenharmony_ci            },
218e509ee18Sopenharmony_ci            "regresstest": {
219e509ee18Sopenharmony_ci                "flags": ["regresstest", "regress_test", "regress", "testregress", "test_regress"],
220e509ee18Sopenharmony_ci                "description": "Compile arkcompiler target and run regresstest with arkcompiler target.",
221e509ee18Sopenharmony_ci                "gn_targets_depend_on": ["default"],
222e509ee18Sopenharmony_ci            },
223e509ee18Sopenharmony_ci            "gn_target": {
224e509ee18Sopenharmony_ci                "flags": ["<name of target in \"*.gn*\" file>"],  # any other flags
225e509ee18Sopenharmony_ci                "description":
226e509ee18Sopenharmony_ci                    "Build for arkcompiler target assigned by user. Targets include group(ets_runtime), "
227e509ee18Sopenharmony_ci                    "ohos_executable(ark_js_vm), ohos_shared_library(libark_jsruntime), "
228e509ee18Sopenharmony_ci                    "ohos_static_library(static_icuuc), ohos_source_set(libark_jsruntime_set), "
229e509ee18Sopenharmony_ci                    "ohos_unittest(EcmaVm_001_Test), action(EcmaVm_001_TestAction) and other target of user-defined "
230e509ee18Sopenharmony_ci                    "template type in \"*.gn*\" file.",
231e509ee18Sopenharmony_ci                "gn_targets_depend_on": [],  # not need, depend on deps of itself in "*.gn*" file
232e509ee18Sopenharmony_ci            },
233e509ee18Sopenharmony_ci        },
234e509ee18Sopenharmony_ci        "option": {
235e509ee18Sopenharmony_ci            "clean": {
236e509ee18Sopenharmony_ci                "flags": ["--clean", "-clean"],
237e509ee18Sopenharmony_ci                "description":
238e509ee18Sopenharmony_ci                    "Clean the root-out-dir(x64.release-->out/x64.release) execept for file args.gn. "
239e509ee18Sopenharmony_ci                    "Then exit.",
240e509ee18Sopenharmony_ci            },
241e509ee18Sopenharmony_ci            "clean-continue": {
242e509ee18Sopenharmony_ci                "flags": ["--clean-continue", "-clean-continue"],
243e509ee18Sopenharmony_ci                "description":
244e509ee18Sopenharmony_ci                    "Clean the root-out-dir(x64.release-->out/x64.release) execept for file args.gn. "
245e509ee18Sopenharmony_ci                    "Then continue to build.",
246e509ee18Sopenharmony_ci            },
247e509ee18Sopenharmony_ci            "gn-args": {
248e509ee18Sopenharmony_ci                "flags": ["--gn-args=*", "-gn-args=*"],
249e509ee18Sopenharmony_ci                "description":
250e509ee18Sopenharmony_ci                    "Pass args(*) to gn command. Example: python3 ark.py x64.release "
251e509ee18Sopenharmony_ci                    "--gn-args=\"bool_declared_in_src_gn=true string_declared_in_src_gn=\\\"abcd\\\" "
252e509ee18Sopenharmony_ci                    "list_declared_in_src_gn=[ \\\"element0\\\", \\\"element1\\\" ] print(list_declared_in_src_gn) "
253e509ee18Sopenharmony_ci                    "exec_script(\\\"script_in_src\\\", [ \\\"arg_to_script\\\" ])\"  .",
254e509ee18Sopenharmony_ci            },
255e509ee18Sopenharmony_ci            "keepdepfile": {
256e509ee18Sopenharmony_ci                "flags": ["--keepdepfile", "-keepdepfile"],
257e509ee18Sopenharmony_ci                "description":
258e509ee18Sopenharmony_ci                    "Keep depfile(\"*.o.d\") generated by commands(CXX, CC ...) called by ninja during compilation.",
259e509ee18Sopenharmony_ci            },
260e509ee18Sopenharmony_ci            "verbose": {
261e509ee18Sopenharmony_ci                "flags": ["--verbose", "-verbose"],
262e509ee18Sopenharmony_ci                "description": "Print full commands(CXX, CC, LINK ...) called by ninja during compilation.",
263e509ee18Sopenharmony_ci            },
264e509ee18Sopenharmony_ci            "keep-going": {
265e509ee18Sopenharmony_ci                "flags": ["--keep-going=*", "-keep-going=*"],
266e509ee18Sopenharmony_ci                "description": "Keep running unittest etc. until errors occured less than N times"
267e509ee18Sopenharmony_ci                               " (use 0 to ignore all errors).",
268e509ee18Sopenharmony_ci            },
269e509ee18Sopenharmony_ci        },
270e509ee18Sopenharmony_ci        "help": {
271e509ee18Sopenharmony_ci            "flags": ["help", "--help", "--h", "-help", "-h"],
272e509ee18Sopenharmony_ci            "description": "Show the usage of ark.py.",
273e509ee18Sopenharmony_ci        },
274e509ee18Sopenharmony_ci    }
275e509ee18Sopenharmony_ci
276e509ee18Sopenharmony_ci    # variables which would change with the change of host_os or host_cpu
277e509ee18Sopenharmony_ci    gn_binary_path = ""
278e509ee18Sopenharmony_ci    ninja_binary_path = ""
279e509ee18Sopenharmony_ci
280e509ee18Sopenharmony_ci    # variables which would change with the change of ark.py command
281e509ee18Sopenharmony_ci    has_cleaned = False
282e509ee18Sopenharmony_ci    enable_verbose = False
283e509ee18Sopenharmony_ci    enable_keepdepfile = False
284e509ee18Sopenharmony_ci    ignore_errors = 1
285e509ee18Sopenharmony_ci
286e509ee18Sopenharmony_ci    def __main__(self, arg_list: list):
287e509ee18Sopenharmony_ci        enable_ccache()
288e509ee18Sopenharmony_ci        # delete duplicate arg in arg_list
289e509ee18Sopenharmony_ci        arg_list = list(dict.fromkeys(arg_list))
290e509ee18Sopenharmony_ci        # match [help] flag
291e509ee18Sopenharmony_ci        if len(arg_list) == 0 or (
292e509ee18Sopenharmony_ci                True in [self.is_dict_flags_match_arg(self.ARG_DICT.get("help"), arg) for arg in arg_list]):
293e509ee18Sopenharmony_ci            print(self.get_help_msg_of_all())
294e509ee18Sopenharmony_ci            return
295e509ee18Sopenharmony_ci        # match [[os_cpu].[mode]] flag
296e509ee18Sopenharmony_ci        [match_success, key_to_dict_in_os_cpu, key_to_dict_in_mode] = self.dict_in_os_cpu_mode_match_arg(arg_list[0])
297e509ee18Sopenharmony_ci        if match_success:
298e509ee18Sopenharmony_ci            self.start_for_matched_os_cpu_mode(key_to_dict_in_os_cpu, key_to_dict_in_mode, arg_list[1:])
299e509ee18Sopenharmony_ci        else:
300e509ee18Sopenharmony_ci            print("\033[92mThe command is not supported! Help message shows below.\033[0m\n{}".format(
301e509ee18Sopenharmony_ci                self.get_help_msg_of_all()))
302e509ee18Sopenharmony_ci        return
303e509ee18Sopenharmony_ci
304e509ee18Sopenharmony_ci    @staticmethod
305e509ee18Sopenharmony_ci    def is_dict_flags_match_arg(dict_to_match: dict, arg_to_match: str) -> bool:
306e509ee18Sopenharmony_ci        for flag in dict_to_match["flags"]:
307e509ee18Sopenharmony_ci            if fnmatch(arg_to_match, flag):
308e509ee18Sopenharmony_ci                return True
309e509ee18Sopenharmony_ci        return False
310e509ee18Sopenharmony_ci
311e509ee18Sopenharmony_ci    @staticmethod
312e509ee18Sopenharmony_ci    def libs_dir(is_arm, is_aot, is_pgo, out_dir, x64_out_dir) -> str:
313e509ee18Sopenharmony_ci        if is_arm and is_aot and is_pgo:
314e509ee18Sopenharmony_ci            return (f"--libs-dir ../../{out_dir}/arkcompiler/ets_runtime:"
315e509ee18Sopenharmony_ci                    f"../../{out_dir}/thirdparty/icu:"
316e509ee18Sopenharmony_ci                    f"../../{out_dir}/third_party/icu:"
317e509ee18Sopenharmony_ci                    f"../../thirdparty/zlib:"
318e509ee18Sopenharmony_ci                    f"../../prebuilts/clang/ohos/linux-x86_64/llvm/lib")
319e509ee18Sopenharmony_ci        if is_arm and is_aot and not is_pgo:
320e509ee18Sopenharmony_ci            return ("--libs-dir ../../prebuilts/clang/ohos/linux-x86_64/llvm/lib"
321e509ee18Sopenharmony_ci                    f":../../{x64_out_dir}/thirdparty/icu/")
322e509ee18Sopenharmony_ci        if not is_arm and is_aot:
323e509ee18Sopenharmony_ci            return (f"--libs-dir ../../{out_dir}/arkcompiler/ets_runtime"
324e509ee18Sopenharmony_ci                    f":../../{out_dir}/thirdparty/icu:"
325e509ee18Sopenharmony_ci                    f"../../{out_dir}/third_party/icu:"
326e509ee18Sopenharmony_ci                    f"../../thirdparty/zlib:"
327e509ee18Sopenharmony_ci                    f"../../prebuilts/clang/ohos/linux-x86_64/llvm/lib")
328e509ee18Sopenharmony_ci        # not is_arm and not is_aot
329e509ee18Sopenharmony_ci        return " --libs-dir ../../prebuilts/clang/ohos/linux-x86_64/llvm/lib"
330e509ee18Sopenharmony_ci
331e509ee18Sopenharmony_ci    @staticmethod
332e509ee18Sopenharmony_ci    def get_cmd(test_suite, test_script_name, test_script_path, gn_args, out_path, x64_out_path, aot_mode, run_pgo,
333e509ee18Sopenharmony_ci                enable_litecg, args_to_cmd, timeout, ignore_list: Optional[str] = None):
334e509ee18Sopenharmony_ci        cmd = [
335e509ee18Sopenharmony_ci            f"cd {test_script_path}",
336e509ee18Sopenharmony_ci            f"&& python3 {test_script_name} {args_to_cmd}",
337e509ee18Sopenharmony_ci            f"--timeout {timeout}",
338e509ee18Sopenharmony_ci            f"--ark-tool=../../{out_path}/arkcompiler/ets_runtime/ark_js_vm",
339e509ee18Sopenharmony_ci            "--ark-frontend=es2panda"
340e509ee18Sopenharmony_ci        ]
341e509ee18Sopenharmony_ci        is_arm = any('target_cpu="arm64"' in arg for arg in gn_args)
342e509ee18Sopenharmony_ci        if is_arm:
343e509ee18Sopenharmony_ci            cmd.append("--ark-arch aarch64")
344e509ee18Sopenharmony_ci            cmd.append(f"--ark-arch-root=../../{out_path}/common/common/libc/")
345e509ee18Sopenharmony_ci            cmd.append(f"--ark-frontend-binary=../../{x64_out_path}/arkcompiler/ets_frontend/es2abc")
346e509ee18Sopenharmony_ci            cmd.append(f"--merge-abc-binary=../../{x64_out_path}/arkcompiler/ets_frontend/merge_abc")
347e509ee18Sopenharmony_ci            if aot_mode:
348e509ee18Sopenharmony_ci                cmd.append(f"--ark-aot-tool=../../{out_path}/arkcompiler/ets_runtime/ark_aot_compiler")
349e509ee18Sopenharmony_ci                if test_suite == "regresstest":
350e509ee18Sopenharmony_ci                    cmd.append(f"--stub-path=../../{out_path}/gen/arkcompiler/ets_runtime/stub.an")
351e509ee18Sopenharmony_ci        else:
352e509ee18Sopenharmony_ci            cmd.append(f"--ark-frontend-binary=../../{out_path}/arkcompiler/ets_frontend/es2abc")
353e509ee18Sopenharmony_ci            cmd.append(f"--merge-abc-binary=../../{out_path}/arkcompiler/ets_frontend/merge_abc")
354e509ee18Sopenharmony_ci            if aot_mode:
355e509ee18Sopenharmony_ci                cmd.append(f"--ark-aot-tool=../../{out_path}/arkcompiler/ets_runtime/ark_aot_compiler")
356e509ee18Sopenharmony_ci                if test_suite == "regresstest":
357e509ee18Sopenharmony_ci                    cmd.append(f"--stub-path=../../{out_path}/gen/arkcompiler/ets_runtime/stub.an")
358e509ee18Sopenharmony_ci
359e509ee18Sopenharmony_ci        cmd.append(ArkPy.libs_dir(
360e509ee18Sopenharmony_ci            is_arm=is_arm,
361e509ee18Sopenharmony_ci            is_aot=aot_mode,
362e509ee18Sopenharmony_ci            is_pgo=run_pgo,
363e509ee18Sopenharmony_ci            out_dir=out_path,
364e509ee18Sopenharmony_ci            x64_out_dir=x64_out_path
365e509ee18Sopenharmony_ci        ))
366e509ee18Sopenharmony_ci
367e509ee18Sopenharmony_ci        if aot_mode:
368e509ee18Sopenharmony_ci            cmd.append("--ark-aot")
369e509ee18Sopenharmony_ci            mode = ["AOT"]
370e509ee18Sopenharmony_ci            if run_pgo:
371e509ee18Sopenharmony_ci                cmd.append("--run-pgo")
372e509ee18Sopenharmony_ci                mode.append("PGO")
373e509ee18Sopenharmony_ci            if enable_litecg:
374e509ee18Sopenharmony_ci                cmd.append("--enable-litecg")
375e509ee18Sopenharmony_ci                mode.append("LiteCG")
376e509ee18Sopenharmony_ci            mode_str = " ".join(mode)
377e509ee18Sopenharmony_ci            print(f"Running {test_suite} in {mode_str} Mode\n")
378e509ee18Sopenharmony_ci
379e509ee18Sopenharmony_ci        if test_suite == "regresstest" and ignore_list:
380e509ee18Sopenharmony_ci            cmd.append(f"--ignore-list {ignore_list}")
381e509ee18Sopenharmony_ci
382e509ee18Sopenharmony_ci        if test_suite == "regresstest":
383e509ee18Sopenharmony_ci            cmd.append(f"--out-dir ../../{out_path}")
384e509ee18Sopenharmony_ci
385e509ee18Sopenharmony_ci        return " ".join(cmd)
386e509ee18Sopenharmony_ci
387e509ee18Sopenharmony_ci    @staticmethod
388e509ee18Sopenharmony_ci    def get_test262_aot_cmd(gn_args, out_path, x64_out_path, run_pgo, enable_litecg, args_to_test262_cmd,
389e509ee18Sopenharmony_ci                            timeout):
390e509ee18Sopenharmony_ci        print("running test262 in AotMode\n")
391e509ee18Sopenharmony_ci        if any('target_cpu="arm64"' in arg for arg in gn_args):
392e509ee18Sopenharmony_ci            if run_pgo:
393e509ee18Sopenharmony_ci                test262_cmd = f"cd arkcompiler/ets_frontend && python3 test262/run_test262.py {args_to_test262_cmd}" \
394e509ee18Sopenharmony_ci                              f" --timeout {timeout}" \
395e509ee18Sopenharmony_ci                              f" --libs-dir ../../{out_path}/arkcompiler/ets_runtime:../../{out_path}/thirdparty/icu:" \
396e509ee18Sopenharmony_ci                              f"../../{out_path}/thirdparty/zlib:../../prebuilts/clang/ohos/linux-x86_64/llvm/lib" \
397e509ee18Sopenharmony_ci                              " --ark-arch aarch64" \
398e509ee18Sopenharmony_ci                              f" --ark-arch-root=../../{out_path}/common/common/libc/" \
399e509ee18Sopenharmony_ci                              f" --ark-tool=../../{out_path}/arkcompiler/ets_runtime/ark_js_vm" \
400e509ee18Sopenharmony_ci                              f" --ark-aot-tool=../../{out_path}/arkcompiler/ets_runtime/ark_aot_compiler" \
401e509ee18Sopenharmony_ci                              f" --ark-frontend-binary=../../{x64_out_path}/arkcompiler/ets_frontend/es2abc" \
402e509ee18Sopenharmony_ci                              f" --merge-abc-binary=../../{x64_out_path}/arkcompiler/ets_frontend/merge_abc" \
403e509ee18Sopenharmony_ci                              " --ark-aot" \
404e509ee18Sopenharmony_ci                              " --ark-frontend=es2panda" \
405e509ee18Sopenharmony_ci                              " --run-pgo"
406e509ee18Sopenharmony_ci            else:
407e509ee18Sopenharmony_ci                test262_cmd = "cd arkcompiler/ets_frontend && python3 test262/run_test262.py {0} --timeout {3}" \
408e509ee18Sopenharmony_ci                              " --libs-dir ../../prebuilts/clang/ohos/linux-x86_64/llvm/lib:../../{2}/thirdparty/icu/" \
409e509ee18Sopenharmony_ci                              " --ark-arch aarch64" \
410e509ee18Sopenharmony_ci                              " --ark-arch-root=../../{1}/common/common/libc/" \
411e509ee18Sopenharmony_ci                              " --ark-aot" \
412e509ee18Sopenharmony_ci                              " --ark-aot-tool=../../{2}/arkcompiler/ets_runtime/ark_aot_compiler" \
413e509ee18Sopenharmony_ci                              " --ark-tool=../../{1}/arkcompiler/ets_runtime/ark_js_vm" \
414e509ee18Sopenharmony_ci                              " --ark-frontend-binary=../../{2}/arkcompiler/ets_frontend/es2abc" \
415e509ee18Sopenharmony_ci                              " --merge-abc-binary=../../{2}/arkcompiler/ets_frontend/merge_abc" \
416e509ee18Sopenharmony_ci                              " --ark-frontend=es2panda".format(args_to_test262_cmd, out_path, x64_out_path, timeout)
417e509ee18Sopenharmony_ci        else:
418e509ee18Sopenharmony_ci            run_pgo_arg = " --run-pgo" if run_pgo else ""
419e509ee18Sopenharmony_ci            test262_cmd = f"cd arkcompiler/ets_frontend && python3 test262/run_test262.py {args_to_test262_cmd}" \
420e509ee18Sopenharmony_ci                          f" --timeout {timeout}" \
421e509ee18Sopenharmony_ci                          f" --libs-dir ../../{out_path}/arkcompiler/ets_runtime:../../{out_path}/thirdparty/icu" \
422e509ee18Sopenharmony_ci                          f":../../{out_path}/thirdparty/zlib:../../prebuilts/clang/ohos/linux-x86_64/llvm/lib" \
423e509ee18Sopenharmony_ci                          f" --ark-tool=../../{out_path}/arkcompiler/ets_runtime/ark_js_vm" \
424e509ee18Sopenharmony_ci                          f" --ark-aot-tool=../../{out_path}/arkcompiler/ets_runtime/ark_aot_compiler" \
425e509ee18Sopenharmony_ci                          f" --ark-frontend-binary=../../{out_path}/arkcompiler/ets_frontend/es2abc" \
426e509ee18Sopenharmony_ci                          f" --merge-abc-binary=../../{out_path}/arkcompiler/ets_frontend/merge_abc" \
427e509ee18Sopenharmony_ci                          " --ark-aot" \
428e509ee18Sopenharmony_ci                          " --ark-frontend=es2panda" \
429e509ee18Sopenharmony_ci                          f" {run_pgo_arg}"
430e509ee18Sopenharmony_ci        if enable_litecg:
431e509ee18Sopenharmony_ci            test262_cmd = test262_cmd + " --enable-litecg"
432e509ee18Sopenharmony_ci        return test262_cmd
433e509ee18Sopenharmony_ci
434e509ee18Sopenharmony_ci    @staticmethod
435e509ee18Sopenharmony_ci    def get_jit_cmd(test_suite, test_script_name, test_script_path, gn_args, out_path, x64_out_path, args_to_cmd,
436e509ee18Sopenharmony_ci                    timeout):
437e509ee18Sopenharmony_ci        print(f"running {test_suite} in JIT mode\n")
438e509ee18Sopenharmony_ci        if any('target_cpu="arm64"' in arg for arg in gn_args):
439e509ee18Sopenharmony_ci            cmd = f"cd {test_script_path} && python3 {test_script_name} {args_to_cmd} --timeout {timeout}" \
440e509ee18Sopenharmony_ci                  f" --libs-dir ../../prebuilts/clang/ohos/linux-x86_64/llvm/lib:../../{out_path}/thirdparty/icu/" \
441e509ee18Sopenharmony_ci                  f":../../{out_path}/thirdparty/bounds_checking_function" \
442e509ee18Sopenharmony_ci                  f":../../{out_path}/arkcompiler/ets_runtime:" \
443e509ee18Sopenharmony_ci                  " --ark-arch aarch64" \
444e509ee18Sopenharmony_ci                  " --run-jit" \
445e509ee18Sopenharmony_ci                  f" --ark-arch-root=../../{out_path}/common/common/libc/" \
446e509ee18Sopenharmony_ci                  f" --ark-aot-tool=../../{x64_out_path}/arkcompiler/ets_runtime/ark_aot_compiler" \
447e509ee18Sopenharmony_ci                  f" --ark-tool=../../{out_path}/arkcompiler/ets_runtime/ark_js_vm" \
448e509ee18Sopenharmony_ci                  f" --ark-frontend-binary=../../{x64_out_path}/arkcompiler/ets_frontend/es2abc" \
449e509ee18Sopenharmony_ci                  f" --merge-abc-binary=../../{x64_out_path}/arkcompiler/ets_frontend/merge_abc" \
450e509ee18Sopenharmony_ci                  " --ark-frontend=es2panda"
451e509ee18Sopenharmony_ci        else:
452e509ee18Sopenharmony_ci            cmd = f"cd arkcompiler/ets_frontend && python3 {test_script_name} {args_to_cmd} --timeout {timeout}" \
453e509ee18Sopenharmony_ci                  f" --libs-dir ../../{out_path}/arkcompiler/ets_runtime:../../{out_path}/thirdparty/icu" \
454e509ee18Sopenharmony_ci                  f":../../{out_path}/thirdparty/zlib:../../prebuilts/clang/ohos/linux-x86_64/llvm/lib" \
455e509ee18Sopenharmony_ci                  " --run-jit" \
456e509ee18Sopenharmony_ci                  f" --ark-tool=../../{out_path}/arkcompiler/ets_runtime/ark_js_vm" \
457e509ee18Sopenharmony_ci                  f" --ark-frontend-binary=../../{out_path}/arkcompiler/ets_frontend/es2abc" \
458e509ee18Sopenharmony_ci                  f" --merge-abc-binary=../../{out_path}/arkcompiler/ets_frontend/merge_abc" \
459e509ee18Sopenharmony_ci                  " --ark-frontend=es2panda"
460e509ee18Sopenharmony_ci        return cmd
461e509ee18Sopenharmony_ci
462e509ee18Sopenharmony_ci    @staticmethod
463e509ee18Sopenharmony_ci    def get_baseline_jit_cmd(test_suite, test_script_name, test_script_path, gn_args, out_path, x64_out_path,
464e509ee18Sopenharmony_ci                             args_to_test262_cmd, timeout):
465e509ee18Sopenharmony_ci        print(f"running {test_suite} in baseline JIT mode\n")
466e509ee18Sopenharmony_ci        if any('target_cpu="arm64"' in arg for arg in gn_args):
467e509ee18Sopenharmony_ci            cmd = f"cd {test_script_path} && python3 {test_script_name} {args_to_test262_cmd} --timeout {timeout}" \
468e509ee18Sopenharmony_ci                  f" --libs-dir ../../prebuilts/clang/ohos/linux-x86_64/llvm/lib" \
469e509ee18Sopenharmony_ci                  f":../../{out_path}/thirdparty/icu" \
470e509ee18Sopenharmony_ci                  f":../../prebuilts/clang/ohos/linux-x86_64/llvm/lib/aarch64-linux-ohos" \
471e509ee18Sopenharmony_ci                  f":../../{out_path}/thirdparty/bounds_checking_function" \
472e509ee18Sopenharmony_ci                  f":../../{out_path}/arkcompiler/ets_runtime" \
473e509ee18Sopenharmony_ci                  f":../../{out_path}/common/common/libc/lib" \
474e509ee18Sopenharmony_ci                  " --ark-arch aarch64" \
475e509ee18Sopenharmony_ci                  " --run-baseline-jit" \
476e509ee18Sopenharmony_ci                  f" --ark-arch-root=../../{out_path}/common/common/libc/" \
477e509ee18Sopenharmony_ci                  f" --ark-aot-tool=../../{x64_out_path}/arkcompiler/ets_runtime/ark_aot_compiler" \
478e509ee18Sopenharmony_ci                  f" --ark-tool=../../{out_path}/arkcompiler/ets_runtime/ark_js_vm" \
479e509ee18Sopenharmony_ci                  f" --ark-frontend-binary=../../{x64_out_path}/arkcompiler/ets_frontend/es2abc" \
480e509ee18Sopenharmony_ci                  f" --merge-abc-binary=../../{x64_out_path}/arkcompiler/ets_frontend/merge_abc" \
481e509ee18Sopenharmony_ci                  " --ark-frontend=es2panda"
482e509ee18Sopenharmony_ci        else:
483e509ee18Sopenharmony_ci            cmd = f"cd {test_script_path} && python3 {test_script_name} {args_to_test262_cmd} --timeout {timeout}" \
484e509ee18Sopenharmony_ci                  f" --libs-dir ../../{out_path}/lib.unstripped/arkcompiler/ets_runtime" \
485e509ee18Sopenharmony_ci                  f":../../{out_path}/thirdparty/icu" \
486e509ee18Sopenharmony_ci                  ":../../prebuilts/clang/ohos/linux-x86_64/llvm/lib" \
487e509ee18Sopenharmony_ci                  f":../../{out_path}/thirdparty/bounds_checking_function/" \
488e509ee18Sopenharmony_ci                  " --run-baseline-jit" \
489e509ee18Sopenharmony_ci                  f" --ark-tool=../../{out_path}/arkcompiler/ets_runtime/ark_js_vm" \
490e509ee18Sopenharmony_ci                  f" --ark-frontend-binary=../../{out_path}/arkcompiler/ets_frontend/es2abc" \
491e509ee18Sopenharmony_ci                  f" --merge-abc-binary=../../{out_path}/arkcompiler/ets_frontend/merge_abc" \
492e509ee18Sopenharmony_ci                  " --ark-frontend=es2panda"
493e509ee18Sopenharmony_ci        return cmd
494e509ee18Sopenharmony_ci
495e509ee18Sopenharmony_ci    @staticmethod
496e509ee18Sopenharmony_ci    def build_args_to_test262_cmd(arg_list):
497e509ee18Sopenharmony_ci        args_to_test262_cmd = []
498e509ee18Sopenharmony_ci
499e509ee18Sopenharmony_ci        disable_force_gc_name = "--disable-force-gc"
500e509ee18Sopenharmony_ci        disable_force_gc_value, arg_list = ArkPy.parse_bool_option(
501e509ee18Sopenharmony_ci            arg_list, option_name=disable_force_gc_name, default_value=False
502e509ee18Sopenharmony_ci        )
503e509ee18Sopenharmony_ci        if disable_force_gc_value:
504e509ee18Sopenharmony_ci            args_to_test262_cmd.extend([disable_force_gc_name])
505e509ee18Sopenharmony_ci
506e509ee18Sopenharmony_ci        threads_name = "--threads"
507e509ee18Sopenharmony_ci        threads_value, arg_list = ArkPy.parse_option(arg_list, option_name=threads_name, default_value=None)
508e509ee18Sopenharmony_ci        if threads_value:
509e509ee18Sopenharmony_ci            args_to_test262_cmd.extend([threads_name, threads_value])
510e509ee18Sopenharmony_ci
511e509ee18Sopenharmony_ci        test_list_name = "--test-list"
512e509ee18Sopenharmony_ci        test_list_value, arg_list = ArkPy.parse_option(arg_list, option_name=test_list_name, default_value=None)
513e509ee18Sopenharmony_ci        if test_list_value is not None:
514e509ee18Sopenharmony_ci            args_to_test262_cmd.extend([test_list_name, test_list_value])
515e509ee18Sopenharmony_ci
516e509ee18Sopenharmony_ci        enable_rm = [arg for arg in arg_list if "enable-rm" in arg]
517e509ee18Sopenharmony_ci        if enable_rm:
518e509ee18Sopenharmony_ci            args_to_test262_cmd.append("--enable-rm")
519e509ee18Sopenharmony_ci            arg_list.remove(enable_rm[0])
520e509ee18Sopenharmony_ci
521e509ee18Sopenharmony_ci        skip_list_name = "--skip-list"
522e509ee18Sopenharmony_ci        skip_list_value, arg_list = ArkPy.parse_option(arg_list, option_name=skip_list_name, default_value=None)
523e509ee18Sopenharmony_ci        if skip_list_value is not None:
524e509ee18Sopenharmony_ci            args_to_test262_cmd.extend([skip_list_name, skip_list_value])
525e509ee18Sopenharmony_ci
526e509ee18Sopenharmony_ci        if len(arg_list) == 0:
527e509ee18Sopenharmony_ci            args_to_test262_cmd.append("--es2021 all")
528e509ee18Sopenharmony_ci        elif len(arg_list) == 1:
529e509ee18Sopenharmony_ci            arg = arg_list[0]
530e509ee18Sopenharmony_ci            if arg == "sendable":
531e509ee18Sopenharmony_ci                args_to_test262_cmd.append("--sendable sendable")
532e509ee18Sopenharmony_ci            elif ".js" in arg:
533e509ee18Sopenharmony_ci                args_to_test262_cmd.append("--file test262/data/test_es2021/{}".format(arg))
534e509ee18Sopenharmony_ci            else:
535e509ee18Sopenharmony_ci                args_to_test262_cmd.append("--dir test262/data/test_es2021/{}".format(arg))
536e509ee18Sopenharmony_ci        else:
537e509ee18Sopenharmony_ci            print("\033[92m\"test262\" not support multiple additional arguments.\033[0m\n".format())
538e509ee18Sopenharmony_ci            sys.exit(0)
539e509ee18Sopenharmony_ci
540e509ee18Sopenharmony_ci        return " ".join(args_to_test262_cmd)
541e509ee18Sopenharmony_ci
542e509ee18Sopenharmony_ci    @staticmethod
543e509ee18Sopenharmony_ci    def build_args_to_regress_cmd(arg_list):
544e509ee18Sopenharmony_ci        args_to_regress_cmd = []
545e509ee18Sopenharmony_ci
546e509ee18Sopenharmony_ci        disable_force_gc_name = "--disable-force-gc"
547e509ee18Sopenharmony_ci        disable_force_gc_value, arg_list = ArkPy.parse_bool_option(
548e509ee18Sopenharmony_ci            arg_list, option_name=disable_force_gc_name, default_value=False
549e509ee18Sopenharmony_ci        )
550e509ee18Sopenharmony_ci        if disable_force_gc_value:
551e509ee18Sopenharmony_ci            args_to_regress_cmd.extend([disable_force_gc_name])
552e509ee18Sopenharmony_ci
553e509ee18Sopenharmony_ci        processes_name = "--processes"
554e509ee18Sopenharmony_ci        processes_value, arg_list = ArkPy.parse_option(arg_list, option_name=processes_name, default_value=1)
555e509ee18Sopenharmony_ci        args_to_regress_cmd.extend([processes_name, processes_value])
556e509ee18Sopenharmony_ci
557e509ee18Sopenharmony_ci        test_list_name = "--test-list"
558e509ee18Sopenharmony_ci        test_list_value, arg_list = ArkPy.parse_option(arg_list, option_name=test_list_name, default_value=None)
559e509ee18Sopenharmony_ci        if test_list_value is not None:
560e509ee18Sopenharmony_ci            args_to_regress_cmd.extend([test_list_name, test_list_value])
561e509ee18Sopenharmony_ci
562e509ee18Sopenharmony_ci        if len(arg_list) == 1:
563e509ee18Sopenharmony_ci            arg = arg_list[0]
564e509ee18Sopenharmony_ci            if ".js" in arg:
565e509ee18Sopenharmony_ci                args_to_regress_cmd.append(f"--test-file {arg}")
566e509ee18Sopenharmony_ci            else:
567e509ee18Sopenharmony_ci                args_to_regress_cmd.append(f"--test-dir {arg}")
568e509ee18Sopenharmony_ci        elif len(arg_list) > 1:
569e509ee18Sopenharmony_ci            print("\033[92m\"regresstest\" not support multiple additional arguments.\033[0m\n".format())
570e509ee18Sopenharmony_ci            sys.exit(0)
571e509ee18Sopenharmony_ci
572e509ee18Sopenharmony_ci        return " ".join([str(arg) for arg in args_to_regress_cmd])
573e509ee18Sopenharmony_ci
574e509ee18Sopenharmony_ci    @staticmethod
575e509ee18Sopenharmony_ci    def parse_option(arg_list: List[str], option_name: str, default_value: Optional[Union[str, int]]) \
576e509ee18Sopenharmony_ci            -> Tuple[Optional[Union[str, int]], List[str]]:
577e509ee18Sopenharmony_ci        option_value, arg_list = ArkPy.__parse_option_with_space(arg_list, option_name)
578e509ee18Sopenharmony_ci        if option_value is None:
579e509ee18Sopenharmony_ci            option_value, arg_list = ArkPy.__parse_option_with_equal(arg_list, option_name)
580e509ee18Sopenharmony_ci        if option_value is None and default_value is not None:
581e509ee18Sopenharmony_ci            option_value = default_value
582e509ee18Sopenharmony_ci        return option_value, arg_list
583e509ee18Sopenharmony_ci
584e509ee18Sopenharmony_ci    @staticmethod
585e509ee18Sopenharmony_ci    def parse_bool_option(arg_list: List[str], option_name: str, default_value: bool) \
586e509ee18Sopenharmony_ci            -> Tuple[bool, List[str]]:
587e509ee18Sopenharmony_ci        if option_name in arg_list:
588e509ee18Sopenharmony_ci            option_index = arg_list.index(option_name)
589e509ee18Sopenharmony_ci            option_value = not default_value
590e509ee18Sopenharmony_ci            arg_list = arg_list[:option_index] + arg_list[option_index + 1:]
591e509ee18Sopenharmony_ci        else:
592e509ee18Sopenharmony_ci            option_value = default_value
593e509ee18Sopenharmony_ci
594e509ee18Sopenharmony_ci        return option_value, arg_list
595e509ee18Sopenharmony_ci
596e509ee18Sopenharmony_ci    @staticmethod
597e509ee18Sopenharmony_ci    def __is_option_value_int(value: Optional[Union[str, int]]) -> Tuple[bool, Optional[int]]:
598e509ee18Sopenharmony_ci        if isinstance(value, int):
599e509ee18Sopenharmony_ci            return True, int(value)
600e509ee18Sopenharmony_ci        else:
601e509ee18Sopenharmony_ci            return False, None
602e509ee18Sopenharmony_ci
603e509ee18Sopenharmony_ci    @staticmethod
604e509ee18Sopenharmony_ci    def __is_option_value_str(value: Optional[Union[str, int]]) -> Tuple[bool, Optional[str]]:
605e509ee18Sopenharmony_ci        if isinstance(value, str):
606e509ee18Sopenharmony_ci            return True, str(value)
607e509ee18Sopenharmony_ci        else:
608e509ee18Sopenharmony_ci            return False, None
609e509ee18Sopenharmony_ci
610e509ee18Sopenharmony_ci    @staticmethod
611e509ee18Sopenharmony_ci    def __get_option_value(option_name: str, value: Optional[Union[str, int]]) -> Union[str, int]:
612e509ee18Sopenharmony_ci        result, res_value = ArkPy.__is_option_value_int(value)
613e509ee18Sopenharmony_ci        if result:
614e509ee18Sopenharmony_ci            return res_value
615e509ee18Sopenharmony_ci        result, res_value = ArkPy.__is_option_value_str(value)
616e509ee18Sopenharmony_ci        if result:
617e509ee18Sopenharmony_ci            return res_value
618e509ee18Sopenharmony_ci        print(f"Invalid '{option_name}' value.")
619e509ee18Sopenharmony_ci        sys.exit(1)
620e509ee18Sopenharmony_ci
621e509ee18Sopenharmony_ci    @staticmethod
622e509ee18Sopenharmony_ci    def __parse_option_with_space(arg_list: List[str], option_name: str) \
623e509ee18Sopenharmony_ci            -> Tuple[Optional[Union[str, int]], List[str]]:
624e509ee18Sopenharmony_ci        if option_name in arg_list:
625e509ee18Sopenharmony_ci            option_index = arg_list.index(option_name)
626e509ee18Sopenharmony_ci            if len(arg_list) > option_index + 1:
627e509ee18Sopenharmony_ci                option_value = ArkPy.__get_option_value(option_name, arg_list[option_index + 1])
628e509ee18Sopenharmony_ci                arg_list = arg_list[:option_index] + arg_list[option_index + 2:]
629e509ee18Sopenharmony_ci            else:
630e509ee18Sopenharmony_ci                print(f"Missing {option_name} value.")
631e509ee18Sopenharmony_ci                sys.exit(1)
632e509ee18Sopenharmony_ci
633e509ee18Sopenharmony_ci            return option_value, arg_list
634e509ee18Sopenharmony_ci        return None, arg_list
635e509ee18Sopenharmony_ci
636e509ee18Sopenharmony_ci    @staticmethod
637e509ee18Sopenharmony_ci    def __parse_option_with_equal(arg_list: List[str], option_name: str) \
638e509ee18Sopenharmony_ci            -> Tuple[Optional[Union[str, int]], List[str]]:
639e509ee18Sopenharmony_ci        for index, arg in enumerate(arg_list):
640e509ee18Sopenharmony_ci            local_option_name = f"{option_name}="
641e509ee18Sopenharmony_ci            if arg.startswith(local_option_name):
642e509ee18Sopenharmony_ci                option_value = arg[len(local_option_name):]
643e509ee18Sopenharmony_ci                if option_value:
644e509ee18Sopenharmony_ci                    option_value = ArkPy.__get_option_value(option_name, option_value)
645e509ee18Sopenharmony_ci                    arg_list = arg_list[:index] + arg_list[index + 1:]
646e509ee18Sopenharmony_ci                    return option_value, arg_list
647e509ee18Sopenharmony_ci                else:
648e509ee18Sopenharmony_ci                    print(f"Missing {option_name} value.")
649e509ee18Sopenharmony_ci                    sys.exit(1)
650e509ee18Sopenharmony_ci        return None, arg_list
651e509ee18Sopenharmony_ci
652e509ee18Sopenharmony_ci    @staticmethod
653e509ee18Sopenharmony_ci    def __get_x64_out_path(out_path) -> str:
654e509ee18Sopenharmony_ci        if 'release' in out_path:
655e509ee18Sopenharmony_ci            return 'out/x64.release'
656e509ee18Sopenharmony_ci        if 'debug' in out_path:
657e509ee18Sopenharmony_ci            return 'out/x64.debug'
658e509ee18Sopenharmony_ci        if 'fastverify' in out_path:
659e509ee18Sopenharmony_ci            return 'out/x64.fastverify'
660e509ee18Sopenharmony_ci        return ""
661e509ee18Sopenharmony_ci
662e509ee18Sopenharmony_ci    def get_binaries(self):
663e509ee18Sopenharmony_ci        host_os = sys.platform
664e509ee18Sopenharmony_ci        host_cpu = platform.machine()
665e509ee18Sopenharmony_ci        try:
666e509ee18Sopenharmony_ci            with open(self.PREBUILTS_DOWNLOAD_CONFIG_FILE_PATH) as file_prebuilts_download_config:
667e509ee18Sopenharmony_ci                prebuilts_download_config_dict = json.load(file_prebuilts_download_config)
668e509ee18Sopenharmony_ci                file_prebuilts_download_config.close()
669e509ee18Sopenharmony_ci            for element in prebuilts_download_config_dict[host_os][host_cpu]["copy_config"]:
670e509ee18Sopenharmony_ci                if element["unzip_filename"] == "gn":
671e509ee18Sopenharmony_ci                    self.gn_binary_path = os.path.join(element["unzip_dir"], element["unzip_filename"])
672e509ee18Sopenharmony_ci                elif element["unzip_filename"] == "ninja":
673e509ee18Sopenharmony_ci                    self.ninja_binary_path = os.path.join(element["unzip_dir"], element["unzip_filename"])
674e509ee18Sopenharmony_ci        except Exception as error:
675e509ee18Sopenharmony_ci            print("\nLogic of getting gn binary or ninja binary does not match logic of prebuilts_download." \
676e509ee18Sopenharmony_ci                  "\nCheck func \033[92m{0} of class {1} in file {2}\033[0m against file {3} if the name of this " \
677e509ee18Sopenharmony_ci                  "file had not changed!\n".format(
678e509ee18Sopenharmony_ci                sys._getframe().f_code.co_name, self.__class__.__name__, CURRENT_FILENAME,
679e509ee18Sopenharmony_ci                self.PREBUILTS_DOWNLOAD_CONFIG_FILE_PATH))
680e509ee18Sopenharmony_ci            raise error
681e509ee18Sopenharmony_ci        if self.gn_binary_path == "" or self.ninja_binary_path == "":
682e509ee18Sopenharmony_ci            print("\nLogic of prebuilts_download may be wrong." \
683e509ee18Sopenharmony_ci                  "\nCheck \033[92mdata in file {0}\033[0m against func {1} of class {2} in file {3}!\n".format(
684e509ee18Sopenharmony_ci                self.PREBUILTS_DOWNLOAD_CONFIG_FILE_PATH, sys._getframe().f_code.co_name, self.__class__.__name__,
685e509ee18Sopenharmony_ci                CURRENT_FILENAME))
686e509ee18Sopenharmony_ci            sys.exit(0)
687e509ee18Sopenharmony_ci        if not os.path.isfile(self.gn_binary_path) or not os.path.isfile(self.ninja_binary_path):
688e509ee18Sopenharmony_ci            print("\nStep for prebuilts_download may be ommited. (\033[92m./prebuilts_download.sh\033[0m)" \
689e509ee18Sopenharmony_ci                  "\nCheck \033[92mwhether gn binary and ninja binary are under directory prebuilts\033[0m!\n".format())
690e509ee18Sopenharmony_ci            sys.exit(0)
691e509ee18Sopenharmony_ci        return
692e509ee18Sopenharmony_ci
693e509ee18Sopenharmony_ci    def which_dict_flags_match_arg(self, dict_including_dicts_to_match: dict, arg_to_match: str) -> str:
694e509ee18Sopenharmony_ci        for key in dict_including_dicts_to_match.keys():
695e509ee18Sopenharmony_ci            if self.is_dict_flags_match_arg(dict_including_dicts_to_match[key], arg_to_match):
696e509ee18Sopenharmony_ci                return key
697e509ee18Sopenharmony_ci        return ""
698e509ee18Sopenharmony_ci
699e509ee18Sopenharmony_ci    def dict_in_os_cpu_mode_match_arg(self, arg: str) -> [bool, str, str]:
700e509ee18Sopenharmony_ci        os_cpu_part = ""
701e509ee18Sopenharmony_ci        mode_part = ""
702e509ee18Sopenharmony_ci        match_success = True
703e509ee18Sopenharmony_ci        key_to_dict_in_os_cpu_matched_arg = ""
704e509ee18Sopenharmony_ci        key_to_dict_in_mode_matched_arg = ""
705e509ee18Sopenharmony_ci        arg_to_list = arg.split(self.DELIMITER_BETWEEN_OS_CPU_MODE_FOR_COMMAND)
706e509ee18Sopenharmony_ci        if len(arg_to_list) == 1:
707e509ee18Sopenharmony_ci            os_cpu_part = arg_to_list[0]
708e509ee18Sopenharmony_ci            mode_part = "release"
709e509ee18Sopenharmony_ci            key_to_dict_in_os_cpu_matched_arg = self.which_dict_flags_match_arg(self.ARG_DICT.get("os_cpu"), os_cpu_part)
710e509ee18Sopenharmony_ci            key_to_dict_in_mode_matched_arg = self.which_dict_flags_match_arg(self.ARG_DICT.get("mode"), mode_part)
711e509ee18Sopenharmony_ci        elif len(arg_to_list) == 2:
712e509ee18Sopenharmony_ci            os_cpu_part = arg_to_list[0]
713e509ee18Sopenharmony_ci            mode_part = arg_to_list[1]
714e509ee18Sopenharmony_ci            key_to_dict_in_os_cpu_matched_arg = self.which_dict_flags_match_arg(self.ARG_DICT.get("os_cpu"), os_cpu_part)
715e509ee18Sopenharmony_ci            key_to_dict_in_mode_matched_arg = self.which_dict_flags_match_arg(self.ARG_DICT.get("mode"), mode_part)
716e509ee18Sopenharmony_ci        else:
717e509ee18Sopenharmony_ci            print("\"\033[92m{0}\033[0m\" combined with more than 2 flags is not supported.".format(arg))
718e509ee18Sopenharmony_ci        if (key_to_dict_in_os_cpu_matched_arg == "") | (key_to_dict_in_mode_matched_arg == ""):
719e509ee18Sopenharmony_ci            match_success = False
720e509ee18Sopenharmony_ci        return [match_success, key_to_dict_in_os_cpu_matched_arg, key_to_dict_in_mode_matched_arg]
721e509ee18Sopenharmony_ci
722e509ee18Sopenharmony_ci    def get_help_msg_of_dict(self, dict_in: dict, indentation_str_current: str, indentation_str_per_level: str) -> str:
723e509ee18Sopenharmony_ci        help_msg = "".format()
724e509ee18Sopenharmony_ci        for key in dict_in.keys():
725e509ee18Sopenharmony_ci            if isinstance(dict_in[key], dict):
726e509ee18Sopenharmony_ci                help_msg += "{0}{1}:\n".format(indentation_str_current, key)
727e509ee18Sopenharmony_ci                help_msg += self.get_help_msg_of_dict(
728e509ee18Sopenharmony_ci                    dict_in[key], indentation_str_current + indentation_str_per_level, indentation_str_per_level)
729e509ee18Sopenharmony_ci            elif key == "flags":
730e509ee18Sopenharmony_ci                help_msg += "{0}{1}: \033[92m{2}\033[0m\n".format(indentation_str_current, key, " ".join(dict_in[key]))
731e509ee18Sopenharmony_ci            elif key == "description":
732e509ee18Sopenharmony_ci                help_msg += "{0}{1}: {2}\n".format(indentation_str_current, key, dict_in[key])
733e509ee18Sopenharmony_ci        return help_msg
734e509ee18Sopenharmony_ci
735e509ee18Sopenharmony_ci    def get_help_msg_of_all(self) -> str:
736e509ee18Sopenharmony_ci        help_msg = "".format()
737e509ee18Sopenharmony_ci        # Command template
738e509ee18Sopenharmony_ci        help_msg += "\033[32mCommand template:\033[0m\n{}\n\n".format(
739e509ee18Sopenharmony_ci            "  python3 ark.py \033[92m[os_cpu].[mode] [gn_target] [option]\033[0m\n"
740e509ee18Sopenharmony_ci            "  python3 ark.py \033[92m[os_cpu].[mode] [test262] [none or --aot] " \
741e509ee18Sopenharmony_ci            "[none or --pgo] [none or --litecg] [none, file or dir] [none or --threads=X] [option]\033[0m\n"
742e509ee18Sopenharmony_ci            "  python3 ark.py \033[92m[os_cpu].[mode] [test262] [none or --jit] [none or --threads=X]\033[0m\n"
743e509ee18Sopenharmony_ci            "  python3 ark.py \033[92m[os_cpu].[mode] [test262] [none or --baseline-jit] [none or --enable-rm] " \
744e509ee18Sopenharmony_ci            "[none or --threads=X and/or --test-list TEST_LIST_NAME]\033[0m\n"
745e509ee18Sopenharmony_ci            "  python3 ark.py \033[92m[os_cpu].[mode] [unittest] [option]\033[0m\n"
746e509ee18Sopenharmony_ci            "  python3 ark.py \033[92m[os_cpu].[mode] [regresstest] [none, file or dir] " \
747e509ee18Sopenharmony_ci              "[none or --processes X and/or --test-list TEST_LIST_NAME]\033[0m\n")
748e509ee18Sopenharmony_ci        # Command examples
749e509ee18Sopenharmony_ci        help_msg += "\033[32mCommand examples:\033[0m\n{}\n\n".format(
750e509ee18Sopenharmony_ci            "  python3 ark.py \033[92mx64.release\033[0m\n"
751e509ee18Sopenharmony_ci            "  python3 ark.py \033[92mx64.release ets_runtime\033[0m\n"
752e509ee18Sopenharmony_ci            "  python3 ark.py \033[92mx64.release ark_js_vm es2panda\033[0m\n"
753e509ee18Sopenharmony_ci            "  python3 ark.py \033[92mx64.release ark_js_vm es2panda --clean\033[0m\n"
754e509ee18Sopenharmony_ci            "  python3 ark.py \033[92mx64.release test262\033[0m\n"
755e509ee18Sopenharmony_ci            "  python3 ark.py \033[92mx64.release test262 --threads=16\033[0m\n"
756e509ee18Sopenharmony_ci            "  python3 ark.py \033[92mx64.release test262 --aot --pgo --litecg\033[0m\n"
757e509ee18Sopenharmony_ci            "  python3 ark.py \033[92mx64.release test262 --aot --pgo --litecg --threads=8\033[0m\n"
758e509ee18Sopenharmony_ci            "  python3 ark.py \033[92mx64.release test262 --jit --enable-rm\033[0m\n"
759e509ee18Sopenharmony_ci            "  python3 ark.py \033[92mx64.release test262 --baseline-jit\033[0m\n"
760e509ee18Sopenharmony_ci            "  python3 ark.py \033[92mx64.release test262 built-ins/Array\033[0m\n"
761e509ee18Sopenharmony_ci            "  python3 ark.py \033[92mx64.release test262 built-ins/Array/name.js\033[0m\n"
762e509ee18Sopenharmony_ci            "  python3 ark.py \033[92mx64.release unittest\033[0m\n"
763e509ee18Sopenharmony_ci            "  python3 ark.py \033[92mx64.release regresstest\033[0m\n"
764e509ee18Sopenharmony_ci            "  python3 ark.py \033[92mx64.release regresstest --processes=4\033[0m\n"
765e509ee18Sopenharmony_ci            "  python3 ark.py \033[92mx64.release workload\033[0m\n"
766e509ee18Sopenharmony_ci            "  python3 ark.py \033[92mx64.release workload report\033[0m\n"
767e509ee18Sopenharmony_ci            "  python3 ark.py \033[92mx64.release workload report dev\033[0m\n"
768e509ee18Sopenharmony_ci            "  python3 ark.py \033[92mx64.release workload report dev -20\033[0m\n"
769e509ee18Sopenharmony_ci            "  python3 ark.py \033[92mx64.release workload report dev -20 10\033[0m\n"
770e509ee18Sopenharmony_ci            "  python3 ark.py \033[92mx64.release workload report dev -20 10 weekly_workload\033[0m\n")
771e509ee18Sopenharmony_ci        # Arguments
772e509ee18Sopenharmony_ci        help_msg += "\033[32mArguments:\033[0m\n{}".format(
773e509ee18Sopenharmony_ci            self.get_help_msg_of_dict(
774e509ee18Sopenharmony_ci                self.ARG_DICT, self.INDENTATION_STRING_PER_LEVEL, self.INDENTATION_STRING_PER_LEVEL))
775e509ee18Sopenharmony_ci        return help_msg
776e509ee18Sopenharmony_ci
777e509ee18Sopenharmony_ci    def clean(self, out_path: str):
778e509ee18Sopenharmony_ci        if not os.path.exists(out_path):
779e509ee18Sopenharmony_ci            print("Path \"{}\" does not exist! No need to clean it.".format(out_path))
780e509ee18Sopenharmony_ci        else:
781e509ee18Sopenharmony_ci            print("=== clean start ===")
782e509ee18Sopenharmony_ci            code = _call("{0} clean {1}".format(self.gn_binary_path, out_path))
783e509ee18Sopenharmony_ci            if code != 0:
784e509ee18Sopenharmony_ci                print("=== clean failed! ===\n")
785e509ee18Sopenharmony_ci                sys.exit(code)
786e509ee18Sopenharmony_ci            print("=== clean success! ===\n")
787e509ee18Sopenharmony_ci        return
788e509ee18Sopenharmony_ci
789e509ee18Sopenharmony_ci    def build_for_gn_target(self, out_path: str, gn_args: list, arg_list: list, log_file_name: str):
790e509ee18Sopenharmony_ci        # prepare log file
791e509ee18Sopenharmony_ci        build_log_path = os.path.join(out_path, log_file_name)
792e509ee18Sopenharmony_ci        backup(build_log_path, "w")
793e509ee18Sopenharmony_ci        if arg_list is not None:
794e509ee18Sopenharmony_ci            build_target = " ".join([str(arg).strip() for arg in arg_list
795e509ee18Sopenharmony_ci                                     if arg is not None or len(str(arg).strip()) > 0])
796e509ee18Sopenharmony_ci        else:
797e509ee18Sopenharmony_ci            build_target = ""
798e509ee18Sopenharmony_ci        str_to_build_log = "================================\nbuild_time: {0}\nbuild_target: {1}\n\n".format(
799e509ee18Sopenharmony_ci            str_of_time_now(), build_target)
800e509ee18Sopenharmony_ci        _write(build_log_path, str_to_build_log, "a")
801e509ee18Sopenharmony_ci        # gn command
802e509ee18Sopenharmony_ci        print("=== gn gen start ===")
803e509ee18Sopenharmony_ci        code = call_with_output(
804e509ee18Sopenharmony_ci            "{0} gen {1} --args=\"{2}\" --export-compile-commands".format(
805e509ee18Sopenharmony_ci                self.gn_binary_path, out_path, " ".join(gn_args).replace("\"", "\\\"")),
806e509ee18Sopenharmony_ci            build_log_path)
807e509ee18Sopenharmony_ci        if code != 0:
808e509ee18Sopenharmony_ci            print("=== gn gen failed! ===\n")
809e509ee18Sopenharmony_ci            sys.exit(code)
810e509ee18Sopenharmony_ci        else:
811e509ee18Sopenharmony_ci            print("=== gn gen success! ===\n")
812e509ee18Sopenharmony_ci        # ninja command
813e509ee18Sopenharmony_ci        # Always add " -d keeprsp" to ninja command to keep response file("*.rsp"), thus we could get shared libraries
814e509ee18Sopenharmony_ci        # of an excutable from its response file.
815e509ee18Sopenharmony_ci        ninja_cmd = \
816e509ee18Sopenharmony_ci            self.ninja_binary_path + \
817e509ee18Sopenharmony_ci            (" -v" if self.enable_verbose else "") + \
818e509ee18Sopenharmony_ci            (" -d keepdepfile" if self.enable_keepdepfile else "") + \
819e509ee18Sopenharmony_ci            " -d keeprsp" + \
820e509ee18Sopenharmony_ci            " -C {}".format(out_path) + \
821e509ee18Sopenharmony_ci            " {}".format(" ".join(arg_list if arg_list else [])) + \
822e509ee18Sopenharmony_ci            " -k {}".format(self.ignore_errors)
823e509ee18Sopenharmony_ci        print(ninja_cmd)
824e509ee18Sopenharmony_ci        code = call_with_output(ninja_cmd, build_log_path)
825e509ee18Sopenharmony_ci        if code != 0:
826e509ee18Sopenharmony_ci            print("=== ninja failed! ===\n")
827e509ee18Sopenharmony_ci            sys.exit(code)
828e509ee18Sopenharmony_ci        else:
829e509ee18Sopenharmony_ci            print("=== ninja success! ===\n")
830e509ee18Sopenharmony_ci        return
831e509ee18Sopenharmony_ci
832e509ee18Sopenharmony_ci    def call_build_gn_target(self, gn_args, out_path, x64_out_path, test_suite, log_file_name):
833e509ee18Sopenharmony_ci        if any('target_cpu="arm64"' in arg for arg in gn_args):
834e509ee18Sopenharmony_ci            gn_args.append("so_dir_for_qemu=\"../../{0}/common/common/libc/\"".format(out_path))
835e509ee18Sopenharmony_ci            gn_args.append("run_with_qemu=true".format(out_path))
836e509ee18Sopenharmony_ci            if not os.path.exists(x64_out_path):
837e509ee18Sopenharmony_ci                os.makedirs(x64_out_path)
838e509ee18Sopenharmony_ci            self.build_for_gn_target(
839e509ee18Sopenharmony_ci                x64_out_path,
840e509ee18Sopenharmony_ci                ['target_os="linux"', 'target_cpu="x64"', 'is_debug=false'],
841e509ee18Sopenharmony_ci                self.ARG_DICT.get("target").get(test_suite).get("gn_targets_depend_on"),
842e509ee18Sopenharmony_ci                log_file_name)
843e509ee18Sopenharmony_ci            self.build_for_gn_target(
844e509ee18Sopenharmony_ci                out_path,
845e509ee18Sopenharmony_ci                gn_args,
846e509ee18Sopenharmony_ci                self.ARG_DICT.get("target").get(test_suite).get("arm64_gn_targets_depend_on"),
847e509ee18Sopenharmony_ci                log_file_name)
848e509ee18Sopenharmony_ci        else:
849e509ee18Sopenharmony_ci            self.build_for_gn_target(
850e509ee18Sopenharmony_ci                out_path,
851e509ee18Sopenharmony_ci                gn_args,
852e509ee18Sopenharmony_ci                self.ARG_DICT.get("target").get(test_suite).get("gn_targets_depend_on"),
853e509ee18Sopenharmony_ci                log_file_name)
854e509ee18Sopenharmony_ci
855e509ee18Sopenharmony_ci    def get_build_cmd(self, *, test_suite, test_script_name, test_script_path,
856e509ee18Sopenharmony_ci                      out_path, x64_out_path, gn_args: list, args_to_cmd: str, timeout,
857e509ee18Sopenharmony_ci                      run_jit: bool = False, run_baseline_jit: bool = False, aot_mode: bool = False,
858e509ee18Sopenharmony_ci                      run_pgo: bool = False, enable_litecg: bool = False, ignore_list: Optional[str] = None) -> str:
859e509ee18Sopenharmony_ci        if run_jit:
860e509ee18Sopenharmony_ci            cmd = self.get_jit_cmd(test_suite, test_script_name, test_script_path,
861e509ee18Sopenharmony_ci                                   gn_args, out_path, x64_out_path, args_to_cmd, timeout)
862e509ee18Sopenharmony_ci        elif run_baseline_jit:
863e509ee18Sopenharmony_ci            cmd = self.get_baseline_jit_cmd(test_suite, test_script_name, test_script_path,
864e509ee18Sopenharmony_ci                                            gn_args, out_path, x64_out_path, args_to_cmd, timeout)
865e509ee18Sopenharmony_ci        elif aot_mode and test_suite == "test262":
866e509ee18Sopenharmony_ci            cmd = self.get_test262_aot_cmd(gn_args, out_path, x64_out_path, run_pgo,
867e509ee18Sopenharmony_ci                                           enable_litecg, args_to_cmd, timeout)
868e509ee18Sopenharmony_ci        else:
869e509ee18Sopenharmony_ci            cmd = self.get_cmd(test_suite, test_script_name, test_script_path,
870e509ee18Sopenharmony_ci                               gn_args, out_path, x64_out_path, aot_mode, run_pgo,
871e509ee18Sopenharmony_ci                               enable_litecg, args_to_cmd, timeout, ignore_list)
872e509ee18Sopenharmony_ci        return cmd
873e509ee18Sopenharmony_ci
874e509ee18Sopenharmony_ci    def build_for_suite(self, *, test_suite, test_script_name, test_script_path,
875e509ee18Sopenharmony_ci                        out_path, gn_args: list, log_file_name, args_to_cmd: str, timeout,
876e509ee18Sopenharmony_ci                        run_jit: bool = False, run_baseline_jit: bool = False, aot_mode: bool = False,
877e509ee18Sopenharmony_ci                        run_pgo: bool = False, enable_litecg: bool = False, ignore_list: Optional[str] = None):
878e509ee18Sopenharmony_ci        x64_out_path = self.__get_x64_out_path(out_path)
879e509ee18Sopenharmony_ci        self.call_build_gn_target(gn_args, out_path, x64_out_path, test_suite, log_file_name)
880e509ee18Sopenharmony_ci        cmd = self.get_build_cmd(
881e509ee18Sopenharmony_ci            test_suite=test_suite,
882e509ee18Sopenharmony_ci            test_script_name=test_script_name,
883e509ee18Sopenharmony_ci            test_script_path=test_script_path,
884e509ee18Sopenharmony_ci            out_path=out_path,
885e509ee18Sopenharmony_ci            x64_out_path=x64_out_path,
886e509ee18Sopenharmony_ci            gn_args=gn_args,
887e509ee18Sopenharmony_ci            args_to_cmd=args_to_cmd,
888e509ee18Sopenharmony_ci            timeout=timeout,
889e509ee18Sopenharmony_ci            run_jit=run_jit,
890e509ee18Sopenharmony_ci            run_baseline_jit=run_baseline_jit,
891e509ee18Sopenharmony_ci            aot_mode=aot_mode, run_pgo=run_pgo, enable_litecg=enable_litecg, ignore_list=ignore_list)
892e509ee18Sopenharmony_ci        log_path = str(os.path.join(out_path, log_file_name))
893e509ee18Sopenharmony_ci        str_to_log = "================================\n{2}_time: {0}\n{2}_target: {1}\n\n".format(
894e509ee18Sopenharmony_ci            str_of_time_now(), args_to_cmd, test_suite)
895e509ee18Sopenharmony_ci        _write(log_path, str_to_log, "a")
896e509ee18Sopenharmony_ci        print(f"=== {test_suite} start ===")
897e509ee18Sopenharmony_ci        code = call_with_output(cmd, log_path)
898e509ee18Sopenharmony_ci        if code != 0:
899e509ee18Sopenharmony_ci            print(f"=== {test_suite} fail! ===\n")
900e509ee18Sopenharmony_ci            sys.exit(code)
901e509ee18Sopenharmony_ci        print(f"=== {test_suite} success! ===\n")
902e509ee18Sopenharmony_ci
903e509ee18Sopenharmony_ci    def build_for_test262(self, out_path, gn_args: list, arg_list: list):
904e509ee18Sopenharmony_ci        timeout, arg_list = self.parse_timeout(arg_list)
905e509ee18Sopenharmony_ci        arg_list = arg_list[1:]
906e509ee18Sopenharmony_ci
907e509ee18Sopenharmony_ci        is_aot_mode, arg_list = self.__purge_arg_list("--aot", arg_list)
908e509ee18Sopenharmony_ci        is_pgo, arg_list = self.__purge_arg_list("--pgo", arg_list)
909e509ee18Sopenharmony_ci        is_litecg, arg_list = self.__purge_arg_list("--litecg", arg_list)
910e509ee18Sopenharmony_ci        is_jit, arg_list = self.__purge_arg_list("--jit", arg_list)
911e509ee18Sopenharmony_ci        is_baseline_jit, arg_list = self.__purge_arg_list("--baseline-jit", arg_list)
912e509ee18Sopenharmony_ci        print(f"Test262: arg_list = {arg_list}")
913e509ee18Sopenharmony_ci
914e509ee18Sopenharmony_ci        args_to_test262_cmd = self.build_args_to_test262_cmd(arg_list)
915e509ee18Sopenharmony_ci        self.build_for_suite(
916e509ee18Sopenharmony_ci            test_suite="test262",
917e509ee18Sopenharmony_ci            test_script_name="test262/run_test262.py",
918e509ee18Sopenharmony_ci            test_script_path="arkcompiler/ets_frontend",
919e509ee18Sopenharmony_ci            out_path=out_path,
920e509ee18Sopenharmony_ci            gn_args=gn_args,
921e509ee18Sopenharmony_ci            log_file_name=self.TEST262_LOG_FILE_NAME,
922e509ee18Sopenharmony_ci            args_to_cmd=args_to_test262_cmd,
923e509ee18Sopenharmony_ci            timeout=timeout,
924e509ee18Sopenharmony_ci            run_jit=is_jit,
925e509ee18Sopenharmony_ci            run_pgo=is_pgo,
926e509ee18Sopenharmony_ci            run_baseline_jit=is_baseline_jit,
927e509ee18Sopenharmony_ci            aot_mode=is_aot_mode,
928e509ee18Sopenharmony_ci            enable_litecg=is_litecg
929e509ee18Sopenharmony_ci        )
930e509ee18Sopenharmony_ci
931e509ee18Sopenharmony_ci    def build_for_unittest(self, out_path: str, gn_args: list, log_file_name: str):
932e509ee18Sopenharmony_ci        self.build_for_gn_target(
933e509ee18Sopenharmony_ci            out_path, gn_args, self.ARG_DICT.get("target").get("unittest").get("gn_targets_depend_on"),
934e509ee18Sopenharmony_ci            log_file_name)
935e509ee18Sopenharmony_ci        return
936e509ee18Sopenharmony_ci
937e509ee18Sopenharmony_ci    def build_for_regress_test(self, out_path, gn_args: list, arg_list: list):
938e509ee18Sopenharmony_ci        timeout, arg_list = self.parse_option(arg_list, option_name="--timeout", default_value=200)
939e509ee18Sopenharmony_ci        ignore_list, arg_list = self.parse_option(arg_list, option_name="--ignore-list", default_value=None)
940e509ee18Sopenharmony_ci
941e509ee18Sopenharmony_ci        arg_list = arg_list[1:]
942e509ee18Sopenharmony_ci
943e509ee18Sopenharmony_ci        is_aot, arg_list = self.__purge_arg_list("--aot", arg_list)
944e509ee18Sopenharmony_ci        is_pgo, arg_list = self.__purge_arg_list("--pgo", arg_list)
945e509ee18Sopenharmony_ci        is_litecg, arg_list = self.__purge_arg_list("--litecg", arg_list)
946e509ee18Sopenharmony_ci        is_jit, arg_list = self.__purge_arg_list("--jit", arg_list)
947e509ee18Sopenharmony_ci        is_baseline_jit, arg_list = self.__purge_arg_list("--baseline-jit", arg_list)
948e509ee18Sopenharmony_ci        print(f"Regress: arg_list = {arg_list}")
949e509ee18Sopenharmony_ci
950e509ee18Sopenharmony_ci        args_to_regress_test_cmd = self.build_args_to_regress_cmd(arg_list)
951e509ee18Sopenharmony_ci        self.build_for_suite(
952e509ee18Sopenharmony_ci            test_suite="regresstest",
953e509ee18Sopenharmony_ci            test_script_name="test/regresstest/run_regress_test.py",
954e509ee18Sopenharmony_ci            test_script_path="arkcompiler/ets_runtime",
955e509ee18Sopenharmony_ci            out_path=out_path,
956e509ee18Sopenharmony_ci            gn_args=gn_args,
957e509ee18Sopenharmony_ci            log_file_name=self.REGRESS_TEST_LOG_FILE_NAME,
958e509ee18Sopenharmony_ci            args_to_cmd=args_to_regress_test_cmd,
959e509ee18Sopenharmony_ci            timeout=timeout,
960e509ee18Sopenharmony_ci            run_jit=is_jit,
961e509ee18Sopenharmony_ci            run_pgo=is_pgo,
962e509ee18Sopenharmony_ci            run_baseline_jit=is_baseline_jit,
963e509ee18Sopenharmony_ci            aot_mode=is_aot,
964e509ee18Sopenharmony_ci            enable_litecg=is_litecg,
965e509ee18Sopenharmony_ci            ignore_list=ignore_list
966e509ee18Sopenharmony_ci        )
967e509ee18Sopenharmony_ci
968e509ee18Sopenharmony_ci    def build(self, out_path: str, gn_args: list, arg_list: list):
969e509ee18Sopenharmony_ci        if not os.path.exists(out_path):
970e509ee18Sopenharmony_ci            print("# mkdir -p {}".format(out_path))
971e509ee18Sopenharmony_ci            os.makedirs(out_path)
972e509ee18Sopenharmony_ci        if len(arg_list) == 0:
973e509ee18Sopenharmony_ci            self.build_for_gn_target(out_path, gn_args, ["default"], self.GN_TARGET_LOG_FILE_NAME)
974e509ee18Sopenharmony_ci        elif self.is_dict_flags_match_arg(self.ARG_DICT.get("target").get("workload"), arg_list[0]):
975e509ee18Sopenharmony_ci            self.build_for_workload(arg_list, out_path, gn_args, 'workload.log')
976e509ee18Sopenharmony_ci        elif self.is_dict_flags_match_arg(self.ARG_DICT.get("target").get("test262"), arg_list[0]):
977e509ee18Sopenharmony_ci            self.build_for_test262(out_path, gn_args, arg_list)
978e509ee18Sopenharmony_ci        elif self.is_dict_flags_match_arg(self.ARG_DICT.get("target").get("unittest"), arg_list[0]):
979e509ee18Sopenharmony_ci            if len(arg_list) > 1:
980e509ee18Sopenharmony_ci                print("\033[92m\"unittest\" not support additional arguments.\033[0m\n".format())
981e509ee18Sopenharmony_ci                sys.exit(0)
982e509ee18Sopenharmony_ci            self.build_for_unittest(out_path, gn_args, self.UNITTEST_LOG_FILE_NAME)
983e509ee18Sopenharmony_ci        elif self.is_dict_flags_match_arg(self.ARG_DICT.get("target").get("regresstest"), arg_list[0]):
984e509ee18Sopenharmony_ci            self.build_for_regress_test(out_path, gn_args, arg_list)
985e509ee18Sopenharmony_ci        else:
986e509ee18Sopenharmony_ci            self.build_for_gn_target(out_path, gn_args, arg_list, self.GN_TARGET_LOG_FILE_NAME)
987e509ee18Sopenharmony_ci        return
988e509ee18Sopenharmony_ci
989e509ee18Sopenharmony_ci    def parse_timeout(self, arg_list) -> Tuple[Optional[Union[str, int]], List[str]]:
990e509ee18Sopenharmony_ci        return self.parse_option(arg_list, option_name="--timeout", default_value=400000)
991e509ee18Sopenharmony_ci
992e509ee18Sopenharmony_ci    def match_options(self, arg_list: list, out_path: str) -> [list, list]:
993e509ee18Sopenharmony_ci        arg_list_ret = []
994e509ee18Sopenharmony_ci        gn_args_ret = []
995e509ee18Sopenharmony_ci        for arg in arg_list:
996e509ee18Sopenharmony_ci            # match [option][clean] flag
997e509ee18Sopenharmony_ci            if self.is_dict_flags_match_arg(self.ARG_DICT.get("option").get("clean"), arg):
998e509ee18Sopenharmony_ci                self.clean(out_path)
999e509ee18Sopenharmony_ci                sys.exit(0)
1000e509ee18Sopenharmony_ci            # match [option][clean-continue] flag
1001e509ee18Sopenharmony_ci            elif self.is_dict_flags_match_arg(self.ARG_DICT.get("option").get("clean-continue"), arg):
1002e509ee18Sopenharmony_ci                if not self.has_cleaned:
1003e509ee18Sopenharmony_ci                    self.clean(out_path)
1004e509ee18Sopenharmony_ci                    self.has_cleaned = True
1005e509ee18Sopenharmony_ci            # match [option][gn-args] flag
1006e509ee18Sopenharmony_ci            elif self.is_dict_flags_match_arg(self.ARG_DICT.get("option").get("gn-args"), arg):
1007e509ee18Sopenharmony_ci                gn_args_ret.append(arg[(arg.find("=") + 1):])
1008e509ee18Sopenharmony_ci            # match [option][keepdepfile] flag
1009e509ee18Sopenharmony_ci            elif self.is_dict_flags_match_arg(self.ARG_DICT.get("option").get("keepdepfile"), arg):
1010e509ee18Sopenharmony_ci                if not self.enable_keepdepfile:
1011e509ee18Sopenharmony_ci                    self.enable_keepdepfile = True
1012e509ee18Sopenharmony_ci            # match [option][verbose] flag
1013e509ee18Sopenharmony_ci            elif self.is_dict_flags_match_arg(self.ARG_DICT.get("option").get("verbose"), arg):
1014e509ee18Sopenharmony_ci                if not self.enable_verbose:
1015e509ee18Sopenharmony_ci                    self.enable_verbose = True
1016e509ee18Sopenharmony_ci            # match [option][keep-going] flag
1017e509ee18Sopenharmony_ci            elif self.is_dict_flags_match_arg(self.ARG_DICT.get("option").get("keep-going"), arg):
1018e509ee18Sopenharmony_ci                if self.ignore_errors == 1:
1019e509ee18Sopenharmony_ci                    input_value = arg[(arg.find("=") + 1):]
1020e509ee18Sopenharmony_ci                    try:
1021e509ee18Sopenharmony_ci                        self.ignore_errors = int(input_value)
1022e509ee18Sopenharmony_ci                    except Exception as _:
1023e509ee18Sopenharmony_ci                        print("\033[92mIllegal value \"{}\" for \"--keep-going\" argument.\033[0m\n".format(
1024e509ee18Sopenharmony_ci                            input_value
1025e509ee18Sopenharmony_ci                        ))
1026e509ee18Sopenharmony_ci                        sys.exit(0)
1027e509ee18Sopenharmony_ci            # make a new list with flag that do not match any flag in [option]
1028e509ee18Sopenharmony_ci            else:
1029e509ee18Sopenharmony_ci                arg_list_ret.append(arg)
1030e509ee18Sopenharmony_ci        return [arg_list_ret, gn_args_ret]
1031e509ee18Sopenharmony_ci
1032e509ee18Sopenharmony_ci    def build_for_workload(self, arg_list, out_path, gn_args, log_file_name):
1033e509ee18Sopenharmony_ci        root_dir = os.path.dirname(os.path.abspath(__file__))
1034e509ee18Sopenharmony_ci        report = False
1035e509ee18Sopenharmony_ci        tools = 'dev'
1036e509ee18Sopenharmony_ci        boundary_value = '-10'
1037e509ee18Sopenharmony_ci        run_count = '10'
1038e509ee18Sopenharmony_ci        code_v = ''
1039e509ee18Sopenharmony_ci        run_interpreter = False
1040e509ee18Sopenharmony_ci        if len(arg_list) >= 2 and arg_list[1] == 'report':
1041e509ee18Sopenharmony_ci            report = True
1042e509ee18Sopenharmony_ci        if len(arg_list) >= 3 and arg_list[2]:
1043e509ee18Sopenharmony_ci            tools = arg_list[2]
1044e509ee18Sopenharmony_ci        if len(arg_list) >= 4 and arg_list[3]:
1045e509ee18Sopenharmony_ci            boundary_value = arg_list[3]
1046e509ee18Sopenharmony_ci        if len(arg_list) >= 5 and arg_list[4]:
1047e509ee18Sopenharmony_ci            run_count = arg_list[4]
1048e509ee18Sopenharmony_ci        if len(arg_list) >= 6 and arg_list[5]:
1049e509ee18Sopenharmony_ci            code_v = arg_list[5]
1050e509ee18Sopenharmony_ci        if len(arg_list) >= 7 and arg_list[6] == '--run-interpreter':
1051e509ee18Sopenharmony_ci            run_interpreter = True
1052e509ee18Sopenharmony_ci        self.build_for_gn_target(out_path, gn_args, ["default"], self.GN_TARGET_LOG_FILE_NAME)
1053e509ee18Sopenharmony_ci        workload_cmd = "cd arkcompiler/ets_runtime/test/workloadtest/ && python3 work_load.py" \
1054e509ee18Sopenharmony_ci                       " --code-path {0}" \
1055e509ee18Sopenharmony_ci                       " --report {1}" \
1056e509ee18Sopenharmony_ci                       " --tools-type {2}" \
1057e509ee18Sopenharmony_ci                       " --boundary-value {3}" \
1058e509ee18Sopenharmony_ci                       " --run-count {4}" \
1059e509ee18Sopenharmony_ci                       " --code-v {5}" \
1060e509ee18Sopenharmony_ci            .format(root_dir, report, tools, boundary_value, run_count, code_v)
1061e509ee18Sopenharmony_ci        if run_interpreter:
1062e509ee18Sopenharmony_ci            workload_cmd += " --run-interpreter true"
1063e509ee18Sopenharmony_ci        workload_log_path = os.path.join(out_path, log_file_name)
1064e509ee18Sopenharmony_ci        str_to_workload_log = "================================\nwokload_time: {0}\nwokload_target: {1}\n\n".format(
1065e509ee18Sopenharmony_ci            str_of_time_now(), 'file')
1066e509ee18Sopenharmony_ci        _write(workload_log_path, str_to_workload_log, "a")
1067e509ee18Sopenharmony_ci        print("=== workload start ===")
1068e509ee18Sopenharmony_ci        code = call_with_output(workload_cmd, workload_log_path)
1069e509ee18Sopenharmony_ci        if code != 0:
1070e509ee18Sopenharmony_ci            print("=== workload fail! ===\n")
1071e509ee18Sopenharmony_ci            sys.exit(code)
1072e509ee18Sopenharmony_ci        print("=== workload success! ===\n")
1073e509ee18Sopenharmony_ci        return
1074e509ee18Sopenharmony_ci
1075e509ee18Sopenharmony_ci    def start_for_matched_os_cpu_mode(self, os_cpu_key: str, mode_key: str, arg_list: list):
1076e509ee18Sopenharmony_ci        # get binary gn and ninja
1077e509ee18Sopenharmony_ci        self.get_binaries()
1078e509ee18Sopenharmony_ci        # get out_path
1079e509ee18Sopenharmony_ci        name_of_out_dir_of_second_level = \
1080e509ee18Sopenharmony_ci            self.ARG_DICT.get("os_cpu").get(os_cpu_key).get("prefix_of_name_of_out_dir_of_second_level") + \
1081e509ee18Sopenharmony_ci            self.DELIMITER_FOR_SECOND_OUT_DIR_NAME + \
1082e509ee18Sopenharmony_ci            self.ARG_DICT.get("mode").get(mode_key).get("suffix_of_name_of_out_dir_of_second_level")
1083e509ee18Sopenharmony_ci        out_path = os.path.join(self.NAME_OF_OUT_DIR_OF_FIRST_LEVEL, name_of_out_dir_of_second_level)
1084e509ee18Sopenharmony_ci        # match [option] flag
1085e509ee18Sopenharmony_ci        [arg_list, gn_args] = self.match_options(arg_list, out_path)
1086e509ee18Sopenharmony_ci        # get expression which would be written to args.gn file
1087e509ee18Sopenharmony_ci        gn_args.extend(self.ARG_DICT.get("os_cpu").get(os_cpu_key).get("gn_args"))
1088e509ee18Sopenharmony_ci        gn_args.extend(self.ARG_DICT.get("mode").get(mode_key).get("gn_args"))
1089e509ee18Sopenharmony_ci        # start to build
1090e509ee18Sopenharmony_ci        self.build(out_path, gn_args, arg_list)
1091e509ee18Sopenharmony_ci        return
1092e509ee18Sopenharmony_ci
1093e509ee18Sopenharmony_ci    def __purge_arg_list(self, option_name: str, arg_list: List[Any]) -> Tuple[bool, List[Any]]:
1094e509ee18Sopenharmony_ci        if option_name in arg_list:
1095e509ee18Sopenharmony_ci            arg_list.remove(option_name)
1096e509ee18Sopenharmony_ci            return True, arg_list
1097e509ee18Sopenharmony_ci        return False, arg_list
1098e509ee18Sopenharmony_ci
1099e509ee18Sopenharmony_ci
1100e509ee18Sopenharmony_ciif __name__ == "__main__":
1101e509ee18Sopenharmony_ci    ark_py = ArkPy()
1102e509ee18Sopenharmony_ci    ark_py.__main__(sys.argv[1:])
1103