3262e49841012ef372b834e02c8410234a6f68bb
[mesa.git] / bin / mklib
1 #!/bin/sh
2
3 # Make a shared library.
4 # Basically do a switch/case depending on the OS and make a shared (or static)
5 # library conforming to that OS.
6
7
8 # Usage:
9 # mklib [options] objects ...
10 # Options:
11 # -o LIBRARY specifies the name of resulting library
12 # ("-o GL" for example, might result in "libGL.so" being made)
13 # -major N specifies major version number (default is 1)
14 # -minor N specifies minor version number (default is 0)
15 # -patch N specifies patch version number (default is 0)
16 # -lLIBRARY specifies a dependency on LIBRARY
17 # -LDIR search in DIR for library dependencies
18 # -cplusplus link with C++ runtime
19 # -static make a static library (default is dynamic/shared)
20 # -install DIR move resulting library file(s) to DIR
21 # -arch ARCH override using `uname` to determine architecture
22 # -archopt OPT specify an extra achitecture-specific option OPT
23 #
24 # The library name should just be "GL" or "GLU", etc. The 'lib' prefix
25 # will be added here if needed, as well as the ".so" or ".a" suffix, etc.
26 #
27 # objects should be: foo.o bar.o etc.o
28 #
29 # Environment variables recognized:
30 # CC C compiler command
31 # CXX C++ compiler command
32 #
33
34
35 #
36 # Option defaults
37 #
38 LIBNAME=""
39 MAJOR=1
40 MINOR=0
41 PATCH=""
42 DEPS=""
43 CPLUSPLUS=0
44 STATIC=0
45 INSTALLDIR="."
46 ARCH="auto"
47 ARCHOPT=""
48
49
50 #
51 # Parse arguments
52 #
53 while true
54 do
55 case $1 in
56 '-o') shift 1; LIBNAME=$1;;
57 '-major') shift 1; MAJOR=$1;;
58 '-minor') shift 1; MINOR=$1;;
59 '-patch') shift 1; PATCH=$1;;
60 -l*) DEPS="$DEPS $1";;
61 -L*) DEPS="$DEPS $1";;
62 '-cplusplus') CPLUSPLUS=1;;
63 '-static') STATIC=1;;
64 '-install') shift 1; INSTALLDIR=$1;;
65 '-arch') shift 1; ARCH=$1;;
66 '-archopt') shift 1; ARCHOPT=$1;;
67 -*) echo "mklib: Unknown option: " $1 ; exit 1;;
68 *) break
69 esac
70 shift 1
71 done
72 OBJECTS=$@
73
74 if [ ${ARCH} = "auto" ] ; then
75 ARCH=`uname`
76 fi
77
78
79 #
80 # Error checking
81 #
82 if [ "x${LIBNAME}" = "x" ] ; then
83 echo "mklib: Error: no library name specified"
84 exit 1
85 fi
86 if [ "x${OBJECTS}" = "x" ] ; then
87 echo "mklib: Error: no object files specified"
88 exit 1
89 fi
90
91
92 #
93 # Debugging info
94 #
95 if [ ] ; then
96 echo "-----------------"
97 echo ARCH is $ARCH
98 echo LIBNAME is $LIBNAME
99 echo MAJOR is $MAJOR
100 echo MINOR is $MINOR
101 echo PATCH is $PATCH
102 echo DEPS are $DEPS
103 echo "-----------------"
104 fi
105
106
107 #
108 # OK, make the library now
109 #
110 case $ARCH in
111
112 'Linux' | 'OpenBSD')
113 # GCC-based environment
114 LIBNAME="lib${LIBNAME}" # prefix with "lib"
115
116 if [ $STATIC = 1 ] ; then
117 echo "mklib: Making" $ARCH "static library: " ${LIBNAME}.a
118 LINK="ar"
119 OPTS="-ru"
120 # make lib
121 ${LINK} ${OPTS} ${LIBNAME}.a ${OBJECTS}
122 ranlib ${LIBNAME}.a
123 # finish up
124 FINAL_LIBS=${LIBNAME}.a
125 else
126 if [ $ARCH = 'Linux' ] ; then
127 OPTS="-Xlinker -Bsymbolic -shared -Wl,-soname,${LIBNAME}.so.${MAJOR}"
128 else
129 OPTS="-shared -Wl,-soname,${LIBNAME}.so.${MAJOR}"
130 fi
131 if [ x${PATCH} = "x" ] ; then
132 VERSION="${MAJOR}.${MINOR}"
133 else
134 VERSION="${MAJOR}.${MINOR}.${PATCH}"
135 fi
136
137 echo "mklib: Making" $ARCH "shared library: " ${LIBNAME}.so.${VERSION}
138
139 if [ $CPLUSPLUS = 1 ] ; then
140 LINK="g++"
141 else
142 LINK="gcc"
143 fi
144
145 # rm any old libs
146 rm -f ${LIBNAME}.so.${VERSION}
147 rm -f ${LIBNAME}.so.${MAJOR}
148 rm -f ${LIBNAME}.so
149
150 # make lib
151 ${LINK} ${OPTS} -o ${LIBNAME}.so.${VERSION} ${OBJECTS} ${DEPS}
152 # make usual symlinks
153 ln -s ${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR}
154 ln -s ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so
155 # finish up
156 FINAL_LIBS="${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so"
157 fi
158 ;;
159
160 'SunOS')
161 if [ $STATIC = 1 ] ; then
162 LIBNAME="lib${LIBNAME}.a"
163 echo "mklib: Making SunOS static library: " ${LIBNAME}
164 rm -f ${LIBNAME}
165 ar -ruv ${LIBNAME} ${OBJECTS}
166 FINAL_LIBS=${LIBNAME}
167 else
168 LIBNAME="lib${LIBNAME}.so"
169 echo "mklib: Making SunOS shared library: " ${LIBNAME}
170 # XXX OPTS for gcc should be -shared, but that doesn't work.
171 # Using -G does work though.
172 if [ $CPLUSPLUS = 1 ] ; then
173 # determine linker and options for C++ code
174 if [ "x${CXX}" = "xg++" ] ; then
175 # use g++
176 LINK="g++"
177 OPTS="-G"
178 elif [ "x${CXX}" = "xCC" ] ; then
179 # use Sun CC
180 LINK="CC"
181 OPTS="-G"
182 elif [ "x${CXX}" = "xc++" ] ; then
183 # use Sun c++
184 LINK="c++"
185 OPTS="-G"
186 elif [ `which c++` ] ; then
187 # use Sun c++
188 LINK="c++"
189 OPTS="-G"
190 elif [ `type g++` ] ; then
191 # use g++
192 LINK="g++"
193 OPTS="-G"
194 else
195 echo "mklib: warning: can't find C++ comiler, trying CC."
196 LINK="CC"
197 OPTS="-G"
198 fi
199 elif [ "x${CC}" = "xgcc" ] ; then
200 # use gcc for linking
201 LINK="gcc"
202 OPTS="-G"
203 else
204 # use native Sun linker
205 LINK="ld"
206 OPTS="-G"
207 fi
208 echo "mklib: linker is" ${LINK} ${OPTS}
209 rm -f ${LIBNAME}.${MAJOR} ${LIBNAME}
210 ${LINK} ${OPTS} -o ${LIBNAME}.${MAJOR} ${OBJECTS} ${DEPS}
211 ln -s ${LIBNAME}.${MAJOR} ${LIBNAME}
212 FINAL_LIBS="${LIBNAME}.${MAJOR} ${LIBNAME}"
213 fi
214 ;;
215
216 'FreeBSD')
217 if [ $STATIC = 1 ] ; then
218 STLIB="lib${LIBNAME}.a"
219 echo "mklib: Making FreeBSD static library: " ${STLIB}
220 rm -f ${STLIB}
221 ar cq ${STLIB} ${OBJECTS}
222 ranlib ${STLIB}
223 FINAL_LIBS=${STLIB}
224 else
225 SHLIB="lib${LIBNAME}.so.${MAJOR}.${MINOR}"
226 echo "mklib: Making FreeBSD shared library: " ${SHLIB}
227 rm -f ${SHLIB}
228 ld -Bshareable -o ${SHLIB} ${OBJECTS}
229 # XXX make lib${LIBNAME}.so.${MAJOR} symlink?
230 FINAL_LIBS=${SHLIB}
231 fi
232 ;;
233
234 'NetBSD')
235 if [ $STATIC = 1 ] ; then
236 LIBNAME="lib${LIBNAME}_pic.a"
237 echo "mklib: Making NetBSD PIC static library: " ${LIBNAME}
238 rm -f ${LIBNAME}
239 ar cq ${LIBNAME} ${OBJECTS}
240 ranlib ${LIBNAME}
241 FINAL_LIBS=${LIBNAME}
242 else
243 LIBNAME="lib${LIBNAME}.so.${MAJOR}.${MINOR}"
244 echo "mklib: Making NetBSD PIC shared library: " ${LIBNAME}
245 rm -f ${LIBNAME}
246 ld -x -Bshareable -Bforcearchive -o ${LIBNAME} ${OBJECTS}
247 FINAL_LIBS=${LIBNAME}
248 fi
249 ;;
250
251 'IRIX' | 'IRIX64')
252 if [ $STATIC = 1 ] ; then
253 LIBNAME="lib${LIBNAME}.a"
254 rm -f ${LIBNAME}
255 ar rc ${LIBNAME} ${OBJECTS}
256 FINAL_LIBS=${LIBNAME}
257 else
258 LIBNAME="lib${LIBNAME}.so" # prefix with "lib", suffix with ".so"
259 if [ $ARCHOPT = "64" ] ; then
260 # 64-bit ABI
261 OPTS="-64 -shared -all"
262 echo "mklib: Making IRIX 64-bit shared library: " ${LIBNAME}
263 elif [ $ARCHOPT = "o32" ] ; then
264 # old 32-bit ABI
265 OPTS="-32 -shared -all"
266 echo "mklib: Making IRIX o32-bit shared library: " ${LIBNAME}
267 else
268 # new 32-bit ABI
269 OPTS="-n32 -shared -all"
270 echo "mklib: Making IRIX n32-bit shared library: " ${LIBNAME}
271 fi
272 if [ $CPLUSPLUS = 1 ] ; then
273 LINK="CC"
274 else
275 LINK="ld"
276 fi
277 ${LINK} ${OPTS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
278 FINAL_LIBS=${LIBNAME}
279 fi
280 ;;
281
282 'linux-cygwin')
283 LIBNAME="lib${LIBNAME}.a"
284 echo "mklib: Making linux-cygwin library: " ${LIBNAME}
285 rm -f ${LIBNAME}
286 gnuwin32ar ruv ${LIBNAME} ${OBJECTS}
287 FINAL_LIBS=${LIBNAME}
288 ;;
289
290 'HP-UX')
291 if [ $STATIC = 1 ] ; then
292 LIBNAME="lib${LIBNAME}.a"
293 echo "mklib: Making HPUX static library: " ${LIBNAME}
294 rm -f ${LIBNAME}
295 ar -ruv ${LIBNAME} ${OBJECTS}
296 FINAL_LIBS=${LIBNAME}
297 else
298 RUNLIB="lib${LIBNAME}.${MAJOR}"
299 DEVLIB="lib${LIBNAME}.sl"
300 echo "mklib: Making HPUX stared library: " ${RUNLIB} ${DEVLIB}
301 ld -b -o ${RUNLIB} +b ${RUNLIB} ${OBJECTS} ${DEPS}
302 ln -s ${RUNLIB} ${DEVLIB}
303 FINAL_LIBS="{RUNLIB} ${DEVLIB}"
304 fi
305 ;;
306
307 'AIX')
308 if [ $STATIC = 1 ] ; then
309 LIBNAME="lib${LIBNAME}.a"
310 echo "mklib: Making AIX static library: " ${LIBNAME}
311 ar -ruv ${LIBNAME} ${OBJECTS}
312 FINAL_LIBS=${LIBNAME}
313 else
314 echo "mklib: PROBLEM: AIX shared libs not supported!!!"
315 fi
316 ;;
317
318 'AIX64')
319 if [ $STATIC = 1 ] ; then
320 LIBNAME="lib${LIBNAME}.a"
321 echo "mklib: Making AIX static library: " ${LIBNAME}
322 ar -X64 -ruv ${LIBNAME} ${OBJECTS}
323 FINAL_LIBS=${LIBNAME}
324 else
325 echo "mklib: PROBLEM: AIX64 shared libs not supported!!!"
326 fi
327 ;;
328
329 'OpenSTEP')
330 LIBNAME="lib${LIBNAME}.a"
331 echo "mklib: Making OpenSTEP static library: " ${LIBNAME}
332 libtool -static -o ${LIBNAME} - ${OBJECTS}
333 FINAL_LIBS=${LIBNAME}
334 ;;
335
336 'OSF1')
337 if [ $STATIC = 1 ] ; then
338 LIBNAME="lib${LIBNAME}.a"
339 echo "mklib: Making OSF/1 static library: " ${LIBNAME}
340 rm -f ${LIBNAME}
341 ar -ruv ${LIBNAME} ${OBJECTS}
342 FINAL_LIBS=${LIBNAME}
343 else
344 VERSION="${MAJOR}.${MINOR}"
345 LIBNAME="lib${LIBNAME}.so"
346 echo "mklib: Making OSF/1 shared library: " ${LIBNAME}
347 rm -f ${LIBNAME}.${VERSION}
348 ld -o ${LIBNAME}.${VERSION} -shared -no_archive -set_version ${VERSION} -soname ${LIBNAME}.${VERSION} -expect_unresolved \* -all ${OBJECTS} ${DEPS}
349 ln -sf ${LIBNAME}.${VERSION} ${LIBNAME}
350 FINAL_LIBS="${LIBNAME} ${LIBNAME}.${VERSION}"
351 fi
352 ;;
353
354 'Darwin')
355 if [ $STATIC = 1 ] ; then
356 LIBNAME="lib${LIBNAME}.a"
357 echo "mklib: Making Darwin static library: " ${LIBNAME}
358 LINK="ar"
359 OPTS="-ruv"
360 ${LINK} ${OPTS} ${LIBNAME} ${OBJECTS}
361 FINAL_LIBS=${LIBNAME}
362 else
363 VERSION="${MAJOR}.${MINOR}.${PATCH}"
364 LIBNAME="${LIBNAME}.dylib"
365 ARNAME="${LIBNAME}.dylib.a"
366 echo "mklib: Making Darwin libraries: " ${LIBNAME} ${ARNAME}
367 FLAGS="-dynamiclib -multiply_defined suppress"
368 cc ${FLAGS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
369 # also make regular .a files,
370 # provided by Danek Duvall (duvall@dhduvall.student.princeton.edu)
371 ar ruv ${ARNAME} ${OBJECTS}
372 ranlib ${ARNAME}
373 FINAL_LIBS="${ARNAME} ${LIBNAME}"
374 fi
375 ;;
376
377 'LynxOS')
378 LIBNAME="lib${LIBNAME}.a"
379 echo "mklib: Making LynxOS static library: " ${LIBNAME}
380 rm -f ${LIBNAME}
381 ar ru ${LIBNAME} ${OBJECTS}
382 FINAL_LIBS=${LIBNAME}
383 ;;
384
385 'BeOS')
386 LIBNAME="lib${LIBNAME}.so"
387 echo "mklib: Making BeOS shared library: " ${LIBNAME}
388 gcc -nostart -Xlinker -soname=${LIBNAME} -L/Be/develop/lib/x86 ${OBJECTS} -lbe -o ${LIBNAME}
389 FINAL_LIBS=${LIBNAME}
390 ;;
391
392 'QNX')
393 LIBNAME="lib${LIBNAME}.a"
394 echo "mklib: Making QNX library: " ${LIBNAME}
395 wlib ${LIBNAME} ${OBJECTS}
396 FINAL_LIBS=${LIBNAME}
397 ;;
398
399 'MorphOS')
400 LIBNAME="lib${LIBNAME}.a"
401 echo "mklib: Making MorphOS library: " ${LIBNAME}
402 ppc-morphos-ar rc ${LIBNAME} ${OBJECTS}
403 FINAL_LIBS="${LIBNAME}"
404 ;;
405
406 'icc')
407 # Intel C compiler
408 LIBNAME="lib${LIBNAME}" # prefix with "lib"
409
410 if [ $STATIC = 1 ] ; then
411 echo "mklib: Making Intel ICC static library: " ${LIBNAME}.a
412 LINK="ar"
413 OPTS="-ruv"
414 # make lib
415 ${LINK} ${OPTS} ${LIBNAME}.a ${OBJECTS}
416 # finish up
417 FINAL_LIBS="${LIBNAME}.a"
418 else
419 OPTS="-shared"
420 VERSION="${MAJOR}.${MINOR}.${PATCH}"
421 echo "mklib: Making Intel ICC shared library: " ${LIBNAME}.so.${VERSION}
422
423 if [ $CPLUSPLUS = 1 ] ; then
424 LINK="icc"
425 else
426 LINK="icc"
427 fi
428 # rm any old libs
429 rm -f ${LIBNAME}.so.${VERSION}
430 rm -f ${LIBNAME}.so.${MAJOR}
431 rm -f ${LIBNAME}.so
432 # make lib
433 ${LINK} ${OPTS} -o ${LIBNAME}.so.${VERSION} ${OBJECTS} ${DEPS}
434 # make usual symlinks
435 ln -s ${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR}
436 ln -s ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so
437 # finish up
438 FINAL_LIBS="${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so"
439 fi
440 ;;
441
442 'aix-gcc')
443 # AIX with gcc
444 if [ $STATIC = 1 ] ; then
445 LIBNAME="lib${LIBNAME}.a"
446 echo "mklib: Making AIX GCC static library: " ${LIBNAME}
447 rm -f ${LIBNAME}
448 ar ru ${LIBNAME} ${OBJECTS}
449 FINAL_LIBS=${LIBNAME}
450 else
451 LIBNAME="lib${LIBNAME}.so" # prefix with "lib", suffix with ".so"
452 echo "mklib: Making AIX GCC shared library: " ${LIBNAME}
453 # remove old lib
454 rm -f ${LIBNAME}
455 # make the lib
456 gcc -shared -Wl,-G ${OBJECTS} ${DEPS} -o ${LIBNAME}
457 # NOTE: the application linking with this library must specify
458 # the -Wl,-brtl flags to gcc
459 FINAL_LIBS=${LIBNAME}
460 fi
461 ;;
462
463 'ultrix')
464 # XXX untested
465 if [ $STATIC = 0 ] ; then
466 echo "mklib: Warning shared libs not supported on Ultrix"
467 fi
468 LIBNAME="lib${LIBNAME}.a"
469 echo "mklib: Making static library for Ultrix: " ${LIBNAME}
470 rm -f ${LIBNAME}
471 ar ru ${LIBNAME} ${OBJECTS}
472 FINAL_LIBS="${LIBNAME}"
473 ;;
474
475 'example')
476 # If you're adding support for a new architecture, you can
477 # start with this:
478 if [ $STATIC = 1 ] ; then
479 LIBNAME="lib${LIBNAME}.a"
480 echo "mklib: Making static library for example arch: " ${LIBNAME}
481 rm -f ${LIBNAME}
482 ar rv ${LIBNAME} ${OBJECTS}
483 FINAL_LIBS="${LIBNAME}"
484 else
485 LIBNAME="lib${LIBNAME}.so" # prefix with "lib"
486 echo "mklib: Making shared library for example arch: " ${LIBNAME}
487 ld -o ${LIBNAME} ${OBJECTS} ${DEPS}
488 FINAL_LIBS="${LIBNAME}"
489 fi
490 ;;
491
492 *)
493 echo "mklib: ERROR: Don't know how to make a static/shared library for" ${ARCH}
494 echo "mklib: Please add necessary commands to mklib script."
495 ;;
496 esac
497
498
499 #
500 # Put library files into installation directory if specified.
501 #
502 if [ ${INSTALLDIR} != "." ] ; then
503 echo "mklib: Installing" ${FINAL_LIBS} "in" ${INSTALLDIR}
504 mv ${FINAL_LIBS} ${INSTALLDIR}/
505 fi