1 |
9016
|
aaronmk
|
#!/bin/bash -e
|
2 |
12777
|
aaronmk
|
if false; then #### make target template:
|
3 |
|
|
target()
|
4 |
|
|
{
|
5 |
|
|
begin_target
|
6 |
|
|
#...
|
7 |
|
|
}
|
8 |
|
|
fi ####
|
9 |
|
|
|
10 |
9016
|
aaronmk
|
. "$(dirname "${BASH_SOURCE[0]}")"/util.sh
|
11 |
|
|
|
12 |
|
|
if self_not_included; then
|
13 |
|
|
|
14 |
9666
|
aaronmk
|
if isset rm; then : "${remake:=$rm}"; fi #mnemonic: files are rm'd (overwritten)
|
15 |
10996
|
aaronmk
|
unset rm # don't allow rm to override remake if an invoked script uses this file
|
16 |
11588
|
aaronmk
|
export remake # propagate to invoked commands by default
|
17 |
9666
|
aaronmk
|
|
18 |
12854
|
aaronmk
|
alias echo_target='log-- echo_func' # display by default
|
19 |
12780
|
aaronmk
|
|
20 |
12775
|
aaronmk
|
# helper alias; use begin_target instead
|
21 |
9354
|
aaronmk
|
# usage: target_filename/command() { echo_func; set_make_vars; use $target...; }
|
22 |
9016
|
aaronmk
|
alias set_make_vars="$(cat <<'EOF'
|
23 |
13318
|
aaronmk
|
log_local; log++ # don\'t display make vars at verbosity 2 to avoid clutter
|
24 |
9016
|
aaronmk
|
local command="${FUNCNAME##*/}"; echo_vars command
|
25 |
|
|
local target_filename="${FUNCNAME%/*}"; echo_vars target_filename
|
26 |
|
|
local target="$top_dir/$target_filename"; echo_vars target
|
27 |
9431
|
aaronmk
|
local target_stem="${target_filename%.*}"; echo_vars target_stem
|
28 |
9691
|
aaronmk
|
local _remake="$remake" remake=; echo_vars _remake
|
29 |
13318
|
aaronmk
|
log--
|
30 |
9016
|
aaronmk
|
EOF
|
31 |
9674
|
aaronmk
|
)" # `remake=`: don't progagate remake to prerequisites
|
32 |
9016
|
aaronmk
|
|
33 |
12775
|
aaronmk
|
# usage: target() { begin_target; use $target...; }
|
34 |
13317
|
aaronmk
|
alias begin_target='echo_target; set_make_vars' # echo all targets
|
35 |
12775
|
aaronmk
|
|
36 |
12983
|
aaronmk
|
# usage: begin_target; ...; remaking || check if exists
|
37 |
9667
|
aaronmk
|
# cmd line usage: [remake=1] func
|
38 |
12983
|
aaronmk
|
# **WARNING**: you *MUST* use begin_target at the beginning of any function
|
39 |
11976
|
aaronmk
|
# that uses this, so that $_remake is properly set to $remake and not left at
|
40 |
|
|
# its previous value
|
41 |
9667
|
aaronmk
|
alias remaking='test "$_remake"'
|
42 |
|
|
|
43 |
12983
|
aaronmk
|
# usage: begin_target; ...; with_rm target ... # progagates $rm/$remake
|
44 |
12969
|
aaronmk
|
# **WARNING**: only works inside a runscript target that starts w/ begin_target
|
45 |
12967
|
aaronmk
|
function with_rm() { remake="$_remake" "$@"; }
|
46 |
|
|
alias with_rm='"with_rm" ' # last space alias-expands next word
|
47 |
9679
|
aaronmk
|
|
48 |
12983
|
aaronmk
|
# usage: begin_target; check_target_exists
|
49 |
9122
|
aaronmk
|
alias check_target_exists='remaking || require_not_exists "$target" || return 0'
|
50 |
9942
|
aaronmk
|
alias deferred_check_target_exists='remaking || declare if_not_exists=1'
|
51 |
9059
|
aaronmk
|
# defer check until to_file
|
52 |
9942
|
aaronmk
|
alias check_fake_target_exists='deferred_check_target_exists'
|
53 |
9940
|
aaronmk
|
alias wildcard1_target='local target="$(wildcard1 "$top_dir"/$target_filename)"'
|
54 |
|
|
alias check_wildcard_target_exists='wildcard1_target; check_target_exists'
|
55 |
9016
|
aaronmk
|
|
56 |
12983
|
aaronmk
|
# usage: begin_target; to_target cmd...
|
57 |
9016
|
aaronmk
|
alias to_target='stdout="$target" to_file ' # last space alias-expands next word
|
58 |
|
|
|
59 |
9734
|
aaronmk
|
make() # usage: make target...
|
60 |
9584
|
aaronmk
|
{
|
61 |
9710
|
aaronmk
|
echo_func
|
62 |
9721
|
aaronmk
|
(
|
63 |
9739
|
aaronmk
|
# at verbosity < 4, hide messages about making included Makefiles
|
64 |
9721
|
aaronmk
|
# this can reduce # lines of output to 1/3 as much
|
65 |
12829
|
aaronmk
|
if test ! "$make_filter_active" && ! log+ 3 can_log; then
|
66 |
10066
|
aaronmk
|
local_export make_filter_active=1
|
67 |
12829
|
aaronmk
|
local cmd="$(log++ sys_cmd_path "$(self_name)")"; echo_vars cmd
|
68 |
|
|
fd="$log_fd" log++ filter_fd sed \
|
69 |
10065
|
aaronmk
|
-e "\%^$cmd ([^-][^[:space:]]*)?Makefile%,/^make\[[[:digit:]]+\]: .*Makefile/d" \
|
70 |
|
|
-e '/^make\[[[:digit:]]+\]: .*Makefile/d'
|
71 |
9752
|
aaronmk
|
fi
|
72 |
10047
|
aaronmk
|
time cmd_log_fd=1 self_sys $(if ! can_log; then echo '--silent ';fi)"$@"
|
73 |
9721
|
aaronmk
|
)
|
74 |
9584
|
aaronmk
|
}
|
75 |
9016
|
aaronmk
|
|
76 |
9505
|
aaronmk
|
top_make() { echo_func; make --directory="$top_dir" "$@"; }
|
77 |
|
|
|
78 |
9016
|
aaronmk
|
if false; then ## usage:
|
79 |
9353
|
aaronmk
|
inline_make <<'EOF'
|
80 |
9016
|
aaronmk
|
target:
|
81 |
|
|
$(self_dir)/cmd >$@
|
82 |
|
|
EOF
|
83 |
|
|
# target will be run automatically because it's first in the makefile
|
84 |
|
|
fi ##
|
85 |
9074
|
aaronmk
|
inline_make()
|
86 |
9016
|
aaronmk
|
{
|
87 |
|
|
echo_func
|
88 |
9237
|
aaronmk
|
local_export self="$(readlink -f "${BASH_SOURCE[1]}")"; echo_vars self
|
89 |
|
|
local_export self_dir="$(dirname "$self")"; echo_vars self_dir
|
90 |
9016
|
aaronmk
|
|
91 |
|
|
make --makefile=<((
|
92 |
9353
|
aaronmk
|
cat # script stdin from caller-provided stdin
|
93 |
9016
|
aaronmk
|
echo -n "
|
94 |
|
|
.SUFFIXES: # turn off built-in suffix rules
|
95 |
|
|
.SECONDARY: # don't automatically delete intermediate files
|
96 |
|
|
.DELETE_ON_ERROR: # delete target if recipe fails
|
97 |
|
|
"
|
98 |
9353
|
aaronmk
|
)|echo_stdin) "$@" <&20 # make's stdin from global stdin
|
99 |
9016
|
aaronmk
|
}
|
100 |
|
|
|
101 |
|
|
fi
|