summaryrefslogtreecommitdiff
blob: ddc3512381a3d925292cb93f6865a4c5cf9ac435 (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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/bin/bash
# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/x11-base/opengl-update/files/opengl-update-2.2.2,v 1.1 2005/08/10 06:39:34 spyderous Exp $
# Author:  Martin Schlemmer <azarah@gentoo.org>
# Further modifications by Donnie Berkholz <spyderous@gentoo.org>
# Further modifications based off submissions to bug #54984 <cyfred@gentoo.org>
# Further modifications by Jeremy Huddleston <eradicator@gentoo.org>

. /etc/init.d/functions.sh

hasq() {
	local x

	local me=${1}
	shift
        
	for x in "${@}"; do
		if [[ "${x}" == "${me}" ]]; then
			return 0
		fi
	done
	return 1
}

check_user() {
	if [[ $(id -u) -ne 0 ]]; then
		eerror "${0}: Must be run as root."
		exit 1
	fi
}

check_version() {
	if portageq has_version / 'x11-base/xorg-x11'; then
		if ! portageq has_version / '>=x11-base/xorg-x11-6.8.0-r4'; then
			eerror "This version requires >=x11-base/xorg-x11-6.8.0-r4"
			exit 1
		fi
	fi
}

get_current_implem() {
	local implem
	if [[ -f ${ENV_D} ]]; then
		source ${ENV_D}
		if [[ -n "${OPENGL_PROFILE}" ]]; then
			implem="${OPENGL_PROFILE}"
		elif [[ -n "${LDPATH}" ]]; then
			implem="${LDPATH%%:*}"
			implem="${implem##*opengl/}"
			implem="${implem%/lib*}"
		fi
		unset LDPATH
		unset OPENGL_PROFILE
	fi

	echo ${implem}
}

get_implementations() {
	local implems
	for dir in ${PREFIX}/lib{,32,64}/opengl/*; do 
		if [[ -d "${dir}" && ${dir##*/} != "global" ]] && ! hasq ${dir##*/} ${implems}; then
			implems=${implems:+${implems} }${dir##*/}
		fi
	done
	echo ${implems}
}

print_version() {
	echo "opengl-update ${VERSION}"
}

print_usage() {
	# Get grammar right in message
	local IS_ARE IMPLEM_PLURAL
	if [[ $(echo ${AVAIL_IMPLEMS} | wc -w) -eq 1 ]]; then
		IS_ARE="is"
		IMPLEM_PLURAL=""
	else
		IS_ARE="are"
		IMPLEM_PLURAL="s"
	fi

	print_version

	cat << FOO
Usage: ${0##*/} [<options>] <GL implementation>
       Set the opengl implementation.
       Valid options:
       --use-old:          If an implementation is already set, use that one.
       --prefix=<val>:     Set the source prefix (default: /usr)
       --dst-prefix=<val>: Set the destination prefix (default: /usr)
       --impl-headers:     Use headers provided by this implementation to
                           override golbal ones provided by opengl-update.

Usage: ${0##*/} --get-implementation
       Print the current implementaion

Notes:
       --impl-headers was default in <opengl-update-2.2.

       This utility switches between OpenGL implementations.  There ${IS_ARE}
       $(echo ${AVAIL_IMPLEMS} | wc -w) available implementation${IMPLEM_PLURAL}: ${AVAIL_IMPLEMS}

Examples:
       ${0##*/} xorg-x11
       This will setup things to use libGL.so from X.org.

       ${0##*/} nvidia
       This will setup things to use libGL.so from the nVidia drivers.

FOO
	exit 1
}

parse_options() {
	local opt
	while [[ ${#} -gt 0 ]]; do
		opt=${1}
		shift
		case ${opt} in
			--use-old)
				if [[ -n "${ACTION}" ]]; then
					ACTION="error"
					eerror "Invalid usage."
				else
					if [[ -n "${CURRENT_GL_IMPLEM}" ]] && hasq ${CURRENT_GL_IMPLEM} ${AVAIL_IMPLEMS}; then
						ACTION="old-implementation"
					fi
				fi				
			;;
			--get-implementation)
				if [[ -n "${ACTION}" ]]; then
					ACTION="error"
					eerror "Invalid usage."
				else
					ACTION="get-implementation"
				fi				
			;;
			--prefix=*)
				PREFIX=${opt#*=}
				AVAIL_IMPLEMS=$(get_implementations)
			;;
			--dst-prefix=*)
				DST_PREFIX=${opt#*=}
			;;
			--impl-headers)
				USE_PROFILE_HEADERS="yes"
			;;
			--help|-h|-?)
				ACION="usage"
			;;
			--version)
				ACTION="version"
			;;
			*)
				if hasq ${opt} ${AVAIL_IMPLEMS}; then
					if [[ "${ACTION}" != "old-implementation" ]]; then
						if [[ -n "${ACTION}" ]]; then
							ACTION="error"
							eerror "Invalid usage."
						else
							ACTION="set-implementation"
							NEW_GL_IMPLEM="${opt}"
						fi
					fi				
				else
					eerror "Unrecognized option: ${opt}"
					ACTION="error"
				fi
			;;
		esac
	done
}

set-new-implementation() {
	local GL_IMPLEM=${1}
	local GL_LOCAL

	check_version
	check_user

	# Set a sane umask... bug #83115
	umask 022

	if ! hasq ${GL_IMPLEM} ${AVAIL_IMPLEMS}; then
		eerror "Invalid profile selected."
		exit 1
	fi

	ebegin "Switching to ${GL_IMPLEM} OpenGL interface"
		rm -f ${ENV_D} &> /dev/null

		LIBDIRS="lib32 lib lib64"
		for LIBDIR in ${LIBDIRS}; do
			# Special case handling of lib32 because it can be a symlink to
			# emul libs
			if [[ "${LIBDIR}" = "lib32" ]]; then
				[[ -d "${PREFIX}/${LIBDIR}/opengl" ]] || continue
			else
				[[ -d "${PREFIX}/${LIBDIR}/opengl" && ! -h "${PREFIX}/${LIBDIR}" ]] || continue
			fi

			# Fallback on xorg-x11 if we don't have this implementation for this LIBDIR.
			if [[ ! -d ${PREFIX}/${LIBDIR}/opengl/"${GL_IMPLEM}" ]]; then
				GL_LOCAL="xorg-x11"
			else
				GL_LOCAL="${GL_IMPLEM}"
			fi
		
			mkdir -p ${DST_PREFIX}/${LIBDIR}
			pushd ${DST_PREFIX}/${LIBDIR} &> /dev/null
				# First remove old symlinks
				for file in libGL{,core}.{a,so,la} ; do
					[[ -h ${file} ]] && rm -f ${file}
				done

				# Note that we don't do .so*, just .so on purpose.  The
				# loader knows to look in the profile dir, and the
				# linked just needs the .so
				for file in ${PREFIX}/${LIBDIR}/opengl/${GL_LOCAL}/lib/libGL{,core}.{so,a,la}; do
					[[ -f "${file}" ]] || continue
					[[ -f "${file##*/}" ]] && rm -f ${file##*/}

					# Fix libtool archives (#48297)
					if [[ "${file%.la}" != "${file}" ]]; then
						sed "s:${PREFIX}/[^/]*/opengl/[^/]*/lib:${DST_PREFIX}/${LIBDIR}:g" ${file} > ${file##*/}
					else
						ln -s ${file}
					fi
				done
			popd &> /dev/null

			if [[ -e "${PREFIX}/${LIBDIR}/${LIBDIR}/opengl/${GL_LOCAL}/lib/tls" ]]; then
				mkdir -p ${DST_PREFIX}/${LIBDIR}/tls
				pushd ${DST_PREFIX}/${LIBDIR}/tls &> /dev/null
					# First remove old symlinks
					for file in libGL{,core}.{a,so,la} ; do
						[[ -h ${file} ]] && rm -f ${file}
					done

					for file in ${PREFIX}/${LIBDIR}/opengl/${GL_LOCAL}/lib/tls/libGL{,core}.{so,a,la}; do
						[[ -f "${file}" ]] || continue
						[[ -f "${file##*/}" ]] && rm -f ${file##*/}

						# Fix libtool archives (#48297)
						if [ "${file%.la}" != "${file}" ]; then
							sed "s:${PREFIX}/[^/]*/opengl/[^/]*/lib:${DST_PREFIX}/${LIBDIR}:g" ${file} > ${file##*/}
						else
							ln -s ${file}
						fi
					done
				popd &> /dev/null
			fi

			if [[ -e "${PREFIX}/${LIBDIR}/opengl/${GL_LOCAL}/extensions" ]]; then
				mkdir -p ${DST_PREFIX}/${LIBDIR}/modules/extensions
				pushd ${DST_PREFIX}/${LIBDIR}/modules/extensions &> /dev/null
					# First remove old symlinks
					for file in libglx.so libglx.a; do
						[[ -h ${file} ]] && rm -f ${file}
					done

					for file in ${PREFIX}/${LIBDIR}/opengl/${GL_LOCAL}/extensions/*.{so,a,la}; do
						[[ -f "${file}" ]] || continue
						[[ -f "${file##*/}" ]] && rm -f ${file##*/}

						# Fix libtool archives (#48297)
						if [[ "${file%.la}" != "${file}" ]]; then
							sed "s:${PREFIX}/[^/]*/opengl/[^/]*/lib:${DST_PREFIX}/${LIBDIR}:g" ${file} > ${file##*/}
						else
							ln -s ${file}
						fi
					done
				popd &> /dev/null
			fi

			# Setup the includes
			mkdir -p ${DST_PREFIX}/include/GL
			pushd ${DST_PREFIX}/include/GL &> /dev/null
				for file in gl.h glx.h glxtokens.h glext.h glxext.h glxmd.h glxproto.h; do
					# IMPORTANT
					# It is preferable currently to use the standard glext.h file
					# however if an OpenGL provider must use a self produced glext.h
					# then it should be installed to ${GL_IMPLEM}/include and the user
					# can add the --impl-headers option to select it.

					if [[ ${USE_PROFILE_HEADERS} == "yes" ]] ; then
						# Check the profile first.
						if [[ -e ${PREFIX}/${LIBDIR}/opengl/${GL_IMPLEM}/include/${file} ]]; then
							[[ -f "${file}" || ( -L "${file}" && ! -e "${file}" ) ]] && rm -f ${file}
							ln -s ${PREFIX}/${LIBDIR}/opengl/${GL_IMPLEM}/include/${file}
						fi
					fi

					if [[ -e ${PREFIX}/${LIBDIR}/opengl/global/include/${file} ]]; then
						[[ -f "${file}" || ( -L "${file}" && ! -e "${file}" ) ]] && rm -f ${file}
						ln -s ${PREFIX}/${LIBDIR}/opengl/global/include/${file}
					elif [[ -e ${PREFIX}/${LIBDIR}/opengl/${GL_IMPLEM}/include/${file} ]]; then
						[[ -f "${file}" || ( -L "${file}" && ! -e "${file}" ) ]] && rm -f ${file}
						ln -s ${PREFIX}/${LIBDIR}/opengl/${GL_IMPLEM}/include/${file}
					elif [[ -e ${PREFIX}/${LIBDIR}/opengl/xorg-x11/include/${file} ]]; then
						[[ -f "${file}" || ( -L "${file}" && ! -e "${file}" ) ]] && rm -f ${file}
						ln -s ${PREFIX}/${LIBDIR}/opengl/xorg-x11/include/${file}
					fi
				done
			popd &> /dev/null

			# Setup the $LDPATH
			ldpath="${ldpath:+${ldpath}:}${PREFIX}/${LIBDIR}/opengl/${GL_LOCAL}/lib"

		done

		echo "LDPATH=\"${ldpath}\"" > ${ENV_D}
		echo "OPENGL_PROFILE=\"${GL_IMPLEM}\"" >> ${ENV_D}

		env-update

	eend 0
}

## START PROGRAM ##

ENV_D="/etc/env.d/03opengl"
NEW_GL_IMPLEM=""
ACTION=""
PREFIX="/usr"
DST_PREFIX="/usr"
AVAIL_IMPLEMS=$(get_implementations)
CURRENT_GL_IMPLEM=$(get_current_implem)
USE_PROFILE_HEADERS="no"
VERSION="2.2.1"

parse_options ${@}

case ${ACTION} in
	get-implementation)
		if [[ -n "${CURRENT_GL_IMPLEM}" ]]; then
			echo ${CURRENT_GL_IMPLEM}
			exit 0
		else
			exit 2
		fi
	;;
	old-implementation)
		set-new-implementation ${CURRENT_GL_IMPLEM}
		exit $?
	;;
	set-implementation)
		if [[ -n "${NEW_GL_IMPLEM}" ]]; then
			set-new-implementation ${NEW_GL_IMPLEM}
			exit $?
		else
			print_usage
			exit 1
		fi
	;;
	version)
		print_version
		exit 0
	;;
	usage)
		print_usage
		exit 0
	;;
	error)
		print_usage
		exit 1
	;;
	*)
		print_usage
		exit 1
	;;
esac