1#!/bin/bash
2# Copyright (c) 2023 Huawei Device Co., Ltd.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15set -e
16
17do_fetch() {
18    echo "skip."
19}
20
21do_patch() {
22    echo "skip."
23}
24
25do_configure() {
26    pushd ${workdir}
27    if [[ "${TARGET_CPU}" = "x86_64" ]]; then
28        ./configure --shared; return
29    fi
30    ./configure \
31        --prefix=${workdir} \
32        --dest-cpu=${TARGET_CPU} --dest-os=linux \
33        --cross-compiling \
34        --shared \
35        --with-arm-float-abi=hard \
36        --without-corepack \
37        --without-npm \
38        --without-intl
39    popd
40}
41
42do_unstripped_copy() {
43    mkdir -p ${TARGET_GEN_DIR}/../../../../../lib.unstripped/jsvm/
44    cp -u ${workdir}/out/Release/libjsvm.so ${TARGET_GEN_DIR}/../../../../../lib.unstripped/jsvm/
45    cp -u ${workdir}/deps/v8/lib.unstripped/libv8_shared.so ${TARGET_GEN_DIR}/../../../../../lib.unstripped/jsvm/
46    pushd ${out_dir}
47    rm -rf *
48    popd
49}
50
51get_thread_num() {
52    quota_us_file="/sys/fs/cgroup/cpu/cpu.cfs_quota_us"
53    period_us_file="/sys/fs/cgroup/cpu/cpu.cfs_period_us"
54    if [ -f "${quota_us_file}" ]; then
55        cfs_quota_us=$(cat ${quota_us_file})
56    fi
57    if [ -f "${period_us_file}" ]; then
58        cfs_period_us=$(cat ${period_us_file})
59    fi
60    # Set the default value when the variable is empty.
61    cfs_quota_us=${cfs_quota_us:=-1}
62    cfs_period_us=${cfs_period_us:=0}
63    if [ "${cfs_quota_us}" != -1 -a "${cfs_period_us}" != 0 ]; then
64        PROCESSORS=$(expr ${cfs_quota_us} / ${cfs_period_us})
65        echo "cpu.cfs_quota_us: "$PROCESSORS
66    else
67        PROCESSORS=$(cat /proc/cpuinfo | grep "processor" | wc -l)
68        echo "cpuinfo: "$PROCESSORS
69    fi
70}
71
72do_compile() {
73    pushd ${workdir}
74    get_thread_num
75    cpu_num=$[PROCESSORS*2]
76    make -j${cpu_num}
77    popd
78}
79
80do_strip() {
81    stripped_binary_path=${TARGET_GEN_DIR}/libjsvm.so
82    binary=${stripped_binary_path}
83    echo "${binary}"
84    dynsyms_path="${stripped_binary_path}.dynsyms"
85    funcsysms_path="${stripped_binary_path}.funcsyms"
86    keep_path="${stripped_binary_path}.keep"
87    debug_path="${stripped_binary_path}.debug"
88    mini_debug_path="${stripped_binary_path}.minidebug"
89
90    ${NM} -D ${binary} --format=posix --defined-only \
91            | awk '{ print $1 }' | sort > ${dynsyms_path}
92    ${NM} ${binary} --format=posix --defined-only \
93            | awk '{ if ($2 == "T" || $2 == "t" || $2 == "D") print $1 }' \
94            | sort > ${funcsysms_path}
95    comm -13 ${dynsyms_path} ${funcsysms_path} > ${keep_path}
96
97    ${OBJCOPY} --only-keep-debug ${binary} ${debug_path}
98    ${OBJCOPY} -S --remove-section .gdb_index --remove-section .comment \
99            --keep-symbols=${keep_path} ${debug_path} ${mini_debug_path}
100
101    ${STRIP} --strip-all --keep-section=.comment ${binary}
102
103    xz ${mini_debug_path}
104    ${OBJCOPY} --add-section .gnu_debugdata=${mini_debug_path}.xz ${binary}
105
106    rm -f ${dynsyms_path}
107    rm -f ${funcsysms_path}
108    rm -f ${keep_path}
109    rm -f ${debug_path}
110    rm -f ${mini_debug_path}
111    rm -f ${mini_debug_path}.xz
112}
113
114do_install () {
115    cp -u ${workdir}/out/Release/libjsvm.so ${TARGET_GEN_DIR}
116}
117
118do_env() {
119    # init workspace
120    out_dir=${TARGET_GEN_DIR}/out
121    workdir=${NODE_PATH}
122    [ -d "${out_dir}" ] || mkdir -p ${out_dir}
123    [ -L "${workdir}/out" ] || ln -s ${out_dir} ${workdir}/out
124
125    argurment+=" -fstack-protector-strong"
126    argurment+=" -Wl,-z,noexecstack"
127    argurment+=" -Wl,-z,relro"
128    argurment+=" -Wl,-z,now"
129    argurment+=" -pie"
130    if [ $1 -eq 1 ]; then
131        argurment+=" -ggdb3"
132    fi
133    if [[ "${TARGET_CPU}" = "arm" ]]; then
134        cflags="  --target=arm-linux-ohos"
135        cflags+=" --sysroot=${SYSROOT}"
136        cflags+=" -march=armv7-a"
137        cflags+=" -mfpu=neon"
138        cflags_host="-m32"
139        ARCH="arm"
140    elif [[ "${TARGET_CPU}" = "arm64" ]]; then
141        cflags="  --target=aarch64-linux-ohos"
142        cflags+=" --sysroot=${SYSROOT}"
143        cflags+=" -march=armv8-a"
144        cflags+=" -DV8_OS_OH=1"
145        cflags+=" -mfpu=neon"
146        cflags_host="-m64"
147        ARCH="aarch64"
148    elif [[ "${TARGET_CPU}" = "x86_64" ]]; then
149        export CC="${CCACHE_EXEC} gcc"
150        export CXX="${CCACHE_EXEC} g++"
151        return
152    else
153        die "not support target cpu"
154    fi
155
156    if [[ "${TARGET_CLANG_COVERAGE}" = "true" ]]; then
157        cflags+=" --coverage"
158    fi
159
160    cflags+=" ${argurment}"
161
162    # linux host env
163    HOST_OS="linux"
164    HOST_ARCH="x86_64"
165    export LINK_host="${CCACHE_EXEC} ${PREFIX}/clang++ ${cflags_host}"
166    export CXX_host="${CCACHE_EXEC} ${PREFIX}/clang++ ${cflags_host}"
167    export CC_host="${CCACHE_EXEC} ${PREFIX}/clang ${cflags_host}"
168    export AR_host=${PREFIX}/llvm-ar
169
170    # target env
171    export CC="${CCACHE_EXEC} ${PREFIX}/clang ${cflags}"
172    export CXX="${CCACHE_EXEC} ${PREFIX}/clang++ ${cflags}"
173    export LD="${PREFIX}/ld.lld"
174    export AS="${PREFIX}/llvm-as"
175    export AR="${PREFIX}/llvm-ar"
176    export STRIP="${PREFIX}/llvm-strip"
177    export OBJCOPY="${PREFIX}/llvm-objcopy"
178    export OBJDUMP="${PREFIX}/llvm-obidump"
179    export RANLIB="${PREFIX}/llvm-ranlib"
180    export NM="${PREFIX}/llvm-nm"
181    export STRINGS="${PREFIX}/llvm-strings"
182    export READELF="${PREFIX}/llvm-readelf"
183    env > ${out_dir}/log.do_env
184}
185