forked from onetrueawk/awk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbuild.sh
executable file
·85 lines (78 loc) · 2.18 KB
/
cbuild.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/sh
POSIXLY_CORRECT=1
cbuild_OPWD="$PWD"
BASE="$(realpath "$0")" && BASE="${BASE%/*}"
if [ "$OPWD" != "$BASE" ]; then
cd "$BASE" || log "$R" "Unable to change directory to ${BASE##*/}. Re-execute using a POSIX shell and check again."
fi
trap 'cd "$cbuild_OPWD"' EXIT
# Color escape sequences
G="\033[32m" # Green
R="\033[31m" # Red
B="\033[34m" # Blue
NC="\033[m" # Unset
log() {
# shellcheck disable=SC2059 # Using %s with ANSII escape sequences is not possible
printf "${1}->$NC "
shift
printf "%s\n" "$*"
}
require() {
command -v "$1" >/dev/null 2>&1 || {
log "$R" "[$1] is not installed. Please ensure the command is available [$1] and try again."
exit 1
}
}
run() {
log "$B" "$*"
# shellcheck disable=SC2068 # We want to split elements, but avoid whitespace problems (`$*`), and also avoid `eval $*`
$@
}
: "${CC:=cc}"
build() {
require "make"
require "${CC}"
log "$G" "Entering step: \"Build \"${BASE##*/}\" using \"$CC\""
make -j"$(nproc)" || {
log "$R" "Failed during step: \"Build \"${BASE##*/}\" using \"$CC\""
exit 1
}
}
# Argument processing
while [ $# -gt 0 ] || [ "$1" = "" ]; do
case "$1" in
"" | "build")
# If the user doesn't use "build" explicitely, do not run the build step again.
[ "$1" = "build" ] || {
explicit="1"
} && [ -n "$1" ] && shift
if [ "$explicit" = "1" ]; then
[ -f ./a.out ] || [ -f ./awk ] && log "$R" "Nothing to do; \"${BASE##*/}\" was already compiled" && exit 0
fi
# Start build process
build && exit 0 || exit 1
;;
"clean")
shift
run rm ./a.out ./awk 2>/dev/null
exit 0
;;
"retrieve")
shift
if [ -x "./a.out" ]; then
if [ ! -e "./awk" ]; then
mv ./a.out ./awk
fi
elif [ ! -e "./awk" ]; then
log "$R" "\"${BASE##*/}\" was never compiled OR it was but its binaries weren't found anyways."
exit 1
fi
readlink -f ./awk
exit 0
;;
*)
echo "Usage: $0 {build|clean}"
exit 1
;;
esac
done