lalbornoz 340f4c
#!/bin/sh
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
#
lalbornoz 340f4c
{
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
. ./build.subr;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
parse_args_into_vars "${0}" build-scripts,build-steps,help,tarball "${@}";
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
[ -n "${ARG_HELP}" ] && { exec cat build.usage; } || . ./build.vars;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
clear_env_with_except HOME PATH SHELL TERM USER;
Lucio Andrés Illanes Albornoz (arab, vxp) d30b8f
check_path_vars PREFIX PREFIX_NATIVE WORKDIR;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
check_prereqs git make mktemp openssl sed sort tar tr wget;
Lucio Andrés Illanes Albornoz (arab, vxp) d30b8f
log_msg info "Build started by ${BUILD_USER:=${USER}}@${BUILD_HNAME:=$(hostname)} at ${BUILD_DATE:=$(date %Y-%m-%d-%H-%M-%S)}.";
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
log_env_vars ${LOG_ENV_VARS};
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
(mkdir -p ${PREFIX} ${PREFIX_NATIVE} ${PREFIX_TARGET} ${WORKDIR};
Lucio Andrés Illanes Albornoz (arab, vxp) 96b309
BUILD_NFINI=${BUILD_NSKIP:=${BUILD_NFAIL:=${BUILD_NBUILT:=0}}};
Lucio Andrés Illanes Albornoz (arab, vxp) 75a203
for BUILD_LVL in 0 1 2 3; do
lalbornoz 340f4c
	for BUILD_SCRIPT_FNAME in ${BUILD_LVL}[0-9][0-9].*.build; do
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
		if [ -n "${ARG_BUILD_SCRIPTS}" ]\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
		&& [ "${ARG_BUILD_SCRIPTS}" != "ALL" ]\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
		&& ! match_list "${ARG_BUILD_SCRIPTS}"	\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
				, "${BUILD_SCRIPT_FNAME}"; then
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			log_msg info "Skipped build script \`${BUILD_SCRIPT_FNAME}' (--build-scripts policy.)";
lalbornoz 340f4c
			continue;
Lucio Andrés Illanes Albornoz (arab, vxp) 29df05
		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) 1d0876
			if [ "x${ARG_BUILD_SCRIPTS}" != "xALL" ]\
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) 1d0876
			log_msg info "Invoking build script${ARG_BUILD_SCRIPTS:+ (forcibly)} \`${BUILD_SCRIPT_FNAME}'${ARG_BUILD_STEPS:+ at build step(s) ${ARG_BUILD_STEPS}}.";
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			(set -o errexit -- $(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) d30b8f
			 export CFLAGS="$(eval echo \${CFLAGS_LVL${BUILD_LVL}})";	\
Lucio Andrés Illanes Albornoz (arab, vxp) f9c386
			 export PREFIX_LVL="$(eval echo \${PREFIX_LVL${BUILD_LVL}})";	\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			 _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) 1d0876
			 	[ -r ${_PWD}/${SCRIPT_SOURCE} ] &&			\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
					 . ${_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) 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) d30b8f
if [ $(( ${BUILD_NFINI} + ${BUILD_NSKIP} )) -ge 0 ]\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
&& [ ${BUILD_NFAIL} -eq 0 ]\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
&& [ ${ARG_TARBALL:-0} -eq 1 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) d30b8f
	log_msg info "Building distribution tarball.";
Lucio Andrés Illanes Albornoz (arab, vxp) d30b8f
	(cd ${PREFIX};
Lucio Andrés Illanes Albornoz (arab, vxp) d30b8f
	DISTRIB_FNAME=midipix.${BUILD_USER}@${BUILD_HNAME}-${BUILD_DATE}.tar.bz2;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	PREFIX_BASENAME=${PREFIX_NATIVE##*/}; WORKDIR_BASENAME=${WORKDIR##*/};
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	rm_if_exists -m ${PREFIX_BASENAME}/lib.bak; rm_if_exists ${DISTRIB_FNAME};
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	tar -C ${PREFIX_BASENAME}/lib -cpf - . | tar -C ${PREFIX_BASENAME}/lib.bak -xpf -;
Lucio Andrés Illanes Albornoz (arab, vxp) d30b8f
	(cd native/lib &&
Lucio Andrés Illanes Albornoz (arab, vxp) d30b8f
	 find . -maxdepth 1 -type l				\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	 	-exec sh -c 'dest=$(readlink -- "$0") && rm -- "$0" && ln -- "$dest" "$0"' {} \;);
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	 wait;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	 find .	-maxdepth 2 -type d				\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	 	-not -path .					\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	 	-not -path ./${WORKDIR_BASENAME}		\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	 	-not -path ./${WORKDIR_BASENAME}/\*		\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	 	-not -path ./${PREFIX_BASENAME}			\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	 	-not -path ./${PREFIX_BASENAME}/lib.bak		|\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	 tar -T - -cpf - | bzip2 -9c - > ${DISTRIB_FNAME}
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	 rm -rf ${PREFIX_BASENAME}/lib;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	 mv ${PREFIX_BASENAME}/lib.bak ${PREFIX_BASENAME}/lib); wait;
Lucio Andrés Illanes Albornoz (arab, vxp) d30b8f
fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
exit ${BUILD_SCRIPT_RC})} 2>&1 | tee build.log;
lalbornoz 340f4c
lalbornoz 340f4c
# vim:filetype=sh