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 62 63host_cpu_prefix="" 64 65case $(uname -m) in 66 *x86_64) 67 host_cpu_prefix="x86" 68 ;; 69 *arm*) 70 host_cpu_prefix="arm64" 71 ;; 72 *) 73 echo "\033[31m[OHOS ERROR] Unsupported host arch: $(uname -m)\033[0m" 74 RET=1 75 exit $RET 76esac 77 78case $(uname -s) in 79 Darwin) 80 HOST_DIR="darwin-x86" 81 PYTHON_DIR="darwin-$host_cpu_prefix" 82 HOST_OS="mac" 83 NODE_PLATFORM="darwin-x64" 84 ;; 85 Linux) 86 HOST_DIR="linux-x86" 87 PYTHON_DIR="linux-$host_cpu_prefix" 88 HOST_OS="linux" 89 NODE_PLATFORM="linux-x64" 90 ;; 91 *) 92 echo "\033[31m[OHOS ERROR] Unsupported host platform: $(uname -s)\033[0m" 93 RET=1 94 exit $RET 95esac 96 97# set python3 98PYTHON3_DIR=${SOURCE_ROOT_DIR}/prebuilts/python/${PYTHON_DIR}/current/ 99PYTHON3=${PYTHON3_DIR}/bin/python3 100PYTHON=${PYTHON3_DIR}/bin/python 101if [[ ! -f "${PYTHON3}" ]]; then 102 echo -e "\033[31m[OHOS ERROR] Please execute the build/prebuilts_download.sh \033[0m" 103 exit 1 104else 105 if [[ ! -f "${PYTHON}" ]]; then 106 ln -sf "${PYTHON3}" "${PYTHON}" 107 fi 108fi 109 110export PATH=${SOURCE_ROOT_DIR}/prebuilts/build-tools/${HOST_DIR}/bin:${PYTHON3_DIR}/bin:$PATH 111 112# set nodejs and ohpm 113EXPECTED_NODE_VERSION="14.21.1" 114export PATH=${SOURCE_ROOT_DIR}/prebuilts/build-tools/common/nodejs/node-v${EXPECTED_NODE_VERSION}-${NODE_PLATFORM}/bin:$PATH 115export NODE_HOME=${SOURCE_ROOT_DIR}/prebuilts/build-tools/common/nodejs/node-v${EXPECTED_NODE_VERSION}-${NODE_PLATFORM} 116export PATH=${SOURCE_ROOT_DIR}/prebuilts/build-tools/common/oh-command-line-tools/ohpm/bin:$PATH 117echo "[OHOS INFO] Current Node.js version is $(node -v)" 118NODE_VERSION=$(node -v) 119if [ "$NODE_VERSION" != "v$EXPECTED_NODE_VERSION" ]; then 120 echo -e "\033[31m[OHOS ERROR] Node.js version mismatch. Expected $EXPECTED_NODE_VERSION but found $NODE_VERSION\033[0m" >&2 121 exit 1 122fi 123echo -e "\033[32m[OHOS INFO] Node.js version check passed!\033[0m" 124npm config set registry https://repo.huaweicloud.com/repository/npm/ 125npm config set @ohos:registry https://repo.harmonyos.com/npm/ 126npm config set strict-ssl false 127npm config set lockfile false 128cat $HOME/.npmrc | grep 'lockfile=false' > /dev/null || echo 'lockfile=false' >> $HOME/.npmrc > /dev/null 129 130function init_ohpm() { 131 TOOLS_INSTALL_DIR="${SOURCE_ROOT_DIR}/prebuilts/build-tools/common" 132 pushd ${TOOLS_INSTALL_DIR} > /dev/null 133 if [[ ! -f "${TOOLS_INSTALL_DIR}/oh-command-line-tools/ohpm/bin/ohpm" ]]; then 134 echo "[OHOS INFO] download oh-command-line-tools" 135 wget https://repo.huaweicloud.com/harmonyos/ohpm/5.0.2/oh-command-line-tools-20240715.zip -O ohcommandline-tools-linux.zip 136 unzip ohcommandline-tools-linux.zip 137 fi 138 OHPM_HOME=${TOOLS_INSTALL_DIR}/oh-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" 177function build_sdk() { 178 ROOT_PATH=${SOURCE_ROOT_DIR} 179 SDK_PREBUILTS_PATH=${ROOT_PATH}/prebuilts/ohos-sdk 180 181 pushd ${ROOT_PATH} > /dev/null 182 echo -e "[OHOS INFO] building the latest ohos-sdk..." 183 ./build.py --product-name ohos-sdk $ccache_args $xcache_args --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 enable_archive_sdk=false enable_notice_collection=false enable_process_notice=false 184 if [[ "$?" -ne 0 ]]; then 185 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" 186 exit 1 187 fi 188 if [ -d "${ROOT_PATH}/prebuilts/ohos-sdk/linux" ]; then 189 rm -rf ${ROOT_PATH}/prebuilts/ohos-sdk/linux 190 fi 191 mkdir -p ${SDK_PREBUILTS_PATH} 192 mv ${ROOT_PATH}/out/sdk/ohos-sdk/linux ${SDK_PREBUILTS_PATH}/ 193 mkdir -p ${SDK_PREBUILTS_PATH}/linux/native 194 mv ${ROOT_PATH}/out/sdk/sdk-native/os-irrelevant/* ${SDK_PREBUILTS_PATH}/linux/native/ 195 mv ${ROOT_PATH}/out/sdk/sdk-native/os-specific/linux/* ${SDK_PREBUILTS_PATH}/linux/native/ 196 pushd ${SDK_PREBUILTS_PATH}/linux > /dev/null 197 api_version=$(grep apiVersion toolchains/oh-uni-package.json | awk '{print $2}' | sed -r 's/\",?//g') || api_version="11" 198 mkdir -p $api_version 199 for i in */; do 200 if [ -d "$i" ] && [ "$i" != "$api_version/" ]; then 201 mv $i $api_version 202 fi 203 done 204 popd > /dev/null 205 popd > /dev/null 206} 207if [[ ! -d "${SOURCE_ROOT_DIR}/prebuilts/ohos-sdk/linux" && "$*" != *ohos-sdk* && "$*" != *"--no-prebuilt-sdk"* || "${@}" =~ "--prebuilt-sdk" ]]; then 208 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" 209 if [[ "${@}" =~ "--ccache=false" || "${@}" =~ "--ccache false" ]]; then 210 ccache_args="--ccache=false" 211 else 212 ccache_args="--ccache=true" 213 fi 214 if [[ "${@}" =~ "--xcache=true" || "${@}" =~ "--xcache true" || "${@}" =~ "--xcache" ]]; then 215 xcache_args="--xcache=true" 216 else 217 xcache_args="--xcache=false" 218 fi 219 build_sdk 220 if [[ "$?" -ne 0 ]]; then 221 echo -e "\033[31m[OHOS ERROR] ohos-sdk build failed, please remove the out/sdk directory and try again!\033[0m" 222 exit 1 223 fi 224fi 225 226${PYTHON3} ${SOURCE_ROOT_DIR}/build/scripts/tools_checker.py 227 228flag=true 229args_list=$@ 230for var in $@ 231do 232 OPTIONS=${var%%=*} 233 PARAM=${var#*=} 234 if [[ "$OPTIONS" == "using_hb_new" && "$PARAM" == "false" ]]; then 235 flag=false 236 ${PYTHON3} ${SOURCE_ROOT_DIR}/build/scripts/entry.py --source-root-dir ${SOURCE_ROOT_DIR} $args_list 237 break 238 fi 239done 240if [[ ${flag} == "true" ]]; then 241 ${PYTHON3} ${SOURCE_ROOT_DIR}/build/hb/main.py build $args_list 242fi 243 244if [[ "$?" -ne 0 ]]; then 245 echo -e "\033[31m=====build ${product_name} error=====\033[0m" 246 exit 1 247fi 248echo -e "\033[32m=====build ${product_name} successful=====\033[0m" 249 250date +%F' '%H:%M:%S 251echo "++++++++++++++++++++++++++++++++++++++++" 252