Project

General

Profile

1
#!/bin/bash -e
2
if false; then #### make target template:
3
target()
4
{
5
	begin_target
6
	#...
7
}
8
fi ####
9

    
10
. "$(dirname "${BASH_SOURCE[0]}")"/util.sh
11

    
12
if self_not_included; then
13

    
14
if isset rm; then : "${remake:=$rm}"; fi #mnemonic: files are rm'd (overwritten)
15
unset rm # don't allow rm to override remake if an invoked script uses this file
16
export remake # propagate to invoked commands by default
17

    
18
# helper alias; use begin_target instead
19
# usage: target_filename/command() { echo_func; set_make_vars; use $target...; }
20
alias set_make_vars="$(cat <<'EOF'
21
local command="${FUNCNAME##*/}"; echo_vars command
22
local target_filename="${FUNCNAME%/*}"; echo_vars target_filename
23
local target="$top_dir/$target_filename"; echo_vars target
24
local target_stem="${target_filename%.*}"; echo_vars target_stem
25
local _remake="$remake" remake=; echo_vars _remake
26
EOF
27
)" # `remake=`: don't progagate remake to prerequisites
28

    
29
# usage: target() { begin_target; use $target...; }
30
alias begin_target='echo_func; set_make_vars'
31

    
32
# usage: set_make_vars; ...; remaking || check if exists
33
# cmd line usage: [remake=1] func
34
# **WARNING**: you *MUST* use set_make_vars at the beginning of any function
35
# that uses this, so that $_remake is properly set to $remake and not left at
36
# its previous value
37
alias remaking='test "$_remake"'
38

    
39
# usage: set_make_vars; ...; self_make overridden_target... # progagates $remake
40
function self_make() { remake="$_remake" "$@"; }
41
alias self_make='"self_make" ' # last space alias-expands next word
42

    
43
# usage: set_make_vars; check_target_exists
44
alias check_target_exists='remaking || require_not_exists "$target" || return 0'
45
alias deferred_check_target_exists='remaking || declare if_not_exists=1'
46
	# defer check until to_file
47
alias check_fake_target_exists='deferred_check_target_exists'
48
alias wildcard1_target='local target="$(wildcard1 "$top_dir"/$target_filename)"'
49
alias check_wildcard_target_exists='wildcard1_target; check_target_exists'
50

    
51
# usage: set_make_vars; to_target cmd...
52
alias to_target='stdout="$target" to_file ' # last space alias-expands next word
53

    
54
make() # usage: make target...
55
{
56
	echo_func
57
	(
58
		# at verbosity < 4, hide messages about making included Makefiles
59
		# this can reduce # lines of output to 1/3 as much
60
		if test ! "$make_filter_active" && ! "log+" 3 can_log; then
61
			local_export make_filter_active=1
62
			local cmd="$(clog++ sys_cmd_path "$(self_name)")"; echo_vars cmd
63
			fd="$log_fd" clog++ filter_fd sed \
64
-e "\%^$cmd ([^-][^[:space:]]*)?Makefile%,/^make\[[[:digit:]]+\]: .*Makefile/d" \
65
-e                                       '/^make\[[[:digit:]]+\]: .*Makefile/d'
66
		fi
67
		time cmd_log_fd=1 self_sys $(if ! can_log; then echo '--silent ';fi)"$@"
68
	)
69
}
70

    
71
top_make() { echo_func; make --directory="$top_dir" "$@"; }
72

    
73
if false; then ## usage:
74
inline_make <<'EOF'
75
target:
76
	$(self_dir)/cmd >$@
77
EOF
78
# target will be run automatically because it's first in the makefile
79
fi ##
80
inline_make()
81
{
82
	echo_func
83
	local_export self="$(readlink -f "${BASH_SOURCE[1]}")"; echo_vars self
84
	local_export self_dir="$(dirname "$self")"; echo_vars self_dir
85
	
86
	make --makefile=<((
87
		cat # script stdin from caller-provided stdin
88
		echo -n "
89
.SUFFIXES: # turn off built-in suffix rules
90
.SECONDARY: # don't automatically delete intermediate files
91
.DELETE_ON_ERROR: # delete target if recipe fails
92
"
93
	)|echo_stdin) "$@" <&20 # make's stdin from global stdin
94
}
95

    
96
fi
(7-7/11)