Project

General

Profile

1 8272 aaronmk
#!/bin/bash
2
set -o errexit; shopt -s expand_aliases
3
# run scripts: a bash-based replacement for make
4
# unlike make, supports full bash functionality including multiline commands
5
# usage: path/to/dir/run cmd args
6
7
if false; then #### run script template:
8
#!/bin/bash
9
set -o errexit
10
. "$(dirname "${BASH_SOURCE[0]}")"/path/to/util.run or file_including_util.run
11
. "$(dirname "${BASH_SOURCE[0]}")"/other_includes
12
13
cmd ()
14
{
15 8280 aaronmk
	echo_func "$FUNCNAME" "$@"
16 8272 aaronmk
	"$(dirname "${BASH_SOURCE[0]}")"/path_relative_to_self
17
	"$(dirname "${BASH_SOURCE[1]}")"/path_relative_to_caller
18
	"$top_dir"/path_relative_to_outermost_script
19
}
20
21
run_cmd "$@"
22
fi ####
23
24 8278 aaronmk
echo_cmd () { echo "$PS4$*" >&2; }
25 8272 aaronmk
26 8278 aaronmk
echo_run () { echo_cmd "$@"; "$@"; }
27
28 8279 aaronmk
# usage: echo_func "$FUNCNAME" "$@"
29
echo_func () { echo_cmd "${BASH_SOURCE[1]}" "$@"; }
30
31 8275 aaronmk
echo_stdin () { tee -a /dev/stderr; } # usage: input|echo_stdin|cmd
32
33 8272 aaronmk
usage () { echo "Usage: $1" >&2; (exit 2); }
34
35
top_dir="$(dirname "$0")" # outermost script
36
37
run_cmd ()
38
{
39
	# only if called in outermost script (+1 for this script)
40
	if test "${#BASH_SOURCE[@]}" -eq 2; then
41
		test "$#" -ge 1 || usage "$0 cmd args" || return
42 8281 aaronmk
		echo_cmd "$0" "$@"; "$@"
43 8272 aaronmk
	fi
44
}
45
46 8283 aaronmk
fwd () # usage: fwd "$FUNCNAME" "$@"
47 8272 aaronmk
{
48 8280 aaronmk
	echo_func "$FUNCNAME" "$@"
49 8272 aaronmk
	for subdir in "${subdirs[@]}"; do
50 8280 aaronmk
		"$(dirname "${BASH_SOURCE[1]}")"/"$subdir"/run "$@"
51 8272 aaronmk
	done
52
}
53
54 8280 aaronmk
make ()
55
{
56
	echo_func "$FUNCNAME" "$@"
57
	echo_run env make --directory="$top_dir" "$@"
58
}
59 8272 aaronmk
60 8276 aaronmk
if false; then ## usage:
61
inline_make <<'EOF'
62
target:
63
	$(self_dir)/cmd >$@
64
EOF
65
# target will be run automatically because it's first in the makefile
66
fi ##
67
inline_make ()
68
{
69 8280 aaronmk
	echo_func "$FUNCNAME" "$@"
70 8276 aaronmk
	(cat
71
	cat <<EOF
72
73
.SUFFIXES: # turn off built-in suffix rules
74
.SECONDARY: # don't automatically delete intermediate files
75
.DELETE_ON_ERROR: # delete target if recipe fails
76
EOF
77
	)|echo_stdin|make --makefile=/dev/stdin \
78
self_dir="$(dirname "$(readlink -f "${BASH_SOURCE[1]}")")" "$@"
79
}
80
81 8272 aaronmk
run_cmd "$@"