lalbornoz 340f4c
#!/bin/sh
Lucio Andrés Illanes Albornoz 4b7725
# Copyright (c) 2016, 2017, 2018, 2019 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
#
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
Lucio Andrés Illanes Albornoz c6d6e0
buildp_ast() {
Lucio Andrés Illanes Albornoz c6d6e0
	local _param="${1}" _pids_killed="";
Lucio Andrés Illanes Albornoz c6d6e0
	if [ "${_param}" = "abort" ]; then
Lucio Andrés Illanes Albornoz c6d6e0
		rtl_log_msg failexit "Build aborted.";
Lucio Andrés Illanes Albornoz c6d6e0
	fi;
Lucio Andrés Illanes Albornoz c6d6e0
	if [ -n "${DEFAULT_BUILD_STATUS_IN_PROGRESS_FNAME}" ]; then
Lucio Andrés Illanes Albornoz c6d6e0
		rtl_fileop rm "${DEFAULT_BUILD_STATUS_IN_PROGRESS_FNAME}";
Lucio Andrés Illanes Albornoz c6d6e0
	fi;
Lucio Andrés Illanes Albornoz c6d6e0
	if rtl_kill_tree "${$}"\
Lucio Andrés Illanes Albornoz c6d6e0
	&& [ -n "${_pids_killed}" ]; then
Lucio Andrés Illanes Albornoz c6d6e0
		rtl_log_msg vnfo "Killed PIDs ${_pids_killed}";
Lucio Andrés Illanes Albornoz c6d6e0
	fi;
Lucio Andrés Illanes Albornoz c6d6e0
};
Lucio Andrés Illanes Albornoz c6d6e0
Lucio Andrés Illanes Albornoz c6d6e0
buildp_dispatch_fail_pkg() {
Lucio Andrés Illanes Albornoz c6d6e0
	local _group_name="${1}" _pkg_name="${2}";
Lucio Andrés Illanes Albornoz c6d6e0
	: $((BUILD_NFAIL+=1)); BUILD_PKGS_FAILED="$(rtl_lconcat "${BUILD_PKGS_FAILED}" "${_pkg_name}")";
Lucio Andrés Illanes Albornoz c6d6e0
	if [ "${ARG_RELAXED:-0}" -eq 1 ]; then
Lucio Andrés Illanes Albornoz c6d6e0
		rtl_log_msg fail "$(printf "Build failed in \`%s', check \`%s' for details." "${_pkg_name}" "${BUILD_WORKDIR}/${_pkg_name}_stderrout.log")";
Lucio Andrés Illanes Albornoz c6d6e0
	else	rtl_log_msg fail "${BUILD_WORKDIR}/${_pkg_name}_stderrout.log:";
Lucio Andrés Illanes Albornoz c6d6e0
		cat "${BUILD_WORKDIR}/${_pkg_name}_stderrout.log";
Lucio Andrés Illanes Albornoz c6d6e0
		if [ -n "${DEFAULT_BUILD_LAST_FAILED_PKG_FNAME}" ]; then
Lucio Andrés Illanes Albornoz c6d6e0
			printf "%s" "${_pkg_name}" > "${DEFAULT_BUILD_LAST_FAILED_PKG_FNAME}";
Lucio Andrés Illanes Albornoz c6d6e0
		fi;
Lucio Andrés Illanes Albornoz c6d6e0
		rtl_log_msg failexit "$(printf "Build failed in \`%s'." "${_pkg_name}")";
Lucio Andrés Illanes Albornoz c6d6e0
	fi;
Lucio Andrés Illanes Albornoz c6d6e0
};
Lucio Andrés Illanes Albornoz c6d6e0
Lucio Andrés Illanes Albornoz c6d6e0
buildp_dispatch_group_state() {
Lucio Andrés Illanes Albornoz 1c6c04
	local _msg="${1}" _group_name="${2}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
	case "${_msg}" in
Lucio Andrés Illanes Albornoz c6d6e0
	finish_group)	rtl_log_msg suc2 "Finished \`${_group_name}' build group."; ;;
Lucio Andrés Illanes Albornoz c6d6e0
	start_group)	rtl_log_msg inf2 "Starting \`${_group_name}' build group..."; ;;
Lucio Andrés Illanes Albornoz c6d6e0
	esac;
Lucio Andrés Illanes Albornoz c6d6e0
};
Lucio Andrés Illanes Albornoz (arab, vxp) fc9417
Lucio Andrés Illanes Albornoz c6d6e0
buildp_dispatch_pkg_state() {
Lucio Andrés Illanes Albornoz c6d6e0
	local _msg="${1}" _group_name="${2}" _pkg_name="${3}";
Lucio Andrés Illanes Albornoz c6d6e0
	case "${_msg}" in
Lucio Andrés Illanes Albornoz c6d6e0
	disabled_pkg)	: $((BUILD_NSKIP+=1)); rtl_log_msg vnfo "$(printf "Skipping disabled package \`%s.'" "${_pkg_name}")"; ;;
Lucio Andrés Illanes Albornoz c6d6e0
	missing_pkg)	rtl_log_msg failexit "Error: package \`${_pkg_name}' missing in build.vars."; ;;
Lucio Andrés Illanes Albornoz c6d6e0
	skipped_pkg)	: $((BUILD_NSKIP+=1)); rtl_log_msg vnfo "$(printf "Skipping finished package \`%s.'" "${_pkg_name}")"; ;;
Lucio Andrés Illanes Albornoz c6d6e0
	start_pkg)	rtl_log_msg info "$(printf "[%03d/%03d] Starting \`%s' build..." "${4}" "${5}" "${_pkg_name}")"; ;;
Lucio Andrés Illanes Albornoz c6d6e0
	step_pkg)	rtl_log_msg vucc "$(printf "Finished build step %s of package \`%s'." "${4}" "${_pkg_name}")"; ;;
Lucio Andrés Illanes Albornoz c6d6e0
	finish_pkg)
Lucio Andrés Illanes Albornoz c6d6e0
		: $((BUILD_NFINI+=1));
Lucio Andrés Illanes Albornoz c6d6e0
		if [ "${ARG_VERBOSE:-0}" -ge 2 ]; then
Lucio Andrés Illanes Albornoz c6d6e0
			cat "${BUILD_WORKDIR}/${_pkg_name}_stderrout.log";
Lucio Andrés Illanes Albornoz c6d6e0
		fi;
Lucio Andrés Illanes Albornoz c6d6e0
		rtl_log_msg succ "$(printf "Finished \`%s' build." "${_pkg_name}")"; ;;
Lucio Andrés Illanes Albornoz c6d6e0
	start_pkg_child)
Lucio Andrés Illanes Albornoz c6d6e0
		if [ "${PKG_NO_LOG_VARS:-0}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz c6d6e0
			rtl_log_env_vars "build" $(set | awk -F= '/^PKG_/{print $1}' | sort);
Lucio Andrés Illanes Albornoz c6d6e0
		fi;
Lucio Andrés Illanes Albornoz c6d6e0
		if [ "${ARG_VERBOSE:-0}" -ge 3 ]; then
Lucio Andrés Illanes Albornoz c6d6e0
			set -o xtrace;
Lucio Andrés Illanes Albornoz c6d6e0
		fi; ;;
Lucio Andrés Illanes Albornoz c6d6e0
	esac;
Lucio Andrés Illanes Albornoz c6d6e0
};
Lucio Andrés Illanes Albornoz (arab, vxp) fc9417
Lucio Andrés Illanes Albornoz c6d6e0
buildp_dispatch() {
Lucio Andrés Illanes Albornoz c6d6e0
	local _msg="${1}"; shift;
Lucio Andrés Illanes Albornoz c6d6e0
	case "${_msg}" in
Lucio Andrés Illanes Albornoz c6d6e0
	disabled_pkg|finish_pkg|missing_pkg|skipped_pkg|start_pkg|start_pkg_child|step_pkg)
Lucio Andrés Illanes Albornoz c6d6e0
		buildp_dispatch_pkg_state "${_msg}" "${@}"; ;;
Lucio Andrés Illanes Albornoz c6d6e0
	finish_group|start_group)
Lucio Andrés Illanes Albornoz c6d6e0
		buildp_dispatch_group_state "${_msg}" "${@}"; ;;
Lucio Andrés Illanes Albornoz dda17d
	*)	if command -v "buildp_dispatch_${_msg}" >/dev/null 2>&1; then
Lucio Andrés Illanes Albornoz c6d6e0
			"buildp_dispatch_${_msg}" "${@}";
Lucio Andrés Illanes Albornoz c6d6e0
		fi; ;;
Lucio Andrés Illanes Albornoz c6d6e0
	esac;
Lucio Andrés Illanes Albornoz c6d6e0
};
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
Lucio Andrés Illanes Albornoz c6d6e0
build() {
Lucio Andrés Illanes Albornoz dda17d
	local	_build_time_hours=0 _build_time_mins=0 _build_time_secs=0	\
Lucio Andrés Illanes Albornoz dda17d
		BUILD_DATE_START="" BUILD_NFAIL=0 BUILD_NFINI=0 BUILD_NSKIP=0	\
Lucio Andrés Illanes Albornoz dda17d
		BUILD_PKGS_FAILED="" EX_PKG_DISPATCH_UNKNOWN="";
Lucio Andrés Illanes Albornoz c6d6e0
	if trap "buildp_ast abort" HUP INT TERM USR1 USR2\
Lucio Andrés Illanes Albornoz c6d6e0
	&& trap "buildp_ast exit" EXIT\
Lucio Andrés Illanes Albornoz c6d6e0
	&& . ./subr/build_init.subr && build_init "${@}"; then
Lucio Andrés Illanes Albornoz c6d6e0
		_build_date_start="$(rtl_date %Y-%m-%d-%H-%M-%S)"; _build_time_secs="$(rtl_date %s)";
Lucio Andrés Illanes Albornoz c6d6e0
		rtl_log_msg info "Build started by ${BUILD_USER:=${USER}}@${BUILD_HNAME:=$(hostname)} at ${_build_date_start}.";
Lucio Andrés Illanes Albornoz c6d6e0
		rtl_log_env_vars "build (global)" ${DEFAULT_LOG_ENV_VARS};
Lucio Andrés Illanes Albornoz c6d6e0
		if ! ex_pkg_dispatch "${DEFAULT_BUILD_STEPS}" "${DEFAULT_BUILD_VARS}"		\
Lucio Andrés Illanes Albornoz c6d6e0
				buildp_dispatch "${BUILD_GROUPS}" "${ARG_PARALLEL:-1}"		\
Lucio Andrés Illanes Albornoz c6d6e0
				"${BUILD_WORKDIR}/build.fifo" "${ARG_RESTART}"			\
Lucio Andrés Illanes Albornoz c6d6e0
				"${ARG_RESTART_AT}" "${ARG_RESTART_RECURSIVE}" "${BUILD_WORKDIR}"; then
Lucio Andrés Illanes Albornoz c6d6e0
			rtl_log_msg failexit "Error: package \`${EX_PKG_DISPATCH_UNKNOWN}' unknown.";
Lucio Andrés Illanes Albornoz c6d6e0
		else	: $((_build_time_secs=$(rtl_date %s)-${_build_time_secs}));
Lucio Andrés Illanes Albornoz c6d6e0
			: $((_build_time_hours=${_build_time_secs}/3600));
Lucio Andrés Illanes Albornoz c6d6e0
			: $((_build_time_minutes=(${_build_time_secs}%3600)/60));
Lucio Andrés Illanes Albornoz c6d6e0
			: $((_build_time_secs=(${_build_time_secs}%3600)%60));
Lucio Andrés Illanes Albornoz c6d6e0
			rtl_log_msg info "${BUILD_NFINI:-0} finished, ${BUILD_NSKIP:-0} skipped, and ${BUILD_NFAIL:-0} failed package(s).";
Lucio Andrés Illanes Albornoz c6d6e0
			rtl_log_msg info "Build time: ${_build_time_hours:-0} hour(s), ${_build_time_minutes:-0} minute(s), and ${_build_time_secs:-0} second(s).";
Lucio Andrés Illanes Albornoz c6d6e0
			if [ -n "${BUILD_PKGS_FAILED}" ]; then
Lucio Andrés Illanes Albornoz c6d6e0
				rtl_log_msg failexit "Build script failure(s) in: ${BUILD_PKGS_FAILED}.";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			fi;
Lucio Andrés Illanes Albornoz c6d6e0
		fi;
Lucio Andrés Illanes Albornoz c6d6e0
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
};
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
Lucio Andrés Illanes Albornoz c6d6e0
set +o errexit -o noglob; build "${@}";
lalbornoz 340f4c
Lucio Andrés Illanes Albornoz fc9bdb
# vim:filetype=sh textwidth=0