summaryrefslogtreecommitdiffhomepage
path: root/subr/build.subr
blob: 0e5bea4e722fa0425b46ba2a1fa1b263fc4d7eaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#
# set -o errexit -o noglob are assumed.
#

date() { command date "+${1:-${TIMESTAMP_FMT}}"; };
get_var_unsafe() { eval echo \${${1}}; };
set_var_unsafe() { [ -n "${2}" ] && eval ${1}=\"${2}\" || return 0; };
pop_IFS() { IFS="${_pI_IFS}"; unset _pI_IFS; };
push_IFS() { _pI_IFS="${IFS}"; IFS="${1}"; };
set_build_dir() { PKG_BUILD_DIR="${1}-${2}-${PKG_TARGET}"; };
test_cmd() { command -v "${1}" >/dev/null; };

build_fileop() {
	local _op="${1}"; shift;
	if [ "${_op}" = cd ]; then
		log_msg varn "Changing working directory to \`${1}'.";
		[ \( -n "${1}" \) -a \( -e "${1}" \) ] && cd -- "${1}";
	elif [ "${_op}" = cp ]; then
		log_msg varn "Copying \`${1}' to \`${2}' w/ -pPR.";
		[ ${#} -ge 2 ] && cp -pPR -- "${@}";
	elif [ "${_op}" = ln_symbolic ]; then
		log_msg varn "Linking \`${1}' to \`${2}' w/ -fs";
		[ \( -n "${1}" \) -a \( -n "${2}" \) ] && ln -fs -- "${1}" "${2}";
	elif [ "${_op}" = mv ]; then
		log_msg varn "Moving \`${1}' to \`${2}' w/ -fs";
		[ \( -n "${1}" \) -a \( -n "${2}" \) ] && mv -f -- "${1}" "${2}";
	elif [ "${_op}" = mkdir ]\
	||   [ "${_op}" = rm ]; then
		while [ ${#} -gt 0 ]; do
			if [ -z "${1}" ]; then
				return 1;
			elif [ "${_op}" = mkdir ]\
			&&   [ ! -e "${1}" ]; then
				log_msg varn "Making directory \`${1}'.";
				mkdir -p -- "${1}";
			elif [ "${_op}" = rm ]\
			&&   [ -e "${1}" ]; then
				log_msg varn "Removing directory or file \`${1}'.";
				rm -rf -- "${1}";
			fi; shift;
		done;
	else
		log_msg failexit "Error: build_fileop() called w/ invalid parameter(s) \`${@}'.";
	fi;
};

install_files() {
	local _verbose _prefix _ln_target _ln_fname _mkdir_fname	\
		_file_fname_src _file_fname_dst;
	if [ "${1}" = "-v" ]; then
		_verbose=1; shift;
	fi; _prefix="${1}"; shift;
	while [ ${#} -gt 0 ]; do
	case "${1}" in
	@*=*)
		_ln_target="${1%=*}";
		_ln_target="${_ln_target#@}";
		_ln_fname="${1#*=}";
		if [ "${_ln_fname#/}" = "${_ln_fname}" ]; then
			_ln_fname="${_prefix:+${_prefix}/}${_ln_fname}";
		fi;
		if [ -e "${_ln_fname}" ]; then
			if [ ${_verbose:-0} -eq 1 ]; then
				echo build_fileop rm "${_ln_fname}";
			fi;
			build_fileop rm "${_ln_fname}";
		fi;
		build_fileop ln_symbolic "${_ln_target}" "${_ln_fname}";
		;;
	/=*)
		_mkdir_fname="${1#/=}";
		if [ "${_mkdir_fname#/}" = "${_mkdir_fname}" ]; then
			_mkdir_fname="${_prefix:+${_prefix}/}${_mkdir_fname}";
		fi;
		build_fileop mkdir "${_mkdir_fname}";
		;;
	*)
		_file_fname_src="${1%=*}";
		_file_fname_dst="${1#*=}";
		if [ "${_file_fname_dst#/}" = "${_file_fname_dst}" ]; then
			_file_fname_dst="${_prefix:+${_prefix}/}${_file_fname_dst}";
		fi;
		build_fileop cp "${_file_fname_src}" "${_file_fname_dst}";
		;;
	esac; shift;
	done;
};

is_build_script_done() {
	local _done_fname="${WORKDIR}/.${1}.${2}" _restart_at="${3}";
	if [ -z "${_restart_at}" ]; then
		if [ -e "${_done_fname}" ]; then
			return 0;	# Skip
		else
			return 1;	# Build
		fi;
	elif [ "${_restart_at}" = "ALL" ]; then
		return 1;		# Build
	elif lmatch "${_restart_at}" , "${2}"; then
		return 1;		# Build
	else
		return 0;		# Skip
	fi;
};

set_build_script_done() {
	local _pkg_fname="${1}";
	local _done_fname_pfx="${WORKDIR}/.${_pkg_fname}";
	shift; while [ ${#} -ge 1 ]; do
		if [ "${1#-}" != "${1}" ]; then
			build_fileop rm "${_done_fname_pfx}.${1#-}";
		else
			touch "${_done_fname_pfx}.${1}";
			log_msg info "Finished build step ${1} of package \`${_pkg_fname}'.";
		fi; shift;
	done;
};

lfilter() {
	local _list="${1}" _filter="${2}" _lnew _litem _lfilter;
	for _litem in ${_list}; do
		for _lfilter in ${_filter}; do
			if [ "${_lfilter}" = "${_litem}" ]; then
				_lnew="${_lnew:+${_lnew} }${_litem}"; break;
			fi; 
		done;
	done;
	echo ${_lnew};
};

lmatch() {
	local _cmp="${3}"; push_IFS "${2}"; set -- ${1}; pop_IFS;
	while [ ${#} -gt 0 ]; do
		if [ "${1}" = "${_cmp}" ]; then
			return 0;
		fi; shift;
	done; return 1;
};

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

log_msg() {
	local _lvl="${1}"; shift;
	if [ "${_lvl#v}" != "${_lvl}" ]\
	&& [ ${ARG_VERBOSE:-0} -eq 0 ]; then
		return;
	fi;
	case "${_lvl}" in
		failexit) printf "\033[0m\033[${LOG_MSG_FAIL_COLOUR}m"; ;;
		fail) printf "\033[0m\033[${LOG_MSG_FAIL_COLOUR}m"; ;;
		info) printf "\033[0m\033[${LOG_MSG_INFO_COLOUR}m"; ;;
		vnfo) printf "\033[0m\033[${LOG_MSG_INFO_COLOUR}m"; ;;
		succ) printf "\033[0m\033[${LOG_MSG_SUCC_COLOUR}m"; ;;
		warn) printf "\033[0m\033[${LOG_MSG_WARN_COLOUR}m"; ;;
		varn) printf "\033[0m\033[${LOG_MSG_WARN_COLOUR}m"; ;;
	esac;
	if [ ${#} -gt 1 ]; then
		printf "==> %s %s %s\033[0m\n" "$(date)" "${1}" "$*";
	else
		printf "==> %s %s\033[0m\n" "$(date)" "${1}";
	fi; [ ${_lvl} = failexit ] && exit 1 || return 0;
};

run_cmd_unsplit() {
	local _cmd="${1}" _cmdline _rc; shift;
	while [ ${#} -gt 0 ]; do
		[ -n "${1}" ] &&\
			_cmdline="${_cmdline:+${_cmdline}:}${1}";
		shift;
	done;
	push_IFS :; ${_cmd} ${_cmdline}; _rc=$?; pop_IFS;
	return ${_rc};
};

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

subst_tgts() {
	while [ ${#} -ge 1 ]; do
	case "${1}" in
	dev)
		echo "${DEV_PACKAGES}"; ;;
	devroot)
		echo "${DEVROOT_PACKAGES}"; ;;
	world)	echo "${WORLD_PACKAGES}"; ;;
	*)	echo "${1}"; ;;
	esac; shift;
	done;
};

# vim:filetype=sh