more Darwin tweaks
[mesa.git] / bin / mklib
1 #!/bin/sh
2
3 # Make a shared library.
4 # Basically do a switch/case depending on the OS and make a shared (or static)
5 # library conforming to that OS.
6
7
8 # Usage:
9 # mklib [options] objects ...
10 # Options:
11 # -o LIBRARY specifies the name of resulting library
12 # ("-o GL" for example, might result in "libGL.so" being made)
13 # -major N specifies major version number (default is 1)
14 # -minor N specifies minor version number (default is 0)
15 # -patch N specifies patch version number (default is 0)
16 # -lLIBRARY specifies a dependency on LIBRARY
17 # -LDIR search in DIR for library dependencies
18 # -cplusplus link with C++ runtime
19 # -static make a static library (default is dynamic/shared)
20 # -install DIR move resulting library file(s) to DIR
21 # -arch ARCH override using `uname` to determine architecture
22 # -archopt OPT specify an extra achitecture-specific option OPT
23 #
24 # The library name should just be "GL" or "GLU", etc. The 'lib' prefix
25 # will be added here if needed, as well as the ".so" or ".a" suffix, etc.
26 #
27 # objects should be: foo.o bar.o etc.o
28 #
29 # Environment variables recognized:
30 # CC C compiler command
31 # CXX C++ compiler command
32 #
33
34
35 #
36 # Option defaults
37 #
38 LIBNAME=""
39 MAJOR=1
40 MINOR=0
41 PATCH=""
42 DEPS=""
43 CPLUSPLUS=0
44 STATIC=0
45 INSTALLDIR="."
46 ARCH="auto"
47 ARCHOPT=""
48
49
50 #
51 # Parse arguments
52 #
53 while true
54 do
55 case $1 in
56 '-o') shift 1; LIBNAME=$1;;
57 '-major') shift 1; MAJOR=$1;;
58 '-minor') shift 1; MINOR=$1;;
59 '-patch') shift 1; PATCH=$1;;
60 -l*) DEPS="$DEPS $1";;
61 -L*) DEPS="$DEPS $1";;
62 '-cplusplus') CPLUSPLUS=1;;
63 '-static') STATIC=1;;
64 '-install') shift 1; INSTALLDIR=$1;;
65 '-arch') shift 1; ARCH=$1;;
66 '-archopt') shift 1; ARCHOPT=$1;;
67 -*) echo "mklib: Unknown option: " $1 ; exit 1;;
68 *) break
69 esac
70 shift 1
71 done
72 OBJECTS=$@
73
74 if [ ${ARCH} = "auto" ] ; then
75 ARCH=`uname`
76 fi
77
78
79 #
80 # Error checking
81 #
82 if [ "x${LIBNAME}" = "x" ] ; then
83 echo "mklib: Error: no library name specified"
84 exit 1
85 fi
86 if [ "x${OBJECTS}" = "x" ] ; then
87 echo "mklib: Error: no object files specified"
88 exit 1
89 fi
90
91
92 #
93 # Debugging info
94 #
95 if [ ] ; then
96 echo "-----------------"
97 echo ARCH is $ARCH
98 echo LIBNAME is $LIBNAME
99 echo MAJOR is $MAJOR
100 echo MINOR is $MINOR
101 echo PATCH is $PATCH
102 echo DEPS are $DEPS
103 echo "-----------------"
104 fi
105
106
107 #
108 # OK, make the library now
109 #
110 case $ARCH in
111
112 'Linux' | 'OpenBSD')
113 # GCC-based environment
114 LIBNAME="lib${LIBNAME}" # prefix with "lib"
115
116 if [ $STATIC = 1 ] ; then
117 echo "mklib: Making" $ARCH "static library: " ${LIBNAME}.a
118 LINK="ar"
119 OPTS="-ru"
120 # make lib
121 ${LINK} ${OPTS} ${LIBNAME}.a ${OBJECTS}
122 ranlib ${LIBNAME}.a
123 # finish up
124 FINAL_LIBS=${LIBNAME}.a
125 else
126 if [ $ARCH = 'Linux' ] ; then
127 OPTS="-Xlinker -Bsymbolic -shared -Wl,-soname,${LIBNAME}.so.${MAJOR}"
128 else
129 OPTS="-shared -Wl,-soname,${LIBNAME}.so.${MAJOR}"
130 fi
131 if [ x${PATCH} = "x" ] ; then
132 VERSION="${MAJOR}.${MINOR}"
133 else
134 VERSION="${MAJOR}.${MINOR}.${PATCH}"
135 fi
136
137 echo "mklib: Making" $ARCH "shared library: " ${LIBNAME}.so.${VERSION}
138
139 if [ $CPLUSPLUS = 1 ] ; then
140 LINK="g++"
141 else
142 LINK="gcc"
143 fi
144
145 # rm any old libs
146 rm -f ${LIBNAME}.so.${VERSION}
147 rm -f ${LIBNAME}.so.${MAJOR}
148 rm -f ${LIBNAME}.so
149
150 # make lib
151 ${LINK} ${OPTS} -o ${LIBNAME}.so.${VERSION} ${OBJECTS} ${DEPS}
152 # make usual symlinks
153 ln -s ${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR}
154 ln -s ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so
155 # finish up
156 FINAL_LIBS="${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so"
157 fi
158 ;;
159
160 'SunOS')
161 if [ $STATIC = 1 ] ; then
162 LIBNAME="lib${LIBNAME}.a"
163 echo "mklib: Making SunOS static library: " ${LIBNAME}
164 rm -f ${LIBNAME}
165 ar -ruv ${LIBNAME} ${OBJECTS}
166 FINAL_LIBS=${LIBNAME}
167 else
168 LIBNAME="lib${LIBNAME}.so"
169 echo "mklib: Making SunOS shared library: " ${LIBNAME}
170 # XXX OPTS for gcc should be -shared, but that doesn't work.
171 # Using -G does work though.
172 if [ $CPLUSPLUS = 1 ] ; then
173 # determine linker and options for C++ code
174 if [ "x${CXX}" = "xg++" ] ; then
175 # use g++
176 LINK="g++"
177 OPTS="-G"
178 elif [ "x${CXX}" = "xCC" ] ; then
179 # use Sun CC
180 LINK="CC"
181 OPTS="-G"
182 elif [ "x${CXX}" = "xc++" ] ; then
183 # use Sun c++
184 LINK="c++"
185 OPTS="-G"
186 elif [ `which c++` ] ; then
187 # use Sun c++
188 LINK="c++"
189 OPTS="-G"
190 elif [ `type g++` ] ; then
191 # use g++
192 LINK="g++"
193 OPTS="-G"
194 else
195 echo "mklib: warning: can't find C++ comiler, trying CC."
196 LINK="CC"
197 OPTS="-G"
198 fi
199 elif [ "x${CC}" = "xgcc" ] ; then
200 # use gcc for linking
201 LINK="gcc"
202 OPTS="-G"
203 else
204 # use native Sun linker
205 LINK="ld"
206 OPTS="-G"
207 fi
208 echo "mklib: linker is" ${LINK} ${OPTS}
209 rm -f ${LIBNAME}.${MAJOR} ${LIBNAME}
210 ${LINK} ${OPTS} -o ${LIBNAME}.${MAJOR} ${OBJECTS} ${DEPS}
211 ln -s ${LIBNAME}.${MAJOR} ${LIBNAME}
212 FINAL_LIBS="${LIBNAME}.${MAJOR} ${LIBNAME}"
213 fi
214 ;;
215
216 'FreeBSD')
217 if [ $STATIC = 1 ] ; then
218 STLIB="lib${LIBNAME}.a"
219 echo "mklib: Making FreeBSD static library: " ${STLIB}
220 rm -f ${STLIB}
221 ar cq ${STLIB} ${OBJECTS}
222 ranlib ${STLIB}
223 FINAL_LIBS=${STLIB}
224 else
225 SHLIB="lib${LIBNAME}.so.${MAJOR}.${MINOR}"
226 echo "mklib: Making FreeBSD shared library: " ${SHLIB}
227 rm -f ${SHLIB}
228 ld -Bshareable -o ${SHLIB} ${OBJECTS}
229 # XXX make lib${LIBNAME}.so.${MAJOR} symlink?
230 FINAL_LIBS=${SHLIB}
231 fi
232 ;;
233
234 'NetBSD')
235 if [ $STATIC = 1 ] ; then
236 LIBNAME="lib${LIBNAME}_pic.a"
237 echo "mklib: Making NetBSD PIC static library: " ${LIBNAME}
238 rm -f ${LIBNAME}
239 ar cq ${LIBNAME} ${OBJECTS}
240 ranlib ${LIBNAME}
241 FINAL_LIBS=${LIBNAME}
242 else
243 LIBNAME="lib${LIBNAME}.so.${MAJOR}.${MINOR}"
244 echo "mklib: Making NetBSD PIC shared library: " ${LIBNAME}
245 rm -f ${LIBNAME}
246 ld -x -Bshareable -Bforcearchive -o ${LIBNAME} ${OBJECTS}
247 FINAL_LIBS=${LIBNAME}
248 fi
249 ;;
250
251 'IRIX' | 'IRIX64')
252 if [ $STATIC = 1 ] ; then
253 LIBNAME="lib${LIBNAME}.a"
254 rm -f ${LIBNAME}
255 ar rc ${LIBNAME} ${OBJECTS}
256 FINAL_LIBS=${LIBNAME}
257 else
258 LIBNAME="lib${LIBNAME}.so" # prefix with "lib", suffix with ".so"
259 if [ $ARCHOPT = "64" ] ; then
260 # 64-bit ABI
261 OPTS="-64 -shared -all"
262 echo "mklib: Making IRIX 64-bit shared library: " ${LIBNAME}
263 elif [ $ARCHOPT = "o32" ] ; then
264 # old 32-bit ABI
265 OPTS="-32 -shared -all"
266 echo "mklib: Making IRIX o32-bit shared library: " ${LIBNAME}
267 else
268 # new 32-bit ABI
269 OPTS="-n32 -shared -all"
270 echo "mklib: Making IRIX n32-bit shared library: " ${LIBNAME}
271 fi
272 if [ $CPLUSPLUS = 1 ] ; then
273 LINK="CC"
274 else
275 LINK="ld"
276 fi
277 ${LINK} ${OPTS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
278 FINAL_LIBS=${LIBNAME}
279 fi
280 ;;
281
282 'linux-cygwin')
283 LIBNAME="lib${LIBNAME}.a"
284 echo "mklib: Making linux-cygwin library: " ${LIBNAME}
285 rm -f ${LIBNAME}
286 gnuwin32ar ruv ${LIBNAME} ${OBJECTS}
287 FINAL_LIBS=${LIBNAME}
288 ;;
289
290 'HP-UX')
291 if [ $STATIC = 1 ] ; then
292 LIBNAME="lib${LIBNAME}.a"
293 echo "mklib: Making HPUX static library: " ${LIBNAME}
294 rm -f ${LIBNAME}
295 ar -ruv ${LIBNAME} ${OBJECTS}
296 FINAL_LIBS=${LIBNAME}
297 else
298 RUNLIB="lib${LIBNAME}.${MAJOR}"
299 DEVLIB="lib${LIBNAME}.sl"
300 echo "mklib: Making HPUX stared library: " ${RUNLIB} ${DEVLIB}
301 ld -b -o ${RUNLIB} +b ${RUNLIB} ${OBJECTS} ${DEPS}
302 ln -s ${RUNLIB} ${DEVLIB}
303 FINAL_LIBS="{RUNLIB} ${DEVLIB}"
304 fi
305 ;;
306
307 'AIX')
308 if [ $STATIC = 1 ] ; then
309 LIBNAME="lib${LIBNAME}.a"
310 echo "mklib: Making AIX static library: " ${LIBNAME}
311 ar -ruv ${LIBNAME} ${OBJECTS}
312 FINAL_LIBS=${LIBNAME}
313 else
314 echo "mklib: PROBLEM: AIX shared libs not supported!!!"
315 fi
316 ;;
317
318 'AIX64')
319 if [ $STATIC = 1 ] ; then
320 LIBNAME="lib${LIBNAME}.a"
321 echo "mklib: Making AIX static library: " ${LIBNAME}
322 ar -X64 -ruv ${LIBNAME} ${OBJECTS}
323 FINAL_LIBS=${LIBNAME}
324 else
325 echo "mklib: PROBLEM: AIX64 shared libs not supported!!!"
326 fi
327 ;;
328
329 'OpenSTEP')
330 LIBNAME="lib${LIBNAME}.a"
331 echo "mklib: Making OpenSTEP static library: " ${LIBNAME}
332 libtool -static -o ${LIBNAME} - ${OBJECTS}
333 FINAL_LIBS=${LIBNAME}
334 ;;
335
336 'OSF1')
337 if [ $STATIC = 1 ] ; then
338 LIBNAME="lib${LIBNAME}.a"
339 echo "mklib: Making OSF/1 static library: " ${LIBNAME}
340 rm -f ${LIBNAME}
341 ar -ruv ${LIBNAME} ${OBJECTS}
342 FINAL_LIBS=${LIBNAME}
343 else
344 VERSION="${MAJOR}.${MINOR}"
345 LIBNAME="lib${LIBNAME}.so"
346 echo "mklib: Making OSF/1 shared library: " ${LIBNAME}
347 rm -f ${LIBNAME}.${VERSION}
348 ld -o ${LIBNAME}.${VERSION} -shared -no_archive -set_version ${VERSION} -soname ${LIBNAME}.${VERSION} -expect_unresolved \* -all ${OBJECTS} ${DEPS}
349 ln -sf ${LIBNAME}.${VERSION} ${LIBNAME}
350 FINAL_LIBS="${LIBNAME} ${LIBNAME}.${VERSION}"
351 fi
352 ;;
353
354 'Darwin')
355 if [ $STATIC = 1 ] ; then
356 LIBNAME="lib${LIBNAME}.a"
357 echo "mklib: Making Darwin static library: " ${LIBNAME}
358 LINK="ar"
359 OPTS="-ruv"
360 ${LINK} ${OPTS} ${LIBNAME} ${OBJECTS}
361 FINAL_LIBS=${LIBNAME}
362 else
363 LIBNAME="${LIBNAME}.dylib"
364 echo "mklib: Making Darwin libraries: " ${LIBNAME}
365 FLAGS="-dynamiclib -multiply_defined suppress"
366 if [ $CPLUSPLUS = 1 ] ; then
367 LINK="g++"
368 else
369 LINK="cc"
370 fi
371 ${LINK} ${FLAGS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
372 FINAL_LIBS=${LIBNAME}
373 fi
374 ;;
375
376 'LynxOS')
377 LIBNAME="lib${LIBNAME}.a"
378 echo "mklib: Making LynxOS static library: " ${LIBNAME}
379 rm -f ${LIBNAME}
380 ar ru ${LIBNAME} ${OBJECTS}
381 FINAL_LIBS=${LIBNAME}
382 ;;
383
384 'BeOS')
385 LIBNAME="lib${LIBNAME}.so"
386 echo "mklib: Making BeOS shared library: " ${LIBNAME}
387 gcc -nostart -Xlinker -soname=${LIBNAME} -L/Be/develop/lib/x86 ${OBJECTS} -lbe -o ${LIBNAME}
388 FINAL_LIBS=${LIBNAME}
389 ;;
390
391 'QNX')
392 LIBNAME="lib${LIBNAME}.a"
393 echo "mklib: Making QNX library: " ${LIBNAME}
394 wlib ${LIBNAME} ${OBJECTS}
395 FINAL_LIBS=${LIBNAME}
396 ;;
397
398 'MorphOS')
399 LIBNAME="lib${LIBNAME}.a"
400 echo "mklib: Making MorphOS library: " ${LIBNAME}
401 ppc-morphos-ar rc ${LIBNAME} ${OBJECTS}
402 FINAL_LIBS="${LIBNAME}"
403 ;;
404
405 'icc')
406 # Intel C compiler
407 LIBNAME="lib${LIBNAME}" # prefix with "lib"
408
409 if [ $STATIC = 1 ] ; then
410 echo "mklib: Making Intel ICC static library: " ${LIBNAME}.a
411 LINK="ar"
412 OPTS="-ruv"
413 # make lib
414 ${LINK} ${OPTS} ${LIBNAME}.a ${OBJECTS}
415 # finish up
416 FINAL_LIBS="${LIBNAME}.a"
417 else
418 OPTS="-shared"
419 VERSION="${MAJOR}.${MINOR}.${PATCH}"
420 echo "mklib: Making Intel ICC shared library: " ${LIBNAME}.so.${VERSION}
421
422 if [ $CPLUSPLUS = 1 ] ; then
423 LINK="icc"
424 else
425 LINK="icc"
426 fi
427 # rm any old libs
428 rm -f ${LIBNAME}.so.${VERSION}
429 rm -f ${LIBNAME}.so.${MAJOR}
430 rm -f ${LIBNAME}.so
431 # make lib
432 ${LINK} ${OPTS} -o ${LIBNAME}.so.${VERSION} ${OBJECTS} ${DEPS}
433 # make usual symlinks
434 ln -s ${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR}
435 ln -s ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so
436 # finish up
437 FINAL_LIBS="${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so"
438 fi
439 ;;
440
441 'aix-gcc')
442 # AIX with gcc
443 if [ $STATIC = 1 ] ; then
444 LIBNAME="lib${LIBNAME}.a"
445 echo "mklib: Making AIX GCC static library: " ${LIBNAME}
446 rm -f ${LIBNAME}
447 ar ru ${LIBNAME} ${OBJECTS}
448 FINAL_LIBS=${LIBNAME}
449 else
450 LIBNAME="lib${LIBNAME}.so" # prefix with "lib", suffix with ".so"
451 echo "mklib: Making AIX GCC shared library: " ${LIBNAME}
452 # remove old lib
453 rm -f ${LIBNAME}
454 # make the lib
455 gcc -shared -Wl,-G ${OBJECTS} ${DEPS} -o ${LIBNAME}
456 # NOTE: the application linking with this library must specify
457 # the -Wl,-brtl flags to gcc
458 FINAL_LIBS=${LIBNAME}
459 fi
460 ;;
461
462 'ultrix')
463 # XXX untested
464 if [ $STATIC = 0 ] ; then
465 echo "mklib: Warning shared libs not supported on Ultrix"
466 fi
467 LIBNAME="lib${LIBNAME}.a"
468 echo "mklib: Making static library for Ultrix: " ${LIBNAME}
469 rm -f ${LIBNAME}
470 ar ru ${LIBNAME} ${OBJECTS}
471 FINAL_LIBS="${LIBNAME}"
472 ;;
473
474 'example')
475 # If you're adding support for a new architecture, you can
476 # start with this:
477 if [ $STATIC = 1 ] ; then
478 LIBNAME="lib${LIBNAME}.a"
479 echo "mklib: Making static library for example arch: " ${LIBNAME}
480 rm -f ${LIBNAME}
481 ar rv ${LIBNAME} ${OBJECTS}
482 FINAL_LIBS="${LIBNAME}"
483 else
484 LIBNAME="lib${LIBNAME}.so" # prefix with "lib"
485 echo "mklib: Making shared library for example arch: " ${LIBNAME}
486 ld -o ${LIBNAME} ${OBJECTS} ${DEPS}
487 FINAL_LIBS="${LIBNAME}"
488 fi
489 ;;
490
491 *)
492 echo "mklib: ERROR: Don't know how to make a static/shared library for" ${ARCH}
493 echo "mklib: Please add necessary commands to mklib script."
494 ;;
495 esac
496
497
498 #
499 # Put library files into installation directory if specified.
500 #
501 if [ ${INSTALLDIR} != "." ] ; then
502 echo "mklib: Installing" ${FINAL_LIBS} "in" ${INSTALLDIR}
503 mv ${FINAL_LIBS} ${INSTALLDIR}/
504 fi