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-2005 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.
53 echo 'Usage: mklib [options] objects'
54 echo 'Create a shared library from object files.'
55 echo ' -o LIBRARY specifies the name of the resulting library, without'
56 echo ' the leading "lib" or any suffix.'
57 echo ' (eg: "-o GL" might result in "libGL.so" being made)'
58 echo ' -major N specifies major version number (default is 1)'
59 echo ' -minor N specifies minor version number (default is 0)'
60 echo ' -patch N specifies patch version number (default is 0)'
61 echo ' -lLIBRARY specifies a dependency on LIBRARY'
62 echo ' -LDIR search in DIR for library dependencies'
63 echo ' -linker L explicity specify the linker program to use (eg: gcc, g++)'
64 echo ' Not observed on all systems at this time.'
65 echo ' -cplusplus link with C++ runtime'
66 echo ' -static make a static library (default is dynamic/shared)'
67 echo ' -install DIR put resulting library file(s) in DIR'
68 echo ' -arch ARCH override using `uname` to determine host system'
69 echo ' -archopt OPT specify an extra achitecture-specific option OPT'
70 echo " -noprefix don't prefix library name with 'lib' nor add any suffix"
71 echo ' -exports FILE only export the symbols listed in FILE'
72 echo ' -h, --help display this information and exit'
127 echo "mklib: Unknown option: " $1 ;
131 # This should be the first object file, stop parsing
139 if [ ${ARCH} = "auto" ] ; then
147 if [ "x${LIBNAME}" = "x" ] ; then
148 echo "mklib: Error: no library name specified"
151 if [ "x${OBJECTS}" = "x" ] ; then
152 echo "mklib: Error: no object files specified"
161 echo "-----------------"
163 echo LIBNAME is
$LIBNAME
168 echo "EXPORTS in" $EXPORTS
169 echo "-----------------"
174 # OK, make the library now
181 if [ "x$LINK" = "x" ] ; then
182 # -linker was not specified so set default link command now
183 if [ $CPLUSPLUS = 1 ] ; then
190 if [ $NOPREFIX = 1 ] ; then
191 # No "lib" or ".so" part
192 echo "mklib: Making" $ARCH "shared library: " ${LIBNAME}
193 #OPTS="-shared -Wl,-soname,${LIBNAME}" # soname???
196 # Check if objects are 32-bit and we're running in 64-bit
197 # environment. If so, pass -m32 flag to linker.
199 ABI32
=`file $1 | grep 32-bit`
200 if [ "${ABI32}" -a `uname -m` = "x86_64" ] ; then
206 ${LINK} ${OPTS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
208 FINAL_LIBS
="${LIBNAME}"
209 elif [ $STATIC = 1 ] ; then
210 LIBNAME
="lib${LIBNAME}" # prefix with "lib"
211 echo "mklib: Making" $ARCH "static library: " ${LIBNAME}.a
213 ar -ru ${LIBNAME}.a
${OBJECTS}
216 FINAL_LIBS
=${LIBNAME}.a
218 LIBNAME
="lib${LIBNAME}" # prefix with "lib"
219 if [ $ARCH = 'Linux' ] ; then
220 OPTS
="-Xlinker -Bsymbolic -shared -Wl,-soname,${LIBNAME}.so.${MAJOR}"
222 OPTS
="-shared -Wl,-soname,${LIBNAME}.so.${MAJOR}"
224 if [ $EXPORTS ] ; then
225 #OPTS="${OPTS} -Xlinker --retain-symbols-file ${EXPORTS}"
226 # Make the 'exptmp' file for --version-script option
227 echo "VERSION_${MAJOR}.${MINOR} {" > exptmp
228 echo "global:" >> exptmp
229 sed 's/$/;/' ${EXPORTS} >> exptmp
230 echo "local:" >> exptmp
233 OPTS
="${OPTS} -Xlinker --version-script=exptmp"
234 # exptmp is removed below
237 # Check if objects are 32-bit and we're running in 64-bit
238 # environment. If so, pass -m32 flag to linker.
240 ABI32
=`file $1 | grep 32-bit`
241 if [ "${ABI32}" -a `uname -m` = "x86_64" ] ; then
245 if [ x
${PATCH} = "x" ] ; then
246 VERSION
="${MAJOR}.${MINOR}"
248 VERSION
="${MAJOR}.${MINOR}.${PATCH}"
251 echo "mklib: Making" $ARCH "shared library: " ${LIBNAME}.so.
${VERSION}
254 rm -f ${LIBNAME}.so.
${VERSION}
255 rm -f ${LIBNAME}.so.
${MAJOR}
259 ${LINK} ${OPTS} -o ${LIBNAME}.so.${VERSION} ${OBJECTS} ${DEPS}
260 # make usual symlinks
261 ln -s ${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR}
262 ln -s ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so
264 FINAL_LIBS
="${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so"
270 if [ $STATIC = 1 ] ; then
271 LIBNAME
="lib${LIBNAME}.a"
272 echo "mklib: Making SunOS static library: " ${LIBNAME}
274 ar -ruv ${LIBNAME} ${OBJECTS}
275 FINAL_LIBS
=${LIBNAME}
277 LIBNAME
="lib${LIBNAME}.so"
278 echo "mklib: Making SunOS shared library: " ${LIBNAME}
279 # XXX OPTS for gcc should be -shared, but that doesn't work.
280 # Using -G does work though.
282 if [ "x$LINK" = "x" ] ; then
283 # -linker was not specified, choose default linker now
284 if [ $CPLUSPLUS = 1 ] ; then
285 # determine linker and options for C++ code
286 if [ `which c++` ] ; then
289 elif [ `type g++` ] ; then
293 echo "mklib: warning: can't find C++ comiler, trying CC."
297 # use native Sun linker for C code
301 echo "mklib: linker is" ${LINK} ${OPTS}
302 rm -f ${LIBNAME}.${MAJOR} ${LIBNAME}
303 ${LINK} ${OPTS} -o ${LIBNAME}.${MAJOR} ${OBJECTS} ${DEPS}
304 ln -s ${LIBNAME}.${MAJOR} ${LIBNAME}
305 FINAL_LIBS
="${LIBNAME}.${MAJOR} ${LIBNAME}"
312 if [ "x$LINK" = "x" ] ; then
313 # -linker was not specified so set default link command now
314 if [ $CPLUSPLUS = 1 ] ; then
321 if [ $NOPREFIX = 1 ] ; then
322 # No "lib" or ".so" part
323 echo "mklib: Making FreeBSD shared library: " ${LIBNAME}
326 ${LINK} ${OPTS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
327 FINAL_LIBS
=${LIBNAME}
328 elif [ $STATIC = 1 ] ; then
329 STLIB
="lib${LIBNAME}.a"
330 echo "mklib: Making FreeBSD static library: " ${STLIB}
332 ar cq
${STLIB} ${OBJECTS}
336 SHLIB
="lib${LIBNAME}.so.${MAJOR}"
337 OPTS
="-shared -Wl,-soname,${SHLIB}"
338 echo "mklib: Making FreeBSD shared library: " ${SHLIB}
340 ${LINK} ${OPTS} -o ${SHLIB} ${OBJECTS} ${DEPS}
341 ln -sf ${SHLIB} "lib${LIBNAME}.so"
342 FINAL_LIBS
="${SHLIB} lib${LIBNAME}.so"
347 if [ $STATIC = 1 ] ; then
348 LIBNAME
="lib${LIBNAME}_pic.a"
349 echo "mklib: Making NetBSD PIC static library: " ${LIBNAME}
351 ar cq
${LIBNAME} ${OBJECTS}
353 FINAL_LIBS
=${LIBNAME}
355 LIBNAME
="lib${LIBNAME}.so.${MAJOR}.${MINOR}"
356 echo "mklib: Making NetBSD PIC shared library: " ${LIBNAME}
358 ld
-x -Bshareable -Bforcearchive -o ${LIBNAME} ${OBJECTS}
359 FINAL_LIBS
=${LIBNAME}
364 if [ $STATIC = 1 ] ; then
365 LIBNAME
="lib${LIBNAME}.a"
367 ar rc
${LIBNAME} ${OBJECTS}
368 FINAL_LIBS
=${LIBNAME}
370 LIBNAME
="lib${LIBNAME}.so" # prefix with "lib", suffix with ".so"
371 # XXX we should run 'file' on the first object file to determine
372 # if it's o32, n32 or 64 format, as we do for Linux above.
373 if [ $ARCHOPT = "64" ] ; then
375 OPTS
="-64 -shared -all"
376 echo "mklib: Making IRIX 64-bit shared library: " ${LIBNAME}
377 elif [ $ARCHOPT = "o32" ] ; then
379 OPTS
="-32 -shared -all"
380 echo "mklib: Making IRIX o32-bit shared library: " ${LIBNAME}
383 OPTS
="-n32 -shared -all"
384 echo "mklib: Making IRIX n32-bit shared library: " ${LIBNAME}
386 if [ $CPLUSPLUS = 1 ] ; then
391 ${LINK} ${OPTS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
392 FINAL_LIBS
=${LIBNAME}
397 LIBNAME
="lib${LIBNAME}.a"
398 echo "mklib: Making linux-cygwin library: " ${LIBNAME}
400 gnuwin32ar ruv
${LIBNAME} ${OBJECTS}
401 FINAL_LIBS
=${LIBNAME}
405 if [ $STATIC = 1 ] ; then
406 LIBNAME
="lib${LIBNAME}.a"
407 echo "mklib: Making HP-UX static library: " ${LIBNAME}
409 ar -ruv ${LIBNAME} ${OBJECTS}
410 FINAL_LIBS
=${LIBNAME}
412 RUNLIB
="lib${LIBNAME}.${MAJOR}"
413 DEVLIB
="lib${LIBNAME}.sl"
414 echo "mklib: Making HP-UX shared library: " ${RUNLIB} ${DEVLIB}
415 ld
-b -o ${RUNLIB} +b ${RUNLIB} ${OBJECTS} ${DEPS}
416 ln -s ${RUNLIB} ${DEVLIB}
417 FINAL_LIBS
="${RUNLIB} ${DEVLIB}"
422 if [ $ARCH = "AIX64" ] ; then
426 if [ $STATIC = 1 ] ; then
427 LIBNAME
="lib${LIBNAME}.a"
428 echo "mklib: Making AIX static library: " ${LIBNAME}
430 ar -ruv ${X64} ${LIBNAME} ${OBJECTS}
431 FINAL_LIBS
=${LIBNAME}
433 EXPFILE
="lib${LIBNAME}.exp"
434 OFILE
=shr.o
#Want to be consistent with the IBM libGL.a
435 LIBNAME
="lib${LIBNAME}.a" # shared objects are still stored in the .a libraries
436 if [ $ARCH = "AIX64" ] ; then
437 OPTS
="-bE:${EXPFILE} -bM:SRE -bnoentry -q64"
439 OPTS
="-bE:${EXPFILE} -bM:SRE -bnoentry"
441 rm -f ${EXPFILE} ${OFILE}
442 NM
="/bin/nm -eC ${X64}"
443 echo "#! /usr/lib/${LIBNAME}" > ${EXPFILE}
444 ${NM} ${OBJECTS} |
awk '{
445 if ((($2 == "T") || ($2 == "D") || ($2 == "B")) \
446 && ( substr($1,1,1) != ".")) {
447 if (substr ($1, 1, 7) != "__sinit" &&
448 substr ($1, 1, 7) != "__sterm") {
449 if (substr ($1, 1, 5) == "__tf1")
450 print (substr ($1, 7))
451 else if (substr ($1, 1, 5) == "__tf9")
452 print (substr ($1, 15))
457 }' |
sort -u >> ${EXPFILE}
458 cc
${OPTS} -o ${OFILE} ${OBJECTS} ${DEPS}
459 ar ${X64} -r ${LIBNAME} ${OFILE}
460 FINAL_LIBS
="${LIBNAME}"
465 LIBNAME
="lib${LIBNAME}.a"
466 echo "mklib: Making OpenSTEP static library: " ${LIBNAME}
467 libtool
-static -o ${LIBNAME} - ${OBJECTS}
468 FINAL_LIBS
=${LIBNAME}
472 if [ $STATIC = 1 ] ; then
473 LIBNAME
="lib${LIBNAME}.a"
474 echo "mklib: Making OSF/1 static library: " ${LIBNAME}
476 ar -ruv ${LIBNAME} ${OBJECTS}
477 FINAL_LIBS
=${LIBNAME}
479 VERSION
="${MAJOR}.${MINOR}"
480 LIBNAME
="lib${LIBNAME}.so"
481 echo "mklib: Making OSF/1 shared library: " ${LIBNAME}
482 if [ "x$LINK" = "x" ] ; then
483 if [ $CPLUSPLUS = 1 ] ; then
489 rm -f ${LIBNAME}.
${VERSION}
490 ${LINK} -o ${LIBNAME}.${VERSION} -shared -set_version ${VERSION} -soname ${LIBNAME}.${VERSION} -expect_unresolved \* -all ${OBJECTS} ${DEPS}
491 ln -sf ${LIBNAME}.${VERSION} ${LIBNAME}
492 FINAL_LIBS
="${LIBNAME} ${LIBNAME}.${VERSION}"
497 if [ $STATIC = 1 ] ; then
498 LIBNAME
="lib${LIBNAME}.a"
499 echo "mklib: Making Darwin static library: " ${LIBNAME}
502 ${LINK} ${OPTS} ${LIBNAME} ${OBJECTS}
503 FINAL_LIBS
=${LIBNAME}
505 LIBNAME
="lib${LIBNAME}.dylib"
506 echo "mklib: Making Darwin shared library: " ${LIBNAME}
507 FLAGS
="-dynamiclib -multiply_defined suppress -current_version ${MAJOR}.${MINOR}.0 -compatibility_version ${MAJOR}.${MINOR}.0"
508 if [ $CPLUSPLUS = 1 ] ; then
513 ${LINK} ${FLAGS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
514 FINAL_LIBS
=${LIBNAME}
519 LIBNAME
="lib${LIBNAME}.a"
520 echo "mklib: Making LynxOS static library: " ${LIBNAME}
522 ar ru
${LIBNAME} ${OBJECTS}
523 FINAL_LIBS
=${LIBNAME}
527 if [ $STATIC = 1 ] ; then
528 LIBNAME
="lib${LIBNAME}.a"
529 echo "mklib: Making BeOS static library: " ${LIBNAME}
530 ar -cru "${LIBNAME}" ${OBJECTS}
532 LIBNAME
="lib${LIBNAME}.so"
533 echo "mklib: Making BeOS shared library: " ${LIBNAME}
534 gcc
-nostart -Xlinker "-soname=${LIBNAME}" -L/Be/develop/lib/x86 -lbe ${DEPS} ${OBJECTS} -o "${LIBNAME}"
535 mimeset
-f "${LIBNAME}"
536 # XXX remove the Mesa3D stuff here since mklib isn't mesa-specific.
537 setversion
"${LIBNAME}" -app ${MAJOR} ${MINOR} ${PATCH} -short "Powered by Mesa3D
!" -long "Powered by Mesa3D
!"
539 FINAL_LIBS=${LIBNAME}
543 LIBNAME="lib
${LIBNAME}.a
"
544 echo "mklib
: Making QNX library
: " ${LIBNAME}
545 wlib ${LIBNAME} ${OBJECTS}
546 FINAL_LIBS=${LIBNAME}
550 LIBNAME="lib
${LIBNAME}.a
"
551 echo "mklib
: Making MorphOS library
: " ${LIBNAME}
552 ppc-morphos-ar rc ${LIBNAME} ${OBJECTS}
553 FINAL_LIBS="${LIBNAME}"
558 # This should get merged into the Linux code, above, since this isn't
559 # really a different architecture.
560 LIBNAME="lib
${LIBNAME}" # prefix with "lib
"
562 if [ $STATIC = 1 ] ; then
563 echo "mklib
: Making Intel ICC static library
: " ${LIBNAME}.a
567 ${LINK} ${OPTS} ${LIBNAME}.a ${OBJECTS}
569 FINAL_LIBS="${LIBNAME}.a
"
572 VERSION="${MAJOR}.${MINOR}.${PATCH}"
573 echo "mklib
: Making Intel ICC shared library
: " ${LIBNAME}.so.${VERSION}
575 if [ $CPLUSPLUS = 1 ] ; then
581 rm -f ${LIBNAME}.so.${VERSION}
582 rm -f ${LIBNAME}.so.${MAJOR}
585 ${LINK} ${OPTS} -o ${LIBNAME}.so.${VERSION} ${OBJECTS} ${DEPS}
586 # make usual symlinks
587 ln -s ${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR}
588 ln -s ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so
590 FINAL_LIBS="${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so
"
596 if [ $STATIC = 1 ] ; then
597 LIBNAME="lib
${LIBNAME}.a
"
598 echo "mklib
: Making AIX GCC static library
: " ${LIBNAME}
600 ar ru ${LIBNAME} ${OBJECTS}
601 FINAL_LIBS=${LIBNAME}
603 LIBNAME="lib
${LIBNAME}.so
" # prefix with "lib
", suffix with ".so
"
604 echo "mklib
: Making AIX GCC shared library
: " ${LIBNAME}
608 gcc -shared -Wl,-G ${OBJECTS} ${DEPS} -o ${LIBNAME}
609 # NOTE: the application linking with this library must specify
610 # the -Wl,-brtl flags to gcc
611 FINAL_LIBS=${LIBNAME}
617 if [ $STATIC = 0 ] ; then
618 echo "mklib
: Warning shared libs not supported on Ultrix
"
620 LIBNAME="lib
${LIBNAME}.a
"
621 echo "mklib
: Making static library
for Ultrix
: " ${LIBNAME}
623 ar ru ${LIBNAME} ${OBJECTS}
624 FINAL_LIBS="${LIBNAME}"
628 # GCC-based environment
629 CYGNAME="cyg
${LIBNAME}" # prefix with "cyg
"
630 LIBNAME="lib
${LIBNAME}" # prefix with "lib
"
632 if [ $STATIC = 1 ] ; then
633 echo "mklib
: Making
" $ARCH "static library
: " ${LIBNAME}.a
637 ${LINK} ${OPTS} ${LIBNAME}.a ${OBJECTS}
640 FINAL_LIBS=${LIBNAME}.a
642 OPTS="-shared -Wl,-export-all -Wl,--out-implib=${LIBNAME}-${MAJOR}.dll.a
"
643 echo "mklib
: Making
" $ARCH "shared library
: " ${LIBNAME}-${MAJOR}.dll
645 if [ $CPLUSPLUS = 1 ] ; then
652 rm -f ${LIBNAME}-${MAJOR}.dll
653 rm -f ${LIBNAME}.dll.a
657 ${LINK} ${OPTS} -o ${CYGNAME}-${MAJOR}.dll ${OBJECTS} ${DEPS}
658 # make usual symlinks
659 ln -s ${LIBNAME}-${MAJOR}.dll.a ${LIBNAME}.dll.a
661 FINAL_LIBS="${LIBNAME}-${MAJOR}.dll.a ${LIBNAME}.dll.a
"
662 # special case for installing in bin
663 FINAL_BINS="${CYGNAME}-${MAJOR}.dll
"
668 # If you're adding support for a new architecture, you can
670 if [ $STATIC = 1 ] ; then
671 LIBNAME="lib
${LIBNAME}.a
"
672 echo "mklib
: Making static library
for example arch
: " ${LIBNAME}
674 ar rv ${LIBNAME} ${OBJECTS}
675 FINAL_LIBS="${LIBNAME}"
677 LIBNAME="lib
${LIBNAME}.so
" # prefix with "lib
", suffix with ".so
"
678 echo "mklib
: Making shared library
for example arch
: " ${LIBNAME}
679 ld -o ${LIBNAME} ${OBJECTS} ${DEPS}
680 FINAL_LIBS="${LIBNAME}"
685 echo "mklib
: ERROR
: Don
't know how to make a static/shared library for" ${ARCH}
686 echo "mklib: Please add necessary commands to mklib script."
692 # Put library files into installation directory if specified.
694 if [ ${INSTALLDIR} != "." ] ; then
695 echo "mklib: Installing" ${FINAL_LIBS} "in" ${INSTALLDIR}
696 mv ${FINAL_LIBS} ${INSTALLDIR}/