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. 14 15set -e 16set +e 17echo -e "\n\033[32m\t*********Welcome to OpenHarmony!*********\033[0m\n" 18echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++" 19function check_shell_environment() { 20 case $(uname -s) in 21 Linux) 22 shell_result=$(/bin/sh -c 'echo ${BASH_VERSION}') 23 if [ -n "${shell_result}" ]; then 24 echo -e "\033[32mSystem shell: bash ${shell_result}\033[0m" 25 else 26 echo -e "\033[33m Your system shell isn't bash, we recommend you to use bash, because some commands may not be supported in other shells, such as pushd and shopt are not supported in dash. \n You can follow these tips to modify the system shell to bash on Ubuntu: \033[0m" 27 echo -e "\033[33m [1]:Open the Terminal tool and execute the following command: sudo dpkg-reconfigure dash \n [2]:Enter the password and select <no> \033[0m" 28 fi 29 ;; 30 Darwin) 31 echo -e "\033[31m[OHOS ERROR] Darwin system is not supported yet\033[0m" 32 ;; 33 *) 34 echo -e "\033[31m[OHOS ERROR] Unsupported this system: $(uname -s)\033[0m" 35 exit 1 36 esac 37} 38 39check_shell_environment 40 41echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++" 42echo -e "\033[32mCurrent time: $(date +%F' '%H:%M:%S)\033[0m" 43echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++" 44echo -e "\033[32mBuild args: $@\033[0m" 45echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++" 46 47export SOURCE_ROOT_DIR=$(cd $(dirname $0);pwd) 48 49while [[ ! -f "${SOURCE_ROOT_DIR}/.gn" ]]; do 50 SOURCE_ROOT_DIR="$(dirname "${SOURCE_ROOT_DIR}")" 51 if [[ "${SOURCE_ROOT_DIR}" == "/" ]]; then 52 echo -e "\033[31m[OHOS ERROR] Cannot find source tree containing $(pwd)\033[0m" 53 exit 1 54 fi 55done 56 57if [[ "${SOURCE_ROOT_DIR}x" == "x" ]]; then 58 echo -e "\033[31m[OHOS ERROR] SOURCE_ROOT_DIR cannot be empty.\033[0m" 59 exit 1 60fi 61 62case $(uname -m) in 63 *x86_64) 64 host_cpu=x86_64 65 host_cpu_prefix="x86" 66 python_prefix="x86" 67 node_prefix="x64" 68 ;; 69 *arm* | *aarch64) 70 host_cpu=arm64 71 host_cpu_prefix="aarch64" 72 python_prefix="arm64" 73 node_prefix="aarch64" 74 ;; 75 *) 76 echo "\033[31m[OHOS ERROR] Unsupported host arch: $(uname -m)\033[0m" 77 RET=1 78 exit $RET 79esac 80 81case $(uname -s) in 82 Darwin) 83 HOST_DIR="darwin-$host_cpu_prefix" 84 PYTHON_DIR="darwin-$python_prefix" 85 HOST_OS="mac" 86 NODE_PLATFORM="darwin-x64" 87 ;; 88 Linux) 89 HOST_DIR="linux-$host_cpu_prefix" 90 PYTHON_DIR="linux-$python_prefix" 91 HOST_OS="linux" 92 NODE_PLATFORM="linux-$node_prefix" 93 ;; 94 *) 95 echo "\033[31m[OHOS ERROR] Unsupported host platform: $(uname -s)\033[0m" 96 RET=1 97 exit $RET 98esac 99 100# set python3 101PYTHON3_DIR=$(realpath ${SOURCE_ROOT_DIR}/prebuilts/python/${PYTHON_DIR}/*/ | tail -1) 102PYTHON3=${PYTHON3_DIR}/bin/python3 103PYTHON=${PYTHON3_DIR}/bin/python 104if [[ ! -f "${PYTHON3}" ]]; then 105 echo -e "\033[31m[OHOS ERROR] Please execute the build/prebuilts_download.sh \033[0m" 106 exit 1 107else 108 if [[ ! -f "${PYTHON}" ]]; then 109 ln -sf "${PYTHON3}" "${PYTHON}" 110 fi 111fi 112 113export PATH=${SOURCE_ROOT_DIR}/prebuilts/build-tools/${HOST_DIR}/bin:${PYTHON3_DIR}/bin:$PATH 114 115# set nodejs and ohpm 116EXPECTED_NODE_VERSION="14.21.1" 117export PATH=${SOURCE_ROOT_DIR}/prebuilts/build-tools/common/nodejs/node-v${EXPECTED_NODE_VERSION}-${NODE_PLATFORM}/bin:$PATH 118export NODE_HOME=${SOURCE_ROOT_DIR}/prebuilts/build-tools/common/nodejs/node-v${EXPECTED_NODE_VERSION}-${NODE_PLATFORM} 119export PATH=${SOURCE_ROOT_DIR}/prebuilts/build-tools/common/oh-command-line-tools/ohpm/bin:$PATH 120export PATH=${SOURCE_ROOT_DIR}/prebuilts/tool/command-line-tools/bin:$PATH 121chmod +x ${SOURCE_ROOT_DIR}/prebuilts/tool/command-line-tools/hvigor/bin/hvigorw 122echo "[OHOS INFO] Current Node.js version is $(node -v)" 123NODE_VERSION=$(node -v) 124if [ "$NODE_VERSION" != "v$EXPECTED_NODE_VERSION" ]; then 125 echo -e "\033[31m[OHOS ERROR] Node.js version mismatch. Expected $EXPECTED_NODE_VERSION but found $NODE_VERSION\033[0m" >&2 126 exit 1 127fi 128echo -e "\033[32m[OHOS INFO] Node.js version check passed!\033[0m" 129npm config set registry https://repo.huaweicloud.com/repository/npm/ 130npm config set @ohos:registry https://repo.harmonyos.com/npm/ 131npm config set strict-ssl false 132npm config set lockfile false 133cat $HOME/.npmrc | grep 'lockfile=false' > /dev/null || echo 'lockfile=false' >> $HOME/.npmrc > /dev/null 134 135function init_ohpm() { 136 TOOLS_INSTALL_DIR="${SOURCE_ROOT_DIR}/prebuilts/build-tools/common" 137 pushd ${TOOLS_INSTALL_DIR} > /dev/null 138 OHPM_HOME=${TOOLS_INSTALL_DIR}/../../tool/command-line-tools/ohpm/bin 139 chmod +x ${OHPM_HOME}/ohpm 140 export PATH=${OHPM_HOME}:$PATH 141 chmod +x ${OHPM_HOME}/init 142 ${OHPM_HOME}/init > /dev/null 143 echo "[OHOS INFO] Current ohpm version is $(ohpm -v)" 144 ohpm config set registry https://repo.harmonyos.com/ohpm/ 145 ohpm config set strict_ssl false 146 ohpm config set log_level debug 147 popd > /dev/null 148 if [[ -d "$HOME/.hvigor" ]]; then 149 rm -rf $HOME/.hvigor/daemon $HOME/.hvigor/wrapper 150 fi 151 mkdir -p $HOME/.hvigor/wrapper/tools 152 echo '{"dependencies": {"pnpm": "7.30.0"}}' > $HOME/.hvigor/wrapper/tools/package.json 153 pushd $HOME/.hvigor/wrapper/tools > /dev/null 154 echo "[OHOS INFO] installing pnpm..." 155 npm install --silent > /dev/null 156 popd > /dev/null 157 mkdir -p $HOME/.ohpm 158 echo '{"devDependencies":{"@ohos/hypium":"1.0.6"}}' > $HOME/.ohpm/oh-package.json5 159 pushd $HOME/.ohpm > /dev/null 160 echo "[OHOS INFO] installing hypium..." 161 ohpm install > /dev/null 162 popd > /dev/null 163} 164 165if [[ "$*" != *ohos-sdk* ]]; then 166 echo "[OHOS INFO] Ohpm initialization started..." 167 init_ohpm 168 if [[ "$?" -ne 0 ]]; then 169 echo -e "\033[31m[OHOS ERROR] ohpm initialization failed!\033[0m" 170 exit 1 171 fi 172 echo -e "\033[32m[OHOS INFO] ohpm initialization successful!\033[0m" 173fi 174echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n" 175 176echo -e "\033[32m[OHOS INFO] Start building...\033[0m\n" 177 178${PYTHON3} ${SOURCE_ROOT_DIR}/build/scripts/tools_checker.py 179 180if [[ "${@}" =~ "--ccache=false" || "${@}" =~ "--ccache false" ]]; then 181 ccache_args="--ccache=false" 182else 183 ccache_args="--ccache=true" 184fi 185if [[ "${@}" =~ "--xcache=true" || "${@}" =~ "--xcache true" || "${@}" =~ "--xcache" ]]; then 186 xcache_args="--xcache=true" 187else 188 xcache_args="--xcache=false" 189fi 190 191api_version=$(grep -m1 'api_version =' build/version.gni | sed -n 's/.*api_version = *"\([^"]*\)".*/\1/p') 192using_hb_new=true 193fetching_prebuilt_sdk_gn_args=false 194need_prebuilt_sdk=true 195generate_sbom=false 196force_prebuilt_sdk=false 197prebuilt_sdk_gn_args=() 198args_list=() 199 200for arg in "$@"; do 201 case "$arg" in 202 --using_hb_new=*) 203 value="${arg#*=}" 204 if [[ "$value" == "false" ]]; then 205 using_hb_new=false 206 python3 "${SOURCE_ROOT_DIR}/build/scripts/entry.py" --source-root-dir "${SOURCE_ROOT_DIR}" "$@" 207 exit $? 208 fi 209 args_list+=("$arg") 210 ;; 211 --prebuilts-sdk-gn-args) 212 fetching_prebuilt_sdk_gn_args=true 213 ;; 214 ohos-sdk|--no-prebuilt-sdk) 215 need_prebuilt_sdk=false 216 args_list+=("$arg") 217 ;; 218 --sbom|--sbom=*) 219 if [[ "$arg" == "--sbom" ]] || [[ "${arg#--sbom=}" != "false" ]]; then 220 generate_sbom=true 221 args_list+=("--gn-flags=--ide=json") 222 args_list+=("--gn-flags=--json-file-name=sbom/gn_gen.json") 223 fi 224 args_list+=("$arg") 225 ;; 226 --prebuilt-sdk) 227 force_prebuilt_sdk=true 228 ;; 229 *) 230 echo "arg:$arg" 231 if [[ "$fetching_prebuilt_sdk_gn_args" == "true" ]]; then 232 prebuilt_sdk_gn_args+=("$arg") 233 else 234 args_list+=("$arg") 235 fi 236 ;; 237 esac 238 shift 239done 240 241echo "prebuilts_sdk_gn_args:${prebuilt_sdk_gn_args[@]}" 242echo "args_list:${args_list[@]}" 243if [[ ! -d "${SOURCE_ROOT_DIR}/prebuilts/ohos-sdk/linux/${api_version}" && "${need_prebuilt_sdk}" == "true" || "${force_prebuilt_sdk}" == "true" ]]; then 244 "${SOURCE_ROOT_DIR}/build/build_scripts/build_ohos_sdk.sh" \ 245 "${SOURCE_ROOT_DIR}" \ 246 "${PYTHON3_DIR}" \ 247 "${HOST_OS}" \ 248 "${ccache_args}" \ 249 "${xcache_args}" \ 250 "${api_version}" \ 251 "${generate_sbom}" \ 252 "${prebuilt_sdk_gn_args[@]}" 253fi 254 255if [[ "$using_hb_new" == "true" ]]; then 256 python3 "${SOURCE_ROOT_DIR}/build/hb/main.py" build "${args_list[@]}" 257fi 258 259if [[ "$?" -ne 0 ]]; then 260 echo -e "\033[31m=====build ${product_name} error=====\033[0m" 261 exit 1 262fi 263echo -e "\033[32m=====build ${product_name} successful=====\033[0m" 264 265date +%F' '%H:%M:%S 266echo "++++++++++++++++++++++++++++++++++++++++" 267