Project

General

Profile

1 8291 aaronmk
#!/bin/bash -e
2
shopt -s expand_aliases
3 8272 aaronmk
# 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 8291 aaronmk
#!/bin/bash -e
9 8272 aaronmk
. "$(dirname "${BASH_SOURCE[0]}")"/path/to/util.run or file_including_util.run
10
. "$(dirname "${BASH_SOURCE[0]}")"/other_includes
11
12
cmd ()
13
{
14 8463 aaronmk
	echo_func "$@"
15 8272 aaronmk
	"$(dirname "${BASH_SOURCE[0]}")"/path_relative_to_self
16
	"$(dirname "${BASH_SOURCE[1]}")"/path_relative_to_caller
17
	"$top_dir"/path_relative_to_outermost_script
18
}
19
fi ####
20
21 8278 aaronmk
echo_cmd () { echo "$PS4$*" >&2; }
22 8272 aaronmk
23 8278 aaronmk
echo_run () { echo_cmd "$@"; "$@"; }
24
25 8463 aaronmk
# usage: echo_func "$@"
26
echo_func ()
27
{
28
	echo_cmd "${BASH_SOURCE[1]}:${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$@"
29
}
30 8279 aaronmk
31 8275 aaronmk
echo_stdin () { tee -a /dev/stderr; } # usage: input|echo_stdin|cmd
32
33 8641 aaronmk
echo_vars () { declare -p "$@" >&2; } # usage: echo_vars var...
34
35
echo_export ()
36
{
37
	echo_vars "$@"
38
	export "$@"
39
}
40
41 8643 aaronmk
alias export="echo_export" # automatically echo env vars when they are set
42 8642 aaronmk
43 8272 aaronmk
usage () { echo "Usage: $1" >&2; (exit 2); }
44
45
top_dir="$(dirname "$0")" # outermost script
46
47 8465 aaronmk
run_args_cmd () # runs the command line args command
48 8272 aaronmk
{
49 8292 aaronmk
	test "$?" -eq 0 || return
50 8289 aaronmk
	set -- "${BASH_ARGV[@]}"
51 8290 aaronmk
	test "$#" -ge 1 || set -- all
52 8289 aaronmk
	echo_cmd "$0" "$@"; "$@"
53 8272 aaronmk
}
54
55 8464 aaronmk
# users can override this function to perform other commands (or no commands)
56
# after the script is read
57 8465 aaronmk
on_exit () { run_args_cmd; }
58 8464 aaronmk
trap on_exit EXIT
59
60 8284 aaronmk
fwd () # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
61 8272 aaronmk
{
62 8463 aaronmk
	echo_func "$@"
63 8284 aaronmk
	: "${subdirs?}"
64
65 8272 aaronmk
	for subdir in "${subdirs[@]}"; do
66 8280 aaronmk
		"$(dirname "${BASH_SOURCE[1]}")"/"$subdir"/run "$@"
67 8272 aaronmk
	done
68
}
69
70 8280 aaronmk
make ()
71
{
72 8463 aaronmk
	echo_func "$@"
73 8280 aaronmk
	echo_run env make --directory="$top_dir" "$@"
74
}
75 8272 aaronmk
76 8276 aaronmk
if false; then ## usage:
77
inline_make <<'EOF'
78
target:
79
	$(self_dir)/cmd >$@
80
EOF
81
# target will be run automatically because it's first in the makefile
82
fi ##
83
inline_make ()
84
{
85 8640 aaronmk
	local self="$(readlink -f "${BASH_SOURCE[1]}")"
86
	local self_dir="$(dirname "$self")"
87 8644 aaronmk
	export self_dir
88 8640 aaronmk
89 8463 aaronmk
	echo_func "$@"
90 8276 aaronmk
	(cat
91
	cat <<EOF
92
93
.SUFFIXES: # turn off built-in suffix rules
94
.SECONDARY: # don't automatically delete intermediate files
95
.DELETE_ON_ERROR: # delete target if recipe fails
96
EOF
97 8644 aaronmk
	)|echo_stdin|make --makefile=/dev/stdin "$@"
98 8276 aaronmk
}