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