133eb0b6dSopenharmony_ci#!/bin/bash
233eb0b6dSopenharmony_ci# Copyright (c) 2023 Huawei Device Co., Ltd.
333eb0b6dSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
433eb0b6dSopenharmony_ci# you may not use this file except in compliance with the License.
533eb0b6dSopenharmony_ci# You may obtain a copy of the License at
633eb0b6dSopenharmony_ci#
733eb0b6dSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
833eb0b6dSopenharmony_ci#
933eb0b6dSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
1033eb0b6dSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
1133eb0b6dSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1233eb0b6dSopenharmony_ci# See the License for the specific language governing permissions and
1333eb0b6dSopenharmony_ci# limitations under the License.
1433eb0b6dSopenharmony_ci
1533eb0b6dSopenharmony_ciset -e
1633eb0b6dSopenharmony_ci
1733eb0b6dSopenharmony_cido_fetch() {
1833eb0b6dSopenharmony_ci    echo "skip."
1933eb0b6dSopenharmony_ci}
2033eb0b6dSopenharmony_ci
2133eb0b6dSopenharmony_cido_patch() {
2233eb0b6dSopenharmony_ci    echo "skip."
2333eb0b6dSopenharmony_ci}
2433eb0b6dSopenharmony_ci
2533eb0b6dSopenharmony_cido_configure() {
2633eb0b6dSopenharmony_ci    pushd ${workdir}
2733eb0b6dSopenharmony_ci    if [[ "${TARGET_CPU}" = "x86_64" ]]; then
2833eb0b6dSopenharmony_ci        ./configure --shared; return
2933eb0b6dSopenharmony_ci    fi
3033eb0b6dSopenharmony_ci    ./configure \
3133eb0b6dSopenharmony_ci        --prefix=${workdir} \
3233eb0b6dSopenharmony_ci        --dest-cpu=${TARGET_CPU} --dest-os=linux \
3333eb0b6dSopenharmony_ci        --cross-compiling \
3433eb0b6dSopenharmony_ci        --shared \
3533eb0b6dSopenharmony_ci        --with-arm-float-abi=hard \
3633eb0b6dSopenharmony_ci        --without-corepack \
3733eb0b6dSopenharmony_ci        --without-npm \
3833eb0b6dSopenharmony_ci        --without-intl
3933eb0b6dSopenharmony_ci    popd
4033eb0b6dSopenharmony_ci}
4133eb0b6dSopenharmony_ci
4233eb0b6dSopenharmony_cido_unstripped_copy() {
4333eb0b6dSopenharmony_ci    mkdir -p ${TARGET_GEN_DIR}/../../../../../lib.unstripped/jsvm/
4433eb0b6dSopenharmony_ci    cp -u ${workdir}/out/Release/libjsvm.so ${TARGET_GEN_DIR}/../../../../../lib.unstripped/jsvm/
4533eb0b6dSopenharmony_ci    cp -u ${workdir}/deps/v8/lib.unstripped/libv8_shared.so ${TARGET_GEN_DIR}/../../../../../lib.unstripped/jsvm/
4633eb0b6dSopenharmony_ci    pushd ${out_dir}
4733eb0b6dSopenharmony_ci    rm -rf *
4833eb0b6dSopenharmony_ci    popd
4933eb0b6dSopenharmony_ci}
5033eb0b6dSopenharmony_ci
5133eb0b6dSopenharmony_ciget_thread_num() {
5233eb0b6dSopenharmony_ci    quota_us_file="/sys/fs/cgroup/cpu/cpu.cfs_quota_us"
5333eb0b6dSopenharmony_ci    period_us_file="/sys/fs/cgroup/cpu/cpu.cfs_period_us"
5433eb0b6dSopenharmony_ci    if [ -f "${quota_us_file}" ]; then
5533eb0b6dSopenharmony_ci        cfs_quota_us=$(cat ${quota_us_file})
5633eb0b6dSopenharmony_ci    fi
5733eb0b6dSopenharmony_ci    if [ -f "${period_us_file}" ]; then
5833eb0b6dSopenharmony_ci        cfs_period_us=$(cat ${period_us_file})
5933eb0b6dSopenharmony_ci    fi
6033eb0b6dSopenharmony_ci    # Set the default value when the variable is empty.
6133eb0b6dSopenharmony_ci    cfs_quota_us=${cfs_quota_us:=-1}
6233eb0b6dSopenharmony_ci    cfs_period_us=${cfs_period_us:=0}
6333eb0b6dSopenharmony_ci    if [ "${cfs_quota_us}" != -1 -a "${cfs_period_us}" != 0 ]; then
6433eb0b6dSopenharmony_ci        PROCESSORS=$(expr ${cfs_quota_us} / ${cfs_period_us})
6533eb0b6dSopenharmony_ci        echo "cpu.cfs_quota_us: "$PROCESSORS
6633eb0b6dSopenharmony_ci    else
6733eb0b6dSopenharmony_ci        PROCESSORS=$(cat /proc/cpuinfo | grep "processor" | wc -l)
6833eb0b6dSopenharmony_ci        echo "cpuinfo: "$PROCESSORS
6933eb0b6dSopenharmony_ci    fi
7033eb0b6dSopenharmony_ci}
7133eb0b6dSopenharmony_ci
7233eb0b6dSopenharmony_cido_compile() {
7333eb0b6dSopenharmony_ci    pushd ${workdir}
7433eb0b6dSopenharmony_ci    get_thread_num
7533eb0b6dSopenharmony_ci    cpu_num=$[PROCESSORS*2]
7633eb0b6dSopenharmony_ci    make -j${cpu_num}
7733eb0b6dSopenharmony_ci    popd
7833eb0b6dSopenharmony_ci}
7933eb0b6dSopenharmony_ci
8033eb0b6dSopenharmony_cido_strip() {
8133eb0b6dSopenharmony_ci    stripped_binary_path=${TARGET_GEN_DIR}/libjsvm.so
8233eb0b6dSopenharmony_ci    binary=${stripped_binary_path}
8333eb0b6dSopenharmony_ci    echo "${binary}"
8433eb0b6dSopenharmony_ci    dynsyms_path="${stripped_binary_path}.dynsyms"
8533eb0b6dSopenharmony_ci    funcsysms_path="${stripped_binary_path}.funcsyms"
8633eb0b6dSopenharmony_ci    keep_path="${stripped_binary_path}.keep"
8733eb0b6dSopenharmony_ci    debug_path="${stripped_binary_path}.debug"
8833eb0b6dSopenharmony_ci    mini_debug_path="${stripped_binary_path}.minidebug"
8933eb0b6dSopenharmony_ci
9033eb0b6dSopenharmony_ci    ${NM} -D ${binary} --format=posix --defined-only \
9133eb0b6dSopenharmony_ci            | awk '{ print $1 }' | sort > ${dynsyms_path}
9233eb0b6dSopenharmony_ci    ${NM} ${binary} --format=posix --defined-only \
9333eb0b6dSopenharmony_ci            | awk '{ if ($2 == "T" || $2 == "t" || $2 == "D") print $1 }' \
9433eb0b6dSopenharmony_ci            | sort > ${funcsysms_path}
9533eb0b6dSopenharmony_ci    comm -13 ${dynsyms_path} ${funcsysms_path} > ${keep_path}
9633eb0b6dSopenharmony_ci
9733eb0b6dSopenharmony_ci    ${OBJCOPY} --only-keep-debug ${binary} ${debug_path}
9833eb0b6dSopenharmony_ci    ${OBJCOPY} -S --remove-section .gdb_index --remove-section .comment \
9933eb0b6dSopenharmony_ci            --keep-symbols=${keep_path} ${debug_path} ${mini_debug_path}
10033eb0b6dSopenharmony_ci
10133eb0b6dSopenharmony_ci    ${STRIP} --strip-all --keep-section=.comment ${binary}
10233eb0b6dSopenharmony_ci
10333eb0b6dSopenharmony_ci    xz ${mini_debug_path}
10433eb0b6dSopenharmony_ci    ${OBJCOPY} --add-section .gnu_debugdata=${mini_debug_path}.xz ${binary}
10533eb0b6dSopenharmony_ci
10633eb0b6dSopenharmony_ci    rm -f ${dynsyms_path}
10733eb0b6dSopenharmony_ci    rm -f ${funcsysms_path}
10833eb0b6dSopenharmony_ci    rm -f ${keep_path}
10933eb0b6dSopenharmony_ci    rm -f ${debug_path}
11033eb0b6dSopenharmony_ci    rm -f ${mini_debug_path}
11133eb0b6dSopenharmony_ci    rm -f ${mini_debug_path}.xz
11233eb0b6dSopenharmony_ci}
11333eb0b6dSopenharmony_ci
11433eb0b6dSopenharmony_cido_install () {
11533eb0b6dSopenharmony_ci    cp -u ${workdir}/out/Release/libjsvm.so ${TARGET_GEN_DIR}
11633eb0b6dSopenharmony_ci}
11733eb0b6dSopenharmony_ci
11833eb0b6dSopenharmony_cido_env() {
11933eb0b6dSopenharmony_ci    # init workspace
12033eb0b6dSopenharmony_ci    out_dir=${TARGET_GEN_DIR}/out
12133eb0b6dSopenharmony_ci    workdir=${NODE_PATH}
12233eb0b6dSopenharmony_ci    [ -d "${out_dir}" ] || mkdir -p ${out_dir}
12333eb0b6dSopenharmony_ci    [ -L "${workdir}/out" ] || ln -s ${out_dir} ${workdir}/out
12433eb0b6dSopenharmony_ci
12533eb0b6dSopenharmony_ci    argurment+=" -fstack-protector-strong"
12633eb0b6dSopenharmony_ci    argurment+=" -Wl,-z,noexecstack"
12733eb0b6dSopenharmony_ci    argurment+=" -Wl,-z,relro"
12833eb0b6dSopenharmony_ci    argurment+=" -Wl,-z,now"
12933eb0b6dSopenharmony_ci    argurment+=" -pie"
13033eb0b6dSopenharmony_ci    if [ $1 -eq 1 ]; then
13133eb0b6dSopenharmony_ci        argurment+=" -ggdb3"
13233eb0b6dSopenharmony_ci    fi
13333eb0b6dSopenharmony_ci    if [[ "${TARGET_CPU}" = "arm" ]]; then
13433eb0b6dSopenharmony_ci        cflags="  --target=arm-linux-ohos"
13533eb0b6dSopenharmony_ci        cflags+=" --sysroot=${SYSROOT}"
13633eb0b6dSopenharmony_ci        cflags+=" -march=armv7-a"
13733eb0b6dSopenharmony_ci        cflags+=" -mfpu=neon"
13833eb0b6dSopenharmony_ci        cflags_host="-m32"
13933eb0b6dSopenharmony_ci        ARCH="arm"
14033eb0b6dSopenharmony_ci    elif [[ "${TARGET_CPU}" = "arm64" ]]; then
14133eb0b6dSopenharmony_ci        cflags="  --target=aarch64-linux-ohos"
14233eb0b6dSopenharmony_ci        cflags+=" --sysroot=${SYSROOT}"
14333eb0b6dSopenharmony_ci        cflags+=" -march=armv8-a"
14433eb0b6dSopenharmony_ci        cflags+=" -DV8_OS_OH=1"
14533eb0b6dSopenharmony_ci        cflags+=" -mfpu=neon"
14633eb0b6dSopenharmony_ci        cflags_host="-m64"
14733eb0b6dSopenharmony_ci        ARCH="aarch64"
14833eb0b6dSopenharmony_ci    elif [[ "${TARGET_CPU}" = "x86_64" ]]; then
14933eb0b6dSopenharmony_ci        export CC="${CCACHE_EXEC} gcc"
15033eb0b6dSopenharmony_ci        export CXX="${CCACHE_EXEC} g++"
15133eb0b6dSopenharmony_ci        return
15233eb0b6dSopenharmony_ci    else
15333eb0b6dSopenharmony_ci        die "not support target cpu"
15433eb0b6dSopenharmony_ci    fi
15533eb0b6dSopenharmony_ci
15633eb0b6dSopenharmony_ci    if [[ "${TARGET_CLANG_COVERAGE}" = "true" ]]; then
15733eb0b6dSopenharmony_ci        cflags+=" --coverage"
15833eb0b6dSopenharmony_ci    fi
15933eb0b6dSopenharmony_ci
16033eb0b6dSopenharmony_ci    cflags+=" ${argurment}"
16133eb0b6dSopenharmony_ci
16233eb0b6dSopenharmony_ci    # linux host env
16333eb0b6dSopenharmony_ci    HOST_OS="linux"
16433eb0b6dSopenharmony_ci    HOST_ARCH="x86_64"
16533eb0b6dSopenharmony_ci    export LINK_host="${CCACHE_EXEC} ${PREFIX}/clang++ ${cflags_host}"
16633eb0b6dSopenharmony_ci    export CXX_host="${CCACHE_EXEC} ${PREFIX}/clang++ ${cflags_host}"
16733eb0b6dSopenharmony_ci    export CC_host="${CCACHE_EXEC} ${PREFIX}/clang ${cflags_host}"
16833eb0b6dSopenharmony_ci    export AR_host=${PREFIX}/llvm-ar
16933eb0b6dSopenharmony_ci
17033eb0b6dSopenharmony_ci    # target env
17133eb0b6dSopenharmony_ci    export CC="${CCACHE_EXEC} ${PREFIX}/clang ${cflags}"
17233eb0b6dSopenharmony_ci    export CXX="${CCACHE_EXEC} ${PREFIX}/clang++ ${cflags}"
17333eb0b6dSopenharmony_ci    export LD="${PREFIX}/ld.lld"
17433eb0b6dSopenharmony_ci    export AS="${PREFIX}/llvm-as"
17533eb0b6dSopenharmony_ci    export AR="${PREFIX}/llvm-ar"
17633eb0b6dSopenharmony_ci    export STRIP="${PREFIX}/llvm-strip"
17733eb0b6dSopenharmony_ci    export OBJCOPY="${PREFIX}/llvm-objcopy"
17833eb0b6dSopenharmony_ci    export OBJDUMP="${PREFIX}/llvm-obidump"
17933eb0b6dSopenharmony_ci    export RANLIB="${PREFIX}/llvm-ranlib"
18033eb0b6dSopenharmony_ci    export NM="${PREFIX}/llvm-nm"
18133eb0b6dSopenharmony_ci    export STRINGS="${PREFIX}/llvm-strings"
18233eb0b6dSopenharmony_ci    export READELF="${PREFIX}/llvm-readelf"
18333eb0b6dSopenharmony_ci    env > ${out_dir}/log.do_env
18433eb0b6dSopenharmony_ci}
185