Skip to content

Commit

Permalink
Moved some of the common code in Bash scripts into a single file that
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mdadams committed Feb 4, 2024
1 parent faeed11 commit 0cd594d
Show file tree
Hide file tree
Showing 25 changed files with 152 additions and 65 deletions.
92 changes: 92 additions & 0 deletions bin/base_utilities
Original file line number Diff line number Diff line change
@@ -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"
}
1 change: 1 addition & 0 deletions build/base_utilities
16 changes: 10 additions & 6 deletions build/build
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

panic()
{
echo "ERROR: $@"
echo "FATAL ERROR: $*" 1>& 2
exit 1
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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"
Expand Down
18 changes: 11 additions & 7 deletions build/build_all
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion build/build_wasi_jasper
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

panic()
{
echo "ERROR: $*" 1>& 2
echo "FATAL ERROR: $*" 1>& 2
exit 1
}

Expand Down
2 changes: 1 addition & 1 deletion build/install_wasi_sdk
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

panic()
{
echo "ERROR: $*"
echo "FATAL ERROR: $*" 1>& 2
exit 1
}

Expand Down
2 changes: 1 addition & 1 deletion build/install_wasmtime
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

panic()
{
echo "ERROR: $*"
echo "FATAL ERROR: $*" 1>& 2
exit 1
}

Expand Down
9 changes: 0 additions & 9 deletions build/jas_realpath

This file was deleted.

2 changes: 1 addition & 1 deletion build/make_dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

panic()
{
echo "FATAL ERROR: $@"
echo "FATAL ERROR: $*" 1>& 2
exit 1
}

Expand Down
12 changes: 9 additions & 3 deletions build/make_release
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

panic()
{
echo "ERROR: $*" 1>& 2
echo "FATAL ERROR: $*" 1>& 2
exit 1
}

Expand Down Expand Up @@ -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"

################################################################################
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion build/sysinfo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

panic()
{
echo "ERROR: $@" 1>&2
echo "FATAL ERROR: $*" 1>& 2
exit 1
}

Expand Down
2 changes: 1 addition & 1 deletion build/wasm_cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ eecho()

panic()
{
eecho "$*"
echo "FATAL ERROR: $*" 1>& 2
exit 1
}

Expand Down
1 change: 1 addition & 0 deletions test/bin/base_utilities
3 changes: 2 additions & 1 deletion test/bin/jpcod
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
3 changes: 2 additions & 1 deletion test/bin/jpdec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion test/bin/jpenc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion test/bin/run_codec_test
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
3 changes: 2 additions & 1 deletion test/bin/run_conformance_tests
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion test/bin/run_test_1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/bin/run_test_2
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/bin/run_test_3
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
3 changes: 2 additions & 1 deletion test/bin/run_test_5
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/bin/run_test_imgcmp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/bin/run_test_imginfo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 0cd594d

Please sign in to comment.