summaryrefslogtreecommitdiffhomepage
path: root/pkgtool.sh
blob: af911ec5a5263db3f5b0d2d9efaabf0a5d83230c (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
#!/bin/sh
# Copyright (c) 2020, 2021 LucĂ­a Andrea Illanes Albornoz <lucia@luciaillanes.de>
#

pkgtoolp_info() {
	local	_pkg_name="${1}" _group_name="" _pkg_name_uc="$(rtl_toupper "${1}")" _fname="" _pkg_names=""\
		_rc=0 EX_PKG_BUILD_GROUPS="" EX_PKG_DISABLED="" EX_PKG_FINISHED="" EX_PKG_NAMES=""; _status="";

	if ! ex_pkg_load_groups; then
		_rc=1; _status="Error: failed to load build groups.";
	elif ! _group_name="$(ex_pkg_find_package "${EX_PKG_BUILD_GROUPS}" "${_pkg_name}")"; then
		_rc=1; _status="Error: unknown package \`${_pkg_name}'.";
	elif ! _pkg_names="$(ex_pkg_get_packages "${_group_name}")"; then
		_rc=1; _status="Error: failed to expand package list of build group \`${_group_name}'.";
	elif ! ex_pkg_env "${DEFAULT_BUILD_STEPS}" "${DEFAULT_BUILD_VARS}"\
			"${_group_name}" 1 "${_pkg_name}" "" "${BUILD_WORKDIR}"; then
		_rc=1; _status="Error: failed to set package environment for \`${_pkg_name}'.";
	else	_pkg_version="$(rtl_get_var_unsafe -u "PKG_"${_pkg_name}"_VERSION")";
		rtl_log_env_vars "verbose" "package" $(rtl_get_vars_fast "^PKG_${_pkg_name_uc}");
		rtl_log_msg "info" "${MSG_pkgtool_build_group}" "${_group_name}";
		if [ -z "${PKG_DEPENDS:-}" ]; then
			rtl_log_msg "info" "${MSG_pkgtool_pkg_no_deps}" "${_pkg_name}";
		else	rtl_log_msg "info" "${MSG_pkgtool_pkg_direct_deps}" "${_pkg_name}" "${PKG_DEPENDS}";
			if ! ex_pkg_unfold_depends 1 1 "${_group_name}" "${_pkg_names}" "${_pkg_name}" 0; then
				rtl_log_msg "warning" "${MSG_pkgtool_pkg_deps_fail}" "${_pkg_name}";
			else	EX_PKG_NAMES="$(rtl_lfilter "${EX_PKG_NAMES}" "${_pkg_name}")";
				if [ -n "${EX_PKG_NAMES}" ]; then
					rtl_log_msg "info" "${MSG_pkgtool_pkg_deps_full}"\
							"${_pkg_name}" "$(rtl_lsort "${EX_PKG_NAMES}")";
				fi;
				if [ -n "${EX_PKG_DISABLED}" ]; then
					rtl_log_msg "info" "${MSG_pkgtool_pkg_deps_full_disabled}"\
							"${_pkg_name}" "$(rtl_lsort "${EX_PKG_DISABLED}")";
				fi;
			fi;
		fi;
		set +o noglob;
		for _fname in	\
				"vars/${_pkg_name}.vars"								\
				"patches/${_pkg_name}/"*.patch								\
				"patches/${_pkg_name}${_pkg_version:+-${_pkg_version}}.local.patch"			\
				"patches/${_pkg_name}${_pkg_version:+-${_pkg_version}}.local@${BUILD_HNAME}.patch"	\
				"patches/${_pkg_name}${_pkg_version:+-${_pkg_version}}_pre.local.patch"			\
				"patches/${_pkg_name}${_pkg_version:+-${_pkg_version}}_pre.local@${BUILD_HNAME}.patch"	\
				"${BUILD_WORKDIR}/chainport/patches/${_pkg_name%%_*}/${_pkg_name%%_*}-${_pkg_version}.midipix.patch"; do
			if [ -e "${_fname}" ]; then
				sha256sum "${_fname}";
			fi;
		done;
		set -o noglob;
	fi; return "${_rc}";
};

pkgtoolp_mirror() {
	local	_mirror_dname="${1}" _mirror_dname_git="${2}" _group_name="" _pkg_name="" _pkg_parent="" _rc=0;

	umask 022;
	_mirror_dname="$(rtl_subst "${_mirror_dname}" "~" "${HOME}")";
	_mirror_dname_git="$(rtl_subst "${_mirror_dname_git}" "~" "${HOME}")";
	if ! ex_pkg_load_groups; then
		_rc=1; _status="Error: failed to load build groups.";
	elif [ -n "${_mirror_dname}" ]\
	&& ! rtl_fileop mkdir "${_mirror_dname}"; then
		_rc=1; _status="Error: failed to create \`${_mirror_dname}'.";
	elif [ -n "${_mirror_dname_git}" ]\
	&& ! rtl_fileop mkdir "${_mirror_dname_git}"; then
		_rc=1; _status="Error: failed to create \`${_mirror_dname_git}'.";
	else	for _group_name in ${EX_PKG_BUILD_GROUPS}; do
			for _pkg_name in $(ex_pkg_get_packages "${_group_name}"); do
				_pkg_parent="$(rtl_get_var_unsafe -u "PKG_${_pkg_name}_INHERIT_FROM")";
				if ! pkgtoolp_mirror_fetch						\
						"${_mirror_dname}" "${_mirror_dname_git}" "${_pkg_name}"\
						"${_pkg_parent:-${_pkg_name}}"; then
					_rc=1; _status="Warning: failed to mirror one or more packages.";
				fi;
			done;
		done;
	fi; return "${_rc}";
};

pkgtoolp_mirror_fetch() {
	local	_mirror_dname="${1}" _mirror_dname_git="${2}" _pkg_name="${3}" _pkg_name_real="${4}"\
		_fname="" _pkg_disabled=0 _pkg_fname="" _pkg_sha256sum="" _pkg_url="" _pkg_urls_git=""\
		_rc=0;

	if _pkg_disabled="$(rtl_get_var_unsafe -u "PKG_${_pkg_name_real}_DISABLED")"\
	&& [ "${_pkg_disabled:-0}" -eq 1 ]; then
		rtl_log_msg "verbose" "${MSG_pkgtool_pkg_disabled}" "${_pkg_name}" "${_pkg_name_real}";
	else	if _pkg_url="$(rtl_get_var_unsafe -u "PKG_${_pkg_name_real}_URL")"\
		&& _pkg_sha256sum="$(rtl_get_var_unsafe -u "PKG_${_pkg_name_real}_SHA256SUM")"; then
			if [ -z "${_mirror_dname}" ]; then
				_rc=0; rtl_log_msg "verbose" "${MSG_pkgtool_pkg_skip_archive_mirror}" "${_pkg_name}";
			elif [ "${_pkg_name}" != "${_pkg_name_real}" ]; then
				rtl_log_msg "info" "${MSG_pkgtool_pkg_archive_mirroring_parent}" "${_pkg_name}" "${_pkg_name_real}" "${_pkg_url}";
				if ! rtl_fileop ln_symbolic "${_pkg_name_real}" "${_mirror_dname}/${_pkg_name}"; then
					_rc=1; rtl_log_msg "warning" "${MSG_pkgtool_pkg_link_fail}"\
							"${_mirror_dname}/${_pkg_name}" "${_pkg_name}" "${_pkg_name_real}";
				fi;
			else
				if ! _pkg_fname="$(rtl_get_var_unsafe -u "PKG_${_pkg_name_real}_FNAME")"; then
					_pkg_fname="${_pkg_url##*/}";
				fi;
				rtl_log_msg "info" "${MSG_pkgtool_pkg_archive_mirroring}" "${_pkg_name}" "${_pkg_url}";
				if ! rtl_fileop mkdir "${_mirror_dname}/${_pkg_name}"\
				|| ! rtl_fetch_url_wget "${_pkg_url}" "${_pkg_sha256sum}" "${_mirror_dname}/${_pkg_name}" "${_pkg_fname}" "${_pkg_name_real}" ""; then
					_rc=1; rtl_log_msg "warning" "${MSG_pkgtool_pkg_mirror_fail}" "${_pkg_name}";
				else
					pkg_fetch_download_clean_dlcache "${_mirror_dname}" "${_pkg_name}" "${_pkg_fname}" "${_pkg_urls_git}";
				fi;
			fi;
		fi;
		if _pkg_urls_git="$(rtl_get_var_unsafe -u "PKG_${_pkg_name_real}_URLS_GIT")"; then
			if [ -z "${_mirror_dname_git}" ]; then
				_rc=0; rtl_log_msg "verbose" "${MSG_pkgtool_pkg_skip_git_mirror}" "${_pkg_name}";
			elif [ "$(rtl_get_var_unsafe -u "PKG_${_pkg_name_real}_MIRRORS_GIT")" = "skip" ]; then
				_rc=0; rtl_log_msg "verbose" "${MSG_pkgtool_pkg_skip_git_mirror_disabled}" "${_pkg_name}";
			elif [ "${_pkg_name}" != "${_pkg_name_real}" ]; then
				rtl_log_msg "info" "${MSG_pkgtool_pkg_git_mirroring_parent}" "${_pkg_name}" "${_pkg_name_real}" "${_pkg_urls_git}";
				if ! rtl_fileop ln_symbolic "${_pkg_name_real}" "${_mirror_dname_git}/${_pkg_name}"; then
					_rc=1; rtl_log_msg "warning" "${MSG_pkgtool_pkg_link_fail}"\
							"${_mirror_dname_git}/${_pkg_name}" "${_pkg_name}" "${_pkg_name_real}";
				fi;
			else
				rtl_log_msg "info" "${MSG_pkgtool_pkg_git_mirroring}" "${_pkg_name}" "${_pkg_urls_git}";
				if ! rtl_fileop mkdir "${_mirror_dname_git}/${_pkg_name}"\
				|| ! rtl_fetch_mirror_urls_git "${DEFAULT_GIT_ARGS}" "${_mirror_dname_git}/${_pkg_name}" ${_pkg_urls_git}; then
					_rc=1; rtl_log_msg "warning" "${MSG_pkgtool_pkg_mirror_fail}" "${_pkg_name}";
				else
					pkg_fetch_download_clean_dlcache "${_mirror_dname_git}" "${_pkg_name}" "${_pkg_fname}" "${_pkg_urls_git}";
				fi;
			fi;
		fi;
		if [ -z "${_pkg_url}" ]\
		&& [ -z "${_pkg_sha256sum}" ]\
		&& [ -z "${_pkg_urls_git}" ]; then
			_rc=0; rtl_log_msg "verbose" "${MSG_pkgtool_pkg_skip_no_urls}" "${_pkg_name}";
		fi;
	fi; return "${_rc}";
};

pkgtoolp_restart_at() {
	local _pkg_name="${1}" _rc=0; _status="";

	if ! ex_pkg_load_dump "${_pkg_name}" "${BUILD_WORKDIR}"; then
		_rc=1; _status="${_status}";
	else	case "${ARG_RESTART_AT}" in
		ALL)	if ! "${MIDIPIX_BUILD_PWD}/build.sh" -P -r "${_pkg_name}" -v; then
				_rc=1; _status="Error: failed to run command line ${MIDIPIX_BUILD_PWD}/build.sh -P -r ${_pkg_name} -v";
			fi; ;;
		*)	if ! "${MIDIPIX_BUILD_PWD}/build.sh" -P -r "${_pkg_name}:${ARG_RESTART_AT}" -v; then
				_rc=1; _status="Error: failed to run command line ${MIDIPIX_BUILD_PWD}/build.sh -P -r ${_pkg_name}:${ARG_RESTART_AT} -v";
			fi; ;;
		esac;
	fi; return "${_rc}";
};

pkgtoolp_rdepends() {
	local	_pkg_name="${1}" _group_name="" _pkg_depends="" _pkg_name_rdepend="" _pkg_names=""\
		_pkg_rdepends="" _rc=0 EX_PKG_BUILD_GROUPS="" EX_PKG_DISABLED="" EX_PKG_RDEPENDS_DIRECT=""; _status="";

	if ! ex_pkg_load_groups; then
		_rc=1; _status="Error: failed to load build groups.";
	elif ! _group_name="$(ex_pkg_find_package "${EX_PKG_BUILD_GROUPS}" "${_pkg_name}")"; then
		_rc=1; _status="Error: unknown package \`${_pkg_name}'.";
	elif ! _pkg_names="$(ex_pkg_get_packages "${_group_name}")"; then
		_rc=1; _status="Error: failed to expand package list of build group \`${_group_name}'.";
	elif ! ex_pkg_unfold_rdepends_direct "${_group_name}" "${_pkg_names}" "${_pkg_name}"; then
		_rc=1; _status="Error: failed to unfold reverse dependency-expanded package name list for \`${_pkg_name}'.";
	elif [ -z "${EX_PKG_DISABLED}" ] && [ -z "${EX_PKG_RDEPENDS_DIRECT}" ]; then
		rtl_log_msg "info" "${MSG_pkgtool_pkg_deps_rev_none}" "${_pkg_name}";
	else	for _pkg_name_rdepend in $(rtl_lsort "${EX_PKG_RDEPENDS_DIRECT}"); do
			_pkg_rdepends="$(rtl_lconcat "${_pkg_rdepends}" "${_pkg_name_rdepend}")";
			if _pkg_depends="$(rtl_lunfold_depends 'PKG_${_name}_DEPENDS' $(rtl_get_var_unsafe -u "PKG_"${_pkg_name}"_DEPENDS"))"\
			&& [ -n "${_pkg_depends}" ]; then
				_pkg_rdepends="$(rtl_lconcat "${_pkg_rdepends}" "${_pkg_depends}")";
			fi;
		done;
		if [ -n "${_pkg_rdepends}" ]; then
			rtl_log_msg "info" "${MSG_pkgtool_pkgs_deps_rev}" "${_pkg_name}" "${_pkg_rdepends}";
		fi;
		if [ -n "${EX_PKG_DISABLED}" ]; then
			rtl_log_msg "info" "${MSG_pkgtool_pkgs_deps_rev_disabled}" "${_pkg_name}" "$(rtl_lsort "${EX_PKG_DISABLED}")";
		fi;
	fi; return "${_rc}";
};

pkgtoolp_shell() {
	local _pkg_name="${1}" _rc=0; _status="";

	if ! ex_pkg_load_dump "${_pkg_name}" "${BUILD_WORKDIR}"; then
		_rc=1; _status="${_status}";
	else	rtl_log_env_vars "verbose" "package" $(rtl_get_vars_fast "^PKG_");
		rtl_log_msg "info" "${MSG_pkgtool_shell_env1}" "${SHELL}" "${PKG_BUILD_DIR}";
		rtl_log_msg "info" "${MSG_pkgtool_shell_env2}" "${_pkg_name}";
		rtl_log_msg "info" "${MSG_pkgtool_shell_env3}" "${_pkg_name}";
		rtl_log_msg "info" "${MSG_pkgtool_shell_env4}" "${_pkg_name}";
		export	ARCH BUILD_KIND						\
			BUILD_DLCACHEDIR BUILD_WORKDIR				\
			MAKE="make LIBTOOL=${PKG_LIBTOOL:-slibtool}"		\
			MIDIPIX_BUILD_PWD					\
			PKG_NAME						\
			PREFIX PREFIX_CROSS PREFIX_MINGW32 PREFIX_MINIPIX	\
			PREFIX_NATIVE PREFIX_ROOT PREFIX_RPM;
		D="${MIDIPIX_BUILD_PWD}/${0##*/} --update-diff"			\
		R="${MIDIPIX_BUILD_PWD}/${0##*/} --restart-at ALL"		\
		RS="${MIDIPIX_BUILD_PWD}/${0##*/} --restart-at "		\
		"${SHELL}";
	fi; return "${_rc}";
};

pkgtoolp_tarball() {
	local	_pkg_name="${1}" _date="" _group_name="" _hname="" _pkg_name_full=""\
		_pkg_version="" _rc=0 _tarball_fname="" EX_PKG_BUILD_GROUPS=""; _status="";

	if ! ex_pkg_load_groups; then
		_rc=1; _status="Error: failed to load build groups.";
	elif ! _group_name="$(ex_pkg_find_package "${EX_PKG_BUILD_GROUPS}" "${_pkg_name}")"; then
		_rc=1; _status="Error: unknown package \`${_pkg_name}'.";
	elif ! ex_pkg_env "${DEFAULT_BUILD_STEPS}" "${DEFAULT_BUILD_VARS}"	\
			"${_group_name}" 0 "${_pkg_name}" "" "${BUILD_WORKDIR}"; then
		_rc=1; _status="Error: failed to set package environment for \`${_pkg_name}'.";
	elif ! _date="$(date +%Y%m%d_%H%M%S)"; then
		_rc=1; _status="Error: failed to call date(1).";
	elif ! _hname="$(hostname -f)"; then
		_rc=1; _status="Error: failed to call hostname(1).";
	else	if [ -n "${PKG_VERSION}" ]; then
			_pkg_name_full="${_pkg_name}-${PKG_VERSION}";
		else
			_pkg_name_full="${_pkg_name}";
		fi;
		_tarball_fname="${_pkg_name_full}@${_hname}-${_date}.tbz2";
		rtl_log_msg "info" "${MSG_pkgtool_tarball_creating}" "${PKG_BASE_DIR}" "${_pkg_name}";
		if ! tar -C "${BUILD_WORKDIR}" -cpf -				\
				"${PKG_BASE_DIR#${BUILD_WORKDIR%/}/}"		\
				"${_pkg_name}_stderrout.log"			|\
					bzip2 -c -9 - > "${_tarball_fname}"; then
			_rc=1; _status="Error: failed to create compressed tarball of \`${PKG_BASE_DIR}' and \`${_pkg_name}_stderrout.log'.";
		else
			rtl_log_msg "info" "${MSG_pkgtool_tarball_created}" "${PKG_BASE_DIR}" "${_pkg_name}";
		fi;
	fi; return "${_rc}";
};

pkgtoolp_update_diff() {
	local	_pkg_name="${1}" _diff_fname_dst="" _diff_fname_src="" _fname=""\
		_fname_base="" _rc=0; _status="";

	if ! ex_pkg_load_dump "${_pkg_name}" "${BUILD_WORKDIR}"; then
		_rc=1; _status="${_status}";
	else	if [ -n "${PKG_VERSION}" ]; then
			_diff_fname_dst="${_pkg_name}-${PKG_VERSION}.local.patch";
		else
			_diff_fname_dst="${_pkg_name}.local.patch";
		fi;
		if ! _diff_fname_src="$(mktemp)"; then
			_rc=1; _status="Error: failed to create temporary target diff(1) file.";
		else	trap "rm -f \"${_diff_fname_src}\" >/dev/null 2>&1" EXIT HUP INT TERM USR1 USR2;
			(cd "${PKG_BASE_DIR}" && printf "" > "${_diff_fname_src}";
			 for _fname in $(find "${PKG_SUBDIR}" -iname \*.orig); do
				_fname_base="${_fname##*/}"; _fname_base="${_fname_base%.orig}";
				case "${_fname_base}" in
				config.sub)
					continue; ;;
				*)	diff -u "${_fname}" "${_fname%.orig}" >> "${_diff_fname_src}"; ;;
				esac;
			done);
			if [ "${?}" -ne 0 ]; then
				_rc=1; _status="Error: failed to create diff(1).";
			elif ! rtl_fileop mv "${_diff_fname_src}" "${MIDIPIX_BUILD_PWD}/patches/${_diff_fname_dst}"; then
				_rc=1; _status="Error: failed to rename diff(1) to \`${MIDIPIX_BUILD_PWD}/patches/${_diff_fname_dst}'.";
			else	trap - EXIT HUP INT TERM USR1 USR2;
				rtl_log_msg "info" "${MSG_pkgtool_updated_patches}" "${MIDIPIX_BUILD_PWD}" "${_diff_fname_dst}";
			fi;
		fi;
	fi; return "${_rc}";
};

pkgtool() {
	local _rc=0 _status="" BUILD_GROUPS="" ARCH BUILD_KIND BUILD_WORKDIR PKGTOOL_PKGNAME PREFIX;

	if ! . "${0%/*}/subr/pkgtool_init.subr"; then
		_rc=1; printf "Error: failed to source \`${0%/*}/subr/pkgtool_init.subr'." >&2;
	elif ! pkgtool_init "${@}"; then
		_rc=1; _status="${_status}";
	else	case "1" in
		"${ARG_INFO:-0}")		pkgtoolp_info "${PKGTOOL_PKG_NAME}"; ;;
		"${ARG_MIRROR:-0}")		pkgtoolp_mirror "${ARG_MIRROR_DNAME}" "${ARG_MIRROR_DNAME_GIT}"; ;;
		"${ARG_RDEPENDS:-0}")		pkgtoolp_rdepends "${PKGTOOL_PKG_NAME}"; ;;
		"${ARG_RESTART_AT:+1}")		pkgtoolp_restart_at "${PKGTOOL_PKG_NAME}"; ;;
		"${ARG_SHELL:-0}")		pkgtoolp_shell "${PKGTOOL_PKG_NAME}"; ;;
		"${ARG_TARBALL:-0}")		pkgtoolp_tarball "${PKGTOOL_PKG_NAME}"; ;;
		"${ARG_UPDATE_DIFF:-0}")	pkgtoolp_update_diff "${PKGTOOL_PKG_NAME}"; ;;
		esac; _rc="${?}";
	fi;
	if [ "${_rc}" -ne 0 ]; then
		rtl_log_msg "fatalexit" "0;${_status}";
	elif [ -n "${_status}" ]; then
		rtl_log_msg "info" "0;${_status}";
	fi;
};

set +o errexit -o noglob -o nounset; pkgtool "${@}";

# vim:filetype=sh textwidth=0