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
42get_thread_num() {
43    quota_us_file="/sys/fs/cgroup/cpu/cpu.cfs_quota_us"
44    period_us_file="/sys/fs/cgroup/cpu/cpu.cfs_period_us"
45    if [ -f "${quota_us_file}" ]; then
46        cfs_quota_us=$(cat ${quota_us_file})
47    fi
48    if [ -f "${period_us_file}" ]; then
49        cfs_period_us=$(cat ${period_us_file})
50    fi
51    # Set the default value when the variable is empty.
52    cfs_quota_us=${cfs_quota_us:=-1}
53    cfs_period_us=${cfs_period_us:=0}
54    if [ "${cfs_quota_us}" != -1 -a "${cfs_period_us}" != 0 ]; then
55        PROCESSORS=$(expr ${cfs_quota_us} / ${cfs_period_us})
56        echo "cpu.cfs_quota_us: "$PROCESSORS
57    else
58        PROCESSORS=$(cat /proc/cpuinfo | grep "processor" | wc -l)
59        echo "cpuinfo: "$PROCESSORS
60    fi
61}
62
63do_compile() {
64    pushd ${workdir}
65    get_thread_num
66    cpu_num=$[PROCESSORS*2]
67    make -j${cpu_num}
68    popd
69}
70
71do_strip() {
72    stripped_binary_path=${TARGET_GEN_DIR}/libjsvm.so
73    binary=${stripped_binary_path}
74    echo "${binary}"
75    dynsyms_path="${stripped_binary_path}.dynsyms"
76    funcsysms_path="${stripped_binary_path}.funcsyms"
77    keep_path="${stripped_binary_path}.keep"
78    debug_path="${stripped_binary_path}.debug"
79    mini_debug_path="${stripped_binary_path}.minidebug"
80
81    ${NM} -D ${binary} --format=posix --defined-only \
82            | awk '{ print $1 }' | sort > ${dynsyms_path}
83    ${NM} ${binary} --format=posix --defined-only \
84            | awk '{ if ($2 == "T" || $2 == "t" || $2 == "D") print $1 }' \
85            | sort > ${funcsysms_path}
86    comm -13 ${dynsyms_path} ${funcsysms_path} > ${keep_path}
87
88    ${OBJCOPY} --only-keep-debug ${binary} ${debug_path}
89    ${OBJCOPY} -S --remove-section .gdb_index --remove-section .comment \
90            --keep-symbols=${keep_path} ${debug_path} ${mini_debug_path}
91
92    ${STRIP} --strip-all --keep-section=.comment ${binary}
93
94    xz ${mini_debug_path}
95    ${OBJCOPY} --add-section .gnu_debugdata=${mini_debug_path}.xz ${binary}
96
97    rm -f ${dynsyms_path}
98    rm -f ${funcsysms_path}
99    rm -f ${keep_path}
100    rm -f ${debug_path}
101    rm -f ${mini_debug_path}
102    rm -f ${mini_debug_path}.xz
103}
104
105do_install () {
106    cp -u ${workdir}/out/Release/libjsvm.so ${TARGET_GEN_DIR}
107}
108
109do_env() {
110    # init workspace
111    out_dir=${TARGET_GEN_DIR}/out
112    workdir=${NODE_PATH}
113    [ -d "${out_dir}" ] || mkdir -p ${out_dir}
114    [ -L "${workdir}/out" ] || ln -s ${out_dir} ${workdir}/out
115
116    argurment+=" -fstack-protector-strong"
117    argurment+=" -Wl,-z,noexecstack"
118    argurment+=" -Wl,-z,relro"
119    argurment+=" -Wl,-z,now"
120    argurment+=" -pie"
121
122    if [[ "${TARGET_CPU}" = "arm" ]]; then
123        cflags="  --target=arm-linux-ohos"
124        cflags+=" --sysroot=${SYSROOT}"
125        cflags+=" -march=armv7-a"
126        cflags+=" -mfpu=neon"
127        cflags_host="-m32"
128        ARCH="arm"
129    elif [[ "${TARGET_CPU}" = "arm64" ]]; then
130        cflags="  --target=aarch64-linux-ohos"
131        cflags+=" --sysroot=${SYSROOT}"
132        cflags+=" -march=armv8-a"
133        cflags+=" -DV8_OS_OH=1"
134        cflags+=" -mfpu=neon"
135        cflags_host="-m64"
136        ARCH="aarch64"
137    elif [[ "${TARGET_CPU}" = "x86_64" ]]; then
138        export CC="${CCACHE_EXEC} gcc"
139        export CXX="${CCACHE_EXEC} g++"
140        return
141    else
142        die "not support target cpu"
143    fi
144
145    if [[ "${TARGET_CLANG_COVERAGE}" = "true" ]]; then
146        cflags+=" --coverage"
147    fi
148
149    cflags+=" ${argurment}"
150
151    # linux host env
152    HOST_OS="linux"
153    HOST_ARCH="x86_64"
154    export LINK_host="${CCACHE_EXEC} ${PREFIX}/clang++ ${cflags_host}"
155    export CXX_host="${CCACHE_EXEC} ${PREFIX}/clang++ ${cflags_host}"
156    export CC_host="${CCACHE_EXEC} ${PREFIX}/clang ${cflags_host}"
157    export AR_host=${PREFIX}/llvm-ar
158
159    # target env
160    export CC="${CCACHE_EXEC} ${PREFIX}/clang ${cflags}"
161    export CXX="${CCACHE_EXEC} ${PREFIX}/clang++ ${cflags}"
162    export LD="${PREFIX}/ld.lld"
163    export AS="${PREFIX}/llvm-as"
164    export AR="${PREFIX}/llvm-ar"
165    export STRIP="${PREFIX}/llvm-strip"
166    export OBJCOPY="${PREFIX}/llvm-objcopy"
167    export OBJDUMP="${PREFIX}/llvm-obidump"
168    export RANLIB="${PREFIX}/llvm-ranlib"
169    export NM="${PREFIX}/llvm-nm"
170    export STRINGS="${PREFIX}/llvm-strings"
171    export READELF="${PREFIX}/llvm-readelf"
172    env > ${out_dir}/log.do_env
173}
174