midipix / build / midipix_build

Forked from build/midipix_build 4 years ago
Clone

Blame subr/pkgtool_init.subr

Lucio Andrés Illanes Albornoz 8a27f9
#
Lucio Andrés Illanes Albornoz 8a27f9
# set +o errexit -o noglob is assumed.
Lucio Andrés Illanes Albornoz 8a27f9
#
Lucio Andrés Illanes Albornoz 8a27f9
Lucio Andrés Illanes Albornoz 8a27f9
pkgtoolp_init_defaults() {
Lucio Andrés Illanes Albornoz 8a27f9
	: ${ARCH:="nt64"}; : ${BUILD:="debug"}; : ${PKG_NAME:=""};
Lucio Andrés Illanes Albornoz 8a27f9
	: ${BUILD_WORKDIR:=""}; : ${PREFIX=""};
Lucio Andrés Illanes Albornoz 60fba6
	ARG_INFO=0; ARG_RESTART_AT=""; ARG_RDEPENDS=0;
Lucio Andrés Illanes Albornoz 60fba6
	ARG_UPDATE_DIFF=0; ARG_SHELL=0; ARG_TARBALL=0;
Lucio Andrés Illanes Albornoz 60fba6
	BUILD_GROUPS="";
Lucio Andrés Illanes Albornoz 60fba6
};
Lucio Andrés Illanes Albornoz 60fba6
Lucio Andrés Illanes Albornoz 60fba6
pkgtoolp_init_dump() {
Lucio Andrés Illanes Albornoz 60fba6
	local _rc=0; _status="";
Lucio Andrés Illanes Albornoz 60fba6
	if [ -n "${ARG_RESTART_AT}" ]\
Lucio Andrés Illanes Albornoz 60fba6
	|| [ "${ARG_UPDATE_DIFF:-0}" -eq 1 ]\
Lucio Andrés Illanes Albornoz 60fba6
	|| [ "${ARG_SHELL:-0}" -eq 1 ]; then
Lucio Andrés Illanes Albornoz 60fba6
		if [ ! -e "${BUILD_WORKDIR}/${PKG_NAME}.dump" ]; then
Lucio Andrés Illanes Albornoz 60fba6
			rtl_log_msg warn "Warning: failed to locate environment dump for package \`${PKG_NAME}' in \`${BUILD_WORKDIR}'.";
Lucio Andrés Illanes Albornoz 60fba6
			rtl_log_msg info "Rebuilding package \`${PKG_NAME}' w/ --dump-in build...";
Lucio Andrés Illanes Albornoz 60fba6
			(export	ARCH BUILD						\
Lucio Andrés Illanes Albornoz 60fba6
				BUILD_DLCACHEDIR BUILD_WORKDIR				\
Lucio Andrés Illanes Albornoz 60fba6
				PREFIX PREFIX_CROSS PREFIX_MINGW32 PREFIX_MINIPIX	\
Lucio Andrés Illanes Albornoz 60fba6
				PREFIX_NATIVE PREFIX_ROOT PREFIX_RPM;
Lucio Andrés Illanes Albornoz 60fba6
			./build.sh -a "${ARCH}" -b "${BUILD}" --dump-in build -P -r "${PKG_NAME}" -v);
Lucio Andrés Illanes Albornoz 60fba6
			if [ ! -e "${BUILD_WORKDIR}/${PKG_NAME}.dump" ]; then
Lucio Andrés Illanes Albornoz 60fba6
				_rc=1; _status="Error: failed to locate environment dump for package \`${PKG_NAME}' in \`${BUILD_WORKDIR}'.";
Lucio Andrés Illanes Albornoz 60fba6
			fi;
Lucio Andrés Illanes Albornoz 60fba6
		else
Lucio Andrés Illanes Albornoz 60fba6
			_rc=0;
Lucio Andrés Illanes Albornoz 60fba6
		fi;
Lucio Andrés Illanes Albornoz 60fba6
		if [ "${_rc:-0}" -eq 0 ]\
Lucio Andrés Illanes Albornoz 60fba6
		&& ! . "${BUILD_WORKDIR}/${PKG_NAME}.dump"; then
Lucio Andrés Illanes Albornoz 60fba6
			_rc=1; _status="Error: failed to source environment dump for package \`${PKG_NAME}' from \`${BUILD_WORKDIR}'.";
Lucio Andrés Illanes Albornoz 60fba6
		elif [ "${_rc:-0}" -eq 0 ]\
Lucio Andrés Illanes Albornoz 60fba6
		&&   ! rtl_fileop cd "${PKG_BUILD_DIR}"; then
Lucio Andrés Illanes Albornoz 60fba6
			_rc=1; _status="Error: failed to change working directory to \`${PKG_BUILD_DIR}'.";
Lucio Andrés Illanes Albornoz 60fba6
		fi;
Lucio Andrés Illanes Albornoz 60fba6
	fi; return "${_rc}";
Lucio Andrés Illanes Albornoz 8a27f9
};
Lucio Andrés Illanes Albornoz 8a27f9
Lucio Andrés Illanes Albornoz 8a27f9
pkgtoolp_init_env() {
Lucio Andrés Illanes Albornoz 8a27f9
	local _fname="" _rc=0; _status="";
Lucio Andrés Illanes Albornoz 8a27f9
	if ! umask 022; then
Lucio Andrés Illanes Albornoz 8a27f9
		printf "Error: failed to setup environment.\n"; exit 1;
Lucio Andrés Illanes Albornoz 8a27f9
	else	for _fname in $(find subr -name *.subr); do
Lucio Andrés Illanes Albornoz 8a27f9
			if ! . "${_fname}"; then
Lucio Andrés Illanes Albornoz 8a27f9
				printf "Error: failed to source \`%s'.\n" "${_fname}"; exit 1;
Lucio Andrés Illanes Albornoz 8a27f9
			fi;
Lucio Andrés Illanes Albornoz 8a27f9
		done; fi;
Lucio Andrés Illanes Albornoz 8a27f9
	return "${_rc}";
Lucio Andrés Illanes Albornoz 8a27f9
};
Lucio Andrés Illanes Albornoz 8a27f9
Lucio Andrés Illanes Albornoz 8a27f9
pkgtoolp_init_getopts() {
Lucio Andrés Illanes Albornoz 8a27f9
	local _opt="" _shiftfl=0 _rc=0 OPTIND=0; _status="";
Lucio Andrés Illanes Albornoz 8a27f9
	while [ "${#}" -gt 0 ]; do
Lucio Andrés Illanes Albornoz 8a27f9
		case "${1}" in
Lucio Andrés Illanes Albornoz 8a27f9
		--update-diff)
Lucio Andrés Illanes Albornoz 8a27f9
			ARG_UPDATE_DIFF=1; _shiftfl=1; ;;
Lucio Andrés Illanes Albornoz 8a27f9
		--restart-at)
Lucio Andrés Illanes Albornoz 8a27f9
			if [ "${#}" -lt 2 ]\
Lucio Andrés Illanes Albornoz 8a27f9
			|| [ -z "${2}" ]; then
Lucio Andrés Illanes Albornoz 8a27f9
				_rc=1; _status="Error: missing argument for option --restart-at.";
Lucio Andrés Illanes Albornoz 8a27f9
			else
Lucio Andrés Illanes Albornoz 8a27f9
				ARG_RESTART_AT="${2}"; _shiftfl=2;
Lucio Andrés Illanes Albornoz 8a27f9
			fi; ;;
Lucio Andrés Illanes Albornoz 8a27f9
		*)	_shiftfl=0; ;;
Lucio Andrés Illanes Albornoz 8a27f9
		esac;
Lucio Andrés Illanes Albornoz 8a27f9
		if [ "${_rc:-0}" -ne 0 ]; then
Lucio Andrés Illanes Albornoz 8a27f9
			break;
Lucio Andrés Illanes Albornoz 8a27f9
		elif [ "${_shiftfl:-0}" -gt 0 ]; then
Lucio Andrés Illanes Albornoz 8a27f9
			shift "${_shiftfl}"; continue;
Lucio Andrés Illanes Albornoz 60fba6
		elif getopts a:b:hirst _opt; then
Lucio Andrés Illanes Albornoz 8a27f9
			case "${_opt}" in
Lucio Andrés Illanes Albornoz 8a27f9
			a)	ARCH="${OPTARG}"; ;;
Lucio Andrés Illanes Albornoz 8a27f9
			b)	BUILD="${OPTARG}"; ;;
Lucio Andrés Illanes Albornoz 60fba6
			h)	cat etc/pkgtool.usage; exit 0; ;;
Lucio Andrés Illanes Albornoz 60fba6
			i)	ARG_INFO=1; ;;
Lucio Andrés Illanes Albornoz 60fba6
			r)	ARG_RDEPENDS=1; ;;
Lucio Andrés Illanes Albornoz 60fba6
			s)	ARG_SHELL=1; ;;
Lucio Andrés Illanes Albornoz 60fba6
			t)	ARG_TARBALL=1; ;;
Lucio Andrés Illanes Albornoz 60fba6
			*)	cat etc/pkgtool.usage; exit 1; ;;
Lucio Andrés Illanes Albornoz 8a27f9
			esac; shift $((${OPTIND}-1)); OPTIND=1;
Lucio Andrés Illanes Albornoz 8a27f9
		else
Lucio Andrés Illanes Albornoz 8a27f9
			break;
Lucio Andrés Illanes Albornoz 8a27f9
		fi;
Lucio Andrés Illanes Albornoz 8a27f9
	done;
Lucio Andrés Illanes Albornoz 8a27f9
	if [ "${_rc}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz 60fba6
		if [ "$((${ARG_INFO:-0} + ${ARG_RDEPENDS:-0} + ${ARG_SHELL:-0} + ${ARG_TARBALL:-0}))" -gt 1 ]; then
Lucio Andrés Illanes Albornoz 60fba6
			cat etc/pkgtool.usage; rtl_log_msg failexit "Error: only one of -i, -r, -s, or -t must be specified.";
Lucio Andrés Illanes Albornoz 60fba6
		elif [ "$((${ARG_INFO:-0} + ${ARG_RDEPENDS:-0} + ${ARG_SHELL:-0} + ${ARG_TARBALL:-0}))" -eq 0 ]; then
Lucio Andrés Illanes Albornoz 60fba6
			if [ -z "${ARG_RESTART_AT}" ]\
Lucio Andrés Illanes Albornoz 60fba6
			&& [ "${ARG_UPDATE_DIFF:-0}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz 60fba6
				cat etc/pkgtool.usage; rtl_log_msg failexit "Error: one of -i, -r, -s, or -t must be specified.";
Lucio Andrés Illanes Albornoz 60fba6
			fi;
Lucio Andrés Illanes Albornoz 60fba6
		fi;
Lucio Andrés Illanes Albornoz 8a27f9
		while [ "${#}" -gt 0 ]; do
Lucio Andrés Illanes Albornoz 8a27f9
		case "${1}" in
Lucio Andrés Illanes Albornoz 8a27f9
		*=*)	rtl_set_var_unsafe "${1%%=*}" "${1#*=}"; ;;
Lucio Andrés Illanes Albornoz 8a27f9
		*)	if [ "${#}" -ne 1 ]; then
Lucio Andrés Illanes Albornoz 8a27f9
				_rc=1; _status="Error: invalid argument \`${1}'.";
Lucio Andrés Illanes Albornoz 8a27f9
			fi; break; ;;
Lucio Andrés Illanes Albornoz 8a27f9
		esac; shift; done;
Lucio Andrés Illanes Albornoz 8a27f9
		if [ "${_rc:-0}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz 8a27f9
			if [ "${#}" -ne 1 ]\
Lucio Andrés Illanes Albornoz 8a27f9
			&& [ -z "${PKG_NAME}" ]; then
Lucio Andrés Illanes Albornoz 8a27f9
				_rc=1; _status="Error: missing package name.";
Lucio Andrés Illanes Albornoz 8a27f9
			elif [ "${#}" -eq 1 ]; then
Lucio Andrés Illanes Albornoz 8a27f9
				PKG_NAME="${1}";
Lucio Andrés Illanes Albornoz 8a27f9
			fi;
Lucio Andrés Illanes Albornoz 8a27f9
		fi;
Lucio Andrés Illanes Albornoz 8a27f9
	fi;
Lucio Andrés Illanes Albornoz 8a27f9
	return "${_rc}";
Lucio Andrés Illanes Albornoz 8a27f9
};
Lucio Andrés Illanes Albornoz 8a27f9
Lucio Andrés Illanes Albornoz 60fba6
pkgtoolp_init_groups() {
Lucio Andrés Illanes Albornoz 60fba6
	local _default_build_groups="" _fname="" _group="" _groups="" _rc=0; _status="";
Lucio Andrés Illanes Albornoz 60fba6
	if [ "${ARG_INFO:-0}" -eq 1 ]\
Lucio Andrés Illanes Albornoz 60fba6
	|| [ "${ARG_RDEPENDS:-0}" -eq 1 ]\
Lucio Andrés Illanes Albornoz 60fba6
	|| [ "${ARG_TARBALL:-0}" -eq 1 ]; then
Lucio Andrés Illanes Albornoz 60fba6
		for _fname in $(find ./groups -name *.group | sort); do
Lucio Andrés Illanes Albornoz 60fba6
			rtl_fileop source_opt "${_fname}";
Lucio Andrés Illanes Albornoz 60fba6
			if [ -n "${GROUP_TARGET}" ]; then
Lucio Andrés Illanes Albornoz 60fba6
				_group="${GROUP_TARGET}"; unset GROUP_TARGET;
Lucio Andrés Illanes Albornoz 60fba6
			else
Lucio Andrés Illanes Albornoz 60fba6
				_group="${_fname##*/}"; _group="${_group%.group}"; _group="${_group#*.}";
Lucio Andrés Illanes Albornoz 60fba6
			fi;
Lucio Andrés Illanes Albornoz 60fba6
			if ! rtl_lmatch "${_groups}" "${_group}"; then
Lucio Andrés Illanes Albornoz 60fba6
				_groups="$(rtl_lconcat "${_groups}" "${_group}")";
Lucio Andrés Illanes Albornoz 60fba6
				if [ -n "${GROUP_AUTO}" ]; then
Lucio Andrés Illanes Albornoz 60fba6
					if [ "${GROUP_AUTO:-0}" -ne 0 ]; then
Lucio Andrés Illanes Albornoz 60fba6
						_default_build_groups="$(rtl_lconcat "${_default_build_groups}" "${_group}")";
Lucio Andrés Illanes Albornoz 60fba6
					fi;
Lucio Andrés Illanes Albornoz 60fba6
					unset GROUP_AUTO;
Lucio Andrés Illanes Albornoz 60fba6
				else
Lucio Andrés Illanes Albornoz 60fba6
					_default_build_groups="$(rtl_lconcat "${_default_build_groups}" "${_group}")";
Lucio Andrés Illanes Albornoz 60fba6
				fi;
Lucio Andrés Illanes Albornoz 60fba6
			fi;
Lucio Andrés Illanes Albornoz 60fba6
		done;
Lucio Andrés Illanes Albornoz 60fba6
		_default_build_groups="$(rtl_uniq "${_default_build_groups}")";
Lucio Andrés Illanes Albornoz 60fba6
		BUILD_GROUPS="${_default_build_groups}";
Lucio Andrés Illanes Albornoz 60fba6
	fi; return "${_rc}";
Lucio Andrés Illanes Albornoz 60fba6
};
Lucio Andrés Illanes Albornoz 60fba6
Lucio Andrés Illanes Albornoz 60fba6
pkgtoolp_init_package() {
Lucio Andrés Illanes Albornoz 60fba6
	local _foundfl=0 _group_name="" _pkg_names="" _rc=0; _status="";
Lucio Andrés Illanes Albornoz 60fba6
	if [ "${ARG_INFO:-0}" -eq 1 ]\
Lucio Andrés Illanes Albornoz 60fba6
	|| [ "${ARG_RDEPENDS:-0}" -eq 1 ]\
Lucio Andrés Illanes Albornoz 60fba6
	|| [ "${ARG_TARBALL:-0}" -eq 1 ]; then
Lucio Andrés Illanes Albornoz 60fba6
		for _group_name in ${BUILD_GROUPS}; do
Lucio Andrés Illanes Albornoz 60fba6
			if ! _pkg_names="$(rtl_get_var_unsafe -u "${_group_name}_PACKAGES")"\
Lucio Andrés Illanes Albornoz 60fba6
			|| [ -z "${_pkg_names}" ]; then
Lucio Andrés Illanes Albornoz 60fba6
				rtl_log_msg warn "Warning: ignoring non-existent or invalid build group \`${_build_group}'.";
Lucio Andrés Illanes Albornoz 60fba6
			elif rtl_lmatch "${_pkg_names}" "${PKG_NAME}"; then
Lucio Andrés Illanes Albornoz 60fba6
				_foundfl=1;
Lucio Andrés Illanes Albornoz 60fba6
			fi;
Lucio Andrés Illanes Albornoz 60fba6
		done;
Lucio Andrés Illanes Albornoz 60fba6
		if [ "${_foundfl:-0}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz 60fba6
			_rc=1; _status="Error: package \`${PKG_NAME}' unknown.";
Lucio Andrés Illanes Albornoz 60fba6
		fi;
Lucio Andrés Illanes Albornoz 60fba6
	fi; return "${_rc}";
Lucio Andrés Illanes Albornoz 60fba6
};
Lucio Andrés Illanes Albornoz 60fba6
Lucio Andrés Illanes Albornoz 8a27f9
pkgtoolp_init_prereqs() {
Lucio Andrés Illanes Albornoz 8a27f9
	local _cmd="" _cmds_missing="" _rc=0; _status="";
Lucio Andrés Illanes Albornoz 8a27f9
	for _cmd in	\
Lucio Andrés Illanes Albornoz 60fba6
			awk bunzip2 bzip2 cat chmod cmake cp date find flock	\
Lucio Andrés Illanes Albornoz 60fba6
			g++ gcc git grep gunzip gzip hostname install kill	\
Lucio Andrés Illanes Albornoz 60fba6
			ln lzip make mkdir mkfifo mktemp mv paste patch perl	\
Lucio Andrés Illanes Albornoz 60fba6
			pgrep pkill printf readlink rm sed sha256sum sort	\
Lucio Andrés Illanes Albornoz 60fba6
			tail tar test touch tr uniq wget xz zip; do
Lucio Andrés Illanes Albornoz 8a27f9
		if ! which "${_cmd}" >/dev/null 2>&1; then
Lucio Andrés Illanes Albornoz 8a27f9
			_cmds_missing="${_cmds_missing:+${_cmds_missing} }${_cmd}";
Lucio Andrés Illanes Albornoz 8a27f9
		fi;
Lucio Andrés Illanes Albornoz 8a27f9
	done;
Lucio Andrés Illanes Albornoz 8a27f9
	if [ -n "${_cmds_missing}" ]; then
Lucio Andrés Illanes Albornoz 8a27f9
		_rc=1; _status="Error: missing prerequisite package(s): ${_cmds_missing}";
Lucio Andrés Illanes Albornoz 60fba6
	elif ! (FNAME="$(mktemp)" && { trap "rm -f \"\${FNAME}\"" EXIT;		\
Lucio Andrés Illanes Albornoz 60fba6
			sed -i'' -e '' "${FNAME}" >/dev/null 2>&1; }); then
Lucio Andrés Illanes Albornoz 60fba6
		_rc=1; _status="Error: sed(1) in \${PATH} does not support the \`-i' option.";
Lucio Andrés Illanes Albornoz 8a27f9
	fi;
Lucio Andrés Illanes Albornoz 8a27f9
	return "${_rc}";
Lucio Andrés Illanes Albornoz 8a27f9
};
Lucio Andrés Illanes Albornoz 8a27f9
Lucio Andrés Illanes Albornoz 8a27f9
pkgtoolp_init_vars() {
Lucio Andrés Illanes Albornoz 8a27f9
	local _rc=0; _status="";
Lucio Andrés Illanes Albornoz 8a27f9
	if ! rtl_lmatch "${ARCH}" "nt32 nt64"; then
Lucio Andrés Illanes Albornoz 8a27f9
		_rc=1; _status="Error: invalid architecture \`${ARCH}'.";
Lucio Andrés Illanes Albornoz 8a27f9
	elif ! rtl_lmatch "${BUILD}" "debug release"; then
Lucio Andrés Illanes Albornoz 8a27f9
		_rc=1; _status="Error: unknown build type \`${BUILD}'.";
Lucio Andrés Illanes Albornoz 8a27f9
	else	case "${ARCH}" in
Lucio Andrés Illanes Albornoz 8a27f9
		nt32)	DEFAULT_TARGET="i686-nt32-midipix"; ;;
Lucio Andrés Illanes Albornoz 8a27f9
		nt64)	DEFAULT_TARGET="x86_64-nt64-midipix"; ;;
Lucio Andrés Illanes Albornoz 8a27f9
		esac;
Lucio Andrés Illanes Albornoz 8a27f9
		rtl_fileop source_opt							\
Lucio Andrés Illanes Albornoz 8a27f9
			"${HOME}/midipix_build.vars" "${HOME}/.midipix_build.vars"	\
Lucio Andrés Illanes Albornoz 8a27f9
			../midipix_build.vars ./midipix.env;
Lucio Andrés Illanes Albornoz 8a27f9
		if [ -z "${PREFIX}" ]; then
Lucio Andrés Illanes Albornoz 8a27f9
			_rc=1; _status="Error: \${PREFIX} empty or unset.";
Lucio Andrés Illanes Albornoz 8a27f9
		fi; fi;
Lucio Andrés Illanes Albornoz 8a27f9
	return "${_rc}";
Lucio Andrés Illanes Albornoz 8a27f9
};
Lucio Andrés Illanes Albornoz 8a27f9
Lucio Andrés Illanes Albornoz 8a27f9
pkgtool_init() {
Lucio Andrés Illanes Albornoz 8a27f9
	local _fname="" _rc=0 _status="";
Lucio Andrés Illanes Albornoz 8a27f9
	if ! pkgtoolp_init_env			\
Lucio Andrés Illanes Albornoz 8a27f9
	|| ! pkgtoolp_init_defaults		\
Lucio Andrés Illanes Albornoz 8a27f9
	|| ! pkgtoolp_init_getopts "${@}"	\
Lucio Andrés Illanes Albornoz 8a27f9
	|| ! pkgtoolp_init_prereqs		\
Lucio Andrés Illanes Albornoz 60fba6
	|| ! pkgtoolp_init_vars			\
Lucio Andrés Illanes Albornoz 60fba6
	|| ! pkgtoolp_init_dump			\
Lucio Andrés Illanes Albornoz 60fba6
	|| ! pkgtoolp_init_groups		\
Lucio Andrés Illanes Albornoz 60fba6
	|| ! pkgtoolp_init_package; then
Lucio Andrés Illanes Albornoz 8a27f9
		_rc="${?}"; rtl_log_msg fail "${_status}"; exit "${_rc}";
Lucio Andrés Illanes Albornoz 8a27f9
	elif [ -n "${_status}" ]; then
Lucio Andrés Illanes Albornoz 8a27f9
		rtl_log_msg info "${_status}"; exit 0;
Lucio Andrés Illanes Albornoz 8a27f9
	else
Lucio Andrés Illanes Albornoz 8a27f9
		return "${_rc}";
Lucio Andrés Illanes Albornoz 8a27f9
	fi;
Lucio Andrés Illanes Albornoz 8a27f9
};
Lucio Andrés Illanes Albornoz 8a27f9
Lucio Andrés Illanes Albornoz 8a27f9
# vim:filetype=sh