midipix / build / midipix_build

Forked from build/midipix_build 4 years ago
Clone
Blob Blame History Raw
#
# . ./build.vars and set -o errexit -o noglob are assumed.
# See warning at the top of build.vars.
#

# N.B.	URLs ($1) may contain `?' or '&' characters.
fetch() {
	_f_url="${1}"; _f_sha256sum_src="${2}";
	_f_url_dst="${DLCACHEDIR}/$(basename "${_f_url}")";
	if [ ${ARG_OFFLINE:-0} -eq 1 ]\
	|| [ -e ${_f_url_dst}.fetched ]; then
		unset _f_url _f_sha256sum_src _f_url_dst;
		return 0;
	else
		wget ${WGET_ARGS} -c -O ${_f_url_dst} "${_f_url}";
	fi;
	if [ -n "${_f_sha256sum_src}" ]; then
		set -- $(openssl dgst -sha256 ${_f_url_dst}); shift $((${#}-1));
		if [ "${_f_sha256sum_dst:=${1}}" != "${_f_sha256sum_src}" ]; then
			log_msg failexit "Error: hash mismatch for URL \`${_f_url}' (is: ${_f_sha256sum_dst}, should be: ${_f_sha256sum_src}.)";
		fi;
	fi;
	touch ${_f_url_dst}.fetched;
	unset _f_url _f_url_dst _f_sha256sum_src _f_sha256sum_dst;
};

fetch_git() {
	_fg_subdir="${1}"; _fg_url="${2}"; _fg_branch="${3}";
	if [ ${ARG_OFFLINE:-0} -eq 0 ]; then
		if [ -e "${DLCACHEDIR}/${_fg_subdir}" ]; then
			cd ${DLCACHEDIR}/${_fg_subdir} &&\
				git pull origin ${_fg_branch:-main} && cd ${OLDPWD};
		else
			git clone ${_fg_url} ${DLCACHEDIR}/${_fg_subdir};
			if [ -n "${_fg_branch}" -a		\
			     \( -z "${_fg_branch#main}" \) -a	\
			     \( -z "${_fg_branch#master}" \) ]; then
				cd ${DLCACHEDIR}/${_fg_subdir} &&\
					git checkout -b ${_fg_branch} && cd ${OLDPWD};
			fi;
		fi;
	fi;
	secure_rm ${_fg_subdir};
	cp -pr ${DLCACHEDIR}/${_fg_subdir} .;
};

is_build_script_done() {
	if match_list "${ARG_RESTART}" , ${BUILD_PACKAGE_LC}; then
		if [ -n "${ARG_RESTART_AT}" ]			\
		&& [ "${ARG_RESTART_AT}" != "${1}" ]; then
			return 0;	# Skip
		else
			return 1;	# Build
		fi;
	elif [ -f ${WORKDIR}/.${2:-${BUILD_PACKAGE_LC}}.${1} ]; then
		return 0;		# Skip
	else
		return 1;		# Build
	fi;
};

log_env_vars() {
	log_msg info "Variables for this ${1:-build}:"; shift;
	while [ ${_lev_nvar:=0} -lt ${#} ]; do
		_lev_arg="$(eval echo \${${_lev_nvar}})";
		_lev_arg="${_lev_arg%%=*}";
		if [ ${#_lev_arg} -gt ${_lev_arg_len_max:=0} ]; then
			_lev_arg_len_max=${#_lev_arg};
		fi; : $((_lev_nvar+=1));
	done; unset _lev_nvar _lev_arg;
	while [ ${#} -gt 0 ]; do
		log_msg info "$(printf					\
			"%${_lev_arg_len_max}.${_lev_arg_len_max}s=%s"	\
			"${1%%=*}" "$(get_var_unsafe ${1#*=})")";
		shift;
	done; unset _lev_arg_len_max;
};

parse_with_pkg_name() {
	PKG_NAME=${1}; shift;
	_pwpn_pkg_name_uc=$(echo ${PKG_NAME} | tr a-z A-Z);
	if [ -z "$(get_var_unsafe PKG_${_pwpn_pkg_name_uc}_URL)" ]; then
		unset _pwpn_pkg_name_uc;
		log_msg failexit "Error: package \`${PKG_NAME}' missing in build.vars.";
	else
		for _pwpn_vname in ${PKG_BUILD_VARS}; do
			if [ -n "$(get_var_unsafe DEFAULT_${_pwpn_vname})" ]; then
				set_var_unsafe PKG_${_pwpn_vname} "$(get_var_unsafe DEFAULT_${_pwpn_vname})";
			fi;
			if [ -n "$(get_var_unsafe ${BUILD_TARGET}_${_pwpn_vname})" ]; then
				set_var_unsafe PKG_${_pwpn_vname} "$(get_var_unsafe ${BUILD_TARGET}_${_pwpn_vname})";
			fi;
			if [ "${BUILD}" = "debug" ]	\
			&& [ -n "$(get_var_unsafe PKG_${_pwpn_pkg_name_uc}_${_pwpn_vname}_DEBUG)" ]; then
				set_var_unsafe PKG_${_pwpn_vname} "$(get_var_unsafe PKG_${_pwpn_pkg_name_uc}_${_pwpn_vname}_DEBUG)";
			elif [ "${BUILD}" = "release" ]	\
			&& [ -n "$(get_var_unsafe PKG_${_pwpn_pkg_name_uc}_${_pwpn_vname}_RELEASE)" ]; then
				set_var_unsafe PKG_${_pwpn_vname} "$(get_var_unsafe PKG_${_pwpn_pkg_name_uc}_${_pwpn_vname}_RELEASE)";
			elif [ -n "$(get_var_unsafe PKG_${_pwpn_pkg_name_uc}_${_pwpn_vname})" ]; then
				set_var_unsafe PKG_${_pwpn_vname} "$(get_var_unsafe PKG_${_pwpn_pkg_name_uc}_${_pwpn_vname})";
			fi;
		done; unset _pwpn_vname;
	fi;
	[ -z "${PKG_FNAME}" ] && PKG_FNAME=${PKG_URL##*/};
	[ -z "${PKG_SUBDIR}" ] && PKG_SUBDIR=${PKG_FNAME%%.tar*};
	[ -n "${PKG_ENV_VARS_EXTRA}" ] && set_env_vars_with_sep : "${PKG_ENV_VARS_EXTRA}";
	for _pwpn_env_var in $(set); do
		if [ "${_pwpn_env_var#PKG_${_pwpn_pkg_name_uc}}" != "${_pwpn_env_var}" ]; then
			_pwpn_env_vars="${_pwpn_env_vars:+${_pwpn_env_vars} }${_pwpn_env_var%%=*}";
		fi;
	done;
	if [ -n "${_pwpn_env_vars}" ]; then
		log_env_vars "build" ${_pwpn_env_vars};
	fi; unset _pwpn_pkg_name_uc _pwpn_env_var _pwpn_env_vars;
};

run_cmd_unsplit() {
	_rcu_cmd=${1}; shift;
	while [ ${#} -gt 0 ]; do
		[ -n "${1}" ] &&\
			_rcu_cmdline="${_rcu_cmdline:+${_rcu_cmdline}:}${1}";
		shift;
	done;
	push_IFS :; ${_rcu_cmd} ${_rcu_cmdline}; _rcu_rc=$?; pop_IFS;
	unset _rcu_cmd _rcu_cmdline; return ${_rcu_rc};
};

set_build_script_done() {
	_sbsd_script_fname=${BUILD_PACKAGE_LC};
	_sbsd_done_fname=${WORKDIR}/.${_sbsd_script_fname};
	while [ $# -ge 1 ]; do
		if [ "${1#-}" != "${1}" ]; then
			secure_rm ${_sbsd_done_fname}.${1#-};
		else
			touch ${_sbsd_done_fname}.${1};
			log_msg info "Finished build step ${1} of \`${_sbsd_script_fname}'.";
		fi; shift;
	done; unset _sbsd_script_fname _sbsd_done_fname;
};

set_env_vars_with_sep() {
	_sevws_sep=${1}; shift; push_IFS ${_sevws_sep}; set -- ${1};
	while [ ${#} -gt 0 ]; do
		export "${1}"; shift;
	done; unset _sevws_sep; pop_IFS;
};

# vim:filetype=sh