From d693318a0267421aa460efc7b7f5524c552e64c6 Mon Sep 17 00:00:00 2001 From: wyc <1104749580@qq.com> Date: Thu, 13 Aug 2026 09:31:12 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E4=B8=80=E4=BA=9Bp0=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- linux_shell/3rd/elfutils | 50 +--- linux_shell/3rd/openssh.bash | 53 +--- linux_shell/3rd/patchelf | 20 +- linux_shell/3rd/patchelf.sh | 59 ++-- linux_shell/build_script/Qt/core.bash | 8 +- linux_shell/build_script/attach/core.bash | 46 ++- linux_shell/build_script/base/common.bash | 281 +++++++++--------- linux_shell/build_script/base/deploy.bash | 2 +- .../build_script/base/download_msys2.bash | 10 +- linux_shell/build_script/base/get_src.bash | 115 +++---- linux_shell/build_script/base/git.bash | 52 +--- .../build_script/base/global_rely/export.bash | 13 +- .../build_script/base/global_rely/log.bash | 142 ++++----- .../base/global_rely/stack_trace.bash | 121 +++----- linux_shell/build_script/base/init_env.bash | 84 ++---- linux_shell/build_script/base/other.bash | 101 ++----- linux_shell/build_script/base/windows.bash | 73 ----- linux_shell/build_script/cmake.bash | 2 +- .../cmake_toolchain/llvm-mingw.cmake | 5 +- .../#U8f85#U52a9#U811a#U672c/python_gdb.bash | 39 +++ linux_shell/build_script/gcc/bge.bash | 6 +- .../cmake_template/toolchain_template.cmake | 98 ++---- linux_shell/build_script/install_llvm.bash | 6 +- linux_shell/build_script/libc/core.bash | 12 +- linux_shell/build_script/lld.bash | 28 +- linux_shell/build_script/llvm/cmake.bash | 8 +- linux_shell/build_script/main.bash | 22 +- linux_shell/build_script/pragma_once.bash | 48 +-- .../3rd_build_syroot/ubuntu2004/build_sysroot | 2 +- .../build_sysroot_from_iso/base_rootfs.bash | 33 ++ .../function_rootfs.bash | 9 +- .../build_sysroot_from_iso/test_rootfs.bash | 9 +- 32 files changed, 608 insertions(+), 949 deletions(-) create mode 100644 linux_shell/build_script/gcc/#U8f85#U52a9#U811a#U672c/python_gdb.bash diff --git a/linux_shell/3rd/elfutils b/linux_shell/3rd/elfutils index c5348bd..a70efad 100644 --- a/linux_shell/3rd/elfutils +++ b/linux_shell/3rd/elfutils @@ -1,50 +1,30 @@ #!/bin/bash - -if ! type "include" > /dev/null 2>&1; then +if ! type include >/dev/null 2>&1; then include() { - . "$(cd "$(dirname "$1")" && pwd)/${2}"; - }; + . "$(cd "$(dirname "$1")" && pwd)/${2}" + } fi -include "${BASH_SOURCE[0]}" ./global.bash -if ! pragma_once ${BASH_SOURCE[0]}; then return 0; - source ./../gcc/global.bash +include "${BASH_SOURCE[0]}" ../build_script/pragma_once.bash +if ! pragma_once "${BASH_SOURCE[0]}"; then + return 0 fi - +include "${BASH_SOURCE[0]}" ../build_script/base/export.bash +init_build_environment load_compile_environment() { load_library "gcc-14.2.0:mpc-1.3.1:mpfr-4.2.1:gmp-6.2.1:cmake-3.26.5:make-4.4:patchelf-0.18.0" - export LD_LIBRARY_PATH=/home/env/Qt5.14.2/5.14.2/gcc_64/lib/:$LD_LIBRARY_PATH + export LD_LIBRARY_PATH="/home/env/Qt5.14.2/5.14.2/gcc_64/lib/${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" } load_compile_environment - -_install_="" -_src_="" - -if [[ "$(uname)" == "Linux" ]]; then - echo "This is Linux." - _install_="/ae/install" - _src_="/ae/src" -elif [[ "$(uname)" == "MINGW"* || "$(uname)" == "CYGWIN"* ]]; then - echo "This is Windows (Cygwin or MinGW)." - _install_="D:\wyc\cached_external_library\bash_install" - _src_="D:\wyc\cached_external_library\bash_srcs" -else - echo "Unknown system." -fi - - - packageName="elfutils" version="0.189" -_src="${_src_}/${packageName}-${version}" +_src="${_ae_root_}/src/${packageName}-${version}" _build="${_src_}/${packageName}-${version}-build" -_install="${_install_}/${packageName}-${version}-install" - -#wget -P "${src}" https://sourceware.org/elfutils/ftp/0.189/elfutils-0.189.tar.bz2 -tar -xvf "${_src_}/elfutils-0.189.tar.bz2" -C "${_src_}" +_install="${_ae_root_}/install/${packageName}-${version}-install" +tar -xvf "${_ae_root_}/src/elfutils-0.189.tar.bz2" -C "${_ae_root_}/src" chmod 777 "${_src}" -mkdir -m 777 "${_build}" -cd "${_build}" || exit -../"${_src}"/configure --prefix="${_install}" +mkdir -p -m 777 "${_build}" +cd "${_build}" || exit $? +"${_src}/configure" --prefix="${_install}" make -j"$(nproc)" sudo make install sudo chmod -R 777 "${_install}" diff --git a/linux_shell/3rd/openssh.bash b/linux_shell/3rd/openssh.bash index 4e8252f..b88f979 100644 --- a/linux_shell/3rd/openssh.bash +++ b/linux_shell/3rd/openssh.bash @@ -1,44 +1,23 @@ #!/bin/bash - -if ! type "include" > /dev/null 2>&1; then +if ! type include >/dev/null 2>&1; then include() { - . "$(cd "$(dirname "$1")" && pwd)/${2}"; - }; + . "$(cd "$(dirname "$1")" && pwd)/${2}" + } fi -include "${BASH_SOURCE[0]}" ../pragma_once.bash -if ! pragma_once ${BASH_SOURCE[0]}; then return 0; - source ../base/export.bash +include "${BASH_SOURCE[0]}" ../build_script/pragma_once.bash +if ! pragma_once "${BASH_SOURCE[0]}"; then + return 0 fi -include "${BASH_SOURCE[0]}" ../base/export.bash - - - +include "${BASH_SOURCE[0]}" ../build_script/base/export.bash build_python() { - return 0 } - - - - -build_open_ssh(){ - build_dir=/ae/env - zlib_ver=zlib-1.3.1 - zlib_dir=${build_dir}/${zlib_ver} - openssh_ver=openssh_10.0p1 - openssh_dir=${build_dir}/${openssh_ver} - - - build_gnu_zip "https://zlib.net/zlib-1.3.1.tar.gz" \ - --prefix=${zlib_dir} - - - # openssl zlib - # --with-privsep-path OpenSSH 的“特权分离(Privilege Separation)目录 - build_gnu_zip "https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-10.0p1.tar.gz" \ - --prefix=${openssh_dir} \ - --with-zlib=${zlib_dir} \ - --with-privsep-path=${openssh_dir}/var/empty - - return 0 -} \ No newline at end of file +build_open_ssh() { + local build_dir="${_ae_root_}/env" + local zlib_ver="zlib-1.3.1" + local zlib_dir="${build_dir}/${zlib_ver}" + local openssh_ver="openssh_10.0p1" + local openssh_dir="${build_dir}/${openssh_ver}" + build_gnu_zip "https://zlib.net/zlib-1.3.1.tar.gz" --prefix="${zlib_dir}" + build_gnu_zip "https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-10.0p1.tar.gz" --prefix="${openssh_dir}" --with-zlib="${zlib_dir}" --with-privsep-path="${openssh_dir}/var/empty" +} diff --git a/linux_shell/3rd/patchelf b/linux_shell/3rd/patchelf index 2bbd015..8fc93e3 100644 --- a/linux_shell/3rd/patchelf +++ b/linux_shell/3rd/patchelf @@ -1,23 +1,21 @@ #!/bin/bash - -if ! type "include" > /dev/null 2>&1; then +if ! type include >/dev/null 2>&1; then include() { - . "$(cd "$(dirname "$1")" && pwd)/${2}"; - }; + . "$(cd "$(dirname "$1")" && pwd)/${2}" + } fi -include "${BASH_SOURCE[0]}" ../base/export.bash -if ! pragma_once ${BASH_SOURCE[0]}; then return 0; - source ../base/export.bash +include "${BASH_SOURCE[0]}" ../build_script/pragma_once.bash +if ! pragma_once "${BASH_SOURCE[0]}"; then + return 0 fi - - +include "${BASH_SOURCE[0]}" ../build_script/base/export.bash +init_build_environment packageName="patchelf" version="0.15.0" _src="${_src_}/${packageName}-${version}" _install="${_install_}/${packageName}-${version}" - git clone --branch "${version}" https://github.com/NixOS/patchelf.git "${_src}" -cd "${_src}" || exit +cd "${_src}" || exit $? ./bootstrap.sh ./configure --prefix="${_install}" make -j"$(nproc)" diff --git a/linux_shell/3rd/patchelf.sh b/linux_shell/3rd/patchelf.sh index 6da954b..07544c0 100644 --- a/linux_shell/3rd/patchelf.sh +++ b/linux_shell/3rd/patchelf.sh @@ -1,41 +1,26 @@ #!/bin/bash -if ! type "include" > /dev/null 2>&1; then +if ! type include >/dev/null 2>&1; then include() { - . "$(cd "$(dirname "$1")" && pwd)/${2}"; - }; -fi - -if ! pragma_once ${BASH_SOURCE[0]}; then return 0; - source ../base/export.bash -fi - -include "${BASH_SOURCE[0]}" ../base/export.bash - - -if pragma_once "${BASH_SOURCE[0]}"; then - load_compile_environment() { - load_library "gcc-14.2.0:mpc-1.3.1:mpfr-4.2.1:gmp-6.2.1:cmake-3.26.5:make-4.4" + . "$(cd "$(dirname "$1")" && pwd)/${2}" } - load_compile_environment - git clone https://github.com/NixOS/patchelf.git "${_src_}/patchelf-0.18.0" - cd "$_src_/patchelf-0.18.0" || { - echo "$_src_/patchelf-0.18 不存在!" - exit 1 - } - git checkout 0.18.0 - ./bootstrap.sh - ./configure --prefix="${_install_}/patchelf-0.18.0" - make -j"$(nproc)" - make check - sudo make install - sudo chmod -R 777 "${_install_}/patchelf-0.18.0" - patchelf --version fi - -# /ae/win/wyc/projects/CLionProjects/plantSunCat/0_linux_pro - -#./bootstrap.sh -#./configure -#make -#make check -#sudo make install +include "${BASH_SOURCE[0]}" ../build_script/pragma_once.bash +if ! pragma_once "${BASH_SOURCE[0]}"; then + return 0 +fi +include "${BASH_SOURCE[0]}" ../build_script/base/export.bash +init_build_environment +load_compile_environment() { + load_library "gcc-14.2.0:mpc-1.3.1:mpfr-4.2.1:gmp-6.2.1:cmake-3.26.5:make-4.4" +} +load_compile_environment +git clone https://github.com/NixOS/patchelf.git "${_src_}/patchelf-0.18.0" +cd "${_src_}/patchelf-0.18.0" || exit $? +git checkout 0.18.0 +./bootstrap.sh +./configure --prefix="${_install_}/patchelf-0.18.0" +make -j"$(nproc)" +make check +sudo make install +sudo chmod -R 777 "${_install_}/patchelf-0.18.0" +patchelf --version diff --git a/linux_shell/build_script/Qt/core.bash b/linux_shell/build_script/Qt/core.bash index 934b634..cb56fa2 100644 --- a/linux_shell/build_script/Qt/core.bash +++ b/linux_shell/build_script/Qt/core.bash @@ -93,8 +93,8 @@ build_qt_host_tools() { ${bq_host_tool_suffix_opts} \ " configure_gnu_ex "${src_dir}/qtbase" "${opts}" - exec_record "make -j $(nproc)" - exec_record "make install -j $(nproc)" + exec_record_fast_fail "make -j $(nproc)" + exec_record_fast_fail "make install -j $(nproc)" if [ ! -d "${bq_host_tool_install_prefix}/bin" ]; then echo "host tool build failed: ${bq_host_tool_install_prefix}/bin not found" >&2 return 1 @@ -230,8 +230,8 @@ build_qt() { # export CC="${bq_host_triplet}-gcc" # export CXX="${bq_host_triplet}-g++" configure_gnu_ex "$src_dir" "${opts}" - exec_record "make -j $(nproc)" - exec_record "make install -j $(nproc)" + exec_record_fast_fail "make -j $(nproc)" + exec_record_fast_fail "make install -j $(nproc)" if [[ "$bq_cross" == "1" ]]; then patch_cross_qt "${bq_sysroot}" "${bq_install_prefix}/lib/cmake" fi diff --git a/linux_shell/build_script/attach/core.bash b/linux_shell/build_script/attach/core.bash index 8b2fe90..a818150 100644 --- a/linux_shell/build_script/attach/core.bash +++ b/linux_shell/build_script/attach/core.bash @@ -67,7 +67,7 @@ init_by_env_name() { gev_cur_env_name="${1}" gev_cur_toolchain_name="${gev_cur_env_name}" gev_target="${2}" - gev_target_env="/ae/env/${gev_cur_env_name}" + gev_target_env="${_ae_root_}/env/${gev_cur_env_name}" # 这个地方可以改 sysroot 叫什么名称 gev_target_sysroot="${gev_target_env}/sysroot_${gev_target}" gev_cross_dir="${gev_target_env}" @@ -81,12 +81,16 @@ init_by_env_name() { } -cacl_toolchain_name(){ +cacl_toolchain_name() { local -n ret="$1" local build="$2" local host="$3" local target="$4" local ver="$5" + if [[ -z "$build" || -z "$host" || -z "$target" || -z "$ver" ]]; then + log_error "cacl_toolchain_name requires build/host/target/ver" + return 1 + fi ver="${ver//\//_}" ret="${ver}_${host}" } @@ -196,11 +200,14 @@ init_env_build_sysroot() { # /ae/bash/0_git_cache_dir/gcc/build/gcc/specs - init_by_env_name "${1}" local target="${2}" + if [[ -z "$target" ]]; then + log_error "ERROR: target is required for init_env_build_sysroot\n" + exit 1 + fi + init_by_env_name "${1}" "$target" local t_cpu t_vendor t_os t_abi parse_triplet "$target" t_cpu t_vendor t_os t_abi - local linuxarch=$t_cpu if [[ $linuxarch == "aarch64" ]]; then linuxarch="arm64" @@ -209,11 +216,6 @@ init_env_build_sysroot() { elif [[ $linuxarch != x86_64 ]]; then linuxarch="${linuxarch%%[0-9]*}" fi - # 参数检查:确保必要参数存在 - if [[ -z "$target" ]]; then - log_error "ERROR: target is required for init_env_build_sysroot\n" - exit 1 - fi local build_triplet build_triplet_no_vendor detect_platform_triplet build_triplet build_triplet_no_vendor ( @@ -228,7 +230,7 @@ init_env_build_sysroot() { ensure_repo_version src_dir "linux" "${gev_linux_ver}" cd $src_dir || exit 89 check_and_create_dir "${install_prefix}" - exec_record "make headers_install ARCH=${linuxarch} -j$(nproc) INSTALL_HDR_PATH=\"${install_prefix}\"" + exec_record_fast_fail "make headers_install ARCH=${linuxarch} -j$(nproc) INSTALL_HDR_PATH=\"${install_prefix}\"" ) check_ret_exit "安装内核头文件失败!" ( @@ -308,30 +310,20 @@ attach_env_local_cross() { load_toolchains_under() { local base="$1" [[ -d "$base" ]] || return 0 - - local prefix trip - - # base 下每个子目录当作一个工具链 prefix(例如 cross/gcc_xxx 或 canadian/gcc_xxx) + local prefix + local trip + local n for prefix in "$base"/*; do - [[ -d "$prefix" ]] || continue [[ -d "$prefix/bin" ]] || continue - - # prefix 下可能有 triplet 目录(aarch64-xxx-gnu 这类) for trip in "$prefix"/*; do [[ -d "$trip" ]] || continue - local n n="$(basename "$trip")" - - # 跳过常见非 triplet 目录 case "$n" in - bin | lib | lib64 | include | share | etc | var | tmp | sysroot | cross | canadian | libexec) continue ;; + bin|lib|lib64|include|share|etc|var|tmp|sysroot|cross|canadian|libexec) continue ;; esac - load_toolchain "$prefix" "$n" - log_info "loaded toolchain: prefix=$prefix triplet=$n\n" - # 满足目录形态才加载 - if [[ -d "$prefix/$n/usr" || -d "$prefix/$n/sysroot/usr" || -d "$prefix/$n/sysroot/lib" ]]; then + if [[ -d "$prefix/$n/usr" || -d "$prefix/$n/sysroot/usr" || -d "$prefix/$n/sysroot/lib" || -d "$prefix/$n/sysroot/lib64" ]]; then load_toolchain "$prefix" "$n" - log_info "loaded toolchain: prefix=$prefix triplet=$n\n" + log_info "loaded toolchain: prefix=%s triplet=%s" "$prefix" "$n" fi done done @@ -349,8 +341,6 @@ attach_env_canadian_stage3() { bge_build="$gev_local_triplet" bge_host="$host" bge_target="$target" - local toolchain_name - local toolchain_name cacl_toolchain_name_canadian toolchain_name "${host}" "${ver}" bge_install_prefix="${gev_canadian_dir}/${toolchain_name}" diff --git a/linux_shell/build_script/base/common.bash b/linux_shell/build_script/base/common.bash index ec32a1a..5e50bcf 100644 --- a/linux_shell/build_script/base/common.bash +++ b/linux_shell/build_script/base/common.bash @@ -1,175 +1,172 @@ #!/bin/bash -# shellcheck disable=SC2155 -if ! type "include" > /dev/null 2>&1; then +if ! type include >/dev/null 2>&1; then include() { - . "$(cd "$(dirname "$1")" && pwd)/${2}"; - }; + . "$(cd "$(dirname "$1")" && pwd)/${2}" + } fi include "${BASH_SOURCE[0]}" ../pragma_once.bash -if ! pragma_once ${BASH_SOURCE[0]}; then return 0; fi +if ! pragma_once "${BASH_SOURCE[0]}"; then + return 0 +fi include "${BASH_SOURCE[0]}" ./global_rely/export.bash -# shellcheck source=./global_rely/export.bash - - +_exec_record_duration() { + local start_ts="$1" + local end_ts="$2" + awk "BEGIN {print $end_ts - $start_ts}" +} +_exec_record_report() { + local cmd="$1" + local ret="$2" + local duration="$3" + if ((ret != 0)); then + local pretty_cmd + pretty_cmd="$(printf '%s\n' "$cmd" | sed 's/ --/\n--/g')" + log_error "%s:cmd failed: %s, duration %ss" "$(pwd)" "$pretty_cmd" "$duration" + else + log_info "ok duration %ss" "$duration" + fi +} exec_record() { local cmd="$1" - local start_time=$(date '+%Y-%m-%d %H:%M:%S.%3N') - local start_ts=$(date +%s.%3N) - log_info "[$(pwd)] exec: $cmd " - eval "$cmd" - local ret=$? - local end_time=$(date '+%Y-%m-%d %H:%M:%S.%3N') - local end_ts=$(date +%s.%3N) - local duration=$(awk "BEGIN {print $end_ts - $start_ts}") - if [ $ret -ne 0 ]; then - local pretty_cmd=$(printf '%s\n' "$cmd" | sed 's/ --/\n--/g') - log_error "$(pwd):cmd failed: $pretty_cmd, duration ${duration}s" - else - log_info "ok duration ${duration}s" - fi - return $ret -} - -exec_record_fast_fail() { - local cmd="$1" - local log_file="$LOG_PATH" - local start_time local start_ts - local end_time local end_ts local duration local ret - local pretty_cmd - - start_time=$(date '+%Y-%m-%d %H:%M:%S.%3N') - start_ts=$(date +%s.%3N) - - log_info "[$(pwd)] exec: $cmd" - - { - echo "[CMD-BEGIN] $start_time" - echo "[PWD] $(pwd)" - echo "[CMD] $cmd" - eval "$cmd" - ret=$? - echo "[CMD-END] ret=$ret" - exit $ret - } 2>&1 | tee -a "$log_file" - - ret=${PIPESTATUS[0]} - - end_time=$(date '+%Y-%m-%d %H:%M:%S.%3N') - end_ts=$(date +%s.%3N) - duration=$(awk "BEGIN {print $end_ts - $start_ts}") - - if [ $ret -ne 0 ]; then - pretty_cmd=$(printf '%s\n' "$cmd" | sed 's/ --/\n--/g') - log_error "$(pwd):cmd failed: $pretty_cmd, duration ${duration}s" + start_ts="$(date +%s.%3N)" + log_info "[%s] exec: %s" "$(pwd)" "$cmd" + if eval "$cmd"; then + ret=0 else - log_info "ok duration ${duration}s" + ret=$? fi - - return $ret + end_ts="$(date +%s.%3N)" + duration="$(_exec_record_duration "$start_ts" "$end_ts")" + _exec_record_report "$cmd" "$ret" "$duration" + return "$ret" +} +exec_record_fast_fail() { + local cmd="$1" + local start_ts + local end_ts + local duration + local ret + local stdout_fd + local stderr_fd + start_ts="$(date +%s.%3N)" + log_info "[%s] exec: %s" "$(pwd)" "$cmd" + if [[ -n "${LOG_PATH:-}" ]]; then + exec {stdout_fd}>&1 + exec {stderr_fd}>&2 + exec > >(tee -a "$LOG_PATH") 2>&1 + fi + if eval "$cmd"; then + ret=0 + else + ret=$? + fi + if [[ -n "${LOG_PATH:-}" ]]; then + exec 1>&$stdout_fd 2>&$stderr_fd + exec {stdout_fd}>&- + exec {stderr_fd}>&- + fi + end_ts="$(date +%s.%3N)" + duration="$(_exec_record_duration "$start_ts" "$end_ts")" + _exec_record_report "$cmd" "$ret" "$duration" + if ((ret != 0)); then + exit "$ret" + fi + return 0 } - - exec_record_fast_fail_no_log() { local cmd="$1" - exec_record "$cmd" - local ret=$? - if [ $ret -ne 0 ]; then - exit $ret + if exec_record "$cmd"; then + return 0 + else + local ret=$? + exit "$ret" fi } - - append_rpath() { - if [ -z "$1" ] || [ -z "$2" ]; then - echo "Usage: update_rpath " + local dir_path="$1" + local rpath_value="$2" + if [[ -z "$dir_path" || -z "$rpath_value" ]]; then + echo "Usage: append_rpath " >&2 return 1 fi - DIR_PATH="$1" - RPATH_VALUE="$2" - if [ ! -d "$DIR_PATH" ]; then - echo "Error: Directory '$DIR_PATH' does not exist." + if [[ ! -d "$dir_path" ]]; then + echo "Error: Directory '$dir_path' does not exist." >&2 return 1 fi - find "$DIR_PATH" -type f -exec file {} \; | grep -E 'ELF|shared object' | while read -r line; do - #find "$DIR_PATH" -type f ! -name "*.o" | parallel file {} | grep -E 'ELF|shared object' | while read -r line; do - FILE_PATH=$(echo "$line" | cut -d: -f1) - RPATH=$(readelf -d "$FILE_PATH" | grep -i rpath) - if [ ! -z "$RPATH" ]; then - CURRENT_RPATH=$(readelf -d "$FILE_PATH" | grep -i rpath | awk '{print $2}' | tr -d '[]') - NEW_RPATH="$CURRENT_RPATH:$RPATH_VALUE" + local line + local file_path + local current_rpath + local new_rpath + find "$dir_path" -type f -exec file {} \; | grep -E 'ELF|shared object' | while read -r line; do + file_path="${line%%:*}" + current_rpath="$(readelf -d "$file_path" 2>/dev/null | awk '/RPATH|RUNPATH/ {gsub(/[][]/, "", $NF); print $NF; exit}')" + if [[ -n "$current_rpath" ]]; then + new_rpath="$current_rpath:$rpath_value" else - NEW_RPATH="$RPATH_VALUE" + new_rpath="$rpath_value" fi - patchelf --set-rpath "$NEW_RPATH" "$FILE_PATH" - # 验证修改是否成功 - if [ $? -eq 0 ]; then - echo "Updated rpath for: $FILE_PATH" + if patchelf --set-rpath "$new_rpath" "$file_path"; then + echo "Updated rpath for: $file_path" else - echo "Error: Failed to update rpath for $FILE_PATH" + echo "Error: Failed to update rpath for $file_path" >&2 + return 1 fi done } - - -merge_args(){ - local -n ret=$1 - local t=$2 - local list="" - shift 2 - for param in "$@"; do - list="${list}${list:+$t}$param" - done - ret="$list" +merge_args() { + local -n ret="$1" + local separator="$2" + local list="" + local param + shift 2 + for param in "$@"; do + list="${list}${list:+$separator}$param" + done + ret="$list" } - - - -#load_toolchain() { -# p=$1 # 工具链根路径 -# n=$2 # 工具链架构名三元组 -# local d="${p}/${n}" -# local d2="${p}/${n}/usr" -# -# local libs="$d2/lib:$d2/lib64:${p}/lib:${p}/lib64" -# # exe运行时查找库的路径 -# export LD_LIBRARY_PATH="$libs${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" -# export LIBRARY_PATH="$libs${LIBRARY_PATH:+:$LIBRARY_PATH}" -# # exe查找路径 -# export PATH="${p}/bin${PATH:+:$PATH}" -#} - -load_toolchain() { - local p="$1" # 工具链根路径(prefix) - local n="$2" # 三元组目录名 - local d="${p}/${n}" - - log_info "load_toolchain path: ${p} 三元组: ${n}" - # 兼容两种布局: - # 1) prefix//usr/... - # 2) prefix//sysroot/usr/... - local d2="${d}/usr" - if [[ ! -d "$d2" && -d "${d}/sysroot/usr" ]]; then - d2="${d}/sysroot/usr" - fi - - # 如果还是不存在,就直接跳过 - local libs - if [[ ! -d "$d2" ]]; then - log_warn "skip load_toolchain: no usr found under ${d2}" - libs="${p}/lib:${p}/lib64" +_prepend_path_unique() { + local var_name="$1" + local value="$2" + local current="${!var_name:-}" + case ":$current:" in + *":$value:"*) return 0 ;; + esac + if [[ -n "$current" ]]; then + printf -v "$var_name" '%s:%s' "$value" "$current" else - libs="${d2}/lib:${d2}/lib64:${p}/lib:${p}/lib64" + printf -v "$var_name" '%s' "$value" fi - - - export LD_LIBRARY_PATH="${libs}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" - export LIBRARY_PATH="${libs}${LIBRARY_PATH:+:$LIBRARY_PATH}" - export PATH="${p}/bin${PATH:+:$PATH}" - + export "$var_name" } - -return 0 \ No newline at end of file +load_toolchain() { + local prefix="$1" + local triplet="$2" + local target_root="${prefix}/${triplet}" + local libs=() + local lib + log_info "load_toolchain path: %s 三元组: %s" "$prefix" "$triplet" + if [[ -d "${target_root}/usr" ]]; then + libs+=("${target_root}/usr/lib" "${target_root}/usr/lib64") + elif [[ -d "${target_root}/sysroot/usr" ]]; then + libs+=("${target_root}/sysroot/usr/lib" "${target_root}/sysroot/usr/lib64" "${target_root}/sysroot/lib" "${target_root}/sysroot/lib64") + elif [[ -d "${target_root}/sysroot/lib" || -d "${target_root}/sysroot/lib64" ]]; then + libs+=("${target_root}/sysroot/lib" "${target_root}/sysroot/lib64") + else + log_warn "skip load_toolchain: no recognized sysroot under %s" "$target_root" + return 0 + fi + libs+=("${prefix}/lib" "${prefix}/lib64") + for lib in "${libs[@]}"; do + [[ -d "$lib" ]] || continue + _prepend_path_unique LD_LIBRARY_PATH "$lib" + _prepend_path_unique LIBRARY_PATH "$lib" + done + if [[ -d "${prefix}/bin" ]]; then + _prepend_path_unique PATH "${prefix}/bin" + fi +} +return 0 diff --git a/linux_shell/build_script/base/deploy.bash b/linux_shell/build_script/base/deploy.bash index 5ff759c..9c40dec 100644 --- a/linux_shell/build_script/base/deploy.bash +++ b/linux_shell/build_script/base/deploy.bash @@ -83,7 +83,7 @@ cp_with_symlink() { ret=$_ret_ local rlink_target_path="${install_root}${extern_rlink_target_path}" local rlink_path="${install_root}${extern_rlink_path}" - local rlink_path_dir="$(dirname ${rlink_target_path})" + local rlink_path_dir="$(dirname "$rlink_path")" get_relative_path "$rlink_path_dir" "$rlink_target_path" ln -sf "$_ret_" "$rlink_path" [ -n "$_cur_log_path_" ] && echo "=file_symlink= ln -sf $_ret_ $rlink_path " >>"$_cur_log_path_" diff --git a/linux_shell/build_script/base/download_msys2.bash b/linux_shell/build_script/base/download_msys2.bash index cfae9cd..1eed0de 100644 --- a/linux_shell/build_script/base/download_msys2.bash +++ b/linux_shell/build_script/base/download_msys2.bash @@ -63,16 +63,20 @@ download_msys2_pkg() { local pkg_name="$3" local pkg_root="$4" local sha256="${5:-}" - check_and_create_dir_var pkg_root + check_and_create_dir_var pkg_root || exit $? out_path="${pkg_root}/${pkg_name}" local url="https://mirror.msys2.org/mingw/${repo}/${pkg_name}" if [[ -f "${out_path}" ]]; then echo "[reuse] ${out_path}" >&2 else echo "[download] ${url}" >&2 - curl -L --fail --retry 3 -o "${out_path}" "${url}" + curl -L --fail --retry 3 -o "${out_path}" "${url}" || { + local ret=$? + rm -f -- "${out_path}" + exit "$ret" + } fi if [[ -n "${sha256}" ]]; then - echo "${sha256} ${out_path}" | sha256sum -c - >&2 + echo "${sha256} ${out_path}" | sha256sum -c - >&2 || exit $? fi } diff --git a/linux_shell/build_script/base/get_src.bash b/linux_shell/build_script/base/get_src.bash index 6c144da..8bd4765 100644 --- a/linux_shell/build_script/base/get_src.bash +++ b/linux_shell/build_script/base/get_src.bash @@ -101,47 +101,37 @@ download_cached_as() { } -get_src_from_wget(){ - if [ "$#" -ne 3 ]; then - echo "用法: get_src_from_wget " - echo " - 存储返回路径的变量名" - echo " - 解压到的目标目录,也是用于构建的目录" - echo " - 要下载的文件的URL" - echo "示例:" - echo ' src_dir=""' - echo ' get_src_from_wget src_dir $_build_dir $url' +get_src_from_wget() { + if [[ "$#" -ne 3 ]]; then + echo "用法: get_src_from_wget " >&2 return 1 fi - - local -n ret=$1 - local dest_dir=$2 - local url=$3 + local -n ret="$1" + local dest_dir="$2" + local url="$3" local suffix + local file_name + local name + local zip_file_path get_suffix suffix "$url" - local file_name=$(basename "$url") - local name=$(basename "$url" .$suffix) - local zip_file_path="${_wget_cache_dir_}/$file_name" + file_name="$(basename "$url")" + name="$(basename "$url" ".$suffix")" + zip_file_path="${_wget_cache_dir_}/$file_name" ret="$dest_dir/$name" - if [ -d $ret ]; then - rm -rf $ret + mkdir -p "$_wget_cache_dir_" "$dest_dir" || return $? + if [[ -d "$ret" ]]; then + rm -rf -- "$ret" || return $? fi - - if [ ! -e "$zip_file_path" ]; then - _network_status_=$? # 获取 wget 命令的返回值 - if [ $_network_status_ -ne 0 ]; then - # 如果 wget 命令失败,打印错误信息并要求用户手动下载 - echo "错误: 网络不通,放弃下载源码包,请手动下载源码包并放到以下路径:" - echo "路径: ${zip_file_path}" - echo "请下载源码包并将其放置在该目录下,然后重新运行脚本。" - exit 1 # 退出脚本 + if [[ ! -e "$zip_file_path" ]]; then + if exec_record "wget -P \"${_wget_cache_dir_}\" \"$url\""; then + _network_status_=0 + else + _network_status_=$? + log_error "下载失败,请手动下载源码包并放到: %s" "$zip_file_path" + return "$_network_status_" fi - exec_record_fast_fail "wget -P ${_wget_cache_dir_} $url" fi - -# if [ ! -e $zip_file_path ]; then -# exec_record "wget -P ${_wget_cache_dir_} $url" -# fi - exec_record_fast_fail "extract $suffix $zip_file_path $dest_dir" + exec_record_fast_fail "extract \"$suffix\" \"$zip_file_path\" \"$dest_dir\"" } @@ -150,35 +140,27 @@ get_src_from_wget(){ configure_gnu_ex() { - if [ "$#" -lt 1 ]; then - echo "用法: configure_gnu_ex \"\"" + if [[ "$#" -lt 1 ]]; then + echo "用法: configure_gnu_ex \"\"" >&2 return 1 fi local src_dir="$1" - local opts="$2" + local opts="${2:-}" local build_dir="$src_dir/build" - exec_record "rm -rf \"${src_dir}/build\"" - exec_record "mkdir -p \"$build_dir\"" - exec_record "cd \"$build_dir\"" local print_opts - print_opts="$(printf "%s" "$opts" | tr -s ' ' '\n')" - log_info "【$(pwd)/configure" - log_info "${print_opts}" - log_info "】" + exec_record "rm -rf \"$build_dir\"" || return $? + exec_record "mkdir -p \"$build_dir\"" || return $? + exec_record "cd \"$build_dir\"" || return $? + print_opts="$(printf '%s' "$opts" | tr -s ' ' '\n')" + log_info "【%s/configure\n%s\n】" "$(pwd)" "$print_opts" exec_record_fast_fail "../configure ${opts}" - - - if [ $? -ne 0 ]; then - print_remain_args "src_dir:${src_dir} configure failed" "$opts" - exit 1 - fi } configure_gnu() { - if [ "$#" -lt 1 ]; then - echo "用法: configure_gnu [additional configure options...]" + if [[ "$#" -lt 1 ]]; then + echo "用法: configure_gnu [additional configure options...]" >&2 return 1 fi local src_dir="$1" @@ -190,26 +172,21 @@ configure_gnu() { configure_gnu_zip_ex() { - # 参数检查 - if [ "$#" -lt 1 ]; then - echo "用法: configure_gnu_zip_ex \"\"" - echo " - 要下载并构建的源代码的 URL" - echo " - 传递给 configure 的额外参数(字符串)" - echo "示例:" - echo ' configure_gnu_zip_ex http://example.com/source.tar.gz "--prefix=/usr/local --enable-foo"' + if [[ "$#" -lt 1 ]]; then + echo "用法: configure_gnu_zip_ex \"\"" >&2 return 1 fi local url="$1" - local opts="$2" + local opts="${2:-}" local src_dir="" - get_src_from_wget src_dir "${_build_dir_}" "$url" - log_info "$src_dir" "$opts" + get_src_from_wget src_dir "${_build_dir_}" "$url" || return $? + log_info "src=%s opts=%s" "$src_dir" "$opts" configure_gnu_ex "$src_dir" "$opts" } configure_gnu_zip() { - if [ "$#" -lt 1 ]; then - echo "用法: configure_gnu_zip [additional build options...]" + if [[ "$#" -lt 1 ]]; then + echo "用法: configure_gnu_zip [additional build options...]" >&2 return 1 fi local url="$1" @@ -225,20 +202,20 @@ configure_gnu_zip() { build_gnu_zip_ex() { - if [ "$#" -lt 1 ]; then - echo "用法: build_gnu_zip_ex \"\"" + if [[ "$#" -lt 1 ]]; then + echo "用法: build_gnu_zip_ex \"\"" >&2 return 1 fi local url="$1" - local opts="$2" - configure_gnu_zip_ex "$url" "$opts" + local opts="${2:-}" + configure_gnu_zip_ex "$url" "$opts" || return $? exec_record_fast_fail "make -j $(nproc)" exec_record_fast_fail "make install -j $(nproc)" } build_gnu_zip() { - if [ "$#" -lt 1 ]; then - echo "用法: build_gnu_zip [opts...]" + if [[ "$#" -lt 1 ]]; then + echo "用法: build_gnu_zip [opts...]" >&2 return 1 fi local url="$1" diff --git a/linux_shell/build_script/base/git.bash b/linux_shell/build_script/base/git.bash index 514bd6a..bb7547b 100644 --- a/linux_shell/build_script/base/git.bash +++ b/linux_shell/build_script/base/git.bash @@ -304,61 +304,35 @@ ensure_repo_version() { local ver="$3" repo_dir="${_git_cache_dir_}/${dep}" local urls=() - log_info "ensure_repo_version ${dep} ${ver}" - get_git_urls "$dep" urls || return 1 + get_git_urls "$dep" urls || exit $? local repo_url="${urls[0]}" - # CHECK_REPO - - # CHECK_LOCAL log_info "检查是否需要克隆" if [[ ! -d "$repo_dir/.git" ]]; then log_info "克隆仓库: $repo_url → $repo_dir" - if ! exec_record "git clone --origin origin \"$repo_url\" \"$repo_dir\" "; then - log_error "clone 失败" - return 1 - fi + exec_record_fast_fail "git clone --origin origin \"$repo_url\" \"$repo_dir\" " fi - - # CHECK_LOCAL log_info "测试本地是否有版本" if repo_has_ver_local "$repo_dir" "$ver"; then log_info "checkout $ver" - exec_record "git -C \"$repo_dir\" checkout \"$ver\" " || { - log_error "checkout 失败" - return 1 - } + exec_record_fast_fail "git -C \"$repo_dir\" checkout \"$ver\" " return 0 fi - - # FETCH log_info "fetch 更新仓库" - if ! exec_record "git -C \"$repo_dir\" fetch --tags origin"; then - log_error "fetch 失败" - return 1 - fi - - if repo_has_ver_remote "$repo_dir" "${ver}"; then - : - else - log_error "远端仓库不存在版本: $ver" - print_repo_refs_remote "$repo_dir" - #exec_record " git ls-remote ${repo_url}" - return 3 - fi - - # CHECK_LOCAL AGAIN + exec_record_fast_fail "git -C \"$repo_dir\" fetch --tags origin" if repo_has_ver_local "$repo_dir" "$ver"; then log_info "checkout $ver" - - exec_record "git -C \"$repo_dir\" checkout \"$ver\" " || { - log_error "checkout 失败" - return 1 - } + exec_record_fast_fail "git -C \"$repo_dir\" checkout \"$ver\" " return 0 fi - - return 0 + if git -C "$repo_dir" show-ref --verify --quiet "refs/remotes/origin/$ver"; then + log_info "checkout branch $ver" + exec_record_fast_fail "git -C \"$repo_dir\" checkout -B \"$ver\" \"origin/$ver\" " + return 0 + fi + log_error "远端仓库不存在版本: $ver" + print_repo_refs_remote "$repo_url" + exit 3 } #configure_git_repo(){ diff --git a/linux_shell/build_script/base/global_rely/export.bash b/linux_shell/build_script/base/global_rely/export.bash index 583c283..04887af 100644 --- a/linux_shell/build_script/base/global_rely/export.bash +++ b/linux_shell/build_script/base/global_rely/export.bash @@ -1,14 +1,13 @@ #!/bin/bash -if ! type "include" > /dev/null 2>&1; then +if ! type include >/dev/null 2>&1; then include() { - . "$(cd "$(dirname "$1")" && pwd)/${2}"; - }; + . "$(cd "$(dirname "$1")" && pwd)/${2}" + } fi include "${BASH_SOURCE[0]}" ../../pragma_once.bash -if ! pragma_once ${BASH_SOURCE[0]}; then return 0; - . ./log.bash - . ./stack_trace.bash +if ! pragma_once "${BASH_SOURCE[0]}"; then + return 0 fi include "${BASH_SOURCE[0]}" ./log.bash include "${BASH_SOURCE[0]}" ./stack_trace.bash -return 0 \ No newline at end of file +return 0 diff --git a/linux_shell/build_script/base/global_rely/log.bash b/linux_shell/build_script/base/global_rely/log.bash index fda80f1..e2da8e0 100644 --- a/linux_shell/build_script/base/global_rely/log.bash +++ b/linux_shell/build_script/base/global_rely/log.bash @@ -1,21 +1,14 @@ #!/bin/bash -if ! type "include" > /dev/null 2>&1; then +if ! type include >/dev/null 2>&1; then include() { - . "$(cd "$(dirname "$1")" && pwd)/${2}"; - }; + . "$(cd "$(dirname "$1")" && pwd)/${2}" + } fi - include "${BASH_SOURCE[0]}" ../../pragma_once.bash -if ! pragma_once ${BASH_SOURCE[0]}; then return 0; - source ../../pragma_once.bash +if ! pragma_once "${BASH_SOURCE[0]}"; then + return 0 fi - - - - - -# 这个文件是构建的基础文件 存放最顶层的函数 -RESET='\033[0m' # 重置颜色 +RESET='\033[0m' BLACK='\033[0;30m' RED='\033[0;31m' GREEN='\033[0;32m' @@ -32,127 +25,94 @@ BOLD_BLUE='\033[1;34m' BOLD_PURPLE='\033[1;35m' BOLD_CYAN='\033[1;36m' BOLD_WHITE='\033[1;37m' -export LOG_LEVEL="${LOG_LEVEL:-3}" # 0=ERROR 1=WARN 2=INFO 3=DEBUG 4=TRACE -export LOG_PATH="${LOG_PATH:-}" # 空则不写文件 -export LOG_STDOUT="${LOG_STDOUT:-1}" # 1 输出终端 -export LOG_COLOR_STDOUT="${LOG_COLOR_STDOUT:-1}" # 默认是否彩色 -export LOG_INIT_MODE="${LOG_INIT_MODE:-rotate}" # truncate|append|rotate -export LOG_INITIALIZED=0 - - +export LOG_LEVEL="${LOG_LEVEL:-3}" +export LOG_PATH="${LOG_PATH:-}" +export LOG_STDOUT="${LOG_STDOUT:-1}" +export LOG_COLOR_STDOUT="${LOG_COLOR_STDOUT:-1}" +export LOG_INIT_MODE="${LOG_INIT_MODE:-rotate}" +export LOG_INITIALIZED="${LOG_INITIALIZED:-0}" log_init() { - # 如果没有设置 LOG_PATH,直接返回 [[ -z "$LOG_PATH" ]] && return 0 - - # 如果已经初始化过,直接返回 - (( LOG_INITIALIZED )) && return 0 - - # 获取日志文件的目录 + ((LOG_INITIALIZED)) && return 0 local log_dir - log_dir=$(dirname "$LOG_PATH") - - # 确保日志文件的目录存在,如果不存在则创建 - if [[ ! -d "$log_dir" ]]; then - mkdir -p "$log_dir" || { echo "ERROR: Failed to create log directory: $log_dir"; exit 1; } - fi - - # 确保日志文件存在,如果不存在则创建 - if [[ ! -f "$LOG_PATH" ]]; then - touch "$LOG_PATH" || { echo "ERROR: Failed to create log file: $LOG_PATH"; exit 1; } - chmod 777 $LOG_PATH - fi - - # 根据初始化模式进行操作 + log_dir="$(dirname "$LOG_PATH")" + mkdir -p "$log_dir" || { + echo "ERROR: Failed to create log directory: $log_dir" >&2 + return 1 + } case "$LOG_INIT_MODE" in truncate) - : > "$LOG_PATH" # 清空日志文件 + : >"$LOG_PATH" || return $? ;; append) - # 保持原样,直接追加 + touch "$LOG_PATH" || return $? ;; rotate) - # 如果日志文件存在,重命名为带时间戳的文件 if [[ -f "$LOG_PATH" ]]; then - mv "$LOG_PATH" "$LOG_PATH.$(date +%Y%m%d_%H%M%S)" + mv "$LOG_PATH" "$LOG_PATH.$(TZ=Asia/Shanghai date +%Y%m%d_%H%M%S)" || return $? fi + touch "$LOG_PATH" || return $? ;; *) - echo "Invalid LOG_INIT_MODE: $LOG_INIT_MODE" - exit 1 + echo "Invalid LOG_INIT_MODE: $LOG_INIT_MODE" >&2 + return 1 ;; esac - - # 标记为已初始化 LOG_INITIALIZED=1 } log() { - - log_init - local level_name="$1" local level_num="$2" local use_color="$3" shift 3 - - (( level_num > LOG_LEVEL )) && return 0 - + ((level_num > LOG_LEVEL)) && return 0 + log_init || return $? local fmt="$1" shift - local body - - printf -v body -- "$fmt" "$@" local timestamp - #timestamp="$(date '+%Y-%m-%d %H:%M:%S')" - - timestamp="$(TZ=Asia/Shanghai date '+%Y-%m-%d %H:%M:%S')" - local msg - printf -v msg "[%s] [%s] %s" \ - "$timestamp" \ - "$level_name" \ - "$body" - - # -------- stdout -------- - if (( LOG_STDOUT )); then - + printf -v body -- "$fmt" "$@" + timestamp="$(TZ=Asia/Shanghai date '+%Y-%m-%d %H:%M:%S')" + printf -v msg "[%s] [%s] %s" "$timestamp" "$level_name" "$body" + if ((LOG_STDOUT)); then local enable_color - + local color="" if [[ -z "$use_color" ]]; then enable_color="$LOG_COLOR_STDOUT" else enable_color="$use_color" fi - - if (( enable_color )) && [[ -t 1 ]]; then - - local color="" + if ((enable_color)) && [[ -t 1 ]]; then case "$level_name" in ERROR) color="$BOLD_RED" ;; - WARN) color="$BOLD_YELLOW" ;; - INFO) color="$GREEN" ;; + WARN) color="$BOLD_YELLOW" ;; + INFO) color="$GREEN" ;; DEBUG) color="$CYAN" ;; TRACE) color="$PURPLE" ;; - *) color="$WHITE" ;; + *) color="$WHITE" ;; esac - printf "%b%s%b\n" "$color" "$msg" "$RESET" else printf "%s\n" "$msg" fi fi - - # -------- file -------- if [[ -n "$LOG_PATH" ]]; then - printf "%s\n" "$msg" >> "$LOG_PATH" + printf "%s\n" "$msg" >>"$LOG_PATH" fi } - - -log_error() { log ERROR 0 1 "$@"; } -log_warn() { log WARN 1 1 "$@"; } -log_info() { log INFO 2 1 "$@"; } -log_debug() { log DEBUG 3 1 "$@"; } -log_trace() { log TRACE 4 1 "$@"; } - - +log_error() { + log ERROR 0 1 "$@" +} +log_warn() { + log WARN 1 1 "$@" +} +log_info() { + log INFO 2 1 "$@" +} +log_debug() { + log DEBUG 3 1 "$@" +} +log_trace() { + log TRACE 4 1 "$@" +} diff --git a/linux_shell/build_script/base/global_rely/stack_trace.bash b/linux_shell/build_script/base/global_rely/stack_trace.bash index 0783b41..07a8db3 100644 --- a/linux_shell/build_script/base/global_rely/stack_trace.bash +++ b/linux_shell/build_script/base/global_rely/stack_trace.bash @@ -1,104 +1,63 @@ -if ! type "include" > /dev/null 2>&1; then +#!/bin/bash +if ! type include >/dev/null 2>&1; then include() { - . "$(cd "$(dirname "$1")" && pwd)/${2}"; - }; + . "$(cd "$(dirname "$1")" && pwd)/${2}" + } fi include "${BASH_SOURCE[0]}" ../../pragma_once.bash - -if ! pragma_once ${BASH_SOURCE[0]}; then return 0; - source ../../pragma_once.bash +if ! pragma_once "${BASH_SOURCE[0]}"; then + return 0 fi - - -set -Eeo pipefail -# -e 命令失败退出 -# -E ERR trap 在函数中生效 -# -u 未定义变量报错 -# pipefail pipeline 失败传播 -#set -E - - - -# -E 的作用是:让 ERR trap 在函数、子 shell 等上下文中也会触发 - +include "${BASH_SOURCE[0]}" ./log.bash declare -gi G_PRINT_STACK_TRACE_COUNT=0 - print_stack_trace() { - - printf "\n\n" - local ret="${1}" - local reason="${2}" - - G_Root="D:/proj/plant_sun_cat/build_script" - - + local ret="$1" + local reason="$2" local ts - ts="$(TZ=Asia/Shanghai date '+%Y-%m-%d %H:%M:%S.%3N')" - local pid="$$" local bashpid="${BASHPID:-}" local ppid="${PPID:-}" local pwd_now - pwd_now="$(pwd 2>/dev/null || echo '')" - local script_name="${BASH_SOURCE[1]:-${BASH_SOURCE[0]}}" local last_cmd="${BASH_COMMAND:-}" - - local root_dir="${_library_root_dir_:-}" - local g_root="D:/proj/plant_sun_cat/build_script" - - log_error "\n[#%d]【%s】ret=%d pid=%s bashpid=%s ppid=%s \npwd=%s \nscript=%s \ncmd=%s \nts=%s \nlibrary_root=%s" \ - "$G_PRINT_STACK_TRACE_COUNT" \ - "$reason" \ - "$ret" \ - "$pid" \ - "$bashpid" \ - "$ppid" \ - "$pwd_now" \ - "$script_name" \ - "$last_cmd" \ - "$ts" \ - "$root_dir" - ((G_PRINT_STACK_TRACE_COUNT++)) || true - - - - + local root_dir="${_library_root_dir_:-}" local depth=${#FUNCNAME[@]} - - for ((i=0;i/dev/null || printf '%s' "$file")" + func="${FUNCNAME[$i]:-MAIN}" + if ((i == 0)); then + line="${BASH_LINENO[0]:-0}" else - line="${BASH_LINENO[$((i-1))]}" + line="${BASH_LINENO[$((i - 1))]:-0}" fi - - log_warn " [%2d] [%s] file:///%s:%s" "$i" "$func" "$file" "$line" + log_warn " [%2d] [%s] file://%s:%s" "$i" "$func" "$file" "$line" done - - exit 75 + return "$ret" } - __on_err() { local ret="$1" local cmd="$2" local line="$3" - - print_stack_trace "$ret" "进程错误 cmd=$cmd line=$line" + print_stack_trace "$ret" "进程错误 cmd=$cmd line=$line" || true + return "$ret" +} +enable_stack_trace() { + trap '__on_err "$?" "$BASH_COMMAND" "$LINENO"' ERR +} +disable_stack_trace() { + trap - ERR +} +enable_strict_error_mode() { + set -Eeo pipefail + enable_stack_trace } - -trap '__on_err "$?" "$BASH_COMMAND" "$LINENO"' ERR - -#trap 'print_stack_trace "进程错误" ' ERR - - -#trap 'echo exit $? ' EXIT - - diff --git a/linux_shell/build_script/base/init_env.bash b/linux_shell/build_script/base/init_env.bash index f3de36f..a77ca6c 100644 --- a/linux_shell/build_script/base/init_env.bash +++ b/linux_shell/build_script/base/init_env.bash @@ -1,41 +1,28 @@ - -if ! type "include" > /dev/null 2>&1; then +#!/bin/bash +if ! type include >/dev/null 2>&1; then include() { - . "$(cd "$(dirname "$1")" && pwd)/${2}"; - }; + . "$(cd "$(dirname "$1")" && pwd)/${2}" + } fi include "${BASH_SOURCE[0]}" ../pragma_once.bash -if ! pragma_once ${BASH_SOURCE[0]}; then return 0; fi -include "${BASH_SOURCE[0]}" ./global_rely/export.bash -include "${BASH_SOURCE[0]}" ./other.bash -# shellcheck source=./global_rely/export.bash -# shellcheck source=./other.bash - - -echo -e "============================ 开始加载环境 ============================ " -## 开启调试模式,打印执行的每一行命令 -#set -x -## 关闭调试模式 -#set +x - -_ret_="" -_root_="" -echo "OSTYPE:$OSTYPE" -if [[ "$OSTYPE" == "linux-gnu"* ]]; then - _root_="/ae/bash" -elif [[ "$OSTYPE" == "win32"* ]]; then - _root_="/d/ae/cached_external_library/bash" -elif [[ "$OSTYPE" == "cygwin"* ]]; then - _root_="/d/ae/cached_external_library/bash" -elif [[ "$OSTYPE" == "msys"* ]]; then - _root_="/d/ae/cached_external_library/bash" -else - echo "未知操作系统类型:$OSTYPE" - exit 1 +if ! pragma_once "${BASH_SOURCE[0]}"; then + return 0 fi - -#_library_root_dir_="$(cd "$(dirname ${BASH_SOURCE[0]})" && pwd)/" -# 脚本在 library_root/base 下面所以往上找一层 +include "${BASH_SOURCE[0]}" ./global_rely/export.bash +_ret_="" +case "$OSTYPE" in +linux-gnu*) + _ae_root_="${PSC_AE_ROOT:-/ae}" + ;; +win32*|cygwin*|msys*) + _ae_root_="${PSC_AE_ROOT:-/d/ae/cached_external_library}" + ;; +*) + log_error "未知操作系统类型: %s" "$OSTYPE" + return 1 + ;; +esac +_root_="${PSC_BASH_ROOT:-${_ae_root_}/bash}" _library_root_dir_="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" _cur_log_path_="" _install_="${_root_}/install" @@ -47,24 +34,11 @@ _wget_cache_dir_="${_root_}/0_wget_cache_dir" _git_cache_dir_="${_root_}/0_git_cache_dir" _build_dir_="${_root_}/0_build_dir" _network_status_="" - - - - -echo -e "[init global bash environment!]" -print_important_var _library_root_dir_ -print_important_var _cur_log_path_ -check_and_create_dir_var _root_ -check_and_create_dir_var _install_ -check_and_create_dir_var _src_ -check_and_create_dir_var _win_ -check_and_create_dir_var _iso_repo_ -check_and_create_dir_var _wget_cache_dir_ -check_and_create_dir_var _git_cache_dir_ -check_and_create_dir_var _build_dir_ - - - -echo -e "============================ 加载环境完成 ============================ " - - +init_build_environment() { + local dir + local dirs=("$_root_" "$_install_" "$_src_" "$_win_" "$_iso_repo_" "$_wget_cache_dir_" "$_git_cache_dir_" "$_build_dir_") + for dir in "${dirs[@]}"; do + mkdir -p "$dir" || return $? + done + log_info "build environment initialized: root=%s library=%s" "$_root_" "$_library_root_dir_" +} diff --git a/linux_shell/build_script/base/other.bash b/linux_shell/build_script/base/other.bash index 2cacedb..862441e 100644 --- a/linux_shell/build_script/base/other.bash +++ b/linux_shell/build_script/base/other.bash @@ -288,33 +288,7 @@ print_remain_args(){ -pragma_once() { - # 1. 转换为绝对路径以确保唯一性 - local abs_path - abs_path=$(realpath "$1" 2>/dev/null || echo "$1") - if [ ! -e "$abs_path" ]; then - log_info "错误: 文件 '$1' 不存在。" - exit 1 - fi - # 2. 生成 Key(只保留字母数字,其他转下划线) - local key="key_$(echo "$abs_path" | sed 's/[^a-zA-Z0-9]/_/g')" - - # 3. 精确匹配检查 - # 我们在 map 的两端和 key 的两端都加上分隔符 | - if [[ "$loaded_scripts_map" == *"|$key|"* ]]; then - return 1 # 已经加载过了 - else - # 第一次加载,存入时带上分隔符 - # 如果 map 为空,初始化为 |key|;如果不为空,追加 key| - if [[ -z "$loaded_scripts_map" ]]; then - loaded_scripts_map="|$key|" - else - loaded_scripts_map="${loaded_scripts_map}${key}|" - fi - return 0 # 允许加载 - fi -} # 使用方式 # if pragma_once "${BASH_SOURCE[0]}"; then # fi @@ -439,10 +413,10 @@ is_absolute_elf() { is_valid_absolute_path_format() { local input="$1" if [[ "$input" =~ ^(/[^:/]+)+(:/[^:/]+)+$ ]]; then - return 1 + return 0 fi echo "Invalid format: $input" - return 0 + return 1 } #!/bin/bash @@ -471,27 +445,7 @@ compress_directory_tar_gz() { } -compress_directory_tar_gz() { - local source_dir="$1" # 源目录 - local install_root="$2" # 目标目录 - local archive_name="$3" # 压缩包名称 - if [ ! -d "$source_dir" ]; then - echo "compress_directory Error: Source directory '$source_dir' does not exist!" - return 1 - fi - echo "Compressing '$source_dir' to '$install_root/$archive_name'..." - # 去除硬链接 -# local workdir="$(dirname "$source_dir")" -# local a2="$(basename "$source_dir")" - tar --hard-dereference -czvf "$install_root/$archive_name" -C "${source_dir}" . - if [ $? -eq 0 ]; then - echo "Compression successful! $install_root/$archive_name" - else - echo "Compression failed! $install_root/$archive_name" - return 1 - fi -} # 解压函数:将 .tar.gz 文件解压到目标目录 compress_directory_tar_gz_copy() { @@ -583,47 +537,34 @@ clear_dir_contents() { } check_and_create_dir_var() { - local dir_var=$1 # 传入的变量名 - local dir_path=${!dir_var} # 使用 eval 来获取变量的值 - - # 检查目录是否存在 - if [ -d "$dir_path" ]; then - echo "【${dir_var}】: 【$dir_path】" - : - else - # 如果目录不存在,则尝试创建目录 - echo "【${dir_var}】目录不存在,正在创建: $dir_path" - mkdir -p "$dir_path" # 创建目录,-p 确保父目录也被创建 - if [ $? -ne 0 ]; then - echo "【${dir_var}】错误: 创建目录失败 $dir_path" - exit 1 # 返回失败代码 - else - echo "【${dir_var}】目录创建成功: $dir_path" - fi + local dir_var="$1" + local dir_path="${!dir_var:-}" + if [[ -z "$dir_path" ]]; then + log_error "ERROR: directory variable is empty: %s" "$dir_var" + return 1 fi + if [[ -d "$dir_path" ]]; then + return 0 + fi + mkdir -p "$dir_path" || { + log_error "ERROR: failed to create directory: %s=%s" "$dir_var" "$dir_path" + return 1 + } } check_and_create_dir() { local dir="$1" - # 非空判断:空字符串或全空白都算空 if [[ -z "${dir//[[:space:]]/}" ]]; then log_error "ERROR: dir is empty" - exit 4 + return 4 fi - - if [[ ! -d "$dir" ]]; then - log_info "Attempting to create the directory... 【${dir}】" - mkdir -p "$dir" - if [[ $? -ne 0 ]]; then - log_error "ERROR: Failed to create directory 【${dir}】" - exit 5 - fi - chmod 777 "$dir" - if [[ $? -ne 0 ]]; then - log_error "ERROR: Failed to chmod 777 【${dir}】" - exit 6 - fi + if [[ -d "$dir" ]]; then + return 0 fi + mkdir -p "$dir" || { + log_error "ERROR: failed to create directory: %s" "$dir" + return 5 + } } diff --git a/linux_shell/build_script/base/windows.bash b/linux_shell/build_script/base/windows.bash index 9278c27..7f3db29 100644 --- a/linux_shell/build_script/base/windows.bash +++ b/linux_shell/build_script/base/windows.bash @@ -9,65 +9,7 @@ include "${BASH_SOURCE[0]}" ./global_rely/export.bash include "${BASH_SOURCE[0]}" ./other.bash -run_native_cmd_script() { - local workdir="$1" - local cmd_body="$2" - local mode="${3:-c}" - local win_workdir - local tmp_cmd - local win_tmp_cmd - [[ -n "$workdir" && -n "$cmd_body" ]] || { - echo "[ERROR] 用法: run_native_cmd_script [c|k]" >&2 - return 1 - } - - [[ -d "$workdir" ]] || { - echo "[ERROR] 工作目录不存在: $workdir" >&2 - return 1 - } - - [[ "$mode" == "c" || "$mode" == "k" ]] || { - echo "[ERROR] mode 只能是 c 或 k" >&2 - return 1 - } - - win_workdir="$(cygpath -aw "$workdir")" || { - echo "[ERROR] 工作目录转换失败: $workdir" >&2 - return 1 - } - - tmp_cmd="$(mktemp --suffix=.cmd)" || { - echo "[ERROR] 创建临时 cmd 文件失败" >&2 - return 1 - } - - cat > "$tmp_cmd" <&2 - return 1 - } - - WIN_TMP_CMD="$win_tmp_cmd" \ - WIN_MODE="$mode" \ - powershell.exe -NoProfile -ExecutionPolicy Bypass -Command ' - $cmdFile = $env:WIN_TMP_CMD - $mode = $env:WIN_MODE - $wd = Split-Path -Parent $cmdFile - - Start-Process ` - -FilePath $env:ComSpec ` - -ArgumentList @("/$mode", "`"$cmdFile`"") ` - -WorkingDirectory $wd ` - -UseNewEnvironment - ' -} run_native_cmd_script() { @@ -188,20 +130,5 @@ run_bat_native_env() { # 1. 默认用 /c,执行完退出 # 2. 传 k 时,用 /k,执行完保留 cmd 窗口 -run_bat_native_env() { - local bat_path="$1" - local mode="${2:-c}" - local bat_dir - local bat_name - [[ -f "$bat_path" ]] || { - echo "[ERROR] bat 文件不存在: $bat_path" >&2 - return 1 - } - - bat_dir="$(dirname "$bat_path")" - bat_name="$(basename "$bat_path")" - - run_native_cmd_script "$bat_dir" "call \"$bat_name\"" "$mode" -} diff --git a/linux_shell/build_script/cmake.bash b/linux_shell/build_script/cmake.bash index 347f603..8da1822 100644 --- a/linux_shell/build_script/cmake.bash +++ b/linux_shell/build_script/cmake.bash @@ -16,7 +16,7 @@ include "${BASH_SOURCE[0]}" ./base/export.bash install_cmake() { local ver="$1" - local install_dir="/ae/opt/cmake" + local install_dir="${_ae_root_}/opt/cmake" sudo mkdir -p "${install_dir}" diff --git a/linux_shell/build_script/cmake_toolchain/llvm-mingw.cmake b/linux_shell/build_script/cmake_toolchain/llvm-mingw.cmake index c723df0..2404f9b 100644 --- a/linux_shell/build_script/cmake_toolchain/llvm-mingw.cmake +++ b/linux_shell/build_script/cmake_toolchain/llvm-mingw.cmake @@ -1,6 +1,7 @@ set(CMAKE_SYSTEM_NAME Windows) set(CMAKE_SYSTEM_PROCESSOR x86_64) -set(LLVM_MINGW_ROOT "/ae/opt/llvm-mingw") +get_filename_component(PSC_AE_ROOT "${CMAKE_CURRENT_LIST_DIR}/../../.." ABSOLUTE) +set(LLVM_MINGW_ROOT "${PSC_AE_ROOT}/opt/llvm-mingw") set(CMAKE_C_COMPILER "${LLVM_MINGW_ROOT}/bin/x86_64-w64-mingw32-clang") set(CMAKE_CXX_COMPILER "${LLVM_MINGW_ROOT}/bin/x86_64-w64-mingw32-clang++") set(CMAKE_RC_COMPILER "${LLVM_MINGW_ROOT}/bin/x86_64-w64-mingw32-windres") @@ -12,4 +13,4 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) -set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) \ No newline at end of file +set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) diff --git a/linux_shell/build_script/gcc/#U8f85#U52a9#U811a#U672c/python_gdb.bash b/linux_shell/build_script/gcc/#U8f85#U52a9#U811a#U672c/python_gdb.bash new file mode 100644 index 0000000..e22cc4f --- /dev/null +++ b/linux_shell/build_script/gcc/#U8f85#U52a9#U811a#U672c/python_gdb.bash @@ -0,0 +1,39 @@ +#!/usr/bin/env bash +set -euo pipefail + +# 此脚本用于 binutils-gdb 在host是windows系统的时候 使用的 单独添加选项 --with-python=/ae/sysroots/mingw64/python_gdb.bash 即可使用 +# C:\msys64 +# C:\msys64\var\cache\pacman\pkg 存放下载安装好的mingw包 +# C:\msys64\mingw64 是系统根目录 + +root="/ae/sysroots/mingw64" +lib="${root}/lib" +include="${root}/include" + +case "${2:-${1:-}}" in +--includes|--cflags) + printf '%s\n' "-I${include}/python3.12" + ;; +--ldflags|--libs) + if [ -f "${lib}/libpython3.12.dll.a" ]; then + printf '%s\n' "${lib}/libpython3.12.dll.a" + elif [ -f "${lib}/libpython3.dll.a" ]; then + printf '%s\n' "${lib}/libpython3.dll.a" + elif [ -f "${lib}/libpython3.12.a" ]; then + printf '%s\n' "${lib}/libpython3.12.a" + elif [ -f "${lib}/libpython3.a" ]; then + printf '%s\n' "${lib}/libpython3.a" + else + echo "no usable libpython found in ${lib}" >&2 + exit 1 + fi + ;; +--exec-prefix) + printf '%s\n' "${root}" + ;; +*) + echo "unsupported args: $*" >&2 + exit 1 + ;; +esac + diff --git a/linux_shell/build_script/gcc/bge.bash b/linux_shell/build_script/gcc/bge.bash index 3bfa866..e7c2dbc 100644 --- a/linux_shell/build_script/gcc/bge.bash +++ b/linux_shell/build_script/gcc/bge.bash @@ -101,7 +101,7 @@ bge_refresh_ver(){ bge_init_params() { # 必选项 - bge_install_prefix="/ae/env/local_gcc_15_1_0" + bge_install_prefix="${_ae_root_}/env/local_gcc_15_1_0" # 平台选项 local local_triplet detect_platform_triplet local_triplet local_triplet_no_vendor @@ -316,7 +316,7 @@ build_gcc_ex() { local src_dir ensure_repo_version src_dir "gcc" "$bge_gcc_ver" - exec_record "rm -rf \"${src_dir}/build\"" + exec_record_fast_fail "rm -rf \"${src_dir}/build\"" cd "${src_dir}" || exit @@ -400,7 +400,7 @@ upgrade_local_gcc() { parse_triplet "$local_triplet" local_cpu local_vendor local_os local_abi compose_triplet new_triplet "$local_cpu" "$vendor" "$local_os" "$local_abi" fi - local install_prefix="/ae/toolchain/${new_triplet}" + local install_prefix="${_ae_root_}/toolchain/${new_triplet}" bge_install_prefix="${install_prefix}/$ver" log_info "bge_install_prefix $bge_install_prefix" diff --git a/linux_shell/build_script/gcc/cmake_template/toolchain_template.cmake b/linux_shell/build_script/gcc/cmake_template/toolchain_template.cmake index f42bd96..7935938 100644 --- a/linux_shell/build_script/gcc/cmake_template/toolchain_template.cmake +++ b/linux_shell/build_script/gcc/cmake_template/toolchain_template.cmake @@ -1,75 +1,39 @@ -set(CMAKE_SYSTEM_NAME {{CMAKE_SYSTEM_NAME}}) -set(CMAKE_SYSTEM_PROCESSOR {{CMAKE_SYSTEM_PROCESSOR}}) -set(build {{build}}) -set(host {{host}}) -set(target {{target}}) - - -set(PSC_TOOLCHAIN_ROOT ${CMAKE_CURRENT_LIST_DIR}) +set(CMAKE_SYSTEM_NAME "{{CMAKE_SYSTEM_NAME}}") +set(CMAKE_SYSTEM_PROCESSOR "{{CMAKE_SYSTEM_PROCESSOR}}") +set(build "{{build}}") +set(host "{{host}}") +set(target "{{target}}") +set(PSC_TOOLCHAIN_ROOT "${CMAKE_CURRENT_LIST_DIR}") if(NOT IS_DIRECTORY "${PSC_TOOLCHAIN_ROOT}") message(FATAL_ERROR "Toolchain path not found: ${PSC_TOOLCHAIN_ROOT}") -else() - message( "Toolchain found at: ${PSC_TOOLCHAIN_ROOT}") endif() - - - - - -set(CMAKE_SYSROOT ${PSC_TOOLCHAIN_ROOT}/${target}/sysroot) - -set(prefix ${PSC_TOOLCHAIN_ROOT}/bin/{{target}}-) - -# 编译器 -set(CMAKE_C_COMPILER ${prefix}gcc{{exe_suffix}}) -set(CMAKE_CXX_COMPILER ${prefix}g++{{exe_suffix}}) -set(CMAKE_ASM_COMPILER ${prefix}gcc{{exe_suffix}}) - -# binutils -set(CMAKE_AR ${prefix}ar{{exe_suffix}}) -set(CMAKE_RANLIB ${prefix}ranlib{{exe_suffix}}) -set(CMAKE_STRIP ${prefix}strip{{exe_suffix}}) -set(CMAKE_OBJCOPY ${prefix}objcopy{{exe_suffix}}) -set(CMAKE_OBJDUMP ${prefix}objdump{{exe_suffix}}) -set(CMAKE_NM ${prefix}nm{{exe_suffix}}) -# 其他工具 -set(CMAKE_SIZE ${prefix}size{{exe_suffix}}) -set(CMAKE_STRINGS ${prefix}strings{{exe_suffix}}) -set(CMAKE_ADDR2LINE ${prefix}addr2line{{exe_suffix}}) - -# 处理二进制 -set(CMAKE_READELF ${prefix}readelf{{exe_suffix}}) - -# 链接器 -set(CMAKE_LINKER ${prefix}ld{{exe_suffix}}) - -# 调试工具 -set(CMAKE_GDB ${prefix}gdb{{exe_suffix}}) -set(CMAKE_GDBSERVER ${prefix}gdbserver{{exe_suffix}}) - -# 编译辅助 -set(CMAKE_CPP ${prefix}cpp{{exe_suffix}}) -set(CMAKE_M4 ${prefix}m4{{exe_suffix}}) -#set(CMAKE_MAKE_PROGRAM ${prefix}make{{exe_suffix}}) - -# pkg-config -set(CMAKE_PKG_CONFIG_EXECUTABLE ${prefix}pkg-config{{exe_suffix}}) - -# 内核模块工具 -set(CMAKE_INSMOD ${prefix}insmod{{exe_suffix}}) -set(CMAKE_RMMOD ${prefix}rmmod{{exe_suffix}}) -set(CMAKE_MODINFO ${prefix}modinfo{{exe_suffix}}) - - - +message("Toolchain found at: ${PSC_TOOLCHAIN_ROOT}") +set(CMAKE_SYSROOT "${PSC_TOOLCHAIN_ROOT}/${target}/sysroot") +set(prefix "${PSC_TOOLCHAIN_ROOT}/bin/{{target}}-") +set(CMAKE_C_COMPILER "${prefix}gcc{{exe_suffix}}") +set(CMAKE_CXX_COMPILER "${prefix}g++{{exe_suffix}}") +set(CMAKE_ASM_COMPILER "${prefix}gcc{{exe_suffix}}") +set(CMAKE_AR "${prefix}ar{{exe_suffix}}") +set(CMAKE_RANLIB "${prefix}ranlib{{exe_suffix}}") +set(CMAKE_STRIP "${prefix}strip{{exe_suffix}}") +set(CMAKE_OBJCOPY "${prefix}objcopy{{exe_suffix}}") +set(CMAKE_OBJDUMP "${prefix}objdump{{exe_suffix}}") +set(CMAKE_NM "${prefix}nm{{exe_suffix}}") +set(CMAKE_SIZE "${prefix}size{{exe_suffix}}") +set(CMAKE_STRINGS "${prefix}strings{{exe_suffix}}") +set(CMAKE_ADDR2LINE "${prefix}addr2line{{exe_suffix}}") +set(CMAKE_READELF "${prefix}readelf{{exe_suffix}}") +set(CMAKE_LINKER "${prefix}ld{{exe_suffix}}") +set(CMAKE_GDB "${prefix}gdb{{exe_suffix}}") +set(CMAKE_GDBSERVER "${prefix}gdbserver{{exe_suffix}}") +set(CMAKE_CPP "${prefix}cpp{{exe_suffix}}") +set(CMAKE_M4 "${prefix}m4{{exe_suffix}}") +set(CMAKE_PKG_CONFIG_EXECUTABLE "${prefix}pkg-config{{exe_suffix}}") +set(CMAKE_INSMOD "${prefix}insmod{{exe_suffix}}") +set(CMAKE_RMMOD "${prefix}rmmod{{exe_suffix}}") +set(CMAKE_MODINFO "${prefix}modinfo{{exe_suffix}}") set(CMAKE_FIND_ROOT_PATH "${CMAKE_SYSROOT}") set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) - - - -# 要把 ${PSC_TOOLCHAIN_ROOT}/bin 添加到环境变量 -# DISPLAY=localhost:10.0;QT_QPA_PLATFORM=xcb -# -DCMAKE_TOOLCHAIN_FILE=$ProjectFileDir$/../win_arm_kylin/releases_gcc-9.3.0_x86_64-w64-mingw32/toolchain.cmake \ No newline at end of file diff --git a/linux_shell/build_script/install_llvm.bash b/linux_shell/build_script/install_llvm.bash index 95d451d..5d6777c 100644 --- a/linux_shell/build_script/install_llvm.bash +++ b/linux_shell/build_script/install_llvm.bash @@ -8,7 +8,7 @@ include "${BASH_SOURCE[0]}" ./pragma_once.bash if ! pragma_once ${BASH_SOURCE[0]}; then return 0; fi install_llvm_mingw() { local ver="$1" - local install_dir="/ae/opt/llvm-mingw" + local install_dir="${_ae_root_}/opt/llvm-mingw" local repo="https://github.com/mstorsjo/llvm-mingw.git" local release_base="https://github.com/mstorsjo/llvm-mingw/releases/download" local work_dir="${install_dir}/.install" @@ -77,7 +77,7 @@ install_llvm_mingw() { echo "[llvm-mingw path] ${install_dir}" } check_llvm_mingw() { - local clang="/ae/opt/llvm-mingw/bin/x86_64-w64-mingw32-clang" + local clang="${_ae_root_}/opt/llvm-mingw/bin/x86_64-w64-mingw32-clang" if [[ -x "$clang" ]]; then "$clang" --version | head -n1 return 0 @@ -89,7 +89,7 @@ check_llvm_mingw() { x86_64-w64-mingw32-clang --version | head -n1 } get_llvm_mingw_version() { - local clang="/ae/opt/llvm-mingw/bin/x86_64-w64-mingw32-clang" + local clang="${_ae_root_}/opt/llvm-mingw/bin/x86_64-w64-mingw32-clang" if [[ -x "$clang" ]]; then "$clang" --version | head -n1 return diff --git a/linux_shell/build_script/libc/core.bash b/linux_shell/build_script/libc/core.bash index 7adcbe8..ff51b70 100644 --- a/linux_shell/build_script/libc/core.bash +++ b/linux_shell/build_script/libc/core.bash @@ -43,20 +43,20 @@ make_mingw(){ # configure_gnu_ex "${src_dir}/mingw-w64-headers" "--prefix=${install_prefix}/${MINGWTRIPLET}/usr ${MINGWW64COMMON} " -# exec_record "make -j$(nproc)" -# exec_record "make install-strip -j$(nproc)" +# exec_record_fast_fail "make -j$(nproc)" +# exec_record_fast_fail "make install-strip -j$(nproc)" # # configure_gnu_ex "${src_dir}/mingw-w64-crt" "--prefix=${install_prefix}/${MINGWTRIPLET}/usr ${MINGWW64COMMON}" -# exec_record "make -j$(nproc)" -# exec_record "make install-strip -j$(nproc)" +# exec_record_fast_fail "make -j$(nproc)" +# exec_record_fast_fail "make install-strip -j$(nproc)" #ln -sf "${install_prefix}/${MINGWTRIPLET}/lib" ./usr/lib configure_gnu_ex "${src_dir}/mingw-w64-libraries/winpthreads" "--prefix=${install_prefix} ${MINGWW64COMMON}" log_info "CC ==== $CC" exec_record_fast_fail "../configure --help" - exec_record "make -j$(nproc)" - exec_record "make install-strip -j$(nproc)" + exec_record_fast_fail "make -j$(nproc)" + exec_record_fast_fail "make install-strip -j$(nproc)" } diff --git a/linux_shell/build_script/lld.bash b/linux_shell/build_script/lld.bash index 47121d3..1a5e231 100644 --- a/linux_shell/build_script/lld.bash +++ b/linux_shell/build_script/lld.bash @@ -11,9 +11,9 @@ include "${BASH_SOURCE[0]}" ./patch/export.bash include "${BASH_SOURCE[0]}" ./libc/export.bash include "${BASH_SOURCE[0]}" ./base/export.bash LLVM_SRC_DIR="" -LLVM_BUILD_DIR="/ae/bash/0_cmake_build_dir/lld" -LLVM_INSTALL_DIR="/ae/bash/0_cmake_install_dir/lld" -LLVM_CMAKE="/ae/opt/cmake/bin/cmake" +LLVM_BUILD_DIR="${_root_}/0_cmake_build_dir/lld" +LLVM_INSTALL_DIR="${_root_}/0_cmake_install_dir/lld" +LLVM_CMAKE="${_ae_root_}/opt/cmake/bin/cmake" LLVM_NINJA="/usr/bin/ninja" LLVM_GENERATOR="Ninja" LLVM_LLVM_VERSION="llvmorg-22.1.8" @@ -249,17 +249,17 @@ llvm_build_base() { if [[ -n "${LLVM_TC_FILE}" ]]; then cmd="${cmd} \"-DCMAKE_TOOLCHAIN_FILE=${LLVM_TC_FILE}\"" fi - exec_record "${cmd}" + exec_record_fast_fail "${cmd}" build_cmd="\"${LLVM_CMAKE}\" --build \"${LLVM_BUILD_DIR}\" --parallel \"${jobs}\"" - exec_record "${build_cmd}" + exec_record_fast_fail "${build_cmd}" install_cmd="\"${LLVM_CMAKE}\" --install \"${LLVM_BUILD_DIR}\"" - exec_record "${install_cmd}" + exec_record_fast_fail "${install_cmd}" } build_ubuntu2004_gcc93_native_clang() { local suffix="_ubuntu2004_gcc93_native_clang" clear_llvm_platform_config - LLVM_BUILD_DIR="/ae/0_cmake_build_dir/llvm${suffix}" - LLVM_INSTALL_DIR="/ae/0_cmake_install_dir/llvm${suffix}" + LLVM_BUILD_DIR="${_ae_root_}/0_cmake_build_dir/llvm${suffix}" + LLVM_INSTALL_DIR="${_ae_root_}/0_cmake_install_dir/llvm${suffix}" LLVM_BUILD_TYPE="Release" LLVM_TC_C_COMPILER="/usr/bin/gcc" LLVM_TC_CXX_COMPILER="/usr/bin/g++" @@ -271,9 +271,9 @@ build_ubuntu2004_gcc93_native_clang() { build_windows_lld_linux_x86_by_stage1_clang() { local stage1_install_dir="$1" local suffix="_windows_lld_linux_x86" - local extract_root="/ae/0_sysroot/msys2" - local sysroot="/ae/0_sysroot/msys2/mingw64" - local pkg_root="/ae/bash/0_wget_cache_dir/msys2/mingw64" + local extract_root="${_ae_root_}/0_sysroot/msys2" + local sysroot="${_ae_root_}/0_sysroot/msys2/mingw64" + local pkg_root="${_wget_cache_dir_}/msys2/mingw64" local pkg_repo="mingw64" local mingw_target="x86_64-w64-windows-gnu" local linux_target="x86_64-linux-gnu" @@ -317,8 +317,8 @@ build_windows_lld_linux_x86_by_stage1_clang() { link_dirs="${gcc_dir};${sysroot}/lib;${sysroot}/x86_64-w64-mingw32/lib" cxx_include_dirs="${cxx_dir};${cxx_target_dir};${cxx_dir}/backward" clear_llvm_platform_config - LLVM_BUILD_DIR="/ae/0_cmake_build_dir/llvm${suffix}" - LLVM_INSTALL_DIR="/ae/0_cmake_install_dir/llvm${suffix}" + LLVM_BUILD_DIR="${_ae_root_}/0_cmake_build_dir/llvm${suffix}" + LLVM_INSTALL_DIR="${_ae_root_}/0_cmake_install_dir/llvm${suffix}" LLVM_BUILD_TYPE="Release" LLVM_TC_SYSTEM_NAME="Windows" LLVM_TC_SYSTEM_PROCESSOR="x86_64" @@ -355,7 +355,7 @@ build_windows_lld_linux_x86_by_stage1_clang() { } build_ubuntu2004_gcc93_clang_then_windows_lld_linux_x86() { local stage1_suffix="_ubuntu2004_gcc93_native_clang" - local stage1_install_dir="/ae/0_cmake_install_dir/llvm${stage1_suffix}" + local stage1_install_dir="${_ae_root_}/0_cmake_install_dir/llvm${stage1_suffix}" build_windows_lld_linux_x86_by_stage1_clang "${stage1_install_dir}" return 0 } diff --git a/linux_shell/build_script/llvm/cmake.bash b/linux_shell/build_script/llvm/cmake.bash index 7d9b5cf..f9a08a6 100644 --- a/linux_shell/build_script/llvm/cmake.bash +++ b/linux_shell/build_script/llvm/cmake.bash @@ -27,11 +27,11 @@ cmake_list_targets() { ensure_repo_version base_src_dir "${CBT_dep}" "${CBT_ver}" local src_dir="${base_src_dir}${CBT_relative_cmake_pos}" local name="${CBT_dep}_${CBT_ver}" - local build_dir="/ae/0_cmake_build_dir/${name}" + local build_dir="${_ae_root_}/0_cmake_build_dir/${name}" mkdir -p "${build_dir}" - local cmake="/ae/opt/cmake/bin/cmake" + local cmake="${_ae_root_}/opt/cmake/bin/cmake" local configure_cmd="${cmake} -S ${src_dir} -B ${build_dir}" local list_cmd="${cmake} --build ${build_dir} --target help" - exec_record "${configure_cmd}" - exec_record "${list_cmd}" + exec_record_fast_fail "${configure_cmd}" + exec_record_fast_fail "${list_cmd}" } diff --git a/linux_shell/build_script/main.bash b/linux_shell/build_script/main.bash index 2942692..170ddce 100644 --- a/linux_shell/build_script/main.bash +++ b/linux_shell/build_script/main.bash @@ -1,5 +1,3 @@ -#!/bin/bash - #!/bin/bash # shellcheck disable=SC2155 if ! type "include" >/dev/null 2>&1; then @@ -27,6 +25,7 @@ include "${BASH_SOURCE[0]}" ./llvm/export.bash include "${BASH_SOURCE[0]}" ./cmake.bash include "${BASH_SOURCE[0]}" ./lld.bash include "${BASH_SOURCE[0]}" ./install_llvm.bash +init_build_environment # shellcheck source=./cmake.bash # shellcheck source=./lld.bash # shellcheck source=./install_llvm.bash @@ -73,7 +72,6 @@ function safe_llvm_strip { done } -echo "工作目录: $(pwd)" build_kylin_ver() { local ver=${1} @@ -85,7 +83,7 @@ build_kylin_ver() { # 编译交叉编译工具 attach_env_local_cross "${ver}" # 解压python库 - local pkg_root="/ae/pkg_mingw-w64-x86_64" + local pkg_root="${_ae_root_}/pkg_mingw-w64-x86_64" mkdir -p "${toolchain_root}" # --debug-trycompile --debug-output -DCMAKE_TRY_COMPILE_NO_CLEANUP @@ -132,15 +130,15 @@ build_kylin_ver() { build_arm_kylin() { name="win_arm_kylin_414_1224" local target="aarch64-linux-gnu" - local sysroot="/ae/sysroots/arm_kylin" + local sysroot="${_ae_root_}/sysroots/arm_kylin" init_env "${name}" "${sysroot}" "${target}" gev_cur_toolchain_name="arm_kylin" local root="${gev_target_sysroot}" check_and_create_dir_var root - patch_system_qt5 "${root}" "${root}/usr/lib/aarch64-linux-gnu/cmake" "/ae/sysroots/win_qt_host_tool/5.14.2/msvc2017_64/qt5/bin" + patch_system_qt5 "${root}" "${root}/usr/lib/aarch64-linux-gnu/cmake" "${_ae_root_}/sysroots/win_qt_host_tool/5.14.2/msvc2017_64/qt5/bin" build_kylin_ver "releases/gcc-9.3.0" "9.3.0" #build_kylin_ver "releases/gcc-15.2.0" "15.2.0" - compress_directory_tar_gz "/ae/env/${name}" "/ae/env" "${name}.tar.gz" + compress_directory_tar_gz "${_ae_root_}/env/${name}" "${_ae_root_}/env" "${name}.tar.gz" } #build_arm_kylin @@ -148,12 +146,12 @@ build_arm_kylin() { build_local_gcc() { name="local_ubuntu2004_15_10_527_1412" local target="x86_64-linux-gnu" - local sysroot="/ae/sysroots/ubuntu2004" + local sysroot="${_ae_root_}/sysroots/ubuntu2004" local ver="releases/gcc-15.1.0" gev_cur_toolchain_name="local_gcc" init_env "${name}" "${sysroot}" "${target}" attach_env_local_cross "${ver}" - compress_directory_tar_gz "/ae/env/${name}" "/ae/env" "${name}.tar.gz" + compress_directory_tar_gz "${_ae_root_}/env/${name}" "${_ae_root_}/env" "${name}.tar.gz" } build_ubuntu_2004() { @@ -164,7 +162,7 @@ build_ubuntu_2004() { name="${name}_${inter_ver}" local target="x86_64-linux-gnu" - local sysroot="/ae/sysroots/debootstrap_ubuntu_2004" + local sysroot="${_ae_root_}/sysroots/debootstrap_ubuntu_2004" local ver="releases/gcc-${inter_ver}" local ver2="${inter_ver}" init_env "${name}" "${sysroot}" "${target}" @@ -181,7 +179,7 @@ build_ubuntu_2004() { load_toolchain "${local_gcc_toolchain_dir}" "${target}" attach_env_canadian_stage3 "${host}" "${ver}" - local pkg_root="/ae/pkg_mingw-w64-x86_64" + local pkg_root="${_ae_root_}/pkg_mingw-w64-x86_64" local zlib_pkg download_msys2_pkg zlib_pkg "mingw64" "mingw-w64-x86_64-zlib-1.3.2-2-any.pkg.tar.zst" "${pkg_root}" @@ -200,7 +198,7 @@ build_ubuntu_2004() { local target_bin="${toolchain_root}/${target}/bin" check_directory_var target_bin cp "${toolchain_root}/bin/"*.dll "${target_bin}" - compress_directory_tar_gz "/ae/env/${name}" "/ae/env" "${name}.tar.gz" + compress_directory_tar_gz "${_ae_root_}/env/${name}" "${_ae_root_}/env" "${name}.tar.gz" } build_ubuntu2004_gcc93_clang_then_windows_lld_linux_x86 diff --git a/linux_shell/build_script/pragma_once.bash b/linux_shell/build_script/pragma_once.bash index 9b37df8..07b2420 100644 --- a/linux_shell/build_script/pragma_once.bash +++ b/linux_shell/build_script/pragma_once.bash @@ -1,44 +1,18 @@ #!/bin/bash -# shellcheck disable=SC2155 - - -if declare -p _global_pragma_once_sign &>/dev/null; then - return 0; +if declare -p _global_pragma_once_sign >/dev/null 2>&1; then + return 0 fi - readonly _global_pragma_once_sign=true -declare loaded_scripts_map="" # 关联数组不知道为什么有bug - - - - +declare -gA _global_loaded_scripts=() pragma_once() { - # 1. 转换为绝对路径以确保唯一性 local abs_path - abs_path=$(realpath "$1" 2>/dev/null || echo "$1") - if [ ! -e "$abs_path" ]; then - echo "错误: 文件 '$1' 不存在。" - exit 1 - fi - - # 2. 生成 Key(只保留字母数字,其他转下划线) - local key="key_$(echo "$abs_path" | sed 's/[^a-zA-Z0-9]/_/g')" - - # 3. 精确匹配检查 - # 我们在 map 的两端和 key 的两端都加上分隔符 | - if [[ "$loaded_scripts_map" == *"|$key|"* ]]; then - return 1 # 已经加载过了 - else - # 第一次加载,存入时带上分隔符 - # 如果 map 为空,初始化为 |key|;如果不为空,追加 key| - if [[ -z "$loaded_scripts_map" ]]; then - loaded_scripts_map="|$key|" - else - loaded_scripts_map="${loaded_scripts_map}${key}|" - fi - return 0 # 允许加载 + abs_path="$(realpath "$1" 2>/dev/null)" || { + echo "错误: 文件 '$1' 不存在。" >&2 + return 2 + } + if [[ -n "${_global_loaded_scripts[$abs_path]+x}" ]]; then + return 1 fi + _global_loaded_scripts["$abs_path"]=1 + return 0 } -# 使用方式 -# if pragma_once "${BASH_SOURCE[0]}"; then -# fi diff --git a/linux_shell/build_sysroot/3rd_build_syroot/ubuntu2004/build_sysroot b/linux_shell/build_sysroot/3rd_build_syroot/ubuntu2004/build_sysroot index 2bedc9d..c8c4f1f 100644 --- a/linux_shell/build_sysroot/3rd_build_syroot/ubuntu2004/build_sysroot +++ b/linux_shell/build_sysroot/3rd_build_syroot/ubuntu2004/build_sysroot @@ -14,7 +14,7 @@ DEBOOTSTRAP_VARIANT="minbase" DEBOOTSTRAP_SUITE="focal" DEBOOTSTRAP_MIRROR="http://mirrors.aliyun.com/ubuntu" -SYSROOT="/ae/sysroots/debootstrap_ubuntu_2004" +SYSROOT="${PSC_AE_ROOT:-/ae}/sysroots/debootstrap_ubuntu_2004" diff --git a/linux_shell/build_sysroot/build_sysroot_from_iso/base_rootfs.bash b/linux_shell/build_sysroot/build_sysroot_from_iso/base_rootfs.bash index 74e588a..d2e0ed0 100644 --- a/linux_shell/build_sysroot/build_sysroot_from_iso/base_rootfs.bash +++ b/linux_shell/build_sysroot/build_sysroot_from_iso/base_rootfs.bash @@ -43,6 +43,39 @@ assert_var_name() { fi } +normalize_abs_path() { + local -n ret="$1" + local path="$2" + if [[ "$path" != /* ]]; then + die "path must be absolute: $path" + fi + ret="$(realpath -m -- "$path")" +} +assert_safe_destructive_path() { + local path="$1" + local label="${2:-path}" + local normalized + normalize_abs_path normalized "$path" + case "$normalized" in + /|/bin|/bin/*|/boot|/boot/*|/dev|/dev/*|/etc|/etc/*|/home|/home/*|/lib|/lib/*|/lib64|/lib64/*|/proc|/proc/*|/root|/root/*|/run|/run/*|/sbin|/sbin/*|/sys|/sys/*|/tmp|/usr|/usr/*|/var|/var/*) + die "unsafe $label: $normalized" + ;; + esac + if [[ "${normalized#/}" != */* ]]; then + die "unsafe $label: $normalized" + fi +} +assert_disjoint_paths() { + local first="$1" + local second="$2" + local first_abs + local second_abs + normalize_abs_path first_abs "$first" + normalize_abs_path second_abs "$second" + if [[ "$first_abs" == "$second_abs" || "$first_abs" == "$second_abs/"* || "$second_abs" == "$first_abs/"* ]]; then + die "paths must not overlap: $first_abs <-> $second_abs" + fi +} require_cmd() { local cmd="$1" diff --git a/linux_shell/build_sysroot/build_sysroot_from_iso/function_rootfs.bash b/linux_shell/build_sysroot/build_sysroot_from_iso/function_rootfs.bash index eda1bf2..217e023 100644 --- a/linux_shell/build_sysroot/build_sysroot_from_iso/function_rootfs.bash +++ b/linux_shell/build_sysroot/build_sysroot_from_iso/function_rootfs.bash @@ -38,6 +38,9 @@ init_rootfs_env() { local apt_sources="$5" assert_assoc_name "$ctx_name" + assert_safe_destructive_path "$iso_mount" "iso_mount" + assert_safe_destructive_path "$rootfs" "rootfs" + assert_disjoint_paths "$iso_mount" "$rootfs" eval "declare -g -A ${ctx_name}=()" local -n C="$ctx_name" @@ -263,6 +266,7 @@ step_persistent_init_from_iso() { fi log "remove old rootfs: ${C["rootfs"]}" + assert_safe_destructive_path "${C["rootfs"]}" "rootfs" sudo rm -rf "${C["rootfs"]}" log "create rootfs directory..." @@ -299,7 +303,7 @@ step_force_rebuild_from_iso() { log "force rebuild rootfs..." _rootfs_clear_chroot_mounts "$ctx_name" - + assert_safe_destructive_path "${C["rootfs"]}" "rootfs" sudo rm -rf "${C["rootfs"]}" sudo mkdir -p "${C["rootfs"]}" @@ -551,7 +555,8 @@ step_export_dev_sysroot() { local -n C="$ctx_name" local src_root="${C["rootfs"]}" - + assert_safe_destructive_path "$dst_root" "destination sysroot" + assert_disjoint_paths "$src_root" "$dst_root" _rootfs_clear_chroot_mounts "$ctx_name" log "export slim dev sysroot" diff --git a/linux_shell/build_sysroot/build_sysroot_from_iso/test_rootfs.bash b/linux_shell/build_sysroot/build_sysroot_from_iso/test_rootfs.bash index 2180639..722ade2 100644 --- a/linux_shell/build_sysroot/build_sysroot_from_iso/test_rootfs.bash +++ b/linux_shell/build_sysroot/build_sysroot_from_iso/test_rootfs.bash @@ -26,6 +26,7 @@ set -euo pipefail # ============================================================ source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/function_rootfs.bash" +AE_ROOT="${PSC_AE_ROOT:-/ae}" # ============================================================ # 1. 测试用固定配置 @@ -97,7 +98,7 @@ test_rootfs_init_only() { TEST_ROOTFS_CTX \ "/dev/sr0" \ "/mnt/ubuntu_iso" \ - "/ae/sysroots/ubuntu_2004_4" \ + "${AE_ROOT}/sysroots/ubuntu_2004_4" \ "$ROOTFS_APT_SOURCES_OFFICIAL" local rc=0 @@ -142,7 +143,7 @@ test_rootfs_install_dev_packages() { TEST_ROOTFS_CTX \ "/dev/sr0" \ "/mnt/ubuntu_iso" \ - "/ae/sysroots/ubuntu_2004_4" \ + "${AE_ROOT}/sysroots/ubuntu_2004_4" \ "$ROOTFS_APT_SOURCES_OFFICIAL" local rc=0 @@ -187,7 +188,7 @@ test_rootfs_export_dev_sysroot() { TEST_ROOTFS_CTX \ "/dev/sr0" \ "/mnt/ubuntu_iso" \ - "/ae/sysroots/ubuntu_2004_4" \ + "${AE_ROOT}/sysroots/ubuntu_2004_4" \ "$ROOTFS_APT_SOURCES_OFFICIAL" local rc=0 @@ -234,7 +235,7 @@ test_rootfs_full_basic_flow() { TEST_ROOTFS_CTX \ "/dev/sr0" \ "/mnt/ubuntu_iso" \ - "/ae/sysroots/ubuntu_2004_5" \ + "${AE_ROOT}/sysroots/ubuntu_2004_5" \ "$ROOTFS_APT_SOURCES_OFFICIAL" local rc=0