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_install_hwasan () { 110 mkdir -p ${TARGET_GEN_DIR}/asan 111 cp -u ${workdir}/out/Release/libjsvm.so ${TARGET_GEN_DIR}/asan 112} 113 114do_env() { 115 # init workspace 116 out_dir=${TARGET_GEN_DIR}/out 117 workdir=${NODE_PATH} 118 [ -d "${out_dir}" ] || mkdir -p ${out_dir} 119 [ -L "${workdir}/out" ] || ln -s ${out_dir} ${workdir}/out 120 121 argurment+=" -fstack-protector-strong" 122 argurment+=" -Wl,-z,noexecstack" 123 argurment+=" -Wl,-z,relro" 124 argurment+=" -Wl,-z,now" 125 argurment+=" -pie" 126 127 if [[ "${TARGET_CPU}" = "arm" ]]; then 128 cflags=" --target=arm-linux-ohos" 129 cflags+=" --sysroot=${SYSROOT}" 130 cflags+=" -march=armv7-a" 131 cflags+=" -mfpu=neon" 132 cflags_host="-m32" 133 ARCH="arm" 134 elif [[ "${TARGET_CPU}" = "arm64" ]]; then 135 cflags=" --target=aarch64-linux-ohos" 136 cflags+=" --sysroot=${SYSROOT}" 137 cflags+=" -march=armv8-a" 138 cflags+=" -DV8_OS_OH=1" 139 cflags+=" -mfpu=neon" 140 cflags_host="-m64" 141 ARCH="aarch64" 142 elif [[ "${TARGET_CPU}" = "x86_64" ]]; then 143 export CC="${CCACHE_EXEC} gcc" 144 export CXX="${CCACHE_EXEC} g++" 145 return 146 else 147 die "not support target cpu" 148 fi 149 150 if [[ "${TARGET_CLANG_COVERAGE}" = "true" ]]; then 151 cflags+=" --coverage" 152 fi 153 154 if [[ "${IS_ASAN}" = "true" && "${USE_HWASAN}" = "true"]]; then 155 cflags+=" -shared-libasan" 156 cflags+=" -fsanitize=hwaddress" 157 cflags+=" -mllvm -hwasan-globals=0" 158 cflags+=" -fno-emulated-tls" 159 cflags+=" -fno-omit-frame-pointer" 160 fi 161 162 cflags+=" ${argurment}" 163 164 # linux host env 165 HOST_OS="linux" 166 HOST_ARCH="x86_64" 167 export LINK_host="${CCACHE_EXEC} ${PREFIX}/clang++ ${cflags_host}" 168 export CXX_host="${CCACHE_EXEC} ${PREFIX}/clang++ ${cflags_host}" 169 export CC_host="${CCACHE_EXEC} ${PREFIX}/clang ${cflags_host}" 170 export AR_host=${PREFIX}/llvm-ar 171 172 # target env 173 export CC="${CCACHE_EXEC} ${PREFIX}/clang ${cflags}" 174 export CXX="${CCACHE_EXEC} ${PREFIX}/clang++ ${cflags}" 175 export LD="${PREFIX}/ld.lld" 176 export AS="${PREFIX}/llvm-as" 177 export AR="${PREFIX}/llvm-ar" 178 export STRIP="${PREFIX}/llvm-strip" 179 export OBJCOPY="${PREFIX}/llvm-objcopy" 180 export OBJDUMP="${PREFIX}/llvm-obidump" 181 export RANLIB="${PREFIX}/llvm-ranlib" 182 export NM="${PREFIX}/llvm-nm" 183 export STRINGS="${PREFIX}/llvm-strings" 184 export READELF="${PREFIX}/llvm-readelf" 185 env > ${out_dir}/log.do_env 186} 187