Blame build.sh

lalbornoz 340f4c
#!/bin/sh
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
#
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
lalbornoz 340f4c
{
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
. ./build.subr;
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) 6e48a0
-r)	[ -n "${ARG_RESTART_SCRIPT}" ] && exec cat build.usage;
Lucio Andrés Illanes Albornoz (arab, vxp) 9ba5d3
	match_any "${2}" :								\
Lucio Andrés Illanes Albornoz (arab, vxp) 9ba5d3
		&& { ARG_RESTART_SCRIPT="${2%%:*}"; ARG_RESTART_SCRIPT_AT="${2##*:}"; }	\
Lucio Andrés Illanes Albornoz (arab, vxp) 9ba5d3
		|| { ARG_RESTART_SCRIPT="${2}"; ARG_RESTART_SCRIPT_AT=ALL; };
Lucio Andrés Illanes Albornoz (arab, vxp) 6e48a0
	shift; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 6e48a0
-t)	[ ${ARG_TARBALL:-0} -eq 1 ] && exec cat build.usage;
Lucio Andrés Illanes Albornoz (arab, vxp) 6e48a0
	ARG_TARBALL=1; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 6e48a0
*=*)	set_var_unsafe "$(get_prefix_lrg "${1}" =)"					\
Lucio Andrés Illanes Albornoz (arab, vxp) 6e48a0
			"$(get_postfix "${1}" =)"; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 6e48a0
*)	exec cat build.usage; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 6e48a0
esac; shift; done; . ./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) c13296
check_prereqs git make mktemp openssl patch 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) 6e48a0
touch ${PREFIX}/BUILD_IN_PROGRESS ${BUILD_PROGRESS_FNAME:=${PREFIX}/BUILD_STARTED_AT_${BUILD_DATE}};
Lucio Andrés Illanes Albornoz (arab, vxp) 96b309
BUILD_NFINI=${BUILD_NSKIP:=${BUILD_NFAIL:=${BUILD_NBUILT:=0}}};
Lucio Andrés Illanes Albornoz (arab, vxp) fb434b
BUILD_SECS=$(command date +%s);
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) 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) 6e48a0
		&& ! match_list "${ARG_RESTART_SCRIPT}"					\
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) 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) 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) 090fd4
			 	[ -f ${_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) fb434b
: $((BUILD_SECS=$(command date +%s)-${BUILD_SECS}));
Lucio Andrés Illanes Albornoz (arab, vxp) fb434b
: $((BUILD_HOURS=${BUILD_SECS}/3600));
Lucio Andrés Illanes Albornoz (arab, vxp) fb434b
: $((BUILD_MINUTES=(${BUILD_SECS}%3600)/60));
Lucio Andrés Illanes Albornoz (arab, vxp) fb434b
: $((BUILD_SECS=(${BUILD_SECS}%3600)%60));
Lucio Andrés Illanes Albornoz (arab, vxp) fb434b
log_msg info "Build time: ${BUILD_HOURS} hour(s), ${BUILD_MINUTES} minute(s), and ${BUILD_SECS} second(s).";
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
[ -f ${BUILD_PROGRESS_FNAME} ] && rm -f ${BUILD_PROGRESS_FNAME};
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
touch ${BUILD_FINISH_FNAME:=${PREFIX}/BUILD_FINISHED_AT_$(date %Y-%m-%d-%H-%M-%S)}	\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	${TARBALL_PROGRESS_FNAME:=${PREFIX}/TARBALL_STARTED_AT_$(date %Y-%m-%d-%H-%M-%S)};
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
ln -sf ${BUILD_FINISH_FNAME} ${PREFIX}/BUILD_FINISHED_AT;
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
# rotate BUILD_FINISH_FNAME files
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
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) 090fd4
	 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) 090fd4
	 find .	-maxdepth 2 -type d							\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	 	-not -path .								\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	 	-not -path ./src/midipix_build						\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	 	-not -path ./src/midipix_build/\*					\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	 	-not -path ./${WORKDIR_BASENAME}					\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	 	-not -path ./${WORKDIR_BASENAME}/\*					\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	 	-not -path ./${PREFIX_BASENAME}						\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	 	-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) 090fd4
	# rotate tarballs
Lucio Andrés Illanes Albornoz (arab, vxp) d30b8f
fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
[ -f ${TARBALL_PROGRESS_FNAME} ] && rm -f ${TARBALL_PROGRESS_FNAME};
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
[ -f BUILD_IN_PROGRESS ] && rm -f BUILD_IN_PROGRESS;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
exit ${BUILD_SCRIPT_RC})} 2>&1 | tee build.log;
lalbornoz 340f4c
lalbornoz 340f4c
# vim:filetype=sh