blob: 496a69410ea533ff87ed743708968270b9c6fce2 [file] [log] [blame]
Brian Paul8c20c7b2003-06-01 16:21:45 +00001#!/bin/sh
2
3# Make a shared library.
Brian Paul5396ab22004-02-12 14:48:52 +00004# Basically do a switch/case depending on the OS and make a shared (or static)
5# library conforming to that OS.
Brian Paul8c20c7b2003-06-01 16:21:45 +00006
7
8# Usage:
9# mklib [options] objects ...
10# Options:
Brian Paul1a644b32003-12-06 17:57:12 +000011# -o LIBRARY specifies the name of resulting library
12# ("-o GL" for example, might result in "libGL.so" being made)
Brian Paul8c20c7b2003-06-01 16:21:45 +000013# -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)
Brian Paul5396ab22004-02-12 14:48:52 +000020# -install DIR move resulting library file(s) to DIR
Brian Paul8c20c7b2003-06-01 16:21:45 +000021# -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#
38LIBNAME=""
39MAJOR=1
40MINOR=0
Brian Paul6e450f22004-02-21 18:08:41 +000041PATCH=""
Brian Paul8c20c7b2003-06-01 16:21:45 +000042DEPS=""
43CPLUSPLUS=0
44STATIC=0
45INSTALLDIR="."
46ARCH="auto"
47ARCHOPT=""
48
49
50#
51# Parse arguments
52#
53while true
54do
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
71done
72OBJECTS=$@
73
74if [ ${ARCH} = "auto" ] ; then
75 ARCH=`uname`
76fi
77
78
79#
80# Error checking
81#
82if [ "x${LIBNAME}" = "x" ] ; then
83 echo "mklib: Error: no library name specified"
84 exit 1
85fi
86if [ "x${OBJECTS}" = "x" ] ; then
87 echo "mklib: Error: no object files specified"
88 exit 1
89fi
90
91
92#
93# Debugging info
94#
95if [ ] ; 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 "-----------------"
104fi
105
106
107#
108# OK, make the library now
109#
110case $ARCH in
111
Brian Paul12d6cae2004-01-10 22:12:21 +0000112 'Linux' | 'OpenBSD')
113 # GCC-based environment
Brian Paul8c20c7b2003-06-01 16:21:45 +0000114 LIBNAME="lib${LIBNAME}" # prefix with "lib"
Brian Paul8c20c7b2003-06-01 16:21:45 +0000115
Brian Paul1c4b7112003-10-10 17:58:38 +0000116 if [ $STATIC = 1 ] ; then
Brian Paul5b9a9d42004-01-17 18:31:12 +0000117 echo "mklib: Making" $ARCH "static library: " ${LIBNAME}.a
Brian Paul1c4b7112003-10-10 17:58:38 +0000118 LINK="ar"
Brian Paul6e450f22004-02-21 18:08:41 +0000119 OPTS="-ru"
Brian Paul1c4b7112003-10-10 17:58:38 +0000120 # make lib
121 ${LINK} ${OPTS} ${LIBNAME}.a ${OBJECTS}
Brian Paul6e450f22004-02-21 18:08:41 +0000122 ranlib ${LIBNAME}.a
Brian Paul1c4b7112003-10-10 17:58:38 +0000123 # finish up
Brian Paul6e450f22004-02-21 18:08:41 +0000124 FINAL_LIBS=${LIBNAME}.a
Brian Paul1c4b7112003-10-10 17:58:38 +0000125 else
Brian Paul5b9a9d42004-01-17 18:31:12 +0000126 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
Brian Paul6e450f22004-02-21 18:08:41 +0000131 if [ x${PATCH} = "x" ] ; then
132 VERSION="${MAJOR}.${MINOR}"
133 else
134 VERSION="${MAJOR}.${MINOR}.${PATCH}"
135 fi
Brian Paul8c20c7b2003-06-01 16:21:45 +0000136
Brian Paul5396ab22004-02-12 14:48:52 +0000137 echo "mklib: Making" $ARCH "shared library: " ${LIBNAME}.so.${VERSION}
Brian Paul8c20c7b2003-06-01 16:21:45 +0000138
Brian Paul1c4b7112003-10-10 17:58:38 +0000139 if [ $CPLUSPLUS = 1 ] ; then
140 LINK="g++"
141 else
142 LINK="gcc"
143 fi
Brian Paul8c20c7b2003-06-01 16:21:45 +0000144
Brian Paul1c4b7112003-10-10 17:58:38 +0000145 # 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
Brian Paul8c20c7b2003-06-01 16:21:45 +0000158 ;;
159
160 'SunOS')
Brian Paul5396ab22004-02-12 14:48:52 +0000161 if [ $STATIC = 1 ] ; then
162 LIBNAME="lib${LIBNAME}.a"
163 echo "mklib: Making SunOS static library: " ${LIBNAME}
164 rm -f ${LIBNAME}
165 ar ru ${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"
Brian Paul4b603442004-01-14 23:18:47 +0000202 OPTS="-G"
Brian Paul8c20c7b2003-06-01 16:21:45 +0000203 else
Brian Paul5396ab22004-02-12 14:48:52 +0000204 # use native Sun linker
205 LINK="ld"
Brian Paul1a644b32003-12-06 17:57:12 +0000206 OPTS="-G"
Brian Paul8c20c7b2003-06-01 16:21:45 +0000207 fi
Brian Paul5396ab22004-02-12 14:48:52 +0000208 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}"
Brian Paul8c20c7b2003-06-01 16:21:45 +0000213 fi
Brian Paul8c20c7b2003-06-01 16:21:45 +0000214 ;;
215
216 'FreeBSD')
Brian Paul5396ab22004-02-12 14:48:52 +0000217 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
Brian Paul8c20c7b2003-06-01 16:21:45 +0000232 ;;
233
Brian Paul8c20c7b2003-06-01 16:21:45 +0000234 'NetBSD')
Brian Paul5396ab22004-02-12 14:48:52 +0000235 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
Brian Paul8c20c7b2003-06-01 16:21:45 +0000249 ;;
250
Brian Paulf8c31fc2004-01-29 15:21:47 +0000251 'IRIX' | 'IRIX64')
Brian Paul5396ab22004-02-12 14:48:52 +0000252 if [ $STATIC = 1 ] ; then
253 LIBNAME="lib${LIBNAME}.a"
254 rm -f ${LIBNAME}
255 ar rc ${LIBNAME} ${OBJECTS}
256 FINAL_LIBS=${LIBNAME}
Brian Paul8c20c7b2003-06-01 16:21:45 +0000257 else
Brian Paul5396ab22004-02-12 14:48:52 +0000258 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}
Brian Paul8c20c7b2003-06-01 16:21:45 +0000279 fi
Brian Paul8c20c7b2003-06-01 16:21:45 +0000280 ;;
281
Brian Paul8c20c7b2003-06-01 16:21:45 +0000282 'linux-cygwin')
283 LIBNAME="lib${LIBNAME}.a"
284 echo "mklib: Making linux-cygwin library: " ${LIBNAME}
Brian Paul5396ab22004-02-12 14:48:52 +0000285 rm -f ${LIBNAME}
Brian Paul8c20c7b2003-06-01 16:21:45 +0000286 gnuwin32ar ruv ${LIBNAME} ${OBJECTS}
287 FINAL_LIBS=${LIBNAME}
288 ;;
289
290 'HPUX')
Brian Paul5396ab22004-02-12 14:48:52 +0000291 if [ $STATIC = 1 ] ; then
292 LIBNAME="lib${LIBNAME}.a"
293 echo "mklib: Making HPUX static library: " ${LIBNAME}
294 rm -f ${LIBNAME}
295 ar ru ${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
Brian Paul8c20c7b2003-06-01 16:21:45 +0000305 ;;
306
307 'OpenSTEP')
308 LIBNAME="lib${LIBNAME}.a"
309 echo "mklib: Making OpenSTEP static library: " ${LIBNAME}
310 libtool -static -o ${LIBNAME} - ${OBJECTS}
311 FINAL_LIBS=${LIBNAME}
312 ;;
313
314 'OSF1')
Brian Paul5396ab22004-02-12 14:48:52 +0000315 if [ $STATIC = 1 ] ; then
316 LIBNAME="lib${LIBNAME}.a"
317 echo "mklib: Making OSF/1 static library: " ${LIBNAME}
318 rm -f ${LIBNAME}
319 ar clqz ${LIBNAME} ${OBJECTS}
320 FINAL_LIBS=${LIBNAME}
321 else
322 VERSION="${MAJOR}.${MINOR}"
323 LIBNAME="lib${LIBNAME}.so"
324 echo "mklib: Making OSF/1 shared library: " ${LIBNAME}
325 rm -f ${LIBNAME}.${VERSION}
326 ld -o ${LIBNAME}.${VERSION} -shared -no_archive -set_version ${VERSION} -soname ${LIBNAME}.${VERSION} -expect_unresolved \* -all ${OBJECTS} ${DEPS}
327 ln -sf ${LIBNAME}.${VERSION} ${LIBNAME}
328 FINAL_LIBS="${LIBNAME} ${LIBNAME}.${VERSION}"
329 fi
Brian Paul8c20c7b2003-06-01 16:21:45 +0000330 ;;
331
332 'Darwin')
Brian Paul6e450f22004-02-21 18:08:41 +0000333 VERSION="${MAJOR}.${MINOR}.${PATCH}"
Brian Paul8c20c7b2003-06-01 16:21:45 +0000334 LIBNAME="lib${LIBNAME}.dylib"
335 ARNAME="lib${LIBNAME}.dylib.a"
336 echo "mklib: Making Darwin libraries: " ${LIBNAME} ${ARNAME}
337 FLAGS="-dynamiclib -multiply_defined suppress"
338 cc ${FLAGS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
339 # also make regular .a files,
340 # provided by Danek Duvall (duvall@dhduvall.student.princeton.edu)
Brian Paul5396ab22004-02-12 14:48:52 +0000341 rm -f ${ARNAME}
Brian Paul8c20c7b2003-06-01 16:21:45 +0000342 ar ruv ${ARNAME} ${OBJECTS}
343 ranlib ${ARNAME}
344 FINAL_LIBS="${ARNAME} ${LIBNAME}"
345 ;;
346
347 'LynxOS')
348 LIBNAME="lib${LIBNAME}.a"
Brian Paul5396ab22004-02-12 14:48:52 +0000349 echo "mklib: Making LynxOS static library: " ${LIBNAME}
350 rm -f ${LIBNAME}
Brian Paul8c20c7b2003-06-01 16:21:45 +0000351 ar ru ${LIBNAME} ${OBJECTS}
352 FINAL_LIBS=${LIBNAME}
353 ;;
354
355 'BeOS')
356 LIBNAME="lib${LIBNAME}.so"
357 echo "mklib: Making BeOS shared library: " ${LIBNAME}
358 gcc -nostart -Xlinker -soname=${LIBNAME} -L/Be/develop/lib/x86 ${OBJECTS} -lbe -o ${LIBNAME}
359 FINAL_LIBS=${LIBNAME}
360 ;;
361
362 'QNX')
363 LIBNAME="lib${LIBNAME}.a"
364 echo "mklib: Making QNX library: " ${LIBNAME}
365 wlib ${LIBNAME} ${OBJECTS}
366 FINAL_LIBS=${LIBNAME}
367 ;;
368
Brian Paul65e2ab32003-10-27 18:13:37 +0000369 'MorphOS')
370 LIBNAME="lib${LIBNAME}.a"
371 echo "mklib: Making MorphOS library: " ${LIBNAME}
372 ppc-morphos-ar rc ${LIBNAME} ${OBJECTS}
373 FINAL_LIBS="${LIBNAME}"
374 ;;
375
Brian Paulb3b725b2003-12-15 16:14:55 +0000376 'icc')
377 # Intel C compiler
378 LIBNAME="lib${LIBNAME}" # prefix with "lib"
379
380 if [ $STATIC = 1 ] ; then
381 echo "mklib: Making Intel ICC static library: " ${LIBNAME}.a
382 LINK="ar"
383 OPTS="-ruv"
384 # make lib
385 ${LINK} ${OPTS} ${LIBNAME}.a ${OBJECTS}
386 # finish up
387 FINAL_LIBS="${LIBNAME}.a"
388 else
389 OPTS="-shared"
390 VERSION="${MAJOR}.${MINOR}.${PATCH}"
391
392 echo "mklib: Making Intel ICC shared library: " ${LIBNAME}.so.${VERSION}
393
394 if [ $CPLUSPLUS = 1 ] ; then
395 LINK="icc"
396 else
397 LINK="icc"
398 fi
399
400 # rm any old libs
401 rm -f ${LIBNAME}.so.${VERSION}
402 rm -f ${LIBNAME}.so.${MAJOR}
403 rm -f ${LIBNAME}.so
404
405 # make lib
406 ${LINK} ${OPTS} -o ${LIBNAME}.so.${VERSION} ${OBJECTS} ${DEPS}
407 # make usual symlinks
408 ln -s ${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR}
409 ln -s ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so
410 # finish up
411 FINAL_LIBS="${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so"
412 fi
413 ;;
414
Brian Paul12d6cae2004-01-10 22:12:21 +0000415 'aix-gcc')
416 # AIX with gcc
Brian Paul5396ab22004-02-12 14:48:52 +0000417 if [ $STATIC = 1 ] ; then
418 LIBNAME="lib${LIBNAME}.a"
419 echo "mklib: Making AIX GCC static library: " ${LIBNAME}
420 rm -f ${LIBNAME}
421 ar ru ${LIBNAME} ${OBJECTS}
422 FINAL_LIBS=${LIBNAME}
423 else
424 LIBNAME="lib${LIBNAME}.so" # prefix with "lib", suffix with ".so"
425 echo "mklib: Making AIX GCC shared library: " ${LIBNAME}
426 # remove old lib
427 rm -f ${LIBNAME}
428 # make the lib
429 gcc -shared -Wl,-G ${OBJECTS} ${DEPS} -o ${LIBNAME}
430
431 # NOTE: the application linking with this library must specify
432 # the -Wl,-brtl flags to gcc
433
434 FINAL_LIBS=${LIBNAME}
435 fi
436 ;;
437
438 'ultrix')
439 # XXX untested
440 if [ $STATIC = 0 ] ; then
441 echo "mklib: Warning shared libs not supported on Ultrix"
442 fi
443 LIBNAME="lib${LIBNAME}.a"
444 echo "mklib: Making static library for Ultrix: " ${LIBNAME}
Brian Paul12d6cae2004-01-10 22:12:21 +0000445 rm -f ${LIBNAME}
Brian Paul5396ab22004-02-12 14:48:52 +0000446 ar ru ${LIBNAME} ${OBJECTS}
447 FINAL_LIBS="${LIBNAME}"
Brian Paul12d6cae2004-01-10 22:12:21 +0000448 ;;
Brian Paulb3b725b2003-12-15 16:14:55 +0000449
Brian Paul8c20c7b2003-06-01 16:21:45 +0000450 'example')
451 # If you're adding support for a new architecture, you can
452 # start with this:
Brian Paul5396ab22004-02-12 14:48:52 +0000453 if [ $STATIC = 1 ] ; then
454 LIBNAME="lib${LIBNAME}.a"
455 echo "mklib: Making static library for example arch: " ${LIBNAME}
456 rm -f ${LIBNAME}
457 ar rv ${LIBNAME} ${OBJECTS}
458 FINAL_LIBS="${LIBNAME}"
459 else
460 LIBNAME="lib${LIBNAME}.so" # prefix with "lib"
461 echo "mklib: Making shared library for example arch: " ${LIBNAME}
462 ld -o ${LIBNAME} ${OBJECTS} ${DEPS}
463 FINAL_LIBS="${LIBNAME}"
464 fi
Brian Paul8c20c7b2003-06-01 16:21:45 +0000465 ;;
466
467 *)
Brian Paul5396ab22004-02-12 14:48:52 +0000468 echo "mklib: ERROR: Don't know how to make a static/shared library for" ${ARCH}
469 echo "mklib: Please add necessary commands to mklib script."
Brian Paul8c20c7b2003-06-01 16:21:45 +0000470 ;;
471esac
472
473
474#
475# Put library files into installation directory if specified.
476#
477if [ ${INSTALLDIR} != "." ] ; then
478 echo "mklib: Installing" ${FINAL_LIBS} "in" ${INSTALLDIR}
479 mv ${FINAL_LIBS} ${INSTALLDIR}/
480fi