Browse Source

Clean up 'tool' build script

Removes some redundancies and improves formatting.
master
cancel 4 years ago
parent
commit
e8b4511fb5
  1. 118
      tool

118
tool

@ -40,6 +40,10 @@ Optional Features:
EOF EOF
} }
warn() { printf 'Warning: %s\n' "$*" >&2; }
fatal() { printf 'Error: %s\n' "$*" >&2; exit 1; }
script_error() { printf 'Script error: %s\n' "$*" >&2; exit 1; }
if [ -z "${1:-}" ]; then if [ -z "${1:-}" ]; then
printf 'Error: Command required\n' >&2 printf 'Error: Command required\n' >&2
print_usage >&2 print_usage >&2
@ -49,19 +53,14 @@ fi
cmd=$1 cmd=$1
shift shift
os=
case $(uname -s | awk '{print tolower($0)}') in case $(uname -s | awk '{print tolower($0)}') in
linux*) os=linux;; linux*) os=linux;;
darwin*) os=mac;; darwin*) os=mac;;
cygwin*) os=cygwin;; cygwin*) os=cygwin;;
*bsd*) os=bsd;; *bsd*) os=bsd;;
*) os=unknown;; *) os=unknown; warn "Build script not tested on this platform";;
esac esac
if [ $os = unknown ]; then
warn "Build script not tested on this platform"
fi
cc_exe="${CC:-cc}" cc_exe="${CC:-cc}"
if [ $os = cygwin ]; then if [ $os = cygwin ]; then
@ -78,8 +77,7 @@ if [ $os = cygwin ]; then
# happens. So we'll just explicitly set it to gcc. This might mess up people # happens. So we'll just explicitly set it to gcc. This might mess up people
# who have clang installed but not gcc, I guess? Is that even possible? # who have clang installed but not gcc, I guess? Is that even possible?
case $cc_exe in case $cc_exe in
i686-w64-mingw32-gcc.exe|\ i686-w64-mingw32-gcc.exe|x86_64-w64-mingw32-gcc.exe)
x86_64-w64-mingw32-gcc.exe)
cc_exe=gcc;; cc_exe=gcc;;
esac esac
fi fi
@ -95,8 +93,7 @@ config_mode=release
while getopts c:dhsv-: opt_val; do while getopts c:dhsv-: opt_val; do
case $opt_val in case $opt_val in
-) -) case $OPTARG in
case $OPTARG in
harden) protections_enabled=1;; harden) protections_enabled=1;;
help) print_usage; exit 0;; help) print_usage; exit 0;;
static) static_enabled=1;; static) static_enabled=1;;
@ -105,44 +102,27 @@ while getopts c:dhsv-: opt_val; do
no-portmidi|noportmidi) portmidi_enabled=0;; no-portmidi|noportmidi) portmidi_enabled=0;;
mouse) mouse_disabled=0;; mouse) mouse_disabled=0;;
no-mouse|nomouse) mouse_disabled=1;; no-mouse|nomouse) mouse_disabled=1;;
*) *) printf 'Unknown option --%s\n' "$OPTARG" >&2; exit 1;;
printf 'Unknown option --%s\n' "$OPTARG" >&2 esac;;
exit 1
;;
esac
;;
c) cc_exe=$OPTARG;; c) cc_exe=$OPTARG;;
d) config_mode=debug;; d) config_mode=debug;;
h) print_usage; exit 0;; h) print_usage; exit 0;;
s) stats_enabled=1;; s) stats_enabled=1;;
v) verbose=1;; v) verbose=1;;
\?) print_usage >&2; exit 1;; \?) print_usage >&2; exit 1;;
*) break;;
esac esac
done done
arch=
case $(uname -m) in case $(uname -m) in
x86_64) arch=x86_64;; x86_64) arch=x86_64;;
*) arch=unknown;; *) arch=unknown;;
esac esac
warn() {
printf 'Warning: %s\n' "$*" >&2
}
fatal() {
printf 'Error: %s\n' "$*" >&2
exit 1
}
script_error() {
printf 'Script error: %s\n' "$*" >&2
exit 1
}
verbose_echo() { verbose_echo() {
# Don't print 'timed_stats' if it's the first part of the command # Don't print 'timed_stats' if it's the first part of the command
if [ $verbose = 1 ] && [ $# -gt 1 ]; then if [ $verbose = 1 ] && [ $# -gt 1 ]; then
printf '%s ' "$@" | sed -E -e 's/^timed_stats[[:space:]]+//' -e 's/ $//' | tr -d '\n' printf '%s ' "$@" | sed -E -e 's/^timed_stats[[:space:]]+//' -e 's/ $//' \
| tr -d '\n'
printf '\n' printf '\n'
fi fi
"$@" "$@"
@ -152,17 +132,17 @@ file_size() {
wc -c < "$1" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' wc -c < "$1" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
} }
last_time= timed_stats_result=
timed_stats() { timed_stats() {
if [ $stats_enabled = 1 ] && command -v time >/dev/null 2>&1; then if [ $stats_enabled = 1 ] && command -v time >/dev/null 2>&1; then
TIMEFORMAT='%3R' TIMEFORMAT='%3R'
{ last_time=$( { time "$@" 1>&3- 2>&4-; } 2>&1 ); } 3>&1 4>&2 { timed_stats_result=$( { time "$@" 1>&3- 2>&4-; } 2>&1 ); } 3>&1 4>&2
else else
"$@" "$@"
fi fi
} }
version_string_normalized() { normalized_version() {
printf '%s\n' "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'; printf '%s\n' "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }';
} }
@ -170,7 +150,8 @@ cc_id=
cc_vers= cc_vers=
lld_detected=0 lld_detected=0
lld_name=lld lld_name=lld
if preproc_result=$( ("$cc_exe" -E -xc - 2>/dev/null | tail -n 2 | tr -d '\040') <<EOF if preproc_result=$( \
("$cc_exe" -E -xc - 2>/dev/null | tail -n 2 | tr -d '\040') <<EOF
#if defined(__clang__) #if defined(__clang__)
clang clang
__clang_major__.__clang_minor__.__clang_patchlevel__ __clang_major__.__clang_minor__.__clang_patchlevel__
@ -205,37 +186,28 @@ if [ "$cc_id" = clang ]; then
END { if (a == "") exit -1; printf("lld-%s", a) }'); then END { if (a == "") exit -1; printf("lld-%s", a) }'); then
lld_name=$output lld_name=$output
fi fi
if command -v "$lld_name" >/dev/null 2>&1; then lld_detected=1; fi;; if command -v "$lld_name" >/dev/null 2>&1; then lld_detected=1; fi
;;
esac esac
fi fi
if [ -z "$cc_id" ]; then test -z "$cc_id" && warn "Failed to detect compiler type"
warn "Failed to detect compiler type" test -z "$cc_vers" && warn "Failed to detect compiler version"
fi
if [ -z "$cc_vers" ]; then cc_vers_normalized=$(normalized_version "$cc_vers")
warn "Failed to detect compiler version"
fi
cc_vers_is_gte() { cc_vers_is_gte() {
if [ "$(version_string_normalized "$cc_vers")" -ge \ test "$cc_vers_normalized" -ge "$(normalized_version "$1")"
"$(version_string_normalized "$1")" ]; then
return 0
else
return 1
fi
} }
cc_id_and_vers_gte() { cc_id_and_vers_gte() {
if [ "$cc_id" = "$1" ] && cc_vers_is_gte "$2"; then test "$cc_id" = "$1" && cc_vers_is_gte "$2"
return 0
else
return 1
fi
} }
# Append arguments to a string, separated by newlines. Like a bad array.
add() { add() {
if [ -z "${1:-}" ]; then if [ -z "${1:-}" ]; then
script_error "At least one argument required for array add" script_error "At least one argument required for add"
fi fi
_add_name=${1} _add_name=${1}
shift shift
@ -347,21 +319,19 @@ build_target() {
case $arch in case $arch in
x86_64) x86_64)
case $cc_id in
# 'nehalem' tuning actually produces faster code for orca than later # 'nehalem' tuning actually produces faster code for orca than later
# archs, for both gcc and clang, even if it's running on a later arch # archs, for both gcc and clang, even if it's running on a later arch
# CPU. This is likely due to smaller emitted code size. gcc earlier than # CPU. This is likely due to smaller emitted code size. gcc earlier
# 4.9 does not recognize the arch flag for it it, though, and I haven't # than 4.9 does not recognize the arch flag for it it, though, and I
# tested a compiler that old, so I don't know what optimization behavior # haven't tested a compiler that old, so I don't know what optimization
# we get with it is. Just leave it at default, in that case. # behavior we get with it is. Just leave it at default, in that case.
case $cc_id in
gcc) gcc)
if cc_vers_is_gte 4.9; then if cc_vers_is_gte 4.9; then
add cc_flags -march=nehalem add cc_flags -march=nehalem
fi fi
;; ;;
clang) clang) add cc_flags -march=nehalem;;
add cc_flags -march=nehalem
;;
esac esac
;; ;;
esac esac
@ -476,8 +446,8 @@ EOF
verbose_echo timed_stats "$cc_exe" $cc_flags -o "$out_path" $source_files $libraries verbose_echo timed_stats "$cc_exe" $cc_flags -o "$out_path" $source_files $libraries
compile_ok=$? compile_ok=$?
if [ $stats_enabled = 1 ]; then if [ $stats_enabled = 1 ]; then
if [ -n "$last_time" ]; then if [ -n "$timed_stats_result" ]; then
printf '%s\n' "time: $last_time" printf '%s\n' "time: $timed_stats_result"
else else
printf '%s\n' "time: unavailable (missing 'time' command)" printf '%s\n' "time: unavailable (missing 'time' command)"
fi fi
@ -512,14 +482,11 @@ shift $((OPTIND - 1))
case $cmd in case $cmd in
info) info)
if [ "$#" -gt 1 ]; then test "$#" -gt 1 && fatal "Too many arguments for 'info'"
fatal "Too many arguments for 'info'" print_info; exit 0
fi ;;
print_info; exit 0;;
build) build)
if [ "$#" -lt 1 ]; then test "$#" -lt 1 && fatal "Too few arguments for 'build'"
fatal "Too few arguments for 'build'"
fi
if [ "$#" -gt 1 ]; then if [ "$#" -gt 1 ]; then
cat >&2 <<EOF cat >&2 <<EOF
Too many arguments for 'build' Too many arguments for 'build'
@ -533,17 +500,20 @@ EOF
;; ;;
clean) clean)
if [ -d "$build_dir" ]; then if [ -d "$build_dir" ]; then
verbose_echo rm -rf "$build_dir" verbose_echo rm -rf "$build_dir";
fi fi
;; ;;
help) print_usage; exit 0;; help)
print_usage; exit 0
;;
-*) cat >&2 <<EOF -*) cat >&2 <<EOF
The syntax has changed for the 'tool' build script. The syntax has changed for the 'tool' build script.
The options now need to come after the command name. The options now need to come after the command name.
Do it like this instead: Do it like this instead:
./tool build --portmidi orca ./tool build --portmidi orca
EOF EOF
exit 1 ;; exit 1
;;
*) fatal "Unrecognized command $cmd";; *) fatal "Unrecognized command $cmd";;
esac esac

Loading…
Cancel
Save