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