mirror of
https://github.com/levogevo/ffmpeg-builder.git
synced 2026-01-15 19:06:17 +00:00
tabs to spaces and make utils load first
This commit is contained in:
@@ -32,10 +32,12 @@ The default enabled libraries included in the `ffmpeg` build are:
|
||||
- libx264
|
||||
- libx265
|
||||
- libwebp
|
||||
- libvpx
|
||||
- libvorbis
|
||||
- libmp3lame
|
||||
|
||||
The user-overridable compile options are:
|
||||
- `ENABLE`: configure what ffmpeg enables (default: libsvtav1_psy libopus libdav1d libaom librav1e libvmaf libx264 libx265 libwebp libmp3lame)
|
||||
- `ENABLE`: configure what ffmpeg enables (default: libsvtav1_psy libopus libdav1d libaom librav1e libvmaf libx264 libx265 libwebp libvpx libvorbis libmp3lame)
|
||||
- `PREFIX`: prefix to install to, default is local install in ./gitignore/sysroot (default: local)
|
||||
- `STATIC`: static or shared build (default: ON)
|
||||
- `LTO`: enable link time optimization (default: ON)
|
||||
|
||||
414
lib/0-utils.sh
Normal file
414
lib/0-utils.sh
Normal file
@@ -0,0 +1,414 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# shellcheck disable=SC2034
|
||||
|
||||
# ANSI colors
|
||||
RED='\e[0;31m'
|
||||
CYAN='\e[0;36m'
|
||||
GREEN='\e[0;32m'
|
||||
YELLOW='\e[0;33m'
|
||||
NC='\e[0m'
|
||||
|
||||
# echo wrappers
|
||||
echo_wrapper() {
|
||||
local args
|
||||
if [[ $1 == '-n' ]]; then
|
||||
args=("$1")
|
||||
shift
|
||||
fi
|
||||
# COLOR is override for using ${color}
|
||||
# shellcheck disable=SC2153
|
||||
if [[ ${COLOR} == 'OFF' ]]; then
|
||||
color=''
|
||||
endColor=''
|
||||
else
|
||||
endColor="${NC}"
|
||||
fi
|
||||
|
||||
echo -e "${args[@]}" "${color}${word:-''}${endColor}" "$@"
|
||||
}
|
||||
echo_fail() { color="${RED}" word="FAIL" echo_wrapper "$@"; }
|
||||
echo_info() { color="${CYAN}" word="INFO" echo_wrapper "$@"; }
|
||||
echo_pass() { color="${GREEN}" word="PASS" echo_wrapper "$@"; }
|
||||
echo_warn() { color="${YELLOW}" word="WARN" echo_wrapper "$@"; }
|
||||
echo_exit() {
|
||||
echo_fail "$@"
|
||||
exit 1
|
||||
}
|
||||
void() { echo "$@" >/dev/null; }
|
||||
|
||||
echo_if_fail() {
|
||||
local cmd=("$@")
|
||||
local logName="${LOGNAME:-${RANDOM}}-"
|
||||
local out="${TMP_DIR}/${logName}stdout"
|
||||
local err="${TMP_DIR}/${logName}stderr"
|
||||
|
||||
# set trace to the cmdEvalTrace and open file descriptor
|
||||
local cmdEvalTrace="${TMP_DIR}/${logName}cmdEvalTrace"
|
||||
exec 5>"${cmdEvalTrace}"
|
||||
export BASH_XTRACEFD=5
|
||||
|
||||
set -x
|
||||
"${cmd[@]}" >"${out}" 2>"${err}"
|
||||
local retval=$?
|
||||
|
||||
# unset and close file descriptor
|
||||
set +x
|
||||
exec 5>&-
|
||||
|
||||
# parse out relevant part of the trace
|
||||
local cmdEvalLines=()
|
||||
while IFS= read -r line; do
|
||||
line="${line/${PS4}/}"
|
||||
test "${line}" == 'set +x' && continue
|
||||
test "${line}" == '' && continue
|
||||
cmdEvalLines+=("${line}")
|
||||
done <"${cmdEvalTrace}"
|
||||
|
||||
if ! test ${retval} -eq 0; then
|
||||
echo
|
||||
echo_fail "command failed with ${retval}:"
|
||||
printf "%s\n" "${cmdEvalLines[@]}"
|
||||
echo_warn "command stdout:"
|
||||
tail -n 32 "${out}"
|
||||
echo_warn "command stderr:"
|
||||
tail -n 32 "${err}"
|
||||
echo
|
||||
fi
|
||||
if [[ -z ${LOGNAME} ]]; then
|
||||
rm "${out}" "${err}" "${cmdEvalTrace}"
|
||||
fi
|
||||
return ${retval}
|
||||
}
|
||||
|
||||
is_root_owned() {
|
||||
local path=$1
|
||||
local uid
|
||||
|
||||
if stat --version >/dev/null 2>&1; then
|
||||
# GNU coreutils (Linux)
|
||||
uid=$(stat -c '%u' "$path")
|
||||
else
|
||||
# BSD/macOS
|
||||
uid=$(stat -f '%u' "$path")
|
||||
fi
|
||||
|
||||
test "$uid" -eq 0
|
||||
}
|
||||
|
||||
dump_arr() {
|
||||
local arrayNames=("$@")
|
||||
for arrayName in "${arrayNames[@]}"; do
|
||||
declare -n array="${arrayName}"
|
||||
arrayExpanded=("${array[@]}")
|
||||
|
||||
# skip showing single element arrays by default
|
||||
if [[ ! ${#arrayExpanded[@]} -gt 1 ]]; then
|
||||
if [[ ${SHOW_SINGLE} == true ]]; then
|
||||
echo_info "${arrayName}='${arrayExpanded[*]}'"
|
||||
else
|
||||
continue
|
||||
fi
|
||||
fi
|
||||
|
||||
echo
|
||||
# don't care that the variable has "ARR"
|
||||
echo_info "${arrayName//"_ARR"/}"
|
||||
printf "\t%s\n" "${arrayExpanded[@]}"
|
||||
done
|
||||
}
|
||||
|
||||
has_cmd() {
|
||||
local cmds=("$@")
|
||||
local rv=0
|
||||
for cmd in "${cmds[@]}"; do
|
||||
command -v "${cmd}" >/dev/null 2>&1 || rv=1
|
||||
done
|
||||
|
||||
return ${rv}
|
||||
}
|
||||
|
||||
missing_cmd() {
|
||||
local cmds=("$@")
|
||||
local rv=1
|
||||
for cmd in "${cmds[@]}"; do
|
||||
if ! has_cmd "${cmd}"; then
|
||||
echo_warn "missing ${cmd}"
|
||||
rv=0
|
||||
fi
|
||||
done
|
||||
|
||||
return ${rv}
|
||||
}
|
||||
|
||||
bash_dirname() {
|
||||
local tmp=${1:-.}
|
||||
|
||||
[[ $tmp != *[!/]* ]] && {
|
||||
printf '/\n'
|
||||
return
|
||||
}
|
||||
|
||||
tmp=${tmp%%"${tmp##*[!/]}"}
|
||||
|
||||
[[ $tmp != */* ]] && {
|
||||
printf '.\n'
|
||||
return
|
||||
}
|
||||
|
||||
tmp=${tmp%/*}
|
||||
tmp=${tmp%%"${tmp##*[!/]}"}
|
||||
|
||||
printf '%s\n' "${tmp:-/}"
|
||||
}
|
||||
|
||||
bash_basename() {
|
||||
local tmp
|
||||
path="$1"
|
||||
suffix="${2:-''}"
|
||||
|
||||
tmp=${path%"${path##*[!/]}"}
|
||||
tmp=${tmp##*/}
|
||||
tmp=${tmp%"${suffix/"$tmp"/}"}
|
||||
|
||||
printf '%s\n' "${tmp:-/}"
|
||||
}
|
||||
|
||||
bash_realpath() {
|
||||
local file=$1
|
||||
local dir
|
||||
|
||||
# If the file is already absolute
|
||||
[[ $file == /* ]] && {
|
||||
printf '%s\n' "$file"
|
||||
return
|
||||
}
|
||||
|
||||
# Otherwise: split into directory + basename
|
||||
dir="$(bash_dirname "${file}")"
|
||||
file="$(bash_basename "${file}")"
|
||||
|
||||
# If no directory component, use current directory
|
||||
if [[ $dir == "$file" ]]; then
|
||||
dir="$PWD"
|
||||
else
|
||||
# Save current dir, move into target dir, capture $PWD, then return
|
||||
local oldpwd="$PWD"
|
||||
cd "$dir" || return 1
|
||||
dir="$PWD"
|
||||
cd "$oldpwd" || return 1
|
||||
fi
|
||||
|
||||
printf '%s/%s\n' "$dir" "$file"
|
||||
}
|
||||
|
||||
line_contains() {
|
||||
local line="$1"
|
||||
local substr="$2"
|
||||
if [[ $line == *"${substr}"* ]]; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
line_starts_with() {
|
||||
local line="$1"
|
||||
local substr="$2"
|
||||
if [[ $line == "${substr}"* ]]; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
is_linux() {
|
||||
line_contains "${OSTYPE}" 'linux'
|
||||
}
|
||||
|
||||
is_darwin() {
|
||||
line_contains "$(print_os)" darwin
|
||||
}
|
||||
|
||||
is_windows() {
|
||||
line_contains "$(print_os)" windows
|
||||
}
|
||||
|
||||
is_android() {
|
||||
line_contains "$(print_os)" android
|
||||
}
|
||||
|
||||
print_os() {
|
||||
# cached response
|
||||
if [[ -n ${FB_OS} ]]; then
|
||||
echo "${FB_OS}"
|
||||
return 0
|
||||
fi
|
||||
|
||||
unset FB_OS
|
||||
if [[ -f /etc/os-release ]]; then
|
||||
source /etc/os-release
|
||||
FB_OS="${ID}"
|
||||
if [[ ${VERSION_ID} != '' ]]; then
|
||||
FB_OS+="-${VERSION_ID}"
|
||||
fi
|
||||
if line_starts_with "${FB_OS}" 'arch'; then
|
||||
FB_OS='archlinux'
|
||||
fi
|
||||
else
|
||||
FB_OS="$(uname -o)"
|
||||
fi
|
||||
|
||||
# lowercase
|
||||
FB_OS="${FB_OS,,}"
|
||||
|
||||
# special treatment for windows
|
||||
if line_contains "${FB_OS}" 'windows' || line_contains "${FB_OS}" 'msys'; then
|
||||
FB_OS='windows'
|
||||
fi
|
||||
|
||||
echo "${FB_OS}"
|
||||
}
|
||||
|
||||
is_positive_integer() {
|
||||
local input="$1"
|
||||
if [[ ${input} != ?(-)+([[:digit:]]) || ${input} -lt 0 ]]; then
|
||||
echo_fail "${input} is not a positive integer"
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
replace_line() {
|
||||
local file="$1"
|
||||
local search="$2"
|
||||
local newLine="$3"
|
||||
local newFile="${TMP_DIR}/$(bash_basename "${file}")"
|
||||
|
||||
test -f "${newFile}" && rm "${newFile}"
|
||||
while read -r line; do
|
||||
if line_contains "${line}" "${search}"; then
|
||||
echo -en "${newLine}" >>"${newFile}"
|
||||
continue
|
||||
fi
|
||||
echo "${line}" >>"${newFile}"
|
||||
done <"${file}"
|
||||
|
||||
cp "${newFile}" "${file}"
|
||||
}
|
||||
|
||||
remove_line() {
|
||||
local file="$1"
|
||||
local search="$2"
|
||||
replace_line "${file}" "${search}" ''
|
||||
}
|
||||
|
||||
bash_sort() {
|
||||
local arr=("$@")
|
||||
local n=${#arr[@]}
|
||||
local i j val1 val2
|
||||
|
||||
# Bubble sort, numeric comparison
|
||||
for ((i = 0; i < n; i++)); do
|
||||
for ((j = 0; j < n - i - 1; j++)); do
|
||||
read -r val1 _ <<<"${arr[j]}"
|
||||
read -r val2 _ <<<"${arr[j + 1]}"
|
||||
if (("${val1}" > "${val2}")); then
|
||||
local tmp=${arr[j]}
|
||||
arr[j]=${arr[j + 1]}
|
||||
arr[j + 1]=$tmp
|
||||
fi
|
||||
done
|
||||
done
|
||||
|
||||
printf '%s\n' "${arr[@]}"
|
||||
}
|
||||
|
||||
_start_spinner() {
|
||||
local spinChars=(
|
||||
"-"
|
||||
'\'
|
||||
"|"
|
||||
"/"
|
||||
)
|
||||
|
||||
sleep 1
|
||||
|
||||
while true; do
|
||||
for ((ind = 0; ind < "${#spinChars[@]}"; ind++)); do
|
||||
echo -ne "${spinChars[${ind}]}" '\b\b'
|
||||
sleep .25
|
||||
done
|
||||
done
|
||||
}
|
||||
|
||||
spinner() {
|
||||
local action="$1"
|
||||
local spinPidFile="${TMP_DIR}/.spinner-pid"
|
||||
case "${action}" in
|
||||
start)
|
||||
test -f "${spinPidFile}" && rm "${spinPidFile}"
|
||||
|
||||
# don't want to clutter logs if running headless
|
||||
test "${HEADLESS}" == '1' && return
|
||||
|
||||
_start_spinner &
|
||||
echo $! >"${spinPidFile}"
|
||||
;;
|
||||
stop)
|
||||
test -f "${spinPidFile}" && kill "$(<"${spinPidFile}")"
|
||||
echo -ne ' \n'
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
get_pkgconfig_version() {
|
||||
local pkg="$1"
|
||||
pkg-config --modversion "${pkg}"
|
||||
}
|
||||
|
||||
using_cmake_4() {
|
||||
local cmakeVersion
|
||||
IFS=$' \t' read -r _ _ cmakeVersion <<<"$(command cmake --version)"
|
||||
line_starts_with "${cmakeVersion}" 4
|
||||
}
|
||||
|
||||
recreate_dir() {
|
||||
local dirs=("$@")
|
||||
for dir in "${dirs[@]}"; do
|
||||
test -d "${dir}" && rm -rf "${dir}"
|
||||
mkdir -p "${dir}" || return 1
|
||||
done
|
||||
}
|
||||
|
||||
ensure_dir() {
|
||||
local dirs=("$@")
|
||||
for dir in "${dirs[@]}"; do
|
||||
test -d "${dir}" || mkdir -p "${dir}" || return 1
|
||||
done
|
||||
}
|
||||
|
||||
get_remote_head() {
|
||||
local url="$1"
|
||||
local remoteHEAD=''
|
||||
IFS=$' \t' read -r remoteHEAD _ <<< \
|
||||
"$(git ls-remote "${url}" HEAD)"
|
||||
echo "${remoteHEAD}"
|
||||
}
|
||||
|
||||
fb_max() {
|
||||
local a="$1"
|
||||
local b="$2"
|
||||
test "${a}" -gt "${b}" &&
|
||||
echo "${a}" ||
|
||||
echo "${b}"
|
||||
}
|
||||
|
||||
print_padded() {
|
||||
local str="$1"
|
||||
local padding="$2"
|
||||
echo -n "${str}"
|
||||
for ((i = 0; i < padding - ${#str}; i++)); do
|
||||
echo -n ' '
|
||||
done
|
||||
}
|
||||
45
lib/build.sh
45
lib/build.sh
@@ -323,24 +323,21 @@ fi' >"${compilerDir}/which"
|
||||
echo
|
||||
}
|
||||
|
||||
get_remote_head() {
|
||||
local url="$1"
|
||||
local remoteHEAD=''
|
||||
IFS=$' \t' read -r remoteHEAD _ <<< \
|
||||
"$(git ls-remote "${url}" HEAD)"
|
||||
echo "${remoteHEAD}"
|
||||
}
|
||||
|
||||
get_build_conf() {
|
||||
local getBuild="${1}"
|
||||
|
||||
local longestBuild=0
|
||||
local longestVer=0
|
||||
local longestExt=0
|
||||
local padding=4
|
||||
|
||||
# name version file-extension url dep1,dep2
|
||||
# shellcheck disable=SC2016
|
||||
local BUILDS_CONF='
|
||||
ffmpeg 8.0 tar.gz https://github.com/FFmpeg/FFmpeg/archive/refs/tags/n${ver}.${ext}
|
||||
ffmpeg 8.0.1 tar.gz https://github.com/FFmpeg/FFmpeg/archive/refs/tags/n${ver}.${ext}
|
||||
|
||||
libsvtav1_psy 3.0.2-B tar.gz https://github.com/BlueSwordM/svt-av1-psyex/archive/refs/tags/v${ver}.${ext} dovi_tool,hdr10plus_tool,cpuinfo
|
||||
hdr10plus_tool 1.7.1 tar.gz https://github.com/quietvoid/hdr10plus_tool/archive/refs/tags/${ver}.${ext}
|
||||
hdr10plus_tool 1.7.2 tar.gz https://github.com/quietvoid/hdr10plus_tool/archive/refs/tags/${ver}.${ext}
|
||||
dovi_tool 2.3.1 tar.gz https://github.com/quietvoid/dovi_tool/archive/refs/tags/${ver}.${ext}
|
||||
cpuinfo latest git https://github.com/pytorch/cpuinfo/
|
||||
|
||||
@@ -348,8 +345,8 @@ libsvtav1 3.1.2 tar.gz https://gitlab.com/AOMediaCodec/SVT-AV1/-/archive/v${v
|
||||
librav1e 0.8.1 tar.gz https://github.com/xiph/rav1e/archive/refs/tags/v${ver}.${ext}
|
||||
libaom 3.13.1 tar.gz https://storage.googleapis.com/aom-releases/libaom-${ver}.${ext}
|
||||
libvmaf 3.0.0 tar.gz https://github.com/Netflix/vmaf/archive/refs/tags/v${ver}.${ext}
|
||||
libopus 1.5.2 tar.gz https://github.com/xiph/opus/releases/download/v${ver}/opus-${ver}.${ext}
|
||||
libdav1d 1.5.1 tar.xz http://downloads.videolan.org/videolan/dav1d/${ver}/dav1d-${ver}.${ext}
|
||||
libopus 1.6 tar.gz https://github.com/xiph/opus/archive/refs/tags/v${ver}.${ext}
|
||||
libdav1d 1.5.3 tar.xz https://downloads.videolan.org/videolan/dav1d/${ver}/dav1d-${ver}.${ext}
|
||||
libx264 latest git https://code.videolan.org/videolan/x264.git
|
||||
libmp3lame 3.100 tar.gz https://pilotfiber.dl.sourceforge.net/project/lame/lame/${ver}/lame-${ver}.${ext}
|
||||
libvpx 1.15.2 tar.gz https://github.com/webmproject/libvpx/archive/refs/tags/v${ver}.${ext}
|
||||
@@ -359,7 +356,7 @@ libogg 1.3.6 tar.xz https://github.com/xiph/ogg/releases/download/v${ver}/li
|
||||
|
||||
libwebp 1.6.0 tar.gz https://github.com/webmproject/libwebp/archive/refs/tags/v${ver}.${ext} libpng,libjpeg
|
||||
libjpeg 3.0.3 tar.gz https://github.com/winlibs/libjpeg/archive/refs/tags/libjpeg-turbo-${ver}.${ext}
|
||||
libpng 1.6.50 tar.gz https://github.com/pnggroup/libpng/archive/refs/tags/v${ver}.${ext} zlib
|
||||
libpng 1.6.53 tar.gz https://github.com/pnggroup/libpng/archive/refs/tags/v${ver}.${ext} zlib
|
||||
zlib 1.3.1 tar.gz https://github.com/madler/zlib/archive/refs/tags/v${ver}.${ext}
|
||||
|
||||
libplacebo 7.351.0 tar.gz https://github.com/haasn/libplacebo/archive/refs/tags/v${ver}.${ext} glslang,vulkan_loader,glad
|
||||
@@ -378,6 +375,12 @@ libnuma 2.0.19 tar.gz https://github.com/numactl/numactl/archive/refs/tags/v
|
||||
test "${line}" == '' && continue
|
||||
IFS=$' \t' read -r build ver ext url deps <<<"${line}"
|
||||
supported_builds+=("${build}")
|
||||
|
||||
# padding support
|
||||
longestBuild="$(fb_max "${#build}" "${longestBuild}")"
|
||||
longestVer="$(fb_max "${#ver}" "${longestVer}")"
|
||||
longestExt="$(fb_max "${#ext}" "${longestExt}")"
|
||||
|
||||
if [[ ${getBuild} != "${build}" ]]; then
|
||||
build=''
|
||||
continue
|
||||
@@ -385,11 +388,27 @@ libnuma 2.0.19 tar.gz https://github.com/numactl/numactl/archive/refs/tags/v
|
||||
break
|
||||
done <<<"${BUILDS_CONF}"
|
||||
|
||||
# special arg to print supported builds only
|
||||
if [[ ${getBuild} == 'supported' ]]; then
|
||||
echo "${supported_builds[@]}"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# special arg to print BUILDS_CONF but formatted with spaces
|
||||
if [[ ${getBuild} == 'format-builds-conf' ]]; then
|
||||
echo "local BUILDS_CONF='"
|
||||
while read -r line; do
|
||||
IFS=$' \t' read -r build ver ext url deps <<<"${line}"
|
||||
print_padded "${build}" $((padding + longestBuild))
|
||||
print_padded "${ver}" $((padding + longestVer))
|
||||
print_padded "${ext}" $((padding + longestExt))
|
||||
print_padded "${url}" "${padding}"
|
||||
echo " ${deps}"
|
||||
done <<<"${BUILDS_CONF}"
|
||||
echo "'"
|
||||
return 0
|
||||
fi
|
||||
|
||||
if [[ ${build} == '' ]]; then
|
||||
echo_fail "build ${getBuild} is not supported"
|
||||
return 1
|
||||
|
||||
@@ -182,7 +182,7 @@ efg_encode() {
|
||||
done
|
||||
done
|
||||
|
||||
less "${GRAIN_LOG}"
|
||||
echo "$(<"${GRAIN_LOG}")"
|
||||
}
|
||||
|
||||
efg_plot() {
|
||||
|
||||
389
lib/utils.sh
389
lib/utils.sh
@@ -1,389 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# shellcheck disable=SC2034
|
||||
|
||||
# ANSI colors
|
||||
RED='\e[0;31m'
|
||||
CYAN='\e[0;36m'
|
||||
GREEN='\e[0;32m'
|
||||
YELLOW='\e[0;33m'
|
||||
NC='\e[0m'
|
||||
|
||||
# echo wrappers
|
||||
echo_wrapper() {
|
||||
local args
|
||||
if [[ $1 == '-n' ]]; then
|
||||
args=("$1")
|
||||
shift
|
||||
fi
|
||||
# COLOR is override for using ${color}
|
||||
# shellcheck disable=SC2153
|
||||
if [[ ${COLOR} == 'OFF' ]]; then
|
||||
color=''
|
||||
endColor=''
|
||||
else
|
||||
endColor="${NC}"
|
||||
fi
|
||||
|
||||
echo -e "${args[@]}" "${color}${word:-''}${endColor}" "$@"
|
||||
}
|
||||
echo_fail() { color="${RED}" word="FAIL" echo_wrapper "$@"; }
|
||||
echo_info() { color="${CYAN}" word="INFO" echo_wrapper "$@"; }
|
||||
echo_pass() { color="${GREEN}" word="PASS" echo_wrapper "$@"; }
|
||||
echo_warn() { color="${YELLOW}" word="WARN" echo_wrapper "$@"; }
|
||||
echo_exit() {
|
||||
echo_fail "$@"
|
||||
exit 1
|
||||
}
|
||||
void() { echo "$@" >/dev/null; }
|
||||
|
||||
echo_if_fail() {
|
||||
local cmd=("$@")
|
||||
local logName="${LOGNAME:-${RANDOM}}-"
|
||||
local out="${TMP_DIR}/${logName}stdout"
|
||||
local err="${TMP_DIR}/${logName}stderr"
|
||||
|
||||
# set trace to the cmdEvalTrace and open file descriptor
|
||||
local cmdEvalTrace="${TMP_DIR}/${logName}cmdEvalTrace"
|
||||
exec 5>"${cmdEvalTrace}"
|
||||
export BASH_XTRACEFD=5
|
||||
|
||||
set -x
|
||||
"${cmd[@]}" >"${out}" 2>"${err}"
|
||||
local retval=$?
|
||||
|
||||
# unset and close file descriptor
|
||||
set +x
|
||||
exec 5>&-
|
||||
|
||||
# parse out relevant part of the trace
|
||||
local cmdEvalLines=()
|
||||
while IFS= read -r line; do
|
||||
line="${line/${PS4}/}"
|
||||
test "${line}" == 'set +x' && continue
|
||||
test "${line}" == '' && continue
|
||||
cmdEvalLines+=("${line}")
|
||||
done <"${cmdEvalTrace}"
|
||||
|
||||
if ! test ${retval} -eq 0; then
|
||||
echo
|
||||
echo_fail "command failed with ${retval}:"
|
||||
printf "%s\n" "${cmdEvalLines[@]}"
|
||||
echo_warn "command stdout:"
|
||||
tail -n 32 "${out}"
|
||||
echo_warn "command stderr:"
|
||||
tail -n 32 "${err}"
|
||||
echo
|
||||
fi
|
||||
if [[ -z ${LOGNAME} ]]; then
|
||||
rm "${out}" "${err}" "${cmdEvalTrace}"
|
||||
fi
|
||||
return ${retval}
|
||||
}
|
||||
|
||||
is_root_owned() {
|
||||
local path=$1
|
||||
local uid
|
||||
|
||||
if stat --version >/dev/null 2>&1; then
|
||||
# GNU coreutils (Linux)
|
||||
uid=$(stat -c '%u' "$path")
|
||||
else
|
||||
# BSD/macOS
|
||||
uid=$(stat -f '%u' "$path")
|
||||
fi
|
||||
|
||||
test "$uid" -eq 0
|
||||
}
|
||||
|
||||
dump_arr() {
|
||||
local arrayNames=("$@")
|
||||
for arrayName in "${arrayNames[@]}"; do
|
||||
declare -n array="${arrayName}"
|
||||
arrayExpanded=("${array[@]}")
|
||||
|
||||
# skip showing single element arrays by default
|
||||
if [[ ! ${#arrayExpanded[@]} -gt 1 ]]; then
|
||||
if [[ ${SHOW_SINGLE} == true ]]; then
|
||||
echo_info "${arrayName}='${arrayExpanded[*]}'"
|
||||
else
|
||||
continue
|
||||
fi
|
||||
fi
|
||||
|
||||
echo
|
||||
# don't care that the variable has "ARR"
|
||||
echo_info "${arrayName//"_ARR"/}"
|
||||
printf "\t%s\n" "${arrayExpanded[@]}"
|
||||
done
|
||||
}
|
||||
|
||||
has_cmd() {
|
||||
local cmds=("$@")
|
||||
local rv=0
|
||||
for cmd in "${cmds[@]}"; do
|
||||
command -v "${cmd}" >/dev/null 2>&1 || rv=1
|
||||
done
|
||||
|
||||
return ${rv}
|
||||
}
|
||||
|
||||
missing_cmd() {
|
||||
local cmds=("$@")
|
||||
local rv=1
|
||||
for cmd in "${cmds[@]}"; do
|
||||
if ! has_cmd "${cmd}"; then
|
||||
echo_warn "missing ${cmd}"
|
||||
rv=0
|
||||
fi
|
||||
done
|
||||
|
||||
return ${rv}
|
||||
}
|
||||
|
||||
bash_dirname() {
|
||||
local tmp=${1:-.}
|
||||
|
||||
[[ $tmp != *[!/]* ]] && {
|
||||
printf '/\n'
|
||||
return
|
||||
}
|
||||
|
||||
tmp=${tmp%%"${tmp##*[!/]}"}
|
||||
|
||||
[[ $tmp != */* ]] && {
|
||||
printf '.\n'
|
||||
return
|
||||
}
|
||||
|
||||
tmp=${tmp%/*}
|
||||
tmp=${tmp%%"${tmp##*[!/]}"}
|
||||
|
||||
printf '%s\n' "${tmp:-/}"
|
||||
}
|
||||
|
||||
bash_basename() {
|
||||
local tmp
|
||||
path="$1"
|
||||
suffix="${2:-''}"
|
||||
|
||||
tmp=${path%"${path##*[!/]}"}
|
||||
tmp=${tmp##*/}
|
||||
tmp=${tmp%"${suffix/"$tmp"/}"}
|
||||
|
||||
printf '%s\n' "${tmp:-/}"
|
||||
}
|
||||
|
||||
bash_realpath() {
|
||||
local file=$1
|
||||
local dir
|
||||
|
||||
# If the file is already absolute
|
||||
[[ $file == /* ]] && {
|
||||
printf '%s\n' "$file"
|
||||
return
|
||||
}
|
||||
|
||||
# Otherwise: split into directory + basename
|
||||
dir="$(bash_dirname "${file}")"
|
||||
file="$(bash_basename "${file}")"
|
||||
|
||||
# If no directory component, use current directory
|
||||
if [[ $dir == "$file" ]]; then
|
||||
dir="$PWD"
|
||||
else
|
||||
# Save current dir, move into target dir, capture $PWD, then return
|
||||
local oldpwd="$PWD"
|
||||
cd "$dir" || return 1
|
||||
dir="$PWD"
|
||||
cd "$oldpwd" || return 1
|
||||
fi
|
||||
|
||||
printf '%s/%s\n' "$dir" "$file"
|
||||
}
|
||||
|
||||
line_contains() {
|
||||
local line="$1"
|
||||
local substr="$2"
|
||||
if [[ $line == *"${substr}"* ]]; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
line_starts_with() {
|
||||
local line="$1"
|
||||
local substr="$2"
|
||||
if [[ $line == "${substr}"* ]]; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
is_linux() {
|
||||
line_contains "${OSTYPE}" 'linux'
|
||||
}
|
||||
|
||||
is_darwin() {
|
||||
line_contains "$(print_os)" darwin
|
||||
}
|
||||
|
||||
is_windows() {
|
||||
line_contains "$(print_os)" windows
|
||||
}
|
||||
|
||||
is_android() {
|
||||
line_contains "$(print_os)" android
|
||||
}
|
||||
|
||||
print_os() {
|
||||
# cached response
|
||||
if [[ -n ${FB_OS} ]]; then
|
||||
echo "${FB_OS}"
|
||||
return 0
|
||||
fi
|
||||
|
||||
unset FB_OS
|
||||
if [[ -f /etc/os-release ]]; then
|
||||
source /etc/os-release
|
||||
FB_OS="${ID}"
|
||||
if [[ ${VERSION_ID} != '' ]]; then
|
||||
FB_OS+="-${VERSION_ID}"
|
||||
fi
|
||||
if line_starts_with "${FB_OS}" 'arch'; then
|
||||
FB_OS='archlinux'
|
||||
fi
|
||||
else
|
||||
FB_OS="$(uname -o)"
|
||||
fi
|
||||
|
||||
# lowercase
|
||||
FB_OS="${FB_OS,,}"
|
||||
|
||||
# special treatment for windows
|
||||
if line_contains "${FB_OS}" 'windows' || line_contains "${FB_OS}" 'msys'; then
|
||||
FB_OS='windows'
|
||||
fi
|
||||
|
||||
echo "${FB_OS}"
|
||||
}
|
||||
|
||||
is_positive_integer() {
|
||||
local input="$1"
|
||||
if [[ ${input} != ?(-)+([[:digit:]]) || ${input} -lt 0 ]]; then
|
||||
echo_fail "${input} is not a positive integer"
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
replace_line() {
|
||||
local file="$1"
|
||||
local search="$2"
|
||||
local newLine="$3"
|
||||
local newFile="${TMP_DIR}/$(bash_basename "${file}")"
|
||||
|
||||
test -f "${newFile}" && rm "${newFile}"
|
||||
while read -r line; do
|
||||
if line_contains "${line}" "${search}"; then
|
||||
echo -en "${newLine}" >>"${newFile}"
|
||||
continue
|
||||
fi
|
||||
echo "${line}" >>"${newFile}"
|
||||
done <"${file}"
|
||||
|
||||
cp "${newFile}" "${file}"
|
||||
}
|
||||
|
||||
remove_line() {
|
||||
local file="$1"
|
||||
local search="$2"
|
||||
replace_line "${file}" "${search}" ''
|
||||
}
|
||||
|
||||
bash_sort() {
|
||||
local arr=("$@")
|
||||
local n=${#arr[@]}
|
||||
local i j val1 val2
|
||||
|
||||
# Bubble sort, numeric comparison
|
||||
for ((i = 0; i < n; i++)); do
|
||||
for ((j = 0; j < n - i - 1; j++)); do
|
||||
read -r val1 _ <<<"${arr[j]}"
|
||||
read -r val2 _ <<<"${arr[j + 1]}"
|
||||
if (("${val1}" > "${val2}")); then
|
||||
local tmp=${arr[j]}
|
||||
arr[j]=${arr[j + 1]}
|
||||
arr[j + 1]=$tmp
|
||||
fi
|
||||
done
|
||||
done
|
||||
|
||||
printf '%s\n' "${arr[@]}"
|
||||
}
|
||||
|
||||
_start_spinner() {
|
||||
local spinChars=(
|
||||
"-"
|
||||
'\'
|
||||
"|"
|
||||
"/"
|
||||
)
|
||||
|
||||
sleep 1
|
||||
|
||||
while true; do
|
||||
for ((ind = 0; ind < "${#spinChars[@]}"; ind++)); do
|
||||
echo -ne "${spinChars[${ind}]}" '\b\b'
|
||||
sleep .25
|
||||
done
|
||||
done
|
||||
}
|
||||
|
||||
spinner() {
|
||||
local action="$1"
|
||||
local spinPidFile="${TMP_DIR}/.spinner-pid"
|
||||
case "${action}" in
|
||||
start)
|
||||
test -f "${spinPidFile}" && rm "${spinPidFile}"
|
||||
|
||||
# don't want to clutter logs if running headless
|
||||
test "${HEADLESS}" == '1' && return
|
||||
|
||||
_start_spinner &
|
||||
echo $! >"${spinPidFile}"
|
||||
;;
|
||||
stop)
|
||||
test -f "${spinPidFile}" && kill "$(<"${spinPidFile}")"
|
||||
echo -ne ' \n'
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
get_pkgconfig_version() {
|
||||
local pkg="$1"
|
||||
pkg-config --modversion "${pkg}"
|
||||
}
|
||||
|
||||
using_cmake_4() {
|
||||
local cmakeVersion
|
||||
IFS=$' \t' read -r _ _ cmakeVersion <<<"$(command cmake --version)"
|
||||
line_starts_with "${cmakeVersion}" 4
|
||||
}
|
||||
|
||||
recreate_dir() {
|
||||
local dirs=("$@")
|
||||
for dir in "${dirs[@]}"; do
|
||||
test -d "${dir}" && rm -rf "${dir}"
|
||||
mkdir -p "${dir}" || return 1
|
||||
done
|
||||
}
|
||||
|
||||
ensure_dir() {
|
||||
local dirs=("$@")
|
||||
for dir in "${dirs[@]}"; do
|
||||
test -d "${dir}" || mkdir -p "${dir}" || return 1
|
||||
done
|
||||
}
|
||||
@@ -6,10 +6,10 @@ inotifywait -m -r \
|
||||
-e close_write \
|
||||
-e moved_to \
|
||||
--format '%w%f' \
|
||||
"$base/lib" \
|
||||
"$base/scripts" \
|
||||
"$base/main.sh" | while read -r file; do
|
||||
"${base}/lib" \
|
||||
"${base}/scripts" \
|
||||
"${base}/main.sh" | while read -r file; do
|
||||
if [[ -f $file && $file =~ .sh ]]; then
|
||||
shfmt --write --simplify "$file"
|
||||
shfmt --indent 4 --write --simplify "${file}"
|
||||
fi
|
||||
done
|
||||
|
||||
Reference in New Issue
Block a user