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