Blob Blame History Raw
#
# set +o errexit -o noglob -o nounset is assumed.
#

#
# exp_pkg_dispatch_complete() - XXX
# @_dispatch_fn:	top-level dispatch function name
# @_group_names:	build group name(s)
# @_pkg_disabled:	list of disabled packages
# @_pkg_finished:	list of finished packages
#
# Return:		zero (0) on success, non-zero (>0) on failure.
#
exp_pkg_dispatch_complete() {
	local _dispatch_fn="${1}" _group_name="${2}" _pkg_disabled="${3}" _pkg_finished="${4}" _pkg_name="";
	for _pkg_name in ${_pkg_disabled}; do
		"${_dispatch_fn}" disabled_pkg "${_group_name}" "${_pkg_name}";
	done;
	for _pkg_name in ${_pkg_finished}; do
		"${_dispatch_fn}" skipped_pkg "${_group_name}" "${_pkg_name}";
	done;
};

#
# exp_pkg_dispatch_expand_packages() - expand build group name to list of packages ordered and filtered according to dependency and restart constraints
# @_checkfl:		enable (1) or inhibit (0) dependency expansion
# @_forcefl:		enable (1) or inhibit (0) forcibly rebuilding finished packages
# @_group_name:		build group name
# @_restart:		optional whitespace-separated list of package names to rebuild
# @_reversefl:		unfold reverse dependencies (1) or dependencies (0)
#
# Return:		zero (0) on success, non-zero (>0) on failure, ${EX_PKG_DISABLED}, ${EX_PKG_FINISHED}, and ${EX_PKG_NAMES} set post-return.
#
exp_pkg_dispatch_expand_packages() {
	local	_checkfl="${1}" _forcefl="${2}" _group_name="${3}" _restart="${4}" _reversefl="${5}"\
		_pkg_names=""; EX_PKG_DISABLED=""; EX_PKG_FINISHED=""; EX_PKG_NAMES="";
	if _pkg_names="$(rtl_get_var_unsafe -u "${_group_name}_PACKAGES")"\
	&& [ -n "${_pkg_names}" ]; then
		if [ "${_reversefl:-0}" -eq 0 ]; then
			ex_pkg_unfold_depends "${_checkfl}" "${_forcefl}" "${_group_name}" "${_pkg_names}" "${_restart}" 1;
		else	ex_pkg_unfold_rdepends "${_group_name}" "${_pkg_names}" "${_restart}" 1;
		fi;
	fi;
	return 0;
};

#
# exp_pkg_dispatch_group() - dispatch a single build group
# @_build_steps_default:	list of default build steps
# @_build_vars_default:		list of default build variables
# @_checkfl:			enable (1) or inhibit (0) dependency expansion
# @_dispatch_fn:		top-level dispatch function name
# @_group_name:			build group name
# @_njobs_max:			maximum count of simultaneous jobs
# @_pipe_path:			pathname to build FIFO
# @_restart_at:			optional comma-separated list of build steps at which to rebuild or ALL or LAST
# @_workdir:			pathname to build-specific temporary directory
#
# Return:			zero (0) on success, non-zero (>0) on failure, ${EXP_PKG_DISPATCH_COUNT_CUR} may be mutated post-return.
#
exp_pkg_dispatch_group() {
	local	_build_steps_default="${1}" _build_vars_default="${2}" _checkfl="${3}"\
		_dispatch_fn="${4}" _group_name="${5}" _njobs_max="${6}" _pipe_path="${7}"\
		_restart_at="${8}" _workdir="${9}" _perc_group=0 _perc_pkg=0 _pipe_msg=""\
		_pkg_name="" _rc=0;
	rtl_fileop mkfifo "${_pipe_path}";
	while true; do
		while [ "${EXP_PKG_DISPATCH_NJOBS:-0}" -gt 0 ] && read _pipe_msg; do
		case "${_pipe_msg%% *}" in
		done)	_pkg_name="${_pipe_msg#done * }"; : $((EXP_PKG_DISPATCH_COUNT_CUR+=1)); : $((EXP_PKG_DISPATCH_NJOBS-=1));
			EX_PKG_FINISHED="$(rtl_lconcat "${EX_PKG_FINISHED}" "${_pkg_name}")";
			_perc_group="$(rtl_percentage "${EXP_PKG_DISPATCH_GROUP_CUR}" "${EXP_PKG_DISPATCH_GROUP_MAX}")";
			_perc_pkg="$(rtl_percentage "${EXP_PKG_DISPATCH_COUNT_CUR}" "${EXP_PKG_DISPATCH_COUNT_MAX}")";
			"${_dispatch_fn}" finish_pkg ${_pipe_msg#done } "${EXP_PKG_DISPATCH_COUNT_MAX}" "${_perc_group}" "${_perc_pkg}";
			EX_PKG_NAMES="$(rtl_lfilter "${EX_PKG_NAMES}" "${_pkg_name}")";
			EX_PKG_DISPATCH_WAIT="$(rtl_lfilter "${EX_PKG_DISPATCH_WAIT}" "${_pkg_name}")";
			if [ -n "${EX_PKG_NAMES}" ] && [ "${_rc}" -eq 0 ]; then
				if [ "${EXP_PKG_DISPATCH_NJOBS}" -ne "${_njobs_max}" ]; then
					exp_pkg_dispatch_packages "${_build_steps_default}"	\
						"${_build_vars_default}" "${_checkfl}"		\
						"${_dispatch_fn}" "${_group_name}"		\
						"${_njobs_max}" "${_pipe_path}"			\
						"${EX_PKG_DISABLED}" "${EX_PKG_FINISHED}"	\
						"${_restart_at}" "${_workdir}";
				fi;
			elif [ "${EXP_PKG_DISPATCH_NJOBS:-0}" -eq 0 ]; then
				break;
			fi; ;;
		fail)	: $((EXP_PKG_DISPATCH_NJOBS-=1)); _rc=1;
			"${_dispatch_fn}" fail_pkg ${_pipe_msg#fail } "${EXP_PKG_DISPATCH_COUNT_MAX}"; ;;
		msg_pkg)
			"${_dispatch_fn}" msg_pkg ${_pipe_msg#msg_pkg }; ;;
		step)	"${_dispatch_fn}" step_pkg ${_pipe_msg#step }; ;;
		esac; done <>"${_pipe_path}";
		if [ -n "${EX_PKG_NAMES}" ] && [ "${_rc}" -eq 0 ]; then
			if [ "${EXP_PKG_DISPATCH_NJOBS}" -ne "${_njobs_max}" ]; then
				exp_pkg_dispatch_packages "${_build_steps_default}"		\
					"${_build_vars_default}" "${_checkfl}"			\
					"${_dispatch_fn}" "${_group_name}"			\
					"${_njobs_max}" "${_pipe_path}"				\
					"${EX_PKG_DISABLED}" "${EX_PKG_FINISHED}"		\
					"${_restart_at}" "${_workdir}";
			fi;
		elif [ "${EXP_PKG_DISPATCH_NJOBS:-0}" -eq 0 ]; then
			break;
		fi;
	done;
	rtl_fileop rm "${_pipe_path}";
	return "${_rc}";
};

#
# exp_pkg_dispatch_package() - dispatch single named packages
# @_build_steps_default:	list of default build steps
# @_build_vars_default:		list of default build variables
# @_dispatch_fn:		top-level dispatch function name
# @_group_name:			build group name
# @_pkg_name:			single package name
# @_restart_at:			optional comma-separated list of build steps at which to rebuild or ALL or LAST
# @_workdir:			pathname to build-specific temporary directory
#
# Return:			zero (0) on success, non-zero (>0) on failure, ${EXP_PKG_DISPATCH_NJOBS}, ${EXP_PKG_DISPATCH_COUNT}, ${EX_PKG_NAMES}, and ${EX_PKG_DISPATCH_WAIT} may be mutated post-return.
#
exp_pkg_dispatch_package() {
	local	_build_steps_default="${1}" _build_vars_default="${2}" _dispatch_fn="${3}"\
		_group_name="${4}" _pkg_name="${5}" _restart_at="${6}" _workdir="${7}"\
		_perc_group=0 _perc_pkg=0;
	_perc_group="$(rtl_percentage "${EXP_PKG_DISPATCH_GROUP_CUR}" "${EXP_PKG_DISPATCH_GROUP_MAX}")";
	_perc_pkg="$(rtl_percentage "${EXP_PKG_DISPATCH_COUNT_CUR}" "${EXP_PKG_DISPATCH_COUNT_MAX}")";
	if "${_dispatch_fn}" start_pkg "${_group_name}" "${_pkg_name}" "$((${EXP_PKG_DISPATCH_COUNT}+1))" "${EXP_PKG_DISPATCH_COUNT_MAX}" "${_perc_group}" "${_perc_pkg}"; then
		: $((EXP_PKG_DISPATCH_NJOBS+=1)); : $((EXP_PKG_DISPATCH_COUNT+=1)); EX_PKG_DISPATCH_WAIT="$(rtl_lconcat "${EX_PKG_DISPATCH_WAIT}" "${_pkg_name}")";
		(trap "if [ \${?} -eq 0 ]; then											\
			printf \"done %s %s %d\n\" \"${_group_name}\" \"${_pkg_name}\" \"${EXP_PKG_DISPATCH_COUNT}\" >&3;	\
		      else													\
			printf \"fail %s %s %d\n\" \"${_group_name}\" \"${_pkg_name}\" \"${EXP_PKG_DISPATCH_COUNT}\" >&3;	\
			pkill -U "${$}";											\
		      fi;" EXIT HUP INT TERM USR1 USR2;
		set +o errexit -o noglob -o nounset; BUILD_IS_PARENT=0; rtl_log_set_fname ""; rtl_log_set_no_attr 1;
		if ex_pkg_env "${_build_steps_default}" "${_build_vars_default}"		\
				"${_group_name}" 0 "${_pkg_name}" "${_restart_at}" "${_workdir}"; then
			ex_pkg_exec "${_dispatch_fn}" "${_group_name}" "${_pkg_name}" "${_restart_at}";
		else
			return 1;
		fi;) 1>"${_workdir}/${_pkg_name}_stderrout.log" 2>&1 3>"${_pipe_path}" &
	else
		return 1;
	fi;
};

#
# exp_pkg_dispatch_packages() - dispatch set of packages
# @_build_steps_default:	list of default build steps
# @_build_vars_default:		list of default build variables
# @_checkfl:			enable (1) or inhibit (0) dependency expansion
# @_dispatch_fn:		top-level dispatch function name
# @_group_name:			build group name
# @_njobs_max:			maximum count of simultaneous jobs
# @_pipe_path:			pathname to parent-child process FIFO
# @_pkg_disabled:		list of disabled packages
# @_pkg_finished:		list of finished packages
# @_restart_at:			optional comma-separated list of build steps at which to rebuild or ALL or LAST
# @_workdir:			pathname to build-specific temporary directory
#
# Return:			zero (0) on success, non-zero (>0) on failure, ${EXP_PKG_DISPATCH_NJOBS}, ${EXP_PKG_DISPATCH_COUNT}, ${EX_PKG_NAMES}, and ${EX_PKG_DISPATCH_WAIT} may be mutated post-return.
#
exp_pkg_dispatch_packages() {
	local	_build_steps_default="${1}" _build_vars_default="${2}" _checkfl="${3}"\
		_dispatch_fn="${4}" _group_name="${5}" _njobs_max="${6}" _pipe_path="${7}"\
		_pkg_disabled="${8}" _pkg_finished="${9}" _restart_at="${10}" _workdir="${11}"\
		_foundfl=0 _njob=0 _pkg_depends="" _pkg_name="";
	while [ "${EXP_PKG_DISPATCH_NJOBS:-0}" -lt "${_njobs_max}" ]; do
		_foundfl=0;
		for _pkg_name in ${EX_PKG_NAMES}; do
			if ! rtl_lmatch "${_pkg_disabled}" "${_pkg_name}"\
			&& ! rtl_lmatch "${_pkg_finished}" "${_pkg_name}"\
			&& ! rtl_lmatch "${EX_PKG_DISPATCH_WAIT}" "${_pkg_name}"\
			&& ex_pkg_check_depends "${_checkfl}" "${_pkg_disabled}" "${_pkg_finished}"	\
					"${_pkg_name}" "${EX_PKG_NAMES}"; then
				exp_pkg_dispatch_package "${_build_steps_default}"			\
					"${_build_vars_default}" "${_dispatch_fn}"			\
					"${_group_name}" "${_pkg_name}" "${_restart_at}"		\
					"${_workdir}"; _foundfl=1; break;
			fi;
		done;
		if [ "${_foundfl:-0}" -eq 0 ]; then
			break;
		fi;
	done;
};

#
# ex_pkg_dispatch() - dispatch a set of build group
# @_build_steps_default:	list of default build steps
# @_build_vars_default:		list of default build variables
# @_dispatch_fn:		top-level dispatch function name
# @_group_names:		build group name(s)
# @_groups_inhibit_deps:	inhibit group-group dependency expansion
# @_njobs_max:			maximum count of simultaneous jobs
# @_pipe_path:			pathname to build FIFO
# @_restart:			optional whitespace-separated list of package names to rebuild
# @_restart_at:			optional comma-separated list of build steps at which to rebuild or ALL or LAST
# @_restart_recursive:		optional flag specifiying either no dependency expansion (0,) dependency expansion (1,) dependency expansion and forcibly rebuild (2,) forcibly rebuild reverse dependencies (3.)
# @_workdir:			pathname to build-specific temporary directory
#
# Return:			zero (0) on success, non-zero (>0) on failure, ${EX_PKG_DISPATCH_WAIT} mutated post-return.
#
ex_pkg_dispatch() {
	local	_build_steps_default="${1}" _build_vars_default="${2}" _dispatch_fn="${3}"		\
		_group_names="${4}" _groups_inhibit_deps="${5}" _njobs_max="${6}" _pipe_path="${7}"	\
		_restart="${8}" _restart_at="${9}" _restart_recursive="${10}" _workdir="${11}"		\
		_checkfl=1 _forcefl=0 _perc_group=0 _pkg_name="" _pkg_names="" _rc=0 _reversefl=0	\
		EX_PKG_DISABLED EX_PKG_FINISHED EX_PKG_NAMES EXP_PKG_DISPATCH_COUNT			\
		EXP_PKG_DISPATCH_COUNT_CUR EXP_PKG_DISPATCH_COUNT_MAX EXP_PKG_DISPATCH_GROUP_CUR	\
		EXP_PKG_DISPATCH_GROUP_MAX EXP_PKG_DISPATCH_NJOBS; EX_PKG_DISPATCH_WAIT="";
	case "${_groups_inhibit_deps:-0}" in
	0)	_group_names="$(rtl_uniq $(rtl_lunfold_depends '${_name}_GROUP_DEPENDS' ${_group_names}))";
	esac;
	if [ -n "${_restart}" ]; then
		case "${_restart_recursive:-0}" in
		0)	_checkfl=0; _forcefl=0; _reversefl=0; ;;
		1)	_checkfl=1; _forcefl=0; _reversefl=0; ;;
		2)	_checkfl=1; _forcefl=1; _reversefl=0; ;;
		3)	_checkfl=1; _forcefl=1; _reversefl=1; ;;
		esac;
	fi;
	EXP_PKG_DISPATCH_GROUP_CUR=0; EXP_PKG_DISPATCH_GROUP_MAX="$(rtl_llength "${_group_names}")";
	for _group_name in ${_group_names}; do
		EX_PKG_DISABLED=""; EX_PKG_DISPATCH_WAIT=""; EX_PKG_FINISHED=""; EX_PKG_NAMES="";
		EXP_PKG_DISPATCH_COUNT=0; EXP_PKG_DISPATCH_COUNT_CUR=0; EXP_PKG_DISPATCH_COUNT_MAX=0; EXP_PKG_DISPATCH_NJOBS=0;
		_perc_group="$(rtl_percentage "${EXP_PKG_DISPATCH_GROUP_CUR}" "${EXP_PKG_DISPATCH_GROUP_MAX}")";
		if "${_dispatch_fn}" start_group "${_group_name}" "" "${EXP_PKG_DISPATCH_GROUP_CUR}" "${EXP_PKG_DISPATCH_GROUP_MAX}" "${_perc_group}"; then
			if rtl_fileop mkdir "${_workdir}"\
			&& rtl_log_msg "verbose" "${MSG_build_resolving_deps}" "${_group_name}"\
			&& exp_pkg_dispatch_expand_packages "${_checkfl}" "${_forcefl}" "${_group_name}" "${_restart}" "${_reversefl}"\
			&& exp_pkg_dispatch_complete "${_dispatch_fn}" "${_group_name}" "${EX_PKG_DISABLED}" "${EX_PKG_FINISHED}"\
			&& rtl_log_msg "verbose" "${MSG_build_resolved_deps}" "${_group_name}"\
			&& EXP_PKG_DISPATCH_COUNT_MAX="$(rtl_llength "${EX_PKG_NAMES}")"\
			&& [ "${EXP_PKG_DISPATCH_COUNT_MAX}" -gt 0 ]; then
				_pkg_names="$(rtl_lconcat "${_pkg_names}" "${EX_PKG_NAMES}")";
				exp_pkg_dispatch_group "${_build_steps_default}"			\
					"${_build_vars_default}" "${_checkfl}" "${_dispatch_fn}"	\
					"${_group_name}" "${_njobs_max}" "${_pipe_path}"		\
					"${_restart_at}" "${_workdir}"; _rc="${?}";
			fi;
			: $((EXP_PKG_DISPATCH_GROUP_CUR+=1));
			_perc_group="$(rtl_percentage "${EXP_PKG_DISPATCH_GROUP_CUR}" "${EXP_PKG_DISPATCH_GROUP_MAX}")";
			"${_dispatch_fn}" finish_group "${_group_name}" "" "${EXP_PKG_DISPATCH_GROUP_CUR}" "${EXP_PKG_DISPATCH_GROUP_MAX}" "${_perc_group}";
			if [ "${_rc}" -ne 0 ]; then
				break;
			fi;
		fi;
	done; return "${_rc}";
};

# vim:filetype=sh textwidth=0