Make mklib propogate all errors
[profile/ivi/mesa.git] / bin / mklib
1 #!/bin/sh
2
3 # Make a shared library.
4 # This script should be useful for projects other than Mesa.
5 # Improvements/fixes are welcome.
6
7
8 # Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
9 #
10 # Permission is hereby granted, free of charge, to any person obtaining a
11 # copy of this software and associated documentation files (the "Software"),
12 # to deal in the Software without restriction, including without limitation
13 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 # and/or sell copies of the Software, and to permit persons to whom the
15 # Software is furnished to do so, subject to the following conditions:
16 #
17 # The above copyright notice and this permission notice shall be included
18 # in all copies or substantial portions of the Software.
19 #
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23 # BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
24 # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
27 # propagate any errors
28 function errtrap {
29     es=$?
30     exit $es
31 }
32 trap errtrap ERR
33
34 # Given a list of files, look for .a archives and unpack them.
35 # Return the original list of files minus the .a files plus the unpacked files.
36 expand_archives() {
37     DIR=$1
38     shift
39     FILES=$@
40     NEWFILES=""
41     ORIG_DIR=`pwd`
42     mkdir -p "$DIR"
43     cd "$DIR"
44     for FILE in $FILES ; do
45         case $FILE in
46             *.a)
47                 # extract the .o files from this .a archive
48                 case $FILE in
49                     /*) ;;
50                     *)  FILE="$ORIG_DIR/$FILE" ;;
51                 esac
52                 MEMBERS=`ar t $FILE`
53                 ar x $FILE
54                 for MEMBER in $MEMBERS ; do
55                     NEWFILES="$NEWFILES $DIR/$MEMBER"
56                 done
57                 ;;
58             *)
59                 # other file type, just add to list
60                 NEWFILES="$NEWFILES $FILE"
61                 ;;
62         esac
63     done
64     cd "$ORIG_DIR"
65     echo $NEWFILES
66 }
67
68
69 # Given a list of files, look for .a archives and return a list of all objects
70 # in the .a archives.
71 contents_of_archives() {
72     FILES=$@
73     NEWFILES=""
74     for FILE in $FILES ; do
75         case $FILE in
76             *.a)
77                 # get list of members in this .a archive
78                 MEMBERS=`ar t $FILE`
79                 NEWFILES="$NEWFILES $MEMBERS"
80                 ;;
81             *)
82                 # skip other file types
83                 ;;
84         esac
85     done
86     echo $NEWFILES
87 }
88
89
90 # Make static library with 'ar'
91 # params:
92 #    options to ar
93 #    1 or 0 to indicate if ranlib should be run
94 #    libname to make
95 #    list of object files
96 # Return name of library we made
97 # Example: "make_ar_static_lib -ru 1 libfoo.a foo.o bar.o"
98 make_ar_static_lib() {
99     OPTS=$1
100     shift;
101     RANLIB=$1
102     shift;
103     LIBNAME=$1
104     shift;
105     OBJECTS=$@
106
107     # remove existing lib, if present
108     rm -f ${LIBNAME}
109
110     # make static lib
111     ar ${OPTS} ${LIBNAME} ${OBJECTS}
112
113     # run ranlib
114     if [ ${RANLIB} = 1 ] ; then
115         ranlib ${LIBNAME}
116     fi
117
118     echo ${LIBNAME}
119 }
120
121
122 # Print usage info.
123 usage() {
124     echo 'Usage: mklib [options] objects'
125     echo 'Create a shared library from object files.'
126     echo '  -o LIBRARY    specifies the name of the resulting library, without'
127     echo '                the leading "lib" or any suffix.'
128     echo '                (eg: "-o GL" might result in "libGL.so" being made)'
129     echo '  -major N      specifies major version number (default is 1)'
130     echo '  -minor N      specifies minor version number (default is 0)'
131     echo '  -patch N      specifies patch version number (default is 0)'
132     echo '  -lLIBRARY     specifies a dependency on LIBRARY'
133     echo '  -LDIR         search in DIR for library dependencies at build time'
134     echo '  -RDIR         search in DIR for library dependencies at run time'
135     echo '  -linker L     explicity specify the linker program to use (eg: gcc, g++)'
136     echo '                Not observed on all systems at this time.'
137     echo '  -ldflags OPT  specify any additional linker flags in OPT'
138     echo '  -cplusplus    link with C++ runtime'
139     echo '  -static       make a static library (default is dynamic/shared)'
140     echo '  -dlopen       make a shared library suitable for dynamic loading'
141     echo '  -install DIR  put resulting library file(s) in DIR'
142     echo '  -arch ARCH    override using `uname` to determine host system'
143     echo '  -archopt OPT  specify an extra achitecture-specific option OPT'
144     echo '  -altopts OPTS alternate options to override all others'
145     echo "  -noprefix     don't prefix library name with 'lib' nor add any suffix"
146     echo '  -exports FILE only export the symbols listed in FILE'
147     echo '  -id NAME      Sets the id of the dylib (Darwin)'
148     echo '  -h, --help    display this information and exit'
149 }
150
151
152 #
153 # Option defaults
154 #
155 LIBNAME=""
156 MAJOR=1
157 MINOR=0
158 PATCH=""
159 DEPS=""
160 LINK=""
161 LDFLAGS=""
162 CPLUSPLUS=0
163 STATIC=0
164 DLOPEN=0
165 INSTALLDIR="."
166 ARCH="auto"
167 ARCHOPT=""
168 NOPREFIX=0
169 EXPORTS=""
170 ID=""
171
172 #
173 # Parse arguments
174 #
175 while true
176 do
177     case $1 in
178         '-h' | '--help')
179             usage
180             exit 1
181             ;;
182         '-o')
183             shift 1;
184             LIBNAME=$1
185             ;;
186         '-major')
187             shift 1;
188             MAJOR=$1
189             ;;
190         '-minor')
191             shift 1;
192             MINOR=$1
193             ;;
194         '-patch')
195             shift 1;
196             PATCH=$1
197             ;;
198         '-linker')
199             shift 1;
200             LINK=$1
201             ;;
202         '-ldflags')
203             shift 1;
204             LDFLAGS=$1
205             ;;
206         -l*)
207             DEPS="$DEPS $1"
208             ;;
209         -L*)
210             DEPS="$DEPS $1"
211             ;;
212         -R*)
213             DEPS="$DEPS $1"
214             ;;
215         -Wl*)
216             DEPS="$DEPS $1"
217             ;;
218         -pthread)
219             # this is a special case (see bugzilla 10876)
220             DEPS="$DEPS $1"
221             ;;
222         '-pthread')
223             DEPS="$DEPS -pthread"
224             ;;
225         '-cplusplus')
226             CPLUSPLUS=1
227             ;;
228         '-static')
229             STATIC=1
230             ;;
231         '-dlopen')
232             DLOPEN=1
233             ;;
234         '-install')
235             shift 1;
236             INSTALLDIR=$1
237             ;;
238         '-arch')
239             shift 1;
240             ARCH=$1
241             ;;
242         '-archopt')
243             shift 1;
244             ARCHOPT=$1
245             ;;
246         '-altopts')
247             shift 1;
248             ALTOPTS=$1
249             ;;
250         '-noprefix')
251             NOPREFIX=1
252             ;;
253         '-exports')
254             shift 1;
255             EXPORTS=$1
256             ;;
257         '-id')
258             shift 1;
259             ID=$1
260             ;;
261         -*)
262             echo "mklib: Unknown option: " $1 ;
263             exit 1
264             ;;
265         *)
266             # This should be the first object file, stop parsing
267             break
268     esac
269     shift 1
270 done
271 OBJECTS=$@
272
273
274 if [ ${ARCH} = "auto" ] ; then
275     ARCH=`uname`
276 fi
277
278
279 if [ $STATIC = 1 ]; then
280     # filter out linker options inside object list
281     NEWOBJECTS=""
282     for OBJ in $OBJECTS ; do
283         case $OBJ in
284             -Wl,*)
285                 echo "mklib: warning: ignoring $OBJ for static library"
286                 ;;
287             *)
288                 NEWOBJECTS="$NEWOBJECTS $OBJ"
289                 ;;
290         esac
291     done
292     OBJECTS=$NEWOBJECTS
293 fi
294
295
296 #
297 # Error checking
298 #
299 if [ "x${LIBNAME}" = "x" ] ; then
300     echo "mklib: Error: no library name specified (-h for help)"
301     exit 1
302 fi
303 if [ "x${OBJECTS}" = "x" ] ; then
304     echo "mklib: Error: no object files specified (-h for help)"
305     exit 1
306 fi
307
308
309 #
310 # Debugging info
311 #
312 if [  ]  ; then
313     echo "-----------------"
314     echo ARCH is $ARCH
315     echo LIBNAME is $LIBNAME
316     echo MAJOR is $MAJOR
317     echo MINOR is $MINOR
318     echo PATCH is $PATCH
319     echo DEPS are $DEPS
320     echo "EXPORTS in" $EXPORTS
321     echo ID is $ID
322     echo "-----------------"
323 fi
324
325
326 #
327 # OK, make the library now
328 #
329 case $ARCH in
330
331     'Linux' | 'OpenBSD' | 'DragonFly' | 'GNU' | GNU/*)
332         # we assume gcc
333
334         if [ "x$LINK" = "x" ] ; then
335             # -linker was not specified so set default link command now
336             if [ $CPLUSPLUS = 1 ] ; then
337                 LINK=g++
338             else
339                 LINK=gcc
340             fi
341         fi
342
343         if [ $NOPREFIX = 1 ] ; then
344             # No "lib" or ".so" part
345             echo "mklib: Making" $ARCH "shared library: " ${LIBNAME}
346             case $ARCH in 'Linux' | 'GNU' | GNU/*)
347                 OPTS="-Xlinker -Bsymbolic -shared"
348             ;;
349             *)
350                 OPTS="-shared"
351             ;;
352             esac
353
354             # Check if objects are 32-bit and we're running in 64-bit
355             # environment.  If so, pass -m32 flag to linker.
356             set ${OBJECTS}
357             ABI32=`file $1 | grep 32-bit`
358             if [ "${ABI32}" -a `uname -m` = "x86_64" ] ; then
359                 OPTS="-m32 ${OPTS}"
360             fi
361
362             if [ "${ALTOPTS}" ] ; then
363                 OPTS=${ALTOPTS}
364             fi
365
366             rm -f ${LIBNAME}
367             # make lib
368             ${LINK} ${OPTS} ${LDFLAGS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
369             # finish up
370             FINAL_LIBS="${LIBNAME}"
371         elif [ $STATIC = 1 ] ; then
372             # make a static .a library
373             LIBNAME="lib${LIBNAME}.a"     # prefix with "lib", suffix with ".a"
374             echo "mklib: Making" $ARCH "static library: " ${LIBNAME}
375             OPTS="-ru"
376             if [ "${ALTOPTS}" ] ; then
377                 OPTS=${ALTOPTS}
378             fi
379
380             # expand .a into .o files
381             NEW_OBJECTS=`expand_archives ${LIBNAME}.obj $OBJECTS`
382
383             # make static lib
384             FINAL_LIBS=`make_ar_static_lib ${OPTS} 1 ${LIBNAME} ${NEW_OBJECTS}`
385
386             # remove temporary extracted .o files
387             rm -rf ${LIBNAME}.obj
388         else
389             # make dynamic library
390             LIBNAME="lib${LIBNAME}"     # prefix with "lib"
391             case $ARCH in 'Linux' | 'GNU' | GNU/*)
392                 OPTS="-Xlinker -Bsymbolic -shared -Wl,-soname,${LIBNAME}.so.${MAJOR}"
393             ;;
394             *)
395                 OPTS="-shared -Wl,-soname,${LIBNAME}.so.${MAJOR}"
396             ;;
397             esac
398             if [ $EXPORTS ] ; then
399                 #OPTS="${OPTS} -Xlinker --retain-symbols-file ${EXPORTS}"
400                 # Make the 'exptmp' file for --version-script option
401                 echo "{" > exptmp
402                 echo "global:" >> exptmp
403                 sed 's/$/;/' ${EXPORTS} >> exptmp
404                 echo "local:" >> exptmp
405                 echo "*;" >> exptmp
406                 echo "};" >> exptmp
407                 OPTS="${OPTS} -Xlinker --version-script=exptmp"
408                 # exptmp is removed below
409             fi
410
411             # Check if objects are 32-bit and we're running in 64-bit
412             # environment.  If so, pass -m32 flag to linker.
413             set ${OBJECTS}
414             ABI32=`file $1 | grep 32-bit`
415             if [ "${ABI32}" -a `uname -m` = "x86_64" ] ; then
416                 OPTS="-m32 ${OPTS}"
417             fi
418             if [ "${ALTOPTS}" ] ; then
419                 OPTS=${ALTOPTS}
420             fi
421
422             if [ x${PATCH} = "x" ] ; then
423                 VERSION="${MAJOR}.${MINOR}"
424             else
425                 VERSION="${MAJOR}.${MINOR}.${PATCH}"
426             fi
427
428             echo "mklib: Making" $ARCH "shared library: " ${LIBNAME}.so.${VERSION}
429
430             # rm any old libs
431             rm -f ${LIBNAME}.so.${VERSION}
432             rm -f ${LIBNAME}.so.${MAJOR}
433             rm -f ${LIBNAME}.so
434
435             # make lib
436             ${LINK} ${OPTS} ${LDFLAGS} -o ${LIBNAME}.so.${VERSION} ${OBJECTS} ${DEPS}
437             # make usual symlinks
438             ln -s ${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR}
439             ln -s ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so
440             # finish up
441             FINAL_LIBS="${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so"
442 #           rm -f exptmp
443         fi
444         ;;
445
446     'SunOS')
447         if [ $STATIC = 1 ] ; then
448             LIBNAME="lib${LIBNAME}.a"
449             echo "mklib: Making SunOS static library: " ${LIBNAME}
450             FINAL_LIBS=`make_ar_static_lib -ruc 0 ${LIBNAME} ${OBJECTS}`
451         else
452             if [ $NOPREFIX = 0 ] ; then
453                 LIBNAME="lib${LIBNAME}.so"
454             fi
455             echo "mklib: Making SunOS shared library: " ${LIBNAME}
456
457             if [ "x$LINK" = "x" ] ; then
458                 # -linker was not specified, choose default linker now
459                 if [ $CPLUSPLUS = 1 ] ; then
460                     # determine linker and options for C++ code
461                     if [ `which c++` ] ; then
462                         # use Sun c++
463                         LINK="c++"
464                     elif [ `type g++` ] ; then
465                         # use g++
466                         LINK="g++"
467                     else
468                         echo "mklib: warning: can't find C++ compiler, trying CC."
469                         LINK="CC"
470                     fi
471                 else
472                     # use native Sun linker for C code
473                     LINK="ld"
474                 fi
475             fi
476
477             # linker options
478             if [ ${LINK} = "ld" -o ${LINK} = "cc" -o ${LINK} = "CC" ] ; then
479                 # SunOS tools, -G to make shared libs
480                 OPTS="-G"
481             else
482                 # gcc linker
483                 # Check if objects are 32-bit and we're running in 64-bit
484                 # environment.  If so, pass -m32 flag to linker.
485                 set ${OBJECTS}
486                 ABI32=`file $1 | grep 32-bit`
487                 if [ "${ABI32}" ] ; then
488                     OPTS="-m32 -shared -Wl,-Bdynamic"
489                 else
490                     OPTS="-m64 -shared -Wl,-Bdynamic"
491                 fi
492             fi
493
494             # If using Sun C++ compiler, need to tell it not to add runpaths
495             # that are specific to the build machine
496             if [ ${LINK} = "CC" ] ; then
497                 OPTS="${OPTS} -norunpath"
498             fi
499
500             # Solaris linker requires explicitly listing the Standard C & C++
501             # libraries in the link path when building shared objects
502             if [ ${LINK} = "CC" ] ; then
503                 DEPS="${DEPS} -lCrun"
504             fi
505             DEPS="${DEPS} -lc"
506
507             if [ $EXPORTS ] ; then
508                 # Make the 'mapfile.scope' linker mapfile
509                 echo "{" > mapfile.scope
510                 echo "global:" >> mapfile.scope
511                 sed 's/$/;/' ${EXPORTS} >> mapfile.scope
512                 echo "local:" >> mapfile.scope
513                 echo "    *;" >> mapfile.scope
514                 echo "};" >> mapfile.scope
515                 OPTS="${OPTS} -Wl,-Mmapfile.scope"
516             fi
517
518             # Check if objects are SPARC v9
519             # file says: ELF 64-bit MSB relocatable SPARCV9 Version 1
520             set ${OBJECTS}
521             if [ ${LINK} = "cc" -o ${LINK} = "CC" ] ; then
522                 SPARCV9=`file $1 | grep SPARCV9`
523                 if [ "${SPARCV9}" ] ; then
524                     OPTS="${OPTS} -xarch=v9"
525                 fi
526             fi
527             if [ "${ALTOPTS}" ] ; then
528                 OPTS=${ALTOPTS}
529             fi
530
531             # for debug:
532             #echo "mklib: linker is" ${LINK} ${OPTS}
533             if [ $NOPREFIX = 1 ] ; then
534                 rm -f ${LIBNAME}
535                 ${LINK} ${OPTS} ${LDFLAGS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
536                 FINAL_LIBS="${LIBNAME}"
537             else
538                 rm -f ${LIBNAME}.${MAJOR} ${LIBNAME}
539                 ${LINK} ${OPTS} ${LDFLAGS} -o ${LIBNAME}.${MAJOR} -h ${LIBNAME}.${MAJOR} ${OBJECTS} ${DEPS}
540                 ln -s ${LIBNAME}.${MAJOR} ${LIBNAME}
541                 FINAL_LIBS="${LIBNAME}.${MAJOR} ${LIBNAME}"
542             fi
543         fi
544         ;;
545
546     'FreeBSD')
547         # we assume gcc
548
549         if [ "x$LINK" = "x" ] ; then
550             # -linker was not specified so set default link command now
551             if [ $CPLUSPLUS = 1 ] ; then
552                 LINK=g++
553             else
554                 LINK=gcc
555             fi
556         fi
557
558         if [ $NOPREFIX = 1 ] ; then
559             # No "lib" or ".so" part
560             echo "mklib: Making FreeBSD shared library: " ${LIBNAME}
561             OPTS="-shared"
562             if [ "${ALTOPTS}" ] ; then
563                 OPTS=${ALTOPTS}
564             fi
565             rm -f ${LIBNAME}
566             ${LINK} ${OPTS} ${LDFLAGS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
567             FINAL_LIBS=${LIBNAME}
568         elif [ $STATIC = 1 ] ; then
569             # make a static .a library
570             STLIB="lib${LIBNAME}.a"
571             echo "mklib: Making FreeBSD static library: " ${STLIB}
572
573             # expand .a into .o files
574             NEW_OBJECTS=`expand_archives ${STLIB}.obj $OBJECTS`
575
576             FINAL_LIBS=`make_ar_static_lib cq 1 ${STLIB} ${NEW_OBJECTS}`
577
578             # remove temporary extracted .o files
579             rm -rf ${STLIB}.obj
580         else
581             # make dynamic library
582             SHLIB="lib${LIBNAME}.so.${MAJOR}"
583             OPTS="-shared -Wl,-soname,${SHLIB}"
584             if [ "${ALTOPTS}" ] ; then
585                 OPTS=${ALTOPTS}
586             fi
587             echo "mklib: Making FreeBSD shared library: " ${SHLIB}
588             rm -f ${SHLIB}
589             ${LINK} ${OPTS} ${LDFLAGS} -o ${SHLIB} ${OBJECTS} ${DEPS}
590             ln -sf ${SHLIB} "lib${LIBNAME}.so"
591             FINAL_LIBS="${SHLIB} lib${LIBNAME}.so"
592         fi
593         ;;
594
595     'NetBSD')
596         if [ $STATIC = 1 ] ; then
597             LIBNAME="lib${LIBNAME}_pic.a"
598             echo "mklib: Making NetBSD PIC static library: " ${LIBNAME}
599             FINAL_LIBS=`make_ar_static_lib cq 1 ${LIBNAME} ${OBJECTS}`
600         else
601             LIBNAME="lib${LIBNAME}.so.${MAJOR}.${MINOR}"
602             echo "mklib: Making NetBSD PIC shared library: " ${LIBNAME}
603             rm -f ${LIBNAME}
604             ld -x -Bshareable -Bforcearchive -o ${LIBNAME} ${OBJECTS}
605             FINAL_LIBS=${LIBNAME}
606         fi
607         ;;
608
609     'IRIX' | 'IRIX64')
610         if [ $STATIC = 1 ] ; then
611             LIBNAME="lib${LIBNAME}.a"
612             FINAL_LIBS=`make_ar_static_lib rc 0 ${LIBNAME} ${OBJECTS}`
613         else
614             LIBNAME="lib${LIBNAME}.so"  # prefix with "lib", suffix with ".so"
615
616             # examine first object to determine ABI
617             set ${OBJECTS}
618             ABI_O32=`file $1 | grep 'ELF 32-bit'`
619             ABI_N32=`file $1 | grep 'ELF N32'`
620             ABI_N64=`file $1 | grep 'ELF 64-bit'`
621             if [ "${ABI_O32}" ] ; then
622                 OPTS="-32 -shared -all"
623                 ABI="o32-bit"
624             elif [ "${ABI_N32}" ] ; then
625                 OPTS="-n32 -shared -all"
626                 ABI="n32-bit"
627             elif [ "${ABI_N64}" ] ; then
628                 OPTS="-64 -shared -all"
629                 ABI="64-bit"
630             else
631                 echo "Error: Unexpected IRIX ABI!"
632                 exit 1
633             fi
634
635             if [ "${ALTOPTS}" ] ; then
636                 OPTS=${ALTOPTS}
637             fi
638
639             if [ $CPLUSPLUS = 1 ] ; then
640                 LINK="CC"
641             else
642                 LINK="ld"
643             fi
644
645             echo "mklib: Making IRIX " ${ABI} " shared library: " ${LIBNAME}
646             ${LINK} ${OPTS} ${LDFLAGS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
647             FINAL_LIBS=${LIBNAME}
648         fi
649         ;;
650
651     'linux-cygwin')
652         LIBNAME="lib${LIBNAME}.a"
653         echo "mklib: Making linux-cygwin library: " ${LIBNAME}
654         rm -f ${LIBNAME}
655         gnuwin32ar ruv ${LIBNAME} ${OBJECTS}
656         FINAL_LIBS=${LIBNAME}
657         ;;
658
659     'HP-UX')
660         if [ $STATIC = 1 ] ; then
661             LIBNAME="lib${LIBNAME}.a"
662             echo "mklib: Making HP-UX static library: " ${LIBNAME}
663             FINAL_LIBS=`make_ar_static_lib -ruv 0 ${LIBNAME} ${OBJECTS}`
664         else
665             # HP uses a .2 for their current GL/GLU libraries
666             if [ ${LIBNAME} = "GL" -o ${LIBNAME} = "GLU" ] ; then
667                MAJOR=2
668             fi
669             RUNLIB="lib${LIBNAME}.${MAJOR}"
670             DEVLIB="lib${LIBNAME}.sl"
671             echo "mklib: Making HP-UX shared library: " ${RUNLIB} ${DEVLIB}
672             ld -b -o ${RUNLIB} +b ${RUNLIB} ${OBJECTS} ${DEPS}
673             ln -s ${RUNLIB} ${DEVLIB}
674             FINAL_LIBS="${RUNLIB} ${DEVLIB}"
675         fi
676         ;;
677
678     'AIX' )
679         # examine first object to determine ABI
680         set ${OBJECTS}
681         ABI_64=`file $1 | grep '64-bit'`
682         if [ "${ABI_64}" ] ; then
683             X64="-X64"
684             Q64="-q64"
685             OFILE=shr_64.o
686         else
687             OFILE=shr.o  #Want to be consistent with the IBM libGL.a
688         fi
689
690         if [ $STATIC = 1 ] ; then
691             LIBNAME="lib${LIBNAME}.a"
692             echo "mklib: Making AIX static library: " ${LIBNAME}
693             FINAL_LIBS=`make_ar_static_lib -ruv 0 ${LIBNAME} ${OBJECTS}`
694         else
695             EXPFILE="lib${LIBNAME}.exp"
696             LIBNAME="lib${LIBNAME}.a"  # shared objects are still stored in the .a libraries
697             OPTS="-bE:${EXPFILE} -bM:SRE -bnoentry ${Q64}"
698             rm -f ${EXPFILE} ${OFILE}
699             NM="/bin/nm -eC ${X64}"
700             echo "#! /usr/lib/${LIBNAME}" > ${EXPFILE}
701             ${NM} ${OBJECTS} | awk '{
702             if ((($2 == "T") || ($2 == "D") || ($2 == "B")) \
703             && ( substr($1,1,1) != ".")) {
704                     if (substr ($1, 1, 7) != "__sinit" &&
705                             substr ($1, 1, 7) != "__sterm") {
706                             if (substr ($1, 1, 5) == "__tf1")
707                                 print (substr ($1, 7))
708                             else if (substr ($1, 1, 5) == "__tf9")
709                                 print (substr ($1, 15))
710                             else
711                                 print $1
712                         }
713                 }
714             }' | sort -u >> ${EXPFILE}
715
716             if [ "${ALTOPTS}" ] ; then
717                 OPTS=${ALTOPTS}
718             fi
719
720             # On AIX a shared library is linked differently when
721             # you want to dlopen the file
722             if [ $DLOPEN = "1" ] ; then
723                 cc -G ${OPTS} ${LDFLAGS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
724             else
725                 cc ${OPTS} ${LDFLAGS} -o ${OFILE} ${OBJECTS} ${DEPS}
726                 ar ${X64} -r ${LIBNAME} ${OFILE}
727             fi
728
729             FINAL_LIBS="${LIBNAME}"
730         fi
731         ;;
732
733     'OpenSTEP')
734         LIBNAME="lib${LIBNAME}.a"
735         echo "mklib: Making OpenSTEP static library: " ${LIBNAME}
736         libtool -static -o ${LIBNAME} - ${OBJECTS}
737         FINAL_LIBS=${LIBNAME}
738         ;;
739
740     'OSF1')
741         if [ $STATIC = 1 ] ; then
742             LIBNAME="lib${LIBNAME}.a"
743             echo "mklib: Making OSF/1 static library: " ${LIBNAME}
744             FINAL_LIBS=`make_ar_static_lib -ruv 0 ${LIBNAME} ${OBJECTS}`
745         else
746             VERSION="${MAJOR}.${MINOR}"
747             LIBNAME="lib${LIBNAME}.so"
748             echo "mklib: Making OSF/1 shared library: " ${LIBNAME}
749             if [ "x$LINK" = "x" ] ; then
750                 if [ $CPLUSPLUS = 1 ] ; then
751                     LINK=cxx
752                 else
753                     LINK=cc
754                 fi
755             fi
756             rm -f ${LIBNAME}.${VERSION}
757             ${LINK} -o ${LIBNAME}.${VERSION} -shared -set_version ${VERSION} -soname ${LIBNAME}.${VERSION} -expect_unresolved \* -all ${OBJECTS} ${DEPS}
758             ln -sf ${LIBNAME}.${VERSION} ${LIBNAME}
759             FINAL_LIBS="${LIBNAME} ${LIBNAME}.${VERSION}"
760         fi
761         ;;
762
763     'Darwin')
764         if [ $STATIC = 1 ] ; then
765             LIBNAME="lib${LIBNAME}.a"
766             echo "mklib: Making Darwin static library: " ${LIBNAME}
767             LINK="ar"
768             OPTS="-ruvs"
769             if [ "${ALTOPTS}" ] ; then
770                 OPTS=${ALTOPTS}
771             fi
772             ${LINK} ${OPTS} ${LIBNAME} ${OBJECTS}
773             FINAL_LIBS=${LIBNAME}
774         else
775             # On Darwin a .bundle is used for a library that you want to dlopen
776             if [ $DLOPEN = "1" ] ; then
777                 LIBSUFFIX="bundle"
778                 OPTS="${ARCHOPT} -bundle -multiply_defined suppress"
779             else
780                 LIBSUFFIX="dylib"
781                 if [ -z "$ID" ] ; then
782                     ID="lib${LIBNAME}.${MAJOR}.${LIBSUFFIX}"
783                 fi
784                 OPTS="${ARCHOPT} -dynamiclib -multiply_defined suppress -current_version ${MAJOR}.${MINOR}.0 -compatibility_version ${MAJOR}.${MINOR}.0 -install_name ${ID}"
785             fi
786
787             if [ ${EXPORTS} ] ; then
788                 if [ -f ${EXPORTS}".darwin" ] ; then
789                     EXPORTS=$EXPORTS".darwin"
790                 fi
791                 OPTS="${OPTS} -exported_symbols_list ${EXPORTS}"
792             fi
793
794             LINKNAME="lib${LIBNAME}.${MAJOR}.${LIBSUFFIX}"
795             LINKNAME2="lib${LIBNAME}.${LIBSUFFIX}"
796             LIBNAME="lib${LIBNAME}.${MAJOR}.${MINOR}.${LIBSUFFIX}"
797
798             # examine first object to determine ABI
799             set ${OBJECTS}
800             ABIS=`lipo -info $1 | sed s/.*://`
801             for ABI in $ABIS; do
802                 OPTS="${OPTS} -arch ${ABI}"
803             done
804
805             if [ "${ALTOPTS}" ] ; then
806                 OPTS=${ALTOPTS}
807             fi
808
809             # XXX can we always add -isysroot /Developer/SDKs/MacOSX10.4u.sdk
810             # to OPTS here?
811
812             # determine linker
813             if [ $CPLUSPLUS = 1 ] ; then
814                 LINK="g++"
815             else
816                 LINK="cc"
817             fi
818
819             echo "mklib: Making Darwin shared library: " ${LIBNAME}
820
821             ${LINK} ${OPTS} ${LDFLAGS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
822             ln -s ${LIBNAME} ${LINKNAME}
823             ln -s ${LIBNAME} ${LINKNAME2}
824             FINAL_LIBS="${LIBNAME} ${LINKNAME} ${LINKNAME2}"
825         fi
826         ;;
827
828     'LynxOS')
829         LIBNAME="lib${LIBNAME}.a"
830         echo "mklib: Making LynxOS static library: " ${LIBNAME}
831         FINAL_LIBS=`make_ar_static_lib -ru 0 ${LIBNAME} ${OBJECTS}`
832         ;;
833
834     'BeOS')
835         if [ $STATIC = 1 ] ; then
836             LIBNAME="lib${LIBNAME}.a"
837             echo "mklib: Making BeOS static library: " ${LIBNAME}
838             FINAL_LIBS=`make_ar_static_lib -cru 0 ${LIBNAME} ${OBJECTS}`
839         else
840             LIBNAME="lib${LIBNAME}.so"
841             echo "mklib: Making BeOS shared library: " ${LIBNAME}
842             gcc -nostart -Xlinker "-soname=${LIBNAME}" -L/Be/develop/lib/x86 -lbe ${DEPS} ${OBJECTS} -o "${LIBNAME}"
843             mimeset -f "${LIBNAME}"
844             # XXX remove the Mesa3D stuff here since mklib isn't mesa-specific.
845             setversion "${LIBNAME}" -app ${MAJOR} ${MINOR} ${PATCH} -short "Powered by Mesa3D!" -long "Powered by Mesa3D!"
846         fi
847         FINAL_LIBS=${LIBNAME}
848         ;;
849
850     'QNX')
851         LIBNAME="lib${LIBNAME}.a"
852         echo "mklib: Making QNX library: " ${LIBNAME}
853         wlib ${LIBNAME} ${OBJECTS}
854         FINAL_LIBS=${LIBNAME}
855         ;;
856
857     'MorphOS')
858         LIBNAME="lib${LIBNAME}.a"
859         echo "mklib: Making MorphOS library: " ${LIBNAME}
860         ppc-morphos-ar rc ${LIBNAME} ${OBJECTS}
861         FINAL_LIBS="${LIBNAME}"
862         ;;
863
864     'icc' | 'icc-istatic')
865         # Intel C compiler
866         # This should get merged into the Linux code, above, since this isn't
867         # really a different architecture.
868         LIBNAME="lib${LIBNAME}"     # prefix with "lib"
869
870         if [ $STATIC = 1 ] ; then
871             echo "mklib: Making Intel ICC static library: " ${LIBNAME}.a
872             LINK="ar"
873             OPTS="-ruv"
874             if [ "${ALTOPTS}" ] ; then
875                 OPTS=${ALTOPTS}
876             fi
877             # make lib
878             ${LINK} ${OPTS} ${LIBNAME}.a ${OBJECTS}
879             # finish up
880             FINAL_LIBS="${LIBNAME}.a"
881         else
882             if [ $ARCH = icc-istatic ] ; then
883                  OPTS="-shared -i-static -cxxlib-icc"
884             else
885                  OPTS="-shared"
886             fi
887             if [ "${ALTOPTS}" ] ; then
888                 OPTS=${ALTOPTS}
889             fi
890             VERSION="${MAJOR}.${MINOR}.${PATCH}"
891             echo "mklib: Making Intel ICC shared library: " ${LIBNAME}.so.${VERSION}
892
893             if [ $CPLUSPLUS = 1 ] ; then
894                 LINK="icpc"
895             else
896                 LINK="icc"
897             fi
898             # rm any old libs
899             rm -f ${LIBNAME}.so.${VERSION}
900             rm -f ${LIBNAME}.so.${MAJOR}
901             rm -f ${LIBNAME}.so
902             # make lib
903             ${LINK} ${OPTS} ${LDFLAGS} -o ${LIBNAME}.so.${VERSION} ${OBJECTS} ${DEPS}
904             # make usual symlinks
905             ln -s ${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR}
906             ln -s ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so
907             # finish up
908             FINAL_LIBS="${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so"
909         fi
910         ;;
911
912     'aix-gcc')
913         # AIX with gcc
914         if [ $STATIC = 1 ] ; then
915             LIBNAME="lib${LIBNAME}.a"
916             echo "mklib: Making AIX GCC static library: " ${LIBNAME}
917             FINAL_LIBS=`make_ar_static_lib ru 0 ${LIBNAME} ${OBJECTS}`
918         else
919             LIBNAME="lib${LIBNAME}.so"  # prefix with "lib", suffix with ".so"
920             echo "mklib: Making AIX GCC shared library: " ${LIBNAME}
921             # remove old lib
922             rm -f ${LIBNAME}
923             # make the lib
924             gcc -shared -Wl,-G ${OBJECTS} ${DEPS} -o ${LIBNAME}
925             # NOTE: the application linking with this library must specify
926             # the -Wl,-brtl flags to gcc
927             FINAL_LIBS=${LIBNAME}
928         fi
929         ;;
930
931     'ultrix')
932         # XXX untested
933         if [ $STATIC = 0 ] ; then
934             echo "mklib: Warning shared libs not supported on Ultrix"
935         fi
936         LIBNAME="lib${LIBNAME}.a"
937         echo "mklib: Making static library for Ultrix: " ${LIBNAME}
938         FINAL_LIBS=`make_ar_static_lib ru 0 ${LIBNAME} ${OBJECTS}`
939         ;;
940
941      CYGWIN*)
942         # GCC-based environment
943         if [ $NOPREFIX = 1 ] ; then
944             # No "lib" or ".so" part
945             echo "mklib: Making CYGWIN shared library: " ${LIBNAME}
946             OPTS="-shared -Wl,--enable-auto-image-base"
947             if [ "${ALTOPTS}" ] ; then
948                 OPTS=${ALTOPTS}
949             fi
950             rm -f ${LIBNAME}
951             ${LINK} ${OPTS} ${LDFLAGS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
952             FINAL_LIBS=${LIBNAME}
953         else
954         CYGNAME="cyg${LIBNAME}"     # prefix with "cyg"
955         LIBNAME="lib${LIBNAME}"     # prefix with "lib"
956
957         if [ $STATIC = 1 ] ; then
958             LIBNAME=${LIBNAME}.a
959             echo "mklib: Making" $ARCH "static library: " ${LIBNAME}
960             OPTS="-ru"
961             if [ "${ALTOPTS}" ] ; then
962                 OPTS=${ALTOPTS}
963             fi
964             FINAL_LIBS=`make_ar_static_lib ${OPTS} 1 ${LIBNAME} ${OBJECTS}`
965         else
966             OPTS="-shared -Wl,--enable-auto-image-base -Wl,-export-all -Wl,--out-implib=${LIBNAME}-${MAJOR}.dll.a"
967             if [ "${ALTOPTS}" ] ; then
968                 OPTS=${ALTOPTS}
969             fi
970             echo "mklib: Making" $ARCH "shared library: " ${CYGNAME}-${MAJOR}.dll
971
972             if [ $CPLUSPLUS = 1 ] ; then
973                 LINK="g++"
974             else
975                 LINK="gcc"
976             fi
977
978             # rm any old libs
979             rm -f ${CYGNAME}-${MAJOR}.dll
980             rm -f ${LIBNAME}-${MAJOR}.dll.a
981             rm -f ${LIBNAME}.dll.a
982             rm -f ${LIBNAME}.a
983
984             # make lib
985             ${LINK} ${OPTS} ${LDFLAGS} -o ${CYGNAME}-${MAJOR}.dll ${OBJECTS} ${DEPS}
986             # make build fail if link failed
987             es=$?
988             if [ "$es" -ne "0" ]; then
989                 exit $es
990             fi
991             # make usual symlinks
992             ln -s ${LIBNAME}-${MAJOR}.dll.a ${LIBNAME}.dll.a
993             # finish up
994             FINAL_LIBS="${LIBNAME}-${MAJOR}.dll.a ${LIBNAME}.dll.a"
995             # special case for installing in bin
996             FINAL_BINS="${CYGNAME}-${MAJOR}.dll"
997         fi
998         fi
999         ;;
1000
1001     'example')
1002         # If you're adding support for a new architecture, you can
1003         # start with this:
1004         if [ $STATIC = 1 ] ; then
1005             LIBNAME="lib${LIBNAME}.a"
1006             echo "mklib: Making static library for example arch: " ${LIBNAME}
1007             FINAL_LIBS=`make_ar_static_lib rv 0 ${LIBNAME} ${OBJECTS}`
1008         else
1009             LIBNAME="lib${LIBNAME}.so"  # prefix with "lib", suffix with ".so"
1010             echo "mklib: Making shared library for example arch: " ${LIBNAME}
1011             ld -o ${LIBNAME} ${OBJECTS} ${DEPS}
1012             FINAL_LIBS="${LIBNAME}"
1013         fi
1014         ;;
1015
1016     *)
1017         echo "mklib: ERROR: Don't know how to make a static/shared library for" ${ARCH}
1018         echo "mklib: Please add necessary commands to mklib script."
1019         ;;
1020 esac
1021
1022
1023 #
1024 # Put library files into installation directory if specified.
1025 #
1026 if [ ${INSTALLDIR} != "." ] ; then
1027     echo "mklib: Installing" ${FINAL_LIBS} "in" ${INSTALLDIR}
1028     test -d ${INSTALLDIR} || mkdir -p ${INSTALLDIR}
1029     mv ${FINAL_LIBS} ${INSTALLDIR}/
1030 fi