diff --git a/build_jsvm.sh b/build_jsvm.sh new file mode 100755 index 0000000000000000000000000000000000000000..4f9d3431a79f843864a7085690bd9aea612f7777 --- /dev/null +++ b/build_jsvm.sh @@ -0,0 +1,136 @@ +#!/bin/bash +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -e + +declare SCRIPT_PATCH +declare SYSROOT +declare PREFIX +declare TARGET_CPU +declare TARGET_GEN_DIR +declare TARGET_PLATFORM + +export SCRIPT_PATCH=$(dirname $(readlink -f "$0")) + +source ${SCRIPT_PATCH}/build_jsvm_inter.sh + +options="$(getopt -o h "help,sysroot:,jsvm_path:,prefix:,target_cpu:,base_path:" -- "$@")" || usage + +eval set -- "$options" + +usage() { + echo "Tool $(basename "$0") Usage" + echo "Options:" + echo "-h|--help" + echo "--sysroot Sysroot path." + echo "--prefix Cross-compiler prefix." + echo "--target_cpu Cross-compile CPU types." + echo "--target_gen_dir " + echo " if target_gen_dir not set, will install only to the default path." + exit 0 +} + +die() { + echo $@ + exit 0 +} + +do_man_process() { + do_opt_process $@ + if [[ "${IS_ASAN}" = "true" && "${USE_HWASEN}" = "true" ]]; then + do_env 1 + do_fetch > ${out_dir}/log.do_fetch_asan_unstripped + do_patch > ${out_dir}/log.do_patch_asan_unstripped + do_configure > ${out_dir}/log.do_configure_asan_unstripped + do_compile > ${out_dir}/log.do_compile_asan_unstripped + do_install_asan > ${out_dir}/log.do_install_asan_unstripped + else + do_env 0 + do_fetch > ${out_dir}/log.do_fetch + do_patch > ${out_dir}/log.do_patch + do_configure > ${out_dir}/log.do_configure + do_compile > ${out_dir}/log.do_compile + do_install > ${out_dir}/log.do_install + do_strip + + do_env 1 + do_fetch > ${out_dir}/log.do_fetch_unstripped + do_patch > ${out_dir}/log.do_patch_unstripped + do_configure > ${out_dir}/log.do_configure_unstripped + do_compile > ${out_dir}/log.do_compile_unstripped + do_unstripped_copy + fi +} + +do_opt_process() { + while [[ $# -gt 0 ]]; do + case "$1" in + -h|--help) + usage + ;; + --sysroot) + export SYSROOT=$2 + shift + ;; + --jsvm_path) + export JSVM_PATH=$2 + shift + ;; + --prefix) + export PREFIX=$2 + shift + ;; + --target_cpu) + export TARGET_CPU=$2 + shift + ;; + --target_platform) + export TARGET_PLATFORM=$2 + shift + ;; + --target_gen_dir) + export TARGET_GEN_DIR=$2 + shift + ;; + --target_clang_coverage) + export TARGET_CLANG_COVERAGE=$2 + shift + ;; + --is_asan) + export IS_ASAN=$2 + shift + ;; + --use_hwasan) + export USE_HWASEN=$2 + shift + ;; + --cmake_path) + export CMAKE_PATH=$2 + shift + ;; + --target_out_dir) + export LIB_ROOT_DIR=$2 + shift + ;; + -I*) + export INCLUDE_DIRS="${INCLUDE_DIRS} -I${LIB_ROOT_DIR}/${1#-I}" + ;; + *) + ;; + esac + shift + done +} + +do_man_process $@ diff --git a/build_jsvm_inter.sh b/build_jsvm_inter.sh new file mode 100755 index 0000000000000000000000000000000000000000..7148ee20c82b7f0ba4ea9029e73f427faa44b014 --- /dev/null +++ b/build_jsvm_inter.sh @@ -0,0 +1,195 @@ +#!/bin/bash +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -ex + +do_fetch() { + echo "skip." +} + +do_patch() { + echo "skip." +} + +do_configure() { + use_libz_inner=0 + + if [ -f "${LIB_ROOT_DIR}/obj/vendor/huawei/foundation/bundlemanager/zlib_override/libz_inner.a" ];then + use_libz_inner=1 + fi + ${CMAKE_PATH} \ + -DLIB_ROOT_DIR=${LIB_ROOT_DIR}\ + -DTARGET_CPU=${TARGET_CPU}\ + -DUSE_LIBZ_INNER=${use_libz_inner}\ + -B${TARGET_GEN_DIR}/build\ + -S${JSVM_PATH}\ + -DCMAKE_C_FLAGS="${CMAKE_C_FLAGS}"\ + -DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS}" +} + +do_unstripped_copy() { + JSVM_UNSTRIPPED_PATH=${TARGET_GEN_DIR}/../../../lib.unstripped/arkcompiler/jsvm/ + mkdir -p ${JSVM_UNSTRIPPED_PATH} + cp -u ${workdir}/libjsvm.so ${JSVM_UNSTRIPPED_PATH} + cp -u ${JSVM_PATH}/../../vendor/huawei/binary/artifacts/js_engine_url/v8/${TARGET_CPU}/lib.unstripped_v8/lib.unstripped/libv8_shared.so ${JSVM_UNSTRIPPED_PATH} +} + +get_thread_num() { + quota_us_file="/sys/fs/cgroup/cpu/cpu.cfs_quota_us" + period_us_file="/sys/fs/cgroup/cpu/cpu.cfs_period_us" + if [ -f "${quota_us_file}" ]; then + cfs_quota_us=$(cat ${quota_us_file}) + fi + if [ -f "${period_us_file}" ]; then + cfs_period_us=$(cat ${period_us_file}) + fi + # Set the default value when the variable is empty. + cfs_quota_us=${cfs_quota_us:=-1} + cfs_period_us=${cfs_period_us:=0} + if [ "${cfs_quota_us}" != -1 -a "${cfs_period_us}" != 0 ]; then + PROCESSORS=$(expr ${cfs_quota_us} / ${cfs_period_us}) + echo "cpu.cfs_quota_us: "$PROCESSORS + else + PROCESSORS=$(cat /proc/cpuinfo | grep "processor" | wc -l) + echo "cpuinfo: "$PROCESSORS + fi +} + +do_compile() { + get_thread_num + cpu_num=$[PROCESSORS*2] + make VERBOSE=1 -j${cpu_num} -C${workdir} +} + +do_strip() { + stripped_binary_path=${TARGET_GEN_DIR}/libjsvm.so + binary=${stripped_binary_path} + echo "${binary}" + dynsyms_path="${stripped_binary_path}.dynsyms" + funcsysms_path="${stripped_binary_path}.funcsyms" + keep_path="${stripped_binary_path}.keep" + debug_path="${stripped_binary_path}.debug" + mini_debug_path="${stripped_binary_path}.minidebug" + + ${NM} -D ${binary} --format=posix --defined-only \ + | awk '{ print $1 }' | sort > ${dynsyms_path} + ${NM} ${binary} --format=posix --defined-only \ + | awk '{ if ($2 == "T" || $2 == "t" || $2 == "D") print $1 }' \ + | sort > ${funcsysms_path} + comm -13 ${dynsyms_path} ${funcsysms_path} > ${keep_path} + + ${OBJCOPY} --only-keep-debug ${binary} ${debug_path} + ${OBJCOPY} -S --remove-section .gdb_index --remove-section .comment \ + --keep-symbols=${keep_path} ${debug_path} ${mini_debug_path} + + ${STRIP} --strip-all --keep-section=.comment ${binary} + + xz ${mini_debug_path} + ${OBJCOPY} --add-section .gnu_debugdata=${mini_debug_path}.xz ${binary} + + rm -f ${dynsyms_path} + rm -f ${funcsysms_path} + rm -f ${keep_path} + rm -f ${debug_path} + rm -f ${mini_debug_path} + rm -f ${mini_debug_path}.xz +} + +do_install () { + cp -u ${workdir}/libjsvm.so ${TARGET_GEN_DIR} +} + +do_install_asan() { + # todo replace libv8_shared.so with hwasan + mkdir -p ${TARGET_GEN_DIR}/asan + cp -u ${workdir}/libjsvm.so ${TARGET_GEN_DIR}/asan + cp -u ${JSVM_PATH}/../../vendor/huawei/binary/artifacts/js_engine_url/v8/${TARGET_CPU}/lib.unstripped_v8/lib.unstripped/libv8_shared.so ${TARGET_GEN_DIR}/asan + + mkdir -p ${TARGET_GEN_DIR}/../../../../../lib.unstripped/jsvm/ + cp -u ${workdir}/libjsvm.so ${TARGET_GEN_DIR}/../../../../../lib.unstripped/jsvm/ + cp -u ${JSVM_PATH}/../../vendor/huawei/binary/artifacts/js_engine_url/v8/${TARGET_CPU}/lib.unstripped_v8/lib.unstripped/libv8_shared.so ${TARGET_GEN_DIR}/../../../../../lib.unstripped/jsvm/ +} + +do_env() { + # init workspace + out_dir=${TARGET_GEN_DIR}/build + workdir=${TARGET_GEN_DIR}/build + # alway rebuild + mkdir -p ${workdir} + + argurment+=" -fstack-protector-strong" + argurment+=" -Wl,-z,noexecstack" + argurment+=" -Wl,-z,relro" + argurment+=" -Wl,-z,now" + argurment+=" -pie" + if [ $1 -eq 1 ]; then + argurment+=" -ggdb3" + fi + if [[ "${TARGET_CPU}" = "arm" ]]; then + cflags=" --target=arm-linux-ohos" + cflags+=" --sysroot=${SYSROOT}" + cflags+=" -march=armv7-a" + cflags+=" -mfpu=neon" + cflags+=" -mbranch-protection=pac-ret+b-key+bti" + cflags_host="-m32" + ARCH="arm" + elif [[ "${TARGET_CPU}" = "arm64" ]]; then + cflags=" --target=aarch64-linux-ohos" + cflags+=" --sysroot=${SYSROOT}" + cflags+=" -march=armv8-a" + cflags+=" -DV8_OS_OH=1" + cflags+=" -mfpu=neon" + cflags+=" -mbranch-protection=pac-ret+b-key+bti" + cflags_host="-m64" + ARCH="aarch64" + elif [[ "${TARGET_CPU}" = "x86_64" ]]; then + cflags="--target=x86_64-linux-ohos" + cflags+=" --sysroot=${SYSROOT}" + cflags+=" -DV8_OS_OH=1" + ARCH="x86_64" + else + die "not support target cpu" + fi + + if [[ "${TARGET_CLANG_COVERAGE}" = "true" ]]; then + cflags+=" --coverage" + fi + + if [[ "$1" = 1 && "${IS_ASAN}" = "true" && "${USE_HWASEN}" = "true" ]]; then + cflags+=" -shared-libasan" + cflags+=" -fsanitize=hwaddress" + cflags+=" -mllvm -hwasan-globals=0" + cflags+=" -fno-emulated-tls" + cflags+=" -fno-omit-frame-pointer" + fi + + cflags+=" ${argurment}" + + # target env + export CC="${CCACHE_EXEC} ${PREFIX}/clang ${INCLUDE_DIRS}" + export CXX="${CCACHE_EXEC} ${PREFIX}/clang++ ${INCLUDE_DIRS}" + export CMAKE_C_FLAGS="${cflags}" + export CMAKE_CXX_FLAGS="${cflags}" + export LD="${PREFIX}/ld.lld" + export AS="${PREFIX}/llvm-as" + export AR="${PREFIX}/llvm-ar" + export STRIP="${PREFIX}/llvm-strip" + export OBJCOPY="${PREFIX}/llvm-objcopy" + export OBJDUMP="${PREFIX}/llvm-obidump" + export RANLIB="${PREFIX}/llvm-ranlib" + export NM="${PREFIX}/llvm-nm" + export STRINGS="${PREFIX}/llvm-strings" + export READELF="${PREFIX}/llvm-readelf" + env > ${out_dir}/log.do_env +} diff --git a/copy_v8.sh b/copy_v8.sh index 7f5d18a7cee42b4088c6df61cc00383417e67fb2..ec63bc5eb2fbdc2679d4f415975a71960e6c2ca1 100755 --- a/copy_v8.sh +++ b/copy_v8.sh @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -export SCRIPT_PATCH=$(dirname $(readlink -f "$0")) export TARGET_GEN_DIR=$2 - -cp -u ${SCRIPT_PATCH}/../../third_party/node/deps/v8/libv8_shared.so ${TARGET_GEN_DIR}/libv8_shared.so -cp -r ${SCRIPT_PATCH}/../../third_party/node/deps/v8/include/v8-include ${TARGET_GEN_DIR}/v8-include +v8_path=$3 +target_cpu=$4 +cp -u ${v8_path}/v8/${target_cpu}/libv8_shared.so ${TARGET_GEN_DIR}/libv8_shared.so +cp -r ${v8_path}/v8-include/v8-include ${TARGET_GEN_DIR}/v8-include