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) 1d0876
. ./build.subr;
Lucio Andrés Illanes Albornoz (arab, vxp) 806c98
VALID_BUILD_LEVELS="fetch,extract,build_dir,autoconf,patch,configure,clean,build,install";
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
#
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
# Process command line arguments.
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) 0724e5
-c)	ARG_CLEAN=1; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
-d)	disable_build_script_link "${2}"; exit; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
-e)	enable_build_script_link "${2}"; exit; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) f42965
-t*)	export ARG_TARBALL=1; [ "${1#-t.}" != "${1}" ] && TARBALL_SUFFIX=${1#-t.}; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) f42965
-v)	export ARG_VERBOSE=1; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
-x)	ARG_XTRACE=1; set -o xtrace; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
-a)	[ -z "${2}" ] && exec cat build.usage || ARCH="${2}"; shift; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
-b)	[ -z "${2}" ] && exec cat build.usage || BUILD="${2}"; shift; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
-pi)	[ -z "${2}" ] && exec cat build.usage || insert_build_script_link "${2}"; exit; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
-pr)	[ -z "${2}" ] && exec cat build.usage || remove_build_script_link "${2}"; exit; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 6e48a0
-r)	[ -n "${ARG_RESTART_SCRIPT}" ] && exec cat build.usage;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	if [ "${2#*:*}" != "${2}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
		ARG_RESTART_SCRIPT="${2%%:*}"; ARG_RESTART_SCRIPT_AT="${2##*:}";
Lucio Andrés Illanes Albornoz (arab, vxp) a89601
		if [ "${ARG_RESTART_SCRIPT_AT}" != diff ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) a89601
			for __ in $(split , "${ARG_RESTART_SCRIPT_AT}"); do
Lucio Andrés Illanes Albornoz (arab, vxp) a89601
				if ! match_list "${VALID_BUILD_LEVELS}" , "${__}"; then
Lucio Andrés Illanes Albornoz (arab, vxp) a89601
					log_msg fail "Error: unknown build level specified.";
Lucio Andrés Illanes Albornoz (arab, vxp) a89601
					exec cat build.usage;
Lucio Andrés Illanes Albornoz (arab, vxp) a89601
				fi;
Lucio Andrés Illanes Albornoz (arab, vxp) a89601
			done;
Lucio Andrés Illanes Albornoz (arab, vxp) a89601
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	else
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
		ARG_RESTART_SCRIPT="${2}"; ARG_RESTART_SCRIPT_AT=ALL;
Lucio Andrés Illanes Albornoz (arab, vxp) 49ffbe
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 49ffbe
	if [ ! -e "${ARG_RESTART_SCRIPT}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 49ffbe
		log_msg fail "Error: unknown build script specified.";
Lucio Andrés Illanes Albornoz (arab, vxp) 49ffbe
		exec cat build.usage;
Lucio Andrés Illanes Albornoz (arab, vxp) 49ffbe
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 49ffbe
	shift; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
*=*)	set_var_unsafe "${1%%=*}" "${1#*=}"; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 6e48a0
*)	exec cat build.usage; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
esac; shift; done;
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
if [ -e /proc/cpuinfo ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
	BUILD_CPUS=$(awk '/^processor/{cpus++} END{print cpus}' /proc/cpuinfo);
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
# Source the build variables file and its local overrides, if any.
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
for __ in ${HOME}/midipix_build.vars ../midipix_build.vars ./build.vars; do
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	[ -e ${__} ] && . ${__};
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
done;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
#
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
# Clear the environment.
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
for __ in $(export | sed -e 's/^export //' -e 's/=.*$//'); do
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	if ! match_list "${CLEAR_ENV_VARS_EXCEPT}" " " "${__}"; then
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
		unset "${__}";
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
done;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
for __ in ${CHECK_PATH_VARS}; do
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	if [ -z "${___:=$(get_var_unsafe "${__}")}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
		log_msg failexit "Error: variable \`${__}' is empty or unset.";
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	elif [ "${___#* *}" != "${___}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
		log_msg failexit "Error: variable \`${__}' contains one or more whitespace characters.";
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
done;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
for __ in ${CHECK_PREREQ_CMDS} ${CHECK_PREREQ_FILES}; do
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	if [ "${__#/}" != "${__}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
		if [ ! -e "${__}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
			log_msg fail "Error: missing prerequisite file \`${__}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
			__exit=1;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	else
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
		if ! test_cmd "${__}"; then
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
			log_msg fail "Error: missing prerequisite command \`${__}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
			__exit=1;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
done;
Lucio Andrés Illanes Albornoz (arab, vxp) b66e01
for __ in ${CHECK_PREREQ_PERL_MODULES}; do
Lucio Andrés Illanes Albornoz (arab, vxp) b66e01
	if ! perl -M"${__}" -e "" 2>/dev/null; then
Lucio Andrés Illanes Albornoz (arab, vxp) b66e01
		log_msg fail "Error: missing prerequisite Perl module \`${__}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) b66e01
		__exit=1;
Lucio Andrés Illanes Albornoz (arab, vxp) b66e01
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) b66e01
done;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
if [ ${__exit:-0} = 1 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	exit 1;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
elif [ -n "${__exit}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	unset __exit;
Lucio Andrés Illanes Albornoz (arab, vxp) 578dff
fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
# Clean ${PREFIX} if requested.
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
if [ ${ARG_CLEAN:-0} -eq 1 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	log_msg info "-c specified, cleaning prefix...";
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	for __ in ${CLEAR_PREFIX_DIRS}; do
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
		if [ -e ${PREFIX}/${__} ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
			secure_rm ${PREFIX}/${__};
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) 15af12
fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
# Create directory hierarchy and usr -> . symlinks.
Lucio Andrés Illanes Albornoz (arab, vxp) 2a5799
insecure_mkdir ${PREFIX} ${PREFIX_NATIVE} ${PREFIX_CROSS} ${PREFIX_TARGET}/lib ${DLCACHEDIR} ${WORKDIR};
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
for __ in ${PREFIX}/usr ${PREFIX_NATIVE}/usr; do
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	if [ ! -L "${__}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
		secure_rm "${__}"; ln -sf -- . "${__}";
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
done;
Lucio Andrés Illanes Albornoz (arab, vxp) 2a5799
insecure_mkdir ${PREFIX_MINIPIX}/bin;
Lucio Andrés Illanes Albornoz (arab, vxp) 2a5799
for __ in lib libexec share; do
Lucio Andrés Illanes Albornoz (arab, vxp) 2a5799
	if [ ! -e ${PREFIX_MINIPIX}/${__} ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 2a5799
		ln -sf bin ${PREFIX_MINIPIX}/${__};
Lucio Andrés Illanes Albornoz (arab, vxp) 2a5799
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 2a5799
done;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
if [ -e ${BUILD_LOG_FNAME} ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	mv -- ${BUILD_LOG_FNAME} ${BUILD_LOG_LAST_FNAME};
Lucio Andrés Illanes Albornoz (arab, vxp) 15af12
fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
touch ${BUILD_STATUS_IN_PROGRESS_FNAME};
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
{(
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
BUILD_DATE_START="$(date %Y-%m-%d-%H-%M-%S)";
Lucio Andrés Illanes Albornoz (arab, vxp) 96b309
BUILD_NFINI=${BUILD_NSKIP:=${BUILD_NFAIL:=${BUILD_NBUILT:=0}}};
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
BUILD_TIMES_SECS=$(command date +%s);
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
log_msg info "Build started by ${BUILD_USER:=${USER}}@${BUILD_HNAME:=$(hostname)} at ${BUILD_DATE_START}.";
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
log_env_vars "build (global)" ${LOG_ENV_VARS};
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
Lucio Andrés Illanes Albornoz (arab, vxp) f42965
for BUILD_LVL in 0 1 2 3 9; do
lalbornoz 340f4c
	for BUILD_SCRIPT_FNAME in ${BUILD_LVL}[0-9][0-9].*.build; do
Lucio Andrés Illanes Albornoz (arab, vxp) 6e48a0
		if [ -n "${ARG_RESTART_SCRIPT}" ]					\
Lucio Andrés Illanes Albornoz (arab, vxp) 6e48a0
		&& [ "${ARG_RESTART_SCRIPT}" != "ALL" ]					\
Lucio Andrés Illanes Albornoz (arab, vxp) 806c98
		&& [ "${ARG_RESTART_SCRIPT}" != ${BUILD_SCRIPT_FNAME} ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
			if [ ${ARG_XTRACE:-0} -eq 0 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
				log_msg info "Skipped build script \`${BUILD_SCRIPT_FNAME}' (--build-scripts policy.)";
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
			fi;
lalbornoz 340f4c
			continue;
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
		elif [ ! -f "${BUILD_SCRIPT_FNAME}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			log_msg info "Build script \`${BUILD_SCRIPT_FNAME}' non-existent or not a file.";
lalbornoz 340f4c
			continue;
lalbornoz 340f4c
		else
Lucio Andrés Illanes Albornoz (arab, vxp) 96b309
			unset BUILD_SCRIPT_RC; : $((BUILD_NBUILT+=1));
Lucio Andrés Illanes Albornoz (arab, vxp) 9ba5d3
			if [ "${ARG_RESTART_SCRIPT}" != ALL ]\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			&& is_build_script_done finish "${BUILD_SCRIPT_FNAME%.build}"; then
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
				log_msg info "Skipped build script \`${BUILD_SCRIPT_FNAME}' (already built.)";
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
					: $((BUILD_NSKIP+=1)); BUILD_SCRIPT_RC=0; continue;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 6e48a0
			log_msg info "Invoking build script${ARG_RESTART_SCRIPT:+ (forcibly)} \`${BUILD_SCRIPT_FNAME}'${ARG_RESTART_SCRIPT_AT:+ at build step ${ARG_RESTART_SCRIPT_AT}}.";
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
			(set -o errexit -o noglob;					\
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
			 set -- $(split . ${BUILD_SCRIPT_FNAME%%.build*});		\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			 SCRIPT_FNAME=${BUILD_SCRIPT_FNAME};				\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			 SCRIPT_NAME=${SCRIPT_FNAME%%.build*};				\
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
			 export PKG_BUILD=${BUILD};					\
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
			 export PKG_TARGET=${TARGET};					\
Lucio Andrés Illanes Albornoz (arab, vxp) f42965
			 export PKG_PREFIX=$(get_vars_unsafe PKG_LVL${BUILD_LVL}_PREFIX	\
Lucio Andrés Illanes Albornoz (arab, vxp) f42965
					PKG_$(echo ${2} | tr a-z A-Z)_PREFIX);		\
Lucio Andrés Illanes Albornoz (arab, vxp) 92d6c5
			 export MIDIPIX_BUILD_PWD=$(pwd); cd ${WORKDIR};		\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			 for SCRIPT_SOURCE in build.subr ${SCRIPT_NAME}.vars		\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
					${BUILD_SCRIPT_FNAME}; do			\
Lucio Andrés Illanes Albornoz (arab, vxp) 92d6c5
			 	[ -f ${MIDIPIX_BUILD_PWD}/${SCRIPT_SOURCE} ] &&		\
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
					 . ${MIDIPIX_BUILD_PWD}/${SCRIPT_SOURCE};	\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			 done);
lalbornoz 340f4c
			case ${BUILD_SCRIPT_RC:=${?}} in
lalbornoz 340f4c
			0) log_msg succ "Finished build script \`${BUILD_SCRIPT_FNAME}'.";
lalbornoz 340f4c
				: $((BUILD_NFINI+=1)); continue; ;;
lalbornoz 340f4c
			*) log_msg fail "Build failed in build script \`${BUILD_SCRIPT_FNAME}' (last return code ${BUILD_SCRIPT_RC}.).";
lalbornoz 340f4c
				: $((BUILD_NFAIL+=1)); break; ;;
lalbornoz 340f4c
			esac;
lalbornoz 340f4c
		fi;
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) 820be1
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
: $((BUILD_TIMES_SECS=$(command date +%s)-${BUILD_TIMES_SECS}));
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
: $((BUILD_TIMES_HOURS=${BUILD_TIMES_SECS}/3600));
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
: $((BUILD_TIMES_MINUTES=(${BUILD_TIMES_SECS}%3600)/60));
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
: $((BUILD_TIMES_SECS=(${BUILD_TIMES_SECS}%3600)%60));
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) 820be1
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
if [ -f "${BUILD_STATUS_IN_PROGRESS_FNAME}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
	secure_rm ${BUILD_STATUS_IN_PROGRESS_FNAME};
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
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