Conan_Kudo / build / midipix_build

Forked from build/midipix_build 4 years ago
Clone

Blame subr/build.subr

Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
#
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
# set -o errexit -o noglob are assumed.
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
#
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
date() { command date "+${1:-${TIMESTAMP_FMT}}"; };
Lucio Andrés Illanes Albornoz (arab, vxp) 154865
get_var_unsafe() { eval echo \${${1}}; };
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
set_var_unsafe() { [ -n "${2}" ] && eval ${1}=\"${2}\" || return 0; };
Lucio Andrés Illanes Albornoz (arab, vxp) 154865
pop_IFS() { IFS="${_pI_IFS}"; unset _pI_IFS; };
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
push_IFS() { _pI_IFS="${IFS}"; IFS="${1}"; };
Lucio Andrés Illanes Albornoz (arab, vxp) 8ee843
set_build_dir() { PKG_BUILD_DIR="${1}-${2}-${PKG_TARGET}"; };
Lucio Andrés Illanes Albornoz (arab, vxp) 154865
test_cmd() { command -v "${1}" >/dev/null; };
Lucio Andrés Illanes Albornoz (arab, vxp) 02175d
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
build_fileop() {
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	local _op="${1}"; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	if [ "${_op}" = cd ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
		log_msg varn "Changing working directory to \`${1}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) 1fb552
		[ \( -n "${1}" \) -a \( -e "${1}" \) ] && cd -- "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	elif [ "${_op}" = cp ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
		log_msg varn "Copying \`${1}' to \`${2}' w/ -pPR.";
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
		[ ${#} -ge 2 ] && cp -pPR -- "${@}";
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	elif [ "${_op}" = ln_symbolic ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
		log_msg varn "Linking \`${1}' to \`${2}' w/ -fs";
Lucio Andrés Illanes Albornoz (arab, vxp) 1fb552
		[ \( -n "${1}" \) -a \( -n "${2}" \) ] && ln -fs -- "${1}" "${2}";
Lucio Andrés Illanes Albornoz (arab, vxp) 05e618
	elif [ "${_op}" = mv ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 05e618
		log_msg varn "Moving \`${1}' to \`${2}' w/ -fs";
Lucio Andrés Illanes Albornoz (arab, vxp) 05e618
		[ \( -n "${1}" \) -a \( -n "${2}" \) ] && mv -f -- "${1}" "${2}";
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	elif [ "${_op}" = mkdir ]\
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	||   [ "${_op}" = rm ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
		while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
			if [ -z "${1}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
				return 1;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
			elif [ "${_op}" = mkdir ]\
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
			&&   [ ! -e "${1}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
				log_msg varn "Making directory \`${1}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
				mkdir -p -- "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
			elif [ "${_op}" = rm ]\
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
			&&   [ -e "${1}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 154865
				log_msg varn "Removing directory or file \`${1}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) 154865
				rm -rf -- "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
			fi; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
		done;
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
	else
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
		log_msg failexit "Error: build_fileop() called w/ invalid parameter(s) \`${@}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
install_files() {
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	local _verbose _prefix _ln_target _ln_fname _mkdir_fname	\
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		_file_fname_src _file_fname_dst;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	if [ "${1}" = "-v" ]; then
Lucio Andrés Illanes Albornoz e9e741
		_verbose=1; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	fi; _prefix="${1}"; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	case "${1}" in
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	@*=*)
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		_ln_target="${1%=*}";
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		_ln_target="${_ln_target#@}";
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		_ln_fname="${1#*=}";
Lucio Andrés Illanes Albornoz e9e741
		if [ "${_ln_fname#/}" = "${_ln_fname}" ]; then
Lucio Andrés Illanes Albornoz e9e741
			_ln_fname="${_prefix:+${_prefix}/}${_ln_fname}";
Lucio Andrés Illanes Albornoz e9e741
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 1fb552
		if [ -e "${_ln_fname}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
			if [ ${_verbose:-0} -eq 1 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 1fb552
				echo build_fileop rm "${_ln_fname}";
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
			fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 1fb552
			build_fileop rm "${_ln_fname}";
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
		fi;
Lucio Andrés Illanes Albornoz e9e741
		build_fileop ln_symbolic "${_ln_target}" "${_ln_fname}";
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		;;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	/=*)
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		_mkdir_fname="${1#/=}";
Lucio Andrés Illanes Albornoz e9e741
		if [ "${_mkdir_fname#/}" = "${_mkdir_fname}" ]; then
Lucio Andrés Illanes Albornoz e9e741
			_mkdir_fname="${_prefix:+${_prefix}/}${_mkdir_fname}";
Lucio Andrés Illanes Albornoz e9e741
		fi;
Lucio Andrés Illanes Albornoz e9e741
		build_fileop mkdir "${_mkdir_fname}";
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		;;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	*)
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		_file_fname_src="${1%=*}";
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		_file_fname_dst="${1#*=}";
Lucio Andrés Illanes Albornoz e9e741
		if [ "${_file_fname_dst#/}" = "${_file_fname_dst}" ]; then
Lucio Andrés Illanes Albornoz e9e741
			_file_fname_dst="${_prefix:+${_prefix}/}${_file_fname_dst}";
Lucio Andrés Illanes Albornoz e9e741
		fi;
Lucio Andrés Illanes Albornoz e9e741
		build_fileop cp "${_file_fname_src}" "${_file_fname_dst}";
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		;;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	esac; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
};
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
is_build_script_done() {
Lucio Andrés Illanes Albornoz (arab, vxp) 8ee843
	local _done_fname="${WORKDIR}/.${1}.${2}" _restart_at="${3}";
Lucio Andrés Illanes Albornoz (arab, vxp) 36ab93
	if [ -z "${_restart_at}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 36ab93
		if [ -e "${_done_fname}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 36ab93
			return 0;	# Skip
Lucio Andrés Illanes Albornoz (arab, vxp) 36ab93
		else
Lucio Andrés Illanes Albornoz (arab, vxp) 36ab93
			return 1;	# Build
Lucio Andrés Illanes Albornoz (arab, vxp) 36ab93
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) d5afb4
	elif [ "${_restart_at}" = "ALL" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) d5afb4
		return 1;		# Build
Lucio Andrés Illanes Albornoz (arab, vxp) 36ab93
	elif lmatch "${_restart_at}" , "${2}"; then
Lucio Andrés Illanes Albornoz (arab, vxp) 36ab93
		return 1;		# Build
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
	else
Lucio Andrés Illanes Albornoz (arab, vxp) 36ab93
		return 0;		# Skip
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
};
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
set_build_script_done() {
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	local _pkg_fname="${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	local _done_fname_pfx="${WORKDIR}/.${_pkg_fname}";
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	shift; while [ ${#} -ge 1 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
		if [ "${1#-}" != "${1}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
			build_fileop rm "${_done_fname_pfx}.${1#-}";
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
		else
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
			touch "${_done_fname_pfx}.${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
			log_msg info "Finished build step ${1} of package \`${_pkg_fname}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
		fi; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
};
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
lfilter() {
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	local _list="${1}" _filter="${2}" _lnew _litem _lfilter;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	for _litem in ${_list}; do
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		for _lfilter in ${_filter}; do
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
			if [ "${_lfilter}" = "${_litem}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
				_lnew="${_lnew:+${_lnew} }${_litem}"; break;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
			fi; 
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		done;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	echo ${_lnew};
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
};
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
Lucio Andrés Illanes Albornoz (arab, vxp) 51fbc3
lmatch() {
Lucio Andrés Illanes Albornoz (arab, vxp) 51fbc3
	local _cmp="${3}"; push_IFS "${2}"; set -- ${1}; pop_IFS;
Lucio Andrés Illanes Albornoz (arab, vxp) 51fbc3
	while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) 51fbc3
		if [ "${1}" = "${_cmp}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 51fbc3
			return 0;
Lucio Andrés Illanes Albornoz (arab, vxp) 51fbc3
		fi; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 51fbc3
	done; return 1;
Lucio Andrés Illanes Albornoz (arab, vxp) 51fbc3
};
Lucio Andrés Illanes Albornoz (arab, vxp) 51fbc3
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
log_env_vars() {
Lucio Andrés Illanes Albornoz (arab, vxp) 48549b
	local _nvar=1 _arg _arg_len_max=0;
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
	log_msg info "Variables for this ${1:-build}:"; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 48549b
	while [ ${_nvar} -le ${#} ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		_arg="$(eval echo \${${_nvar}})";
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		_arg="${_arg%%=*}";
Lucio Andrés Illanes Albornoz (arab, vxp) 48549b
		if [ ${#_arg} -gt ${_arg_len_max} ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
			_arg_len_max=${#_arg};
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		fi; : $((_nvar+=1));
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
	while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
		log_msg info "$(printf					\
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
			"%${_arg_len_max}.${_arg_len_max}s=%s"	\
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
			"${1%%=*}" "$(get_var_unsafe ${1#*=})")";
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
		shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
};
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
log_msg() {
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	local _lvl="${1}"; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	if [ "${_lvl#v}" != "${_lvl}" ]\
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	&& [ ${ARG_VERBOSE:-0} -eq 0 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		return;
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	case "${_lvl}" in
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		failexit) printf "\033[0m\033[${LOG_MSG_FAIL_COLOUR}m"; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		fail) printf "\033[0m\033[${LOG_MSG_FAIL_COLOUR}m"; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		info) printf "\033[0m\033[${LOG_MSG_INFO_COLOUR}m"; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		vnfo) printf "\033[0m\033[${LOG_MSG_INFO_COLOUR}m"; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		succ) printf "\033[0m\033[${LOG_MSG_SUCC_COLOUR}m"; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		warn) printf "\033[0m\033[${LOG_MSG_WARN_COLOUR}m"; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		varn) printf "\033[0m\033[${LOG_MSG_WARN_COLOUR}m"; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	esac;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	if [ ${#} -gt 1 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		printf "==> %s %s %s\033[0m\n" "$(date)" "${1}" "$*";
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	else
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		printf "==> %s %s\033[0m\n" "$(date)" "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	fi; [ ${_lvl} = failexit ] && exit 1 || return 0;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
};
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
run_cmd_unsplit() {
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	local _cmd="${1}" _cmdline _rc; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
	while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
		[ -n "${1}" ] &&\
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
			_cmdline="${_cmdline:+${_cmdline}:}${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
		shift;
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	push_IFS :; ${_cmd} ${_cmdline}; _rc=$?; pop_IFS;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	return ${_rc};
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
};
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
set_env_vars_with_sep() {
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	local _sep="${1}"; shift; push_IFS ${_sep}; set -- ${1};
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		export "${1}"; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	done; pop_IFS;
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
};
Lucio Andrés Illanes Albornoz (arab, vxp) ce047e
Lucio Andrés Illanes Albornoz (arab, vxp) d1adc7
subst_tgts() {
Lucio Andrés Illanes Albornoz (arab, vxp) d1adc7
	while [ ${#} -ge 1 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) d1adc7
	case "${1}" in
Lucio Andrés Illanes Albornoz a67d9f
	dev)
Lucio Andrés Illanes Albornoz a67d9f
		echo "${DEV_PACKAGES}"; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) d1adc7
	devroot)
Lucio Andrés Illanes Albornoz (arab, vxp) 1fb552
		echo "${DEVROOT_PACKAGES}"; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 1fb552
	world)	echo "${WORLD_PACKAGES}"; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 1fb552
	*)	echo "${1}"; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) d1adc7
	esac; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) d1adc7
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) d1adc7
};
Lucio Andrés Illanes Albornoz (arab, vxp) d1adc7
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
# vim:filetype=sh