Project

General

Profile

1
#!/bin/bash -e
2
. "$(dirname "${BASH_SOURCE[0]}")"/util.sh
3

    
4
if self_not_included; then
5

    
6
# usage: target_filename/command() { echo_func; set_make_vars; use $target...; }
7
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
local target_stem="${target_filename%.*}"; echo_vars target_stem
12
EOF
13
)"
14

    
15
if isset rm; then : "${remake:=$rm}"; fi #mnemonic: files are rm'd (overwritten)
16

    
17
# usage: remaking || check if exists
18
# cmd line usage: [remake=1] func
19
alias remaking='test "$remake"'
20

    
21
# usage: set_make_vars; check_target_exists
22
alias check_target_exists='remaking || require_not_exists "$target" || return 0'
23
alias check_fake_target_exists='remaking || declare if_not_exists=1'
24
	# defer check until to_file
25

    
26
# usage: set_make_vars; to_target cmd...
27
alias to_target='stdout="$target" to_file ' # last space alias-expands next word
28

    
29
make() { echo_func; cmd_log_fd=1 self "$@"; }
30

    
31
if false; then ## usage:
32
inline_make <<'EOF'
33
target:
34
	$(self_dir)/cmd >$@
35
EOF
36
# target will be run automatically because it's first in the makefile
37
fi ##
38
inline_make()
39
{
40
	echo_func
41
	local_export self="$(readlink -f "${BASH_SOURCE[1]}")"; echo_vars self
42
	local_export self_dir="$(dirname "$self")"; echo_vars self_dir
43
	
44
	make --makefile=<((
45
		cat # script stdin from caller-provided stdin
46
		echo -n "
47
.SUFFIXES: # turn off built-in suffix rules
48
.SECONDARY: # don't automatically delete intermediate files
49
.DELETE_ON_ERROR: # delete target if recipe fails
50
"
51
	)|echo_stdin) "$@" <&20 # make's stdin from global stdin
52
}
53

    
54
fi
(4-4/5)