summaryrefslogtreecommitdiffhomepage
path: root/subr/ex_pkg_dispatch.subr
blob: fea64c8b10b2890d6ca174ef51fcbc69630e61cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#
# set -o noglob is assumed.
# WARNING: ex_pkg_dispatch(), its caller, and its callers must _NOT_ be executed
# as part of conditional evaluation, e.g. if, until, while, !, or && and ||, as
# doing so would inhibit set -o errexit during execution of this function and its
# subshell(!).  Instead, call ex_pkg_dispatch() and subsequently evaluate ${?}.
#

exp_pkg_dispatch() {
	local _pkg_name="${1}" _tgt_name="${2}" _restart_at="${3}"				\
		_dispatch_fn="${4}" _stderrout_path="${5}" _pipe_path="${6}"			\
		_njobs_vname="${7}" _pkg_name_uc;
	_pkg_name_uc="$(ex_rtl_toupper "${_pkg_name}")";
	if [ -n "$(ex_rtl_get_var_unsafe PKG_${_pkg_name_uc}_DISABLED)" ]; then
		"${_dispatch_fn}" disabled_pkg "${_pkg_name}" "${_tgt_name}";
	elif ex_pkg_state_test "${_pkg_name}" finish\
	&& [ -z "${_restart_at}" ]; then
		"${_dispatch_fn}" skipped_pkg "${_pkg_name}" "${_tgt_name}";
	else
		_stderrout_path="${BUILD_WORKDIR}/${_pkg_name}_stderrout.log";
		"${_dispatch_fn}" start_pkg "${_pkg_name}" "${_tgt_name}";
		(set -o errexit -o noglob; BUILD_IS_PARENT=0;
		ex_pkg_env "${_pkg_name}" "${_tgt_name}" "${_restart_at}";
		trap "if [ \${?} -eq 0 ]; then							\
			echo \"done ${_pkg_name} ${_tgt_name}\" >&3;				\
		      else									\
			echo \"fail ${_pkg_name} ${_tgt_name}\" >&3;				\
		      fi;" EXIT HUP INT TERM USR1 USR2;
		ex_pkg_exec "${_pkg_name}" "${_tgt_name}" "${_restart_at}"			\
			"${_dispatch_fn}";) 1>"${_stderrout_path}" 2>&1 3>"${_pipe_path}" &
		: $((${_njobs_vname}+=1));
	fi;
};

ex_pkg_dispatch() {
	local _tgt_name="${1}" _restart="${2}" _restart_at="${3}"				\
		_dispatch_fn="${4}" _tgt_name_uc						\
		_pkg_names _njob _njobs _njobs_max						\
		_pipe_path _stderrout_path _pipe_msg _script_rc;
	ex_rtl_fileop mkdir "${BUILD_WORKDIR}";
	_pipe_path="${BUILD_WORKDIR}/build.fifo";
	_tgt_name_uc="$(ex_rtl_toupper "${_tgt_name}")";
	"${_dispatch_fn}" start_target "" "${_tgt_name}";
	_pkg_names="$(ex_rtl_get_var_unsafe ${_tgt_name_uc}_PACKAGES)";
	if [ -n "${_restart}" ]\
	&& [ "${_restart}" != ALL ]; then
		_pkg_names="$(ex_rtl_lfilter "${_pkg_names}" "${_restart}")";
	fi;
	if [ "$(ex_rtl_get_var_unsafe ${_tgt_name_uc}_PARALLELISE)" = 1 ]\
	&& [ "${ARG_PARALLEL:-0}" -gt 1 ]; then
		_njobs_max="${DEFAULT_BUILD_CPUS}";
	else
		_njobs_max=1;
	fi;
	set -- ${_pkg_names};
	while [ ${#} -gt 0 ]; do
		_script_rc=0; _njobs=0;
		ex_rtl_fileop mkfifo "${_pipe_path}";
		for _njob in $(seq 1 $((${_njobs_max}-${_njobs}))); do
			if [ ${#} -eq 0 ]; then
				break;
			else
				exp_pkg_dispatch "${1}" "${_tgt_name}"				\
					"${_restart_at}" "${_dispatch_fn}"			\
					"${_stderrout_path}" "${_pipe_path}"			\
					_njobs; shift;
			fi;
		done;
		if [ "${_njobs:-0}" -gt 0 ]; then
			while read _pipe_msg; do
			case "${_pipe_msg%% *}" in
			done)	"${_dispatch_fn}" finish_pkg ${_pipe_msg#done };
				: $((_njobs-=1));
				if [ "${_script_rc:-0}" -eq 0 ]; then
					for _njob in $(seq 1 $((${_njobs_max}-${_njobs}))); do
						if [ ${#} -eq 0 ]; then
							break;
						else
							exp_pkg_dispatch "${1}" "${_tgt_name}"		\
								"${_restart_at}" "${_dispatch_fn}"	\
								"${_stderrout_path}" "${_pipe_path}"	\
								_njobs; shift;
						fi;
					done;
				fi;
				if [ "${_njobs:-0}" -eq 0 ]; then
					break;
				fi; ;;
			fail)	_script_rc=1;
				"${_dispatch_fn}" fail_pkg ${_pipe_msg#fail };
				[ $((_njobs-=1)) -eq 0 ] && break; ;;
			step)	"${_dispatch_fn}" step_pkg ${_pipe_msg#step }; ;;
			esac; done <>"${_pipe_path}";
		fi;
		ex_rtl_fileop rm "${_pipe_path}";
		if [ "${_script_rc:-1}" -eq 1 ]; then
			return "${_script_rc}";
		fi;
	done;
	"${_dispatch_fn}" finish_target "" "${_tgt_name}";
};

# vim:filetype=sh