Conan_Kudo / build / midipix_build

Forked from build/midipix_build 4 years ago
Clone

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) 963869
[ -f ${HOME}/midipix_build.vars ] && . ${HOME}/midipix_build.vars;
Lucio Andrés Illanes Albornoz (arab, vxp) 963869
[ -f ../midipix_build.vars ] && . ../midipix_build.vars;
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) ffc940
{(
Lucio Andrés Illanes Albornoz (arab, vxp) ffc940
update_build_status build_start; build_times_init; trap "clean_build_status abort; exit 1" HUP INT TERM USR1 USR2;
Lucio Andrés Illanes Albornoz (arab, vxp) ffc940
log_msg info "Build started by ${BUILD_USER:=${USER}}@${BUILD_HNAME:=$(hostname)} at ${BUILD_DATE_START}.";
Lucio Andrés Illanes Albornoz (arab, vxp) c288df
log_env_vars "build (global)" ${LOG_ENV_VARS}; [ ${ARG_CLEAN:-0} -eq 1 ] && clean_prefix;
Lucio Andrés Illanes Albornoz (arab, vxp) 951620
mkdir -p ${PREFIX} ${PREFIX_NATIVE} ${PREFIX_TARGET} ${PREFIX_TARGET}/lib ${WORKDIR};
Lucio Andrés Illanes Albornoz (arab, vxp) 15af12
if [ -d ${PREFIX}/usr -o -f ${PREFIX}/usr -o -L ${PREFIX}/usr ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 15af12
	rm -rf ${PREFIX}/usr;
Lucio Andrés Illanes Albornoz (arab, vxp) 15af12
fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 15af12
ln -sf . ${PREFIX}/usr;
Lucio Andrés Illanes Albornoz (arab, vxp) 15af12
if [ -d ${PREFIX_NATIVE}/usr -o -f ${PREFIX_NATIVE}/usr -o -L ${PREFIX_NATIVE}/usr ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 15af12
	rm -rf ${PREFIX_NATIVE}/usr;
Lucio Andrés Illanes Albornoz (arab, vxp) 15af12
fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 15af12
ln -sf . ${PREFIX_NATIVE}/usr;
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) f9c386
			 export PREFIX_LVL="$(eval echo \${PREFIX_LVL${BUILD_LVL}})";	\
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) 92d6c5
					 . ${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}.).";
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) 95bf77
	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) 95bf77
	find native/lib -maxdepth 1 -type l						\
Lucio Andrés Illanes Albornoz (arab, vxp) 86c0f5
	 	-exec sh -c '[ -f "${DEST:=native/lib/$(readlink -- "${0}")}" ] &&	\
Lucio Andrés Illanes Albornoz (arab, vxp) 86c0f5
			rm -f -- "${0}" && ln -f -- "${DEST}" "${0}"' {} \;;
Lucio Andrés Illanes Albornoz (arab, vxp) 95bf77
	log_msg info "Converted symbolic links in ${PREFIX_BASENAME}/lib to hard links.";
Lucio Andrés Illanes Albornoz (arab, vxp) b0384a
	
Lucio Andrés Illanes Albornoz (arab, vxp) 95bf77
	tar -cJpf ${TARBALL_FNAME_PREFIX}${BUILD_USER}@${BUILD_HNAME}-${BUILD_DATE_START}.tar.xz\
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) c288df
			-mindepth 1 -maxdepth 2 -type d -not -path ./native)		\
Lucio Andrés Illanes Albornoz (arab, vxp) c288df
		"Create \`Midipix mintty shell' shortcut.vbs" midipix.sh;
Lucio Andrés Illanes Albornoz (arab, vxp) ca3031
	log_msg info "Finished building distribution tarball.";
Lucio Andrés Illanes Albornoz (arab, vxp) 70df12
	if [ -x "$(which gpg 2>/dev/null)" -a -n "${TARBALL_SRC_SIGN_GPG_KEY}" ] &&\
Lucio Andrés Illanes Albornoz (arab, vxp) d3b354
				gpg --list-keys "${TARBALL_SRC_SIGN_GPG_KEY}" >/dev/null 2>&1; then
Lucio Andrés Illanes Albornoz (arab, vxp) 52bc51
		gpg --armor --passphrase-file /dev/null					\
Lucio Andrés Illanes Albornoz (arab, vxp) d3b354
			--local-user "${TARBALL_SRC_SIGN_GPG_KEY}" --sign		\
Lucio Andrés Illanes Albornoz (arab, vxp) d3b354
			${TARBALL_FNAME_PREFIX}${BUILD_USER}@${BUILD_HNAME}-${BUILD_DATE_START}.tar.xz;
Lucio Andrés Illanes Albornoz (arab, vxp) d3b354
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 95bf77
	rm -rf ${PREFIX_BASENAME}/lib; mv ${PREFIX_BASENAME}/lib.bak ${PREFIX_BASENAME}/lib;
Lucio Andrés Illanes Albornoz (arab, vxp) ca3031
	log_msg info "Restored ${PREFIX_BASENAME}/lib.";
Lucio Andrés Illanes Albornoz (arab, vxp) ca3031
	log_msg info "Building source tarball...";
Lucio Andrés Illanes Albornoz (arab, vxp) ca3031
	tar -cJpf ${TARBALL_SRC_FNAME_PREFIX}${BUILD_USER}@${BUILD_HNAME}-${BUILD_DATE_START}.tar.xz\
Lucio Andrés Illanes Albornoz (arab, vxp) 951620
		$(find tmp -mindepth 1 -maxdepth 1 -type d);
Lucio Andrés Illanes Albornoz (arab, vxp) ca3031
	log_msg info "Finished building source tarball.";
Lucio Andrés Illanes Albornoz (arab, vxp) 70df12
	if [ -x "$(which gpg 2>/dev/null)" -a -n "${TARBALL_SIGN_GPG_KEY}" ] &&\
Lucio Andrés Illanes Albornoz (arab, vxp) d3b354
				gpg --list-keys "${TARBALL_SIGN_GPG_KEY}" >/dev/null 2>&1; then
Lucio Andrés Illanes Albornoz (arab, vxp) 52bc51
		gpg --armor --passphrase-file /dev/null					\
Lucio Andrés Illanes Albornoz (arab, vxp) d3b354
			--local-user "${TARBALL_SIGN_GPG_KEY}" --sign			\
Lucio Andrés Illanes Albornoz (arab, vxp) d3b354
			${TARBALL_SRC_FNAME_PREFIX}${BUILD_USER}@${BUILD_HNAME}-${BUILD_DATE_START}.tar.xz;
Lucio Andrés Illanes Albornoz (arab, vxp) d3b354
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 95bf77
	cd ${OLDPWD};
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) 75cb84
update_build_status finish; clean_build_status;
Lucio Andrés Illanes Albornoz (arab, vxp) 95bf77
exit ${BUILD_SCRIPT_RC})} 2>&1 | tee ${PREFIX}/${BUILD_LOG_FNAME:=build-$(date ${TIMESTAMP_FMT_STATUS_FILES}).log} &
Lucio Andrés Illanes Albornoz (arab, vxp) 75cb84
trap "kill -INT $!" HUP INT TERM USR1 USR2; wait;
lalbornoz 340f4c
lalbornoz 340f4c
# vim:filetype=sh