Project

General

Profile

1 9016 aaronmk
#!/bin/bash -e
2
. "$(dirname "${BASH_SOURCE[0]}")"/util.sh
3
4
if self_not_included; then
5
6 9074 aaronmk
# usage: target_filename/command() { echo_func; set_make_vars; ...; }
7 9016 aaronmk
alias set_make_vars="$(cat <<'EOF'
8
local command="${FUNCNAME##*/}"; echo_vars command
9
local target_filename="${FUNCNAME%/*}"; echo_vars target_filename
10
local target="$top_dir/$target_filename"; echo_vars target
11
EOF
12
)"
13
14 9147 aaronmk
if isset rm; then : "${remake:=$rm}"; fi #mnemonic: files are rm'd (overwritten)
15
16 9054 aaronmk
alias remaking='test "$remake"'
17
18 9016 aaronmk
# usage: set_make_vars; check_target_exists
19 9122 aaronmk
alias check_target_exists='remaking || require_not_exists "$target" || return 0'
20 9059 aaronmk
alias check_fake_target_exists='remaking || declare if_not_exists=1'
21
	# defer check until to_file
22 9016 aaronmk
23
# usage: set_make_vars; to_target cmd...
24
alias to_target='stdout="$target" to_file ' # last space alias-expands next word
25
26 9194 aaronmk
make() { echo_func; limit_stdout=1 self "$@"; }
27 9016 aaronmk
28
if false; then ## usage:
29 9130 aaronmk
inline_make 10<<'EOF'
30 9016 aaronmk
target:
31
	$(self_dir)/cmd >$@
32
EOF
33
# target will be run automatically because it's first in the makefile
34
fi ##
35 9074 aaronmk
inline_make()
36 9016 aaronmk
{
37
	echo_func
38
	local self="$(readlink -f "${BASH_SOURCE[1]}")"
39
	local self_dir="$(dirname "$self")"
40
	export self self_dir
41
42
	make --makefile=<((
43 9130 aaronmk
		cat /dev/fd/10
44 9016 aaronmk
		echo -n "
45
.SUFFIXES: # turn off built-in suffix rules
46
.SECONDARY: # don't automatically delete intermediate files
47
.DELETE_ON_ERROR: # delete target if recipe fails
48
"
49
	)|echo_stdin) "$@"
50
}
51
52
fi