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