Blame subr/build_init.subr

Lucio Andrés Illanes Albornoz c6d6e0
#
Lucio Andrés Illanes Albornoz e1d469
# set +o errexit -o noglob -o nounset is assumed.
Lucio Andrés Illanes Albornoz c6d6e0
#
Lucio Andrés Illanes Albornoz 6c3660
Lucio Andrés Illanes Albornoz 370e98
buildp_expand_restart_at() {
Lucio Andrés Illanes Albornoz 370e98
	local _restart_at_spec="${1}" _restart_at="" _restart_at_spec_new="";
Lucio Andrés Illanes Albornoz 370e98
Lucio Andrés Illanes Albornoz 370e98
	for _restart_at in $(rtl_llift "${_restart_at_spec}" "," " "); do
Lucio Andrés Illanes Albornoz 370e98
		case "${_restart_at}" in
Lucio Andrés Illanes Albornoz 370e98
		@fetch)	_restart_at="fetch_download,fetch_extract"; ;;
Lucio Andrés Illanes Albornoz 370e98
		@configure)
Lucio Andrés Illanes Albornoz 370e98
			_restart_at="configure_patch_pre,configure_autotools,configure_patch,configure"; ;;
Lucio Andrés Illanes Albornoz 370e98
		@build)	_restart_at="build"; ;;
Lucio Andrés Illanes Albornoz 370e98
		@install)
Lucio Andrés Illanes Albornoz 370e98
			_restart_at="install_subdirs,install_make,install_files,install_libs,install,install_rpm"; ;;
Lucio Andrés Illanes Albornoz 370e98
		@clean)
Lucio Andrés Illanes Albornoz 370e98
			_restart_at="clean"; ;;
Lucio Andrés Illanes Albornoz 370e98
		*)	;;
Lucio Andrés Illanes Albornoz 370e98
		esac;
Lucio Andrés Illanes Albornoz 370e98
		_restart_at_spec_new="$(rtl_lconcat "${_restart_at_spec_new}" "${_restart_at}" ",")"
Lucio Andrés Illanes Albornoz 370e98
	done;
Lucio Andrés Illanes Albornoz 370e98
	printf "%s" "${_restart_at_spec_new}";
Lucio Andrés Illanes Albornoz 370e98
};
Lucio Andrés Illanes Albornoz 370e98
Lucio Andrés Illanes Albornoz 370e98
buildp_expand_restart_recursive() {
Lucio Andrés Illanes Albornoz 370e98
	local _restart_spec="${1}" _last_pkg="" _restart_spec_at="" _restart_spec_recursive=0;
Lucio Andrés Illanes Albornoz 370e98
Lucio Andrés Illanes Albornoz 370e98
	case "${_restart_spec}" in
Lucio Andrés Illanes Albornoz 370e98
	\*\*\*[a-zA-Z]*)
Lucio Andrés Illanes Albornoz 370e98
			_restart_spec="${_restart_spec#\*\*\*}"; _restart_spec_recursive=3; ;;
Lucio Andrés Illanes Albornoz 370e98
	\*\*[a-zA-Z]*)	_restart_spec="${_restart_spec#\*\*}"; _restart_spec_recursive=2; ;;
Lucio Andrés Illanes Albornoz 370e98
	\*[a-zA-Z]*)	_restart_spec="${_restart_spec#\*}"; _restart_spec_recursive=1; ;;
Lucio Andrés Illanes Albornoz 370e98
	ALL)		_restart_spec_at=ALL; _restart_spec_recursive=2; ;;
Lucio Andrés Illanes Albornoz 370e98
	LAST)		_restart_spec_at=ALL; _restart_spec_recursive=0;
Lucio Andrés Illanes Albornoz 370e98
			if [ -n "${DEFAULT_BUILD_LAST_FAILED_PKG_FNAME}" ]\
Lucio Andrés Illanes Albornoz 370e98
			&& [ -e "${DEFAULT_BUILD_LAST_FAILED_PKG_FNAME}" ]; then
Lucio Andrés Illanes Albornoz 370e98
				_last_pkg="$(cat "${DEFAULT_BUILD_LAST_FAILED_PKG_FNAME}")";
Lucio Andrés Illanes Albornoz 370e98
				rtl_fileop rm "${DEFAULT_BUILD_LAST_FAILED_PKG_FNAME}";
Lucio Andrés Illanes Albornoz 370e98
				rtl_state_clear "${BUILD_WORKDIR}" "${_last_pkg}";
Lucio Andrés Illanes Albornoz 370e98
				_restart_spec="${_last_pkg}";
Lucio Andrés Illanes Albornoz 370e98
			else
Lucio Andrés Illanes Albornoz 370e98
				_status="cannot rebuild last failed package"; return 1;
Lucio Andrés Illanes Albornoz 370e98
			fi; ;;
Lucio Andrés Illanes Albornoz 370e98
	esac;
Lucio Andrés Illanes Albornoz 370e98
	ARG_RESTART="${_restart_spec}";
Lucio Andrés Illanes Albornoz 370e98
	ARG_RESTART_AT="${_restart_spec_at}";
Lucio Andrés Illanes Albornoz 370e98
	ARG_RESTART_RECURSIVE="${_restart_spec_recursive}";
Lucio Andrés Illanes Albornoz 370e98
	return 0;
Lucio Andrés Illanes Albornoz 370e98
};
Lucio Andrés Illanes Albornoz 370e98
Lucio Andrés Illanes Albornoz 370e98
buildp_process_restart_spec() {
Lucio Andrés Illanes Albornoz 370e98
	local _restart_spec="${1}" _eqfl=0 _ltfl=0 _restart_spec_at0="" _restart_spec_at="" _step="" _step1=""; _status="";
Lucio Andrés Illanes Albornoz 370e98
Lucio Andrés Illanes Albornoz 370e98
	buildp_expand_restart_recursive "${_restart_spec}" || return "${?}";
Lucio Andrés Illanes Albornoz 370e98
	_restart_spec="${ARG_RESTART}"; _restart_spec_at="${ARG_RESTART_AT}";
Lucio Andrés Illanes Albornoz 370e98
Lucio Andrés Illanes Albornoz 370e98
	case "${_restart_spec}" in
Lucio Andrés Illanes Albornoz 370e98
	"")	return 0; ;;
Lucio Andrés Illanes Albornoz 370e98
	*:*)
Lucio Andrés Illanes Albornoz 370e98
		_restart_spec_at="${_restart_spec#*:}"; _restart_spec="${_restart_spec%%:*}";
Lucio Andrés Illanes Albornoz 370e98
		_restart_spec_at0="${_restart_spec_at}"; _restart_spec_at="";
Lucio Andrés Illanes Albornoz 370e98
		case "${_restart_spec_at0}" in
Lucio Andrés Illanes Albornoz 370e98
		ALL|LAST)
Lucio Andrés Illanes Albornoz 370e98
			_restart_spec_at="${_restart_spec_at0}"; ;;
Lucio Andrés Illanes Albornoz 370e98
Lucio Andrés Illanes Albornoz 370e98
		^*)	_restart_spec_at0="${_restart_spec_at0#^}";
Lucio Andrés Illanes Albornoz 370e98
			_restart_spec_at0="$(buildp_expand_restart_at "${_restart_spec_at0}")";
Lucio Andrés Illanes Albornoz 370e98
			_restart_spec_at="$(rtl_llift "${DEFAULT_BUILD_STEPS}" " 	" ",")";
Lucio Andrés Illanes Albornoz 370e98
			for _restart_at in $(rtl_llift "${_restart_spec_at0}" "," " "); do
Lucio Andrés Illanes Albornoz 370e98
				_restart_spec_at="$(rtl_lfilter "${_restart_spec_at}" "${_restart_at}" ",")";
Lucio Andrés Illanes Albornoz 370e98
			done; _restart_spec_at="$(rtl_lfilter "${_restart_spec_at}" "finish" ",")"; ;;
Lucio Andrés Illanes Albornoz 370e98
Lucio Andrés Illanes Albornoz 370e98
		\<=*|\<*|\>=*|\>*)
Lucio Andrés Illanes Albornoz 370e98
			[ "${_restart_spec_at0#<}" != "${_restart_spec_at0}" ] && _ltfl=1;
Lucio Andrés Illanes Albornoz 370e98
			if [ "${_restart_spec_at0#[<>]=}" != "${_restart_spec_at0}" ]; then
Lucio Andrés Illanes Albornoz 370e98
				_restart_spec_at0="${_restart_spec_at0#[<>]=}"; _eqfl=1;
Lucio Andrés Illanes Albornoz 370e98
			else
Lucio Andrés Illanes Albornoz 370e98
				_restart_spec_at0="${_restart_spec_at0#[<>]}"; _eqfl=0;
Lucio Andrés Illanes Albornoz 370e98
			fi; _restart_spec_at="";
Lucio Andrés Illanes Albornoz 370e98
			_restart_spec_at0="$(buildp_expand_restart_at "${_restart_spec_at0%%,*}")";
Lucio Andrés Illanes Albornoz 370e98
			if [ \( "${_eqfl}" -eq 1 \) -a \( "${_ltfl}" -eq 1 \) ]\
Lucio Andrés Illanes Albornoz 370e98
			|| [ \( "${_eqfl}" -eq 0 \) -a \( "${_ltfl}" -eq 0 \) ]; then
Lucio Andrés Illanes Albornoz 370e98
				_restart_spec_at0="${_restart_spec_at0##*,}";
Lucio Andrés Illanes Albornoz 370e98
			elif [ \( "${_eqfl}" -eq 1 \) -a \( "${_ltfl}" -eq 0 \) ]\
Lucio Andrés Illanes Albornoz 370e98
			||   [ \( "${_eqfl}" -eq 0 \) -a \( "${_ltfl}" -eq 1 \) ]; then
Lucio Andrés Illanes Albornoz 370e98
				_restart_spec_at0="${_restart_spec_at0%%,*}";
Lucio Andrés Illanes Albornoz 370e98
			fi;
Lucio Andrés Illanes Albornoz 370e98
			for _restart_at in ${DEFAULT_BUILD_STEPS}; do
Lucio Andrés Illanes Albornoz 370e98
				if [ "${_ltfl}" -eq 1 ]; then
Lucio Andrés Illanes Albornoz 370e98
					if [ "${_restart_at}" = "${_restart_spec_at0%%,*}" ]; then
Lucio Andrés Illanes Albornoz 370e98
						if [ "${_eqfl}" -eq 1 ]; then
Lucio Andrés Illanes Albornoz 370e98
							_restart_spec_at="$(rtl_lconcat "${_restart_spec_at}" "${_restart_at}" ",")";
Lucio Andrés Illanes Albornoz 370e98
						fi; break;
Lucio Andrés Illanes Albornoz 370e98
					fi;
Lucio Andrés Illanes Albornoz 370e98
				else
Lucio Andrés Illanes Albornoz 370e98
					if [ "${_restart_at}" = "${_restart_spec_at0%%,*}" ]; then
Lucio Andrés Illanes Albornoz 370e98
						_foundfl=1; [ "${_eqfl}" -eq 0 ] && continue;
Lucio Andrés Illanes Albornoz 370e98
					fi; [ "${_foundfl}" -eq 0 ] && continue;
Lucio Andrés Illanes Albornoz 370e98
				fi;
Lucio Andrés Illanes Albornoz 370e98
				_restart_spec_at="$(rtl_lconcat "${_restart_spec_at}" "${_restart_at}" ",")";
Lucio Andrés Illanes Albornoz 370e98
			done; ;;
Lucio Andrés Illanes Albornoz 370e98
Lucio Andrés Illanes Albornoz 370e98
		*)	_restart_spec_at="$(buildp_expand_restart_at "${_restart_spec_at0}")"; ;;
Lucio Andrés Illanes Albornoz 370e98
		esac; ;;
Lucio Andrés Illanes Albornoz 370e98
Lucio Andrés Illanes Albornoz 370e98
	*)	_restart_spec_at=ALL; ;;
Lucio Andrés Illanes Albornoz 370e98
	esac;
Lucio Andrés Illanes Albornoz 370e98
Lucio Andrés Illanes Albornoz 370e98
	ARG_RESTART="$(rtl_llift "${_restart_spec}" "," " ")";
Lucio Andrés Illanes Albornoz 370e98
	ARG_RESTART_AT="${_restart_spec_at}";
Lucio Andrés Illanes Albornoz 370e98
	if [ "${#ARG_RESTART_AT}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz 370e98
		_status="zero-length build step list"; return 1;
Lucio Andrés Illanes Albornoz 370e98
	elif [ "${ARG_RESTART_AT}" != "ALL" ]\
Lucio Andrés Illanes Albornoz 370e98
	&&   [ "${ARG_RESTART_AT}" != "LAST" ]; then
Lucio Andrés Illanes Albornoz 370e98
		for _restart_at in $(rtl_lfilter "$(rtl_llift "${ARG_RESTART_AT}" "," " ")" "${DEFAULT_BUILD_STEPS}"); do
Lucio Andrés Illanes Albornoz 370e98
			case "${_restart_at}" in
Lucio Andrés Illanes Albornoz 370e98
			start)	;;
Lucio Andrés Illanes Albornoz 370e98
			*)	_status="unknown build step \`${_restart_at}'"; return 1; ;;
Lucio Andrés Illanes Albornoz 370e98
			esac;
Lucio Andrés Illanes Albornoz 370e98
		done;
Lucio Andrés Illanes Albornoz 370e98
	fi;
Lucio Andrés Illanes Albornoz 370e98
	if [ "${#ARG_RESTART_AT}" -gt 0 ]\
Lucio Andrés Illanes Albornoz 370e98
	&& [ "${ARG_RESTART_AT}" != "ALL" ]\
Lucio Andrés Illanes Albornoz 370e98
	&& [ "${ARG_RESTART_AT}" != "LAST" ]; then
Lucio Andrés Illanes Albornoz 370e98
		_restart_spec_at="${ARG_RESTART_AT}"; ARG_RESTART_AT="";
Lucio Andrés Illanes Albornoz 370e98
		for _restart_at in ${DEFAULT_BUILD_STEPS}; do
Lucio Andrés Illanes Albornoz 370e98
			if rtl_lmatch "${_restart_spec_at}" "${_restart_at}" ","; then
Lucio Andrés Illanes Albornoz 370e98
				ARG_RESTART_AT="$(rtl_lconcat "${ARG_RESTART_AT}" "${_restart_at}" ",")";
Lucio Andrés Illanes Albornoz 370e98
			fi;
Lucio Andrés Illanes Albornoz 370e98
		done;
Lucio Andrés Illanes Albornoz 370e98
		if [ "${ARG_RESTART_AT##*,}" != "finish" ]; then
Lucio Andrés Illanes Albornoz 370e98
			_step="$(rtl_lfilter "${ARG_RESTART_AT}" "clean,finish" ",")"; _step="${_step##*,}";
Lucio Andrés Illanes Albornoz 370e98
			_step1="$(rtl_lfilter "${DEFAULT_BUILD_STEPS}" "clean finish")"; _step1="${_step1##* }";
Lucio Andrés Illanes Albornoz 370e98
			if [ "${_step}" = "${_step1}" ]; then
Lucio Andrés Illanes Albornoz 370e98
				ARG_RESTART_AT="$(rtl_lconcat "${ARG_RESTART_AT}" "finish" ",")";
Lucio Andrés Illanes Albornoz 370e98
			fi;
Lucio Andrés Illanes Albornoz 370e98
		fi;
Lucio Andrés Illanes Albornoz 370e98
	fi;
Lucio Andrés Illanes Albornoz 370e98
	return 0;
Lucio Andrés Illanes Albornoz 370e98
};
Lucio Andrés Illanes Albornoz 370e98
Lucio Andrés Illanes Albornoz c3bcd7
buildp_init_args() {
Lucio Andrés Illanes Albornoz 370e98
	local	_eqfl=0 _foundfl=0 _group="" _pkg_names_unknown="" _rc=0	\
Lucio Andrés Illanes Albornoz 1dd016
		EX_PKG_BUILD_GROUPS EX_PKG_BUILD_GROUPS_NOAUTO; _status="";
Lucio Andrés Illanes Albornoz 1dd016
Lucio Andrés Illanes Albornoz 370e98
	case "${ARG_FETCH_FORCE}" in
Lucio Andrés Illanes Albornoz 370e98
	ipv4)	DEFAULT_GIT_ARGS="$(rtl_lconcat "-4" "${DEFAULT_GIT_ARGS}")";
Lucio Andrés Illanes Albornoz 370e98
		DEFAULT_WGET_ARGS="$(rtl_lconcat "-4" "${DEFAULT_WGET_ARGS}")"; ;;
Lucio Andrés Illanes Albornoz 370e98
	ipv6)	DEFAULT_GIT_ARGS="$(rtl_lconcat "-6" "${DEFAULT_GIT_ARGS}")";
Lucio Andrés Illanes Albornoz 370e98
		DEFAULT_WGET_ARGS="$(rtl_lconcat "-6" "${DEFAULT_WGET_ARGS}")"; ;;
Lucio Andrés Illanes Albornoz 370e98
	esac;
Lucio Andrés Illanes Albornoz 9b5d49
	if [ -z "${BUILD_HNAME:-}" ]\
Lucio Andrés Illanes Albornoz 9b5d49
	&& ! BUILD_HNAME="$(hostname)"; then
Lucio Andrés Illanes Albornoz 9b5d49
		_rc=1; _status="Error: failed to obtain hostname.";
Lucio Andrés Illanes Albornoz 9b5d49
	elif [ "${ARG_DUMP_ON_ABORT:-0}" -eq 1 ]\
Lucio Andrés Illanes Albornoz b6a9a1
	&& [ "${ARG_RELAXED:-0}" -eq 1 ]; then
Lucio Andrés Illanes Albornoz b6a9a1
		_rc=1; _status="Error: --dump-on-abort excludes -R.";
Lucio Andrés Illanes Albornoz b6a9a1
	elif [ "${ARG_AS_NEEDED:-0}" -eq 1 ]\
Lucio Andrés Illanes Albornoz c3bcd7
	&& [ -e "${PREFIX}/build.gitref" ]\
Lucio Andrés Illanes Albornoz c3bcd7
	&& [ "$(git rev-parse HEAD)" = "$(cat "${PREFIX}/build.gitref")" ]; then
Lucio Andrés Illanes Albornoz b6a9a1
		_rc=0; _status="Git repository has not changed since last build and --as-needed was specified.";
Lucio Andrés Illanes Albornoz 370e98
	elif ! buildp_process_restart_spec "${ARG_RESTART:-}"; then
Lucio Andrés Illanes Albornoz 370e98
		_rc=1; _status="Error: failed to process -r specification: ${_status}.";
Lucio Andrés Illanes Albornoz 370e98
	elif ! ex_pkg_load_groups; then
Lucio Andrés Illanes Albornoz 370e98
		_rc=1; _status="Error: failed to load build groups.";
Lucio Andrés Illanes Albornoz 370e98
	else	if ! rtl_lmatch "${ARG_DIST:-}" "rpm" ","\
Lucio Andrés Illanes Albornoz 370e98
		&& [ -z "${ARG_DUMP_IN:-}" ]\
Lucio Andrés Illanes Albornoz 370e98
		&& [ "${ARG_DUMP_ON_ABORT:-0}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz 370e98
			EX_PKG_BUILD_GROUPS="$(rtl_lfilter "${EX_PKG_BUILD_GROUPS}" "host_deps_rpm")";
Lucio Andrés Illanes Albornoz 370e98
		fi;
Lucio Andrés Illanes Albornoz 370e98
		if [ -z "${BUILD_GROUPS}" ]; then
Lucio Andrés Illanes Albornoz 370e98
			BUILD_GROUPS="${EX_PKG_BUILD_GROUPS}";
Lucio Andrés Illanes Albornoz 370e98
		else	_foundfl=0; for _group in ${BUILD_GROUPS}; do
Lucio Andrés Illanes Albornoz 370e98
				if rtl_lmatch "${EX_PKG_BUILD_GROUPS}" "${_group}"; then
Lucio Andrés Illanes Albornoz 370e98
					_foundfl=1; break;
Lucio Andrés Illanes Albornoz e1d469
				fi;
Lucio Andrés Illanes Albornoz 370e98
			done;
Lucio Andrés Illanes Albornoz 370e98
			if [ "${_foundfl}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz 370e98
				_foundfl=0; for _group in ${BUILD_GROUPS}; do
Lucio Andrés Illanes Albornoz 370e98
					if rtl_lmatch "${EX_PKG_BUILD_GROUPS}" "${_group}"; then
Lucio Andrés Illanes Albornoz 370e98
						_rc=1; _status="Error: unknown build group \`${_group}'."; break;
Lucio Andrés Illanes Albornoz b6a9a1
					fi;
Lucio Andrés Illanes Albornoz 370e98
				done;
Lucio Andrés Illanes Albornoz 370e98
			fi;
Lucio Andrés Illanes Albornoz 370e98
		fi;
Lucio Andrés Illanes Albornoz 370e98
		if [ "${_rc:-0}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz 370e98
			if [ -n "${ARG_DIST}" ]; then
Lucio Andrés Illanes Albornoz 370e98
				BUILD_GROUPS="$(rtl_lconcat "$(rtl_lfilter "${BUILD_GROUPS}" "dist")" "dist")";
Lucio Andrés Illanes Albornoz 370e98
			fi;
Lucio Andrés Illanes Albornoz 370e98
			if [ -n "${ARG_RESTART}" ] && ! rtl_lmatch "${ARG_RESTART}" "ALL LAST"; then
Lucio Andrés Illanes Albornoz 370e98
				for _pkg_name in ${ARG_RESTART}; do
Lucio Andrés Illanes Albornoz 370e98
					if ! ex_pkg_find_package "${BUILD_GROUPS}" "${_pkg_name}" >/dev/null; then
Lucio Andrés Illanes Albornoz 370e98
						_pkg_names_unknown="$(rtl_lconcat "${_pkg_names_unknown}" "${_pkg_name}")";
Lucio Andrés Illanes Albornoz b6a9a1
					fi;
Lucio Andrés Illanes Albornoz 370e98
				done;
Lucio Andrés Illanes Albornoz 370e98
				case "$(rtl_llength "${_pkg_names_unknown}")" in
Lucio Andrés Illanes Albornoz 370e98
				0)	;;
Lucio Andrés Illanes Albornoz 370e98
				1)	_rc=1; _status="Error: unknown package \`${_pkg_names_unknown}'."; ;;
Lucio Andrés Illanes Albornoz 370e98
				*)	_rc=1; _status="Error: unknown packages: $(rtl_subst "${_pkg_names_unknown}" " " ", ")"; ;;
Lucio Andrés Illanes Albornoz 370e98
				esac;
Lucio Andrés Illanes Albornoz b6a9a1
			fi;
Lucio Andrés Illanes Albornoz c3bcd7
		fi;
Lucio Andrés Illanes Albornoz b6a9a1
	fi; return "${_rc}";
Lucio Andrés Illanes Albornoz c3bcd7
};
Lucio Andrés Illanes Albornoz c3bcd7
Lucio Andrés Illanes Albornoz c3bcd7
buildp_init_env() {
Lucio Andrés Illanes Albornoz 9d2842
	local _fname="" _rc=0; _status="";
Lucio Andrés Illanes Albornoz b6a9a1
	if ! cd "${0%/*}"; then
Lucio Andrés Illanes Albornoz b6a9a1
		printf "Error: failed to change working directory to \`${0%/*}'." >&2; exit 1;
Lucio Andrés Illanes Albornoz b6a9a1
	elif ! umask 022; then
Lucio Andrés Illanes Albornoz b6a9a1
		printf "Error: failed to set umask(2).\n" >&2; exit 1;
Lucio Andrés Illanes Albornoz b6a9a1
	elif ! BUILD_USER="$(id -nu)"; then
Lucio Andrés Illanes Albornoz 11ef38
		printf "Error: failed to obtain username." >&2; exit 1;
Lucio Andrés Illanes Albornoz 612534
	else	for _fname in $(find subr.rtl -name *.subr) $(find subr -name *.subr); do
Lucio Andrés Illanes Albornoz 6d9a3c
			if ! . "${_fname}"; then
Lucio Andrés Illanes Albornoz 6d9a3c
				printf "Error: failed to source \`%s'.\n" "${_fname}"; exit 1;
Lucio Andrés Illanes Albornoz 6d9a3c
			fi;
Lucio Andrés Illanes Albornoz b6a9a1
		done;
Lucio Andrés Illanes Albornoz b6a9a1
	fi; return "${_rc}";
Lucio Andrés Illanes Albornoz c3bcd7
};
Lucio Andrés Illanes Albornoz c3bcd7
Lucio Andrés Illanes Albornoz c3bcd7
buildp_init_files() {
Lucio Andrés Illanes Albornoz 0d1c4f
	local _log_last_fname="" _log_last_num=1 _rc=0; _status=""
Lucio Andrés Illanes Albornoz c6d6e0
	if ! rtl_fileop mkdir "${BUILD_DLCACHEDIR}" "${BUILD_WORKDIR}"\
Lucio Andrés Illanes Albornoz c6d6e0
	|| rtl_lmatch "${ARG_DIST}" "rpm" ","\
Lucio Andrés Illanes Albornoz c6d6e0
	&& ! rtl_fileop mkdir "${PREFIX_RPM}"; then
Lucio Andrés Illanes Albornoz c6d6e0
		_rc=1; _status="Error: cannot create build directories.";
Lucio Andrés Illanes Albornoz 2b85d0
	elif [ -e "${DEFAULT_BUILD_STATUS_IN_PROGRESS_FNAME}" ]; then
Lucio Andrés Illanes Albornoz c6d6e0
		_rc=1; _status="Error: another build targeting this architecture and build type is currently in progress.";
Lucio Andrés Illanes Albornoz c6d6e0
	elif ! rtl_clean_env "${DEFAULT_CLEAR_ENV_VARS_EXCEPT}"; then
Lucio Andrés Illanes Albornoz c6d6e0
		_rc=1; _status="Error: failed to clean environment.";
Lucio Andrés Illanes Albornoz c6d6e0
	elif ! rtl_check_path_vars "${DEFAULT_CHECK_PATH_VARS}"; then
Lucio Andrés Illanes Albornoz 8a27f9
		_rc=1; _status="${_status}";
Lucio Andrés Illanes Albornoz 98c928
	else	export TMP="${BUILD_WORKDIR}" TMPDIR="${BUILD_WORKDIR}";
Lucio Andrés Illanes Albornoz 98c928
		touch "${DEFAULT_BUILD_STATUS_IN_PROGRESS_FNAME}";
Lucio Andrés Illanes Albornoz c3bcd7
		if [ -e "${DEFAULT_BUILD_LOG_FNAME}" ]; then
Lucio Andrés Illanes Albornoz 0d1c4f
			while [ -e "${DEFAULT_BUILD_LOG_FNAME}.${_log_last_num}" ]; do
Lucio Andrés Illanes Albornoz 0d1c4f
				: $((_log_last_num+=1));
Lucio Andrés Illanes Albornoz 0d1c4f
			done;
Lucio Andrés Illanes Albornoz 0d1c4f
			_log_last_fname="${DEFAULT_BUILD_LOG_FNAME}.${_log_last_num}";
Lucio Andrés Illanes Albornoz c6d6e0
			rtl_fileop mv "${DEFAULT_BUILD_LOG_FNAME}" "${_log_last_fname}";
Lucio Andrés Illanes Albornoz c6d6e0
			rtl_fileop ln_symbolic "${_log_last_fname}" "${DEFAULT_BUILD_LOG_LAST_FNAME}";
Lucio Andrés Illanes Albornoz c6d6e0
		fi;
Lucio Andrés Illanes Albornoz b6a9a1
		rtl_fileop touch "${DEFAULT_BUILD_LOG_FNAME}"; rtl_log_set_fname "${DEFAULT_BUILD_LOG_FNAME}";
Lucio Andrés Illanes Albornoz c6d6e0
		if rtl_lmatch "${ARG_CLEAN_BUILDS}" "prefix" ","; then
Lucio Andrés Illanes Albornoz 0a3032
			trap "rm -f \"${DEFAULT_BUILD_STATUS_IN_PROGRESS_FNAME}\" 2>/dev/null; rtl_log_msg fatalexit \"Build aborted.\"" HUP INT TERM USR1 USR2;
Lucio Andrés Illanes Albornoz c6d6e0
			rtl_log_msg info "-C prefix specified, cleaning prefix...";
Lucio Andrés Illanes Albornoz c6d6e0
			for _pname in ${DEFAULT_CLEAR_PREFIX_PATHS}; do
Lucio Andrés Illanes Albornoz c6d6e0
				if ! rtl_fileop rm "${PREFIX}/${_pname}"; then
Lucio Andrés Illanes Albornoz c6d6e0
					_rc=1; _status="Error: failed to remove \`${PREFIX}/${_pname}'."; break;
Lucio Andrés Illanes Albornoz c6d6e0
				fi;
Lucio Andrés Illanes Albornoz c6d6e0
			done;
Lucio Andrés Illanes Albornoz 144d63
			trap - HUP INT TERM USR1 USR2;
Lucio Andrés Illanes Albornoz c3bcd7
		fi;
Lucio Andrés Illanes Albornoz c6d6e0
		export PATH="${PREFIX}/bin${PATH:+:${PATH}}";
Lucio Andrés Illanes Albornoz c3bcd7
	fi;
Lucio Andrés Illanes Albornoz c3bcd7
	return "${_rc}";
Lucio Andrés Illanes Albornoz c3bcd7
};
Lucio Andrés Illanes Albornoz c3bcd7
Lucio Andrés Illanes Albornoz c3bcd7
buildp_init_getopts() {
Lucio Andrés Illanes Albornoz 487c40
	local _arg="" _opt="" _rc=0 _shiftfl=0 OPTIND=0; _status="";
Lucio Andrés Illanes Albornoz a6970c
	: ${ARCH:="nt64"}; : ${BUILD_KIND:="debug"};
Lucio Andrés Illanes Albornoz dd8dfb
	ARG_AS_NEEDED=0; ARG_CLEAN_BUILDS=""; ARG_DEBUG_MINIPIX=0; ARG_DIST=""; ARG_DUMP_IN="";
Lucio Andrés Illanes Albornoz 370e98
	ARG_DUMP_ON_ABORT=0; ARG_FETCH_FORCE=""; ARG_PARALLEL=1; ARG_RELAXED=0; ARG_RESET_PKG=0;
Lucio Andrés Illanes Albornoz 370e98
	ARG_RESTART=""; ARG_RESTART_AT=""; ARG_RESTART_RECURSIVE=""; ARG_VERBOSE=0;
Lucio Andrés Illanes Albornoz c3bcd7
	while [ "${#}" -gt 0 ]; do
Lucio Andrés Illanes Albornoz c3bcd7
		case "${1}" in
Lucio Andrés Illanes Albornoz c3bcd7
		--as-needed)	ARG_AS_NEEDED=1; _shiftfl=1; ;;
Lucio Andrés Illanes Albornoz f243be
		--dump-in)	if [ "${#}" -ge 2 ]; then
Lucio Andrés Illanes Albornoz f243be
					ARG_DUMP_IN="${2}"; ARG_DUMP_ON_ABORT=1; _shiftfl=2;
Lucio Andrés Illanes Albornoz f243be
				else
Lucio Andrés Illanes Albornoz f243be
					_rc=1; _status="Error: missing argument to option --dump-in.";
Lucio Andrés Illanes Albornoz f243be
				fi; ;;
Lucio Andrés Illanes Albornoz 8a27f9
		--dump-on-abort)
Lucio Andrés Illanes Albornoz 8a27f9
				ARG_DUMP_ON_ABORT=1; _shiftfl=1; ;;
Lucio Andrés Illanes Albornoz c3bcd7
		--debug-minipx)	ARG_DEBUG_MINIPIX=1; _shiftfl=1; ;;
Lucio Andrés Illanes Albornoz 370e98
		--help)		cat etc/build.usage; exit 0; ;;
Lucio Andrés Illanes Albornoz 370e98
		--reset-state)	ARG_RESET_PKG=1; _shiftfl=1; ;;
Lucio Andrés Illanes Albornoz c3bcd7
		-v*)		_opt="${1#-}"; while [ -n "${_opt}" ]; do
Lucio Andrés Illanes Albornoz c3bcd7
					: $((ARG_VERBOSE+=1)); _opt="${_opt#?}";
Lucio Andrés Illanes Albornoz c3bcd7
				done; _shiftfl=1; ;;
Lucio Andrés Illanes Albornoz 75bb81
		# {{{ --roar
Lucio Andrés Illanes Albornoz 75bb81
		--roar)		printf "%s\n" '
Lucio Andrés Illanes Albornoz 75bb81
?[49;37m                ?[41;97m     ?[49;37m            ?[0m
Lucio Andrés Illanes Albornoz 75bb81
?[49;37m             ?[41;97m           ?[49;37m         ?[0m
Lucio Andrés Illanes Albornoz 75bb81
?[103;31m%.?[49;37m          ?[41;33m ?[103;33m/\?[41;31m  ?[41;97m     ?[103;33m/\?[41;97m ?[49;97m   ROAR!?[0m
Lucio Andrés Illanes Albornoz 75bb81
?[103;31m`%%.?[49;37m        ?[41;33m ?[41;31m  ?[103;31m  ?[103;33m"""?[103;31m ?[103;30m ?[41;30m   ?[49;97m  /?[49;37m     ?[0m
Lucio Andrés Illanes Albornoz 75bb81
?[49;37m ?[103;31m`%%?[49;37m        ?[41;97m ?[103;97m   ?[103;32mo?[103;97m ?[103;30m_?[103;97m ?[103;32mo?[103;97m   ?[41;31m ?[49;97m /?[49;37m      ?[0m
Lucio Andrés Illanes Albornoz 75bb81
?[49;37m ?[103;30m//?[49;37m        ?[103;30m;?[41;33m  ?[103;31m ?[103;33m(__?[103;30mY?[103;33m__)?[103;30m ?[41;31m ?[41;97m ?[49;37m        ?[0m
Lucio Andrés Illanes Albornoz 75bb81
?[103;30m((?[49;37m        ?[103;30m/?[103;33m ?[103;97m  ?[41;97m  ?[103;33m`\_/?[41;30m ?[41;97m  ?[49;37m          ?[0m
Lucio Andrés Illanes Albornoz 75bb81
?[49;37m ?[103;30m\\?[49;37m     ?[103;30m.'\''  ?[103;97m  ?[103;31m  ?[41;31m     ?[49;37m            ?[0m
Lucio Andrés Illanes Albornoz 75bb81
?[49;37m  ?[103;30m\\?[49;37m   ?[103;30m/?[103;97m         ?[41;97m   ?[49;37m             ?[0m
Lucio Andrés Illanes Albornoz 75bb81
?[49;37m   ?[103;30m\\/   ?[103;97m ?[103;30m\?[103;97m    ?[103;30m|?[103;33m ?[103;30m|?[49;37m               ?[0m
Lucio Andrés Illanes Albornoz 75bb81
?[49;37m    ?[103;30m\?[103;33m      ?[103;30m)___|?[103;33m ?[103;30m|?[49;37m               ?[0m
Lucio Andrés Illanes Albornoz 75bb81
?[49;37m   ?[103;30m(_____/__)))))))?[49;37m              ?[0m'; exit 0; ;;
Lucio Andrés Illanes Albornoz 75bb81
		# }}}
Lucio Andrés Illanes Albornoz c3bcd7
		*)		_shiftfl=0; ;;
Lucio Andrés Illanes Albornoz c3bcd7
		esac;
Lucio Andrés Illanes Albornoz f243be
		if [ "${_rc}" -ne 0 ]; then
Lucio Andrés Illanes Albornoz f243be
			break;
Lucio Andrés Illanes Albornoz f243be
		elif [ "${_shiftfl}" -gt 0 ]; then
Lucio Andrés Illanes Albornoz c3bcd7
			shift "${_shiftfl}"; continue;
Lucio Andrés Illanes Albornoz 157d4b
		elif getopts a:b:C:D:F:hp:Pr:R _opt; then
Lucio Andrés Illanes Albornoz c3bcd7
			case "${_opt}" in
Lucio Andrés Illanes Albornoz c3bcd7
			a)	ARCH="${OPTARG}"; ;;
Lucio Andrés Illanes Albornoz a6970c
			b)	BUILD_KIND="${OPTARG}"; ;;
Lucio Andrés Illanes Albornoz c3bcd7
			C)	ARG_CLEAN_BUILDS="${OPTARG}"; ;;
Lucio Andrés Illanes Albornoz c3bcd7
			D)	ARG_DIST="${OPTARG}"; ;;
Lucio Andrés Illanes Albornoz 157d4b
			F)	ARG_FETCH_FORCE="${OPTARG}"; ;;
Lucio Andrés Illanes Albornoz 370e98
			h)	cat etc/build.usage.short; exit 0; ;;
Lucio Andrés Illanes Albornoz 8f50a3
			p)	ARG_PARALLEL="${OPTARG}"; ;;
Lucio Andrés Illanes Albornoz 73f6c6
			P)	ARG_PARALLEL="auto";
Lucio Andrés Illanes Albornoz 73f6c6
				if [ -n "${2:-}" ]\
Lucio Andrés Illanes Albornoz 73f6c6
				&& rtl_isnumber "${2}"; then
Lucio Andrés Illanes Albornoz 73f6c6
					_rc=1; _status="Error: maximum parallelisation job count is set with the \`-p jobs' option."; break
Lucio Andrés Illanes Albornoz 73f6c6
				fi; ;;
Lucio Andrés Illanes Albornoz c3bcd7
			r)	ARG_RESTART="${OPTARG}"; ;;
Lucio Andrés Illanes Albornoz c3bcd7
			R)	ARG_RELAXED=1; ;;
Lucio Andrés Illanes Albornoz 370e98
			*)	cat etc/build.usage.short; exit 1; ;;
Lucio Andrés Illanes Albornoz 8f50a3
			esac; shift $((${OPTIND}-1)); OPTIND=1;
Lucio Andrés Illanes Albornoz 286c43
		else	if rtl_match "${1}" "=*"; then
Lucio Andrés Illanes Albornoz b6a9a1
				BUILD_GROUPS_INHIBIT_DEPS=1; _arg="${1#=}";
Lucio Andrés Illanes Albornoz 5f8a15
			else
Lucio Andrés Illanes Albornoz 5f8a15
				_arg="${1}";
Lucio Andrés Illanes Albornoz 5f8a15
			fi;
Lucio Andrés Illanes Albornoz 5f8a15
			case "${_arg}" in
Lucio Andrés Illanes Albornoz 5f8a15
			*=*)		rtl_set_var_unsafe "${_arg%%=*}" "${_arg#*=}"; ;;
Lucio Andrés Illanes Albornoz 5f8a15
			[^a-zA-Z]*)	_rc=1; _status="Error: build group names must start with [a-zA-Z] (in argument \`${_arg}'.)"; ;;
Lucio Andrés Illanes Albornoz 5f8a15
			*[^_a-zA-Z]*)	_rc=1; _status="Error: build group names must not contain [^_a-zA-Z] (in argument \`${_arg}'.)"; ;;
Lucio Andrés Illanes Albornoz 5f8a15
			*)		BUILD_GROUPS="$(rtl_lconcat "${BUILD_GROUPS}" "${_arg}")"; ;;
Lucio Andrés Illanes Albornoz b6a9a1
			esac; shift;
Lucio Andrés Illanes Albornoz 286c43
		fi;
Lucio Andrés Illanes Albornoz 286c43
	done;
Lucio Andrés Illanes Albornoz 286c43
	if [ "${_rc:-0}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz 286c43
		case "${ARG_PARALLEL}" in
Lucio Andrés Illanes Albornoz 286c43
		auto)	if ! ARG_PARALLEL="$(rtl_get_cpu_count)"; then
Lucio Andrés Illanes Albornoz 286c43
				_rc=1; _status="Error: failed to get CPU count.";
Lucio Andrés Illanes Albornoz 286c43
			else
Lucio Andrés Illanes Albornoz 286c43
				ARG_PARALLEL=$((${ARG_PARALLEL}/2));
Lucio Andrés Illanes Albornoz 286c43
			fi; ;;
Lucio Andrés Illanes Albornoz 286c43
		"")	ARG_PARALLEL=1; ;;
Lucio Andrés Illanes Albornoz 286c43
		*)	if ! rtl_isnumber "${ARG_PARALLEL}"; then
Lucio Andrés Illanes Albornoz 286c43
				_rc=1; _status="Error: invalid jobs count \`${ARG_PARALLEL}'.";
Lucio Andrés Illanes Albornoz 286c43
			fi; ;;
Lucio Andrés Illanes Albornoz 286c43
		esac;
Lucio Andrés Illanes Albornoz e1d469
		if [ "${_rc:-0}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz 286c43
			DEFAULT_BUILD_CPUS="${ARG_PARALLEL}";
Lucio Andrés Illanes Albornoz 286c43
			case "${ARG_VERBOSE:-0}" in
Lucio Andrés Illanes Albornoz 286c43
			0)	rtl_fileop_set_log 0; rtl_log_set_lvl 0; ;;
Lucio Andrés Illanes Albornoz 286c43
			1)	rtl_fileop_set_log 0; rtl_log_set_lvl 2; ;;
Lucio Andrés Illanes Albornoz 286c43
			2)	rtl_fileop_set_log 0; rtl_log_set_lvl 2; ;;
Lucio Andrés Illanes Albornoz 286c43
			3)	rtl_fileop_set_log 0; rtl_log_set_lvl 2; ;;
Lucio Andrés Illanes Albornoz 286c43
			4)	rtl_fileop_set_log 1; rtl_log_set_lvl 3; ;;
Lucio Andrés Illanes Albornoz 286c43
			*)	_rc=1; _status="Error: invalid verbosity level (max. -vvvv)"; ;;
Lucio Andrés Illanes Albornoz e1d469
			esac;
Lucio Andrés Illanes Albornoz e1d469
		fi;
Lucio Andrés Illanes Albornoz 8f50a3
	fi;
Lucio Andrés Illanes Albornoz ae3ff0
	return "${_rc}";
Lucio Andrés Illanes Albornoz c3bcd7
};
Lucio Andrés Illanes Albornoz c3bcd7
Lucio Andrés Illanes Albornoz 9d2842
buildp_init_prereqs() {
Lucio Andrés Illanes Albornoz 11ef38
	if ! rtl_check_prereqs \
Lucio Andrés Illanes Albornoz 0d1c4f
			awk bunzip2 bzip2 cat chmod cmake cp date find flock	\
Lucio Andrés Illanes Albornoz b6a9a1
			g++ gcc git grep gunzip gzip hostname id install kill	\
Lucio Andrés Illanes Albornoz 0d1c4f
			ln lzip make mkdir mkfifo mktemp mv paste patch perl	\
Lucio Andrés Illanes Albornoz 0d1c4f
			pgrep pkill printf readlink rm sed sha256sum sort	\
Lucio Andrés Illanes Albornoz b03d3c
			stat tail tar test touch tr uniq wget xz zip; then
Lucio Andrés Illanes Albornoz 11ef38
		printf "%s\n" "${_status}" >&2; exit 1;
Lucio Andrés Illanes Albornoz a778c2
	elif ! awk -V 2>/dev/null | grep -q "^GNU Awk "; then
Lucio Andrés Illanes Albornoz a778c2
		printf "Error: awk(1) in \$PATH must be GNU Awk." >&2; exit 1;
Lucio Andrés Illanes Albornoz 0d1c4f
	elif ! (FNAME="$(mktemp)" && { trap "rm -f \"\${FNAME}\"" EXIT;		\
Lucio Andrés Illanes Albornoz 0d1c4f
			sed -i'' -e '' "${FNAME}" >/dev/null 2>&1; }); then
Lucio Andrés Illanes Albornoz 11ef38
		printf "Error: sed(1) in \${PATH} does not support the \`-i' option.\n" >&2; exit 1;
Lucio Andrés Illanes Albornoz 9d2842
	fi;
Lucio Andrés Illanes Albornoz 9d2842
};
Lucio Andrés Illanes Albornoz 9d2842
Lucio Andrés Illanes Albornoz c3bcd7
build_init() {
Lucio Andrés Illanes Albornoz b6a9a1
	local _rc=0; _status="";
Lucio Andrés Illanes Albornoz 11ef38
	if ! buildp_init_env			\
Lucio Andrés Illanes Albornoz 11ef38
	|| ! buildp_init_getopts "${@}"		\
Lucio Andrés Illanes Albornoz 370e98
	|| ! buildp_init_prereqs		\
Lucio Andrés Illanes Albornoz 11ef38
	|| ! ex_pkg_load_vars			\
Lucio Andrés Illanes Albornoz 11ef38
	|| ! buildp_init_args			\
Lucio Andrés Illanes Albornoz c6d6e0
	|| ! buildp_init_files; then
Lucio Andrés Illanes Albornoz b6a9a1
		_rc=1; _status="${_status}";
Lucio Andrés Illanes Albornoz b6a9a1
	fi; return "${_rc}";
Lucio Andrés Illanes Albornoz (arab, vxp) e2fea1
};
Lucio Andrés Illanes Albornoz (arab, vxp) e2fea1
Lucio Andrés Illanes Albornoz (arab, vxp) e2fea1
# vim:filetype=sh