summaryrefslogtreecommitdiff
blob: a6529f2d41e50325f51d1540e36a21a9f93b1a21 (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
376
377
378
379
#!/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.0_pre4,v 1.2 2004/12/27 00:04:04 cyfred 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>

. /etc/init.d/functions.sh

need_version() {
	local X11 X11_VER X11_MAJOR_VER X11_MINOR_VER X11_MICRO_VER ERROR_MSG
	ERROR_MSG="This version requires >=x11-base/xorg-x11-6.8.0-r2"
	X11="$(portageq match / virtual/x11)"
	# Got the egrep from ferringb, who got it from portage code
	X11_VER="$(echo ${X11} | egrep -o '(cvs\.)?([[:digit:]]+)((\.[[:digit:]]+)*)([a-z]?)((_(pre|p|beta|alpha|rc)[[:digit:]]*)*)(-r([[:digit:]]+))?$')"
	X11_MAJOR_VER="${X11_VER%%.*}"
	# Hack around a little to get the minor version
	X11_MINOR_VER="${X11_VER#*.}"
	X11_MINOR_VER="${X11_MINOR_VER%%.*}"
	X11_REVISION_VER="${X11_VER##*-r}"

	# If version <6.*, die
	if [ ${X11_MAJOR_VER} -lt 6 ]
	then
		einfo "Detected X ${X11_MAJOR_VER}.${X11_MINOR_VER} series, revision ${X11_REVISION_VER}"
		eerror "${ERROR_MSG}"
		exit 1
	# If version =6.* and <6.8.*, die
	elif [ ${X11_MAJOR_VER} -eq 6 -a ${X11_MINOR_VER} -lt 8 ]
	then
		einfo "Detected X ${X11_MAJOR_VER}.${X11_MINOR_VER} series, revision ${X11_REVISION_VER}"
		eerror "${ERROR_MSG}"
		exit 1	
	# If version =6.8.0 and <r4, die
	elif [ "${X11_VER}" = "6.8.0" ]
	then
		if [ ! ${X11_REVISION_VER} -lt 4 ]
		then
			einfo "Detected X ${X11_MAJOR_VER}.${X11_MINOR_VER} series, revision ${X11_REVISION_VER}"
			eerror "${ERROR_MSG}"
			exit 1
		fi
	fi
}

count_implementations() {
	local DIR
	COUNT="0"
	for DIR in $(ls /usr/lib/opengl)
	do
		if [ "${DIR}" != "global" ]
		then
			DIRS="${DIRS} ${DIR}"
			COUNT="$((COUNT + 1))"
		fi
	done
}

usage() {
# In addition to the below function, there are two extra uses for this.
#
# They aren't in the printed help message because they're intended
# 	for developer use.
# 1) --use-old $NEW_IMPLEM will switch to the new implementation only if
# 	no old setup existed (i.e., X has never been installed)
# 2) --get-implementation will return the implementation

count_implementations

# Get grammar right in message
local IS_ARE IMPLEM_PLURAL
if [ ${COUNT} -eq 1 ]
then
	IS_ARE="is"
	IMPLEM_PLURAL=""
else
	IS_ARE="are"
	IMPLEM_PLURAL="s"
fi

cat << FOO
usage: ${0##*/} <GL implementation>

note:
       This utility switches between OpenGL implementations.  There ${IS_ARE}
       ${COUNT} available implementation${IMPLEM_PLURAL}: ${DIRS}.

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
}

need_version

if [ "$#" -ne 1 -a "$#" -ne 2 ] || \
	[ "$#" -eq 2 -a "$1" != "--use-old" ]
then
	usage
fi

GL_IMPLEM=""

# Discover GL implementation if it exists
get_implem() {
	if [ -f /etc/env.d/09opengl ]
	then
		source /etc/env.d/09opengl
		if [ -n "${LDPATH}" ]
		then
			GL_IMPLEM="${LDPATH/:\*/}"
			GL_IMPLEM="${GL_IMPLEM/\/usr\/lib\/opengl\/}"
			GL_IMPLEM="${GL_IMPLEM/\/lib}"
			unset LDPATH
		fi
	fi
}

# Return current GL implementation
if [ "$1" = "--get-implementation" ]
then
	get_implem

	if [ -n "${GL_IMPLEM}" ]
	then
		echo "${GL_IMPLEM}"
	fi

	if [ -z "${GL_IMPLEM}" ]
	then
		exit 2
	fi

	exit 0
fi

if [ $(id -u) -ne 0 ]
then
	eerror "${0}: must be root."
	exit 1
fi

# Only use specified implementation if it is not already selected.
if [ "$1" = "--use-old" ]
then
	shift

	get_implem

	if [ -z "${GL_IMPLEM}" ]
	then
		GL_IMPLEM="$1"
	fi
else
	GL_IMPLEM="$1"
fi

if [ ! -d /usr/lib/opengl/${GL_IMPLEM} ]
then
	usage
fi

ebegin "Switching to ${GL_IMPLEM} OpenGL interface"

	# Provide the right libnvidia-tls depending on ntpl or not
	if [ "${GL_IMPLEM}" = "nvidia" ]
	then
		if [ -e /usr/lib/opengl/${GL_IMPLEM}/lib/tls ]
		then
			rm -f /usr/lib/opengl/${GL_IMPLEM}/lib/tls
		fi
		
#		This was the nvidia approach but it seems to not work, new method
#		as suggested on various bug reports, #64927, #70545
#		/usr/lib/misc/tls_test /usr/lib/misc/tls_test_dso.so 2>/dev/null
		getconf GNU_LIBPTHREAD_VERSION | grep -i nptl > /dev/null
		if [ ${?} = 0 ]
		then
			ln -sf /usr/lib/opengl/${GL_IMPLEM}/tls /usr/lib/opengl/${GL_IMPLEM}/lib/tls
			if [ -d /usr/lib32/opengl/${GL_IMPLEM} ]
			then
				ln -sf /usr/lib32/opengl/${GL_IMPLEM}/tls /usr/lib32/opengl/${GL_IMPLEM}/lib/tls
			fi
		fi
	fi
	
	# Setup the $LDPATH
	echo "LDPATH=/usr/lib/opengl/${GL_IMPLEM}/lib" > /etc/env.d/09opengl
	# Check if we need 32 bit compatibility
	if [ -d /usr/lib32/opengl/${GL_IMPLEM} ]
	then
		source /etc/env.d/09opengl
		echo "LDPATH=${LDPATH}:/usr/lib32/opengl/${GL_IMPLEM}/lib" > /etc/env.d/09opengl
		unset LDPATH

		# We also need to make TLS directories now aswell, make 32bit one here
		lib32="$(readlink /usr/lib32)"
		if [ -h ${lib32}/tls ]
		then
			rm -f ${lib32}/tls
		fi
		if [ ! -d ${lib32}/tls ]
		then
			mkdir -p ${lib32}/tls
		fi
			
	fi

	# System wide tls stuff
	if [ -h /usr/lib/tls ]
	then
		rm -f /usr/lib/tls
	fi
	if [ ! -d /usr/lib/tls ] 
	then
		mkdir -p /usr/lib/tls
	fi
	
	/usr/sbin/env-update &>/dev/null

	LIBDIRS="lib lib32"
	for LIBDIR in ${LIBDIRS}
	do
		# If there is a 32 bit compatibility implementation we shall use it
		# Otherwise we want to have some compatibility GL, use xorg-x11.
		if [ "${LIBDIR}" = "lib32" ] && [ ! -d /usr/lib32/opengl/"${GL_IMPLEM}" ]
		then
			GL_LOCAL="xorg-x11"
		else
			GL_LOCAL="${GL_IMPLEM}"
		fi
		
		#setup the /usr/lib/libGL.so symlink
		if [ -e /usr/${LIBDIR}/libGL.so ]
		then
			rm -f /usr/${LIBDIR}/libGL.so
		fi
		if [ -e /usr/${LIBDIR}/libGL.so.1 ]
		then
			rm -f /usr/${LIBDIR}/libGL.so.1
		fi
		if [ -e /usr/${LIBDIR}/opengl/${GL_LOCAL}/lib/libGL.so ]
		then
			realname="$(readlink /usr/${LIBDIR}/opengl/${GL_LOCAL}/lib/libGL.so)"
			ln -sf /usr/${LIBDIR}/opengl/${GL_LOCAL}/lib/${realname} \
				/usr/${LIBDIR}/libGL.so
			ln -sf /usr/${LIBDIR}/opengl/${GL_LOCAL}/lib/${realname} \
				/usr/${LIBDIR}/libGL.so.1
		fi

		# Setup the /usr/lib/libGLcore.so symlink
		if [ -e /usr/${LIBDIR}/libGLcore.so ]
		then
			rm -f /usr/${LIBDIR}/libGLcore.so
		fi
		if [ -e /usr/${LIBDIR}/libGLcore.so.1 ]
		then
			rm -f /usr/${LIBDIR}/libGLcore.so.1
		fi
		if [ -e /usr/${LIBDIR}/opengl/${GL_IMPLEM}/lib/libGLcore.so ]
		then
			realname="$(readlink /usr/${LIBDIR}/opengl/${GL_IMPLEM}/lib/libGLcore.so)"
			ln -sf /usr/${LIBDIR}/opengl/${GL_LOCAL}/lib/${realname} \
				/usr/${LIBDIR}/libGLcore.so
			ln -sf /usr/${LIBDIR}/opengl/${GL_LOCAL}/lib/${realname} \
				/usr/${LIBDIR}/libGLcore.so.1
		fi

		# Now setup the TLS library links
		if [ -e /usr/${LIBDIR}/libnvidia-tls.so ]
		then
			rm -f /usr/${LIBDIR}/libnvidia-tls.so
		fi
		if [ -e /usr/${LIBDIR}/libnvidia-tls.so.1 ]
		then
			rm -f /usr/${LIBDIR}/libnvidia-tls.so.1
		fi
		if [ -e /usr/${LIBDIR}/opengl/${GL_LOCAL}/lib/tls/libnvidia-tls.so.1 ]
		then
			realtls="$(readlink /usr/${LIBDIR}/opengl/${GL_LOCAL}/lib/tls/libnvidia-tls.so.1)"
			ln -sf /usr/${LIBDIR}/opengl/${GL_LOCAL}/lib/tls/${realtls} \
				/usr/${LIBDIR}/tls/libnvidia-tls.so
			ln -sf /usr/${LIBDIR}/opengl/${GL_LOCAL}/lib/tls/${realtls} \
				/usr/${LIBDIR}/tls/libnvidia-tls.so.1
		fi
		if [ -e /usr/${LIBDIR}/opengl/${GL_LOCAL}/lib/libnvidia-tls.so.1 ]
		then
			realname="$(readlink /usr/${LIBDIR}/opengl/${GL_LOCAL}/lib/libnvidia-tls.so.1)"
			ln -sf /usr/${LIBDIR}/opengl/${GL_LOCAL}/lib/${realname} \
				/usr/${LIBDIR}/libnvidia-tls.so
			ln -sf /usr/${LIBDIR}/opengl/${GL_LOCAL}/lib/${realname} \
				/usr/${LIBDIR}/libnvidia-tls.so.1
		fi
	done

	# Setup the /usr/lib/libMesaGL.so symlink
	if [ -e /usr/lib/libMesaGL.so ]
	then
		rm -f /usr/lib/libMesaGL.so
	fi
	realname="$(readlink /usr/lib/opengl/${GL_IMPLEM}/lib/libGL.so)"
	ln -sf /usr/lib/opengl/${GL_IMPLEM}/lib/${realname} \
		/usr/lib/libMesaGL.so
	
	# Setup the /usr/lib/libGL.la symlink
	if [ -e /usr/lib/libGL.la ]
	then
		rm -f /usr/lib/libGL.la
	fi
	if [ -e /usr/lib/opengl/${GL_IMPLEM}/lib/libGL.la ]
	then
		ln -sf /usr/lib/opengl/${GL_IMPLEM}/lib/libGL.la \
			/usr/lib/libGL.la
	fi

	# Fix libtool archives (#48297)
	CURDIR="$(pwd)"
	cd /usr/lib
	if [ "`grep -l /usr/lib/opengl *.la`" ]
	then
		sed -i 's:/usr/lib/opengl/[^/]*/lib/libGL.la:/usr/lib/libGL.la:' \
			`grep -l /usr/lib/opengl *.la`
	fi
	cd ${CURDIR}

	# Setup the /usr/lib/modules/extensions/libglx.so symlink
	if [ -e /usr/lib/modules/extensions/libglx.so ]
	then
		rm -f /usr/lib/modules/extensions/libglx.so
	fi
	if [ -e /usr/lib/opengl/${GL_IMPLEM}/extensions/libglx.so ]
	then
		ln -sf /usr/lib/opengl/${GL_IMPLEM}/extensions/libglx.so \
			/usr/lib/modules/extensions/libglx.so
	fi

	#setup the /usr/lib/modules/extensions/libglx.a symlink
	if [ -e /usr/lib/modules/extensions/libglx.a ]
	then
		rm -f /usr/lib/modules/extensions/libglx.a
	fi
	if [ -e /usr/lib/opengl/${GL_IMPLEM}/extensions/libglx.a ]
	then
		ln -sf /usr/lib/opengl/${GL_IMPLEM}/extensions/libglx.a \
			/usr/lib/modules/extensions/libglx.a
	fi

	#setup the includes
	for x in gl.h glx.h glxtokens.h glext.h
	do
		if [ -e /usr/X11R6/include/GL/${x} ]
		then
			rm -f /usr/X11R6/include/GL/${x}
		fi

		# 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
		
		if [ -e /usr/lib/opengl/${GL_IMPLEM}/include/${x} ]
		then
			ln -sf /usr/lib/opengl/${GL_IMPLEM}/include/${x} \
				/usr/X11R6/include/GL/${x}
		else
			ln -sf /usr/lib/opengl/global/include/${x} \
				/usr/X11R6/include/GL/${x}
		fi
	done

eend 0

# vim:ts=4