aboutsummaryrefslogtreecommitdiff
path: root/Mk/Scripts/do-depends.sh
blob: 238a8f221b186797619badd7276e6cf0d612b77e (plain) (blame)
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
#!/bin/sh
#
# MAINTAINER: portmgr@FreeBSD.org

set -e
set -o pipefail

. ${dp_SCRIPTSDIR}/functions.sh

validate_env dp_RAWDEPENDS dp_DEPTYPE dp_DEPENDS_TARGET dp_DEPENDS_PRECLEAN \
	dp_DEPENDS_CLEAN dp_DEPENDS_ARGS dp_USE_PACKAGE_DEPENDS \
	dp_USE_PACKAGE_DEPENDS_ONLY dp_PKG_ADD dp_PKG_INFO dp_WRKDIR \
	dp_PKGNAME dp_STRICT_DEPENDS dp_LOCALBASE dp_LIB_DIRS dp_SH \
	dp_SCRIPTSDIR PORTSDIR dp_MAKE dp_MAKEFLAGS dp_OVERLAYS

[ -n "${DEBUG_MK_SCRIPTS}" -o -n "${DEBUG_MK_SCRIPTS_DO_DEPENDS}" ] && set -x

set -u

install_depends()
{
	origin=$1
	target=$2
	subpkg=$3
	depends_args=$4
	if [ -z "${dp_USE_PACKAGE_DEPENDS}" -a -z "${dp_USE_PACKAGE_DEPENDS_ONLY}" ]; then
		MAKEFLAGS="${dp_MAKEFLAGS}" ${dp_MAKE} -C ${origin} -DINSTALLS_DEPENDS ${target} ${depends_args}
		return 0
	fi

	if [ -z "${subpkg}" ]; then
		port_var_fetch "${origin}" "${depends_args}" \
			PKGFILE pkgfile \
			PKGBASE pkgbase
	else
		port_var_fetch "${origin}" "${depends_args}" \
			PKGFILE.${subpkg} pkgfile \
			PKGBASE.${subpkg} pkgbase
	fi

	if [ -r "${pkgfile}" -a "${target}" = "${dp_DEPENDS_TARGET}" ]; then
		echo "===>   Installing existing package ${pkgfile}"
		if [ "${pkgbase}" = "pkg" ]; then
			[ -d ${dp_WRKDIR} ] || mkdir -p ${dp_WRKDIR}
			tar xf ${pkgfile} -C ${dp_WRKDIR} -s ",/.*/,,g" "*/pkg-static"
			${dp_WRKDIR}/pkg-static add ${pkgfile}
			rm -f ${dp_WRKDIR}/pkg-static
		else
			${dp_PKG_ADD} -A ${pkgfile}
		fi
	elif [ -n "${dp_USE_PACKAGE_DEPENDS_ONLY}" -a "${target}" = "${dp_DEPENDS_TARGET}" ]; then
		echo "===>   ${dp_PKGNAME} depends on package: ${pkgfile} - not found" >&2
		echo "===>   USE_PACKAGE_DEPENDS_ONLY set - not building missing dependency from source" >&2
		exit 1
	else
		MAKEFLAGS="${dp_MAKEFLAGS}" ${dp_MAKE} -C ${origin} -DINSTALLS_DEPENDS ${target} ${depends_args}
	fi
}

find_package()
{
	if ${dp_PKG_INFO} "$1" >/dev/null 2>&1; then
		echo "===>   ${dp_PKGNAME} depends on package: $1 - found"
		return 0
	fi
	echo "===>   ${dp_PKGNAME} depends on package: $1 - not found"
	return 1
}

find_file()
{
	if [ -e "$1" ]; then
		echo "===>   ${dp_PKGNAME} depends on file: $1 - found"
		return 0
	fi
	echo "===>   ${dp_PKGNAME} depends on file: $1 - not found"
	return 1
}

find_file_path()
{
	if which -s $1 ; then
		echo "===>   ${dp_PKGNAME} depends on executable: $1 - found"
		return 0
	fi
	echo "===>   ${dp_PKGNAME} depends on executable: $1 - not found"
	return 1
}

find_lib()
{
	echo -n "===>   ${dp_PKGNAME} depends on shared library: $1"
	libfile=$(env -i PATH="${PATH}" LIB_DIRS="${dp_LIB_DIRS}" LOCALBASE="${dp_LOCALBASE}" ${dp_SH} ${dp_SCRIPTSDIR}/find-lib.sh $1)
	if [ -z "${libfile}" ]; then
		echo " - not found"
		return 1
	fi
	echo " - found (${libfile})"
}

anynotfound=0
err=0
for _line in ${dp_RAWDEPENDS} ; do
	# ensure we never leak flavors
	unset FLAVOR
	myifs=${IFS}
	IFS=:
	set -- ${_line}
	IFS=${myifs}
	if [ $# -lt 2 -o $# -gt 3 ]; then
		echo "Error: bad dependency syntax in ${dp_DEPTYPE}" >&2
		echo "expecting: pattern:origin[@flavour][:target]" >&2
		echo "got: ${_line}" >&2
		err=1
		continue
	fi
	pattern=$1
	origin=$2
	last=${3:-}

	if [ -z "${pattern}" ]; then
		echo "Error: there is an empty port dependency in ${dp_DEPTYPE}" >&2
		err=1
		continue
	fi

	if [ -z "${origin}" ]; then
		echo "Error: a dependency has an empty origin in ${dp_DEPTYPE}" >&2
		err=1
		continue
	fi

	subpkg=
	case "${origin}" in
	*@*/*) ;; # Ignore @ in the path which would not be a flavor
	*@*)
		export FLAVOR="${origin##*@}"
		origin=${origin%@*}
		;;
	*~*/*) ;; # Ignore ~ in the path which would not be a subpackage
	*~*)
		subpkg="${origin##*~}"
		origin=${origin%~*}
		;;
	esac

	case "${origin}" in
	/*) ;;
	*)
		for overlay in ${dp_OVERLAYS} ${PORTSDIR}; do
			orig="${overlay}/${origin}"
			if [ -f "${orig}/Makefile" ]; then
				break
			fi
		done
		origin="${orig}"
		;;
	esac

	depends_args="${dp_DEPENDS_ARGS}"
	target=${dp_DEPENDS_TARGET}
	if [ -n "${last}" ]; then
		# In case we depend on the fetch stage, actually run checksum,
		# this prevent a MITM attack.
		if [ "${last}" = "fetch" ]; then
			target=checksum
		else
			target=${last}
		fi
		if [ -n "${dp_DEPENDS_PRECLEAN}" ]; then
			target="clean ${target}"
			depends_args="${depends_args:+${depends_args} }NOCLEANDEPENDS=yes"
		fi
		if [ -n "${dp_DEPENDS_CLEAN}" ]; then
			target="${target} clean"
			depends_args="${depends_args:+${depends_args} }NOCLEANDEPENDS=yes"
		fi
	fi

	case ${dp_DEPTYPE} in
	  LIB_DEPENDS)
	    case ${pattern} in
	      lib*.so*)      fct=find_lib ;;
	      *)
		echo "Error: pattern ${pattern} in LIB_DEPENDS is not valid"
		err=1
		continue
		;;
	    esac ;;
	  *)
	    case ${pattern} in
	      *\>*|*\<*|*=*) fct=find_package ;;
	      /nonexistent)  fct=false ;;
	      /*)            fct=find_file ;;
	      *)             fct=find_file_path ;;
	    esac ;;
	esac
	if ${fct} "${pattern}" ; then
		continue
	fi
	[ ${pattern} = "/nonexistent" ] || anynotfound=1

	if [ ! -f "${origin}/Makefile" ]; then
		echo "Error a dependency refers to a non existing origin: ${origin} in ${dp_DEPTYPE}" >&2
		err=1
		continue
	fi

	# Now actually install the dependencies
	install_depends "${origin}" "${target}" "${subpkg}" "${depends_args}"
	# Recheck if the installed dependency validates the pattern except for /nonexistent
	[ "${fct}" = "false" ] || ${fct} "${pattern}"
	echo "===>   Returning to build of ${dp_PKGNAME}"
done

if [ $err -eq 1 ]; then
	echo "Errors with dependencies."
	exit 1
fi

if [ -n "${dp_STRICT_DEPENDS}" -a ${anynotfound} -eq 1 ]; then \
	echo "===>   STRICT_DEPENDS set - Not installing missing dependencies."
	echo "       This means a dependency is wrong since it was not satisfied in the ${dp_DEPTYPE} phase."
	exit 1
fi