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

    
9
# usage: target_filename/command() { echo_func; set_make_vars; use $target...; }
10
alias set_make_vars="$(cat <<'EOF'
11
local command="${FUNCNAME##*/}"; echo_vars command
12
local target_filename="${FUNCNAME%/*}"; echo_vars target_filename
13
local target="$top_dir/$target_filename"; echo_vars target
14
local target_stem="${target_filename%.*}"; echo_vars target_stem
15
local _remake="$remake" remake=; echo_vars _remake
16
EOF
17
)" # `remake=`: don't progagate remake to prerequisites
18

    
19
# usage: remaking || check if exists
20
# cmd line usage: [remake=1] func
21
alias remaking='test "$_remake"'
22

    
23
# usage: set_make_vars; ...; self_make overridden_target... # progagates $remake
24
function self_make() { remake="$_remake" "$@"; }
25
alias self_make='"self_make" ' # last space alias-expands next word
26

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

    
35
# usage: set_make_vars; to_target cmd...
36
alias to_target='stdout="$target" to_file ' # last space alias-expands next word
37

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

    
55
top_make() { echo_func; make --directory="$top_dir" "$@"; }
56

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

    
80
fi
(7-7/11)