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
log_local; log++ # don\'t display make vars at verbosity 2 to avoid clutter
24
local command="${FUNCNAME##*/}"; echo_vars command
25
local target_filename="${FUNCNAME%/*}"; echo_vars target_filename
26
local target="$top_dir/$target_filename"; echo_vars target
27
local target_stem="${target_filename%.*}"; echo_vars target_stem
28
local _remake="$remake" remake=; echo_vars _remake
29
log--
30
EOF
31
)" # `remake=`: don't progagate remake to prerequisites
32

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

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

    
43
# usage: begin_target; ...; with_rm target ... # progagates $rm/$remake
44
# **WARNING**: only works inside a runscript target that starts w/ begin_target
45
function with_rm() { remake="$_remake" "$@"; }
46
alias with_rm='"with_rm" ' # last space alias-expands next word
47

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

    
56
# usage: begin_target; to_target cmd...
57
alias to_target='stdout="$target" to_file ' # last space alias-expands next word
58

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

    
79
top_make() { echo_func; make --directory="$top_dir" "$@"; }
80

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

    
104
fi
(7-7/11)