1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4# Copyright (c) 2023-2024 Huawei Device Co., Ltd.
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17#
18#
19# This is a script for running a minimal reasonable set of tests
20# during front-end development. Can be used for a quick self-check.
21# Definitely not a substitute / equivalent of the full CI run.
22# Can be dropped to your favorite location in $PATH to be invoked
23# as a CLI tool. Try --help if not sure how to proceed.
24#
25
26ROOT_DIR=
27BUILD_DIR=
28BRANCH_NAME=
29BUILD_TYPE=Debug
30DO_CONFIGURE=no
31DO_CLEAN=no
32BUILD_TARGETS=
33RUN_FUNC_SUITE=no
34RUN_CTS=no
35PARSER=
36RUN_CLANG_TIDY=yes
37PARENT_BRANCH=
38
39function print_help
40{
41    HELP_MESSAGE="
42    Options:
43
44    --root-dir=REQUIRED
45    --build-dir=REQUIRED
46
47    [--branch=BRANCH_NAME]
48    [--build-type=CMAKE_BUILD_TYPE]
49    [--build-clean]
50    [--configure]
51    [--build=TARGET1 --build=TARGET2 ...]
52    [--run-func-suite]
53    [--run-cts]
54    [--parser]
55    [--run-clang_tidy=yes/no]
56
57    Notes:
58    * --build-type should correspond to the main project's build
59      types, i.e.: Debug, FastVerify, Release.
60    * --build can be repeated to build several targets (correspond
61      to the main project's build targets).
62    * --run-clang_tidy set to "yes" by default.
63    "
64    echo "$HELP_MESSAGE"
65}
66
67for opt in "$@"
68do
69    case $opt in
70    -h|--help)
71        print_help
72        exit 0
73        ;;
74    --root-dir=*)
75        ROOT_DIR=${opt//[-a-zA-Z0-9]*=/}
76        ;;
77    --build-dir=*)
78        BUILD_DIR=${opt//[-a-zA-Z0-9]*=/}
79        ;;
80    --build-type=*)
81        BUILD_TYPE=${opt//[-a-zA-Z0-9]*=/}
82        ;;
83    --branch=*)
84        BRANCH_NAME=${opt//[-a-zA-Z0-9]*=/}
85        ;;
86    --configure)
87        DO_CONFIGURE=yes
88        ;;
89    --build-clean)
90        DO_CLEAN=yes
91        ;;
92    --build=*)
93        build_target=${opt//[-a-zA-Z0-9]*=/}
94        BUILD_TARGETS="${BUILD_TARGETS} ${build_target}"
95        ;;
96    --run-func-suite)
97        RUN_FUNC_SUITE=yes
98        ;;
99    --run-cts)
100        RUN_CTS=yes
101        ;;
102    --parser)
103        PARSER=yes
104        ;;
105    --run-clang_tidy=*)
106        $RUN_CLANG_TIDY=$opt
107        ;;
108    *)
109        echo "Error: Unsupported flag $opt" >&2
110        exit 1
111        ;;
112    esac
113done
114
115set -e
116
117if [[ ! -d "${ROOT_DIR}" ]]; then
118    echo 'Fatal: --root-dir is missing or invalid'
119    exit 1
120fi
121
122if [[ "${BUILD_DIR}" == '' ]]; then
123    echo 'Fatal: --build-dir is missing'
124    exit 1
125fi
126
127mkdir -p "$BUILD_DIR"
128if [[ "$DO_CLEAN" == 'yes' ]]; then
129    pushd "$BUILD_DIR"
130        rm -rf *
131    popd
132fi
133
134RUNTIME_CORE_DIR=${ROOT_DIR}/runtime_core
135ETS_FRONTEND_DIR=${ROOT_DIR}/ets_frontend
136CLANG_TIDY_DIRS=(${RUNTIME_CORE_DIR} ${ETS_FRONTEND_DIR})
137if [[ "${BRANCH_NAME}" != '' ]]; then
138    for dir in "${CLANG_TIDY_DIRS[@]}"
139    do
140        pushd $dir
141            git checkout "${BRANCH_NAME}"
142        popd
143    done
144fi
145
146STATIC_CORE_DIR=${ROOT_DIR}/runtime_core/static_core
147TEST_RUNNER=${STATIC_CORE_DIR}/tests/tests-u-runner/runner.sh
148CLANG_TIDY_CHECK=${STATIC_CORE_DIR}/scripts/clang-tidy/clang_tidy_check.py
149
150if [[ "$RUN_CLANG_TIDY" == 'yes' ]]; then
151    for dir in "${CLANG_TIDY_DIRS[@]}"
152    do
153        pushd $dir > /dev/null
154        echo $dir
155        while [[ "$PARENT_BRANCH" == '' || $i != 25 ]]
156        do
157            i=1
158            branch=$(git show-branch | sed "s/].*//" | grep "\*" | grep -v "$(git rev-parse --abbrev-ref HEAD)" | head -n${i} | sed "s/^.*\[//" | sed 's/[\^~].*//')
159            if [[ "$branch" == 'fe_dev_stable' || "$branch" == 'master' ]]; then
160                PARENT_BRANCH=$branch
161                echo "PARENT_BRANCH = $branch"
162                FILE_NAME_FILTER=$(git diff --name-only $PARENT_BRANCH | sed -rn "/^.*(\.(c|cpp|h))$/p" | sed ':a;N;$!ba;s/\n/\|/g')
163                echo "$FILE_NAME_FILTER"
164                if [[ $FILE_NAME_FILTER != '' ]]; then
165                    ${CLANG_TIDY_CHECK} --filename-filter=${FILE_NAME_FILTER} "$STATIC_CORE_DIR" "$BUILD_DIR"
166                else
167                    echo "Nothing to check"
168                fi
169                break
170            fi
171            i=$i+1
172            PARENT_BRANCH=
173        done
174        if [[ "$PARENT_BRANCH" == '' ]]; then
175            echo "Parent branch for $dir not found. All files will be checked"
176            ${CLANG_TIDY_CHECK} "$STATIC_CORE_DIR" "$BUILD_DIR"
177        fi
178        PARENT_BRANCH=
179        popd > /dev/null
180    done
181fi
182
183if [[ "${DO_CONFIGURE}" == 'yes' ]]; then
184    pushd ${BUILD_DIR}
185        cmake -GNinja \
186            -DCMAKE_BUILD_TYPE="$BUILD_TYPE" \
187            -DCMAKE_TOOLCHAIN_FILE=cmake/toolchain/host_clang_14.cmake \
188            -DPANDA_ETS_INTEROP_JS=ON \
189            -DPANDA_WITH_ECMASCRIPT=OFF \
190            -DPANDA_WITH_ETS=ON \
191            "$STATIC_CORE_DIR"
192    popd
193fi
194
195if [[ "$BUILD_TARGETS" != '' ]]; then
196    pushd "$BUILD_DIR"
197        ninja ${BUILD_TARGETS}
198    popd
199fi
200
201if [[ "$RUN_FUNC_SUITE" == 'yes' ]]; then
202    # Func tests INT:
203    ${TEST_RUNNER} --build-dir "$BUILD_DIR" \
204        --processes=all --force-generate \
205        --ets-func-tests
206
207    # Func tests JIT:
208    ${TEST_RUNNER} --build-dir "$BUILD_DIR" \
209        --processes=all --force-generate \
210        --ets-func-tests --jit --ark-args='--compiler-ignore-failures=false'
211fi
212
213if [[ "$RUN_CTS" == 'yes' ]]; then
214    # ArkTS CTS:
215    ${TEST_RUNNER} --build-dir "$BUILD_DIR" \
216        --processes=all --force-generate \
217        --ets-cts
218fi
219
220if [[ "$PARSER" == 'yes' ]]; then
221    # Pasrer:
222    ${TEST_RUNNER} --build-dir "$BUILD_DIR" \
223        --processes=all --force-generate \
224        --parser --no-js
225fi