#!/bin/bash # shellcheck disable=SC2155 if ! type "include" > /dev/null 2>&1; then include() { . "$(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 # shellcheck source=./global_rely/export.bash # shellcheck source=./common.bash # shellcheck source=./compress.bash include "${BASH_SOURCE[0]}" ./common.bash include "${BASH_SOURCE[0]}" ./compress.bash download_cached_as() { local url="$1" local dest_dir="$2" local filename="$3" local cache_filepath="$4" # 参数检查 if [[ -z "$url" || -z "$dest_dir" || -z "$filename" || -z "$cache_filepath" ]]; then echo "[ERROR] 用法: download_cached_as " >&2 return 1 fi # 检查全局缓存目录变量 if [[ -z "${_wget_cache_dir_}" ]]; then echo "[ERROR] 全局缓存目录变量 _wget_cache_dir_ 未设置" >&2 return 1 fi # 创建目标目录 mkdir -p "$dest_dir" || { echo "[ERROR] 创建目标目录失败: $dest_dir" >&2 return 1 } # 创建缓存目录 mkdir -p "$_wget_cache_dir_" || { echo "[ERROR] 创建缓存目录失败: $_wget_cache_dir_" >&2 return 1 } # 目标文件路径 local dest_file="${dest_dir%/}/$filename" # 缓存文件路径 local cache_file="${_wget_cache_dir_%/}/$cache_filepath" local cache_dir="$(dirname "$cache_file")" check_and_create_dir "$cache_dir" # 如果目标文件已经存在,直接跳过 if [[ -f "$dest_file" ]]; then echo "[CACHE] 目标文件已存在,跳过: $dest_file" return 0 fi # 如果缓存文件不存在,则先下载到缓存目录 if [[ ! -f "$cache_file" ]]; then echo "[DOWNLOAD] $url" echo "[CACHE TO] $cache_file" local tmp_cache_file="${cache_file}.tmp.$$" wget -O "$tmp_cache_file" "$url" local rc=$? if [[ $rc -ne 0 ]]; then echo "[ERROR] 下载失败: $url" >&2 rm -f "$tmp_cache_file" return $rc fi mv -f "$tmp_cache_file" "$cache_file" || { echo "[ERROR] 缓存文件写入失败: $cache_file" >&2 rm -f "$tmp_cache_file" return 1 } echo "[OK] 已缓存: $cache_file" else echo "[CACHE] 命中缓存: $cache_file" fi # 从缓存复制到目标位置 cp -f "$cache_file" "$dest_file" || { echo "[ERROR] 从缓存复制到目标失败: $cache_file -> $dest_file" >&2 return 1 } echo "[OK] 已复制到目标: $dest_file" return 0 } 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 suffix local file_name local name local zip_file_path get_suffix suffix "$url" file_name="$(basename "$url")" name="$(basename "$url" ".$suffix")" zip_file_path="${_wget_cache_dir_}/$file_name" ret="$dest_dir/$name" mkdir -p "$_wget_cache_dir_" "$dest_dir" || return $? if [[ -d "$ret" ]]; then rm -rf -- "$ret" || return $? fi 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 fi exec_record_fast_fail "extract \"$suffix\" \"$zip_file_path\" \"$dest_dir\"" } configure_gnu_ex() { if [[ "$#" -lt 1 ]]; then echo "用法: configure_gnu_ex \"\"" >&2 return 1 fi local src_dir="$1" local opts="${2:-}" local build_dir="$src_dir/build" local print_opts 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}" } configure_gnu() { if [[ "$#" -lt 1 ]]; then echo "用法: configure_gnu [additional configure options...]" >&2 return 1 fi local src_dir="$1" shift local opts_string="$*" configure_gnu_ex "$src_dir" "$opts_string" } configure_gnu_zip_ex() { if [[ "$#" -lt 1 ]]; then echo "用法: configure_gnu_zip_ex \"\"" >&2 return 1 fi local url="$1" local opts="${2:-}" local src_dir="" 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...]" >&2 return 1 fi local url="$1" shift local opts_string="$*" configure_gnu_zip_ex "$url" "$opts_string" } 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" || 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...]" >&2 return 1 fi local url="$1" shift local opts_string="$*" build_gnu_zip_ex "$url" "$opts_string" } return 0