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