summaryrefslogtreecommitdiffhomepage
path: root/pkgtool.sh
blob: afd7ed0a6fd1831c35610825e5ee4c0caf26994a (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
#!/bin/sh
# Copyright (c) 2020 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.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 "package" $(rtl_get_vars_fast "^PKG_${_pkg_name_uc}");
		rtl_log_msg info "Build group: %s" "${_group_name}";
		if [ -z "${PKG_DEPENDS:-}" ]; then
			rtl_log_msg info "Package \`%s' has no dependencies." "${_pkg_name}";
		else	rtl_log_msg info "Direct dependencies of \`%s': %s" "${_pkg_name}" "${PKG_DEPENDS}";
			if ! ex_pkg_unfold_depends 1 1 "${_group_name}" "${_pkg_names}" "${_pkg_name}" 0; then
				rtl_log_msg warning "Warning: failed to unfold dependency-expanded package name list for \`%s'." "${_pkg_name}";
			else	EX_PKG_NAMES="$(rtl_lfilter "${EX_PKG_NAMES}" "${_pkg_name}")";
				if [ -n "${EX_PKG_NAMES}" ]; then
					rtl_log_msg info "Full dependencies of \`%s': %s"\
							"${_pkg_name}" "$(rtl_lsort "${EX_PKG_NAMES}")";
				fi;
				if [ -n "${EX_PKG_DISABLED}" ]; then
					rtl_log_msg info "Full dependencies of \`%s' (disabled packages:) %s"\
							"${_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_dnames="${1}" _group_name="" _mirror_dname="" _mirror_dname_git=""\
		_pkg_name="" _pkg_parent="" _rc=0;

	rtl_lassign "_mirror_dname _mirror_dname_git" "," "${_mirror_dnames}"; umask 022;
	if ! ex_pkg_load_groups; then
		_rc=1; _status="Error: failed to load build groups.";
	elif ! rtl_fileop mkdir "${_mirror_dname}"; then
		_rc=1; _status="Error: failed to create \`${_mirror_dname}'.";
	elif ! 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}"\
		_pkg_disabled=0 _pkg_fname="" _pkg_sha256sum="" _pkg_url="" _rc=0;

	if _pkg_disabled="$(rtl_get_var_unsafe -u "PKG_${_pkg_name_real}_DISABLED")"\
	&& [ "${_pkg_disabled:-0}" -eq 1 ]; then
		rtl_log_msg info "Package \`%s' (parent package: \`%s') disabled, skipping." "${_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 [ "${_pkg_name}" != "${_pkg_name_real}" ]; then
				rtl_log_msg info "Mirroring package \`%s' (parent package: \`%s'), archive URL(s): \`%s'..." "${_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 "Failed to create symbolic link \`%s' for package \`%s' w/ parent package \`%s'."\
							"${_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 "Mirroring package \`%s', archive URL(s): \`%s'..." "${_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 "Failed to mirror package \`%s', skipping." "${_pkg_name}";
				fi;
			fi;
		elif _pkg_url="$(rtl_get_var_unsafe -u "PKG_${_pkg_name_real}_URLS_GIT")"; then
			if [ "$(rtl_get_var_unsafe -u "PKG_${_pkg_name_real}_MIRRORS_GIT")" = "skip" ]; then
				_rc=0; rtl_log_msg warning "Package \`%s' specifies to skip Git URL(s) mirroring, skipping." "${_pkg_name}";
			elif [ "${_pkg_name}" != "${_pkg_name_real}" ]; then
				rtl_log_msg info "Mirroring package \`%s' (parent package: \`%s'), Git URL(s): \`%s'..." "${_pkg_name}" "${_pkg_name_real}" "${_pkg_url}";
				if ! rtl_fileop ln_symbolic "${_pkg_name_real}" "${_mirror_dname_git}/${_pkg_name}"; then
					_rc=1; rtl_log_msg warning "Failed to create symbolic link \`%s' for package \`%s' w/ parent package \`%s'."\
							"${_mirror_dname_git}/${_pkg_name}" "${_pkg_name}" "${_pkg_name_real}";
				fi;
			else
				rtl_log_msg info "Mirroring package \`%s', Git URL(s): \`%s'..." "${_pkg_name}" "${_pkg_url}";
				if ! rtl_fileop mkdir "${_mirror_dname_git}/${_pkg_name}"\
				|| ! rtl_fetch_mirror_urls_git "${DEFAULT_GIT_ARGS}" "${_mirror_dname_git}/${_pkg_name}" ${_pkg_url}; then
					_rc=1; rtl_log_msg warning "Failed to mirror package \`%s', skipping." "${_pkg_name}";
				fi;
			fi;
		else
			_rc=0; rtl_log_msg warning "Package \`%s' has neither archive nor Git URL(s), skipping." "${_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 "Package \`%s' has no reverse dependencies." "${_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 "Reverse dependencies of \`%s': %s" "${_pkg_name}" "${_pkg_rdepends}";
		fi;
		if [ -n "${EX_PKG_DISABLED}" ]; then
			rtl_log_msg info "Reverse dependencies of \`%s' (disabled packages:) %s"\
					"${_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 "package" $(rtl_get_vars_fast "^PKG_");
		rtl_log_msg info "Launching shell \`%s' within package environment and \`%s'." "${SHELL}" "${PKG_BUILD_DIR}";
		rtl_log_msg info "Run \$R to rebuild \`%s'." "${_pkg_name}";
		rtl_log_msg info "Run \$RS <step> to restart the specified build step of \`%s'" "${_pkg_name}";
		rtl_log_msg info "Run \$D to automatically regenerate the patch for \`%s'." "${_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 "Creating compressed tarball of \`%s' and \`%s_stderrout.log'..."\
				"${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 "Created compressed tarball of \`%s' and \`%s_stderrout.log'."\
					"${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 "Updated \`%s/patches/%s'."\
						"${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_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 "${_status}";
	elif [ -n "${_status}" ]; then
		rtl_log_msg info "${_status}";
	fi;
};

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

# vim:filetype=sh textwidth=0