Project

General

Profile

1
#!/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
	echo_func "$FUNCNAME" "$@"
16
	"$(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
echo_cmd () { echo "$PS4$*" >&2; }
25

    
26
echo_run () { echo_cmd "$@"; "$@"; }
27

    
28
# usage: echo_func "$FUNCNAME" "$@"
29
echo_func () { echo_cmd "${BASH_SOURCE[1]}" "$@"; }
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_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
		echo_cmd "$0" "$@"; "$@"
43
	fi
44
}
45

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

    
56
make ()
57
{
58
	echo_func "$FUNCNAME" "$@"
59
	echo_run env make --directory="$top_dir" "$@"
60
}
61

    
62
if false; then ## usage:
63
inline_make <<'EOF'
64
target:
65
	$(self_dir)/cmd >$@
66
EOF
67
# target will be run automatically because it's first in the makefile
68
fi ##
69
inline_make ()
70
{
71
	echo_func "$FUNCNAME" "$@"
72
	(cat
73
	cat <<EOF
74

    
75
.SUFFIXES: # turn off built-in suffix rules
76
.SECONDARY: # don't automatically delete intermediate files
77
.DELETE_ON_ERROR: # delete target if recipe fails
78
EOF
79
	)|echo_stdin|make --makefile=/dev/stdin \
80
self_dir="$(dirname "$(readlink -f "${BASH_SOURCE[1]}")")" "$@"
81
}
82

    
83
run_cmd "$@"
(45-45/50)