Blame build.sh

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) 0724e5
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) 0724e5
-nd)	ARG_NO_DOWNLOAD=1; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) d824e1
-pt)	ARG_PEDANTIC=1; ;;
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) 0724e5
-t)	[ ${ARG_TARBALL:-0} -eq 1 ] && exec cat build.usage || ARG_TARBALL=1; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) b94746
-x)	set -o xtrace; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) b94746
-X)	set -o xtrace; ARG_DEBUG_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) 0724e5
esac; shift; done;
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
. ./build.vars;
Lucio Andrés Illanes Albornoz (arab, vxp) b94746
clear_env_with_except ${CLEAR_ENV_VARS_EXCEPT};
Lucio Andrés Illanes Albornoz (arab, vxp) b94746
check_path_vars ${CHECK_PATH_VARS}; check_prereqs ${PREREQ_CMDS};
Lucio Andrés Illanes Albornoz (arab, vxp) b94746
mkdir -p ${PREFIX} ${PREFIX_NATIVE} ${PREFIX_TARGET} ${WORKDIR};
Lucio Andrés Illanes Albornoz (arab, vxp) b8dc4b
update_build_status build_start; build_times_init;
Lucio Andrés Illanes Albornoz (arab, vxp) b94746
trap clean_build_status HUP INT TERM USR1 USR2;
Lucio Andrés Illanes Albornoz (arab, vxp) b94746
Lucio Andrés Illanes Albornoz (arab, vxp) b94746
{(
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
log_msg info "Build started by ${BUILD_USER:=${USER}}@${BUILD_HNAME:=$(hostname)} at ${BUILD_DATE_START}.";
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
log_env_vars ${LOG_ENV_VARS}; [ ${ARG_CLEAN:-0} -eq 1 ] && clean_prefix;
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) 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}.).";
Lucio Andrés Illanes Albornoz (arab, vxp) b94746
				if [ ${ARG_DEBUG_TARBALL:-0} -eq 1 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) b94746
					log_msg info "-X specified, creating debug tarball.";
Lucio Andrés Illanes Albornoz (arab, vxp) b94746
					SCRIPT_NAME=${BUILD_SCRIPT_FNAME%%.build};
Lucio Andrés Illanes Albornoz (arab, vxp) b94746
					SCRIPT_NAME=${SCRIPT_NAME#*.};
Lucio Andrés Illanes Albornoz (arab, vxp) b94746
					SCRIPT_NAME=$(echo "${SCRIPT_NAME}" | tr a-z A-Z);
Lucio Andrés Illanes Albornoz (arab, vxp) b94746
					if [ -z ${PKG_SUBDIR=$(get_var_unsafe PKG_${SCRIPT_NAME}_SUBDIR)} ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) b94746
						PKG_URL=$(get_var_unsafe PKG_${SCRIPT_NAME}_URL);
Lucio Andrés Illanes Albornoz (arab, vxp) b94746
						PKG_FNAME=${PKG_URL##*/};
Lucio Andrés Illanes Albornoz (arab, vxp) b94746
					 	PKG_SUBDIR=${PKG_FNAME%%.tar*};
Lucio Andrés Illanes Albornoz (arab, vxp) b94746
					fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 4e7360
					BUILD_DEBUG_TARBALL_FNAME=${PREFIX}/midipix-debug-${BUILD_USER}@${BUILD_HNAME}_$(date %Y-%m-%d-%H-%M-%S).tar.xz;
Lucio Andrés Illanes Albornoz (arab, vxp) 4e7360
					tar -C ${PREFIX} -cJp				\
Lucio Andrés Illanes Albornoz (arab, vxp) 4e7360
						-f ${BUILD_DEBUG_TARBALL_FNAME}		\
Lucio Andrés Illanes Albornoz (arab, vxp) 4e7360
						build.log $(cd ${PREFIX} &&		\
Lucio Andrés Illanes Albornoz (arab, vxp) 4e7360
							find ${WORKDIR#${PREFIX}/}	\
Lucio Andrés Illanes Albornoz (arab, vxp) 4e7360
								-mindepth 1 -maxdepth 1	\
Lucio Andrés Illanes Albornoz (arab, vxp) 4e7360
								-type d -iname ${PKG_SUBDIR}-*);
Lucio Andrés Illanes Albornoz (arab, vxp) b94746
					log_msg info "Please upload ${BUILD_DEBUG_TARBALL_FNAME} and provide an URL to it in <irc://irc.freenode.net/midipix>.";
Lucio Andrés Illanes Albornoz (arab, vxp) b94746
				fi;
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) 0724e5
build_times_get; 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) 090fd4
if [ $(( ${BUILD_NFINI} + ${BUILD_NSKIP} )) -ge 0 ]					\
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
&& [ ${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) 54dbce
	update_build_status build_finish tarball_start;
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
	log_msg info "Building distribution tarball...";
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
	(cd ${PREFIX}; PREFIX_BASENAME=${PREFIX_NATIVE##*/};
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
	rm_if_exists -m ${PREFIX_BASENAME}/lib.bak;
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) 0724e5
	log_msg info "Backed up ${PREFIX_BASENAME}/lib.";
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) 0724e5
	 	-exec sh -c 'DEST=$(readlink -- "${0}") &&				\
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
			rm -f -- "${0}" && ln -- "${DEST}" "${0}"' {} \;); wait;
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
	 log_msg info "Converted symbolic links in ${PREFIX_BASENAME}/lib to hard links.";
Lucio Andrés Illanes Albornoz (arab, vxp) 4e7360
	TARBALL_FNAME=${TARBALL_FNAME_PREFIX}${BUILD_USER}@${BUILD_HNAME}-${BUILD_DATE_START}.tar.xz;
Lucio Andrés Illanes Albornoz (arab, vxp) 4e7360
	tar -cJpf ${TARBALL_FNAME}							\
Lucio Andrés Illanes Albornoz (arab, vxp) 4e7360
		$(find_with_no_paths "${TARBALL_EXCLUDE_PATHS} native/lib.bak" .	\
Lucio Andrés Illanes Albornoz (arab, vxp) 4e7360
			-mindepth 1 -maxdepth 2 -type d);
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
	 rm -rf ${PREFIX_BASENAME}/lib; mv ${PREFIX_BASENAME}/lib.bak ${PREFIX_BASENAME}/lib);
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
	wait; log_msg info "Finished building distribution tarball.";
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
	update_build_status tarball_finish;
Lucio Andrés Illanes Albornoz (arab, vxp) d30b8f
fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
update_build_status finish;
Lucio Andrés Illanes Albornoz (arab, vxp) b94746
exit ${BUILD_SCRIPT_RC})} 2>&1 | tee ${PREFIX}/build.log;
lalbornoz 340f4c
lalbornoz 340f4c
# vim:filetype=sh