192 lines
4.8 KiB
Bash
192 lines
4.8 KiB
Bash
#!/bin/bash
|
|
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
|
|
_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_ts
|
|
local end_ts
|
|
local duration
|
|
local ret
|
|
start_ts="$(date +%s.%3N)"
|
|
log_info "[%s] exec: %s" "$(pwd)" "$cmd"
|
|
if eval "$cmd"; then
|
|
ret=0
|
|
else
|
|
ret=$?
|
|
fi
|
|
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"
|
|
if exec_record "$cmd"; then
|
|
return 0
|
|
else
|
|
local ret=$?
|
|
exit "$ret"
|
|
fi
|
|
}
|
|
exec_args_fast_fail() {
|
|
local ret
|
|
local cmd
|
|
printf -v cmd '%q ' "$@"
|
|
cmd="${cmd% }"
|
|
log_info "[%s] exec: %s" "$(pwd)" "$cmd"
|
|
if "$@"; then
|
|
return 0
|
|
else
|
|
ret=$?
|
|
fi
|
|
log_error "[%s] exec failed ret=%s: %s" "$(pwd)" "$ret" "$cmd"
|
|
exit "$ret"
|
|
}
|
|
append_rpath() {
|
|
local dir_path="$1"
|
|
local rpath_value="$2"
|
|
if [[ -z "$dir_path" || -z "$rpath_value" ]]; then
|
|
echo "Usage: append_rpath <directory> <rpath_value>" >&2
|
|
return 1
|
|
fi
|
|
if [[ ! -d "$dir_path" ]]; then
|
|
echo "Error: Directory '$dir_path' does not exist." >&2
|
|
return 1
|
|
fi
|
|
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"
|
|
fi
|
|
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" >&2
|
|
return 1
|
|
fi
|
|
done
|
|
}
|
|
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"
|
|
}
|
|
_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
|
|
printf -v "$var_name" '%s' "$value"
|
|
fi
|
|
export "$var_name"
|
|
}
|
|
load_toolchain_bin_only() {
|
|
local prefix="$1"
|
|
log_info "load_toolchain_bin_only path: %s" "$prefix"
|
|
_prepend_path_unique PATH "${prefix}/bin"
|
|
}
|
|
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
|