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