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 -s) in 63 Darwin) 64 HOST_DIR="darwin-x86" 65 HOST_OS="mac" 66 NODE_PLATFORM="darwin-x64" 67 ;; 68 Linux) 69 HOST_DIR="linux-x86" 70 HOST_OS="linux" 71 NODE_PLATFORM="linux-x64" 72 ;; 73 *) 74 echo "\033[31m[OHOS ERROR] Unsupported host platform: $(uname -s)\033[0m" 75 RET=1 76 exit $RET 77esac 78 79# set python3 80PYTHON3_DIR=${SOURCE_ROOT_DIR}/prebuilts/python/${HOST_DIR}/current/ 81PYTHON3=${PYTHON3_DIR}/bin/python3 82PYTHON=${PYTHON3_DIR}/bin/python 83if [[ ! -f "${PYTHON3}" ]]; then 84 echo -e "\033[31m[OHOS ERROR] Please execute the build/prebuilts_download.sh \033[0m" 85 exit 1 86else 87 if [[ ! -f "${PYTHON}" ]]; then 88 ln -sf "${PYTHON3}" "${PYTHON}" 89 fi 90fi 91 92export PATH=${SOURCE_ROOT_DIR}/prebuilts/build-tools/${HOST_DIR}/bin:${PYTHON3_DIR}/bin:$PATH 93 94# set nodejs and ohpm 95EXPECTED_NODE_VERSION="14.21.1" 96export PATH=${SOURCE_ROOT_DIR}/prebuilts/build-tools/common/nodejs/node-v${EXPECTED_NODE_VERSION}-${NODE_PLATFORM}/bin:$PATH 97export NODE_HOME=${SOURCE_ROOT_DIR}/prebuilts/build-tools/common/nodejs/node-v${EXPECTED_NODE_VERSION}-${NODE_PLATFORM} 98export PATH=${SOURCE_ROOT_DIR}/prebuilts/build-tools/common/oh-command-line-tools/ohpm/bin:$PATH 99echo "[OHOS INFO] Current Node.js version is $(node -v)" 100NODE_VERSION=$(node -v) 101if [ "$NODE_VERSION" != "v$EXPECTED_NODE_VERSION" ]; then 102 echo -e "\033[31m[OHOS ERROR] Node.js version mismatch. Expected $EXPECTED_NODE_VERSION but found $NODE_VERSION\033[0m" >&2 103 exit 1 104fi 105echo -e "\033[32m[OHOS INFO] Node.js version check passed!\033[0m" 106npm config set registry https://repo.huaweicloud.com/repository/npm/ 107npm config set @ohos:registry https://repo.harmonyos.com/npm/ 108npm config set strict-ssl false 109npm config set lockfile false 110cat $HOME/.npmrc | grep 'lockfile=false' > /dev/null || echo 'lockfile=false' >> $HOME/.npmrc > /dev/null 111 112function init_ohpm() { 113 TOOLS_INSTALL_DIR="${SOURCE_ROOT_DIR}/prebuilts/build-tools/common" 114 pushd ${TOOLS_INSTALL_DIR} > /dev/null 115 if [[ ! -f "${TOOLS_INSTALL_DIR}/oh-command-line-tools/ohpm/bin/ohpm" ]]; then 116 echo "[OHOS INFO] download oh-command-line-tools" 117 wget https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_package_901_9/68/v3/r-5H8I7LT9mBjSFpSOY0Sg/ohcommandline-tools-linux-2.1.0.6.zip\?HW-CC-KV\=V1\&HW-CC-Date\=20231027T004601Z\&HW-CC-Expire\=315360000\&HW-CC-Sign\=A4D5E1A29C1C6962CA65592C3EB03ED363CE664CBE6C5974094064B67C34325E -O ohcommandline-tools-linux.zip 118 unzip ohcommandline-tools-linux.zip 119 fi 120 OHPM_HOME=${TOOLS_INSTALL_DIR}/oh-command-line-tools/ohpm 121 chmod +x ${OHPM_HOME}/bin/init 122 ${OHPM_HOME}/bin/init > /dev/null 123 echo "[OHOS INFO] Current ohpm version is $(ohpm -v)" 124 ohpm config set registry https://repo.harmonyos.com/ohpm/ 125 ohpm config set strict_ssl false 126 ohpm config set log_level debug 127 popd > /dev/null 128 if [[ -d "$HOME/.hvigor" ]]; then 129 rm -rf $HOME/.hvigor/daemon $HOME/.hvigor/wrapper 130 fi 131 mkdir -p $HOME/.hvigor/wrapper/tools 132 echo '{"dependencies": {"pnpm": "7.30.0"}}' > $HOME/.hvigor/wrapper/tools/package.json 133 pushd $HOME/.hvigor/wrapper/tools > /dev/null 134 echo "[OHOS INFO] installing pnpm..." 135 npm install --silent > /dev/null 136 popd > /dev/null 137 mkdir -p $HOME/.ohpm 138 echo '{"devDependencies":{"@ohos/hypium":"1.0.6"}}' > $HOME/.ohpm/oh-package.json5 139 pushd $HOME/.ohpm > /dev/null 140 echo "[OHOS INFO] installing hypium..." 141 ohpm install > /dev/null 142 popd > /dev/null 143} 144 145if [[ "$*" != *ohos-sdk* ]]; then 146 echo "[OHOS INFO] Ohpm initialization started..." 147 init_ohpm 148 if [[ "$?" -ne 0 ]]; then 149 echo -e "\033[31m[OHOS ERROR] ohpm initialization failed!\033[0m" 150 exit 1 151 fi 152 echo -e "\033[32m[OHOS INFO] ohpm initialization successful!\033[0m" 153fi 154echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n" 155 156echo -e "\033[32m[OHOS INFO] Start building...\033[0m\n" 157function build_sdk() { 158 ROOT_PATH=${SOURCE_ROOT_DIR} 159 SDK_PREBUILTS_PATH=${ROOT_PATH}/prebuilts/ohos-sdk 160 161 pushd ${ROOT_PATH} > /dev/null 162 echo -e "[OHOS INFO] building the latest ohos-sdk..." 163 ./build.py --product-name ohos-sdk --load-test-config=false --get-warning-list=false --stat-ccache=false --compute-overlap-rate=false --deps-guard=false --generate-ninja-trace=false --gn-args skip_generate_module_list_file=true sdk_platform=linux ndk_platform=linux use_cfi=false use_thin_lto=false enable_lto_O0=true sdk_check_flag=false enable_ndk_doxygen=false archive_ndk=false sdk_for_hap_build=true 164 if [[ "$?" -ne 0 ]]; then 165 echo -e "\033[31m[OHOS ERROR] ohos-sdk build failed! You can try to use '--no-prebuilt-sdk' to skip the build of ohos-sdk.\033[0m" 166 exit 1 167 fi 168 if [ -d "${ROOT_PATH}/prebuilts/ohos-sdk/linux" ]; then 169 rm -rf ${ROOT_PATH}/prebuilts/ohos-sdk/linux 170 fi 171 mkdir -p ${SDK_PREBUILTS_PATH} 172 mv ${ROOT_PATH}/out/sdk/ohos-sdk/linux ${SDK_PREBUILTS_PATH}/ 173 mkdir -p ${SDK_PREBUILTS_PATH}/linux/native 174 mv ${ROOT_PATH}/out/sdk/sdk-native/os-irrelevant/* ${SDK_PREBUILTS_PATH}/linux/native/ 175 mv ${ROOT_PATH}/out/sdk/sdk-native/os-specific/linux/* ${SDK_PREBUILTS_PATH}/linux/native/ 176 pushd ${SDK_PREBUILTS_PATH}/linux > /dev/null 177 api_version=$(grep apiVersion toolchains/oh-uni-package.json | awk '{print $2}' | sed -r 's/\",?//g') || api_version="11" 178 mkdir -p $api_version 179 for i in */; do 180 if [ -d "$i" ] && [ "$i" != "$api_version/" ]; then 181 mv $i $api_version 182 fi 183 done 184 popd > /dev/null 185 popd > /dev/null 186} 187if [[ ! -d "${SOURCE_ROOT_DIR}/prebuilts/ohos-sdk/linux" && "$*" != *ohos-sdk* && "$*" != *"--no-prebuilt-sdk"* || "${@}" =~ "--prebuilt-sdk" ]]; then 188 echo -e "\033[33m[OHOS INFO] The OHOS-SDK was not detected, so the SDK compilation will be prioritized automatically. You can also control whether to execute this process by using '--no-prebuilt-sdk' and '--prebuilt-sdk'.\033[0m" 189 build_sdk 190 if [[ "$?" -ne 0 ]]; then 191 echo -e "\033[31m[OHOS ERROR] ohos-sdk build failed, please remove the out/sdk directory and try again!\033[0m" 192 exit 1 193 fi 194fi 195 196${PYTHON3} ${SOURCE_ROOT_DIR}/build/scripts/tools_checker.py 197 198flag=true 199args_list=$@ 200for var in $@ 201do 202 OPTIONS=${var%%=*} 203 PARAM=${var#*=} 204 if [[ "$OPTIONS" == "using_hb_new" && "$PARAM" == "false" ]]; then 205 flag=false 206 ${PYTHON3} ${SOURCE_ROOT_DIR}/build/scripts/entry.py --source-root-dir ${SOURCE_ROOT_DIR} $args_list 207 break 208 fi 209done 210if [[ ${flag} == "true" ]]; then 211 ${PYTHON3} ${SOURCE_ROOT_DIR}/build/hb/main.py build $args_list 212fi 213 214if [[ "$?" -ne 0 ]]; then 215 echo -e "\033[31m=====build ${product_name} error=====\033[0m" 216 exit 1 217fi 218echo -e "\033[32m=====build ${product_name} successful=====\033[0m" 219 220date +%F' '%H:%M:%S 221echo "++++++++++++++++++++++++++++++++++++++++" 222