1
|
#!/bin/bash -e
|
2
|
. "$(dirname "${BASH_SOURCE[0]}")"/util.sh
|
3
|
|
4
|
if self_not_included; then
|
5
|
|
6
|
if isset rm; then : "${remake:=$rm}"; fi #mnemonic: files are rm'd (overwritten)
|
7
|
|
8
|
# usage: target_filename/command() { echo_func; set_make_vars; use $target...; }
|
9
|
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
|
local target_stem="${target_filename%.*}"; echo_vars target_stem
|
14
|
local _remake="$remake" remake=; echo_vars _remake
|
15
|
EOF
|
16
|
)" # `remake=`: don't progagate remake to prerequisites
|
17
|
|
18
|
# usage: remaking || check if exists
|
19
|
# cmd line usage: [remake=1] func
|
20
|
alias remaking='test "$_remake"'
|
21
|
|
22
|
# usage: set_make_vars; ...; self_make overridden_target... # progagates $remake
|
23
|
function self_make() { remake="$_remake" "$@"; }
|
24
|
alias self_make='"self_make" ' # last space alias-expands next word
|
25
|
|
26
|
# usage: set_make_vars; check_target_exists
|
27
|
alias check_target_exists='remaking || require_not_exists "$target" || return 0'
|
28
|
alias check_fake_target_exists='remaking || declare if_not_exists=1'
|
29
|
# defer check until to_file
|
30
|
alias wildcard1_target='local target="$(wildcard1 "$top_dir"/$target_filename)"'
|
31
|
alias check_wildcard_target_exists='wildcard1_target; check_target_exists'
|
32
|
|
33
|
# usage: set_make_vars; to_target cmd...
|
34
|
alias to_target='stdout="$target" to_file ' # last space alias-expands next word
|
35
|
|
36
|
make() # usage: make target...
|
37
|
{
|
38
|
echo_func
|
39
|
(
|
40
|
# at verbosity < 4, hide messages about making included Makefiles
|
41
|
# this can reduce # lines of output to 1/3 as much
|
42
|
if ! "log+" 3 can_log; then fd="$log_fd" clog++ filter_fd sed \
|
43
|
-e '/^make ([^-][^[:space:]]*)?Makefile/,/^make\[[[:digit:]]+\]: .*Makefile/d' \
|
44
|
-e '/^make\[[[:digit:]]+\]: .*Makefile/d'
|
45
|
fi
|
46
|
time cmd_log_fd=1 self $(if ! can_log; then echo '--silent '; fi)"$@"
|
47
|
)
|
48
|
}
|
49
|
|
50
|
top_make() { echo_func; make --directory="$top_dir" "$@"; }
|
51
|
|
52
|
if false; then ## usage:
|
53
|
inline_make <<'EOF'
|
54
|
target:
|
55
|
$(self_dir)/cmd >$@
|
56
|
EOF
|
57
|
# target will be run automatically because it's first in the makefile
|
58
|
fi ##
|
59
|
inline_make()
|
60
|
{
|
61
|
echo_func
|
62
|
local_export self="$(readlink -f "${BASH_SOURCE[1]}")"; echo_vars self
|
63
|
local_export self_dir="$(dirname "$self")"; echo_vars self_dir
|
64
|
|
65
|
make --makefile=<((
|
66
|
cat # script stdin from caller-provided stdin
|
67
|
echo -n "
|
68
|
.SUFFIXES: # turn off built-in suffix rules
|
69
|
.SECONDARY: # don't automatically delete intermediate files
|
70
|
.DELETE_ON_ERROR: # delete target if recipe fails
|
71
|
"
|
72
|
)|echo_stdin) "$@" <&20 # make's stdin from global stdin
|
73
|
}
|
74
|
|
75
|
fi
|