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 9666 aaronmk
if isset rm; then : "${remake:=$rm}"; fi #mnemonic: files are rm'd (overwritten)
7
8 9354 aaronmk
# usage: target_filename/command() { echo_func; set_make_vars; use $target...; }
9 9016 aaronmk
alias set_make_vars="$(cat <<'EOF'
10
local command="${FUNCNAME##*/}"; echo_vars command
11
local target_filename="${FUNCNAME%/*}"; echo_vars target_filename
12
local target="$top_dir/$target_filename"; echo_vars target
13 9431 aaronmk
local target_stem="${target_filename%.*}"; echo_vars target_stem
14 9667 aaronmk
local _remake="$remake" remake= # don\'t progagate remake to prerequisites
15 9016 aaronmk
EOF
16
)"
17
18 9667 aaronmk
# usage: remaking || check if exists
19
# cmd line usage: [remake=1] func
20
alias remaking='test "$_remake"'
21
22 9016 aaronmk
# usage: set_make_vars; check_target_exists
23 9122 aaronmk
alias check_target_exists='remaking || require_not_exists "$target" || return 0'
24 9059 aaronmk
alias check_fake_target_exists='remaking || declare if_not_exists=1'
25
	# defer check until to_file
26 9016 aaronmk
27
# usage: set_make_vars; to_target cmd...
28
alias to_target='stdout="$target" to_file ' # last space alias-expands next word
29
30 9584 aaronmk
make() # usage: [silent=1] table_make target...
31
{
32
	echo_func; kw_params silent
33
	cmd_log_fd=1 self ${silent:+--silent } "$@"
34
}
35 9016 aaronmk
36 9505 aaronmk
top_make() { echo_func; make --directory="$top_dir" "$@"; }
37
38 9016 aaronmk
if false; then ## usage:
39 9353 aaronmk
inline_make <<'EOF'
40 9016 aaronmk
target:
41
	$(self_dir)/cmd >$@
42
EOF
43
# target will be run automatically because it's first in the makefile
44
fi ##
45 9074 aaronmk
inline_make()
46 9016 aaronmk
{
47
	echo_func
48 9237 aaronmk
	local_export self="$(readlink -f "${BASH_SOURCE[1]}")"; echo_vars self
49
	local_export self_dir="$(dirname "$self")"; echo_vars self_dir
50 9016 aaronmk
51
	make --makefile=<((
52 9353 aaronmk
		cat # script stdin from caller-provided stdin
53 9016 aaronmk
		echo -n "
54
.SUFFIXES: # turn off built-in suffix rules
55
.SECONDARY: # don't automatically delete intermediate files
56
.DELETE_ON_ERROR: # delete target if recipe fails
57
"
58 9353 aaronmk
	)|echo_stdin) "$@" <&20 # make's stdin from global stdin
59 9016 aaronmk
}
60
61
fi