summaryrefslogtreecommitdiffhomepage
path: root/subr.ex/ex_pkg_exec.subr
blob: d64d81599d751c7247578f7849b5612d66a495d8 (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
#
# Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 Lucía Andrea Illanes Albornoz <lucia@luciaillanes.de>
# set +o errexit -o noglob -o nounset is assumed.
#

#
# exp_pkg_exec_pre() - prepare environment for single named package w/ dispatcher
# @_group_name:		build group name
# @_pkg_name:		single package name
# @_restart_at:		optional comma-separated list of build steps at which to rebuild or ALL or LAST
# @_workdir:		pathname to build-specific temporary directory
#
# Returns:		zero (0) on success, non-zero (>0) on failure
#
exp_pkg_exec_pre() {
	local _eppep_group_name="${1}" _eppep_pkg_name="${2}" _eppep_restart_at="${3}" _eppep_workdir="${4}";

	if [ "${PKG_URL:+1}" != 1 ]\
	&& [ "${PKG_URLS_GIT:+1}" != 1 ]\
	&& [ "${PKG_VERSION:+1}" != 1 ]\
	&& [ "${PKG_INSTALL_FILES:+1}" != 1 ]\
	&& [ "${PKG_INSTALL_FILES_V2:+1}" != 1 ]\
	&& ! rtl_test_cmd "pkg_${_eppep_pkg_name}_all";
	then
		"${_eppep_dispatch_fn}" missing_pkg "${_eppep_group_name}" "${_eppep_pkg_name}";
		return 1;
	elif ! ex_pkg_state_test2			\
			"${_eppep_workdir}"		\
			"${_eppep_pkg_name}" "start"	\
			"${_eppep_restart_at}";
	then
		if [ "${PKG_NO_CLEAN_BASE_DIR:-0}" -eq 0 ]\
		&& ! rtl_fileop rm "${PKG_BASE_DIR}" "${PKG_BUILD_DIR}" "${PKG_DESTDIR}" "${PKG_DESTDIR_HOST}"\
		|| ! rtl_fileop mkdir "${PKG_BASE_DIR}";
		then
			return 1;
		fi;

		if ! rtl_fileop mkdir "${PKG_BUILD_DIR}" "${PKG_DESTDIR}"\
		|| ! ex_pkg_state_set2 "${_eppep_workdir}" "${_eppep_pkg_name}" "start";
		then
			return 1;
		fi;
	elif ! rtl_exists_any "${PKG_BASE_DIR}" "${PKG_BUILD_DIR}" "${PKG_DESTDIR}" "${PKG_DESTDIR_HOST}"\
	&&   ! rtl_fileop mkdir "${PKG_BASE_DIR}" "${PKG_BUILD_DIR}" "${PKG_DESTDIR}" "${PKG_DESTDIR_HOST}";
	then
		return 1
	fi;

	rtl_fileop cd "${PKG_BUILD_DIR}";
	return "${?}";
};

#
# exp_pkg_exec_step() - execute single build steps & update state for single named package w/ dispatcher
# @_group_name:		build group name
# @_pkg_name:		single package name
# @_restart_at:		optional comma-separated list of build steps at which to rebuild or ALL or LAST
# @_step:		build step to execute
#
# Returns:		zero (0) on success, non-zero (>0) on failure
#
exp_pkg_exec_step() {
	local	_eppes_group_name="${1}" _eppes_pkg_name="${2}" _eppes_restart_at="${3}"	\
		_eppes_step="${4}"								\
		_eppes_fn_name="" _eppes_pkg_step_fn="" _eppes_rc=0;

	if rtl_test_cmd "pkg_${_eppes_pkg_name}_${_eppes_step}"; then
		_eppes_pkg_step_fn="pkg_${_eppes_pkg_name}_${_eppes_step}";
	else
		_eppes_pkg_step_fn="pkg_${_eppes_step}";
	fi;

	for _eppes_fn_name in 						\
			"pkg_${_eppes_pkg_name}_${_eppes_step}_pre"	\
			"${_eppes_pkg_step_fn}"				\
			"pkg_${_eppes_pkg_name}_${_eppes_step}_post";
	do
		if rtl_test_cmd "${_eppes_fn_name}"\
		&& ! "${_eppes_fn_name}"					\
				"${_eppes_group_name}" "${_eppes_pkg_name}"	\
				"${_eppes_restart_at}";
		then
			_eppes_rc=1; break;
		fi;
	done;

	return "${_eppes_rc}";
};

#
# exp_pkg_exec() - execute all pertaining build steps & update state for single named package w/ dispatcher
# @_dispatch_fn:	top-level dispatch function name
# @_group_name:		build group name
# @_pkg_name:		single package name
# @_restart_at:		optional comma-separated list of build steps at which to rebuild or ALL or LAST
# @_workdir:		pathname to build-specific temporary directory
#
# Returns:		zero (0) on success, non-zero (>0) on failure
#
exp_pkg_exec() {
	local	_eppe_dispatch_fn="${1}" _eppe_group_name="${2}" _eppe_pkg_name="${3}"	\
		_eppe_restart_at="${4}" _eppe_workdir="${5}"				\
		_eppe_build_step_last="" _eppe_rc=0 _eppe_step="";

	if ! exp_pkg_exec_pre						\
			"${_eppe_group_name}" "${_eppe_pkg_name}"	\
			"${_eppe_restart_at}" "${_eppe_workdir}"\
	|| ! "${_eppe_dispatch_fn}"				\
			start_pkg_child "${_eppe_group_name}"	\
			"${_eppe_pkg_name}";
	then
		_eppe_rc=1;
	elif rtl_test_cmd "pkg_${_eppe_pkg_name}_all"; then
		"pkg_${_eppe_pkg_name}_all"				\
			"${_eppe_group_name}" "${_eppe_pkg_name}"	\
			"${_eppe_restart_at}";
		_eppe_rc="${?}";
	else
		set -- ${PKG_BUILD_STEPS};
		while [ ${#} -gt 0 ]; do
			_eppe_step="${1}"; shift;

			if [ "${#_eppe_restart_at}" -gt 0 ]\
			&& [ "${_eppe_restart_at}" != "ALL" ]\
			&& [ "${_eppe_restart_at}" != "LAST" ]\
			&& ! rtl_lmatch \$_eppe_restart_at "${_eppe_step}" ","; then
				continue;
			fi;

			if [ "${_eppe_step}" = "finish" ]; then
				ex_pkg_state_set2 "${_eppe_workdir}" "${_eppe_pkg_name}" finish; break;
			elif [ "${PKG_FORCE:-0}" -eq 0 ]\
			&&   ex_pkg_state_test2 "${_eppe_workdir}" "${_eppe_pkg_name}" "${_eppe_step}" "${_eppe_restart_at}";
			then
				continue;
			elif ! exp_pkg_exec_step					\
					"${_eppe_group_name}" "${_eppe_pkg_name}"	\
					"${_eppe_restart_at}" "${_eppe_step}";
			then
				_eppe_rc=1; break;
			else	ex_pkg_dispatch_send "step" "${_eppe_group_name}" "${_eppe_pkg_name}" "${_eppe_step}";
				ex_pkg_state_set2 "${_eppe_workdir}" "${_eppe_pkg_name}" "${_eppe_step}" "${@}";
			fi;
		done;
	fi;

	return "${_eppe_rc}";
};

#
# ex_pkg_exec() - prepare environment for, execute all pertaining build steps & update state for single named package w/ dispatcher
# @_rdispatch_count:		reference to inout variable of dispatcher count
# @_dispatch_count_cur:		current dispatcher count
# @_dispatch_count_max:		maximum dispatcher count
# @_dispatch_group_cur:		current group
# @_dispatch_group_max:		maximum group
# @_rdispatch_njobs:		reference to inout variable of dispatcher count
# @_rdispatch_wait:		reference to inout variable of package names in a wait state
# @_build_steps_default:	list of default build steps
# @_build_vars_default:		list of default build variables
# @_dispatch_fn:		top-level dispatch function name
# @_group_name:			build group name
# @_pipe_path:			pathname to parent-child process FIFO
# @_pkg_name:			single package name
# @_restart_at:			optional comma-separated list of build steps at which to rebuild or ALL or LAST
# @_workdir:			pathname to build-specific temporary directory
#
# Returns:			zero (0) on success, non-zero (>0) on failure.
#
ex_pkg_exec() {
	local	_epdp_rdispatch_count="${1#\$}" _epdp_dispatch_count_cur="${2}" _epdp_dispatch_count_max="${3}"		\
		_epdp_dispatch_group_cur="${4}" _epdp_dispatch_group_max="${5}" _epdp_rdispatch_njobs="${6#\$}"		\
		_epdp_rdispatch_wait="${7#\$}" _epdp_build_steps_default="${8}" _epdp_build_vars_default="${9}"		\
		_epdp_dispatch_fn="${10}" _epdp_group_name="${11}" _epdp_pipe_path="${12}" _epdp_pkg_name="${13}"	\
		_epdp_restart_at="${14}" _epdp_workdir="${15}"								\
		_epdp_dispatch_count_new=0 _epdp_perc_group=0 _epdp_perc_pkg=0;

	rtl_percentage2 \$_epdp_dispatch_group_cur \$_epdp_dispatch_group_max \$_epdp_perc_group;
	rtl_percentage2 \$_epdp_dispatch_count_cur \$_epdp_dispatch_count_max \$_epdp_perc_pkg;

	eval _epdp_dispatch_count_new='$((${'"${_epdp_rdispatch_count}"'}+1))';
	if "${_epdp_dispatch_fn}"				\
			start_pkg "${_epdp_group_name}"		\
			"${_epdp_pkg_name}"			\
			"${_epdp_dispatch_count_new}"		\
			"${_epdp_dispatch_count_max}"		\
			"${_epdp_perc_group}" "${_epdp_perc_pkg}";
	then
		eval : '$(('${_epdp_rdispatch_njobs}'+=1))';
		eval ${_epdp_rdispatch_count}=\"\${_epdp_dispatch_count_new}\";
		rtl_lconcat "${_epdp_rdispatch_wait}" "${_epdp_pkg_name}";

		(trap "if [ \${?} -eq 0 ]; then												\
			ex_pkg_dispatch_send \"done\" \"${_epdp_group_name}\" \"${_epdp_pkg_name}\" \"${_epdp_dispatch_count_new}\";	\
		      else														\
			ex_pkg_dispatch_send \"fail\" \"${_epdp_group_name}\" \"${_epdp_pkg_name}\" \"${_epdp_dispatch_count_new}\";	\
			pkill -U "${$}";												\
		      fi;" EXIT HUP INT TERM USR1 USR2;
		set +o errexit -o noglob -o nounset; BUILD_IS_PARENT=0; rtl_log_set_fname ""; rtl_log_set_no_attr 1;

		if ex_pkg_env									\
				"${_epdp_build_steps_default}" "${_epdp_build_vars_default}"	\
				"${_epdp_group_name}" "${_epdp_pkg_name}"			\
				"${_epdp_restart_at}" "${_epdp_workdir}";
		then
			exp_pkg_exec						\
				"${_epdp_dispatch_fn}" "${_epdp_group_name}"	\
				"${_epdp_pkg_name}" "${_epdp_restart_at}"	\
				"${_epdp_workdir}";
		else
			return 1;
		fi;) 1>"${_epdp_workdir}/${_epdp_pkg_name}_stderrout.log" 2>&1 3>"${_epdp_pipe_path}" &
		return "${?}";
	else
		return 1;
	fi;
};

# vim:filetype=sh textwidth=0