summaryrefslogtreecommitdiffstats
path: root/src/deps.sh
blob: 9a30fe95517b42dcbbe91f7590d97a4fb34f124f (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
# Functions for parsing dependency field values
#
# Copyright (C) 2012-2014  Patrick "P. J." McDermott
#
# This file is part of the ProteanOS Development Kit.
#
# The ProteanOS Development Kit 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.
#
# The ProteanOS Development Kit 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 the ProteanOS Development Kit.  If not, see
# <http://www.gnu.org/licenses/>.

resolve_deps()
{
	local new_pkgs="${1}"
	local deps="${2}"
	local all_deps=
	local new_deps=
	local pkg=

	all_deps=''
	new_pkgs="${new_pkgs} "
	while [ "x${new_pkgs}" != 'x' ]; do
		all_deps="${all_deps}${new_pkgs}"
		new_deps=''
		for pkg in ${new_pkgs}; do
			case "${pkg}" in
				*[!a-z0-9+.-]*) continue;;
			esac
			new_deps="${new_deps} $(printf '%s' "${deps}" | \
				sed -n "s/^${pkg}: *//p")"
		done
		new_deps="$(printf '%s\n' ${new_deps} | sort -u)"
		new_pkgs=''
		for pkg in ${new_deps}; do
			if [ "x${all_deps# ${pkg} }" = "x${all_deps}" ]; then
				new_pkgs="${new_pkgs}${pkg} "
			fi
		done
	done

	printf '%s\n' "${all_deps% }"
}

parse_dep()
{
	local dep="${1}"
	local host_arch="${2}"
	local host_plat="${3}"
	local pkgarchqual=
	local pkg=
	local archqual=
	local relver=
	local rel=
	local ver=
	local arches=
	local plats=

	dep="$(printf '%s\n' "${dep}" | sed -n '
		H;                        # Store each input line in hold space.
		${                        # On the last line:
		  g;                      # Restore everything to pattern space.
		  s/[\t\n]/ /g;           # Replace tabs & newlines with spaces.
		  s/ *\([(\[<]\) */ \1/g; # One space before "(", "[", and "<".
		  s/ *\([)\]>]\) */\1 /g; # One space after "(", "[", and "<".
		  s/ *\(<[<=]\) */\1 /g;  # One space after "<<" and "<=".
		  s/ *\(>[>=]\) */\1 /g;  # One space after ">>" and ">=".
		  s/ *\(=\) */\1 /g;      # One space after "=".
		  s/^ *//;                # Remove leading spaces.
		  s/ *$//;                # Remove trailing spaces.
		  s/  */ /g;              # Remove duplicate spaces.
		  p;                      # Print the pattern space.
		};                        # End.
		')"

	# dep is now of the form:
	# pkg[:archqual] [(rel ver)] [\[arches\]] [<plats>]

	pkgarchqual="${dep%% *}"
	dep=" ${dep#* }"
	pkg="${pkgarchqual%:*}"
	if [ "x${pkg}" != "x${pkgarchqual}" ]; then
		archqual="${pkgarchqual##*:}"
	fi

	if [ "x${dep# (*)}" != "x${dep}" ]; then
		relver="${dep# (}"
		relver="${relver%%)*}"
		dep="${dep# (*)}"
		rel="${relver% *}"
		ver="${relver##* }"
	fi

	if [ "x${dep# \[*\]}" != "x${dep}" ]; then
		arches="${dep# \[}"
		arches="${arches%%\]*}"
		dep="${dep# \[*\]}"
	fi

	if [ "x${dep# <*>}" != "x${dep}" ]; then
		plats="${dep# <}"
		plats="${plats%%>*}"
		dep="${dep# <*>}"
	fi

	if [ "x${host_arch}" != 'x' ] && ! arch_is_concerned \
			"${host_arch}" "${arches}"; then
		return 0
	fi
	if [ "x${host_plat}" != 'x' ] && ! plat_is_concerned \
			"${host_plat}" "${plats}"; then
		return 0
	fi

	printf '%s' "${pkg}"
	[ "x${archqual}" != 'x' ] && printf ':%s' "${archqual}"
	[ "x${ver}" != 'x' ] && printf ' (%s %s)' "${rel}" "${ver}"
	[ "x${host_arch}" = 'x' ] && [ "x${arches}" != 'x' ] && \
		printf ' [%s]' "${arches}"
	[ "x${host_plat}" = 'x' ] && [ "x${plats}" != 'x' ] && \
		printf ' <%s>' "${plats}"
	printf '\n'

	return 0
}

reduce_deps()
{
	local deps="${1}"
	local union="${2}"
	local host_arch="${3}"
	local host_plat="${4}"
	local dep_and=
	local dep_or=
	local dep_list=
	local dep_or_list=
	local dep=

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

	printf '%s\n' "${dep_list}"

	return 0
}