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 120echo "[OHOS INFO] Current Node.js version is $(node -v)" 121NODE_VERSION=$(node -v) 122if [ "$NODE_VERSION" != "v$EXPECTED_NODE_VERSION" ]; then 123 echo -e "\033[31m[OHOS ERROR] Node.js version mismatch. Expected $EXPECTED_NODE_VERSION but found $NODE_VERSION\033[0m" >&2 124 exit 1 125fi 126echo -e "\033[32m[OHOS INFO] Node.js version check passed!\033[0m" 127npm config set registry https://repo.huaweicloud.com/repository/npm/ 128npm config set @ohos:registry https://repo.harmonyos.com/npm/ 129npm config set strict-ssl false 130npm config set lockfile false 131cat $HOME/.npmrc | grep 'lockfile=false' > /dev/null || echo 'lockfile=false' >> $HOME/.npmrc > /dev/null 132 133function init_ohpm() { 134 TOOLS_INSTALL_DIR="${SOURCE_ROOT_DIR}/prebuilts/build-tools/common" 135 pushd ${TOOLS_INSTALL_DIR} > /dev/null 136 if [[ ! -f "${TOOLS_INSTALL_DIR}/oh-command-line-tools/ohpm/bin/ohpm" ]]; then 137 echo "[OHOS INFO] download oh-command-line-tools" 138 wget https://repo.huaweicloud.com/harmonyos/ohpm/5.0.2/oh-command-line-tools-20240715.zip -O ohcommandline-tools-linux.zip 139 unzip ohcommandline-tools-linux.zip 140 fi 141 OHPM_HOME=${TOOLS_INSTALL_DIR}/oh-command-line-tools/ohpm/bin 142 chmod +x ${OHPM_HOME}/ohpm 143 export PATH=${OHPM_HOME}:$PATH 144 chmod +x ${OHPM_HOME}/init 145 ${OHPM_HOME}/init > /dev/null 146 echo "[OHOS INFO] Current ohpm version is $(ohpm -v)" 147 ohpm config set registry https://repo.harmonyos.com/ohpm/ 148 ohpm config set strict_ssl false 149 ohpm config set log_level debug 150 popd > /dev/null 151 if [[ -d "$HOME/.hvigor" ]]; then 152 rm -rf $HOME/.hvigor/daemon $HOME/.hvigor/wrapper 153 fi 154 mkdir -p $HOME/.hvigor/wrapper/tools 155 echo '{"dependencies": {"pnpm": "7.30.0"}}' > $HOME/.hvigor/wrapper/tools/package.json 156 pushd $HOME/.hvigor/wrapper/tools > /dev/null 157 echo "[OHOS INFO] installing pnpm..." 158 npm install --silent > /dev/null 159 popd > /dev/null 160 mkdir -p $HOME/.ohpm 161 echo '{"devDependencies":{"@ohos/hypium":"1.0.6"}}' > $HOME/.ohpm/oh-package.json5 162 pushd $HOME/.ohpm > /dev/null 163 echo "[OHOS INFO] installing hypium..." 164 ohpm install > /dev/null 165 popd > /dev/null 166} 167 168if [[ "$*" != *ohos-sdk* ]]; then 169 echo "[OHOS INFO] Ohpm initialization started..." 170 init_ohpm 171 if [[ "$?" -ne 0 ]]; then 172 echo -e "\033[31m[OHOS ERROR] ohpm initialization failed!\033[0m" 173 exit 1 174 fi 175 echo -e "\033[32m[OHOS INFO] ohpm initialization successful!\033[0m" 176fi 177echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n" 178 179echo -e "\033[32m[OHOS INFO] Start building...\033[0m\n" 180function build_sdk() { 181 ROOT_PATH=${SOURCE_ROOT_DIR} 182 SDK_PREBUILTS_PATH=${ROOT_PATH}/prebuilts/ohos-sdk 183 184 pushd ${ROOT_PATH} > /dev/null 185 echo -e "[OHOS INFO] building the latest ohos-sdk..." 186 ./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 187 if [[ "$?" -ne 0 ]]; then 188 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" 189 exit 1 190 fi 191 if [ -d "${ROOT_PATH}/prebuilts/ohos-sdk/linux" ]; then 192 rm -rf ${ROOT_PATH}/prebuilts/ohos-sdk/linux 193 fi 194 mkdir -p ${SDK_PREBUILTS_PATH} 195 mv ${ROOT_PATH}/out/sdk/ohos-sdk/linux ${SDK_PREBUILTS_PATH}/ 196 mkdir -p ${SDK_PREBUILTS_PATH}/linux/native 197 mv ${ROOT_PATH}/out/sdk/sdk-native/os-irrelevant/* ${SDK_PREBUILTS_PATH}/linux/native/ 198 mv ${ROOT_PATH}/out/sdk/sdk-native/os-specific/linux/* ${SDK_PREBUILTS_PATH}/linux/native/ 199 pushd ${SDK_PREBUILTS_PATH}/linux > /dev/null 200 api_version=$(grep apiVersion toolchains/oh-uni-package.json | awk '{print $2}' | sed -r 's/\",?//g') || api_version="11" 201 mkdir -p $api_version 202 for i in */; do 203 if [ -d "$i" ] && [ "$i" != "$api_version/" ]; then 204 mv $i $api_version 205 fi 206 done 207 popd > /dev/null 208 popd > /dev/null 209} 210function get_api(){ 211 current_dir=$(pwd) 212 if [ -d "$current_dir/prebuilts/ohos-sdk-12/ohos-sdk/linux/12" ]; then 213 mkdir -p $current_dir/prebuilts/ohos-sdk/linux/12 214 mv "$current_dir/prebuilts/ohos-sdk-12/ohos-sdk/linux/12/"* "$current_dir/prebuilts/ohos-sdk/linux/12/" 215 fi 216} 217if [[ ! -d "${SOURCE_ROOT_DIR}/prebuilts/ohos-sdk/linux" && "$*" != *ohos-sdk* && "$*" != *"--no-prebuilt-sdk"* || "${@}" =~ "--prebuilt-sdk" ]]; then 218 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" 219 if [[ "${@}" =~ "--ccache=false" || "${@}" =~ "--ccache false" ]]; then 220 ccache_args="--ccache=false" 221 else 222 ccache_args="--ccache=true" 223 fi 224 if [[ "${@}" =~ "--xcache=true" || "${@}" =~ "--xcache true" || "${@}" =~ "--xcache" ]]; then 225 xcache_args="--xcache=true" 226 else 227 xcache_args="--xcache=false" 228 fi 229 build_sdk 230 get_api 231 if [[ "$?" -ne 0 ]]; then 232 echo -e "\033[31m[OHOS ERROR] ohos-sdk build failed, please remove the out/sdk directory and try again!\033[0m" 233 exit 1 234 fi 235fi 236 237${PYTHON3} ${SOURCE_ROOT_DIR}/build/scripts/tools_checker.py 238 239flag=true 240args_list=$@ 241for var in $@ 242do 243 OPTIONS=${var%%=*} 244 PARAM=${var#*=} 245 if [[ "$OPTIONS" == "using_hb_new" && "$PARAM" == "false" ]]; then 246 flag=false 247 ${PYTHON3} ${SOURCE_ROOT_DIR}/build/scripts/entry.py --source-root-dir ${SOURCE_ROOT_DIR} $args_list 248 break 249 fi 250done 251if [[ ${flag} == "true" ]]; then 252 ${PYTHON3} ${SOURCE_ROOT_DIR}/build/hb/main.py build $args_list 253fi 254 255if [[ "$?" -ne 0 ]]; then 256 echo -e "\033[31m=====build ${product_name} error=====\033[0m" 257 exit 1 258fi 259echo -e "\033[32m=====build ${product_name} successful=====\033[0m" 260 261date +%F' '%H:%M:%S 262echo "++++++++++++++++++++++++++++++++++++++++" 263