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

#
# ex_pkg_check_depends() - check single named package for unsatisfied dependencies
# @_checkfl:		enable (1) or inhibit (0) dependency expansion
# @_pkg_disabled:	list of disabled packages
# @_pkg_finished:	list of finished packages
# @_pkg_name:		single package name
# @_pkg_names:		list of package names
#
# Return:		zero (0) given no outstanding dependencies, non-zero (>0) otherwise
#
ex_pkg_check_depends() {
	local	_checkfl="${1}" _pkg_disabled="${2}" _pkg_finished="${3}" _pkg_name="${4}" _pkg_names="${5}"\
		_dependfl=0 _pkg_depends="" _pkg_name_depend="";
	if [ "${_checkfl:-0}" -eq 1 ]\
	&& _pkg_depends="$(rtl_uniq $(rtl_lunfold_depends 'PKG_${_name}_DEPENDS' $(rtl_get_var_unsafe -u "PKG_"${_pkg_name}"_DEPENDS")))"; then
		for _pkg_name_depend in $(rtl_uniq ${_pkg_depends}); do
			if ! rtl_lmatch "${_pkg_disabled}" "${_pkg_name_depend}"\
			&& ! rtl_lmatch "${_pkg_finished}" "${_pkg_name_depend}"; then
				if ! rtl_lmatch "${_pkg_names}" "${_pkg_name_depend}"; then
					rtl_log_msg "fatalexit" "${MSG_build_unknown_dep}" "${_pkg_name_depend}" "${_pkg_name}";
				else
					_dependfl=1; break;
				fi;
			fi;
		done;
	fi;
	return "${_dependfl}";
};

#
# ex_pkg_find_package() - find build group a single named package belongs to
# @_group_names:	build group names
# @_pkg_name:		single named package
#
# Return:		zero (0) on success, non-zero (>0) if package not found, group name on stdout if package was found.
#
ex_pkg_find_package() {
	local _group_names="${1}" _pkg_name="${2}" _foundfl=0 _group_name="" _pkg_names="";
	for _group_name in ${_group_names}; do
		if _pkg_names="$(rtl_get_var_unsafe -u "${_group_name}_PACKAGES")"\
		&& [ -n "${_pkg_names}" ]\
		&& rtl_lmatch "${_pkg_names}" "${_pkg_name}"; then
			_foundfl=1; break;
		fi;
	done;
	case "${_foundfl:-0}" in
	0)	return 1; ;;
	1)	printf "%s" "${_group_name}"; return 0; ;;
	esac;
};

#
# ex_pkg_get_packages() - get list of packages belonging to single named build group
# @_group_name:	build group name
#
# Return:	zero (0) on success, non-zero (>0) on failure, list of package names on stdout on success.
#
ex_pkg_get_packages() {
	local _group_name="${1}" _pkg_names="";
	if _pkg_names="$(rtl_get_var_unsafe -u "${_group_name}_PACKAGES")"\
	&& [ -n "${_pkg_names}" ]; then
		printf "%s" "${_pkg_names}"; return 0;
	else
		return 1;
	fi;
};

#
# ex_pkg_load_dump() - load package dump
# @_pkg_name:	package name
#
# Return:	zero (0) on success, non-zero (>0) on failure, package dump post-return on success.
#
ex_pkg_load_dump() {
	local _pkg_name="${1}" _workdir="${2}" _rc=0; _status="";
	if [ ! -e "${_workdir}/${_pkg_name}.dump" ]; then
		rtl_log_msg "warning" "${MSG_pkgtool_no_env_dump}" "${_pkg_name}" "${_workdir}";
		rtl_log_msg "info" "${MSG_pkgtool_rebuilding_pkg}" "${_pkg_name}";
		(export	ARCH BUILD_KIND BUILD_DLCACHEDIR BUILD_WORKDIR		\
			PREFIX PREFIX_CROSS PREFIX_MINGW32 PREFIX_MINIPIX	\
			PREFIX_NATIVE PREFIX_ROOT PREFIX_RPM;
		./build.sh --dump-in _build -P -r "${_pkg_name}" -v);
		if [ ! -e "${_workdir}/${_pkg_name}.dump" ]; then
			_rc=1; _status="Error: failed to locate environment dump for package \`${_pkg_name}' in \`${_workdir}'.";
		fi;
	else
		_rc=0;
	fi;
	if [ "${_rc:-0}" -eq 0 ]\
	&& ! . "${_workdir}/${_pkg_name}.dump"; then
		_rc=1; _status="Error: failed to source environment dump for package \`${_pkg_name}' from \`${_workdir}'.";
	elif [ "${_rc:-0}" -eq 0 ]\
	&&   ! rtl_fileop cd "${PKG_BUILD_DIR}"; then
		_rc=1; _status="Error: failed to change working directory to \`${PKG_BUILD_DIR}'.";
	fi; return "${_rc}";
};

#
# ex_pkg_load_vars() - load build variables
#
# Return:	zero (0) on success, non-zero (>0) on failure, build variables post-return on success.
#
ex_pkg_load_vars() {
	local _rc=0 _fname=""; _status="";
	if ! rtl_lmatch "${ARCH}" "nt32 nt64"; then
		_rc=1; _status="Error: invalid architecture \`${ARCH}'.";
	elif ! rtl_lmatch "${BUILD_KIND}" "debug release"; then
		_rc=1; _status="Error: unknown build type \`${BUILD_KIND}'.";
	else	case "${ARCH}" in
		nt32)	DEFAULT_TARGET="i686-nt32-midipix"; ;;
		nt64)	DEFAULT_TARGET="x86_64-nt64-midipix"; ;;
		esac;
		for _fname in	\
				"${HOME}/midipix_build.vars"	\
				"${HOME}/.midipix_build.vars"	\
				../midipix_build.vars		\
				./midipix.env; do
			if [ -r "${_fname}" ]; then
				rtl_fileop source "${_fname}";
			fi;
		done;
		if [ -z "${PREFIX}" ]; then
			_rc=1; _status="Error: \${PREFIX} empty or unset.";
		fi;
	fi; return "${_rc}";
};

#
# ex_pkg_load_groups() - load all available build groups
#
# Return:	zero (0) on success, non-zero (>0) on failure, build groups loaded and ${EX_PKG_BUILD_GROUPS} and ${EX_PKG_BUILD_GROUPS_NOAUTO} set post-return.
#
ex_pkg_load_groups() {
	local  _build_groups="" _build_groups_noauto="" _fname="" _group="" _groups="";
	for _fname in $(find ./groups -name *.group | sort); do
		rtl_fileop source_opt "${_fname}";
		if [ -n "${GROUP_TARGET:-}" ]; then
			_group="${GROUP_TARGET}"; unset GROUP_TARGET;
		else
			_group="${_fname##*/}"; _group="${_group%.group}"; _group="${_group#*.}";
		fi;
		if ! rtl_lmatch "${_groups}" "${_group}"; then
			_groups="$(rtl_lconcat "${_groups}" "${_group}")";
			if [ -n "${GROUP_AUTO:-}" ]; then
				if [ "${GROUP_AUTO:-0}" -ne 0 ]; then
					_build_groups="$(rtl_lconcat "${_build_groups}" "${_group}")";
				else
					_build_groups_noauto="$(rtl_lconcat "${_build_groups_noauto}" "${_group}")";
				fi;
				unset GROUP_AUTO;
			else
				_build_groups="$(rtl_lconcat "${_build_groups}" "${_group}")";
			fi;
		fi;
	done;
	EX_PKG_BUILD_GROUPS="$(rtl_uniq "${_build_groups}")";
	EX_PKG_BUILD_GROUPS_NOAUTO="$(rtl_uniq "${_build_groups_noauto}")";
};

#
# ex_pkg_unfold_depends() - unfold list of package names into dependency-expanded set of complete, disabled, finished, and outstanding package names
# @_checkfl:		enable (1) or inhibit (0) dependency expansion
# @_forcefl:		enable (1) or inhibit (0) forcibly rebuilding finished packages
# @_group_name:		build group name
# @_pkg_names:		list of package names
# @_restart:		optional whitespace-separated list of package names to rebuild
# @_test_finished:	only exclude disabled packages from ${EX_PKG_NAMES} (0,) split finished packages into ${EX_PKG_FINISHED}
#
# Return:		zero (0) on success, non-zero (>0) on failure, ${EX_PKG_DISABLED}, ${EX_PKG_FINISHED}, and ${EX_PKG_NAMES} set post-return.
#
ex_pkg_unfold_depends() {
	local	_checkfl="${1}" _forcefl="${2}" _group_name="${3}" _pkg_names="${4}" _restart="${5}" _test_finished="${6}"\
		_pkg_name="" _restartfl=0;
	if [ -n "${_restart}" ] && ! rtl_lmatch "${_restart}" "ALL LAST"; then
		_pkg_names="$(rtl_lsearch "${_pkg_names}" "${_restart}")";
	fi;
	if [ -n "${_restart}" ] && [ "${_checkfl:-0}" -eq 1 ]; then
		_pkg_names="$(rtl_uniq $(rtl_lunfold_depends 'PKG_${_name}_DEPENDS' ${_pkg_names}))";
	fi;
	for _pkg_name in ${_pkg_names}; do
		if [ "${_restart}" = "ALL" ]\
		|| rtl_lmatch "${_restart}" "${_pkg_name}"; then
			_restartfl=1;
		else
			_restartfl=0;
		fi;
		if [ "x$(rtl_get_var_unsafe -u "PKG_${_pkg_name}_DISABLED")" = "x1" ]; then
			EX_PKG_DISABLED="$(rtl_lconcat "${EX_PKG_DISABLED}" "${_pkg_name}")";
			_pkg_names="$(rtl_lfilter "${_pkg_names}" "${_pkg_name}")";
		elif [ "${_test_finished:-1}" -eq 1 ]\
		&&   ex_pkg_state_test "${_pkg_name}" finish\
		&&   [ "${_restartfl:-0}" -eq 0 ]\
		&&   [ "${_forcefl:-0}" -ne 1 ]\
		&&   [ "x$(rtl_get_var_unsafe -u "${_group_name}_FORCE")" != "x1" ]; then
			EX_PKG_FINISHED="$(rtl_lconcat "${EX_PKG_FINISHED}" "${_pkg_name}")";
			_pkg_names="$(rtl_lfilter "${_pkg_names}" "${_pkg_name}")";
		fi;
	done;
	EX_PKG_DISABLED="$(rtl_uniq ${EX_PKG_DISABLED})";
	EX_PKG_FINISHED="$(rtl_uniq ${EX_PKG_FINISHED})";
	EX_PKG_NAMES="$(rtl_uniq ${_pkg_names})";
};

#
# ex_pkg_unfold_rdepends() - unfold list of package names into reverse dependency-expanded set of complete, disabled, finished, and outstanding package names
# @_group_name:		build group name
# @_pkg_names:		list of package names
# @_restart:		optional whitespace-separated list of package names to rebuild
# @_test_finished:	only exclude disabled packages from ${EX_PKG_NAMES} (0,) split finished packages into ${EX_PKG_FINISHED}
#
# Return:		zero (0) on success, non-zero (>0) on failure, ${EX_PKG_DISABLED}, ${EX_PKG_FINISHED}, and ${EX_PKG_NAMES} set post-return.
#
ex_pkg_unfold_rdepends() {
	local	_group_name="${1}" _pkg_names="${2}" _restart="${3}" _test_finished="${4}"\
		_pkg_depends="" _pkg_name="" _pkg_name_depend="" _pkg_rdepends="";
	for _pkg_name_depend in ${_restart}; do
		for _pkg_name in ${_pkg_names}; do
			if [ "${_pkg_name}" != "${_pkg_name_depend}" ]\
			&& [ "x$(rtl_get_var_unsafe -u "PKG_${_pkg_name}_DISABLED")" != "x1" ]\
			&& _pkg_depends="$(rtl_lunfold_depends 'PKG_${_name}_DEPENDS' $(rtl_get_var_unsafe -u "PKG_"${_pkg_name}"_DEPENDS"))"\
			&& [ -n "${_pkg_depends}" ]\
			&& rtl_lmatch "${_pkg_depends}" "${_pkg_name_depend}"; then
				_pkg_rdepends="$(rtl_lconcat "${_pkg_rdepends}" "${_pkg_name}")";
			fi;
		done;
	done;
	_pkg_names="";
	for _pkg_name in ${_pkg_rdepends}; do
		if _pkg_depends="$(rtl_lunfold_depends 'PKG_${_name}_DEPENDS' $(rtl_get_var_unsafe -u "PKG_"${_pkg_name}"_DEPENDS"))"\
		&& [ -n "${_pkg_depends}" ]; then
			for _pkg_name_depend in ${_pkg_depends}; do
				if [ "x$(rtl_get_var_unsafe -u "PKG_${_pkg_name_depend}_DISABLED")" = "x1" ]; then
					EX_PKG_DISABLED="$(rtl_lconcat "${EX_PKG_DISABLED}" "${_pkg_name_depend}")";
				elif [ "${_test_finished:-1}" -eq 1 ]\
				&&   ex_pkg_state_test "${_pkg_name_depend}" finish\
				&&   [ "x$(rtl_get_var_unsafe -u "${_group_name}_FORCE")" != "x1" ]\
				&&   ! rtl_lmatch "${_pkg_rdepends}" "${_pkg_name_depend}"; then
					EX_PKG_FINISHED="$(rtl_lconcat "${EX_PKG_FINISHED}" "${_pkg_name_depend}")";
				elif [ "${_test_finished:-1}" -eq 0 ]\
				||   ! ex_pkg_state_test "${_pkg_name_depend}" finish\
				||   [ "x$(rtl_get_var_unsafe -u "${_group_name}_FORCE")" = "x1" ]; then
					_pkg_names="$(rtl_lconcat "${_pkg_names}" "${_pkg_name_depend}")";
				fi;
			done;
		fi;
		_pkg_names="$(rtl_lconcat "${_pkg_names}" "${_pkg_name}")";
	done;
	EX_PKG_DISABLED="$(rtl_uniq ${EX_PKG_DISABLED})";
	EX_PKG_FINISHED="$(rtl_uniq ${EX_PKG_FINISHED})";
	EX_PKG_NAMES="$(rtl_uniq ${_pkg_names})";
};

#
# ex_pkg_unfold_rdepends_direct() - unfold list of package names into direct reverse dependency-expanded set of disabled and outstanding package names
# @_group_name:		build group name
# @_pkg_names:		list of package names
# @_restart:		optional whitespace-separated list of package names to rebuild
#
# Return:		zero (0) on success, non-zero (>0) on failure, ${EX_PKG_DISABLED} and ${EX_PKG_RDEPENDS_DIRECT} set post-return.
#
ex_pkg_unfold_rdepends_direct() {
	local	_group_name="${1}" _pkg_names="${2}" _restart="${3}"\
		_pkg_depends="" _pkg_disabled="" _pkg_name="" _pkg_name_depend="" _pkg_rdepends="";
	for _pkg_name_depend in ${_restart}; do
		for _pkg_name in ${_pkg_names}; do
			if [ "${_pkg_name}" != "${_pkg_name_depend}" ]\
			&& _pkg_depends="$(rtl_get_var_unsafe -u "PKG_${_pkg_name}_DEPENDS")"\
			&& [ -n "${_pkg_depends}" ]\
			&& rtl_lmatch "${_pkg_depends}" "${_pkg_name_depend}"; then
				if [ "x$(rtl_get_var_unsafe -u "PKG_${_pkg_name}_DISABLED")" != "x1" ]; then
					_pkg_rdepends="$(rtl_lconcat "${_pkg_rdepends}" "${_pkg_name}")";
				else
					_pkg_disabled="$(rtl_lconcat "${_pkg_disabled}" "${_pkg_name}")";
				fi;
			fi;
		done;
	done;
	EX_PKG_DISABLED="$(rtl_uniq ${_pkg_disabled})";
	EX_PKG_RDEPENDS_DIRECT="$(rtl_uniq ${_pkg_rdepends})";
};

# vim:filetype=sh textwidth=0