lalbornoz 340f4c
#!/bin/sh
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
# Copyright (c) 2016 Lucio Andrés Illanes Albornoz <l.illanes@gmx.de>
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
#
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
#
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
# Clear the environment.
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
# Source subroutine scripts.
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
# Source the build variables file and its local overrides, if any.
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
# Process command line arguments.
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
for __ in subr/*.subr; do
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	. ./${__};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
done;
Lucio Andrés Illanes Albornoz (arab, vxp) 6e313c
check_cpuinfo;
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) 6e48a0
case ${1} in
Lucio Andrés Illanes Albornoz (arab, vxp) 6e313c
-c)	ARG_CLEAN=1; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 47c5cd
-C)	ARG_CHECK_UPDATES=1; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 6e313c
-n)	ARG_DRYRUN=1 ARG_VERBOSE=1; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 6e313c
-N)	ARG_OFFLINE=1; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) a0561a
-i)	ARG_IGNORE_SHA256SUMS=1; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 6e313c
-t*)	ARG_TARBALL=1; [ "${1#-t.}" != "${1}" ] && TARBALL_SUFFIX=${1#-t.}; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 6e313c
-v)	ARG_VERBOSE=1; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 6e313c
-x)	ARG_XTRACE=1; set -o xtrace; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 6e313c
-a)	[ -z "${2}" ] && exec cat etc/build.usage || ARCH=${2}; shift; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 6e313c
-b)	[ -z "${2}" ] && exec cat etc/build.usage || BUILD=${2}; shift; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
-r)	if [ -z "${2}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 49ffbe
		exec cat build.usage;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	elif [ "${2%:*}" = "${2}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 6e313c
		ARG_RESTART=${2};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	else
Lucio Andrés Illanes Albornoz (arab, vxp) 6e313c
		ARG_RESTART=${2%:*}; ARG_RESTART_AT=${2#*:};
Lucio Andrés Illanes Albornoz (arab, vxp) a0561a
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) a0561a
	if [ -z "${ARG_RESTART_AT}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) a0561a
		ARG_RESTART_AT=ALL;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	fi; shift; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
host_toolchain|native_toolchain|runtime|lib_packages|leaf_packages|devroot|world)
Lucio Andrés Illanes Albornoz (arab, vxp) 6e313c
	BUILD_TARGETS_META="${BUILD_TARGETS_META:+${BUILD_TARGETS_META} }${1}"; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
*=*)	set_var_unsafe "${1%%=*}" "${1#*=}"; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
*)	exec cat etc/build.usage; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
esac; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
done;
Lucio Andrés Illanes Albornoz (arab, vxp) 6e313c
source_vars; clear_env;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
if [ -z "${BUILD_TARGETS_META}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	BUILD_TARGETS_META=world;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
#
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
# Check whether the pathnames in build.vars contain non-empty valid values.
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
# Check whether all prerequisite command names resolve.
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
# Check whether all prerequisite pathnames resolve.
Lucio Andrés Illanes Albornoz (arab, vxp) b66e01
# Check whether all prerequisite Perl modules exist.
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
# Clean ${PREFIX} if requested.
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
# Create directory hierarchy and usr -> . symlinks.
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
check_paths; clean_prefix; create_dirs;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
init_build_log; init_build_progress_file;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
{(init_build_vars;
Lucio Andrés Illanes Albornoz (arab, vxp) 44046a
if [ ${ARG_CHECK_UPDATES:-0} -eq 0 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 0bb2fe
	log_msg info "Build started by ${BUILD_USER:=${USER}}@${BUILD_HNAME:=$(hostname)} at ${BUILD_DATE_START}.";
Lucio Andrés Illanes Albornoz (arab, vxp) 44046a
	log_env_vars "build (global)" ${LOG_ENV_VARS};
Lucio Andrés Illanes Albornoz (arab, vxp) 0bb2fe
else
Lucio Andrés Illanes Albornoz (arab, vxp) 0bb2fe
	log_msg info "Version check run started by ${BUILD_USER:=${USER}}@${BUILD_HNAME:=$(hostname)} at ${BUILD_DATE_START}.";
Lucio Andrés Illanes Albornoz (arab, vxp) 44046a
fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
for BUILD_TARGET_LC in $(subst_tgts ${BUILD_TARGETS_META}); do
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	BUILD_TARGET=$(echo ${BUILD_TARGET_LC} | tr a-z A-Z);
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	for BUILD_PACKAGE_LC in $(get_var_unsafe ${BUILD_TARGET}_PACKAGES); do
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		BUILD_PACKAGE=$(echo ${BUILD_PACKAGE_LC} | tr a-z A-Z);
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		if [ -n "${ARG_RESTART}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) ea7997
			if [ "${ARG_RESTART}" != "ALL" ] &&\
Lucio Andrés Illanes Albornoz (arab, vxp) ea7997
			! match_list ${ARG_RESTART} , ${BUILD_PACKAGE_LC}; then
Lucio Andrés Illanes Albornoz (arab, vxp) 02175d
				log_msg vnfo "Skipped \`${BUILD_PACKAGE_LC}' (-r specified.)";
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
				: $((BUILD_NSKIP+=1)); BUILD_SCRIPT_RC=0; continue;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
			fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 47c5cd
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 47c5cd
		if [ ${ARG_CHECK_UPDATES:-0} -eq 1 ]\
Lucio Andrés Illanes Albornoz (arab, vxp) 47c5cd
		&& [ "${BUILD_PACKAGE#*.*}" = "${BUILD_PACKAGE}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 47c5cd
			if [ ${ARG_DRYRUN:-0} -eq 1 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 47c5cd
				echo check_pkg_updates "${BUILD_PACKAGE_LC}"			\
Lucio Andrés Illanes Albornoz (arab, vxp) 47c5cd
					"$(get_var_unsafe PKG_${BUILD_PACKAGE}_VERSION)"	\
Lucio Andrés Illanes Albornoz (arab, vxp) 47c5cd
					"$(get_var_unsafe PKG_${BUILD_PACKAGE}_URL)"		\
Lucio Andrés Illanes Albornoz (arab, vxp) 47c5cd
					"$(get_var_unsafe PKG_${BUILD_PACKAGE}_URL_TYPE)";
Lucio Andrés Illanes Albornoz (arab, vxp) 47c5cd
			else
Lucio Andrés Illanes Albornoz (arab, vxp) 47c5cd
				check_pkg_updates "${BUILD_PACKAGE_LC}"			\
Lucio Andrés Illanes Albornoz (arab, vxp) 47c5cd
					"$(get_var_unsafe PKG_${BUILD_PACKAGE}_VERSION)"	\
Lucio Andrés Illanes Albornoz (arab, vxp) 47c5cd
					"$(get_var_unsafe PKG_${BUILD_PACKAGE}_URL)"		\
Lucio Andrés Illanes Albornoz (arab, vxp) 47c5cd
					"$(get_var_unsafe PKG_${BUILD_PACKAGE}_URL_TYPE)";
Lucio Andrés Illanes Albornoz (arab, vxp) 47c5cd
			fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 47c5cd
			continue;
Lucio Andrés Illanes Albornoz (arab, vxp) 47c5cd
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 413df3
		if [ -z "${ARG_RESTART}" ]\
Lucio Andrés Illanes Albornoz (arab, vxp) 413df3
		&& is_build_script_done finish "${BUILD_PACKAGE_LC}"; then
Lucio Andrés Illanes Albornoz (arab, vxp) 02175d
			log_msg vnfo "Skipped \`${BUILD_PACKAGE_LC}' (already built.)";
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			: $((BUILD_NSKIP+=1)); BUILD_SCRIPT_RC=0; continue;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) cb29cd
		if [ -e build/${BUILD_PACKAGE_LC}.build ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) cb29cd
			BUILD_SCRIPT_FNAME=build/${BUILD_PACKAGE_LC}.build;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		else
Lucio Andrés Illanes Albornoz (arab, vxp) cb29cd
			BUILD_SCRIPT_FNAME=build/pkg.build;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 02175d
		log_msg vnfo "Invoking build script \`${BUILD_SCRIPT_FNAME}'${ARG_RESTART:+ (forcibly)} for package \`${BUILD_PACKAGE_LC}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		(set -o errexit -o noglob;
Lucio Andrés Illanes Albornoz (arab, vxp) 952e33
		 MIDIPIX_BUILD_PWD=$(pwd); PKG_BUILD=${BUILD}; PKG_TARGET=${TARGET};
Lucio Andrés Illanes Albornoz (arab, vxp) 952e33
		 PKG_PREFIX=$(get_vars_unsafe ${BUILD_TARGET}_PREFIX			\
Lucio Andrés Illanes Albornoz (arab, vxp) 952e33
			PKG_${BUILD_PACKAGE%.*}_PREFIX);				
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		 cd ${WORKDIR}; source_scripts);
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		BUILD_SCRIPT_RC=${?}; case ${BUILD_SCRIPT_RC} in
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		0) log_msg succ "Finished \`${BUILD_PACKAGE_LC}' build.";
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			: $((BUILD_NFINI+=1)); continue; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		*) log_msg fail "Build failed in \`${BUILD_PACKAGE_LC}' (last return code ${BUILD_SCRIPT_RC}.).";
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			: $((BUILD_NFAIL+=1)); break; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		esac;
lalbornoz 340f4c
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	if [ ${BUILD_SCRIPT_RC:-0} -ne 0 ]; then
lalbornoz 340f4c
		break;
lalbornoz 340f4c
	fi;
lalbornoz 340f4c
done;
Lucio Andrés Illanes Albornoz (arab, vxp) 5ee71e
copy_etc; do_strip; do_tarballs; fini_build_vars;
Lucio Andrés Illanes Albornoz (arab, vxp) 96b309
log_msg info "${BUILD_NFINI} finished, ${BUILD_NSKIP} skipped, and ${BUILD_NFAIL} failed builds in ${BUILD_NBUILT} build script(s).";
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
log_msg info "Build time: ${BUILD_TIMES_HOURS} hour(s), ${BUILD_TIMES_MINUTES} minute(s), and ${BUILD_TIMES_SECS} second(s).";
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
fini_build_progress_file;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
exit ${BUILD_SCRIPT_RC})} 2>&1 | tee ${BUILD_LOG_FNAME} &
Lucio Andrés Illanes Albornoz (arab, vxp) e2a4f2
TEE_PID=${!};
Lucio Andrés Illanes Albornoz (arab, vxp) e2a4f2
trap "rm -f ${BUILD_STATUS_IN_PROGRESS_FNAME};	\
Lucio Andrés Illanes Albornoz (arab, vxp) e2a4f2
	log_msg fail \"Build aborted.\";	\
Lucio Andrés Illanes Albornoz (arab, vxp) e2a4f2
	echo kill ${TEE_PID};			\
Lucio Andrés Illanes Albornoz (arab, vxp) e2a4f2
	kill ${TEE_PID}" HUP INT TERM USR1 USR2;
Lucio Andrés Illanes Albornoz (arab, vxp) e2a4f2
wait;
lalbornoz 340f4c
lalbornoz 340f4c
# vim:filetype=sh