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
alias echo_target='log-- echo_func' # display by default
19

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

    
31
# usage: target() { begin_target; use $target...; }
32
alias begin_target='echo_target; echo_func; set_make_vars' # echo all targets
33

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

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

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

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

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

    
73
top_make() { echo_func; make --directory="$top_dir" "$@"; }
74

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

    
98
fi
(7-7/11)