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
fi ####
21

    
22
echo_cmd () { echo "$PS4$*" >&2; }
23

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

    
26
# usage: echo_func "$FUNCNAME" "$@"
27
echo_func () { echo_cmd "${BASH_SOURCE[1]}" "$@"; }
28

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

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

    
33
top_dir="$(dirname "$0")" # outermost script
34

    
35
run_cmd ()
36
{
37
	set -- "${BASH_ARGV[@]}"
38
	test "$#" -ge 1 || set -- all
39
	echo_cmd "$0" "$@"; "$@"
40
}
41
trap run_cmd EXIT
42

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

    
53
make ()
54
{
55
	echo_func "$FUNCNAME" "$@"
56
	echo_run env make --directory="$top_dir" "$@"
57
}
58

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

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