#!/bin/bash #跳转到当前脚本所在路径 cd "$(dirname "$(readlink -f "$0")")" ECHO() { local current_lineno=${BASH_LINENO[0]} # 获取调用ECHO的行号 local parent_lineno=$((current_lineno-1)) # 通常我们想要的是调用ECHO的行号,所以需要减1 # echo -e "\e[36;47mLine $parent_lineno:\e[0m $@" # 输出行号和传入的参数 # 使用printf格式化行号,确保总是6位数字,前面用空格填充 local formatted_lineno=$(printf "%6d" "$parent_lineno") # echo "Line $formatted_lineno: $@" # 输出格式化的行号和传入的参数 echo -e "\e[36m[$formatted_lineno]: \e[0m$@" # 输出行号和传入的参数 } function help { echo -e "help: \n 脚本使用说明:" echo -e "\t ./build.sh # 默认编译(不含spdlog,sqlite3及external_/)" echo -e "\t ./build.sh all # 全部编译(all:含spdlog,sqlite3; ext: external_/含拷贝)" echo -e "\t ./build.sh ext # 编译external_(并拷贝external_/的动态库和程序)" echo -e "\t ./build.sh extlib # 只拷贝external_/的动态库和程序" echo -e "\t ./build.sh hmi # 只编译 ems_hmi程序,假设库文件已更新" echo -e "\t ./build.sh -r # 编译全部,并release全部升级包" echo -e "\t ./build.sh -r usb # 编译全部,并release U盘升级包" echo -e "\t ./build.sh -r ota # 编译全部,并release 网络升级包(云平台)" echo -e "\t ./build.sh -ro # 直接release升级包,不编译" echo -e "\t ./script/release_usb.sh # 直接release usb升级包,不编译" echo -e "\t ./logical/build.sh # 编译logical" echo -e "\t ./lib_src/a_src/build.sh # 编译静态库" echo -e "\t ./tools_code/prdebug/build.sh # 编译调试工具程序" } # help if [[ "$1" == *-h* ]]; then ##if [ "$#" = 1 ] && [ "$1" = "-h" ]; then help exit 0 # 退出脚本 fi # 获取脚本开始执行的时间 start_time=$(date +%s) # 计算并打印脚本执行时间函数 function print_execution_time { # 脚本结束执行的时间戳 end_time=$(date +%s) # 计算执行时间差(秒) execution_time=$((end_time - start_time)) # 将执行时间差转换为 HH:MM:SS 格式 execution_time_formatted=$(date -u -d @$execution_time +"%T") # 判断正常还是异常退出 if [ $1 -eq 0 ];then SET_COLOR="\e[32m " else SET_COLOR="\e[31m error at $(pwd) \n\e[1m" fi # 打印执行时间 mydir_name="$(d=$(pwd); echo ${d##*/})" echo -e "${SET_COLOR} [$1] 执行时间:$execution_time_formatted ( $mydir_name )\e[0m " } # 捕获退出信号并调用函数 trap 'echo "$(print_execution_time $?)"' EXIT # 获取脚本的名称 script_name=$(basename "$0") # 检查是否被自身调用 #if [ "$0" = "$script_name" ]; then # echo "脚本被自身调用,请检查无效路径" # exit 1 # 退出脚本 #fi # 添加可执行权限 find . -name "*.sh"|xargs chmod +x current_project_path=$(pwd) export current_project_name=`echo ${current_project_path##*/}` echo "current_project_path = ${current_project_path}" echo "====================== build shell start [ ${current_project_name} ] =========================" pd_complier_type=${TARGET_PRODUCT} if [ -z ${TARGET_PRODUCT} ];then PROJECT_ROOT=$(while [ ! -f "build/envsetup.sh" ]; do [ "$(pwd)" = "/" ] && exit 1; cd ..; done; pwd) if [ ! -z ${PROJECT_ROOT} ];then source ${PROJECT_ROOT}/build/envsetup.sh && lunch LAST if [ $? -eq 255 ];then exit 0; fi pd_complier_type=${TARGET_PRODUCT} fi fi echo -e " ready to build\e[34;1m ${pd_complier_type} \e[0m target [\e[34;1m ${current_project_name}\e[0m ] ......" unset LD_LIBRARY_PATH export pd_complier_name="$(if [[ "${TARGET_PRODUCT}" =~ "imx"* ]]; then echo "complier_arm"; else echo "complier_x86"; fi)" export arm_sysroot_path=${SYSROOT} export arm_compile_flags=${CFLAGS} # set obj at: platform/out/build/xxxx/build check_dirs() { # check dir: process/ script/ local dir="$1"; if [[ -d "$dir/process" && -d "$dir/script" ]]; then echo "$dir"; exit 0;fi local parent="${dir%/*}"; if [[ "$parent" != "$dir" ]]; then check_dirs "$parent"; else echo "$(pwd)"; fi } base_folder="$(echo ${PROJECT_ROOT}|tr -s '/')" build_path=$(echo "$current_project_path" | sed "s|${base_folder}|${base_folder}/out/build/${pd_complier_type}|g") echo "build_path = ${build_path}" if [[ $1 == "clean" ]] || [[ $1 == "-c" ]] || [[ $1 == "all" ]];then if [[ "${build_path}" =~ "/out/build/" ]];then rm -rvf ${build_path} else echo "Warn: ${build_path} is set as Code dir, try to rm -f *.o" if [ -d ${build_path}];then find "${build_path}" -name '*.o' -print0 | xargs -r0 rm -f find ${build_path} -name 'cmake_install.cmake' -print0 | xargs -r0 rm -f find ${build_path} -name 'CMakeCache.txt' -print0 | xargs -r0 rm -f find ${build_path} -type d -name 'CMakeFiles' -print0 | xargs -r0 rm -rf fi fi echo " clean done." echo "====================== build shell end [ ${current_project_name} ] ==========================" # if [[ $1 != "all" ]];then exit 0; fi fi ## 强烈建议cd切换目录时用&&连接条件,如果切换失败则不会调用后面的cd -,避免cd不成功但cd ..成功后导致当前脚本被迭代运行 if [ "$1" == "-r" ] || [ "$1" == "-ro" ];then # "build.sh -r 带-r参数时增加打包" echo "jump to release pakage.." cd ../script && \ echo -e "\nrelease USB upgrade package:" && \ sh release.sh ${@:2} || exit $? && \ cd - else # auto find build lib_src_dir_list=$(find */ -maxdepth 1 -type f -name build.sh -exec dirname {} \;) statistical_lists="" ignore_lists="" for dir in ${lib_src_dir_list[@]}; do if [[ "$dir" == "mqtt" ]]; then echo "ignore directory $dir" ignore_lists+=("\n\t\t\t\t${dir}") continue fi if [[ "$dir" =~ "toolsXXX" ]]; then echo -e "\e[35mignore dir: \e[34m$dir\e[0m" ignore_lists+=("\n\t\t\t\t${dir}") sh $dir/build.sh $@ #cd "$dir" || { echo "Failed to change to directory $dir"; continue; } && \ #echo -e "\e[33;42;1mcd `pwd`/ \e[0m" && \ #sh build.sh $@ || exit $? || { echo "Failed to execute build.sh in directory $dir"; cd - > /dev/null; continue; } && \ #cd - else if [ -f "$dir"/build.sh ];then echo -e "\e[32m enter dir: \e[34m$dir\e[0m" cd "$dir" || { echo "Failed to change to directory $dir"; continue; } && \ sh build.sh $@ || exit $? || { echo "Failed to execute build.sh in directory $dir"; cd - > /dev/null; continue; } && \ cd - statistical_lists+=("\n\t\t\t\t$dir") fi fi done echo -e "\e[34m build Statistical Lists [$((${#statistical_lists[@]}-1))]: ${statistical_lists[@]} \e[0m" echo -e "\e[37m Ignore Lists[$((${#ignore_lists[@]}-1))]: ${ignore_lists[@]} \e[0m" fi