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.
30 echo 'Usage: mklib [options] objects'
31 echo 'Create a shared library from object files.'
32 echo ' -o LIBRARY specifies the name of the resulting library, without'
33 echo ' the leading "lib" or any suffix.'
34 echo ' (eg: "-o GL" might result in "libGL.so" being made)'
35 echo ' -major N specifies major version number (default is 1)'
36 echo ' -minor N specifies minor version number (default is 0)'
37 echo ' -patch N specifies patch version number (default is 0)'
38 echo ' -lLIBRARY specifies a dependency on LIBRARY'
39 echo ' -LDIR search in DIR for library dependencies'
40 echo ' -linker L explicity specify the linker program to use (eg: gcc, g++)'
41 echo ' Not observed on all systems at this time.'
42 echo ' -cplusplus link with C++ runtime'
43 echo ' -static make a static library (default is dynamic/shared)'
44 echo ' -install DIR put resulting library file(s) in DIR'
45 echo ' -arch ARCH override using `uname` to determine host system'
46 echo ' -archopt OPT specify an extra achitecture-specific option OPT'
47 echo " -noprefix don't prefix library name with 'lib' nor add any suffix"
48 echo ' -exports FILE only export the symbols listed in FILE'
49 echo ' -h, --help display this information and exit'
133 echo "mklib: Unknown option: " $1 ;
137 # This should be the first object file, stop parsing
145 if [ ${ARCH} = "auto" ] ; then
153 if [ "x${LIBNAME}" = "x" ] ; then
154 echo "mklib: Error: no library name specified"
157 if [ "x${OBJECTS}" = "x" ] ; then
158 echo "mklib: Error: no object files specified"
167 echo "-----------------"
169 echo LIBNAME is
$LIBNAME
174 echo "EXPORTS in" $EXPORTS
175 echo "-----------------"
180 # OK, make the library now
187 if [ "x$LINK" = "x" ] ; then
188 # -linker was not specified so set default link command now
189 if [ $CPLUSPLUS = 1 ] ; then
196 if [ $NOPREFIX = 1 ] ; then
197 # No "lib" or ".so" part
198 echo "mklib: Making" $ARCH "shared library: " ${LIBNAME}
199 #OPTS="-shared -Wl,-soname,${LIBNAME}" # soname???
203 ${LINK} ${OPTS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
205 FINAL_LIBS
="${LIBNAME}"
206 elif [ $STATIC = 1 ] ; then
207 LIBNAME
="lib${LIBNAME}" # prefix with "lib"
208 echo "mklib: Making" $ARCH "static library: " ${LIBNAME}.a
212 ${LINK} ${OPTS} ${LIBNAME}.a ${OBJECTS}
215 FINAL_LIBS
=${LIBNAME}.a
217 LIBNAME
="lib${LIBNAME}" # prefix with "lib"
218 if [ $ARCH = 'Linux' ] ; then
219 OPTS
="-Xlinker -Bsymbolic -shared -Wl,-soname,${LIBNAME}.so.${MAJOR}"
221 OPTS
="-shared -Wl,-soname,${LIBNAME}.so.${MAJOR}"
223 if [ $EXPORTS ] ; then
224 #OPTS="${OPTS} -Xlinker --retain-symbols-file ${EXPORTS}"
225 # Make the 'exptmp' file for --version-script option
226 echo "VERSION_${MAJOR}.${MINOR} {" > exptmp
227 echo "global:" >> exptmp
228 sed 's/$/;/' ${EXPORTS} >> exptmp
229 echo "local:" >> exptmp
232 OPTS
="${OPTS} -Xlinker --version-script=exptmp"
233 # exptmp is removed below
236 # Check if objects are 32-bit and we're running in 64-bit
237 # environment. If so, pass -m32 flag to linker.
239 ABI32
=`file $1 | grep 32-bit`
240 if [ "${ABI32}" -a `uname -m` = "x86_64" ] ; then
244 if [ x
${PATCH} = "x" ] ; then
245 VERSION
="${MAJOR}.${MINOR}"
247 VERSION
="${MAJOR}.${MINOR}.${PATCH}"
250 echo "mklib: Making" $ARCH "shared library: " ${LIBNAME}.so.
${VERSION}
253 rm -f ${LIBNAME}.so.
${VERSION}
254 rm -f ${LIBNAME}.so.
${MAJOR}
258 ${LINK} ${OPTS} -o ${LIBNAME}.so.${VERSION} ${OBJECTS} ${DEPS}
259 # make usual symlinks
260 ln -s ${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR}
261 ln -s ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so
263 FINAL_LIBS
="${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so"
269 if [ $STATIC = 1 ] ; then
270 LIBNAME
="lib${LIBNAME}.a"
271 echo "mklib: Making SunOS static library: " ${LIBNAME}
273 ar -ruv ${LIBNAME} ${OBJECTS}
274 FINAL_LIBS
=${LIBNAME}
276 LIBNAME
="lib${LIBNAME}.so"
277 echo "mklib: Making SunOS shared library: " ${LIBNAME}
278 # XXX OPTS for gcc should be -shared, but that doesn't work.
279 # Using -G does work though.
281 if [ "x$LINK" = "x" ] ; then
282 # -linker was not specified, choose default linker now
283 if [ $CPLUSPLUS = 1 ] ; then
284 # determine linker and options for C++ code
285 if [ `which c++` ] ; then
288 elif [ `type g++` ] ; then
292 echo "mklib: warning: can't find C++ comiler, trying CC."
296 # use native Sun linker for C code
300 echo "mklib: linker is" ${LINK} ${OPTS}
301 rm -f ${LIBNAME}.${MAJOR} ${LIBNAME}
302 ${LINK} ${OPTS} -o ${LIBNAME}.${MAJOR} ${OBJECTS} ${DEPS}
303 ln -s ${LIBNAME}.${MAJOR} ${LIBNAME}
304 FINAL_LIBS
="${LIBNAME}.${MAJOR} ${LIBNAME}"
309 if [ $NOPREFIX = 1 ] ; then
310 # No "lib" or ".so" part
311 echo "mklib: Making FreeBSD shared library: " ${LIBNAME}
313 ld
-Bshareable -o ${LIBNAME} ${OBJECTS}
314 FINAL_LIBS
=${LIBNAME}
315 elif [ $STATIC = 1 ] ; then
316 STLIB
="lib${LIBNAME}.a"
317 echo "mklib: Making FreeBSD static library: " ${STLIB}
319 ar cq
${STLIB} ${OBJECTS}
323 SHLIB
="lib${LIBNAME}.so.${MAJOR}.${MINOR}"
324 echo "mklib: Making FreeBSD shared library: " ${SHLIB}
326 ld
-Bshareable -o ${SHLIB} ${OBJECTS}
327 # XXX make lib${LIBNAME}.so.${MAJOR} symlink?
333 if [ $STATIC = 1 ] ; then
334 LIBNAME
="lib${LIBNAME}_pic.a"
335 echo "mklib: Making NetBSD PIC static library: " ${LIBNAME}
337 ar cq
${LIBNAME} ${OBJECTS}
339 FINAL_LIBS
=${LIBNAME}
341 LIBNAME
="lib${LIBNAME}.so.${MAJOR}.${MINOR}"
342 echo "mklib: Making NetBSD PIC shared library: " ${LIBNAME}
344 ld
-x -Bshareable -Bforcearchive -o ${LIBNAME} ${OBJECTS}
345 FINAL_LIBS
=${LIBNAME}
350 if [ $STATIC = 1 ] ; then
351 LIBNAME
="lib${LIBNAME}.a"
353 ar rc
${LIBNAME} ${OBJECTS}
354 FINAL_LIBS
=${LIBNAME}
356 LIBNAME
="lib${LIBNAME}.so" # prefix with "lib", suffix with ".so"
357 # XXX we should run 'file' on the first object file to determine
358 # if it's o32, n32 or 64 format, as we do for Linux above.
359 if [ $ARCHOPT = "64" ] ; then
361 OPTS
="-64 -shared -all"
362 echo "mklib: Making IRIX 64-bit shared library: " ${LIBNAME}
363 elif [ $ARCHOPT = "o32" ] ; then
365 OPTS
="-32 -shared -all"
366 echo "mklib: Making IRIX o32-bit shared library: " ${LIBNAME}
369 OPTS
="-n32 -shared -all"
370 echo "mklib: Making IRIX n32-bit shared library: " ${LIBNAME}
372 if [ $CPLUSPLUS = 1 ] ; then
377 ${LINK} ${OPTS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
378 FINAL_LIBS
=${LIBNAME}
383 LIBNAME
="lib${LIBNAME}.a"
384 echo "mklib: Making linux-cygwin library: " ${LIBNAME}
386 gnuwin32ar ruv
${LIBNAME} ${OBJECTS}
387 FINAL_LIBS
=${LIBNAME}
391 if [ $STATIC = 1 ] ; then
392 LIBNAME
="lib${LIBNAME}.a"
393 echo "mklib: Making HP-UX static library: " ${LIBNAME}
395 ar -ruv ${LIBNAME} ${OBJECTS}
396 FINAL_LIBS
=${LIBNAME}
398 RUNLIB
="lib${LIBNAME}.${MAJOR}"
399 DEVLIB
="lib${LIBNAME}.sl"
400 echo "mklib: Making HP-UX shared library: " ${RUNLIB} ${DEVLIB}
401 ld
-b -o ${RUNLIB} +b ${RUNLIB} ${OBJECTS} ${DEPS}
402 ln -s ${RUNLIB} ${DEVLIB}
403 FINAL_LIBS
="${RUNLIB} ${DEVLIB}"
408 if [ $ARCH = "AIX64" ] ; then
412 if [ $STATIC = 1 ] ; then
413 LIBNAME
="lib${LIBNAME}.a"
414 echo "mklib: Making AIX static library: " ${LIBNAME}
415 ar -ruv ${X64} ${LIBNAME} ${OBJECTS}
416 FINAL_LIBS
=${LIBNAME}
418 EXPFILE
="lib${LIBNAME}.exp"
419 OFILE
=shr.o
#Want to be consistent with the IBM libGL.a
420 LIBNAME
="lib${LIBNAME}.a" # shared objects are still stored in the .a libraries
421 if [ $ARCH = "AIX64" ] ; then
422 OPTS
="-bE:${EXPFILE} -bM:SRE -bnoentry -q64"
424 OPTS
="-bE:${EXPFILE} -bM:SRE -bnoentry"
426 rm -f ${EXPFILE} ${OFILE}
427 NM
="/bin/nm -eC ${X64}"
428 echo "#! /usr/lib/${LIBNAME}" > ${EXPFILE}
429 ${NM} ${OBJECTS} |
awk '{
430 if ((($2 == "T") || ($2 == "D") || ($2 == "B")) \
431 && ( substr($1,1,1) != ".")) {
432 if (substr ($1, 1, 7) != "__sinit" &&
433 substr ($1, 1, 7) != "__sterm") {
434 if (substr ($1, 1, 5) == "__tf1")
435 print (substr ($1, 7))
436 else if (substr ($1, 1, 5) == "__tf9")
437 print (substr ($1, 15))
442 }' |
sort -u >> ${EXPFILE}
443 cc
${OPTS} -o ${OFILE} ${OBJECTS} ${DEPS}
444 ar ${X64} -r ${LIBNAME} ${OFILE}
445 FINAL_LIBS
="${LIBNAME}"
450 LIBNAME
="lib${LIBNAME}.a"
451 echo "mklib: Making OpenSTEP static library: " ${LIBNAME}
452 libtool
-static -o ${LIBNAME} - ${OBJECTS}
453 FINAL_LIBS
=${LIBNAME}
457 if [ $STATIC = 1 ] ; then
458 LIBNAME
="lib${LIBNAME}.a"
459 echo "mklib: Making OSF/1 static library: " ${LIBNAME}
461 ar -ruv ${LIBNAME} ${OBJECTS}
462 FINAL_LIBS
=${LIBNAME}
464 VERSION
="${MAJOR}.${MINOR}"
465 LIBNAME
="lib${LIBNAME}.so"
466 echo "mklib: Making OSF/1 shared library: " ${LIBNAME}
467 if [ "x$LINK" = "x" ] ; then
468 if [ $CPLUSPLUS = 1 ] ; then
474 rm -f ${LIBNAME}.
${VERSION}
475 ${LINK} -o ${LIBNAME}.${VERSION} -shared -set_version ${VERSION} -soname ${LIBNAME}.${VERSION} -expect_unresolved \* -all ${OBJECTS} ${DEPS}
476 ln -sf ${LIBNAME}.${VERSION} ${LIBNAME}
477 FINAL_LIBS
="${LIBNAME} ${LIBNAME}.${VERSION}"
482 if [ $STATIC = 1 ] ; then
483 LIBNAME
="lib${LIBNAME}.a"
484 echo "mklib: Making Darwin static library: " ${LIBNAME}
487 ${LINK} ${OPTS} ${LIBNAME} ${OBJECTS}
488 FINAL_LIBS
=${LIBNAME}
490 LIBNAME
="lib${LIBNAME}.dylib"
491 echo "mklib: Making Darwin shared library: " ${LIBNAME}
492 FLAGS
="-dynamiclib -multiply_defined suppress"
493 if [ $CPLUSPLUS = 1 ] ; then
498 ${LINK} ${FLAGS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
499 FINAL_LIBS
=${LIBNAME}
504 LIBNAME
="lib${LIBNAME}.a"
505 echo "mklib: Making LynxOS static library: " ${LIBNAME}
507 ar ru
${LIBNAME} ${OBJECTS}
508 FINAL_LIBS
=${LIBNAME}
512 if [ $STATIC = 1 ] ; then
513 LIBNAME
="lib${LIBNAME}.a"
514 echo "mklib: Making BeOS static library: " ${LIBNAME}
515 ar -cru "${LIBNAME}" ${OBJECTS}
517 LIBNAME
="lib${LIBNAME}.so"
518 echo "mklib: Making BeOS shared library: " ${LIBNAME}
519 gcc
-nostart -Xlinker "-soname=${LIBNAME}" -L/Be/develop/lib/x86 -lbe ${DEPS} ${OBJECTS} -o "${LIBNAME}"
520 mimeset
-f "${LIBNAME}"
521 # XXX remove the Mesa3D stuff here since mklib isn't mesa-specific.
522 setversion
"${LIBNAME}" -app ${MAJOR} ${MINOR} ${PATCH} -short "Powered by Mesa3D
!" -long "Powered by Mesa3D
!"
524 FINAL_LIBS=${LIBNAME}
528 LIBNAME="lib
${LIBNAME}.a
"
529 echo "mklib
: Making QNX library
: " ${LIBNAME}
530 wlib ${LIBNAME} ${OBJECTS}
531 FINAL_LIBS=${LIBNAME}
535 LIBNAME="lib
${LIBNAME}.a
"
536 echo "mklib
: Making MorphOS library
: " ${LIBNAME}
537 ppc-morphos-ar rc ${LIBNAME} ${OBJECTS}
538 FINAL_LIBS="${LIBNAME}"
543 # This should get merged into the Linux code, above, since this isn't
544 # really a different architecture.
545 LIBNAME="lib
${LIBNAME}" # prefix with "lib
"
547 if [ $STATIC = 1 ] ; then
548 echo "mklib
: Making Intel ICC static library
: " ${LIBNAME}.a
552 ${LINK} ${OPTS} ${LIBNAME}.a ${OBJECTS}
554 FINAL_LIBS="${LIBNAME}.a
"
557 VERSION="${MAJOR}.${MINOR}.${PATCH}"
558 echo "mklib
: Making Intel ICC shared library
: " ${LIBNAME}.so.${VERSION}
560 if [ $CPLUSPLUS = 1 ] ; then
566 rm -f ${LIBNAME}.so.${VERSION}
567 rm -f ${LIBNAME}.so.${MAJOR}
570 ${LINK} ${OPTS} -o ${LIBNAME}.so.${VERSION} ${OBJECTS} ${DEPS}
571 # make usual symlinks
572 ln -s ${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR}
573 ln -s ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so
575 FINAL_LIBS="${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so
"
581 if [ $STATIC = 1 ] ; then
582 LIBNAME="lib
${LIBNAME}.a
"
583 echo "mklib
: Making AIX GCC static library
: " ${LIBNAME}
585 ar ru ${LIBNAME} ${OBJECTS}
586 FINAL_LIBS=${LIBNAME}
588 LIBNAME="lib
${LIBNAME}.so
" # prefix with "lib
", suffix with ".so
"
589 echo "mklib
: Making AIX GCC shared library
: " ${LIBNAME}
593 gcc -shared -Wl,-G ${OBJECTS} ${DEPS} -o ${LIBNAME}
594 # NOTE: the application linking with this library must specify
595 # the -Wl,-brtl flags to gcc
596 FINAL_LIBS=${LIBNAME}
602 if [ $STATIC = 0 ] ; then
603 echo "mklib
: Warning shared libs not supported on Ultrix
"
605 LIBNAME="lib
${LIBNAME}.a
"
606 echo "mklib
: Making static library
for Ultrix
: " ${LIBNAME}
608 ar ru ${LIBNAME} ${OBJECTS}
609 FINAL_LIBS="${LIBNAME}"
613 # GCC-based environment
614 CYGNAME="cyg
${LIBNAME}" # prefix with "cyg
"
615 LIBNAME="lib
${LIBNAME}" # prefix with "lib
"
617 if [ $STATIC = 1 ] ; then
618 echo "mklib
: Making
" $ARCH "static library
: " ${LIBNAME}.a
622 ${LINK} ${OPTS} ${LIBNAME}.a ${OBJECTS}
625 FINAL_LIBS=${LIBNAME}.a
627 OPTS="-shared -Wl,-export-all -Wl,--out-implib=${LIBNAME}-${MAJOR}.dll.a
"
628 echo "mklib
: Making
" $ARCH "shared library
: " ${LIBNAME}-${MAJOR}.dll
630 if [ $CPLUSPLUS = 1 ] ; then
637 rm -f ${LIBNAME}-${MAJOR}.dll
638 rm -f ${LIBNAME}.dll.a
642 ${LINK} ${OPTS} -o ${CYGNAME}-${MAJOR}.dll ${OBJECTS} ${DEPS}
643 # make usual symlinks
644 ln -s ${LIBNAME}-${MAJOR}.dll.a ${LIBNAME}.dll.a
646 FINAL_LIBS="${LIBNAME}-${MAJOR}.dll.a ${LIBNAME}.dll.a
"
647 # special case for installing in bin
648 FINAL_BINS="${CYGNAME}-${MAJOR}.dll
"
653 # If you're adding support for a new architecture, you can
655 if [ $STATIC = 1 ] ; then
656 LIBNAME="lib
${LIBNAME}.a
"
657 echo "mklib
: Making static library
for example arch
: " ${LIBNAME}
659 ar rv ${LIBNAME} ${OBJECTS}
660 FINAL_LIBS="${LIBNAME}"
662 LIBNAME="lib
${LIBNAME}.so
" # prefix with "lib
", suffix with ".so
"
663 echo "mklib
: Making shared library
for example arch
: " ${LIBNAME}
664 ld -o ${LIBNAME} ${OBJECTS} ${DEPS}
665 FINAL_LIBS="${LIBNAME}"
670 echo "mklib
: ERROR
: Don
't know how to make a static/shared library for" ${ARCH}
671 echo "mklib: Please add necessary commands to mklib script."
677 # Put library files into installation directory if specified.
679 if [ ${INSTALLDIR} != "." ] ; then
680 echo "mklib: Installing" ${FINAL_LIBS} "in" ${INSTALLDIR}
681 mv ${FINAL_LIBS} ${INSTALLDIR}/