summaryrefslogtreecommitdiffhomepage
path: root/subr.ex/ex_pkg.subr
blob: 94dbf6c83e60b0357366ff782a6525cc489daee6 (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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#
# 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.
#


#
# ex_pkg_check_depends() - check single named package for unsatisfied dependencies
# @_checkfl:		enable (1) or inhibit (0) dependency expansion
# @_rpkg_disabled:	in reference to variable of list of disabled packages
# @_rpkg_finished:	in reference to variable of list of finished packages
# @_pkg_name:		single package name
# @_rpkg_names:		in reference to variable of list of package names
# @_workdir:		pathname to build-specific temporary directory
#
# Returns:		zero (0) given no outstanding dependencies, non-zero (>0) otherwise
# Side effects:		${PKG_${_PKG_NAME}_DEPENDS_FULL} may be mutated
#
ex_pkg_check_depends() {
	local	_epcd_checkfl="${1}" _epcd_rpkg_disabled="${2}" _epcd_rpkg_finished="${3#\$}"	\
		_epcd_pkg_name="${4}" _epcd_rpkg_names="${5#\$}" _epcd_workdir="${6}"		\
		_epcd_dependfl=0 _epcd_depends="" _epcd_pkg_name_depend="";

	if [ "${_epcd_checkfl}" -eq 1 ]; then
		if ! rtl_get_var_unsafe \$_epcd_depends -u "PKG_"${_epcd_pkg_name}"_DEPENDS_FULL"\
		|| [ "${_epcd_depends:+1}" != 1 ];
		then
			if rtl_get_var_unsafe \$_epcd_depends -u "PKG_"${_epcd_pkg_name}"_DEPENDS";
			then
				eval PKG_"${_epcd_pkg_name}"_DEPENDS_FULL='$(rtl_uniq ${_epcd_depends})';
			else
				return 0;
			fi;
		fi;

		for _epcd_pkg_name_depend in ${_epcd_depends}; do
			if ! rtl_lmatch "${_epcd_rpkg_disabled}" "${_epcd_pkg_name_depend}"\
			&& ! ex_pkg_state_test2 "${_epcd_workdir}" "${_epcd_pkg_name_depend}" finish;
			then
				if ! rtl_lmatch "${_epcd_rpkg_names}" "${_epcd_pkg_name_depend}"; then
					rtl_log_msgV "fatalexit" "${MSG_build_unknown_dep}" "${_epcd_pkg_name_depend}" "${_epcd_pkg_name}";
				else
					_epcd_dependfl=1; break;
				fi;
			fi;
		done;
	fi;

	return "${_epcd_dependfl}";
};

#
# ex_pkg_find_package() - find build group a single named package belongs to
# @_rgroup_name:	out reference to variable of build group name
# @_group_names:	build group names
# @_pkg_name:		single named package
#
# Returns:		zero (0) on success, non-zero (>0) if package not found, group name on stdout if package was found.
#
ex_pkg_find_package() {
	local	_epfp_rgroup_name="${1#\$}" _epfp_group_names="${2}" _epfp_pkg_name="${3}"	\
		_epfp_foundfl=0 _epfp_group_name="" _epfp_pkg_names="";

	for _epfp_group_name in ${_epfp_group_names}; do
		if rtl_get_var_unsafe \$_epfp_pkg_names -u "${_epfp_group_name}_PACKAGES"\
		&& [ "${_epfp_pkg_names:+1}" = 1 ]\
		&& rtl_lmatch \$_epfp_pkg_names "${_epfp_pkg_name}"; then
			_epfp_foundfl=1; break;
		fi;
	done;

	case "${_epfp_foundfl:-0}" in
	0)	eval ${_epfp_rgroup_name}=;
		return 1; ;;

	1)	eval ${_epfp_rgroup_name}='${_epfp_group_name}';
		return 0; ;;
	esac;
};

#
# ex_pkg_get_default() - get single package default value
# @_rdefault:		out reference to variable of default value or "" on end of list
# @_default_idx:	one-based single default value index
# @_pkg_name:		single package name
# @_pkg_version:	single package version
# @_ldefault:		SP-separated list of default value names (any of: patches, patches_pre, vars_files)
#
# Returns:		zero (0) on success, non-zero (>0) on invalid default value name or unknown package.
#
ex_pkg_get_default() {
	local	_epgd_rdefault="${1#\$}" _epgd_default_idx="${2}" _epgd_pkg_name="${3}"	\
		_epgd_pkg_version="${4}" _epgd_ldefault="${5}"				\
		_epgd_default="" _epgd_group_name="" _epgd_patch_fname=""		\
		_epgd_pkg_name_full="" _epgd_pkg_patches_extra="" _epgd_rc=0;

	set --;
	_epgd_pkg_name_full="${_epgd_pkg_name}${_epgd_pkg_version:+-${_epgd_pkg_version}}";
	_epgd_rc=0;

	for _epgd_default in ${_epgd_ldefault}; do
		case "${_epgd_default}" in
		patches)
			rtl_get_var_unsafe \$_epgd_pkg_patches_extra -u "PKG_${_epgd_pkg_name}_PATCHES_EXTRA";
			set +o noglob;
			set --												\
				"${@}"											\
				"${MIDIPIX_BUILD_PWD}/patches/${_epgd_pkg_name}/"*.patch				\
				"${MIDIPIX_BUILD_PWD}/patches/${_epgd_pkg_name_full}.local.patch"			\
				"${MIDIPIX_BUILD_PWD}/patches/${_epgd_pkg_name_full}.local@${BUILD_HNAME}.patch"	\
				${_epgd_pkg_patches_extra};
			set -o noglob;
			;;

		patches_pre)
			set --										\
				"${@}"									\
				"${MIDIPIX_BUILD_PWD}/patches/${_epgd_pkg_name_full}_pre.local.patch"	\
				"${MIDIPIX_BUILD_PWD}/patches/${_epgd_pkg_name_full}_pre.local@${BUILD_HNAME}.patch";
			;;

		vars_files)
			rtl_get_var_unsafe \$_epgd_group_name -u "PKG_${_epgd_pkg_name}_GROUP";
			set --					\
				"vars/${_epgd_pkg_name}.vars"	\
				"vars.${_epgd_group_name}/${_epgd_pkg_name}.vars";
			;;

		*)
			_epgd_rc=1; break;
			;;
		esac;
	done;

	if [ "${_epgd_rc}" = 0 ]; then
		eval ${_epgd_rdefault}="\${${_epgd_default_idx}:-}";
	fi;

	return "${_epgd_rc}";
};

#
# ex_pkg_get_packages() - get list of packages belonging to single named build group
# @_rpkg_names:	out reference to variable of package names
# @_group_name:	build group name
#
# Returns:	zero (0) on success, non-zero (>0) on failure, list of package names on stdout on success.
#
ex_pkg_get_packages() {
	local	_epgp_rpkg_names="${1#\$}" _epgp_group_name="${2}"	\
		_epgp_pkg_names="";

	if rtl_get_var_unsafe \$_epgp_pkg_names -u "${_epgp_group_name}_PACKAGES"\
	&& [ "${_epgp_pkg_names:+1}" = 1 ]; then
		eval ${_epgp_rpkg_names}='${_epgp_pkg_names}';
		return 0;
	else
		eval ${_epgp_rpkg_names}=;
		return 1;
	fi;
};

#
# ex_pkg_get_package_vars() - get package variable names
# @_rpkg_vnames:		out reference to package variable names variable
# @_build_vars_default:		list of default build variables
# @_pkg_name:			single package name
#
# Returns:			zero (0) on success, non-zero (>0) on failure, list of package names on stdout on success.
#
ex_pkg_get_package_vars() {
	local	_epgpv_rpkg_vnames="${1#\$}" _epgpv_build_vars_default="${2}" _epgpv_pkg_name="${3}"	\
		_epgpv_pkg_name_uc="" _epgpv_vname="" _epgpv_vnames="";
	rtl_toupper2 \$_epgpv_pkg_name \$_epgpv_pkg_name_uc;

	for _epgpv_vname in ${_epgpv_build_vars_default}; do
		if eval [ \"\${PKG_${_epgpv_pkg_name_uc}_${_epgpv_vname}:+1}\" = 1 ]; then
			rtl_lconcat \$_epgpv_vnames "PKG_${_epgpv_pkg_name_uc}_${_epgpv_vname}";
		fi;
	done;

	eval ${_epgpv_rpkg_vnames}='${_epgpv_vnames}';
	return 0;
}

#
# ex_pkg_load_vars() - load build variables
# @_rstatus:		out reference to status string
# @_rbuild_arch:	in reference to build architecture
# @_rbuild_kind:	in reference to build kind
#
# Returns:	zero (0) on success, non-zero (>0) on failure, build variables post-return on success.
#
ex_pkg_load_vars() {
	local	_eplv_rstatus="${1#\$}" _eplv_rbuild_arch="${2#\$}" _eplv_rbuild_kind="${3#\$}"	\
		_eplv_build_arch="" _eplv_rc=0 _eplv_fname="";

	if ! rtl_lmatch "${_eplv_rbuild_arch}" "nt32 nt64"; then
		_eplv_rc=1;
		rtl_setrstatus "${_eplv_rstatus}" 'invalid architecture \`${'"${_eplv_rbuild_arch}"'}'\''.';
	elif ! rtl_lmatch "${_eplv_rbuild_kind}" "debug release"; then
		_eplv_rc=1;
		rtl_setrstatus "${_eplv_rstatus}" 'unknown build type \`${'"${_eplv_rbuild_kind}"'}'\''.';
	else
		eval _eplv_build_arch="\${${_eplv_rbuild_arch}}";
		case "${_eplv_build_arch}" in
		nt32)	DEFAULT_TARGET="i686-nt32-midipix"; ;;
		nt64)	DEFAULT_TARGET="x86_64-nt64-midipix"; ;;
		esac;

		if [ "${PREFIX_ROOT:+1}" ]\
		&& [ "${PREFIX_ROOT#/}" = "${PREFIX_ROOT}" ]; then
			PREFIX_ROOT="${PWD%/}/${PREFIX_ROOT}";
		fi;
		if [ "${PREFIX:+1}" ]\
		&& [ "${PREFIX#/}" = "${PREFIX}" ]; then
			PREFIX="${PWD%/}/${PREFIX}";
		fi;

		for _eplv_fname in				\
				"${HOME}/midipix_build.vars"	\
				"${HOME}/.midipix_build.vars"	\
				../midipix_build.vars;
		do
			if [ -r "${_eplv_fname}" ]; then
				rtl_fileop source "${_eplv_fname}";
			fi;
		done;

		set +o noglob;
		for _eplv_fname in				\
				./vars.env.d/*.env;
		do
			set -o noglob;
			if [ -r "${_eplv_fname}" ]; then
				rtl_fileop source "${_eplv_fname}";
			fi;
		done;
		set -o noglob;
	fi;

	return "${_eplv_rc}";
};

#
# ex_pkg_load_groups() - load all available build groups
# @_rgroups:		out reference to variable of build groups
# @_rgroups_noauoto:	optional out reference to variable of build groups not built automatically
#
# Returns:		zero (0) on success, non-zero (>0) on failure.
#
ex_pkg_load_groups() {
	local	_eplg_rgroups="${1#\$}" _eplg_rgroups_noauto="${2#\$}"	\
		_eplg_build_groups="" _eplg_build_groups_noauto="" _eplg_fname="";

	EXP_PKG_REGISTER_GROUP_RGROUPS="${_eplg_rgroups}";
	EXP_PKG_REGISTER_GROUP_RGROUPS_NOAUTO="${_eplg_rgroups_noauto}";
	for _eplg_fname in $(find ./groups.d -name *.group | sort); do
		rtl_fileop source_with_fnamevar "${_eplg_fname}";
	done;
	unset EXP_PKG_REGISTER_GROUP_RGROUPS;
	unset EXP_PKG_REGISTER_GROUP_RGROUPS_NOAUTO;

	for _eplg_fname in $(find ./groups.d -mindepth 2 -name *.package | sort); do
		rtl_fileop source_with_fnamevar "${_eplg_fname}";
	done;

	return 0;
};

#
# ex_pkg_register() - register single package
# @_pkg_name:		single package name
# @_fname:		pathname to file package is defined in, relative to midipix_build root
# @[_group_name]:	optional build group name; inferred from @_fname if not specified
#
# Returns:		zero (0) on success, non-zero (>0) on failure.
#
ex_pkg_register() {
	local	_epr_pkg_name="${1}" _epr_fname="${2}" _epr_group_name="${3:-}"	\
		_epr_group_name_uc="" _epr_group_noautofl=0;

	if [ "${_epr_group_name:+1}" != 1 ]; then
		_epr_group_name="${_epr_fname#./*/}";
		_epr_group_name="${_epr_group_name%%/*}";
		_epr_group_name="${_epr_group_name%.d}";
		_epr_group_name="${_epr_group_name#*.}";
	fi;

	rtl_toupper2 \$_epr_group_name \$_epr_group_name_uc;
	rtl_lconcat "\$${_epr_group_name_uc}_PACKAGES" "${_epr_pkg_name}";
	rtl_set_var_unsafe -u "PKG_${_epr_pkg_name}_GROUP" "${_epr_group_name}";
	rtl_set_var_unsafe -u "PKG_${_epr_pkg_name}_GROUP_FNAME" "${_epr_fname}";

	return 0;
};

#
# ex_pkg_register_group() - register single group
# @_group_name:					single group name
# @_fname:					pathname to file group is defined in, relative to midipix_build root
# @[_autofl]:					"auto" for groups to build by default, "noauto" for optional groups only built when requested
# ${EXP_PKG_REGISTER_GROUP_RGROUPS}:		inout reference to variable of build groups
# ${EXP_PKG_REGISTER_GROUP_RGROUPS_NOAUTO}:	inout reference to variable of build groups only built when requested
#
# Returns:		zero (0) on success, non-zero (>0) on failure.
#
ex_pkg_register_group() {
	local	_eprg_group_name="${1}" _eprg_fname="${2}" _eprg_autofl="${3:-auto}"	\
		_eprg_pkg_name="" _eprg_pkg_names="" _eprg_rgroups="";

	case "${_eprg_autofl}" in
	auto)	_eprg_rgroups="${EXP_PKG_REGISTER_GROUP_RGROUPS}"; ;;
	noauto)	_eprg_rgroups="${EXP_PKG_REGISTER_GROUP_RGROUPS_NOAUTO}"; ;;
	*)	;;
	esac;
	if ! rtl_lmatch "${_eprg_rgroups}" "${_eprg_group_name}"; then
		rtl_lconcat "${_eprg_rgroups}" "${_eprg_group_name}";
	fi;

	if rtl_get_var_unsafe \$_eprg_pkg_names -u "${_eprg_group_name}_PACKAGES"\
	&& [ "${_eprg_pkg_names:+1}" = 1 ]; then
		case "${_eprg_autofl}" in
		noauto)
			for _eprg_pkg_name in ${_eprg_pkg_names}; do
				rtl_set_var_unsafe -u "PKG_${_eprg_pkg_name}_GROUP" "${_eprg_group_name}";
				rtl_set_var_unsafe -u "PKG_${_eprg_pkg_name}_GROUP_FNAME" "${_eprg_fname}";
			done;
			;;
		esac;
	fi;

	return 0;
};

#
# ex_pkg_unfold_depends() - unfold list of package names into dependency-expanded set of complete, disabled, finished, and outstanding package names
# @_rdisabled:		in reference toout variable of disabled packages
# @_rfinished:		in reference toout variable of finished packages
# @_rnames:		out reference to variable of package names
# @_checkfl:		enable (1) or inhibit (0) dependency expansion
# @_forcefl:		enable (1) or inhibit (0) forcibly rebuilding finished packages
# @_group_name:		build group name
# @_pkg_names:		list of package names
# @_restart:		optional whitespace-separated list of package names to rebuild
# @_test_finished:	only exclude disabled packages (0,) split finished packages
# @_workdir:		pathname to build-specific temporary directory
#
# Returns:		zero (0) on success, non-zero (>0) on failure.
#
ex_pkg_unfold_depends() {
	local	_epud_rdisabled="${1#\$}" _epud_rfinished="${2#\$}" _epud_rnames="${3#\$}"	\
		_epud_checkfl="${4}" _epud_forcefl="${5}" _epud_group_name="${6}"		\
		_epud_pkg_names="${7}" _epud_restart="${8}" _epud_test_finished="${9}"		\
		_epud_workdir="${10}"								\
		_epud_pkg_disabled="" _epud_pkg_force="" _epud_pkg_name=""			\
		_epud_pkg_names_new=""	_epud_restartfl=0;

	if [ "${_epud_restart:+1}" = 1 ]\
	&& ! rtl_lmatch \$_epud_restart "ALL LAST"; then
		rtl_lsearch \$_epud_pkg_names "${_epud_restart}";
	fi;
	if [ "${_epud_restart:+1}" = 1 ]\
	&& [ "${_epud_checkfl:-0}" -eq 1 ]; then
		rtl_lunfold_dependsV 'PKG_${_rld_name}_DEPENDS' \$_epud_pkg_names ${_epud_pkg_names};
		_epud_pkg_names="$(rtl_uniq ${_epud_pkg_names})";
	fi;

	for _epud_pkg_name in ${_epud_pkg_names}; do
		if [ "${_epud_restart}" = "ALL" ]\
		|| rtl_lmatch \$_epud_restart "${_epud_pkg_name}"; then
			_epud_restartfl=1;
		else
			_epud_restartfl=0;
		fi;

		if rtl_get_var_unsafe \$_epud_pkg_disabled -u "PKG_${_epud_pkg_name}_DISABLED"\
		&& [ "${_epud_pkg_disabled}" = 1 ];
		then
			rtl_lconcat "${_epud_rdisabled}" "${_epud_pkg_name}";

		elif [ "${_epud_test_finished:-1}" -eq 1 ]\
		&&   ex_pkg_state_test2 "${_epud_workdir}" "${_epud_pkg_name}" finish\
		&&   [ "${_epud_restartfl:-0}" -eq 0 ]\
		&&   [ "${_epud_forcefl:-0}" -ne 1 ]\
		&&   rtl_get_var_unsafe \$_epud_pkg_force -u "${_epud_group_name}_FORCE"\
		&&   [ "${_epud_pkg_force}" != 1 ];
		then
			rtl_lconcat "${_epud_rfinished}" "${_epud_pkg_name}";

		else
			rtl_lconcat \$_epud_pkg_names_new "${_epud_pkg_name}";
		fi;
	done;

	eval ${_epud_rdisabled}='$(rtl_uniq2 "${_epud_rdisabled}")';
	eval ${_epud_rfinished}='$(rtl_uniq2 "${_epud_rfinished}")';
	eval ${_epud_rnames}='$(rtl_uniq "${_epud_pkg_names_new}")';

	return 0;
};

#
# ex_pkg_unfold_rdepends() - unfold list of package names into reverse dependency-expanded set of complete, disabled, finished, and outstanding package names
# @_rdisabled:		in reference toout variable of disabled packages
# @_rfinished:		in reference toout variable of finished packages
# @_rnames:		out reference to variable of package names
# @_group_name:		build group name
# @_pkg_names:		list of package names
# @_restart:		optional whitespace-separated list of package names to rebuild
# @_test_finished:	only exclude disabled packages (0,) split finished packages
# @_workdir:		pathname to build-specific temporary directory
#
# Returns:		zero (0) on success, non-zero (>0) on failure.
#
ex_pkg_unfold_rdepends() {
	local	_epur_rdisabled="${1#\$}" _epur_rfinished="${2#\$}" _epur_rnames="${3#\$}"	\
		_epur_group_name="${4}" _epur_pkg_names="${5}" _epur_restart="${6}"		\
		_epur_test_finished="${7}" _epur_workdir="${8}"					\
		_epur_depends="" _epur_disabled=0 _epur_force=0 _epur_pkg_depends=""		\
		_epur_pkg_name="" _epur_pkg_names_new="" _epur_pkg_name_depend=""		\
		_epur_pkg_rdepends="";

	for _epur_pkg_name_depend in ${_epur_restart}; do
		for _epur_pkg_name in ${_epur_pkg_names}; do
			if [ "${_epur_pkg_name}" = "${_epur_pkg_name_depend}" ]; then
				continue;

			elif rtl_get_var_unsafe \$_epur_depends -u "PKG_"${_epur_pkg_name}"_DEPENDS"\
			  && rtl_lunfold_dependsV 'PKG_${_rld_name}_DEPENDS' \$_epur_pkg_depends ${_epur_depends}\
			  && [ "${_epur_pkg_depends:+1}" = 1 ]\
			  && rtl_lmatch \$_epur_pkg_depends "${_epur_pkg_name_depend}";
			then
				if rtl_get_var_unsafe \$_epur_disabled -u "PKG_${_epur_pkg_name}_DISABLED"\
				&& [ "${_epur_disabled}" = 1 ];
				then
					rtl_lconcat "${_epur_rdisabled}" "${_epur_pkg_name}";

				elif [ "${_epur_test_finished}" -eq 1 ]\
				&&   ex_pkg_state_test2 "${_epur_workdir}" "${_epur_pkg_name}" finish\
				&&   rtl_get_var_unsafe \$_epur_force -u "${_epur_group_name}_FORCE"\
				&&   [ "${_epur_force}" != 1 ];
				then
					rtl_lconcat "${_epur_rfinished}" "${_epur_pkg_name}";

				elif [ "${_epur_test_finished:-1}" -eq 0 ]\
				||   ! ex_pkg_state_test2 "${_epur_workdir}" "${_epur_pkg_name}" finish\
				||   ( rtl_get_var_unsafe \$_epur_force -u "${_epur_group_name}_FORCE"\
				&&   [ "${_epur_force}" = 1 ] );
				then
					rtl_lconcat \$_epur_pkg_names_new "${_epur_pkg_name}";
				fi;
			fi;
		done;
	done;

	eval ${_epur_rdisabled}='$(rtl_uniq2 "${_epur_rdisabled}")';
	eval ${_epur_rfinished}='$(rtl_uniq2 "${_epur_rfinished}")';
	eval ${_epur_rnames}='$(rtl_uniq "${_epur_pkg_names_new}")';

	return 0;
};

# vim:filetype=sh textwidth=0