Magellan Linux

Contents of /trunk/opengl-update/opengl-update.in

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1978 - (show annotations) (download)
Thu Nov 15 11:55:31 2012 UTC (11 years, 5 months ago) by niro
File size: 10295 byte(s)
-support dynamic GL setup for other implementations like EGL GLES OpenVG and for new extensions like dri, wfb etc
1 #!/bin/bash
2 # Copyright 1999-2004 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4 # $Header: /home/cvsd/magellan-cvs/magellan-src/opengl-update/opengl-update.sh,v 1.10 2007-11-14 19:19:16 niro Exp $
5 # Author: Martin Schlemmer <azarah@gentoo.org>
6 # Further modifications by Donnie Berkholz <spyderous@gentoo.org>
7 # Further modifications based off submissions to bug #54984 <cyfred@gentoo.org>
8 # Further modifications by Jeremy Huddleston <eradicator@gentoo.org>
9 #
10 # hacked for use with magellan-linux
11 # <niro@magellan-linux.de>
12 #
13
14 hasq()
15 {
16 local x
17
18 local me=${1}
19 shift
20
21 for x in $@
22 do
23 [[ ${x} = ${me} ]] && return 0
24 done
25
26 return 1
27 }
28
29 check_user()
30 {
31 if [[ $(id -u) -ne 0 ]]
32 then
33 echo "${0}: Must be run as root."
34 exit 1
35 fi
36 }
37
38 get_current_implem()
39 {
40 local implem
41
42 if [[ -f ${ENV_D} ]]
43 then
44 source ${ENV_D}
45 if [[ -n ${OPENGL_PROFILE} ]]
46 then
47 implem="${OPENGL_PROFILE}"
48 elif [[ -n ${LDPATH} ]]
49 then
50 implem="${LDPATH%%:*}"
51 implem="${implem##*opengl/}"
52 implem="${implem%/lib*}"
53 fi
54 unset LDPATH
55 unset OPENGL_PROFILE
56 fi
57
58 echo ${implem}
59 }
60
61 get_implementations()
62 {
63 local implems
64
65 for dir in ${PREFIX}/lib{,32,64}/opengl/*
66 do
67 if [[ -d ${dir} ]] &&
68 [[ ${dir##*/} != global ]] &&
69 ! hasq ${dir##*/} ${implems}
70 then
71 implems=${implems:+${implems} }${dir##*/}
72 fi
73 done
74
75 echo ${implems}
76 }
77
78 print_version()
79 {
80 echo "opengl-update ${VERSION}"
81 }
82
83 print_usage()
84 {
85 # Get grammar right in message
86 local IS_ARE IMPLEM_PLURAL
87
88 if [[ $(echo ${AVAIL_IMPLEMS} | wc -w) -eq 1 ]]
89 then
90 IS_ARE="is"
91 IMPLEM_PLURAL=""
92 else
93 IS_ARE="are"
94 IMPLEM_PLURAL="s"
95 fi
96
97 print_version
98
99 cat << FOO
100 Usage: ${0##*/} [<options>] <GL implementation>
101 Set the opengl implementation.
102 Valid options:
103 --help|-h|-?: Prints this help.
104 --version: Shows the version of this utility.
105 --use-old: If an implementation is already set, use that one.
106 --prefix=<val>: Set the source prefix (default: /usr)
107 --dst-prefix=<val>: Set the destination prefix (default: /usr)
108 --impl-headers: Use headers provided by this implementation to
109 override global ones provided by opengl-update.
110
111 Usage: ${0##*/} --get-implementation
112 Print the current implementaion
113
114 Notes:
115 --impl-headers was default in <opengl-update-2.2.
116
117 This utility switches between OpenGL implementations. There ${IS_ARE}
118 $(echo ${AVAIL_IMPLEMS} | wc -w) available implementation${IMPLEM_PLURAL}: ${AVAIL_IMPLEMS}
119
120 Examples:
121 ${0##*/} xorg
122 This will setup things to use libGL.so from X.org.
123
124 ${0##*/} nvidia
125 This will setup things to use libGL.so from the nVidia drivers.
126
127 FOO
128 exit 1
129 }
130
131 parse_options()
132 {
133 local opt
134
135 while [[ ${#} -gt 0 ]]
136 do
137 opt=${1}
138 shift
139 case ${opt} in
140 --use-old)
141 if [[ -n ${ACTION} ]]
142 then
143 ACTION="error"
144 echo "Invalid usage."
145 else
146 if [[ -n "${CURRENT_GL_IMPLEM}" ]] &&
147 hasq ${CURRENT_GL_IMPLEM} ${AVAIL_IMPLEMS}
148 then
149 ACTION="old-implementation"
150 fi
151 fi
152 ;;
153
154 --get-implementation)
155 if [[ -n ${ACTION} ]]
156 then
157 ACTION="error"
158 echo "Invalid usage."
159 else
160 ACTION="get-implementation"
161 fi
162 ;;
163
164 --prefix=*)
165 PREFIX=${opt#*=}
166 AVAIL_IMPLEMS=$(get_implementations)
167 ;;
168
169 --dst-prefix=*) DST_PREFIX=${opt#*=} ;;
170
171 --impl-headers) USE_PROFILE_HEADERS="yes" ;;
172
173 --help|-h|-?) ACTION="usage" ;;
174
175 --version) ACTION="version" ;;
176
177 *)
178 if hasq ${opt} ${AVAIL_IMPLEMS}
179 then
180 if [[ ${ACTION} != old-implementation ]]
181 then
182 if [[ -n ${ACTION} ]]
183 then
184 ACTION="error"
185 echo "Invalid usage."
186 else
187 ACTION="set-implementation"
188 NEW_GL_IMPLEM="${opt}"
189 fi
190 fi
191 else
192 echo "Unrecognized option: ${opt}"
193 ACTION="error"
194 fi
195 ;;
196 esac
197 done
198 }
199
200 set_new_implementation()
201 {
202 local GL_IMPLEM=${1}
203 local GL_LOCAL
204
205 check_user
206
207 # Set a sane umask... bug #83115
208 umask 022
209
210 if ! hasq ${GL_IMPLEM} ${AVAIL_IMPLEMS}
211 then
212 echo "Invalid profile selected."
213 exit 1
214 fi
215
216 echo "Switching to ${GL_IMPLEM} OpenGL interface"
217
218 rm -f ${ENV_D} &> /dev/null
219
220 LIBDIRS="lib32 lib lib64"
221 for LIBDIR in ${LIBDIRS}
222 do
223 # Special case handling of lib32 because it can be a symlink to
224 # emul libs
225 if [[ ${LIBDIR} = lib32 ]]
226 then
227 [[ -d ${PREFIX}/${LIBDIR}/opengl ]] || continue
228 else
229 [[ -d ${PREFIX}/${LIBDIR}/opengl ]] &&
230 [[ ! -h ${PREFIX}/${LIBDIR} ]] || continue
231 fi
232
233 # Fallback on xorg if we don't have this implementation for this LIBDIR.
234 if [[ ! -d ${PREFIX}/${LIBDIR}/opengl/${GL_IMPLEM} ]]
235 then
236 GL_LOCAL="xorg"
237 else
238 GL_LOCAL="${GL_IMPLEM}"
239 fi
240
241 mkdir -p ${DST_PREFIX}/${LIBDIR}
242 pushd ${DST_PREFIX}/${LIBDIR} &> /dev/null
243 # First remove old symlinks
244 for file in lib{EGL,GL*,OpenVG}{,core}.{so,dylib,a,la}
245 do
246 [[ -h ${file} ]] && rm -f ${file}
247 done
248
249 # Note that we don't do .so*, just .so on purpose. The
250 # loader knows to look in the profile dir, and the
251 # linked just needs the .so
252 for file in ${PREFIX}/${LIBDIR}/opengl/${GL_LOCAL}/lib/lib{EGL,GL*,OpenVG}{,core}.{so,dylib,a,la}
253 do
254 [[ -f ${file} ]] || continue
255 [[ -f ${file##*/} ]] && rm -f ${file##*/}
256
257 # Fix libtool archives (#48297)
258 if [[ ${file%.la} != ${file} ]]
259 then
260 sed "s:${PREFIX}/[^/]*/opengl/[^/]*/lib:${DST_PREFIX}/${LIBDIR}:g" ${file} > ${file##*/}
261 else
262 ln -s ${file}
263 fi
264 done
265 popd &> /dev/null
266
267 if [[ -e ${PREFIX}/${LIBDIR}/opengl/${GL_LOCAL}/lib/tls ]]
268 then
269 mkdir -p ${DST_PREFIX}/${LIBDIR}/tls
270 pushd ${DST_PREFIX}/${LIBDIR}/tls &> /dev/null
271 # First remove old symlinks
272 for file in lib{EGL,GL*,OpenVG}{,core}.{so,dylib,a,la}
273 do
274 [[ -h ${file} ]] && rm -f ${file}
275 done
276
277 for file in ${PREFIX}/${LIBDIR}/opengl/${GL_LOCAL}/lib/tls/lib{EGL,GL*,OpenVG}{,core}.{so,dylib,a,la}
278 do
279 [[ -f ${file} ]] || continue
280 [[ -f ${file##*/} ]] && rm -f ${file##*/}
281
282 # Fix libtool archives (#48297)
283 if [[ ${file%.la} != ${file} ]]
284 then
285 sed "s:${PREFIX}/[^/]*/opengl/[^/]*/lib:${DST_PREFIX}/${LIBDIR}:g" ${file} > ${file##*/}
286 else
287 ln -s ${file}
288 fi
289 done
290 popd &> /dev/null
291 fi
292
293 local MODULEDIR
294 if [[ -e ${DST_PREFIX}/${LIBDIR}/xorg/modules ]]
295 then
296 MODULEDIR="xorg/modules"
297 else
298 MODULEDIR="modules"
299 fi
300
301 if [[ -e ${PREFIX}/${LIBDIR}/opengl/${GL_LOCAL}/extensions ]]
302 then
303 mkdir -p ${DST_PREFIX}/${LIBDIR}/${MODULEDIR}/extensions
304 pushd ${DST_PREFIX}/${LIBDIR}/${MODULEDIR}/extensions &> /dev/null
305 # First remove old symlinks
306 for file in lib{wfb,glx,dri,dri2}.{so,dylib,a}
307 do
308 [[ -h ${file} ]] && rm -f ${file}
309 done
310
311 for file in ${PREFIX}/${LIBDIR}/opengl/${GL_LOCAL}/extensions/*.{so,a,la}
312 do
313 [[ -f ${file} ]] || continue
314 [[ -f ${file##*/} ]] && rm -f ${file##*/}
315
316 # Fix libtool archives (#48297)
317 if [[ ${file%.la} != ${file} ]]
318 then
319 sed "s:${PREFIX}/[^/]*/opengl/[^/]*/lib:${DST_PREFIX}/${LIBDIR}:g" ${file} > ${file##*/}
320 else
321 ln -s ${file}
322 fi
323 done
324 popd &> /dev/null
325 fi
326
327 # Setup the includes
328 headers[GL]="gl.h glx.h glxtokens.h glext.h glxext.h glxmd.h glxproto.h"
329 headers[GLES]="egl.h gl_extensions.h glext.h gl.h glplatform.h"
330 headers[GLES2]="gl2ext.h gl2.h gl2platform.h"
331 headers[EGL]="eglext.h egl.h eglplatform.h"
332 headers[KHR]="khrplatform.h"
333 headers[VG]="openvg.h vgext.h vgplatform.h vgu.h"
334 for incl_dir in EGL GL GLES GLES2 KHR VG
335 do
336 mkdir -p ${DST_PREFIX}/include/${incl_dir}
337 pushd ${DST_PREFIX}/include/${incl_dir} &> /dev/null
338 for file in ${headers[${incl_dir}]}
339 do
340 # IMPORTANT
341 # It is preferable currently to use the standard glext.h file
342 # however if an OpenGL provider must use a self produced glext.h
343 # then it should be installed to ${GL_IMPLEM}/include and the user
344 # can add the --impl-headers option to select it.
345 if [[ ${USE_PROFILE_HEADERS} = yes ]]
346 then
347 # Check the profile first.
348 if [[ -e ${PREFIX}/${LIBDIR}/opengl/${GL_IMPLEM}/include/${incl_dir}/${file} ]]
349 then
350 [[ -f "${file}" || ( -L "${file}" && ! -e "${file}" ) ]] && rm -f ${file}
351 ln -s ${PREFIX}/${LIBDIR}/opengl/${GL_IMPLEM}/include/${incl_dir}/${file}
352 fi
353 continue
354 fi
355
356 if [[ -e ${PREFIX}/${LIBDIR}/opengl/global/include/${incl_dir}/${file} ]]
357 then
358 [[ -f "${file}" || ( -L "${file}" && ! -e "${file}" ) ]] && rm -f ${file}
359 ln -s ${PREFIX}/${LIBDIR}/opengl/global/include/${incl_dir}/${file}
360 elif [[ -e ${PREFIX}/${LIBDIR}/opengl/${GL_IMPLEM}/include/${incl_dir}/${file} ]]
361 then
362 [[ -f "${file}" || ( -L "${file}" && ! -e "${file}" ) ]] && rm -f ${file}
363 ln -s ${PREFIX}/${LIBDIR}/opengl/${GL_IMPLEM}/include/${incl_dir}/${file}
364 elif [[ -e ${PREFIX}/${LIBDIR}/opengl/xorg/include/${incl_dir}/${file} ]]
365 then
366 [[ -f "${file}" || ( -L "${file}" && ! -e "${file}" ) ]] && rm -f ${file}
367 ln -s ${PREFIX}/${LIBDIR}/opengl/xorg/include/${incl_dir}/${file}
368 fi
369 done
370 popd &> /dev/null
371 done
372
373 # Setup the $LDPATH
374 ldpath="${ldpath:+${ldpath}:}${PREFIX}/${LIBDIR}/opengl/${GL_LOCAL}/lib"
375
376 done
377
378 # put only one LDPATH per line!
379 local i
380 for i in $(echo ${ldpath} | sed "s:\::\ :g")
381 do
382 echo "LDPATH=\"${i}\"" >> ${ENV_D}
383 done
384 echo "OPENGL_PROFILE=\"${GL_IMPLEM}\"" >> ${ENV_D}
385
386 env-rebuild
387
388 return 0
389 }
390
391 ## START PROGRAM ##
392
393 ENV_D="/etc/env.d/03opengl"
394 NEW_GL_IMPLEM=""
395 ACTION=""
396 PREFIX="/usr"
397 DST_PREFIX="/usr"
398 AVAIL_IMPLEMS=$(get_implementations)
399 CURRENT_GL_IMPLEM=$(get_current_implem)
400 USE_PROFILE_HEADERS="no"
401 VERSION="@@VERSION@@"
402
403 parse_options ${@}
404
405 case ${ACTION} in
406 get-implementation)
407 if [[ -n ${CURRENT_GL_IMPLEM} ]]
408 then
409 echo ${CURRENT_GL_IMPLEM}
410 exit 0
411 else
412 exit 2
413 fi
414 ;;
415
416 old-implementation)
417 set_new_implementation ${CURRENT_GL_IMPLEM}
418 exit $?
419 ;;
420
421 set-implementation)
422 if [[ -n ${NEW_GL_IMPLEM} ]]
423 then
424 set_new_implementation ${NEW_GL_IMPLEM}
425 exit $?
426 else
427 print_usage
428 exit 1
429 fi
430 ;;
431
432 version)
433 print_version
434 exit 0
435 ;;
436
437 usage)
438 print_usage
439 exit 0
440 ;;
441
442 error)
443 print_usage
444 exit 1
445 ;;
446
447 *)
448 print_usage
449 exit 1
450 ;;
451 esac