blob: 05b07f6060db68291443290fa22cb01beef3b52e [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
41PATCH=0
42DEPS=""
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"
119 OPTS="-ruv"
120 # make lib
121 ${LINK} ${OPTS} ${LIBNAME}.a ${OBJECTS}
122 # finish up
Brian Paul5396ab22004-02-12 14:48:52 +0000123 FINAL_LIBS=${LIBNAME}
Brian Paul1c4b7112003-10-10 17:58:38 +0000124 else
Brian Paul5b9a9d42004-01-17 18:31:12 +0000125 if [ $ARCH = 'Linux' ] ; then
126 OPTS="-Xlinker -Bsymbolic -shared -Wl,-soname,${LIBNAME}.so.${MAJOR}"
127 else
128 OPTS="-shared -Wl,-soname,${LIBNAME}.so.${MAJOR}"
129 fi
Brian Paul1c4b7112003-10-10 17:58:38 +0000130 VERSION="${MAJOR}.${MINOR}.${PATCH}"
Brian Paul8c20c7b2003-06-01 16:21:45 +0000131
Brian Paul5396ab22004-02-12 14:48:52 +0000132 echo "mklib: Making" $ARCH "shared library: " ${LIBNAME}.so.${VERSION}
Brian Paul8c20c7b2003-06-01 16:21:45 +0000133
Brian Paul1c4b7112003-10-10 17:58:38 +0000134 if [ $CPLUSPLUS = 1 ] ; then
135 LINK="g++"
136 else
137 LINK="gcc"
138 fi
Brian Paul8c20c7b2003-06-01 16:21:45 +0000139
Brian Paul1c4b7112003-10-10 17:58:38 +0000140 # rm any old libs
141 rm -f ${LIBNAME}.so.${VERSION}
142 rm -f ${LIBNAME}.so.${MAJOR}
143 rm -f ${LIBNAME}.so
144
145 # make lib
146 ${LINK} ${OPTS} -o ${LIBNAME}.so.${VERSION} ${OBJECTS} ${DEPS}
147 # make usual symlinks
148 ln -s ${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR}
149 ln -s ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so
150 # finish up
151 FINAL_LIBS="${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so"
152 fi
Brian Paul8c20c7b2003-06-01 16:21:45 +0000153 ;;
154
155 'SunOS')
Brian Paul5396ab22004-02-12 14:48:52 +0000156 if [ $STATIC = 1 ] ; then
157 LIBNAME="lib${LIBNAME}.a"
158 echo "mklib: Making SunOS static library: " ${LIBNAME}
159 rm -f ${LIBNAME}
160 ar ru ${LIBNAME} ${OBJECTS}
161 FINAL_LIBS=${LIBNAME}
162 else
163 LIBNAME="lib${LIBNAME}.so"
164 echo "mklib: Making SunOS shared library: " ${LIBNAME}
165 # XXX OPTS for gcc should be -shared, but that doesn't work.
166 # Using -G does work though.
167 if [ $CPLUSPLUS = 1 ] ; then
168 # determine linker and options for C++ code
169 if [ "x${CXX}" = "xg++" ] ; then
170 # use g++
171 LINK="g++"
172 OPTS="-G"
173 elif [ "x${CXX}" = "xCC" ] ; then
174 # use Sun CC
175 LINK="CC"
176 OPTS="-G"
177 elif [ "x${CXX}" = "xc++" ] ; then
178 # use Sun c++
179 LINK="c++"
180 OPTS="-G"
181 elif [ `which c++` ] ; then
182 # use Sun c++
183 LINK="c++"
184 OPTS="-G"
185 elif [ `type g++` ] ; then
186 # use g++
187 LINK="g++"
188 OPTS="-G"
189 else
190 echo "mklib: warning: can't find C++ comiler, trying CC."
191 LINK="CC"
192 OPTS="-G"
193 fi
194 elif [ "x${CC}" = "xgcc" ] ; then
195 # use gcc for linking
196 LINK="gcc"
Brian Paul4b603442004-01-14 23:18:47 +0000197 OPTS="-G"
Brian Paul8c20c7b2003-06-01 16:21:45 +0000198 else
Brian Paul5396ab22004-02-12 14:48:52 +0000199 # use native Sun linker
200 LINK="ld"
Brian Paul1a644b32003-12-06 17:57:12 +0000201 OPTS="-G"
Brian Paul8c20c7b2003-06-01 16:21:45 +0000202 fi
Brian Paul5396ab22004-02-12 14:48:52 +0000203 echo "mklib: linker is" ${LINK} ${OPTS}
204 rm -f ${LIBNAME}.${MAJOR} ${LIBNAME}
205 ${LINK} ${OPTS} -o ${LIBNAME}.${MAJOR} ${OBJECTS} ${DEPS}
206 ln -s ${LIBNAME}.${MAJOR} ${LIBNAME}
207 FINAL_LIBS="${LIBNAME}.${MAJOR} ${LIBNAME}"
Brian Paul8c20c7b2003-06-01 16:21:45 +0000208 fi
Brian Paul8c20c7b2003-06-01 16:21:45 +0000209 ;;
210
211 'FreeBSD')
Brian Paul5396ab22004-02-12 14:48:52 +0000212 if [ $STATIC = 1 ] ; then
213 STLIB="lib${LIBNAME}.a"
214 echo "mklib: Making FreeBSD static library: " ${STLIB}
215 rm -f ${STLIB}
216 ar cq ${STLIB} ${OBJECTS}
217 ranlib ${STLIB}
218 FINAL_LIBS=${STLIB}
219 else
220 SHLIB="lib${LIBNAME}.so.${MAJOR}.${MINOR}"
221 echo "mklib: Making FreeBSD shared library: " ${SHLIB}
222 rm -f ${SHLIB}
223 ld -Bshareable -o ${SHLIB} ${OBJECTS}
224 # XXX make lib${LIBNAME}.so.${MAJOR} symlink?
225 FINAL_LIBS=${SHLIB}
226 fi
Brian Paul8c20c7b2003-06-01 16:21:45 +0000227 ;;
228
Brian Paul8c20c7b2003-06-01 16:21:45 +0000229 'NetBSD')
Brian Paul5396ab22004-02-12 14:48:52 +0000230 if [ $STATIC = 1 ] ; then
231 LIBNAME="lib${LIBNAME}_pic.a"
232 echo "mklib: Making NetBSD PIC static library: " ${LIBNAME}
233 rm -f ${LIBNAME}
234 ar cq ${LIBNAME} ${OBJECTS}
235 ranlib ${LIBNAME}
236 FINAL_LIBS=${LIBNAME}
237 else
238 LIBNAME="lib${LIBNAME}.so.${MAJOR}.${MINOR}"
239 echo "mklib: Making NetBSD PIC shared library: " ${LIBNAME}
240 rm -f ${LIBNAME}
241 ld -x -Bshareable -Bforcearchive -o ${LIBNAME} ${OBJECTS}
242 FINAL_LIBS=${LIBNAME}
243 fi
Brian Paul8c20c7b2003-06-01 16:21:45 +0000244 ;;
245
Brian Paulf8c31fc2004-01-29 15:21:47 +0000246 'IRIX' | 'IRIX64')
Brian Paul5396ab22004-02-12 14:48:52 +0000247 if [ $STATIC = 1 ] ; then
248 LIBNAME="lib${LIBNAME}.a"
249 rm -f ${LIBNAME}
250 ar rc ${LIBNAME} ${OBJECTS}
251 FINAL_LIBS=${LIBNAME}
Brian Paul8c20c7b2003-06-01 16:21:45 +0000252 else
Brian Paul5396ab22004-02-12 14:48:52 +0000253 LIBNAME="lib${LIBNAME}.so" # prefix with "lib", suffix with ".so"
254 if [ $ARCHOPT = "64" ] ; then
255 # 64-bit ABI
256 OPTS="-64 -shared -all"
257 echo "mklib: Making IRIX 64-bit shared library: " ${LIBNAME}
258 elif [ $ARCHOPT = "o32" ] ; then
259 # old 32-bit ABI
260 OPTS="-32 -shared -all"
261 echo "mklib: Making IRIX o32-bit shared library: " ${LIBNAME}
262 else
263 # new 32-bit ABI
264 OPTS="-n32 -shared -all"
265 echo "mklib: Making IRIX n32-bit shared library: " ${LIBNAME}
266 fi
267 if [ $CPLUSPLUS = 1 ] ; then
268 LINK="CC"
269 else
270 LINK="ld"
271 fi
272 ${LINK} ${OPTS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
273 FINAL_LIBS=${LIBNAME}
Brian Paul8c20c7b2003-06-01 16:21:45 +0000274 fi
Brian Paul8c20c7b2003-06-01 16:21:45 +0000275 ;;
276
Brian Paul8c20c7b2003-06-01 16:21:45 +0000277 'linux-cygwin')
278 LIBNAME="lib${LIBNAME}.a"
279 echo "mklib: Making linux-cygwin library: " ${LIBNAME}
Brian Paul5396ab22004-02-12 14:48:52 +0000280 rm -f ${LIBNAME}
Brian Paul8c20c7b2003-06-01 16:21:45 +0000281 gnuwin32ar ruv ${LIBNAME} ${OBJECTS}
282 FINAL_LIBS=${LIBNAME}
283 ;;
284
285 'HPUX')
Brian Paul5396ab22004-02-12 14:48:52 +0000286 if [ $STATIC = 1 ] ; then
287 LIBNAME="lib${LIBNAME}.a"
288 echo "mklib: Making HPUX static library: " ${LIBNAME}
289 rm -f ${LIBNAME}
290 ar ru ${LIBNAME} ${OBJECTS}
291 FINAL_LIBS=${LIBNAME}
292 else
293 RUNLIB="lib${LIBNAME}.${MAJOR}"
294 DEVLIB="lib${LIBNAME}.sl"
295 echo "mklib: Making HPUX stared library: " ${RUNLIB} ${DEVLIB}
296 ld -b -o ${RUNLIB} +b ${RUNLIB} ${OBJECTS} ${DEPS}
297 ln -s ${RUNLIB} ${DEVLIB}
298 FINAL_LIBS="{RUNLIB} ${DEVLIB}"
299 fi
Brian Paul8c20c7b2003-06-01 16:21:45 +0000300 ;;
301
302 'OpenSTEP')
303 LIBNAME="lib${LIBNAME}.a"
304 echo "mklib: Making OpenSTEP static library: " ${LIBNAME}
305 libtool -static -o ${LIBNAME} - ${OBJECTS}
306 FINAL_LIBS=${LIBNAME}
307 ;;
308
309 'OSF1')
Brian Paul5396ab22004-02-12 14:48:52 +0000310 if [ $STATIC = 1 ] ; then
311 LIBNAME="lib${LIBNAME}.a"
312 echo "mklib: Making OSF/1 static library: " ${LIBNAME}
313 rm -f ${LIBNAME}
314 ar clqz ${LIBNAME} ${OBJECTS}
315 FINAL_LIBS=${LIBNAME}
316 else
317 VERSION="${MAJOR}.${MINOR}"
318 LIBNAME="lib${LIBNAME}.so"
319 echo "mklib: Making OSF/1 shared library: " ${LIBNAME}
320 rm -f ${LIBNAME}.${VERSION}
321 ld -o ${LIBNAME}.${VERSION} -shared -no_archive -set_version ${VERSION} -soname ${LIBNAME}.${VERSION} -expect_unresolved \* -all ${OBJECTS} ${DEPS}
322 ln -sf ${LIBNAME}.${VERSION} ${LIBNAME}
323 FINAL_LIBS="${LIBNAME} ${LIBNAME}.${VERSION}"
324 fi
Brian Paul8c20c7b2003-06-01 16:21:45 +0000325 ;;
326
327 'Darwin')
328 VERSION="${MAJOR}.${MINOR}.${TINY}"
329 LIBNAME="lib${LIBNAME}.dylib"
330 ARNAME="lib${LIBNAME}.dylib.a"
331 echo "mklib: Making Darwin libraries: " ${LIBNAME} ${ARNAME}
332 FLAGS="-dynamiclib -multiply_defined suppress"
333 cc ${FLAGS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
334 # also make regular .a files,
335 # provided by Danek Duvall (duvall@dhduvall.student.princeton.edu)
Brian Paul5396ab22004-02-12 14:48:52 +0000336 rm -f ${ARNAME}
Brian Paul8c20c7b2003-06-01 16:21:45 +0000337 ar ruv ${ARNAME} ${OBJECTS}
338 ranlib ${ARNAME}
339 FINAL_LIBS="${ARNAME} ${LIBNAME}"
340 ;;
341
342 'LynxOS')
343 LIBNAME="lib${LIBNAME}.a"
Brian Paul5396ab22004-02-12 14:48:52 +0000344 echo "mklib: Making LynxOS static library: " ${LIBNAME}
345 rm -f ${LIBNAME}
Brian Paul8c20c7b2003-06-01 16:21:45 +0000346 ar ru ${LIBNAME} ${OBJECTS}
347 FINAL_LIBS=${LIBNAME}
348 ;;
349
350 'BeOS')
351 LIBNAME="lib${LIBNAME}.so"
352 echo "mklib: Making BeOS shared library: " ${LIBNAME}
353 gcc -nostart -Xlinker -soname=${LIBNAME} -L/Be/develop/lib/x86 ${OBJECTS} -lbe -o ${LIBNAME}
354 FINAL_LIBS=${LIBNAME}
355 ;;
356
357 'QNX')
358 LIBNAME="lib${LIBNAME}.a"
359 echo "mklib: Making QNX library: " ${LIBNAME}
360 wlib ${LIBNAME} ${OBJECTS}
361 FINAL_LIBS=${LIBNAME}
362 ;;
363
Brian Paul65e2ab32003-10-27 18:13:37 +0000364 'MorphOS')
365 LIBNAME="lib${LIBNAME}.a"
366 echo "mklib: Making MorphOS library: " ${LIBNAME}
367 ppc-morphos-ar rc ${LIBNAME} ${OBJECTS}
368 FINAL_LIBS="${LIBNAME}"
369 ;;
370
Brian Paulb3b725b2003-12-15 16:14:55 +0000371 'icc')
372 # Intel C compiler
373 LIBNAME="lib${LIBNAME}" # prefix with "lib"
374
375 if [ $STATIC = 1 ] ; then
376 echo "mklib: Making Intel ICC static library: " ${LIBNAME}.a
377 LINK="ar"
378 OPTS="-ruv"
379 # make lib
380 ${LINK} ${OPTS} ${LIBNAME}.a ${OBJECTS}
381 # finish up
382 FINAL_LIBS="${LIBNAME}.a"
383 else
384 OPTS="-shared"
385 VERSION="${MAJOR}.${MINOR}.${PATCH}"
386
387 echo "mklib: Making Intel ICC shared library: " ${LIBNAME}.so.${VERSION}
388
389 if [ $CPLUSPLUS = 1 ] ; then
390 LINK="icc"
391 else
392 LINK="icc"
393 fi
394
395 # rm any old libs
396 rm -f ${LIBNAME}.so.${VERSION}
397 rm -f ${LIBNAME}.so.${MAJOR}
398 rm -f ${LIBNAME}.so
399
400 # make lib
401 ${LINK} ${OPTS} -o ${LIBNAME}.so.${VERSION} ${OBJECTS} ${DEPS}
402 # make usual symlinks
403 ln -s ${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR}
404 ln -s ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so
405 # finish up
406 FINAL_LIBS="${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so"
407 fi
408 ;;
409
Brian Paul12d6cae2004-01-10 22:12:21 +0000410 'aix-gcc')
411 # AIX with gcc
Brian Paul5396ab22004-02-12 14:48:52 +0000412 if [ $STATIC = 1 ] ; then
413 LIBNAME="lib${LIBNAME}.a"
414 echo "mklib: Making AIX GCC static library: " ${LIBNAME}
415 rm -f ${LIBNAME}
416 ar ru ${LIBNAME} ${OBJECTS}
417 FINAL_LIBS=${LIBNAME}
418 else
419 LIBNAME="lib${LIBNAME}.so" # prefix with "lib", suffix with ".so"
420 echo "mklib: Making AIX GCC shared library: " ${LIBNAME}
421 # remove old lib
422 rm -f ${LIBNAME}
423 # make the lib
424 gcc -shared -Wl,-G ${OBJECTS} ${DEPS} -o ${LIBNAME}
425
426 # NOTE: the application linking with this library must specify
427 # the -Wl,-brtl flags to gcc
428
429 FINAL_LIBS=${LIBNAME}
430 fi
431 ;;
432
433 'ultrix')
434 # XXX untested
435 if [ $STATIC = 0 ] ; then
436 echo "mklib: Warning shared libs not supported on Ultrix"
437 fi
438 LIBNAME="lib${LIBNAME}.a"
439 echo "mklib: Making static library for Ultrix: " ${LIBNAME}
Brian Paul12d6cae2004-01-10 22:12:21 +0000440 rm -f ${LIBNAME}
Brian Paul5396ab22004-02-12 14:48:52 +0000441 ar ru ${LIBNAME} ${OBJECTS}
442 FINAL_LIBS="${LIBNAME}"
Brian Paul12d6cae2004-01-10 22:12:21 +0000443 ;;
Brian Paulb3b725b2003-12-15 16:14:55 +0000444
Brian Paul8c20c7b2003-06-01 16:21:45 +0000445 'example')
446 # If you're adding support for a new architecture, you can
447 # start with this:
Brian Paul5396ab22004-02-12 14:48:52 +0000448 if [ $STATIC = 1 ] ; then
449 LIBNAME="lib${LIBNAME}.a"
450 echo "mklib: Making static library for example arch: " ${LIBNAME}
451 rm -f ${LIBNAME}
452 ar rv ${LIBNAME} ${OBJECTS}
453 FINAL_LIBS="${LIBNAME}"
454 else
455 LIBNAME="lib${LIBNAME}.so" # prefix with "lib"
456 echo "mklib: Making shared library for example arch: " ${LIBNAME}
457 ld -o ${LIBNAME} ${OBJECTS} ${DEPS}
458 FINAL_LIBS="${LIBNAME}"
459 fi
Brian Paul8c20c7b2003-06-01 16:21:45 +0000460 ;;
461
462 *)
Brian Paul5396ab22004-02-12 14:48:52 +0000463 echo "mklib: ERROR: Don't know how to make a static/shared library for" ${ARCH}
464 echo "mklib: Please add necessary commands to mklib script."
Brian Paul8c20c7b2003-06-01 16:21:45 +0000465 ;;
466esac
467
468
469#
470# Put library files into installation directory if specified.
471#
472if [ ${INSTALLDIR} != "." ] ; then
473 echo "mklib: Installing" ${FINAL_LIBS} "in" ${INSTALLDIR}
474 mv ${FINAL_LIBS} ${INSTALLDIR}/
475fi