Blame subr.ex/ex_pkg.subr

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
#
8b075b
# ex_pkg_copy_group_vars() - copy build group variables into build group
8b075b
# @_group_name_src:		single source build group name
8b075b
# @_group_name_dst:		single destination build group name
8b075b
# @[_build_vars_default]:	optional list of default build variables, defaults to ${DEFAULT_BUILD_VARS}
8b075b
#
8b075b
# Returns:			zero (0) on success, non-zero (>0) on failure.
8b075b
#
8b075b
ex_pkg_copy_group_vars() {
8b075b
	local	_epcgv_group_name_src="${1#\$}" _epcgv_group_name_dst="${2#\$}"	\
8b075b
		_epcgv_build_vars_default="${3:-${DEFAULT_BUILD_VARS}}"		\
8b075b
		_epcgv_group_name_dst_uc="" _epcgv_group_name_src_uc=""		\
8b075b
		_epcgv_vname="" _epcgv_vval="";
8b075b
	rtl_toupper2 \$_epcgv_group_name_dst \$_epcgv_group_name_dst_uc;
8b075b
	rtl_toupper2 \$_epcgv_group_name_src \$_epcgv_group_name_src_uc;
8b075b
8b075b
	for _epcgv_vname in ${_epcgv_build_vars_default}; do
8b075b
		if rtl_get_var_unsafe \$_epcgv_vval "${_epcgv_group_name_src_uc}_${_epcgv_vname}"\
8b075b
		&& [ "${_epcgv_vval:+1}" = 1 ]; then
8b075b
			rtl_set_var_unsafe "${_epcgv_group_name_dst_uc}_${_epcgv_vname}" "${_epcgv_vval}";
8b075b
		fi;
8b075b
	done;
8b075b
8b075b
	return 0;
8b075b
};
8b075b
8b075b
#
e9fa07
# ex_pkg_find_package() - find build group a single named package belongs to
7d702f
# @_rgroup_name:	out reference to variable of build group name
e9fa07
# @_group_names:	build group names
e9fa07
# @_pkg_name:		single named package
e9fa07
#
99f4d8
# Returns:		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
7d702f
# @_rdefault:		out reference to 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
80a5c9
# @_ldefault:		SP-separated list of default value names (any of: patches, patches_pre, vars_files)
fd38dc
#
99f4d8
# Returns:		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}"				\
80a5c9
		_epgd_default="" _epgd_group_name="" _epgd_patch_fname=""		\
80a5c9
		_epgd_pkg_name_full="" _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
				"${@}"											\
ebe7b3
				"${MIDIPIX_BUILD_PWD}/patches/${_epgd_pkg_name}/"*.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
80a5c9
		vars_files)
80a5c9
			rtl_get_var_unsafe \$_epgd_group_name -u "PKG_${_epgd_pkg_name}_GROUP";
80a5c9
			set --					\
80a5c9
				"vars/${_epgd_pkg_name}.vars"	\
80a5c9
				"vars.${_epgd_group_name}/${_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
7d702f
# @_rpkg_names:	out reference to variable of package names
e9fa07
# @_group_name:	build group name
e9fa07
#
99f4d8
# Returns:	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
#
34eeaf
# ex_pkg_get_package_vars() - get package variable names
34eeaf
# @_rpkg_vnames:		out reference to package variable names variable
34eeaf
# @_build_vars_default:		list of default build variables
34eeaf
# @_pkg_name:			single package name
34eeaf
#
34eeaf
# Returns:			zero (0) on success, non-zero (>0) on failure, list of package names on stdout on success.
34eeaf
#
34eeaf
ex_pkg_get_package_vars() {
34eeaf
	local	_epgpv_rpkg_vnames="${1#\$}" _epgpv_build_vars_default="${2}" _epgpv_pkg_name="${3}"	\
34eeaf
		_epgpv_pkg_name_uc="" _epgpv_vname="" _epgpv_vnames="";
34eeaf
	rtl_toupper2 \$_epgpv_pkg_name \$_epgpv_pkg_name_uc;
34eeaf
34eeaf
	for _epgpv_vname in ${_epgpv_build_vars_default}; do
34eeaf
		if eval [ \"\${PKG_${_epgpv_pkg_name_uc}_${_epgpv_vname}:+1}\" = 1 ]; then
34eeaf
			rtl_lconcat \$_epgpv_vnames "PKG_${_epgpv_pkg_name_uc}_${_epgpv_vname}";
34eeaf
		fi;
34eeaf
	done;
34eeaf
34eeaf
	eval ${_epgpv_rpkg_vnames}='${_epgpv_vnames}';
34eeaf
	return 0;
8b075b
};
34eeaf
34eeaf
#
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
#
99f4d8
# Returns:	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"	\
bf1a83
				../midipix_build.vars;
e9fa07
		do
e9fa07
			if [ -r "${_eplv_fname}" ]; then
e9fa07
				rtl_fileop source "${_eplv_fname}";
e9fa07
			fi;
e9fa07
		done;
bf1a83
bf1a83
		set +o noglob;
bf1a83
		for _eplv_fname in				\
fe0f0a
				./vars.env.d/*.env;
bf1a83
		do
bf1a83
			set -o noglob;
bf1a83
			if [ -r "${_eplv_fname}" ]; then
bf1a83
				rtl_fileop source "${_eplv_fname}";
bf1a83
			fi;
bf1a83
		done;
bf1a83
		set -o noglob;
e9fa07
	fi;
e9fa07
e9fa07
	return "${_eplv_rc}";
e9fa07
};
e9fa07
e9fa07
#
e9fa07
# ex_pkg_load_groups() - load all available build groups
7d702f
# @_rgroups:		out reference to variable of build groups
7d702f
# @_rgroups_noauoto:	optional out reference to variable of build groups not built automatically
e9fa07
#
99f4d8
# Returns:		zero (0) on success, non-zero (>0) on failure.
e9fa07
#
e9fa07
ex_pkg_load_groups() {
874146
	local	_eplg_rgroups="${1#\$}" _eplg_rgroups_noauto="${2#\$}"	\
874146
		_eplg_build_groups="" _eplg_build_groups_noauto="" _eplg_fname="";
e9fa07
874146
	EXP_PKG_REGISTER_GROUP_RGROUPS="${_eplg_rgroups}";
874146
	EXP_PKG_REGISTER_GROUP_RGROUPS_NOAUTO="${_eplg_rgroups_noauto}";
78240c
	for _eplg_fname in $(find ./groups.d -name *.group | sort); do
874146
		rtl_fileop source_with_fnamevar "${_eplg_fname}";
e9fa07
	done;
874146
	unset EXP_PKG_REGISTER_GROUP_RGROUPS;
874146
	unset EXP_PKG_REGISTER_GROUP_RGROUPS_NOAUTO;
e9fa07
b2c1ea
	for _eplg_fname in $(find ./groups.d -mindepth 2 -name *.package | sort); do
b2c1ea
		rtl_fileop source_with_fnamevar "${_eplg_fname}";
b2c1ea
	done;
b2c1ea
e9fa07
	return 0;
e9fa07
};
e9fa07
e9fa07
#
b2c1ea
# ex_pkg_register() - register single package
b2c1ea
# @_pkg_name:		single package name
b2c1ea
# @_fname:		pathname to file package is defined in, relative to midipix_build root
b2c1ea
# @[_group_name]:	optional build group name; inferred from @_fname if not specified
b2c1ea
#
b2c1ea
# Returns:		zero (0) on success, non-zero (>0) on failure.
b2c1ea
#
b2c1ea
ex_pkg_register() {
b2c1ea
	local	_epr_pkg_name="${1}" _epr_fname="${2}" _epr_group_name="${3:-}"	\
b2c1ea
		_epr_group_name_uc="" _epr_group_noautofl=0;
b2c1ea
b2c1ea
	if [ "${_epr_group_name:+1}" != 1 ]; then
b2c1ea
		_epr_group_name="${_epr_fname#./*/}";
b2c1ea
		_epr_group_name="${_epr_group_name%%/*}";
b2c1ea
		_epr_group_name="${_epr_group_name%.d}";
b2c1ea
		_epr_group_name="${_epr_group_name#*.}";
b2c1ea
	fi;
b2c1ea
b2c1ea
	rtl_toupper2 \$_epr_group_name \$_epr_group_name_uc;
b2c1ea
	rtl_lconcat "\$${_epr_group_name_uc}_PACKAGES" "${_epr_pkg_name}";
b2c1ea
	rtl_set_var_unsafe -u "PKG_${_epr_pkg_name}_GROUP" "${_epr_group_name}";
b2c1ea
	rtl_set_var_unsafe -u "PKG_${_epr_pkg_name}_GROUP_FNAME" "${_epr_fname}";
b2c1ea
b2c1ea
	return 0;
b2c1ea
};
b2c1ea
b2c1ea
#
874146
# ex_pkg_register_group() - register single group
874146
# @_group_name:					single group name
874146
# @_fname:					pathname to file group is defined in, relative to midipix_build root
cf8543
# @[_ownerfl]:					"owner" for groups that own their packages, "copy" for shorthand groups referring to packages from other groups
874146
# @[_autofl]:					"auto" for groups to build by default, "noauto" for optional groups only built when requested
874146
# ${EXP_PKG_REGISTER_GROUP_RGROUPS}:		inout reference to variable of build groups
874146
# ${EXP_PKG_REGISTER_GROUP_RGROUPS_NOAUTO}:	inout reference to variable of build groups only built when requested
874146
#
8b075b
# Returns:					zero (0) on success, non-zero (>0) on failure.
874146
#
874146
ex_pkg_register_group() {
cf8543
	local	_eprg_group_name="${1}" _eprg_fname="${2}"		\
cf8543
		_eprg_ownerfl="${3:-owner}" _eprg_autofl="${4:-auto}"	\
874146
		_eprg_pkg_name="" _eprg_pkg_names="" _eprg_rgroups="";
874146
874146
	case "${_eprg_autofl}" in
874146
	auto)	_eprg_rgroups="${EXP_PKG_REGISTER_GROUP_RGROUPS}"; ;;
874146
	noauto)	_eprg_rgroups="${EXP_PKG_REGISTER_GROUP_RGROUPS_NOAUTO}"; ;;
874146
	*)	;;
874146
	esac;
874146
	if ! rtl_lmatch "${_eprg_rgroups}" "${_eprg_group_name}"; then
874146
		rtl_lconcat "${_eprg_rgroups}" "${_eprg_group_name}";
874146
	fi;
874146
cf8543
	case "${_eprg_ownerfl}" in
cf8543
	owner)
cf8543
		if rtl_get_var_unsafe \$_eprg_pkg_names -u "${_eprg_group_name}_PACKAGES"\
cf8543
		&& [ "${_eprg_pkg_names:+1}" = 1 ]; then
874146
			for _eprg_pkg_name in ${_eprg_pkg_names}; do
874146
				rtl_set_var_unsafe -u "PKG_${_eprg_pkg_name}_GROUP" "${_eprg_group_name}";
874146
				rtl_set_var_unsafe -u "PKG_${_eprg_pkg_name}_GROUP_FNAME" "${_eprg_fname}";
874146
			done;
cf8543
		fi;
cf8543
		;;
cf8543
	copy)	;;
cf8543
	*)	;;
cf8543
	esac;
874146
874146
	return 0;
874146
};
874146
e9fa07
# vim:filetype=sh textwidth=0