#!/bin/bashset-eset +e
echo-e"\n\033[32m\t*********Welcome to OpenHarmony!*********\033[0m\n"echo-e"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"functioncheck_shell_environment(){case$(uname-s)in
Linux)shell_result=$(/bin/sh -c'echo ${BASH_VERSION}')if[-n"${shell_result}"];thenecho-e"\033[32mSystem shell: bash ${shell_result}\033[0m"elseecho-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"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"fi;;
Darwin)echo-e"\033[31m[OHOS ERROR] Darwin system is not supported yet\033[0m";;
*)echo-e"\033[31m[OHOS ERROR] Unsupported this system: $(uname-s)\033[0m"exit1esac}
check_shell_environment
echo-e"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"echo-e"\033[32mCurrent time: $(date +%F' '%H:%M:%S)\033[0m"echo-e"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"echo-e"\033[32mBuild args: $@\033[0m"echo-e"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"exportSOURCE_ROOT_DIR=$(cd$(dirname $0);pwd)while[[!-f"${SOURCE_ROOT_DIR}/.gn"]];doSOURCE_ROOT_DIR="$(dirname"${SOURCE_ROOT_DIR}")"if[["${SOURCE_ROOT_DIR}"=="/"]];thenecho-e"\033[31m[OHOS ERROR] Cannot find source tree containing $(pwd)\033[0m"exit1fidoneif[["${SOURCE_ROOT_DIR}x"=="x"]];thenecho-e"\033[31m[OHOS ERROR] SOURCE_ROOT_DIR cannot be empty.\033[0m"exit1ficase$(uname-s)in
Darwin)HOST_DIR="darwin-x86"HOST_OS="mac"NODE_PLATFORM="darwin-x64";;
Linux)HOST_DIR="linux-x86"HOST_OS="linux"NODE_PLATFORM="linux-x64";;
*)echo"\033[31m[OHOS ERROR] Unsupported host platform: $(uname-s)\033[0m"RET=1exit$RETesac# set python3PYTHON3_DIR=${SOURCE_ROOT_DIR}/prebuilts/python/${HOST_DIR}/current/
PYTHON3=${PYTHON3_DIR}/bin/python3
PYTHON=${PYTHON3_DIR}/bin/python
if[[!-f"${PYTHON3}"]];thenecho-e"\033[31m[OHOS ERROR] Please execute the build/prebuilts_download.sh \033[0m"exit1elseif[[!-f"${PYTHON}"]];thenln-sf"${PYTHON3}""${PYTHON}"fifiexportPATH=${SOURCE_ROOT_DIR}/prebuilts/build-tools/${HOST_DIR}/bin:${PYTHON3_DIR}/bin:$PATH# set nodejs and ohpmEXPECTED_NODE_VERSION="14.21.1"exportPATH=${SOURCE_ROOT_DIR}/prebuilts/build-tools/common/nodejs/node-v${EXPECTED_NODE_VERSION}-${NODE_PLATFORM}/bin:$PATHexportNODE_HOME=${SOURCE_ROOT_DIR}/prebuilts/build-tools/common/nodejs/node-v${EXPECTED_NODE_VERSION}-${NODE_PLATFORM}exportPATH=${SOURCE_ROOT_DIR}/prebuilts/build-tools/common/oh-command-line-tools/ohpm/bin:$PATHecho"[OHOS INFO] Current Node.js version is $(node-v)"NODE_VERSION=$(node-v)if["$NODE_VERSION"!="v$EXPECTED_NODE_VERSION"];thenecho-e"\033[31m[OHOS ERROR] Node.js version mismatch. Expected $EXPECTED_NODE_VERSION but found $NODE_VERSION\033[0m">&2exit1fiecho-e"\033[32m[OHOS INFO] Node.js version check passed!\033[0m"npm config set registry https://repo.huaweicloud.com/repository/npm/
npm config set @ohos:registry https://repo.harmonyos.com/npm/
npm config set strict-ssl falsenpm config set lockfile falsecat$HOME/.npmrc |grep'lockfile=false'> /dev/null ||echo'lockfile=false'>>$HOME/.npmrc > /dev/null
functioninit_ohpm(){TOOLS_INSTALL_DIR="${SOURCE_ROOT_DIR}/prebuilts/build-tools/common"pushd${TOOLS_INSTALL_DIR}> /dev/null
if[[!-f"${TOOLS_INSTALL_DIR}/oh-command-line-tools/ohpm/bin/ohpm"]];thenecho"[OHOS INFO] download oh-command-line-tools"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
unzip ohcommandline-tools-linux.zip
fiOHPM_HOME=${TOOLS_INSTALL_DIR}/oh-command-line-tools/ohpm
chmod +x ${OHPM_HOME}/bin/init
${OHPM_HOME}/bin/init > /dev/null
echo"[OHOS INFO] Current ohpm version is $(ohpm -v)"
ohpm config set registry https://repo.harmonyos.com/ohpm/
ohpm config set strict_ssl false
ohpm config set log_level debug
popd> /dev/null
if[[-d"$HOME/.hvigor"]];thenrm-rf$HOME/.hvigor/daemon $HOME/.hvigor/wrapper
fimkdir-p$HOME/.hvigor/wrapper/tools
echo'{"dependencies": {"pnpm": "7.30.0"}}'>$HOME/.hvigor/wrapper/tools/package.json
pushd$HOME/.hvigor/wrapper/tools > /dev/null
echo"[OHOS INFO] installing pnpm..."npminstall--silent> /dev/null
popd> /dev/null
mkdir-p$HOME/.ohpm
echo'{"devDependencies":{"@ohos/hypium":"1.0.6"}}'>$HOME/.ohpm/oh-package.json5
pushd$HOME/.ohpm > /dev/null
echo"[OHOS INFO] installing hypium..."
ohpm install> /dev/null
popd> /dev/null
}if[["$*"!= *ohos-sdk* ]];thenecho"[OHOS INFO] Ohpm initialization started..."
init_ohpm
if[["$?"-ne0]];thenecho-e"\033[31m[OHOS ERROR] ohpm initialization failed!\033[0m"exit1fiecho-e"\033[32m[OHOS INFO] ohpm initialization successful!\033[0m"fiecho-e"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"echo-e"\033[32m[OHOS INFO] Start building...\033[0m\n"functionbuild_sdk(){ROOT_PATH=${SOURCE_ROOT_DIR}SDK_PREBUILTS_PATH=${ROOT_PATH}/prebuilts/ohos-sdk
pushd${ROOT_PATH}> /dev/null
echo-e"[OHOS INFO] building the latest ohos-sdk..."
./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
if[["$?"-ne0]];thenecho-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"exit1fiif[-d"${ROOT_PATH}/prebuilts/ohos-sdk/linux"];thenrm-rf${ROOT_PATH}/prebuilts/ohos-sdk/linux
fimkdir-p${SDK_PREBUILTS_PATH}mv${ROOT_PATH}/out/sdk/ohos-sdk/linux ${SDK_PREBUILTS_PATH}/
mkdir-p${SDK_PREBUILTS_PATH}/linux/native
mv${ROOT_PATH}/out/sdk/sdk-native/os-irrelevant/* ${SDK_PREBUILTS_PATH}/linux/native/
mv${ROOT_PATH}/out/sdk/sdk-native/os-specific/linux/* ${SDK_PREBUILTS_PATH}/linux/native/
pushd${SDK_PREBUILTS_PATH}/linux > /dev/null
api_version=$(grep apiVersion toolchains/oh-uni-package.json |awk'{print $2}'|sed-r's/\",?//g')||api_version="11"mkdir-p$api_versionforiin */;doif[-d"$i"]&&["$i"!="$api_version/"];thenmv$i$api_versionfidonepopd> /dev/null
popd> /dev/null
}if[[!-d"${SOURCE_ROOT_DIR}/prebuilts/ohos-sdk/linux"&&"$*"!= *ohos-sdk* &&"$*"!= *"--no-prebuilt-sdk"* ||"${@}"=~"--prebuilt-sdk"]];thenecho-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"if[["${@}"=~"--ccache=false"||"${@}"=~"--ccache false"]];thenccache_args="--ccache=false"elseccache_args="--ccache=true"fiif[["${@}"=~"--xcache=true"||"${@}"=~"--xcache true"||"${@}"=~"--xcache"]];thenxcache_args="--xcache=true"elsexcache_args="--xcache=false"fi
build_sdk
if[["$?"-ne0]];thenecho-e"\033[31m[OHOS ERROR] ohos-sdk build failed, please remove the out/sdk directory and try again!\033[0m"exit1fifi${PYTHON3}${SOURCE_ROOT_DIR}/build/scripts/tools_checker.py
flag=true
args_list=$@forvarin$@doOPTIONS=${var%%=*}PARAM=${var#*=}if[["$OPTIONS"=="using_hb_new"&&"$PARAM"=="false"]];thenflag=false
${PYTHON3}${SOURCE_ROOT_DIR}/build/scripts/entry.py --source-root-dir ${SOURCE_ROOT_DIR}$args_listbreakfidoneif[[${flag}=="true"]];then${PYTHON3}${SOURCE_ROOT_DIR}/build/hb/main.py build $args_listfiif[["$?"-ne0]];thenecho-e"\033[31m=====build ${product_name} error=====\033[0m"exit1fiecho-e"\033[32m=====build ${product_name} successful=====\033[0m"date +%F' '%H:%M:%S
echo"++++++++++++++++++++++++++++++++++++++++"
print("root_out_dir=$root_out_dir")print("root_build_dir=$root_build_dir")print("root_gen_dir=$root_gen_dir")print("current_toolchain=$current_toolchain")print("host_toolchain=$host_toolchain")import("//build/ohos_var.gni")#gntarget definedif(product_name =="ohos-sdk"){group("build_ohos_sdk"){
deps =["//build/ohos/ndk:ohos_ndk","//build/ohos/sdk:ohos_sdk","//build/ohos/sdk:ohos_sdk_verify",]}}elseif(product_name =="arkui-x"){group("arkui_targets"){
deps =["//build_plugins/sdk:arkui_cross_sdk"]}}else{group("make_all"){
deps =[":make_inner_kits",":packages",]if(is_standard_system &&!is_llvm_build){#Lite system uses different packaging scheme, which is called in hb.#So skip images for lite system since it's the mkimage#actionfor standard system.
deps +=[":images"]}}if(!is_llvm_build){group("images"){
deps =["//build/ohos/images:make_images"]}}group("packages"){
deps =["//build/ohos/packages:make_packages"]}group("make_inner_kits"){
deps =["$root_build_dir/build_configs:inner_kits"]}group("build_all_test_pkg"){
testonly = true
if(!is_llvm_build){
deps =["$root_build_dir/build_configs:parts_test","//test/testfwk/developer_test:make_temp_test",]}}group("make_test"){
testonly = true
deps =["//build/ohos/packages:build_all_test_pkg","//build/ohos/packages:package_testcase","//build/ohos/packages:package_testcase_mlf",]if(archive_component){
deps +=["//build/ohos/testfwk:archive_testcase"]}}}