summaryrefslogtreecommitdiffhomepage
path: root/build.subr
blob: f44b5be77a9a6c6330538627171db283a366ff56 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#
# . ./build.vars and set -o errexit are assumed.
# See warning at the top of build.vars.
#

date() { command date +"${1:-${TIMESTAMP_FMT}}"; };
get_basename() { set -- $(get_name_without_slash ${1}); echo "${1##*/}"; };
get_var_dyn() { ${1}; };
get_var_unsafe() { eval echo \${${1}}; };
set_var_dyn() { eval ${1}\(\) \{ echo \"${2}\"\; \}; };
set_var_unsafe() { eval ${1}=\"${2}\"; };
get_name_without_slash() { while [ "${1%/}" != "${1}" ]; do set -- ${1%/}; done; echo ${1}; };
get_postfix_lrg() { echo "${1##*${2}}"; };
get_prefix_lrg() { echo "${1%%${2}*}"; };
get_postfix() { echo "${1#*${2}}"; };
get_prefix() { echo "${1%${2}*}"; };
match_any() { [ "${1#*${2}*}" != "${1}" ]; };
match_uname_any() { set -- ${1} $(uname -s); [ "${2#*${1}*}" != "${2}" ]; };
push_IFS() { _pI_IFS="${IFS}"; IFS="${1}"; };
pop_IFS() { IFS="${_pI_IFS}"; unset _pI_IFS; };
set_build_dir() { PKG_BUILD_DIR=${1}-${2}-${TARGET}; };
split() { push_IFS "${1}"; set -- ${2}; pop_IFS; echo "${*}"; };
test_cmd() { command -v "${1}" >/dev/null; };
unsplit() { push_IFS "${1}"; shift; set -- "${@}"; echo "${*}"; pop_IFS; };


build_times_init() { BUILD_TIMES_SECS=$(command date +%s); };
build_times_get() {
	: $((BUILD_TIMES_SECS=$(command date +%s)-${BUILD_TIMES_SECS}));
	: $((BUILD_TIMES_HOURS=${BUILD_TIMES_SECS}/3600));
	: $((BUILD_TIMES_MINUTES=(${BUILD_TIMES_SECS}%3600)/60));
	: $((BUILD_TIMES_SECS=(${BUILD_TIMES_SECS}%3600)%60));
};

clean_prefix() {
	log_msg info "-c specified, cleaning prefix...";
	set -- bin i686-nt32-midipix include lib lib64 libexec native share x86_64-nt64-midipix;
	while [ ${#} -gt 0 ]; do rm -rf ${PREFIX}/${1}; shift; done;
	for _cp_pname in $(find ${PREFIX}/tmp -mindepth 1 -maxdepth 1 -not -iname \*.tar\*)	\
			$(find ${PREFIX}/tmp -mindepth 1 -maxdepth 1 -type f -iname .\*); do
		rm_if_exists ${_cp_pname};
	done; unset _cp_pname;
};

clean_build_status() {
	[ "${1}" = abort ] && _cbs_abort=1;
	set -- BUILD_STATUS_IN_PROGRESS_FNAME BUILD_STATUS_PROGRESS_FNAME BUILD_STATUS_TARBALL_PROGRESS_FNAME;
	while [ ${#} -gt 0 ]; do
		rm -f $(get_var_unsafe ${1}); shift;
	done;
	if [ ${_cbs_abort:-0} -eq 1 ]; then
		unset _cbs_abort;
		touch ${PREFIX}/BUILD_ABORTED_AT_$(date %Y-%m-%d-%H-%M-%S);
		log_msg fail "Build aborted at $(date %Y-%m-%d-%H-%M-%S).";
	fi; unset _cbs_abort;
};

export_vars_subst() {
	_evs_pfx=${1}; _evs_pfx_new=${2}; shift 2;
	while [ ${#} -gt 0 ]; do
		if [ -n "${_evs_vval:=$(get_var_unsafe ${_evs_pfx}${1})}" ]; then
			export "${_evs_pfx_new}${1}=${_evs_vval}";
		fi; unset _evs_vval; shift;
	done; unset _evs_pfx _evs_pfx_new;
};

fetch_git() {
	[ -z "${1}" ] && return 1;
	[ ${ARG_NO_DOWNLOAD:-0} -eq 1 ] && return;
	rm_if_exists ${1};
	[ -d ${1} ] && (cd ${1} && git pull origin main)\
		|| git clone ${3} ${2} ${1};
};

find_with_no_paths() {
	for _fwnp_not_path in ${1}; do
		_fwnp_args="${_fwnp_args:+${_fwnp_args} }-not -path ./${_fwnp_not_path} -not -path ./${_fwnp_not_path}/*";
	done; shift;
	set -f; find "${@}" ${_fwnp_args}; _rc=${?}; set +f;
	unset _fwnp_args _fwnp_not_path; return ${_rc};
};

insert_build_script_link() {
	_ibsl_fname=${1};
	_ibsl_level=${_ibsl_fname%%.*};
	_ibsl_name=${_ibsl_fname#*.}; _ibsl_name=${_ibsl_name%.build};
	if [ -z "${_ibsl_fname}" -o -z "${_ibsl_level}"	\
			-o -z "${_ibsl_name}" ]		\
			|| ! isnumber ${_ibsl_level}	\
			|| [ ${#_ibsl_level} != 3 ]; then
		log_msg failexit "Error: invalid or empty build script filename/level/name.";
	elif [ -e ${_ibsl_fname} ]; then
		log_msg failexit "Error: build script \`${_ibsl_fname}' already exists.";
	elif [ -z "$(find -maxdepth 1 -name ${_ibsl_level}.\* -printf '%P\n' -quit)" ]; then
		echo ln -s pkg.build ${_ibsl_fname};
		ln -s pkg.build ${_ibsl_fname};
		unset _ibsl_fname _ibsl_level _ibsl_name;
		return 0;
	else
		_ibsl_levels="$(find -maxdepth 1 -name		\
			 ${_ibsl_level%[0-9][0-9]}\[0-9\]\[0-9\].\*	\
			-printf '%P\n' | sort -nk1)";
	fi;
	for _ibsl_fname_cur in ${_ibsl_levels}; do
		if [ \( ${_ibsl_changed:=0} -eq 0 \) -a	\
				\( "${_ibsl_fname_cur%%.*}" -eq ${_ibsl_level} \) ]; then
			echo ln -s pkg.build ${_ibsl_fname};
			ln -s pkg.build ${_ibsl_fname};
			_ibsl_changed=1;
		fi;
		if [ ${_ibsl_changed:=0} -eq 1 ]; then
			if [ -z "$(find -maxdepth 1 -name $((${_ibsl_fname_cur%%.*}+1)).\*	\
					-printf '%P\n' -quit)" ]; then
				_ibsl_last=1;
			fi;
			echo mv ${_ibsl_fname_cur}	\
				$((${_ibsl_fname_cur%%.*}+1)).${_ibsl_fname_cur#*.};
			mv ${_ibsl_fname_cur}	\
				$((${_ibsl_fname_cur%%.*}+1)).${_ibsl_fname_cur#*.};
			if [ ${_ibsl_last:-0} -eq 1 ]; then
				break;
			fi;
		fi;
	done;
	if [ ${_ibsl_changed:=0} -eq 0 ]; then
		log_msg failexit "Error: build level ${_ibsl_level} not in \`${_ibsl_levels}'.";
	else
		unset _ibsl_levels _ibsl_fname_cur _ibsl_changed _ibsl_last;
		unset _ibsl_fname _ibsl_level _ibsl_name;
	fi;
};

isnumber() {
	[ -z "${1}" ] && return 1 || _i_num=${1};
	while [ -n "${_i_num}" ]; do
		if [ "${_i_num#[0-9]}" = "${_i_num}" ]; then
			return 1;
		else
			_i_num="${_i_num#[0-9]}";
		fi;
	done; unset _i_num; return 0;
};

rm_if_exists() {
	[ -z "${1#-m}" ] && { _rie_arg_m=1; shift; };
	[ -z "${1#-c}" ] && { _rie_arg_c=1; shift; };
	[ -z "${1}" ] && return 1;
	if [ -d ${1} -o -f ${1} ]; then
		log_msg warn "Removing directory or file \`${1}'.";
		if [ ${ARG_PEDANTIC:-0} -eq 1 ]; then
			printf "Confirm deletion (y|N) ";
			read _rie_prompt; case "${_rie_prompt}" in
				[yY])	rm -rf ${1}; ;;
				*)	log_msg warn "Skipping removal of \`${1}'.";
					_rie_arg_m=0; ;;
			esac;
		else 
			rm -rf ${1};
		fi;
	fi;
	[ ${_rie_arg_m:-0} -eq 1 ] && {
		log_msg warn "Making directory \`${1}'.";
		mkdir ${1}; unset _rie_arg_m; };
	[ ${_rie_arg_c:-0} -eq 1 ] && { cd ${1}; unset _rie_arg_c; };
	return 0;
};

rotate_files() {
	set -- $(find ${PREFIX} -mindepth 1 -maxdepth 1 -type f -iname "${1}" |\
			sort | sed -n $((${2}+1)),\$p);
	while [ ${#} -gt 0 ]; do
		rm -f "${1}"; shift;
	done;
};

set_env_vars() {
	_sev_val_new="${1}"; shift;
	while [ ${#} -gt 1 ]; do
		[ -z "${_sev_val_new}" ] && unset ${1} ||\
			export "${1}=${_sev_val_new}"; shift;
	done; unset _sev_val_new;
};

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;
};


# Download GNU bash-style patch sets into ${2}-patches-extra and
# apply them to ${2} in the correct order.
apply_patches() {
	(rm_if_exists -m -c ${2}-patches-extra;
	wget -c -nd -np -r -R \*.htm\* -R \*.sig ${1};
	for _ap_patch_fname in		\
			$(find . -type f -not -iname \*.sig | sort); do
		patch -b -d ../${2} -p0 < ${_ap_patch_fname};
	done; unset _ap_patch_fname);
};

# Check whether all supplied arguments contain non-empty valid values.
check_path_vars() {
	while [ ${#} -gt 0 ]; do
		if [ -z "${_cpv_val:=$(get_var_unsafe "${1}")}" ]; then
			log_msg failexit "Error: variable \`${1}' is empty or unset.";
		elif match_any "${_cpv_val}" " "; then
			log_msg failexit "Error: variable \`${1}' contains one or more whitespace characters.";
		else
			shift;
		fi;
		unset _cpv_val;
	done;
};

# Check whether all supplied command names resolve.
check_prereq_cmds() {
	while [ ${#} -gt 0 ]; do
		if ! command -v ${1} >/dev/null; then
			_cpc_missing_list="${1}${_cpc_missing_list:+ ${_cpc_missing_list}}";
		fi; shift;
	done;
	if [ -n "${_cpc_missing_list}" ]; then
		log_msg failexit "Error: missing prerequisite command(s): ${_cpc_missing_list}.";
	fi; unset _cpc_missing_list;
};

# Check whether all supplied pathnames resolve.
check_prereq_files() {
	while [ ${#} -gt 0 ]; do
		if [ ! -e ${1} ]; then
			_cpf_missing_list="${1}${_cpf_missing_list:+ ${_cpf_missing_list}}";
		fi; shift;
	done;
	if [ -n "${_cpf_missing_list}" ]; then
		log_msg failexit "Error: missing prerequisite file(s): ${_cpf_missing_list}.";
	fi; unset _cpf_missing_list;
};

# Clear the environment by unsetting each exported variable except
# for those named by the caller. 
clear_env_with_except() {
	_cewe_vfilter="${*}"; _cewe_unset_cmds="$(mktemp -q)";
	export | while read _cewe_vspec; do
		set -- ${_cewe_vspec}; shift;
		[ "${1#*[^\"\'=a-zA-Z0-9_]}" != "${1}" ] && continue;
		if ! match_list "${_cewe_vfilter}" " "	\
				$(get_prefix_lrg ${1} =); then
			echo unset $(get_prefix_lrg ${1} =) >> ${_cewe_unset_cmds};
		fi;
	done; . "${_cewe_unset_cmds}"; rm -f "${_cewe_unset_cmds}" 2>/dev/null;
	unset _cewe_vfilter _cewe_vspec;
};

# N.B.	URLs ($1) may contain `?' or '&' characters.
fetch() {
	[ ${ARG_NO_DOWNLOAD:-0} -eq 1 ] && return;
	_f_url="${1}"; _f_sha256sum_src="${2}";
	_f_url_dst="$(get_basename "${_f_url}")";
	rm_if_exists ${_f_url_dst};
	wget ${WGET_ARGS} -O ${_f_url_dst} "${_f_url}";
	if [ -n "${_f_sha256sum_src}" ]; then
		_f_sha256sum_dst="$(compare_hash ${_f_url_dst} ${_f_sha256sum_src})";
		if [ -n "${_f_sha256sum_dst}" ]; then
			set -- "${_f_url}" ${_f_sha256sum_dst} ${_f_sha256sum_src};
			unset _f_url _f_url_dst _f_sha256sum_src _f_sha256sum_dst;
			log_msg failexit "Error: hash mismatch for URL \`${1}' (is: ${2}, should be: ${3}.)";
		fi; unset _f_sha256sum_dst;
	fi; unset _f_url _f_url_dst _f_sha256sum_src;
};

compare_hash() {
	# Push the output of dgst(1SSL) and the caller-supplied hash
	# value to compare the former with on the pseudo-stack and
	# shift off# all but the last two positional parameters.
	# This corresponds to the hash output and caller-supplied
	# hash values.
	set -- $(openssl dgst -sha256 ${1}) ${2}; shift $((${#}-2));
	[ "${1}" = "${2}" ] || echo "${1}";
};
compare_hash_manifest() {
	while [ ${#} -gt 0 ]; do
		if ! compare_hash ${1} ${2}; then
			log_msg failexit "Error: hash mismatch for patch file \`${1}'.";
		fi; shift;
	done; return 0;
};

is_build_script_done() {
	if [ -n "${ARG_RESTART_SCRIPT_AT}" ]; then
		if [ "${1}" = clean ]\
		&& [ -z "${ARG_RESTART_SCRIPT_AT#ALL}" ]; then
			return 0;	# Skip
		elif [ "${1}" = finish ]\
		|| [ -z "${ARG_RESTART_SCRIPT_AT#ALL}" ]; then
			return 1;	# Build
		elif ! match_list ${ARG_RESTART_SCRIPT_AT} , ${1}; then
			return 0;	# Skip
		else
			return 1;	# Build
		fi;
	elif [ -f "${WORKDIR}/.${2:-$(get_basename ${SCRIPT_FNAME%.build})}.${1}" ]; then
		return 0;		# Skip
	else
		return 1;		# Build
	fi;
};
set_build_script_done() {
	_sbsd_script_fname=${SCRIPT_FNAME##*/};
	_sbsd_done_fname=${WORKDIR}/.${_sbsd_script_fname%.build};
	while [ $# -ge 1 ]; do
		if [ "${1#-}" != "${1}" ]; then
			rm -f ${_sbsd_done_fname}.${1#-};
		else
			touch ${_sbsd_done_fname}.${1};
			log_msg info "Finished build step ${1} of build script \`${_sbsd_script_fname}'.";
		fi; shift;
	done; unset _sbsd_script_fname _sbsd_done_fname;
};

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;
};
log_msg() {
	_lm_lvl=${1}; shift;
	case ${_lm_lvl} in
		failexit) printf "\033[${LOG_MSG_FAIL_COLOUR}m"; ;;
		fail) printf "\033[${LOG_MSG_FAIL_COLOUR}m"; ;;
		info) printf "\033[${LOG_MSG_INFO_COLOUR}m"; ;;
		succ) printf "\033[${LOG_MSG_SUCC_COLOUR}m"; ;;
		warn) printf "\033[${LOG_MSG_WARN_COLOUR}m"; ;;
	esac;
	if [ $# -gt 1 ]; then
		printf "==> %s %s %s\033[0m\n" "$(date "${TIMESTAMP_FMT}")" "${1}" "$*";
	else
		printf "==> %s %s\033[0m\n" "$(date "${TIMESTAMP_FMT}")" "${1}";
	fi; [ ${_lm_lvl} = failexit ] && exit 1 || unset _lm_lvl;
};

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

parse_with_pkg_name() {
	PKG_LVL=${1}; PKG_NAME=${2}; shift 2;
	while [ ${#} -gt 0 ]; do
		if [ "${PKG_NAME}" = "${1}" ]; then
			export_vars_subst PKG_LVL${PKG_LVL}_ PKG_ ${PKG_BUILD_VARS};
			export_vars_subst PKG_$(echo ${PKG_NAME} | tr a-z A-Z)_ PKG_ ${PKG_BUILD_VARS};
			[ -z "${PKG_URL}" ] && return 1;
			[ -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}";
			export PKG_PREFIX=$(get_var_unsafe PREFIX_LVL${PKG_LVL});
			for _pwpn_env_var in $(export); do
				if [ "${_pwpn_env_var#PKG_}" != "${_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 script" ${_pwpn_env_vars};
			fi; unset _pwpn_env_var _pwpn_env_vars;
			return 0;
		fi; shift;
	done;
	log_msg failexit "Error: package \`${PKG_NAME}' missing in \${PKG_BUILD_NAMES}.";
};

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};
};

update_build_status() {
	while [ ${#} -gt 0 ]; do
	case ${1} in
	build_start)
		if [ -f "${BUILD_STATUS_IN_PROGRESS_FNAME}" ]; then
			log_msg failexit "Error: build already in progress.";
		else
			touch ${BUILD_STATUS_IN_PROGRESS_FNAME};
			touch ${BUILD_STATUS_PROGRESS_FNAME:=${PREFIX}/BUILD_STARTED_AT_${BUILD_DATE_START:=$(date %Y-%m-%d-%H-%M-%S)}};
		fi;
		;;
	build_finish)
		if [ -f "${BUILD_STATUS_PROGRESS_FNAME}" ]; then
			_ubs_bsp_fname=${BUILD_STATUS_PROGRESS_FNAME};
			unset BUILD_STATUS_PROGRESS_FNAME;
			rm -f ${_ubs_bsp_fname}; 
			unset _ubs_bsp_fname;
		fi;
		touch ${BUILD_STATUS_FINISHED_AT_FNAME:=${PREFIX}/BUILD_FINISHED_AT_$(date ${TIMESTAMP_FMT_STATUS_FILES})};
		rotate_files BUILD_FINISHED_AT_\* 8;
		ln -sf ${BUILD_STATUS_FINISHED_AT_FNAME}\
			${BUILD_STATUS_FINISHED_AT_CURRENT_FNAME}; ;;
	tarball_start)
		rotate_files midipix.\*.tar.bz2 8;
		touch ${BUILD_STATUS_TARBALL_PROGRESS_FNAME:=${PREFIX}/TARBALL_STARTED_AT_$(date ${TIMESTAMP_FMT_STATUS_FILES})};
		;;
	tarball_finish)
		[ -f "${BUILD_STATUS_TARBALL_PROGRESS_FNAME}" ] &&\
			rm -f ${BUILD_STATUS_TARBALL_PROGRESS_FNAME}; ;;
	finish)
		[ -f "${BUILD_STATUS_IN_PROGRESS_FNAME}" ] &&\
			rm -f ${BUILD_STATUS_IN_PROGRESS_FNAME}; ;;
	esac; shift;
	done;
};

# vim:filetype=sh