summaryrefslogtreecommitdiff
path: root/pkgtools/pkgtasks/files/ocaml_findlib.subr
blob: 976719a4db21f6f235f261a12265f61faeeda85a (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
# Copyright (c) 2017 The NetBSD Foundation, Inc.
# All rights reserved.
#
# This code is derived from software contributed to The NetBSD Foundation
# by Johnny C. Lam.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# NAME
#	ocaml_findlib.subr -- update OCaml findlib search paths
#
# SYNOPSIS
#	task_ocaml_findlib [-s] add | remove
#	task_ocaml_findlib check-add | check-remove
#
# DESCRIPTION
#	The task_ocaml_findlib function supports four actions: "add",
#	"remove", "check-add", and "check-remove".
#
#	The available options are as follows:
#
#	-s	Silent; don't write to standard output.
#
#	The task_ocaml_findlib function reads standard input line by line
#	and looks for lines of the form:
#
#	    # FINDLIB: <libdir> [<ldconf>]
#
#	If libdir is is a relative path, then it is assumed to be
#	relative to ${OCAML_SITELIB}.  If ldconf is a relative path, then
#	it is assumed to be relative to ${PKG_PREFIX}.
#
#	The "add" action adds pathnames to the ld.conf file.
#
#	The "remove" action removes pathnames from the ld.conf file.
#
#	The "check-add" action will check whether pathnames are missing
#	from the ld.conf file and writes an informative message noting
#	the missing pathnames.
#
#	The "check-remove" action will check whether pathnames are still
#	present in the ld.conf file and writes an informative message
#	noting the existing paths.
#
# RETURN VALUES
#	The "add" and "remove" actions return 0 if they are successful
#	for all paths, and >0 if an error occurs.
#
#	The "check-add" and "check-remove" actions return >0 if they
#	write informative messages, and return 0 otherwise.
#
# ENVIRONMENT
#	The following variables are used if they are set:
#
#	MV	The name or path to the mv(1) utility.
#
#	OCAML_SITELIB
#		The site library search path.  If this path is relative,
#		then it is relative to ${PKG_PREFIX}.  The default is
#		"lib/ocaml/site-lib".
#
#	PKGNAME
#		The name of the package.
#
#	PKG_DESTDIR
#		A "destdir" prefix that is prepended to all filesystem
#		paths.  The default value is the empty string.
#
#	PKG_PREFIX
#		The installation prefix of the package.  The default is
#		"/usr/pkg".
#
#	RM	The name or path to the rm(1) utility.
#
#	TASK_MSG
#		String prepended to all normal message written to
#		standard output.
#

__task_ocaml_findlib__="yes"
__task_ocaml_findlib_init__="_task_ocaml_findlib_init"

task_load cleanup
task_load echo
task_load lock
task_load makedir
task_load maketemp
task_load match
task_load quote

task_ocaml_findlib()
{
	: ${CP:=cp}
	: ${MV:=mv}

	: ${OCAML_SITELIB:=lib/ocaml/site-lib}
	: ${PKG_PREFIX:=/usr/pkg}
	: ${PKGNAME:=${0##*/}}
	: ${TASK_MSG:=""}

	local arg
	local echo="task_echo"
	local OPTIND=1
	while getopts ":s" arg "$@"; do
		case $arg in
		s)	echo=":" ;;
		*)	return 127 ;;
		esac
	done
	shift $(( ${OPTIND} - 1 ))
	[ $# -gt 0 ] || return 127

	local action="$1"; shift
	case $action in
	add|remove|check-add|check-remove)
		: "valid actions" ;;
	*)	return 0 ;;
	esac

	# Guard against ${PKG_PREFIX} == "/".
	local prefix
	case ${PKG_PREFIX}/ in
	//)	prefix= ;;
	*)	prefix=${PKG_PREFIX} ;;
	esac

	local sitelib
	case ${OCAML_SITELIB} in
	/*)	sitelib=${OCAML_SITELIB} ;;
	*)	sitelib="$prefix/${OCAML_SITELIB}" ;;
	esac

	local result line_result
	local quoted
	local lock lock_quoted
	local temp temp_quoted

	result=0
	local hash tag libdir ldconf
	while read hash tag libdir ldconf; do
		# Filter for "# SHELL:".
		case $hash/$tag in
		"#/FINDLIB:")
			: "use this line" ;;
		*)	continue ;;
		esac

		# Canonicalize paths.
		case $libdir in
		"")	# skip lines without any required args
			continue ;;
		[!/]*)	libdir="$sitelib/$libdir" ;;
		esac
		case $ldconf in
		"")	ldconf="$prefix/lib/ocaml/ld.conf" ;;
		[!/]*)	ldconf="$prefix/$ldconf" ;;
		esac
		ldconf="${PKG_DESTDIR}$ldconf"
		local ldconfdir=${ldconf%/*}

		line_result=0
		case $action in
		add|remove)
			# Ensure that the ld.conf directory exists.
			[ -d "$ldconfdir" ] || task_makedir "$ldconfdir" 2>/dev/null
			[ -d "$ldconfdir" ] || line_result=1
			if [ $line_result -eq 0 ]; then
				lock="$ldconf.lock"
				task_quote "$lock"
				lock_quoted=$quoted
				__task_ocaml_findlib_locks__="$quoted $__task_ocaml_findlib_locks__"
				task_lock "$lock" || line_result=1
			fi ;;
		esac
		if [ $line_result -eq 0 ]; then
			case $action in
			add)	if [ -f "$ldconf" ] && task_match -qw "$libdir" < $ldconf; then
					$echo "${TASK_MSG}! path already added: $libdir"
				else
					temp=$( task_maketemp "$ldconf.pkgtasks.XXXXXXXXXX" )
					if [ -n "$temp" ]; then
						task_quote "$temp"
						temp_quoted="$quoted"
						__task_ocaml_findlib_temps__="$__task_ocaml_findlib_temps__ $temp_quoted"
						if [ -f "$ldconf" ]; then
							# Preserve existing ownership and permissions onto tempfile.
							${CP} -fp "$ldconf" "$temp"
							# Overwrite contents of tempfile.
							{ task_match -vw "$libdir" < $ldconf; echo "$libdir"; } > $temp
						else
							# Overwrite contents of tempfile.
							echo "$libdir" > $temp
						fi
						# rename(2) is atomic.
						if ${MV} -f "$temp" "$ldconf"; then
							$echo "${TASK_MSG}> path added: $libdir"
							__task_ocaml_findlib_temps__=${__task_ocaml_findlib_temps__% $temp_quoted}
						else
							$echo "${TASK_MSG}! path not added: $libdir"
							line_result=1
						fi
					else
						$echo "${TASK_MSG}! cannot create temporary file for $ldconf"
						line_result=1
					fi
				fi ;;
			remove)	if [ -f "$ldconf" ] && task_match -qw "$libdir" < $ldconf; then
					temp=$( task_maketemp "$ldconf.pkgtasks.XXXXXXXXXX" )
					if [ -n "$temp" ]; then
						task_quote "$temp"
						temp_quoted="$quoted"
						__task_ocaml_findlib_temps__="$__task_ocaml_findlib_temps__ $temp_quoted"
						# Preserve existing ownership and permissions onto tempfile.
						${CP} -fp "$ldconf" "$temp"
						# Overwrite contents of tempfile.
						task_match -vw "$libdir" < $ldconf > $temp
						# rename(2) is atomic.
						if ${MV} -f "$temp" "$ldconf"; then
							$echo "${TASK_MSG}> path removed: $libdir"
							__task_ocaml_findlib_temps__=${__task_ocaml_findlib_temps__% $temp_quoted}
						else
							$echo "${TASK_MSG}! path not removed: $libdir"
							line_result=1
						fi
					else
						$echo "${TASK_MSG}! cannot create temporary file for $ldconf"
						line_result=1
					fi
				else
					$echo "${TASK_MSG}> path already removed: $libdir"
				fi ;;
			check-add)
				if [ -f "$ldconf" ] && task_match -qw "$libdir" < $ldconf; then
				   	: "path already added"
				else
					task_echo "!!! INFO: ${PKGNAME}: Add path \"$libdir\" to $ldconf."
					line_result=1
				fi ;;
			check-remove)
				if [ -f "$ldconf" ] && task_match -qw "$libdir" < $ldconf; then
					task_echo "!!! INFO: ${PKGNAME}: Remove path \"$libdir\" from $ldconf."
					line_result=1
				fi ;;
			esac
		fi
		case $action in
		add|remove)
			task_lock -r "$lock"
			__task_ocaml_findlib_locks__=${__task_ocaml_findlib_locks__#$lock_quoted } ;;
		esac
		[ $line_result -eq 0 ] || result=1
	done

	_task_ocaml_findlib_cleanup
	return $result
}

_task_ocaml_findlib_cleanup()
{
	: ${RM:=rm}

	eval set -- $__task_ocaml_findlib_temps__
	local file
	for file; do
		${RM} -f "$file"
	done
	__task_ocaml_findlib_temps__=

	eval set -- $__task_ocaml_findlib_locks__
	local lockfile
	for lockfile; do
		task_lock -r "$lockfile"
	done
	__task_ocaml_findlib_locks__=
}

_task_ocaml_findlib_init()
{
	task_cleanup_add_hook _task_ocaml_findlib_cleanup
}

# Static variable for temporary files that should be removed if an error occurs.
__task_ocaml_findlib_temps__=
# Static variable for locks that should be released if an error occurs.
__task_ocaml_findlib_locks__=