From 0cd594d1a0b6a9154ac9dda47b3255976abd7eb1 Mon Sep 17 00:00:00 2001 From: Michael Adams Date: Sun, 4 Feb 2024 12:33:17 -0800 Subject: [PATCH] Moved some of the common code in Bash scripts into a single file that is shared between scripts. Replaced a number of different approaches to path canonicalization in various scripts with a single uniform approach. Removed the now obsolete jas_realpath script. --- bin/base_utilities | 92 ++++++++++++++++++++++++++ build/base_utilities | 1 + build/build | 16 +++-- build/build_all | 18 +++-- build/build_wasi_jasper | 2 +- build/install_wasi_sdk | 2 +- build/install_wasmtime | 2 +- build/jas_realpath | 9 --- build/make_dist | 2 +- build/make_release | 12 +++- build/sysinfo | 2 +- build/wasm_cc | 2 +- test/bin/base_utilities | 1 + test/bin/jpcod | 3 +- test/bin/jpdec | 3 +- test/bin/jpenc | 3 +- test/bin/run_codec_test | 3 +- test/bin/run_conformance_tests | 3 +- test/bin/run_test_1 | 3 +- test/bin/run_test_2 | 3 +- test/bin/run_test_3 | 3 +- test/bin/run_test_5 | 3 +- test/bin/run_test_imgcmp | 3 +- test/bin/run_test_imginfo | 3 +- test/bin/{utilities => test_utilities} | 23 ------- 25 files changed, 152 insertions(+), 65 deletions(-) create mode 100644 bin/base_utilities create mode 120000 build/base_utilities delete mode 100755 build/jas_realpath create mode 120000 test/bin/base_utilities rename test/bin/{utilities => test_utilities} (95%) diff --git a/bin/base_utilities b/bin/base_utilities new file mode 100644 index 00000000..1375ceab --- /dev/null +++ b/bin/base_utilities @@ -0,0 +1,92 @@ +################################################################################ +# This file contains some code shared between multiple Bash scripts. +################################################################################ + +################################################################################ +# Some functions related to errors and warnings. +################################################################################ + +panic() +{ + echo "FATAL ERROR: $*" 1>& 2 + exit 1 +} + +warn() +{ + echo "WARNING: $*" 1>& 2 +} + +eecho() +{ + echo "$*" 1>&2 +} + +################################################################################ +# Various utility functions. +################################################################################ + +# Locate a program by name on the executable search path. +find_program() +{ + local program_names=("$@") + local program_name + for program_name in "${program_names[@]}"; do + program_path="$(type -P "$program_name")" || program_path= + if [ -n "$program_path" -a -x "$program_path" ]; then + break + fi + done + if [ -z "$program_path" ]; then + return 1 + fi + echo "$program_path" +} + +# Canonicalize a path. +# Some platforms do not have the realpath program. +# So, we use python and perl as fallbacks, if they are available. +real_path() +{ + [ $# -eq 1 ] || return 1 + local path="$1" + local realpath_path="$(find_program realpath)" || \ + realpath_path= + local python_path="$(find_program python3 python python2)" || \ + python_path= + local perl_path="$(find_program perl)" || perl_path= + + # The following is for testing. + #realpath_path= + #python_path= + #perl_path= + + if [ -n "$realpath_path" ]; then + "$realpath_path" "$path" || \ + return 1 + elif [ -n "$python_path" ]; then + "$python_path" \ + -c 'import os,sys;print(os.path.realpath(sys.argv[1]))' \ + "$path" || \ + return 1 + elif [ -n "$perl_path" ]; then + "$perl_path" \ + -MCwd -e 'print Cwd::realpath($ARGV[0]),qq<\n>' "$path" || \ + return 1 + else + return 1 + fi +} + +repeat_string() +{ + [ $# -eq 2 ] || return 1 + local n="$1" + local string="$2" + local buffer= + local i + for((i = 0; i < $n; ++i)); do + buffer="$buffer$string" + done + echo "$buffer" +} diff --git a/build/base_utilities b/build/base_utilities new file mode 120000 index 00000000..d993bc3f --- /dev/null +++ b/build/base_utilities @@ -0,0 +1 @@ +../bin/base_utilities \ No newline at end of file diff --git a/build/build b/build/build index fbaa2b80..f260a7d9 100755 --- a/build/build +++ b/build/build @@ -6,7 +6,7 @@ panic() { - echo "ERROR: $@" + echo "FATAL ERROR: $*" 1>& 2 exit 1 } @@ -82,10 +82,14 @@ usage() # Early initialization. ################################################################################ -program_dir=$(dirname "$0") || exit 1 -jas_realpath="$program_dir/jas_realpath" -abs_program_dir=$("$jas_realpath" "$program_dir") || panic -sysinfo_program="$program_dir/sysinfo" +self_dir="$(dirname "$0")" || \ + panic "cannot get self directory" +source "$self_dir/base_utilities" || \ + panic "cannot load base utilities" +self_dir="$(real_path "$self_dir")" || \ + panic "cannot get self directory" + +sysinfo_program="$self_dir/sysinfo" ################################################################################ # Process command line. @@ -424,7 +428,7 @@ fi # Perform some initialization. ################################################################################ -source_dir="$abs_program_dir/.." +source_dir="$self_dir/.." if [ -z "$out_dir" ]; then out_dir="$source_dir/tmp_cmake" diff --git a/build/build_all b/build/build_all index 2f08f3c9..bd3cedb4 100755 --- a/build/build_all +++ b/build/build_all @@ -2,15 +2,19 @@ panic() { - echo "ERROR: $@" + echo "FATAL ERROR: $*" 1>& 2 exit 1 } -program_dir=$(dirname "$0") || exit 1 -jas_realpath="$program_dir/jas_realpath" -abs_program_dir=$("$jas_realpath" "$program_dir") || panic -build_program="$program_dir/build" -sysinfo_program="$program_dir/sysinfo" +self_dir="$(dirname "$0")" || \ + panic "cannot get self directory" +source "$self_dir/base_utilities" || \ + panic "cannot load base utilities" +self_dir="$(real_path "$self_dir")" || \ + panic "cannot get self directory" + +build_program="$self_dir/build" +sysinfo_program="$self_dir/sysinfo" list_directory() { @@ -278,7 +282,7 @@ if [ "$debug_level" -ge 1 -a "$verbose" -eq 0 ]; then fi if [ -z "$build_dir" ]; then - build_dir="$abs_program_dir/../tmp_cmake" + build_dir="$self_dir/../tmp_cmake" fi if [ -n "$command_file" ]; then diff --git a/build/build_wasi_jasper b/build/build_wasi_jasper index 5594410f..ca86e75a 100755 --- a/build/build_wasi_jasper +++ b/build/build_wasi_jasper @@ -2,7 +2,7 @@ panic() { - echo "ERROR: $*" 1>& 2 + echo "FATAL ERROR: $*" 1>& 2 exit 1 } diff --git a/build/install_wasi_sdk b/build/install_wasi_sdk index 93dec8bf..2dabe0a5 100755 --- a/build/install_wasi_sdk +++ b/build/install_wasi_sdk @@ -2,7 +2,7 @@ panic() { - echo "ERROR: $*" + echo "FATAL ERROR: $*" 1>& 2 exit 1 } diff --git a/build/install_wasmtime b/build/install_wasmtime index d9ba3359..627f58c3 100755 --- a/build/install_wasmtime +++ b/build/install_wasmtime @@ -8,7 +8,7 @@ panic() { - echo "ERROR: $*" + echo "FATAL ERROR: $*" 1>& 2 exit 1 } diff --git a/build/jas_realpath b/build/jas_realpath deleted file mode 100755 index 038236a5..00000000 --- a/build/jas_realpath +++ /dev/null @@ -1,9 +0,0 @@ -#! /usr/bin/env bash - -if [ $# -ne 1 ]; then - echo "bad usage: no path specified" - exit 1 -fi - -path="$1" -python -c 'import os,sys;print(os.path.realpath(sys.argv[1]))' "$path" diff --git a/build/make_dist b/build/make_dist index 667309b2..9dd4707d 100755 --- a/build/make_dist +++ b/build/make_dist @@ -2,7 +2,7 @@ panic() { - echo "FATAL ERROR: $@" + echo "FATAL ERROR: $*" 1>& 2 exit 1 } diff --git a/build/make_release b/build/make_release index 00ac862e..aab78b68 100755 --- a/build/make_release +++ b/build/make_release @@ -4,7 +4,7 @@ panic() { - echo "ERROR: $*" 1>& 2 + echo "FATAL ERROR: $*" 1>& 2 exit 1 } @@ -36,7 +36,13 @@ usage() ################################################################################ -self_dir="$(dirname "$0")" || panic +self_dir="$(dirname "$0")" || \ + panic "cannot get self directory" +source "$self_dir/base_utilities" || \ + panic "cannot load base utilities" +self_dir="$(real_path "$self_dir")" || \ + panic "cannot get self directory" + build_wasi_jasper="$self_dir/build_wasi_jasper" ################################################################################ @@ -83,7 +89,7 @@ if [ -z "$tmp_dir" ]; then fi # Ensure an absolute pathname. -workspace_dir="$(readlink -f "$workspace_dir")" || \ +workspace_dir="$(real_path "$workspace_dir")" || \ panic "cannot get absolute pathname" if [ ! -d "$workspace_dir" ]; then diff --git a/build/sysinfo b/build/sysinfo index 2de15d7a..8120aab2 100755 --- a/build/sysinfo +++ b/build/sysinfo @@ -2,7 +2,7 @@ panic() { - echo "ERROR: $@" 1>&2 + echo "FATAL ERROR: $*" 1>& 2 exit 1 } diff --git a/build/wasm_cc b/build/wasm_cc index 043dde79..4593fc6b 100755 --- a/build/wasm_cc +++ b/build/wasm_cc @@ -7,7 +7,7 @@ eecho() panic() { - eecho "$*" + echo "FATAL ERROR: $*" 1>& 2 exit 1 } diff --git a/test/bin/base_utilities b/test/bin/base_utilities new file mode 120000 index 00000000..d4fc4306 --- /dev/null +++ b/test/bin/base_utilities @@ -0,0 +1 @@ +../../bin/base_utilities \ No newline at end of file diff --git a/test/bin/jpcod b/test/bin/jpcod index 670c9eb5..fc03abf0 100755 --- a/test/bin/jpcod +++ b/test/bin/jpcod @@ -14,7 +14,8 @@ init() { CMDDIR=$(dirname "$0") || return 1 #CMDDIR=$(realpath "$CMDDIR") || return 1 - CMDDIR=$(readlink -f "$CMDDIR") || return 1 + #CMDDIR=$(readlink -f "$CMDDIR") || return 1 + CMDDIR=$(real_path "$CMDDIR") || return 1 ABSCMDDIR="$CMDDIR" TMPDIR=$(make_tmp_dir jpcod) || return 1 # HOSTNAME=`hostname` diff --git a/test/bin/jpdec b/test/bin/jpdec index 44b3237e..80b4e276 100755 --- a/test/bin/jpdec +++ b/test/bin/jpdec @@ -7,7 +7,8 @@ if [ "$debug_level" -ge 1 ]; then fi cmd_dir=$(dirname "$0") || exit 1 -source "$cmd_dir/utilities" || exit 1 +source "$cmd_dir/base_utilities" || exit 1 +source "$cmd_dir/test_utilities" || exit 1 source "$cmd_dir/jpcod" || exit 1 set_source_and_build_dirs || panic "cannot set source and build directories" diff --git a/test/bin/jpenc b/test/bin/jpenc index 56aa4299..101dc0d3 100755 --- a/test/bin/jpenc +++ b/test/bin/jpenc @@ -7,7 +7,8 @@ if [ "$debug_level" -ge 1 ]; then fi cmd_dir=$(dirname "$0") || exit 1 -source "$cmd_dir/utilities" || exit 1 +source "$cmd_dir/base_utilities" || exit 1 +source "$cmd_dir/test_utilities" || exit 1 source "$cmd_dir/jpcod" || exit 1 set_source_and_build_dirs || panic "cannot set source and build directories" diff --git a/test/bin/run_codec_test b/test/bin/run_codec_test index 9e5d21cc..0a82cd98 100755 --- a/test/bin/run_codec_test +++ b/test/bin/run_codec_test @@ -2,7 +2,8 @@ # Copyright (c) 2016 Michael David Adams cmd_dir=$(dirname "$0") || exit 1 -source "$cmd_dir/utilities" || exit 1 +source "$cmd_dir/base_utilities" || exit 1 +source "$cmd_dir/test_utilities" || exit 1 source "$cmd_dir/jpcod" || exit 1 debug_level="${JAS_DEBUG_LEVEL:-0}" diff --git a/test/bin/run_conformance_tests b/test/bin/run_conformance_tests index c957e229..ecdb26d8 100755 --- a/test/bin/run_conformance_tests +++ b/test/bin/run_conformance_tests @@ -11,7 +11,8 @@ if [ "${BASH_VERSINFO[0]}" -lt 4 ]; then fi cmd_dir=$(dirname "$0") || exit 1 -source "$cmd_dir"/utilities || exit 1 +source "$cmd_dir"/base_utilities || exit 1 +source "$cmd_dir"/test_utilities || exit 1 source "$cmd_dir/jpcod" || exit 1 set_source_and_build_dirs || panic "cannot set source and build directories" diff --git a/test/bin/run_test_1 b/test/bin/run_test_1 index e8a0d3c4..24cb9878 100755 --- a/test/bin/run_test_1 +++ b/test/bin/run_test_1 @@ -8,7 +8,8 @@ ################################################################################ cmd_dir=$(dirname "$0") || exit 1 -source "$cmd_dir"/utilities || exit 1 +source "$cmd_dir"/base_utilities || exit 1 +source "$cmd_dir"/test_utilities || exit 1 debug_level="${JAS_DEBUG_LEVEL:-0}" if [ "$debug_level" -ge 1 ]; then diff --git a/test/bin/run_test_2 b/test/bin/run_test_2 index 8f9e11b7..fb8de934 100755 --- a/test/bin/run_test_2 +++ b/test/bin/run_test_2 @@ -3,7 +3,8 @@ ################################################################################ cmd_dir=$(dirname $0) || exit 1 -source "$cmd_dir/utilities" || exit 1 +source "$cmd_dir/base_utilities" || exit 1 +source "$cmd_dir/test_utilities" || exit 1 debug_level="${JAS_DEBUG_LEVEL:-0}" if [ "$debug_level" -ge 1 ]; then diff --git a/test/bin/run_test_3 b/test/bin/run_test_3 index 5bc9da93..5e8a1460 100755 --- a/test/bin/run_test_3 +++ b/test/bin/run_test_3 @@ -11,7 +11,8 @@ if [ "${BASH_VERSINFO[0]}" -lt 4 ]; then fi cmd_dir=$(dirname "$0") || exit 1 -source "$cmd_dir"/utilities || exit 1 +source "$cmd_dir"/base_utilities || exit 1 +source "$cmd_dir"/test_utilities || exit 1 top_dir="$cmd_dir/../.." sysinfo_program="$top_dir/build/sysinfo" diff --git a/test/bin/run_test_5 b/test/bin/run_test_5 index cbc505f7..69560fe1 100755 --- a/test/bin/run_test_5 +++ b/test/bin/run_test_5 @@ -2,7 +2,8 @@ ################################################################################ cmd_dir=$(dirname "$0") || exit 1 -source "$cmd_dir"/utilities || exit 1 +source "$cmd_dir"/base_utilities || exit 1 +source "$cmd_dir"/test_utilities || exit 1 debug_level="${JAS_DEBUG_LEVEL:-0}" if [ "$debug_level" -ge 1 ]; then diff --git a/test/bin/run_test_imgcmp b/test/bin/run_test_imgcmp index d7ca0e18..a89e0ccd 100755 --- a/test/bin/run_test_imgcmp +++ b/test/bin/run_test_imgcmp @@ -4,7 +4,8 @@ ################################################################################ cmd_dir=$(dirname "$0") || exit 1 -source "$cmd_dir"/utilities || exit 1 +source "$cmd_dir"/base_utilities || exit 1 +source "$cmd_dir"/test_utilities || exit 1 debug_level="${JAS_DEBUG_LEVEL:-0}" if [ "$debug_level" -ge 1 ]; then diff --git a/test/bin/run_test_imginfo b/test/bin/run_test_imginfo index 94aac59a..d5401300 100755 --- a/test/bin/run_test_imginfo +++ b/test/bin/run_test_imginfo @@ -4,7 +4,8 @@ ################################################################################ cmd_dir=$(dirname "$0") || exit 1 -source "$cmd_dir"/utilities || exit 1 +source "$cmd_dir"/base_utilities || exit 1 +source "$cmd_dir"/test_utilities || exit 1 debug_level="${JAS_DEBUG_LEVEL:-0}" if [ "$debug_level" -ge 1 ]; then diff --git a/test/bin/utilities b/test/bin/test_utilities similarity index 95% rename from test/bin/utilities rename to test/bin/test_utilities index 1bbbed06..668ba2c2 100644 --- a/test/bin/utilities +++ b/test/bin/test_utilities @@ -5,17 +5,6 @@ # ################################################################################ -panic() -{ - echo "ERROR: $@" 1>&2 - exit 1 -} - -eecho() -{ - echo "$@" 1>&2 -} - set_source_and_build_dirs() { #eecho "JAS_ABS_TOP_BUILDDIR $JAS_ABS_TOP_BUILDDIR" @@ -252,15 +241,3 @@ is_supported_format() grep -q '^'"$format"'$' && supported=1 echo "$supported" } - -repeat_string() -{ - [ $# -eq 2 ] || return 1 - local n="$1" - local string="$2" - local buffer= - for((i = 0; i < $n; ++i)); do - buffer="$buffer$string" - done - echo "$buffer" -}