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