summaryrefslogtreecommitdiffstats
path: root/lib/deps.sh
blob: fb2550193d4900f356779c89e1ad15b461c4c3bf (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
# Functions for parsing dependency field values
#
# Copyright (C) 2012, 2018 Patrick McDermott
#
# This file is part of opkbuild.
#
# opkbuild is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# opkbuild is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with opkbuild.  If not, see <http://www.gnu.org/licenses/>.

## @brief Parse a single package dependency string
## @details \fBob_parse_dep\fP() parses package dependencies in the format
##          \fIpkg:archqual (rel ver) [arches] <plats>\fP, where the components
##          \fI:archqual\fP, \fI(rel ver)\fP, \fI[arches]\fP, and \fI<plats>\fP
##          are optional.
## @option -P pkg_var      The name of the variable in which to store the
##                         package name.
## @option -q archqual_var The name of the variable in which to store the
##                         architecture qualifier, if any.
## @option -r rel_var      The name of the variable in which to store the
##                         relationship operator, if any.
## @option -v ver_var      The name of the variable in which to store the
##                         version, if any.
## @option -A arches_var   The name of the variable in which to store the
##                         architecture restrictions, if any.
## @option -l plats_var    The name of the variable in which to store the
##                         platform restrictions, if any.
## @option -a host_arch    The host architecture.  If given, the dependency will
##                         only be printed on standard output if it applies to
##                         the given architecture.
## @option -p host_plat    The host platform.  If given, the dependency will
##                         only be printed on standard output if it applies to
##                         the given platform.
## @operand dep req The dependency to parse.
## @return Returns 0.
## @stdout If \fB-a\fP and \fB\p\fP aren't given, the entire dependency string
##         is printed, with normalized formatting.  If \fB-a\fP and/or \fB-p\fP
##         are given and the dependency applies to their arguments, the
##         dependency string is printed, except for the architecture (if
##         \fB-a\fP given) and/or platform (if \fB-p\fP given) restrictions,
##         with normalized formatting.  Normalized formatting is a string of the
##         format \fIpkg:archqual\ (rel\ ver)\ [arches]\ <plats>\fP.
## @pure maybe This function has side effects when used with any of the options
##             \fB-P\fP, \fB-q\fP, \fB-r\fP, \fB-v\fP, \fB-A\fP, or \fB-l\fP.
ob_parse_dep()
{
	local opt=
	local pkg_var=
	local archqual_var=
	local rel_var=
	local ver_var=
	local arches_var=
	local plats_var=
	local host_arch=
	local host_plat=
	local dep=
	local dep_re=
	local pkg=
	local archqual=
	local rel=
	local ver=
	local arches=
	local plats=
	local comp=
	local comp_var=

	OPTIND=1
	while getopts 'P:q:r:v:A:l:a:p:' opt; do
		case "${opt}" in
			P)
				pkg_var="${OPTARG}"
				if ! _ob_validate_var_name "${pkg_var}"; then
					_ob_abort
				fi
				;;
			q)
				archqual_var="${OPTARG}"
				if ! _ob_validate_var_name "${archqual_var}"
				then
					_ob_abort
				fi
				;;
			r)
				rel_var="${OPTARG}"
				if ! _ob_validate_var_name "${rel_var}"; then
					_ob_abort
				fi
				;;
			v)
				ver_var="${OPTARG}"
				if ! _ob_validate_var_name "${ver_var}"; then
					_ob_abort
				fi
				;;
			A)
				arches_var="${OPTARG}"
				if ! _ob_validate_var_name "${arches_var}"; then
					_ob_abort
				fi
				;;
			l)
				plats_var="${OPTARG}"
				if ! _ob_validate_var_name "${plats_var}"; then
					_ob_abort
				fi
				;;
			a)
				host_arch="${OPTARG}"
				;;
			p)
				host_plat="${OPTARG}"
				;;
			?)
				_ob_abort
				;;
		esac
	done
	shift $(($OPTIND - 1))

	dep="${1}"
	shift 1 || _ob_abort

	# pkg[:archqual] [(rel ver)] [\[arches\]] [<plats>]
	dep_re='s/^ *([^ \(\[<]+) *(\((<<|<=|=|>=|>>) *(.+)\))?'
	dep_re="${dep_re}"' *(\[(.+)\])? *(<(.+)>)? *$/\1\n\3\n\4\n\6\n\8/;'

	# BusyBox sed supports -r and (as of version 1.22.0) -E.
	dep="$(printf '%s\n' "${dep}" | sed -nr '
		H;
		${
			g;
			s/[\t\n]/ /g;
			'"${dep_re}"'
			p;
		};
		')"

	{
		IFS=':' read pkg archqual
		read rel || :
		read ver || :
		read arches || :
		read plats || :
	} <<-EOF
		${dep}
		EOF

	# Set the specified variables.
	for comp in pkg archqual rel ver arches plats; do
		comp_var="$(eval "printf '%s' \"\${${comp}_var}\"")"
		[ -z "${comp_var}" ] && continue
		eval "${comp_var}=\"\${${comp}}\""
	done

	if [ -n "${host_arch}" ] && ! ob_arch_is_concerned \
			"${host_arch}" "${arches}"; then
		return 0
	fi
	if [ -n "${host_plat}" ] && ! ob_plat_is_concerned \
			"${host_plat}" "${plats}"; then
		return 0
	fi

	printf '%s' "${pkg}"
	[ -n "${archqual}" ] && printf ':%s' "${archqual}"
	[ -n "${ver}" ] && printf ' (%s %s)' "${rel}" "${ver}"
	[ -z "${host_arch}" ] && [ -n "${arches}" ] && \
		printf ' [%s]' "${arches}"
	[ -z "${host_plat}" ] && [ -n "${plats}" ] && \
		printf ' [%s]' "${plats}"
	printf '\n'

	return 0
}

## @brief Reduce dependencies to a given architecture and platform
## @details \fBob_reduce_deps\fP() reduces a list of dependencies to only those
##          that apply to a given architecture and platform.  A list is either a
##          "normal" list or a "union" list.  A normal list is an "AND" list
##          composed of "OR" lists, which in turn are composed of simple
##          dependencies.  A union list is an AND list composed of only simple
##          dependencies.  That is, a normal list is of the form
##          \fIdep\ |\ dep,\ dep\fP, and a union list is of the form
##          \fIdep,\ dep\fP.  The "Conflicts", "Provides", and "Replaces"
##          package relationships are union lists.
## @option -a host_arch The architecture to which dependencies should be
##                      reduced.  Required.
## @option -p host_plat The platform to which dependencies should be reduced. 
##                      Required.
## @option -u -         Treat \fIdeps\fP as a union list.
## @operand deps req The list of dependencies to reduce.
## @return Returns 0 on success.
## @stdout Prints the reduced list of dependencies.
## @pure yes This function has no side effects.
ob_reduce_deps()
{
	local opt=
	local host_arch=
	local host_plat=
	local deps=
	local dep_and=
	local dep_or=
	local dep_list=
	local dep_or_list=
	local dep=

	union='false'

	OPTIND=1
	while getopts 'a:p:u' opt; do
		case "${opt}" in
			a)
				host_arch="${OPTARG}"
				;;
			p)
				host_plat="${OPTARG}"
				;;
			u)
				union='true'
				;;
			?)
				_ob_abort
				;;
		esac
	done
	shift $(($OPTIND - 1))

	local deps="${1}"
	shift 1 || _ob_abort

	IFS=','
	for dep_and in ${deps}; do
		unset IFS
		if ! ${union}; then
			IFS='|'
			dep_or_list=
			for dep_or in ${dep_and}; do
				unset IFS
				dep="$(ob_parse_dep -a "${host_arch}" \
					-p "${host_plat}" "${dep_or}")"
				if [ -n "${dep}" ]; then
					if [ -n "${dep_or_list}" ]; then
						dep_or_list="${dep_or_list} | "
					fi
					dep_or_list="${dep_or_list}${dep}"
				fi
			done
			unset IFS
			if [ -n "${dep_or_list}" ]; then
				if [ -n "${dep_list}" ]; then
					dep_list="${dep_list}, "
				fi
				dep_list="${dep_list}${dep_or_list}"
			fi
		else
			dep="$(ob_parse_dep -a "${host_arch}" \
				-p "${host_plat}" "${dep_and}")"
			if [ -n "${dep}" ]; then
				if [ -n "${dep_list}" ]; then
					dep_list="${dep_list}, "
				fi
				dep_list="${dep_list}${dep}"
			fi
		fi
	done
	unset IFS

	printf '%s' "${dep_list}"

	return 0
}