Blame build.sh

lalbornoz 340f4c
#!/bin/sh
Lucio Andrés Illanes Albornoz e3e4f9
# Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021 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 e381c3
	trap '' HUP INT TERM USR1 USR2;
Lucio Andrés Illanes Albornoz 725770
	local _param="${1}" _pids="" _pids_niter=0 _pkg_name="" RTL_KILL_TREE_PIDS="";
Lucio Andrés Illanes Albornoz c6d6e0
	if [ "${_param}" = "abort" ]; then
Lucio Andrés Illanes Albornoz e493dc
		rtl_log_msg "fatalexit" "${MSG_build_aborted}";
Lucio Andrés Illanes Albornoz c6d6e0
	fi;
Lucio Andrés Illanes Albornoz a97175
	while [ "${_pids_niter}" -lt 8 ]; do
Lucio Andrés Illanes Albornoz a97175
		_pids="$(rtl_lconcat "${_pids}" "${RTL_KILL_TREE_PIDS}")"; RTL_KILL_TREE_PIDS="";
Lucio Andrés Illanes Albornoz a97175
		if ! rtl_kill_tree "${$}" "TERM"\
Lucio Andrés Illanes Albornoz a97175
		|| [ -z "${RTL_KILL_TREE_PIDS}" ]; then
Lucio Andrés Illanes Albornoz 647a12
			break;
Lucio Andrés Illanes Albornoz a97175
		else
Lucio Andrés Illanes Albornoz a97175
			: $((_pids_niter+=1));
Lucio Andrés Illanes Albornoz 647a12
		fi;
Lucio Andrés Illanes Albornoz 647a12
	done;
Lucio Andrés Illanes Albornoz a97175
	if [ -n "${_pids}" ]; then
Lucio Andrés Illanes Albornoz e493dc
		rtl_log_msg "verbose" "${MSG_build_killed_pids}" "$(rtl_uniq ${_pids})";
Lucio Andrés Illanes Albornoz a97175
	fi;
Lucio Andrés Illanes Albornoz 370e98
	if [ -n "${EX_PKG_DISPATCH_WAIT}" ]\
Lucio Andrés Illanes Albornoz 370e98
	&& [ "${ARG_RESET_PKG}" -eq 1 ]; then
Lucio Andrés Illanes Albornoz 725770
		for _pkg_name in ${EX_PKG_DISPATCH_WAIT}; do
Lucio Andrés Illanes Albornoz 725770
			rtl_state_clear "${BUILD_WORKDIR}" "${_pkg_name}";
Lucio Andrés Illanes Albornoz 725770
		done;
Lucio Andrés Illanes Albornoz e493dc
		rtl_log_msg "verbose" "${MSG_build_reset_pkg_state}" "${EX_PKG_DISPATCH_WAIT}";
Lucio Andrés Illanes Albornoz 725770
	fi;
Lucio Andrés Illanes Albornoz 8cae5b
	if [ -n "${DEFAULT_BUILD_STATUS_IN_PROGRESS_FNAME}" ]; then
Lucio Andrés Illanes Albornoz 8cae5b
		rtl_fileop rm "${DEFAULT_BUILD_STATUS_IN_PROGRESS_FNAME}";
Lucio Andrés Illanes Albornoz 8cae5b
	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 b6a9a1
	if [ "${ARG_RELAXED:-0}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz e493dc
		rtl_log_msg "fatal" "${MSG_pkg_stderrout_log}" "${BUILD_WORKDIR}" "${_pkg_name}";
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 3e5303
			printf "%s\n" "${_pkg_name}" > "${DEFAULT_BUILD_LAST_FAILED_PKG_FNAME}";
Lucio Andrés Illanes Albornoz c6d6e0
		fi;
Lucio Andrés Illanes Albornoz e493dc
		rtl_log_msg "fatal" "${MSG_build_failed_in}" "${_pkg_name}" "${BUILD_WORKDIR}/${_pkg_name}_stderrout.log";
Lucio Andrés Illanes Albornoz e493dc
	else	rtl_log_msg "warning" "${MSG_build_failed_in}" "${_pkg_name}" "${BUILD_WORKDIR}/${_pkg_name}_stderrout.log";
Lucio Andrés Illanes Albornoz b6a9a1
	fi;
Lucio Andrés Illanes Albornoz b6a9a1
	if [ "${ARG_DUMP_ON_ABORT:-0}" -eq 1 ]; then
Lucio Andrés Illanes Albornoz e493dc
		rtl_log_msg "pkg_faildump" "${MSG_pkg_faildump1}" "${_pkg_name}" "${BUILD_WORKDIR}/${_pkg_name}.dump";
Lucio Andrés Illanes Albornoz e493dc
		rtl_log_msg "pkg_faildump" "${MSG_pkg_faildump2}" "${ARCH}" "${BUILD_KIND}" "${_pkg_name}" "${PREFIX}";
Lucio Andrés Illanes Albornoz b6a9a1
	fi;
Lucio Andrés Illanes Albornoz b6a9a1
	if [ "${ARG_RELAXED:-0}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz 8a27f9
		exit 1;
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 e493dc
	finish_group)	rtl_log_msg "group_finish" "${MSG_group_finish}" "${6}" "${4}" "${5}" "${_group_name}"; ;;
Lucio Andrés Illanes Albornoz e493dc
	start_group)	rtl_log_msg "group_begin" "${MSG_group_begin}" "${6}" "${4}" "${5}" "${_group_name}"; ;;
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 e493dc
	disabled_pkg)	: $((BUILD_NSKIP+=1)); rtl_log_msg "pkg_skip" "${MSG_pkg_skip_disabled}" "${_pkg_name}"; ;;
Lucio Andrés Illanes Albornoz e493dc
	missing_pkg)	rtl_log_msg "fatalexit" "${MSG_pkg_skip_unknown}" "${_pkg_name}"; ;;
Lucio Andrés Illanes Albornoz e493dc
	msg_pkg)	shift 3; rtl_log_msg "${MSG_pkg_msg}" "${_group_name}" "${_pkg_name}" "${*}"; ;;
Lucio Andrés Illanes Albornoz e493dc
	skipped_pkg)	: $((BUILD_NSKIP+=1)); rtl_log_msg "pkg_skip" "${MSG_pkg_skip_finished}" "${_pkg_name}"; ;;
Lucio Andrés Illanes Albornoz e493dc
	start_pkg)	rtl_log_msg "pkg_begin" "${MSG_pkg_begin}" "${7}" "${6}" "${4}" "${5}" "${_pkg_name}"; ;;
Lucio Andrés Illanes Albornoz e493dc
	step_pkg)	rtl_log_msg "pkg_step" "${MSG_pkg_step}" "${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 b1d432
		if rtl_lmatch "${ARG_VERBOSE_TAGS}" "build" ","; 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 e493dc
		rtl_log_msg "pkg_finish" "${MSG_pkg_finish}" "${7}" "${6}" "${4}" "${5}" "${_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 ac9e42
			rtl_log_env_vars "info" "build" $(rtl_get_vars_fast "^PKG_");
Lucio Andrés Illanes Albornoz c6d6e0
		fi;
Lucio Andrés Illanes Albornoz b1d432
		if rtl_lmatch "${ARG_VERBOSE_TAGS}" "xtrace" ","; 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 2960a4
	disabled_pkg|finish_pkg|missing_pkg|msg_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 b6a9a1
buildp_time_init() {
Lucio Andrés Illanes Albornoz b6a9a1
	BUILD_DATE_START="$(rtl_date %Y-%m-%d-%H-%M-%S)";
Lucio Andrés Illanes Albornoz b6a9a1
	_build_time_secs="$(rtl_date %s)";
Lucio Andrés Illanes Albornoz b6a9a1
};
Lucio Andrés Illanes Albornoz b6a9a1
Lucio Andrés Illanes Albornoz b6a9a1
buildp_time_update() {
Lucio Andrés Illanes Albornoz b6a9a1
	: $((_build_time_secs=$(rtl_date %s)-${_build_time_secs}));
Lucio Andrés Illanes Albornoz b6a9a1
	: $((_build_time_hours=${_build_time_secs}/3600));
Lucio Andrés Illanes Albornoz b6a9a1
	: $((_build_time_minutes=(${_build_time_secs}%3600)/60));
Lucio Andrés Illanes Albornoz b6a9a1
	: $((_build_time_secs=(${_build_time_secs}%3600)%60));
Lucio Andrés Illanes Albornoz b6a9a1
};
Lucio Andrés Illanes Albornoz b6a9a1
Lucio Andrés Illanes Albornoz c6d6e0
build() {
Lucio Andrés Illanes Albornoz b6a9a1
	local	_build_time_hours=0 _build_time_mins=0 _build_time_secs=0 _pkg_name="" _rc=0 _status=""			\
Lucio Andrés Illanes Albornoz bbb50c
		BUILD_DATE_START="" BUILD_GROUPS="" BUILD_GROUPS_INHIBIT_DEPS=0 BUILD_HNAME BUILD_IS_PARENT=1		\
Lucio Andrés Illanes Albornoz b6a9a1
		BUILD_NFAIL=0 BUILD_NFINI=0 BUILD_NSKIP=0 BUILD_PKGS_FAILED="" BUILD_TARGET="" BUILD_USER=""		\
Lucio Andrés Illanes Albornoz b6a9a1
		DEFAULT_BUILD_CPUS=1 DEFAULT_BUILD_LAST_FAILED_PKG_FNAME="" DEFAULT_BUILD_LOG_FNAME=""			\
Lucio Andrés Illanes Albornoz b6a9a1
		DEFAULT_BUILD_STEPS="" DEFAULT_BUILD_VARS="" DEFAULT_CLEAR_PREFIX_PATHS="" DEFAULT_GIT_ARGS=""		\
Lucio Andrés Illanes Albornoz 7bede2
		DEFAULT_GITROOT_HEAD="${DEFAULT_GITROOT_HEAD:-}" DEFAULT_LOG_ENV_VARS="" DEFAULT_MIRRORS=""		\
Lucio Andrés Illanes Albornoz 7bede2
		DEFAULT_TARGET="" DEFAULT_WGET_ARGS="" MIDIPIX_BUILD_PWD="";
Lucio Andrés Illanes Albornoz 7bede2
	DEFAULT_BUILD_STATUS_IN_PROGRESS_FNAME=""; EX_PKG_DISPATCH_WAIT="";
Lucio Andrés Illanes Albornoz b6a9a1
	if ! . "${0%/*}/subr/build_init.subr"; then
Lucio Andrés Illanes Albornoz b1d432
		printf "Error: failed to source \`${0%/*}/subr/build_init.subr'." >&2; exit "${_rc}";
Lucio Andrés Illanes Albornoz b6a9a1
	elif ! build_init "${@}"; then
Lucio Andrés Illanes Albornoz 8600e6
		printf "Error during initialisation: %s\n" "${_status}" >&2; exit "${_rc}";
Lucio Andrés Illanes Albornoz b6a9a1
	elif [ -n "${_status}" ]; then
Lucio Andrés Illanes Albornoz b6a9a1
		_rc=0; _status="${_status}";
Lucio Andrés Illanes Albornoz b6a9a1
	else	trap "buildp_ast exit" EXIT; trap "buildp_ast abort" HUP INT TERM USR1 USR2;
Lucio Andrés Illanes Albornoz b6a9a1
		buildp_time_init;
Lucio Andrés Illanes Albornoz e493dc
		rtl_log_msg "build_begin" "${MSG_build_begin}" "${BUILD_USER}" "${BUILD_HNAME}" "${BUILD_DATE_START}";
Lucio Andrés Illanes Albornoz b1d432
		rtl_log_env_vars "build_vars" "build (global)" ${DEFAULT_LOG_ENV_VARS};
Lucio Andrés Illanes Albornoz b6a9a1
		ex_pkg_dispatch "${DEFAULT_BUILD_STEPS}" "${DEFAULT_BUILD_VARS}"			\
Lucio Andrés Illanes Albornoz b6a9a1
				buildp_dispatch "${BUILD_GROUPS}" "${BUILD_GROUPS_INHIBIT_DEPS}"	\
Lucio Andrés Illanes Albornoz b6a9a1
				"${ARG_PARALLEL}" "${BUILD_WORKDIR}/build.fifo" "${ARG_RESTART}"	\
Lucio Andrés Illanes Albornoz b6a9a1
				"${ARG_RESTART_AT}" "${ARG_RESTART_RECURSIVE}" "${BUILD_WORKDIR}";
Lucio Andrés Illanes Albornoz b6a9a1
		buildp_time_update;
Lucio Andrés Illanes Albornoz e493dc
		rtl_log_msg "build_finish" "${MSG_build_finish}" "${BUILD_NFINI:-0}" "${BUILD_NSKIP:-0}" "${BUILD_NFAIL:-0}";
Lucio Andrés Illanes Albornoz e493dc
		rtl_log_msg "build_finish_time" "${MSG_build_finish_time}" "${_build_time_hours:-0}" "${_build_time_minutes:-0}" "${_build_time_secs:-0}";
Lucio Andrés Illanes Albornoz b6a9a1
		if [ -n "${BUILD_PKGS_FAILED}" ]; then
Lucio Andrés Illanes Albornoz b6a9a1
			_rc=1; _status="Build script failure(s) in: ${BUILD_PKGS_FAILED}.";
Lucio Andrés Illanes Albornoz c6d6e0
		fi;
Lucio Andrés Illanes Albornoz c6d6e0
	fi;
Lucio Andrés Illanes Albornoz b6a9a1
	if [ "${_rc}" -ne 0 ]; then
Lucio Andrés Illanes Albornoz dc07ba
		rtl_log_msg "fatalexit" "0;${_status}";
Lucio Andrés Illanes Albornoz b6a9a1
	elif [ -n "${_status}" ]; then
Lucio Andrés Illanes Albornoz dc07ba
		rtl_log_msg "info" "0;${_status}";
Lucio Andrés Illanes Albornoz b6a9a1
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
};
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
Lucio Andrés Illanes Albornoz e493dc
set +o errexit -o noglob -o nounset; build "${@}";
lalbornoz 340f4c
Lucio Andrés Illanes Albornoz fc9bdb
# vim:filetype=sh textwidth=0