Project

General

Profile

1
#!/bin/bash -e
2
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 -e
9
. "$(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
	echo_func "$@"
15
	"$(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
echo_cmd () { echo "$PS4$*" >&2; }
22

    
23
echo_run () { echo_cmd "$@"; "$@"; }
24

    
25
# usage: echo_func "$@"
26
echo_func ()
27
{
28
	echo_cmd "${BASH_SOURCE[1]}:${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$@"
29
}
30

    
31
echo_stdin () { tee -a /dev/stderr; } # usage: input|echo_stdin|cmd
32

    
33
usage () { echo "Usage: $1" >&2; (exit 2); }
34

    
35
top_dir="$(dirname "$0")" # outermost script
36

    
37
run_args_cmd () # runs the command line args command
38
{
39
	test "$?" -eq 0 || return
40
	set -- "${BASH_ARGV[@]}"
41
	test "$#" -ge 1 || set -- all
42
	echo_cmd "$0" "$@"; "$@"
43
}
44

    
45
# users can override this function to perform other commands (or no commands)
46
# after the script is read
47
on_exit () { run_args_cmd; }
48
trap on_exit EXIT
49

    
50
fwd () # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
51
{
52
	echo_func "$@"
53
	: "${subdirs?}"
54
	
55
	for subdir in "${subdirs[@]}"; do
56
		"$(dirname "${BASH_SOURCE[1]}")"/"$subdir"/run "$@"
57
	done
58
}
59

    
60
make ()
61
{
62
	echo_func "$@"
63
	echo_run env make --directory="$top_dir" "$@"
64
}
65

    
66
if false; then ## usage:
67
inline_make <<'EOF'
68
target:
69
	$(self_dir)/cmd >$@
70
EOF
71
# target will be run automatically because it's first in the makefile
72
fi ##
73
inline_make ()
74
{
75
	local self_dir="$(dirname "$(readlink -f "${BASH_SOURCE[1]}")")"
76
	echo_func "$@"
77
	(cat
78
	cat <<EOF
79

    
80
.SUFFIXES: # turn off built-in suffix rules
81
.SECONDARY: # don't automatically delete intermediate files
82
.DELETE_ON_ERROR: # delete target if recipe fails
83
EOF
84
	)|echo_stdin|make --makefile=/dev/stdin self_dir="$self_dir" "$@"
85
}
(46-46/51)