summaryrefslogtreecommitdiffhomepage
path: root/subr.ex/ex_rtl_rpm.subr
blob: 871abf6aae1840809cad3e3fbe2b9670e55f8b44 (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
#
# 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_rtl_build_rpm() - build RPM package
# @_destdir:		package destination directory
# @_jobs_max:		maximum count of simultaneously executing rpmbuild(1) processes
# @_jobs_semaphore:	pathname to current count of simultaneously executing rpmbuild(1) processes semaphore file
# @_jobs_wait:		wait period in seconds when @_jobs_semaphore is locked
# @_name:		package name
# @_rpms_dname:		RPMs directory pathname
# @_spec_fname:		RPM .spec file pathname
# @_spec_fname_tgt:	target package RPM .spec file pathname
# @_tmp_dname:		temporary files directory pathname
# @_topdir:		package top directory
# @_url:		package URL
# @_version:		package version
# @_version_rpm:	package RPM version
#
# Returns:		zero (0) on success, non-zero (>0) on failure
# N.B.:			The following variables are substituted prior to running rpmbuild(1):
#			%{pkg_destdir}
#			%{pkg_name}
#			%{pkg_url}
#			%{pkg_version_full}
#			%{pkg_version_rpm}
#
#			The \n-separated list of package files is inserted after the `%files' line.
#
ex_rtl_build_rpm() {
	local	_erbr_destdir="${1}" _erbr_jobs_max="${2}" _erbr_jobs_semaphore="${3}" _erbr_jobs_wait="${4}"	\
		_erbr_name="${5}" _erbr_rpms_dname="${6}" _erbr_spec_fname="${7}" _erbr_spec_fname_tgt="${8}"	\
		_erbr_tmp_dname="${9}" _erbr_topdir="${10}" _erbr_url="${11}" _erbr_version="${12}"		\
		_erbr_version_rpm="${13}"									\
		_erbr_spec_fname_tgt_tmp="" _erbr_job_count=0 _erbr_rc=0 _erbr_rc_jobs=0;

(
	while true; do
		(set +o errexit -o noglob -o nounset;
		rtl_flock_acquire 4 || exit $((${?} + 1));

		read _erbr_job_count <&4;
		if [ "${_erbr_job_count:+1}" != 1 ]; then
			_erbr_job_count=0;
		fi;

		if [ "${_erbr_job_count}" -ge "${_erbr_jobs_max}" ]; then
			exit 1;
		else
			: $((_erbr_job_count += 1));
			printf "%s" "${_erbr_job_count}" >"${_erbr_jobs_semaphore}" || exit $((${?} + 1));
			exit 0;
		fi) 4<>"${_erbr_jobs_semaphore}";
		_erbr_rc_jobs="${?}";

		case "${_erbr_rc_jobs}" in
		0)	break; ;;
		1)	sleep "${_erbr_jobs_wait}" || return "${?}"; ;;
		*)	return $((${_erbr_rc_jobs} - 1));
		esac;
	done;

	trap '
		(set +o errexit -o noglob -o nounset;
		rtl_flock_acquire 4 || exit 1;

		read _erbr_job_count <&4;
		if [ "${_erbr_job_count:+1}" != 1 ]\
		|| [ "${_erbr_job_count}" -lt 1 ]; then
			_erbr_job_count=1;
		fi;

		: $((_erbr_job_count -= 1));
		printf "%s" "${_erbr_job_count}" >"${_erbr_jobs_semaphore}" || exit 1;
		exit 0) 4<>"${_erbr_jobs_semaphore}"'	\
		EXIT HUP INT TERM USR1 USR2;

	(
	_erbr_spec_fname_tgt_tmp="${_erbr_spec_fname_tgt}.tmp";
	trap "rm -f \"${_erbr_spec_fname_tgt_tmp}\"" EXIT HUP INT TERM USR1 USR2;

	#
	# N.B.	Keep list in sync w/ list in function documentation comment block at top.
	#
	if rtl_fileop cp "${_erbr_spec_fname}" "${_erbr_spec_fname_tgt_tmp}"	\
	&& sed	-i""								\
			-ne '/^#REMOVEDBYSED#/!p'				\
			"${_erbr_spec_fname_tgt_tmp}"				\
	&& sed	-i""								\
			-e 's#%{pkg_destdir}#'"${_erbr_destdir}"'#g'		\
			-e 's#%{pkg_name}#'"${_erbr_name}"'#g'			\
			-e 's#%{pkg_url}#'"${_erbr_url}"'#g'			\
			-e 's#%{pkg_version_full}#'"${_erbr_version}"'#g'	\
			-e 's#%{pkg_version_rpm}#'"${_erbr_version_rpm}"'#g'	\
			"${_erbr_spec_fname_tgt_tmp}"				\
	&& sed	-n -e '1,/^%files$/p'						\
			"${_erbr_spec_fname_tgt_tmp}"				\
			> "${_erbr_spec_fname_tgt}"				\
	&& (cd "${_erbr_destdir}";						\
	    find -mindepth 1 \( -type f -or -type l \) |			\
	    sed -e 's,^./,'"/"',')						\
	    		>> "${_erbr_spec_fname_tgt}"				\
	&& sed	-n -e '/^%files$/,${/^%files$/d;p}'				\
			"${_erbr_spec_fname_tgt_tmp}"				\
			>> "${_erbr_spec_fname_tgt}"				\
		;								\
	then
		_erbr_rc=0;
	else
		_erbr_rc=1;
	fi;
	exit "${_erbr_rc}";
	); _erbr_rc="${?}";

	if [ "${_erbr_rc}" -eq 0 ]; then
		while true; do
			rpmbuild						\
				-bb						\
				--define "_binary_payload w2T16.xzdio"		\
				--define="_tmppath ${_erbr_tmp_dname=}"		\
				--define="_topdir ${_erbr_topdir}"		\
				--nodeps					\
				"${_erbr_spec_fname_tgt}";
			_erbr_rc="${?}";

			if [ "${_erbr_rc}" -eq 141 ]; then
				_erbr_rc=0; continue;
			else
				break;
			fi;
		done;

		if [ "${_erbr_rc}" -eq 0 ]; then
			if ! find "${_erbr_rpms_dname}/${_erbr_name}-${_erbr_version_rpm}/RPMS"		\
				-iname \*.rpm -exec cp -pP {} "${_erbr_rpms_dname}/" \;			\
			|| ! rtl_fileop rm "${_erbr_rpms_dname}/${_erbr_name}-${_erbr_version_rpm}"	\
			|| ! rtl_fileop cp "${_erbr_spec_fname_tgt}" "${_erbr_rpms_dname}/";
			then
				_erbr_rc=1;
			fi;
		fi;
	fi;

	exit "${_erbr_rc}";
);
	_erbr_rc="${?}";

	return "${_erbr_rc}";
};

#
# ex_rtl_expand_rpm_version() - expand RPM version strings
# @_rurl:		out reference to package URL
# @_rversion:		out reference to package version
# @_rversion_rpm:	out reference to RPM version string
# @_subdir:		package subdirectory
# @_url:		package URL
# @_url_censor:		package URL filter
# @_urls_git:		package Git URL(s)
# @_version:		package version
#
# Returns:		zero (0) on success, non-zero (>0) on failure
#
ex_rtl_expand_rpm_version() {
	local	_ererv_rurl="${1#\$}" _ererv_rversion="${2#\$}" _ererv_rversion_rpm="${3#\$}"	\
		_ererv_subdir="${4}" _ererv_url="${5}" _ererv_url_censor="${6}"			\
		_ererv_urls_git="${7}" _ererv_version="${8}"					\
		_ererv_version_full="" _ererv_version_rpm=""

	if [ "${_ererv_url:+1}" = 1 ]; then
		_ererv_url="${_ererv_url%% *}";
		_ererv_version_full="${_ererv_version}";
		_ererv_version_rpm="${_ererv_version%%-*}";

	elif [ "${_ererv_urls_git:+1}" = 1 ]; then
		_ererv_url="${_ererv_urls_git%% *}";
		_ererv_url="${_ererv_url##*=}";
		_ererv_url="${_ererv_url%%@*}";

		_ererv_version_rpm="$(cd "${_ererv_subdir}" && git rev-parse HEAD)" || return 1;
		_ererv_version_full="${_ererv_version_rpm} ($(cd "${_ererv_subdir}" && git rev-parse --abbrev-ref HEAD))" || return 1;
	fi;

	if [ "${_ererv_url#${_ererv_url_censor}}" != "${_ererv_url}" ]; then
		_ererv_url="Unknown";
	fi;

	eval ${_ererv_rurl}='${_ererv_url}';
	eval ${_ererv_rversion}='${_ererv_version_full}';
	eval ${_ererv_rversion_rpm}='${_ererv_version_rpm}';

	return 0;
};

#
# ex_rtl_fixup_pkgconfig_paths() - fixup pathname prefixes in pkg-config(1) files
# @_dname_base:		base directory pathname
#
# Returns:		zero (0) on success, non-zero (>0) on failure
#
ex_rtl_fixup_pkgconfig_paths() {
	local	_erfpp_dname_base="${1}"	\
		_erfpp_pc_path="";

	for _erfpp_pc_path in $(find "${_erfpp_dname_base=}" -name \*.pc); do
		if [ -n "$(sed -ne '/^libdir=[^$]*$/p' "${_erfpp_pc_path}")" ]			\
		&& ! sed -i""	-e '/^libdir=[^$]*$/s/^libdir=\(.*\)$/libdir=${exec_prefix}\1/'	\
				-e '/^exec_prefix=$/s/^.*$/exec_prefix=${prefix}/'		\
				"${_erfpp_pc_path}";
		then
			return 1;
		fi;

		if [ -n "$(sed -ne '/^includedir=[^$]*$/p' "${_erfpp_pc_path}")" ]			\
		&& ! sed -i""	-e '/^includedir=[^$]*$/s/^includedir=\(.*\)$/includedir=${prefix}\1/'	\
				"${_erfpp_pc_path}";
		then
			return 1;
		fi;
	done;

	return 0;
};

#
# ex_rtl_purge_la_files() - purge .la files in tree
# @_dname_base:		base directory pathname
#
# Returns:		zero (0) on success, non-zero (>0) on failure
#
ex_rtl_purge_la_files() {
	local	_erplf_dname_base="${1}"	\
		_erplf_la_path="";

	for _erplf_la_path in $(find		\
			"${_erplf_dname_base}"	\
			-type f			\
			-name \*.la);
	do
		if ! rtl_fileop rm "${_erplf_la_path}"; then
			return 1;
		fi;
	done;
	return 0;
};

#
# ex_rtl_strip_files() - strip files of debugging information
# @_strip_cmd:		strip(1) command name
# @_tree_root:		pathname to tree root
# @--:			(ignored)
# @_log_fn:		logging function name; called with @... and pathname of each file stripped
# @...:			@_fn initial arguments list as positional parameters
#
# Returns:		zero (0) on success, non-zero (>0) on failure
#
ex_rtl_strip_files() {
	local	_ersf_strip_cmd="${1}" _ersf_tree_root="${2}"	\
		_ersf_ignored="${3}" _ersf_log_fn="${4}"	\
		_ersf_bin_path="";
	shift 4;

	if [ -e "${_ersf_tree_root}" ]; then
		for _ersf_bin_path in $(find		\
				"${_ersf_tree_root}"	\
				-perm /a=x		\
				-type f);
		do
			if objdump				\
					-sj .debug_frame	\
					-j .debug_info		\
					"${_ersf_bin_path}"	\
					>/dev/null 2>&1;
			then
				if ! "${_ersf_strip_cmd}" "${_ersf_bin_path}"; then
					return 1;
				else
					"${_ersf_log_fn}" "${@}" "${_ersf_bin_path}";
				fi;
			fi;
		done;
	fi;

	return 0;
};

# vim:filetype=sh textwidth=0