Project

General

Profile

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
unset rm # don't allow rm to override remake if an invoked script uses this file
8
export remake # propagate to invoked commands by default
9

    
10
# usage: target_filename/command() { echo_func; set_make_vars; use $target...; }
11
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
local target_stem="${target_filename%.*}"; echo_vars target_stem
16
local _remake="$remake" remake=; echo_vars _remake
17
EOF
18
)" # `remake=`: don't progagate remake to prerequisites
19

    
20
# usage: set_make_vars; ...; remaking || check if exists
21
# cmd line usage: [remake=1] func
22
# **WARNING**: you *MUST* use set_make_vars at the beginning of any function
23
# that uses this, so that $_remake is properly set to $remake and not left at
24
# its previous value
25
alias remaking='test "$_remake"'
26

    
27
# usage: set_make_vars; ...; self_make overridden_target... # progagates $remake
28
function self_make() { remake="$_remake" "$@"; }
29
alias self_make='"self_make" ' # last space alias-expands next word
30

    
31
# usage: set_make_vars; check_target_exists
32
alias check_target_exists='remaking || require_not_exists "$target" || return 0'
33
alias deferred_check_target_exists='remaking || declare if_not_exists=1'
34
	# defer check until to_file
35
alias check_fake_target_exists='deferred_check_target_exists'
36
alias wildcard1_target='local target="$(wildcard1 "$top_dir"/$target_filename)"'
37
alias check_wildcard_target_exists='wildcard1_target; check_target_exists'
38

    
39
# usage: set_make_vars; to_target cmd...
40
alias to_target='stdout="$target" to_file ' # last space alias-expands next word
41

    
42
make() # usage: make target...
43
{
44
	echo_func
45
	(
46
		# at verbosity < 4, hide messages about making included Makefiles
47
		# this can reduce # lines of output to 1/3 as much
48
		if test ! "$make_filter_active" && ! "log+" 3 can_log; then
49
			local_export make_filter_active=1
50
			local cmd="$(clog++ sys_cmd_path "$(self_name)")"; echo_vars cmd
51
			fd="$log_fd" clog++ filter_fd sed \
52
-e "\%^$cmd ([^-][^[:space:]]*)?Makefile%,/^make\[[[:digit:]]+\]: .*Makefile/d" \
53
-e                                       '/^make\[[[:digit:]]+\]: .*Makefile/d'
54
		fi
55
		time cmd_log_fd=1 self_sys $(if ! can_log; then echo '--silent ';fi)"$@"
56
	)
57
}
58

    
59
top_make() { echo_func; make --directory="$top_dir" "$@"; }
60

    
61
if false; then ## usage:
62
inline_make <<'EOF'
63
target:
64
	$(self_dir)/cmd >$@
65
EOF
66
# target will be run automatically because it's first in the makefile
67
fi ##
68
inline_make()
69
{
70
	echo_func
71
	local_export self="$(readlink -f "${BASH_SOURCE[1]}")"; echo_vars self
72
	local_export self_dir="$(dirname "$self")"; echo_vars self_dir
73
	
74
	make --makefile=<((
75
		cat # script stdin from caller-provided stdin
76
		echo -n "
77
.SUFFIXES: # turn off built-in suffix rules
78
.SECONDARY: # don't automatically delete intermediate files
79
.DELETE_ON_ERROR: # delete target if recipe fails
80
"
81
	)|echo_stdin) "$@" <&20 # make's stdin from global stdin
82
}
83

    
84
fi
(7-7/11)