3 # Make a shared library.
4 # This script should be useful for projects other than Mesa.
5 # Improvements/fixes are welcome.
8 # Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
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:
17 # The above copyright notice and this permission notice shall be included
18 # in all copies or substantial portions of the Software.
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.
54 echo 'Usage: mklib [options] objects'
55 echo 'Create a shared library from object files.'
56 echo ' -o LIBRARY specifies the name of the resulting library, without'
57 echo ' the leading "lib" or any suffix.'
58 echo ' (eg: "-o GL" might result in "libGL.so" being made)'
59 echo ' -major N specifies major version number (default is 1)'
60 echo ' -minor N specifies minor version number (default is 0)'
61 echo ' -patch N specifies patch version number (default is 0)'
62 echo ' -lLIBRARY specifies a dependency on LIBRARY'
63 echo ' -LDIR search in DIR for library dependencies'
64 echo ' -linker L explicity specify the linker program to use (eg: gcc, g++)'
65 echo ' Not observed on all systems at this time.'
66 echo ' -cplusplus link with C++ runtime'
67 echo ' -static make a static library (default is dynamic/shared)'
68 echo ' -dlopen make a shared library suitable for dynamic loading'
69 echo ' -install DIR put resulting library file(s) in DIR'
70 echo ' -arch ARCH override using `uname` to determine host system'
71 echo ' -archopt OPT specify an extra achitecture-specific option OPT'
72 echo " -noprefix don't prefix library name with 'lib' nor add any suffix"
73 echo ' -exports FILE only export the symbols listed in FILE'
74 echo ' -h, --help display this information and exit'
132 echo "mklib: Unknown option: " $1 ;
136 # This should be the first object file, stop parsing
144 if [ ${ARCH} = "auto" ] ; then
152 if [ "x${LIBNAME}" = "x" ] ; then
153 echo "mklib: Error: no library name specified"
156 if [ "x${OBJECTS}" = "x" ] ; then
157 echo "mklib: Error: no object files specified"
166 echo "-----------------"
168 echo LIBNAME is $LIBNAME
173 echo "EXPORTS in" $EXPORTS
174 echo "-----------------"
179 # OK, make the library now
183 'Linux' | 'OpenBSD' | 'GNU' | GNU/*)
186 if [ "x$LINK" = "x" ] ; then
187 # -linker was not specified so set default link command now
188 if [ $CPLUSPLUS = 1 ] ; then
195 if [ $NOPREFIX = 1 ] ; then
196 # No "lib" or ".so" part
197 echo "mklib: Making" $ARCH "shared library: " ${LIBNAME}
198 #OPTS="-shared -Wl,-soname,${LIBNAME}" # soname???
201 # Check if objects are 32-bit and we're running in 64-bit
202 # environment. If so, pass -m32 flag to linker.
204 ABI32=`file $1 | grep 32-bit`
205 if [ "${ABI32}" -a `uname -m` = "x86_64" ] ; then
211 ${LINK} ${OPTS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
213 FINAL_LIBS="${LIBNAME}"
214 elif [ $STATIC = 1 ] ; then
215 LIBNAME="lib${LIBNAME}" # prefix with "lib"
216 echo "mklib: Making" $ARCH "static library: " ${LIBNAME}.a
220 ${LINK} ${OPTS} ${LIBNAME}.a ${OBJECTS}
223 FINAL_LIBS=${LIBNAME}.a
225 LIBNAME="lib${LIBNAME}" # prefix with "lib"
226 case $ARCH in 'Linux' | 'GNU' | GNU/*)
227 OPTS="-Xlinker -Bsymbolic -shared -Wl,-soname,${LIBNAME}.so.${MAJOR}"
230 OPTS="-shared -Wl,-soname,${LIBNAME}.so.${MAJOR}"
233 if [ $EXPORTS ] ; then
234 #OPTS="${OPTS} -Xlinker --retain-symbols-file ${EXPORTS}"
235 # Make the 'exptmp' file for --version-script option
236 echo "VERSION_${MAJOR}.${MINOR} {" > exptmp
237 echo "global:" >> exptmp
238 sed 's/$/;/' ${EXPORTS} >> exptmp
239 echo "local:" >> exptmp
242 OPTS="${OPTS} -Xlinker --version-script=exptmp"
243 # exptmp is removed below
246 # Check if objects are 32-bit and we're running in 64-bit
247 # environment. If so, pass -m32 flag to linker.
249 ABI32=`file $1 | grep 32-bit`
250 if [ "${ABI32}" -a `uname -m` = "x86_64" ] ; then
254 if [ x${PATCH} = "x" ] ; then
255 VERSION="${MAJOR}.${MINOR}"
257 VERSION="${MAJOR}.${MINOR}.${PATCH}"
260 echo "mklib: Making" $ARCH "shared library: " ${LIBNAME}.so.${VERSION}
263 rm -f ${LIBNAME}.so.${VERSION}
264 rm -f ${LIBNAME}.so.${MAJOR}
268 ${LINK} ${OPTS} -o ${LIBNAME}.so.${VERSION} ${OBJECTS} ${DEPS}
269 # make usual symlinks
270 ln -s ${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR}
271 ln -s ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so
273 FINAL_LIBS="${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so"
279 if [ $STATIC = 1 ] ; then
280 LIBNAME="lib${LIBNAME}.a"
281 echo "mklib: Making SunOS static library: " ${LIBNAME}
283 ar -ruv ${LIBNAME} ${OBJECTS}
284 FINAL_LIBS=${LIBNAME}
286 LIBNAME="lib${LIBNAME}.so"
287 echo "mklib: Making SunOS shared library: " ${LIBNAME}
289 if [ "x$LINK" = "x" ] ; then
290 # -linker was not specified, choose default linker now
291 if [ $CPLUSPLUS = 1 ] ; then
292 # determine linker and options for C++ code
293 if [ `which c++` ] ; then
296 elif [ `type g++` ] ; then
300 echo "mklib: warning: can't find C++ comiler, trying CC."
304 # use native Sun linker for C code
310 if [ ${LINK} = "ld" -o ${LINK} = "cc" -o ${LINK} = "CC" ] ; then
311 # SunOS tools, -G to make shared libs
315 # Check if objects are 32-bit and we're running in 64-bit
316 # environment. If so, pass -m32 flag to linker.
318 ABI32=`file $1 | grep 32-bit`
319 if [ "${ABI32}" ] ; then
320 OPTS="-m32 -shared -Wl,-Bdynamic"
322 OPTS="-m64 -shared -Wl,-Bdynamic"
326 # Check if objects are SPARC v9
327 # file says: ELF 64-bit MSB relocatable SPARCV9 Version 1
329 SPARCV9=`file $1 | grep SPARCV9`
330 if [ "${SPARCV9}" ] ; then
331 OPTS="${OPTS} -xarch=v9"
335 #echo "mklib: linker is" ${LINK} ${OPTS}
336 rm -f ${LIBNAME}.${MAJOR} ${LIBNAME}
337 ${LINK} ${OPTS} -o ${LIBNAME}.${MAJOR} ${OBJECTS} ${DEPS}
338 ln -s ${LIBNAME}.${MAJOR} ${LIBNAME}
339 FINAL_LIBS="${LIBNAME}.${MAJOR} ${LIBNAME}"
346 if [ "x$LINK" = "x" ] ; then
347 # -linker was not specified so set default link command now
348 if [ $CPLUSPLUS = 1 ] ; then
355 if [ $NOPREFIX = 1 ] ; then
356 # No "lib" or ".so" part
357 echo "mklib: Making FreeBSD shared library: " ${LIBNAME}
360 ${LINK} ${OPTS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
361 FINAL_LIBS=${LIBNAME}
362 elif [ $STATIC = 1 ] ; then
363 STLIB="lib${LIBNAME}.a"
364 echo "mklib: Making FreeBSD static library: " ${STLIB}
366 ar cq ${STLIB} ${OBJECTS}
370 SHLIB="lib${LIBNAME}.so.${MAJOR}"
371 OPTS="-shared -Wl,-soname,${SHLIB}"
372 echo "mklib: Making FreeBSD shared library: " ${SHLIB}
374 ${LINK} ${OPTS} -o ${SHLIB} ${OBJECTS} ${DEPS}
375 ln -sf ${SHLIB} "lib${LIBNAME}.so"
376 FINAL_LIBS="${SHLIB} lib${LIBNAME}.so"
381 if [ $STATIC = 1 ] ; then
382 LIBNAME="lib${LIBNAME}_pic.a"
383 echo "mklib: Making NetBSD PIC static library: " ${LIBNAME}
385 ar cq ${LIBNAME} ${OBJECTS}
387 FINAL_LIBS=${LIBNAME}
389 LIBNAME="lib${LIBNAME}.so.${MAJOR}.${MINOR}"
390 echo "mklib: Making NetBSD PIC shared library: " ${LIBNAME}
392 ld -x -Bshareable -Bforcearchive -o ${LIBNAME} ${OBJECTS}
393 FINAL_LIBS=${LIBNAME}
398 if [ $STATIC = 1 ] ; then
399 LIBNAME="lib${LIBNAME}.a"
401 ar rc ${LIBNAME} ${OBJECTS}
402 FINAL_LIBS=${LIBNAME}
404 LIBNAME="lib${LIBNAME}.so" # prefix with "lib", suffix with ".so"
406 # examine first object to determine ABI
408 ABI_O32=`file $1 | grep 'ELF 32-bit'`
409 ABI_N32=`file $1 | grep 'ELF N32'`
410 ABI_N64=`file $1 | grep 'ELF 64-bit'`
411 if [ "${ABI_O32}" ] ; then
412 OPTS="-32 -shared -all"
414 elif [ "${ABI_N32}" ] ; then
415 OPTS="-n32 -shared -all"
417 elif [ "${ABI_N64}" ] ; then
418 OPTS="-64 -shared -all"
421 echo "Error: Unexpected IRIX ABI!"
425 if [ $CPLUSPLUS = 1 ] ; then
431 echo "mklib: Making IRIX " ${ABI} " shared library: " ${LIBNAME}
432 ${LINK} ${OPTS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
433 FINAL_LIBS=${LIBNAME}
438 LIBNAME="lib${LIBNAME}.a"
439 echo "mklib: Making linux-cygwin library: " ${LIBNAME}
441 gnuwin32ar ruv ${LIBNAME} ${OBJECTS}
442 FINAL_LIBS=${LIBNAME}
446 if [ $STATIC = 1 ] ; then
447 LIBNAME="lib${LIBNAME}.a"
448 echo "mklib: Making HP-UX static library: " ${LIBNAME}
450 ar -ruv ${LIBNAME} ${OBJECTS}
451 FINAL_LIBS=${LIBNAME}
453 # HP uses a .2 for their current GL/GLU libraries
454 if [ ${LIBNAME} = "GL" -o ${LIBNAME} = "GLU" ] ; then
457 RUNLIB="lib${LIBNAME}.${MAJOR}"
458 DEVLIB="lib${LIBNAME}.sl"
459 echo "mklib: Making HP-UX shared library: " ${RUNLIB} ${DEVLIB}
460 ld -b -o ${RUNLIB} +b ${RUNLIB} ${OBJECTS} ${DEPS}
461 ln -s ${RUNLIB} ${DEVLIB}
462 FINAL_LIBS="${RUNLIB} ${DEVLIB}"
467 # examine first object to determine ABI
469 ABI_64=`file $1 | grep '64-bit'`
470 if [ "${ABI_64}" ] ; then
475 OFILE=shr.o #Want to be consistent with the IBM libGL.a
478 if [ $STATIC = 1 ] ; then
479 LIBNAME="lib${LIBNAME}.a"
480 echo "mklib: Making AIX static library: " ${LIBNAME}
481 ar -ruv ${X64} ${LIBNAME} ${OBJECTS}
482 FINAL_LIBS=${LIBNAME}
484 EXPFILE="lib${LIBNAME}.exp"
485 LIBNAME="lib${LIBNAME}.a" # shared objects are still stored in the .a libraries
486 OPTS="-bE:${EXPFILE} -bM:SRE -bnoentry ${Q64}"
487 rm -f ${EXPFILE} ${OFILE}
488 NM="/bin/nm -eC ${X64}"
489 echo "#! /usr/lib/${LIBNAME}" > ${EXPFILE}
490 ${NM} ${OBJECTS} | awk '{
491 if ((($2 == "T") || ($2 == "D") || ($2 == "B")) \
492 && ( substr($1,1,1) != ".")) {
493 if (substr ($1, 1, 7) != "__sinit" &&
494 substr ($1, 1, 7) != "__sterm") {
495 if (substr ($1, 1, 5) == "__tf1")
496 print (substr ($1, 7))
497 else if (substr ($1, 1, 5) == "__tf9")
498 print (substr ($1, 15))
503 }' | sort -u >> ${EXPFILE}
505 # On AIX a shared library is linked differently when
506 # you want to dlopen the file
507 if [ $DLOPEN = "1" ] ; then
508 cc -G ${OPTS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
510 cc ${OPTS} -o ${OFILE} ${OBJECTS} ${DEPS}
511 ar ${X64} -r ${LIBNAME} ${OFILE}
514 FINAL_LIBS="${LIBNAME}"
519 LIBNAME="lib${LIBNAME}.a"
520 echo "mklib: Making OpenSTEP static library: " ${LIBNAME}
521 libtool -static -o ${LIBNAME} - ${OBJECTS}
522 FINAL_LIBS=${LIBNAME}
526 if [ $STATIC = 1 ] ; then
527 LIBNAME="lib${LIBNAME}.a"
528 echo "mklib: Making OSF/1 static library: " ${LIBNAME}
530 ar -ruv ${LIBNAME} ${OBJECTS}
531 FINAL_LIBS=${LIBNAME}
533 VERSION="${MAJOR}.${MINOR}"
534 LIBNAME="lib${LIBNAME}.so"
535 echo "mklib: Making OSF/1 shared library: " ${LIBNAME}
536 if [ "x$LINK" = "x" ] ; then
537 if [ $CPLUSPLUS = 1 ] ; then
543 rm -f ${LIBNAME}.${VERSION}
544 ${LINK} -o ${LIBNAME}.${VERSION} -shared -set_version ${VERSION} -soname ${LIBNAME}.${VERSION} -expect_unresolved \* -all ${OBJECTS} ${DEPS}
545 ln -sf ${LIBNAME}.${VERSION} ${LIBNAME}
546 FINAL_LIBS="${LIBNAME} ${LIBNAME}.${VERSION}"
551 if [ $STATIC = 1 ] ; then
552 LIBNAME="lib${LIBNAME}.a"
553 echo "mklib: Making Darwin static library: " ${LIBNAME}
556 ${LINK} ${OPTS} ${LIBNAME} ${OBJECTS}
557 FINAL_LIBS=${LIBNAME}
559 # On Darwin a .bundle is used for a library that you want to dlopen
560 if [ $DLOPEN = "1" ] ; then
562 OPTS="${ARCHOPT} -bundle -multiply_defined suppress"
565 OPTS="${ARCHOPT} -dynamiclib -multiply_defined suppress -current_version ${MAJOR}.${MINOR}.0 -compatibility_version ${MAJOR}.${MINOR}.0 -install_name lib${LIBNAME}.${MAJOR}.${LIBSUFFIX}"
567 LINKNAME="lib${LIBNAME}.${LIBSUFFIX}"
568 LIBNAME="lib${LIBNAME}.${MAJOR}.${LIBSUFFIX}"
570 # examine first object to determine ABI
572 ABI_PPC=`file $1 | grep 'object ppc'`
573 ABI_I386=`file $1 | grep 'object i386'`
574 if [ "${ABI_PPC}" ] ; then
575 OPTS="${OPTS} -arch ppc"
577 if [ "${ABI_I386}" ] ; then
578 OPTS="${OPTS} -arch i386"
581 # XXX can we always add -isysroot /Developer/SDKs/MacOSX10.4u.sdk
585 if [ $CPLUSPLUS = 1 ] ; then
591 echo "mklib: Making Darwin shared library: " ${LIBNAME}
592 ${LINK} ${OPTS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
593 ln -s ${LIBNAME} ${LINKNAME}
594 FINAL_LIBS="${LIBNAME} ${LINKNAME}"
599 LIBNAME="lib${LIBNAME}.a"
600 echo "mklib: Making LynxOS static library: " ${LIBNAME}
602 ar ru ${LIBNAME} ${OBJECTS}
603 FINAL_LIBS=${LIBNAME}
607 if [ $STATIC = 1 ] ; then
608 LIBNAME="lib${LIBNAME}.a"
609 echo "mklib: Making BeOS static library: " ${LIBNAME}
610 ar -cru "${LIBNAME}" ${OBJECTS}
612 LIBNAME="lib${LIBNAME}.so"
613 echo "mklib: Making BeOS shared library: " ${LIBNAME}
614 gcc -nostart -Xlinker "-soname=${LIBNAME}" -L/Be/develop/lib/x86 -lbe ${DEPS} ${OBJECTS} -o "${LIBNAME}"
615 mimeset -f "${LIBNAME}"
616 # XXX remove the Mesa3D stuff here since mklib isn't mesa-specific.
617 setversion "${LIBNAME}" -app ${MAJOR} ${MINOR} ${PATCH} -short "Powered by Mesa3D!" -long "Powered by Mesa3D!"
619 FINAL_LIBS=${LIBNAME}
623 LIBNAME="lib${LIBNAME}.a"
624 echo "mklib: Making QNX library: " ${LIBNAME}
625 wlib ${LIBNAME} ${OBJECTS}
626 FINAL_LIBS=${LIBNAME}
630 LIBNAME="lib${LIBNAME}.a"
631 echo "mklib: Making MorphOS library: " ${LIBNAME}
632 ppc-morphos-ar rc ${LIBNAME} ${OBJECTS}
633 FINAL_LIBS="${LIBNAME}"
636 'icc' | 'icc-istatic')
638 # This should get merged into the Linux code, above, since this isn't
639 # really a different architecture.
640 LIBNAME="lib${LIBNAME}" # prefix with "lib"
642 if [ $STATIC = 1 ] ; then
643 echo "mklib: Making Intel ICC static library: " ${LIBNAME}.a
647 ${LINK} ${OPTS} ${LIBNAME}.a ${OBJECTS}
649 FINAL_LIBS="${LIBNAME}.a"
651 if [ $ARCH = icc-istatic ] ; then
652 OPTS="-shared -i-static -cxxlib-icc"
656 VERSION="${MAJOR}.${MINOR}.${PATCH}"
657 echo "mklib: Making Intel ICC shared library: " ${LIBNAME}.so.${VERSION}
659 if [ $CPLUSPLUS = 1 ] ; then
665 rm -f ${LIBNAME}.so.${VERSION}
666 rm -f ${LIBNAME}.so.${MAJOR}
669 ${LINK} ${OPTS} -o ${LIBNAME}.so.${VERSION} ${OBJECTS} ${DEPS}
670 # make usual symlinks
671 ln -s ${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR}
672 ln -s ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so
674 FINAL_LIBS="${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so"
680 if [ $STATIC = 1 ] ; then
681 LIBNAME="lib${LIBNAME}.a"
682 echo "mklib: Making AIX GCC static library: " ${LIBNAME}
684 ar ru ${LIBNAME} ${OBJECTS}
685 FINAL_LIBS=${LIBNAME}
687 LIBNAME="lib${LIBNAME}.so" # prefix with "lib", suffix with ".so"
688 echo "mklib: Making AIX GCC shared library: " ${LIBNAME}
692 gcc -shared -Wl,-G ${OBJECTS} ${DEPS} -o ${LIBNAME}
693 # NOTE: the application linking with this library must specify
694 # the -Wl,-brtl flags to gcc
695 FINAL_LIBS=${LIBNAME}
701 if [ $STATIC = 0 ] ; then
702 echo "mklib: Warning shared libs not supported on Ultrix"
704 LIBNAME="lib${LIBNAME}.a"
705 echo "mklib: Making static library for Ultrix: " ${LIBNAME}
707 ar ru ${LIBNAME} ${OBJECTS}
708 FINAL_LIBS="${LIBNAME}"
712 # GCC-based environment
713 CYGNAME="cyg${LIBNAME}" # prefix with "cyg"
714 LIBNAME="lib${LIBNAME}" # prefix with "lib"
716 if [ $STATIC = 1 ] ; then
717 echo "mklib: Making" $ARCH "static library: " ${LIBNAME}.a
721 ${LINK} ${OPTS} ${LIBNAME}.a ${OBJECTS}
724 FINAL_LIBS=${LIBNAME}.a
726 OPTS="-shared -Wl,-export-all -Wl,--out-implib=${LIBNAME}-${MAJOR}.dll.a"
727 echo "mklib: Making" $ARCH "shared library: " ${LIBNAME}-${MAJOR}.dll
729 if [ $CPLUSPLUS = 1 ] ; then
736 rm -f ${LIBNAME}-${MAJOR}.dll
737 rm -f ${LIBNAME}.dll.a
741 ${LINK} ${OPTS} -o ${CYGNAME}-${MAJOR}.dll ${OBJECTS} ${DEPS}
742 # make usual symlinks
743 ln -s ${LIBNAME}-${MAJOR}.dll.a ${LIBNAME}.dll.a
745 FINAL_LIBS="${LIBNAME}-${MAJOR}.dll.a ${LIBNAME}.dll.a"
746 # special case for installing in bin
747 FINAL_BINS="${CYGNAME}-${MAJOR}.dll"
752 # If you're adding support for a new architecture, you can
754 if [ $STATIC = 1 ] ; then
755 LIBNAME="lib${LIBNAME}.a"
756 echo "mklib: Making static library for example arch: " ${LIBNAME}
758 ar rv ${LIBNAME} ${OBJECTS}
759 FINAL_LIBS="${LIBNAME}"
761 LIBNAME="lib${LIBNAME}.so" # prefix with "lib", suffix with ".so"
762 echo "mklib: Making shared library for example arch: " ${LIBNAME}
763 ld -o ${LIBNAME} ${OBJECTS} ${DEPS}
764 FINAL_LIBS="${LIBNAME}"
769 echo "mklib: ERROR: Don't know how to make a static/shared library for" ${ARCH}
770 echo "mklib: Please add necessary commands to mklib script."
776 # Put library files into installation directory if specified.
778 if [ ${INSTALLDIR} != "." ] ; then
779 echo "mklib: Installing" ${FINAL_LIBS} "in" ${INSTALLDIR}
780 mv ${FINAL_LIBS} ${INSTALLDIR}/