e9fa07
#
8d7a8a
# Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 LucĂ­a Andrea Illanes Albornoz <lucia@luciaillanes.de>
e9fa07
# set +o errexit -o noglob -o nounset is assumed.
e9fa07
#
e9fa07
e9fa07
#
e9fa07
# ex_pkg_check_depends() - check single named package for unsatisfied dependencies
e9fa07
# @_checkfl:		enable (1) or inhibit (0) dependency expansion
e9fa07
# @_rpkg_disabled:	reference to in variable of list of disabled packages
e9fa07
# @_rpkg_finished:	reference to in variable of list of finished packages
e9fa07
# @_pkg_name:		single package name
e9fa07
# @_rpkg_names:		reference to in variable of list of package names
e9fa07
# @_workdir:		pathname to build-specific temporary directory
e9fa07
#
e9fa07
# Return:		zero (0) given no outstanding dependencies, non-zero (>0) otherwise
e9fa07
# Side effects:		${PKG_${_PKG_NAME}_DEPENDS_FULL} may be mutated
e9fa07
#
e9fa07
ex_pkg_check_depends() {
e9fa07
	local	_epcd_checkfl="${1}" _epcd_rpkg_disabled="${2}" _epcd_rpkg_finished="${3#\$}"	\
e9fa07
		_epcd_pkg_name="${4}" _epcd_rpkg_names="${5#\$}" _epcd_workdir="${6}"		\
e9fa07
		_epcd_dependfl=0 _epcd_depends="" _epcd_pkg_name_depend="";
e9fa07
e9fa07
	if [ "${_epcd_checkfl}" -eq 1 ]; then
e9fa07
		if ! rtl_get_var_unsafe \$_epcd_depends -u "PKG_"${_epcd_pkg_name}"_DEPENDS_FULL"\
e9fa07
		|| [ "${_epcd_depends:+1}" != 1 ];
e9fa07
		then
e9fa07
			if rtl_get_var_unsafe \$_epcd_depends -u "PKG_"${_epcd_pkg_name}"_DEPENDS";
e9fa07
			then
e9fa07
				eval PKG_"${_epcd_pkg_name}"_DEPENDS_FULL='$(rtl_uniq ${_epcd_depends})';
e9fa07
			else
e9fa07
				return 0;
e9fa07
			fi;
e9fa07
		fi;
e9fa07
e9fa07
		for _epcd_pkg_name_depend in ${_epcd_depends}; do
e9fa07
			if ! rtl_lmatch "${_epcd_rpkg_disabled}" "${_epcd_pkg_name_depend}"\
e9fa07
			&& ! ex_pkg_state_test2 "${_epcd_workdir}" "${_epcd_pkg_name_depend}" finish;
e9fa07
			then
e9fa07
				if ! rtl_lmatch "${_epcd_rpkg_names}" "${_epcd_pkg_name_depend}"; then
e9fa07
					rtl_log_msg "fatalexit" "${MSG_build_unknown_dep}" "${_epcd_pkg_name_depend}" "${_epcd_pkg_name}";
e9fa07
				else
e9fa07
					_epcd_dependfl=1; break;
e9fa07
				fi;
e9fa07
			fi;
e9fa07
		done;
e9fa07
	fi;
e9fa07
e9fa07
	return "${_epcd_dependfl}";
e9fa07
};
e9fa07
e9fa07
#
e9fa07
# ex_pkg_find_package() - find build group a single named package belongs to
e9fa07
# @_rgroup_name:	reference to out variable of build group name
e9fa07
# @_group_names:	build group names
e9fa07
# @_pkg_name:		single named package
e9fa07
#
e9fa07
# Return:		zero (0) on success, non-zero (>0) if package not found, group name on stdout if package was found.
e9fa07
#
e9fa07
ex_pkg_find_package() {
e9fa07
	local	_epfp_rgroup_name="${1#\$}" _epfp_group_names="${2}" _epfp_pkg_name="${3}"	\
e9fa07
		_epfp_foundfl=0 _epfp_group_name="" _epfp_pkg_names="";
e9fa07
e9fa07
	for _epfp_group_name in ${_epfp_group_names}; do
e9fa07
		if rtl_get_var_unsafe \$_epfp_pkg_names -u "${_epfp_group_name}_PACKAGES"\
e9fa07
		&& [ "${_epfp_pkg_names:+1}" = 1 ]\
e9fa07
		&& rtl_lmatch \$_epfp_pkg_names "${_epfp_pkg_name}"; then
e9fa07
			_epfp_foundfl=1; break;
e9fa07
		fi;
e9fa07
	done;
e9fa07
e9fa07
	case "${_epfp_foundfl:-0}" in
e9fa07
	0)	eval ${_epfp_rgroup_name}=;
e9fa07
		return 1; ;;
e9fa07
e9fa07
	1)	eval ${_epfp_rgroup_name}='${_epfp_group_name}';
e9fa07
		return 0; ;;
e9fa07
	esac;
e9fa07
};
e9fa07
e9fa07
#
fd38dc
# ex_pkg_get_default() - get single package default value
fd38dc
# @_rdefault:		reference to out variable of default value or "" on end of list
138059
# @_default_idx:	one-based single default value index
fd38dc
# @_pkg_name:		single package name
fd38dc
# @_pkg_version:	single package version
138059
# @_ldefault:		SP-separated list of default value names (any of: patches, patches_pre, vars_file)
fd38dc
#
fd38dc
# Return:		zero (0) on success, non-zero (>0) on invalid default value name or unknown package.
fd38dc
#
fd38dc
ex_pkg_get_default() {
138059
	local	_epgd_rdefault="${1#\$}" _epgd_default_idx="${2}" _epgd_pkg_name="${3}"	\
138059
		_epgd_pkg_version="${4}" _epgd_ldefault="${5}"				\
fd38dc
		_epgd_default="" _epgd_patch_fname="" _epgd_pkg_name_full=""		\
fd38dc
		_epgd_pkg_patches_extra="" _epgd_rc=0;
fd38dc
fd38dc
	set --;
fd38dc
	_epgd_pkg_name_full="${_epgd_pkg_name}${_epgd_pkg_version:+-${_epgd_pkg_version}}";
fd38dc
	_epgd_rc=0;
fd38dc
fd38dc
	for _epgd_default in ${_epgd_ldefault}; do
fd38dc
		case "${_epgd_default}" in
fd38dc
		patches)
138059
			rtl_get_var_unsafe \$_epgd_pkg_patches_extra -u "PKG_${_epgd_pkg_name}_PATCHES_EXTRA";
fd38dc
			set +o noglob;
fd38dc
			set --												\
fd38dc
				"${@}"											\
fd38dc
				"${MIDIPIX_BUILD_PWD}/patches/${_epgd_pkg_name_full}/"*.patch				\
fd38dc
				"${MIDIPIX_BUILD_PWD}/patches/${_epgd_pkg_name_full}.local.patch"			\
fd38dc
				"${MIDIPIX_BUILD_PWD}/patches/${_epgd_pkg_name_full}.local@${BUILD_HNAME}.patch"	\
fd38dc
				${_epgd_pkg_patches_extra};
fd38dc
			set -o noglob;
fd38dc
			;;
fd38dc
fd38dc
		patches_pre)
fd38dc
			set --										\
fd38dc
				"${@}"									\
fd38dc
				"${MIDIPIX_BUILD_PWD}/patches/${_epgd_pkg_name_full}_pre.local.patch"	\
fd38dc
				"${MIDIPIX_BUILD_PWD}/patches/${_epgd_pkg_name_full}_pre.local@${BUILD_HNAME}.patch";
fd38dc
			;;
fd38dc
fd38dc
		vars_file)
fd38dc
			set --	\
fd38dc
				"vars/${_epgd_pkg_name}.vars";
fd38dc
			;;
fd38dc
fd38dc
		*)
fd38dc
			_epgd_rc=1; break;
fd38dc
			;;
fd38dc
		esac;
fd38dc
	done;
fd38dc
fd38dc
	if [ "${_epgd_rc}" = 0 ]; then
fd38dc
		eval ${_epgd_rdefault}="\${${_epgd_default_idx}:-}";
fd38dc
	fi;
fd38dc
fd38dc
	return "${_epgd_rc}";
fd38dc
};
fd38dc
fd38dc
#
e9fa07
# ex_pkg_get_packages() - get list of packages belonging to single named build group
e9fa07
# @_rpkg_names:	reference to out variable of package names
e9fa07
# @_group_name:	build group name
e9fa07
#
e9fa07
# Return:	zero (0) on success, non-zero (>0) on failure, list of package names on stdout on success.
e9fa07
#
e9fa07
ex_pkg_get_packages() {
e9fa07
	local	_epgp_rpkg_names="${1#\$}" _epgp_group_name="${2}"	\
e9fa07
		_epgp_pkg_names="";
e9fa07
e9fa07
	if rtl_get_var_unsafe \$_epgp_pkg_names -u "${_epgp_group_name}_PACKAGES"\
e9fa07
	&& [ "${_epgp_pkg_names:+1}" = 1 ]; then
e9fa07
		eval ${_epgp_rpkg_names}='${_epgp_pkg_names}';
e9fa07
		return 0;
e9fa07
	else
e9fa07
		eval ${_epgp_rpkg_names}=;
e9fa07
		return 1;
e9fa07
	fi;
e9fa07
};
e9fa07
e9fa07
#
e9fa07
# ex_pkg_load_vars() - load build variables
f6f7f2
# @_rstatus:		out reference to status string
f6f7f2
# @_rbuild_arch:	in reference to build architecture
f6f7f2
# @_rbuild_kind:	in reference to build kind
e9fa07
#
e9fa07
# Return:	zero (0) on success, non-zero (>0) on failure, build variables post-return on success.
e9fa07
#
e9fa07
ex_pkg_load_vars() {
1aa81c
	local	_eplv_rstatus="${1#\$}" _eplv_rbuild_arch="${2#\$}" _eplv_rbuild_kind="${3#\$}"	\
f6f7f2
		_eplv_build_arch="" _eplv_rc=0 _eplv_fname="";
e9fa07
f6f7f2
	if ! rtl_lmatch "${_eplv_rbuild_arch}" "nt32 nt64"; then
e9fa07
		_eplv_rc=1;
f6f7f2
		rtl_setrstatus "${_eplv_rstatus}" 'invalid architecture \`${'"${_eplv_rbuild_arch}"'}'\''.';
f6f7f2
	elif ! rtl_lmatch "${_eplv_rbuild_kind}" "debug release"; then
e9fa07
		_eplv_rc=1;
f6f7f2
		rtl_setrstatus "${_eplv_rstatus}" 'unknown build type \`${'"${_eplv_rbuild_kind}"'}'\''.';
e9fa07
	else
f6f7f2
		eval _eplv_build_arch="\${${_eplv_rbuild_arch}}";
f6f7f2
		case "${_eplv_build_arch}" in
e9fa07
		nt32)	DEFAULT_TARGET="i686-nt32-midipix"; ;;
e9fa07
		nt64)	DEFAULT_TARGET="x86_64-nt64-midipix"; ;;
e9fa07
		esac;
e9fa07
6370be
		if [ "${PREFIX_ROOT:+1}" ]\
6370be
		&& [ "${PREFIX_ROOT#/}" = "${PREFIX_ROOT}" ]; then
6370be
			PREFIX_ROOT="${PWD%/}/${PREFIX_ROOT}";
6370be
		fi;
6370be
		if [ "${PREFIX:+1}" ]\
6370be
		&& [ "${PREFIX#/}" = "${PREFIX}" ]; then
1aa81c
			PREFIX="${PWD%/}/${PREFIX}";
1aa81c
		fi;
1aa81c
e9fa07
		for _eplv_fname in				\
e9fa07
				"${HOME}/midipix_build.vars"	\
e9fa07
				"${HOME}/.midipix_build.vars"	\
e9fa07
				../midipix_build.vars		\
e9fa07
				./midipix.env;
e9fa07
		do
e9fa07
			if [ -r "${_eplv_fname}" ]; then
e9fa07
				rtl_fileop source "${_eplv_fname}";
e9fa07
			fi;
e9fa07
		done;
e9fa07
	fi;
e9fa07
e9fa07
	return "${_eplv_rc}";
e9fa07
};
e9fa07
e9fa07
#
e9fa07
# ex_pkg_load_groups() - load all available build groups
e9fa07
# @_rgroups:		reference to out variable of build groups
e9fa07
# @_rgroups_noauoto:	optional reference to out variable of build groups not built automatically
e9fa07
# @_rgroup_auto:	reference to in variable of flag controlling whether to build group automatically
e9fa07
# @_rgroup_target:	reference to in variable of build group targets
e9fa07
#
e9fa07
# Return:		zero (0) on success, non-zero (>0) on failure.
e9fa07
#
e9fa07
ex_pkg_load_groups() {
e9fa07
	local	_eplg_rgroups="${1#\$}" _eplg_rgroups_noauto="${2#\$}"		\
e9fa07
		_eplg_rgroup_auto="${3#\$}" _eplg_rgroup_target="${4#\$}"	\
e9fa07
		_eplg_build_groups="" _eplg_build_groups_noauto=""		\
e9fa07
		_eplg_fname="" _eplg_group="" _eplg_groups="";
e9fa07
e9fa07
	for _eplg_fname in $(find ./groups -name *.group | sort); do
e9fa07
		rtl_fileop source_opt "${_eplg_fname}";
e9fa07
e9fa07
		if eval [ \"\${${_eplg_rgroup_target}:+1}\" = 1 ]; then
e9fa07
			eval _eplg_group=\"\${${_eplg_rgroup_target}}\";
e9fa07
			eval unset ${_eplg_rgroup_target};
e9fa07
		else
e9fa07
			_eplg_group="${_eplg_fname##*/}";
e9fa07
			_eplg_group="${_eplg_group%.group}";
e9fa07
			_eplg_group="${_eplg_group#*.}";
e9fa07
		fi;
e9fa07
e9fa07
		if ! rtl_lmatch \$_eplg_groups "${_eplg_group}"; then
e9fa07
			rtl_lconcat \$_eplg_groups "${_eplg_group}";
e9fa07
			if eval [ \"\${${_eplg_rgroup_auto}:+1}\" = 1 ]; then
e9fa07
				if eval [ \"\${${_eplg_rgroup_auto}:-0}\" -ne 0 ]; then
e9fa07
					rtl_lconcat \$_eplg_build_groups "${_eplg_group}";
e9fa07
				else
e9fa07
					rtl_lconcat \$_eplg_build_groups_noauto "${_eplg_group}";
e9fa07
				fi;
e9fa07
				eval unset ${_eplg_rgroup_auto};
e9fa07
			else
e9fa07
				rtl_lconcat \$_eplg_build_groups "${_eplg_group}";
e9fa07
			fi;
e9fa07
		fi;
e9fa07
	done;
e9fa07
e9fa07
	_eplg_build_groups="$(rtl_uniq "${_eplg_build_groups}")";
e9fa07
	eval ${_eplg_rgroups}=\"${_eplg_build_groups}\";
e9fa07
e9fa07
	if [ "${_eplg_rgroups_noauto:+1}" = 1 ]; then
e9fa07
		_eplg_build_groups_noauto="$(rtl_uniq "${_eplg_build_groups_noauto}")";
e9fa07
		eval ${_eplg_rgroups_noauto}=\"${_eplg_build_groups_noauto}\";
e9fa07
	fi;
e9fa07
e9fa07
	return 0;
e9fa07
};
e9fa07
e9fa07
#
e9fa07
# ex_pkg_unfold_depends() - unfold list of package names into dependency-expanded set of complete, disabled, finished, and outstanding package names
e9fa07
# @_rdisabled:		reference to inout variable of disabled packages
e9fa07
# @_rfinished:		reference to inout variable of finished packages
e9fa07
# @_rnames:		reference to out variable of package names
e9fa07
# @_checkfl:		enable (1) or inhibit (0) dependency expansion
e9fa07
# @_forcefl:		enable (1) or inhibit (0) forcibly rebuilding finished packages
e9fa07
# @_group_name:		build group name
e9fa07
# @_pkg_names:		list of package names
e9fa07
# @_restart:		optional whitespace-separated list of package names to rebuild
e9fa07
# @_test_finished:	only exclude disabled packages (0,) split finished packages
e9fa07
# @_workdir:		pathname to build-specific temporary directory
e9fa07
#
e9fa07
# Return:		zero (0) on success, non-zero (>0) on failure.
e9fa07
#
e9fa07
ex_pkg_unfold_depends() {
e9fa07
	local	_epud_rdisabled="${1#\$}" _epud_rfinished="${2#\$}" _epud_rnames="${3#\$}"	\
e9fa07
		_epud_checkfl="${4}" _epud_forcefl="${5}" _epud_group_name="${6}"		\
e9fa07
		_epud_pkg_names="${7}" _epud_restart="${8}" _epud_test_finished="${9}"		\
e9fa07
		_epud_workdir="${10}"								\
e9fa07
		_epud_pkg_disabled="" _epud_pkg_force="" _epud_pkg_name=""			\
e9fa07
		_epud_pkg_names_new=""	_epud_restartfl=0;
e9fa07
e9fa07
	if [ "${_epud_restart:+1}" = 1 ]\
e9fa07
	&& ! rtl_lmatch \$_epud_restart "ALL LAST"; then
e9fa07
		rtl_lsearch \$_epud_pkg_names "${_epud_restart}";
e9fa07
	fi;
e9fa07
	if [ "${_epud_restart:+1}" = 1 ]\
e9fa07
	&& [ "${_epud_checkfl:-0}" -eq 1 ]; then
e9fa07
		rtl_lunfold_depends 'PKG_${_rld_name}_DEPENDS' \$_epud_pkg_names ${_epud_pkg_names};
e9fa07
		_epud_pkg_names="$(rtl_uniq ${_epud_pkg_names})";
e9fa07
	fi;
e9fa07
e9fa07
	for _epud_pkg_name in ${_epud_pkg_names}; do
e9fa07
		if [ "${_epud_restart}" = "ALL" ]\
e9fa07
		|| rtl_lmatch \$_epud_restart "${_epud_pkg_name}"; then
e9fa07
			_epud_restartfl=1;
e9fa07
		else
e9fa07
			_epud_restartfl=0;
e9fa07
		fi;
e9fa07
e9fa07
		if rtl_get_var_unsafe \$_epud_pkg_disabled -u "PKG_${_epud_pkg_name}_DISABLED"\
e9fa07
		&& [ "${_epud_pkg_disabled}" = 1 ];
e9fa07
		then
e9fa07
			rtl_lconcat "${_epud_rdisabled}" "${_epud_pkg_name}";
e9fa07
e9fa07
		elif [ "${_epud_test_finished:-1}" -eq 1 ]\
e9fa07
		&&   ex_pkg_state_test2 "${_epud_workdir}" "${_epud_pkg_name}" finish\
e9fa07
		&&   [ "${_epud_restartfl:-0}" -eq 0 ]\
e9fa07
		&&   [ "${_epud_forcefl:-0}" -ne 1 ]\
e9fa07
		&&   rtl_get_var_unsafe \$_epud_pkg_force -u "${_epud_group_name}_FORCE"\
e9fa07
		&&   [ "${_epud_pkg_force}" != 1 ];
e9fa07
		then
e9fa07
			rtl_lconcat "${_epud_rfinished}" "${_epud_pkg_name}";
e9fa07
e9fa07
		else
e9fa07
			rtl_lconcat \$_epud_pkg_names_new "${_epud_pkg_name}";
e9fa07
		fi;
e9fa07
	done;
e9fa07
e9fa07
	eval ${_epud_rdisabled}='$(rtl_uniq2 "${_epud_rdisabled}")';
e9fa07
	eval ${_epud_rfinished}='$(rtl_uniq2 "${_epud_rfinished}")';
e9fa07
	eval ${_epud_rnames}='$(rtl_uniq "${_epud_pkg_names_new}")';
e9fa07
e9fa07
	return 0;
e9fa07
};
e9fa07
e9fa07
#
e9fa07
# ex_pkg_unfold_rdepends() - unfold list of package names into reverse dependency-expanded set of complete, disabled, finished, and outstanding package names
e9fa07
# @_rdisabled:		reference to inout variable of disabled packages
e9fa07
# @_rfinished:		reference to inout variable of finished packages
e9fa07
# @_rnames:		reference to out variable of package names
e9fa07
# @_group_name:		build group name
e9fa07
# @_pkg_names:		list of package names
e9fa07
# @_restart:		optional whitespace-separated list of package names to rebuild
e9fa07
# @_test_finished:	only exclude disabled packages (0,) split finished packages
e9fa07
# @_workdir:		pathname to build-specific temporary directory
e9fa07
#
e9fa07
# Return:		zero (0) on success, non-zero (>0) on failure.
e9fa07
#
e9fa07
ex_pkg_unfold_rdepends() {
e9fa07
	local	_epur_rdisabled="${1#\$}" _epur_rfinished="${2#\$}" _epur_rnames="${3#\$}"	\
e9fa07
		_epur_group_name="${4}" _epur_pkg_names="${5}" _epur_restart="${6}"		\
e9fa07
		_epur_test_finished="${7}" _epur_workdir="${8}"					\
e9fa07
		_epur_depends="" _epur_disabled=0 _epur_force=0 _epur_pkg_depends=""		\
e9fa07
		_epur_pkg_name="" _epur_pkg_names_new="" _epur_pkg_name_depend=""		\
e9fa07
		_epur_pkg_rdepends="";
e9fa07
e9fa07
	for _epur_pkg_name_depend in ${_epur_restart}; do
e9fa07
		for _epur_pkg_name in ${_epur_pkg_names}; do
e9fa07
			if [ "${_epur_pkg_name}" = "${_epur_pkg_name_depend}" ]; then
e9fa07
				continue;
e9fa07
e9fa07
			elif rtl_get_var_unsafe \$_epur_depends -u "PKG_"${_epur_pkg_name}"_DEPENDS"\
e9fa07
			  && rtl_lunfold_depends 'PKG_${_rld_name}_DEPENDS' \$_epur_pkg_depends ${_epur_depends}\
e9fa07
			  && [ "${_epur_pkg_depends:+1}" = 1 ]\
e9fa07
			  && rtl_lmatch \$_epur_pkg_depends "${_epur_pkg_name_depend}";
e9fa07
			then
e9fa07
				if rtl_get_var_unsafe \$_epur_disabled -u "PKG_${_epur_pkg_name}_DISABLED"\
e9fa07
				&& [ "${_epur_disabled}" = 1 ];
e9fa07
				then
e9fa07
					rtl_lconcat "${_epur_rdisabled}" "${_epur_pkg_name}";
e9fa07
e9fa07
				elif [ "${_epur_test_finished}" -eq 1 ]\
e9fa07
				&&   ex_pkg_state_test2 "${_epur_workdir}" "${_epur_pkg_name}" finish\
e9fa07
				&&   rtl_get_var_unsafe \$_epur_force -u "${_epur_group_name}_FORCE"\
e9fa07
				&&   [ "${_epur_force}" != 1 ];
e9fa07
				then
e9fa07
					rtl_lconcat "${_epur_rfinished}" "${_epur_pkg_name}";
e9fa07
e9fa07
				elif [ "${_epur_test_finished:-1}" -eq 0 ]\
e9fa07
				||   ! ex_pkg_state_test2 "${_epur_workdir}" "${_epur_pkg_name}" finish\
e9fa07
				||   ( rtl_get_var_unsafe \$_epur_force -u "${_epur_group_name}_FORCE"\
e9fa07
				&&   [ "${_epur_force}" = 1 ] );
e9fa07
				then
e9fa07
					rtl_lconcat \$_epur_pkg_names_new "${_epur_pkg_name}";
e9fa07
				fi;
e9fa07
			fi;
e9fa07
		done;
e9fa07
	done;
e9fa07
e9fa07
	eval ${_epur_rdisabled}='$(rtl_uniq2 "${_epur_rdisabled}")';
e9fa07
	eval ${_epur_rfinished}='$(rtl_uniq2 "${_epur_rfinished}")';
e9fa07
	eval ${_epur_rnames}='$(rtl_uniq "${_epur_pkg_names_new}")';
e9fa07
e9fa07
	return 0;
e9fa07
};
e9fa07
e9fa07
# vim:filetype=sh textwidth=0