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 -ex 16 17do_fetch() { 18 echo "skip." 19} 20 21do_patch() { 22 echo "skip." 23} 24 25do_configure() { 26 use_libz_inner=0 27 28 if [ -f "${LIB_ROOT_DIR}/obj/vendor/${DEPENDENCY_TAG}/foundation/bundlemanager/zlib_override/libz_inner.a" ];then 29 use_libz_inner=1 30 fi 31 32 ${CMAKE_PATH} \ 33 -DLIB_ROOT_DIR=${LIB_ROOT_DIR}\ 34 -DTARGET_CPU=${TARGET_CPU}\ 35 -DUSE_LIBZ_INNER=${use_libz_inner}\ 36 -B${TARGET_GEN_DIR}/build\ 37 -S${JSVM_PATH}\ 38 -DCMAKE_C_FLAGS="${CMAKE_C_FLAGS}"\ 39 -DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS}"\ 40 -DDEPENDENCY_TAG="${DEPENDENCY_TAG}" 41} 42 43do_unstripped_copy() { 44 JSVM_UNSTRIPPED_PATH=${TARGET_GEN_DIR}/../../../lib.unstripped/arkcompiler/jsvm/ 45 mkdir -p ${JSVM_UNSTRIPPED_PATH} 46 cp -u ${workdir}/libjsvm.so ${JSVM_UNSTRIPPED_PATH} 47 cp -u ${JS_ENGINE_URL}/v8/${TARGET_CPU}/lib.unstripped_v8/lib.unstripped/libv8_shared.so ${JSVM_UNSTRIPPED_PATH} 48} 49 50get_thread_num() { 51 quota_us_file="/sys/fs/cgroup/cpu/cpu.cfs_quota_us" 52 period_us_file="/sys/fs/cgroup/cpu/cpu.cfs_period_us" 53 if [ -f "${quota_us_file}" ]; then 54 cfs_quota_us=$(cat ${quota_us_file}) 55 fi 56 if [ -f "${period_us_file}" ]; then 57 cfs_period_us=$(cat ${period_us_file}) 58 fi 59 # Set the default value when the variable is empty. 60 cfs_quota_us=${cfs_quota_us:=-1} 61 cfs_period_us=${cfs_period_us:=0} 62 if [ "${cfs_quota_us}" != -1 -a "${cfs_period_us}" != 0 ]; then 63 PROCESSORS=$(expr ${cfs_quota_us} / ${cfs_period_us}) 64 echo "cpu.cfs_quota_us: "$PROCESSORS 65 else 66 PROCESSORS=$(cat /proc/cpuinfo | grep "processor" | wc -l) 67 echo "cpuinfo: "$PROCESSORS 68 fi 69} 70 71do_compile() { 72 get_thread_num 73 cpu_num=$[PROCESSORS*2] 74 make VERBOSE=1 -j${cpu_num} -C${workdir} 75} 76 77do_strip() { 78 stripped_binary_path=${TARGET_GEN_DIR}/libjsvm.so 79 binary=${stripped_binary_path} 80 echo "${binary}" 81 dynsyms_path="${stripped_binary_path}.dynsyms" 82 funcsysms_path="${stripped_binary_path}.funcsyms" 83 keep_path="${stripped_binary_path}.keep" 84 debug_path="${stripped_binary_path}.debug" 85 mini_debug_path="${stripped_binary_path}.minidebug" 86 87 ${NM} -D ${binary} --format=posix --defined-only \ 88 | awk '{ print $1 }' | sort > ${dynsyms_path} 89 ${NM} ${binary} --format=posix --defined-only \ 90 | awk '{ if ($2 == "T" || $2 == "t" || $2 == "D") print $1 }' \ 91 | sort > ${funcsysms_path} 92 comm -13 ${dynsyms_path} ${funcsysms_path} > ${keep_path} 93 94 ${OBJCOPY} --only-keep-debug ${binary} ${debug_path} 95 ${OBJCOPY} -S --remove-section .gdb_index --remove-section .comment \ 96 --keep-symbols=${keep_path} ${debug_path} ${mini_debug_path} 97 98 ${STRIP} --strip-all ${binary} 99 100 xz ${mini_debug_path} 101 ${OBJCOPY} --add-section .gnu_debugdata=${mini_debug_path}.xz ${binary} 102 103 rm -f ${dynsyms_path} 104 rm -f ${funcsysms_path} 105 rm -f ${keep_path} 106 rm -f ${debug_path} 107 rm -f ${mini_debug_path} 108 rm -f ${mini_debug_path}.xz 109} 110 111do_install () { 112 cp -u ${workdir}/libjsvm.so ${TARGET_GEN_DIR} 113} 114 115do_install_asan() { 116 # todo replace libv8_shared.so with hwasan 117 mkdir -p ${TARGET_GEN_DIR}/asan 118 cp -u ${workdir}/libjsvm.so ${TARGET_GEN_DIR}/asan 119 cp -u ${JS_ENGINE_URL}/v8/${TARGET_CPU}/lib.unstripped_v8/lib.unstripped/libv8_shared.so ${TARGET_GEN_DIR}/asan 120 121 mkdir -p ${TARGET_GEN_DIR}/../../../../../lib.unstripped/jsvm/ 122 cp -u ${workdir}/libjsvm.so ${TARGET_GEN_DIR}/../../../../../lib.unstripped/jsvm/ 123 cp -u ${JS_ENGINE_URL}/v8/${TARGET_CPU}/lib.unstripped_v8/lib.unstripped/libv8_shared.so ${TARGET_GEN_DIR}/../../../../../lib.unstripped/jsvm/ 124} 125 126do_env() { 127 # init workspace 128 out_dir=${TARGET_GEN_DIR}/build 129 workdir=${TARGET_GEN_DIR}/build 130 # alway rebuild 131 mkdir -p ${workdir} 132 133 argurment+=" -D__MUSL__ -D_LIBCPP_HAS_MUSL_LIBC -DOHOS_JS_ENGINE" 134 argurment+=" -fstack-protector-strong" 135 argurment+=" -Wl,-z,noexecstack" 136 argurment+=" -Wl,-z,relro" 137 argurment+=" -Wl,-z,now" 138 argurment+=" -pie" 139 if [ $1 -eq 1 ]; then 140 argurment+=" -ggdb3" 141 fi 142 if [[ "${TARGET_CPU}" = "arm" ]]; then 143 cflags=" --target=arm-linux-ohos" 144 cflags+=" --sysroot=${SYSROOT}" 145 if [[ "${DEPENDENCY_TAG}" = "default" ]]; then 146 cflags+=" -isystem ${SYSROOT}/usr/include/arm-linux-ohos" 147 fi 148 cflags+=" -march=armv7-a" 149 cflags+=" -mfpu=neon" 150 cflags+=" -mbranch-protection=pac-ret+b-key+bti" 151 cflags_host="-m32" 152 ARCH="arm" 153 elif [[ "${TARGET_CPU}" = "arm64" ]]; then 154 cflags=" --target=aarch64-linux-ohos" 155 cflags+=" --sysroot=${SYSROOT}" 156 if [[ "${DEPENDENCY_TAG}" = "default" ]]; then 157 cflags+=" -isystem ${SYSROOT}/usr/include/aarch64-linux-ohos" 158 fi 159 cflags+=" -march=armv8-a" 160 cflags+=" -DV8_OS_OH=1" 161 cflags+=" -mfpu=neon" 162 cflags+=" -mbranch-protection=pac-ret+b-key+bti" 163 cflags_host="-m64" 164 ARCH="aarch64" 165 elif [[ "${TARGET_CPU}" = "x86_64" ]]; then 166 argurment+=" -I${PREFIX}/../include/c++/v1" 167 cflags="--target=x86_64-linux-ohos" 168 cflags+=" --sysroot=${SYSROOT}" 169 cflags+=" -isystem ${SYSROOT}/usr/include/x86_64-linux-ohos" 170 cflags+=" -DV8_OS_OH=1" 171 ARCH="x86_64" 172 else 173 die "not support target cpu" 174 fi 175 176 if [[ "${TARGET_CLANG_COVERAGE}" = "true" ]]; then 177 cflags+=" --coverage" 178 fi 179 180 if [[ "$1" = 1 && "${IS_ASAN}" = "true" && "${USE_HWASEN}" = "true" ]]; then 181 cflags+=" -shared-libasan" 182 cflags+=" -fsanitize=hwaddress" 183 cflags+=" -mllvm -hwasan-globals=0" 184 cflags+=" -fno-emulated-tls" 185 cflags+=" -fno-omit-frame-pointer" 186 fi 187 188 cflags+=" ${argurment}" 189 190 # target env 191 export CC="${CCACHE_EXEC} ${PREFIX}/clang ${INCLUDE_DIRS}" 192 export CXX="${CCACHE_EXEC} ${PREFIX}/clang++ ${INCLUDE_DIRS}" 193 export CMAKE_C_FLAGS="${cflags}" 194 export CMAKE_CXX_FLAGS="${cflags}" 195 export LD="${PREFIX}/ld.lld" 196 export AS="${PREFIX}/llvm-as" 197 export AR="${PREFIX}/llvm-ar" 198 export STRIP="${PREFIX}/llvm-strip" 199 export OBJCOPY="${PREFIX}/llvm-objcopy" 200 export OBJDUMP="${PREFIX}/llvm-obidump" 201 export RANLIB="${PREFIX}/llvm-ranlib" 202 export NM="${PREFIX}/llvm-nm" 203 export STRINGS="${PREFIX}/llvm-strings" 204 export READELF="${PREFIX}/llvm-readelf" 205 env > ${out_dir}/log.do_env 206} 207