1#!/bin/bash 2# Copyright (c) 2021 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. 14set -e 15 16script_path=$(cd $(dirname $0);pwd) 17code_dir=$(dirname ${script_path}) 18 19if [[ "${@}" =~ "--tool-repo" && -f "${code_dir}/prebuilts.sh" ]]; then 20 # prebuilts.sh should be a symbolic link to a prebuilts_download.sh created by oneself. 21 bash ${code_dir}/prebuilts.sh $@ 22else 23while [ $# -gt 0 ]; do 24 case "$1" in 25 -skip-ssl|--skip-ssl) # wget、npm skip ssl check, which will allow 26 # hacker to get and modify data stream between server and client! 27 SKIP_SSL=YES 28 ;; 29 -h|--help) 30 HELP=YES 31 ;; 32 --disable-rich) # disable the rich module of python 33 DISABLE_RICH=YES 34 ;; 35 --enable-symlink) # enable symlink while copying node_modules 36 ENABLE_SYMLINK=YES 37 ;; 38 --build-arkuix) 39 BUILD_ARKUIX=YES 40 ;; 41 --tool-repo) 42 TOOL_REPO="$2" 43 shift 44 ;; 45 --tool-repo=*) 46 TOOL_REPO="${1#--tool-repo=}" 47 ;; 48 --npm-registry) 49 NPM_REGISTRY="$2" 50 shift 51 ;; 52 --npm-registry=*) 53 NPM_REGISTRY="${1#--npm-registry=}" 54 ;; 55 --trusted-host) 56 TRUSTED_HOST="$2" 57 shift 58 ;; 59 --trusted-host=*) 60 TRUSTED_HOST="${1#--trusted-host=}" 61 ;; 62 --pypi-url) # python package index url 63 PYPI_URL="$2" 64 shift 65 ;; 66 --pypi-url=*) 67 PYPI_URL="${1#--pypi-url=}" 68 ;; 69 --download-sdk) # Download the SDK if this flag is set 70 DOWNLOAD_SDK=YES 71 ;; 72 *) 73 echo "$0: Warning: unsupported parameter: $1" >&2 74 ;; 75 esac 76 shift 77done 78 79case $(uname -s) in 80 Linux) 81 host_platform=linux 82 glibc_version=$(getconf GNU_LIBC_VERSION | grep -oE '[0-9].[0-9]{2}') 83 ;; 84 Darwin) 85 host_platform=darwin 86 ;; 87 *) 88 echo "Unsupported host platform: $(uname -s)" 89 exit 1 90esac 91 92case $(uname -m) in 93 arm64) 94 host_cpu=arm64 95 host_cpu_prefix=arm64 96 ;; 97 aarch64) 98 host_cpu=arm64 99 host_cpu_prefix=aarch64 100 ;; 101 *) 102 host_cpu=x86_64 103 host_cpu_prefix=x86 104esac 105 106if [ "X${SKIP_SSL}" == "XYES" ];then 107 wget_ssl_check="--skip-ssl" 108else 109 wget_ssl_check='' 110fi 111 112if [ "X${HELP}" == "XYES" ];then 113 help="-h" 114else 115 help='' 116fi 117 118if [ "X${ENABLE_SYMLINK}" == "XYES" ];then 119 enable_symlink="--enable-symlink" 120else 121 enable_symlink='' 122fi 123 124if [ ! -z "$TOOL_REPO" ];then 125 tool_repo="--tool-repo $TOOL_REPO" 126else 127 tool_repo='' 128fi 129 130if [ ! -z "$NPM_REGISTRY" ];then 131 npm_registry="--npm-registry $NPM_REGISTRY" 132else 133 npm_registry='' 134fi 135 136if [ ! -z "$TRUSTED_HOST" ];then 137 trusted_host=$TRUSTED_HOST 138elif [ ! -z "$PYPI_URL" ];then 139 trusted_host=${PYPI_URL/#*:\/\//} # remove prefix part such as http:// https:// etc. 140 trusted_host=${trusted_host/%[:\/]*/} # remove suffix part including the port number 141else 142 trusted_host='repo.huaweicloud.com' 143fi 144 145if [ ! -z "$PYPI_URL" ];then 146 pypi_url=$PYPI_URL 147else 148 pypi_url='http://repo.huaweicloud.com/repository/pypi/simple' 149fi 150 151if [ $UID -ne 0 ]; then 152 npm_para='' 153else 154 npm_para='--unsafe-perm' 155fi 156 157if [ "X${BUILD_ARKUIX}" == "XYES" ];then 158 build_arkuix="--build-arkuix" 159else 160 build_arkuix='' 161fi 162 163if [ "X${DISABLE_RICH}" == "XYES" ];then 164 disable_rich='--disable-rich' 165else 166 set +e 167 pip3 install --trusted-host $trusted_host -i $pypi_url rich; 168 if [ $? -eq 0 ];then 169 echo "rich installed successfully" 170 else 171 disable_rich='--disable-rich' 172 fi 173 set -e 174fi 175 176cpu="--host-cpu $host_cpu" 177platform="--host-platform $host_platform" 178if [[ "${glibc_version}" < "2.35" ]]; then 179 glibc_version="--glibc-version GLIBC2.27" 180else 181 glibc_version="--glibc-version GLIBC2.35" 182fi 183echo "prebuilts_download start" 184if [ -d "${code_dir}/prebuilts/build-tools/common/nodejs" ];then 185 rm -rf "${code_dir}/prebuilts/build-tools/common/nodejs" 186 echo "remove nodejs" 187fi 188python3 "${code_dir}/build/prebuilts_download.py" $wget_ssl_check $tool_repo $npm_registry $help $cpu $platform $npm_para $disable_rich $enable_symlink $build_arkuix $glibc_version 189if [ -f "${code_dir}/prebuilts/cmake/linux-${host_cpu_prefix}/bin/ninja" ];then 190 rm -rf "${code_dir}/prebuilts/cmake/linux-${host_cpu_prefix}/bin/ninja" 191fi 192if [ -f "${code_dir}/prebuilts/cmake/windows-x86/bin/ninja.exe" ];then 193 rm -rf "${code_dir}/prebuilts/cmake/windows-x86/bin/ninja.exe" 194fi 195if [ -f "${code_dir}/prebuilts/cmake/darwin-universal/bin/ninja" ];then 196 rm -rf "${code_dir}/prebuilts/cmake/darwin-universal/bin/ninja" 197fi 198echo "remove ninja in cmake" 199echo "prebuilts_download end" 200 201PYTHON_PATH=$(realpath $code_dir/prebuilts/python/${host_platform}-${host_cpu_prefix}/*/bin | tail -1) 202 203if [[ "${host_platform}" == "linux" ]]; then 204 sed -i "1s%.*%#!/usr/bin/env python3%" "${PYTHON_PATH}/pip3" 205elif [[ "${host_platform}" == "darwin" ]]; then 206 sed -i "" "1s%.*%#!/usr/bin/env python3%" "${PYTHON_PATH}/pip3" 207fi 208prebuild_python3_path="${PYTHON_PATH}/python3" 209prebuild_pip3_path="${PYTHON_PATH}/pip3" 210$prebuild_python3_path $prebuild_pip3_path install --trusted-host $trusted_host -i $pypi_url idna\>\=3.7 urllib3\>\=1.26.29 pyyaml requests\>\=2.32.1 prompt_toolkit\=\=1.0.14 asn1crypto cryptography json5\=\=0.9.6 211if [[ "$DOWNLOAD_SDK" == "YES" ]] && [[ ! -d "${code_dir}/prebuilts/ohos-sdk-12" && ! -d "${code_dir}/prebuilts/ohos-sdk/12" ]]; then 212 $prebuild_python3_path ${code_dir}/build/scripts/download_sdk.py --branch OpenHarmony-5.0.0-Release --product-name ohos-sdk-full-5.0.0 --api-version 12 213 mkdir -p ${code_dir}/prebuilts/ohos-sdk-12 214 mv ${code_dir}/prebuilts/ohos-sdk ${code_dir}/prebuilts/ohos-sdk-12 215fi 216 217# llvm_ndk is merged form llvm and libcxx-ndk for compiling the native of hap 218if [[ "${host_platform}" == "linux" && "${host_cpu}" == "arm64" ]]; then 219 llvm_dir="${code_dir}/prebuilts/clang/ohos/linux-aarch64" 220else 221 llvm_dir="${code_dir}/prebuilts/clang/ohos/linux-x86_64" 222fi 223llvm_dir_win="${code_dir}/prebuilts/clang/ohos/windows-x86_64" 224llvm_dir_mac_x86="${code_dir}/prebuilts/clang/ohos/darwin-x86_64" 225llvm_dir_mac_arm64="${code_dir}/prebuilts/clang/ohos/darwin-arm64" 226llvm_dir_ohos_arm64="${code_dir}/prebuilts/clang/ohos/ohos-arm64" 227llvm_dir_list=($llvm_dir $llvm_dir_win $llvm_dir_mac_x86 $llvm_dir_mac_arm64 $llvm_dir_ohos_arm64) 228 229# copy libcxx-ndk library outside c++ 230function copy_inside_cxx(){ 231for i in ${llvm_dir_list[@]} 232do 233 libcxx_dir="${i}/libcxx-ndk/lib" 234 if [[ -d "${i}/libcxx-ndk" ]]; then 235 for file in $(ls ${libcxx_dir}) 236 do 237 if [ ! -d "${libcxx_dir}/${file}/c++" ];then 238 $(mkdir -p ${libcxx_dir}/c++) 239 $(cp -r ${libcxx_dir}/${file}/* ${libcxx_dir}/c++) 240 $(mv ${libcxx_dir}/c++ ${libcxx_dir}/${file}/c++) 241 fi 242 done 243 fi 244done 245} 246 247function update_llvm_ndk(){ 248if [[ -e "${llvm_dir}/llvm_ndk" ]];then 249 rm -rf "${llvm_dir}/llvm_ndk" 250fi 251mkdir -p "${llvm_dir}/llvm_ndk" 252cp -af "${llvm_dir}/llvm/include" "${llvm_dir}/llvm_ndk" 253cp -rfp "${llvm_dir}/libcxx-ndk/include" "${llvm_dir}/llvm_ndk" 254} 255 256if [[ "${BUILD_ARKUIX}" != "YES" ]]; then 257 copy_inside_cxx 258 echo "======copy inside cxx finished!======" 259 if [[ "${host_platform}" == "linux" ]]; then 260 update_llvm_ndk 261 echo "======update llvm ndk finished!======" 262 fi 263fi 264echo -e "\n" 265fi 266