Brian Paul | 8c20c7b | 2003-06-01 16:21:45 +0000 | [diff] [blame^] | 1 | #!/bin/sh |
| 2 | |
| 3 | # Make a shared library. |
| 4 | # Basically do a switch/case depending on the OS and make a shared |
| 5 | # lib conforming to that OS. |
| 6 | |
| 7 | |
| 8 | # Usage: |
| 9 | # mklib [options] objects ... |
| 10 | # Options: |
| 11 | # -o LIBRARY specifies the name of resulting library ("GL" for example) |
| 12 | # -major N specifies major version number (default is 1) |
| 13 | # -minor N specifies minor version number (default is 0) |
| 14 | # -patch N specifies patch version number (default is 0) |
| 15 | # -lLIBRARY specifies a dependency on LIBRARY |
| 16 | # -LDIR search in DIR for library dependencies |
| 17 | # -cplusplus link with C++ runtime |
| 18 | # -static make a static library (default is dynamic/shared) |
| 19 | # -install DIR move resulting library files to DIR |
| 20 | # -arch ARCH override using `uname` to determine architecture |
| 21 | # -archopt OPT specify an extra achitecture-specific option OPT |
| 22 | # |
| 23 | # The library name should just be "GL" or "GLU", etc. The 'lib' prefix |
| 24 | # will be added here if needed, as well as the ".so" or ".a" suffix, etc. |
| 25 | # |
| 26 | # objects should be: foo.o bar.o etc.o |
| 27 | # |
| 28 | # Environment variables recognized: |
| 29 | # CC C compiler command |
| 30 | # CXX C++ compiler command |
| 31 | # |
| 32 | |
| 33 | |
| 34 | # |
| 35 | # Option defaults |
| 36 | # |
| 37 | LIBNAME="" |
| 38 | MAJOR=1 |
| 39 | MINOR=0 |
| 40 | PATCH=0 |
| 41 | DEPS="" |
| 42 | CPLUSPLUS=0 |
| 43 | STATIC=0 |
| 44 | INSTALLDIR="." |
| 45 | ARCH="auto" |
| 46 | ARCHOPT="" |
| 47 | |
| 48 | |
| 49 | # |
| 50 | # Parse arguments |
| 51 | # |
| 52 | while true |
| 53 | do |
| 54 | case $1 in |
| 55 | '-o') shift 1; LIBNAME=$1;; |
| 56 | '-major') shift 1; MAJOR=$1;; |
| 57 | '-minor') shift 1; MINOR=$1;; |
| 58 | '-patch') shift 1; PATCH=$1;; |
| 59 | -l*) DEPS="$DEPS $1";; |
| 60 | -L*) DEPS="$DEPS $1";; |
| 61 | '-cplusplus') CPLUSPLUS=1;; |
| 62 | '-static') STATIC=1;; |
| 63 | '-install') shift 1; INSTALLDIR=$1;; |
| 64 | '-arch') shift 1; ARCH=$1;; |
| 65 | '-archopt') shift 1; ARCHOPT=$1;; |
| 66 | -*) echo "mklib: Unknown option: " $1 ; exit 1;; |
| 67 | *) break |
| 68 | esac |
| 69 | shift 1 |
| 70 | done |
| 71 | OBJECTS=$@ |
| 72 | |
| 73 | if [ ${ARCH} = "auto" ] ; then |
| 74 | ARCH=`uname` |
| 75 | fi |
| 76 | |
| 77 | |
| 78 | # |
| 79 | # Error checking |
| 80 | # |
| 81 | if [ "x${LIBNAME}" = "x" ] ; then |
| 82 | echo "mklib: Error: no library name specified" |
| 83 | exit 1 |
| 84 | fi |
| 85 | if [ "x${OBJECTS}" = "x" ] ; then |
| 86 | echo "mklib: Error: no object files specified" |
| 87 | exit 1 |
| 88 | fi |
| 89 | |
| 90 | |
| 91 | # |
| 92 | # Debugging info |
| 93 | # |
| 94 | if [ ] ; then |
| 95 | echo "-----------------" |
| 96 | echo ARCH is $ARCH |
| 97 | echo LIBNAME is $LIBNAME |
| 98 | echo MAJOR is $MAJOR |
| 99 | echo MINOR is $MINOR |
| 100 | echo PATCH is $PATCH |
| 101 | echo DEPS are $DEPS |
| 102 | echo "-----------------" |
| 103 | fi |
| 104 | |
| 105 | |
| 106 | # |
| 107 | # OK, make the library now |
| 108 | # |
| 109 | case $ARCH in |
| 110 | |
| 111 | 'Linux') |
| 112 | LIBNAME="lib${LIBNAME}" # prefix with "lib" |
| 113 | OPTS="-Xlinker -Bsymbolic -shared -Wl,-soname,${LIBNAME}.so.${MAJOR}" |
| 114 | VERSION="${MAJOR}.${MINOR}.${PATCH}" |
| 115 | |
| 116 | echo "mklib: Making Linux shared library: " ${LIBNAME}.so.${VERSION} |
| 117 | |
| 118 | if [ $CPLUSPLUS = 1 ] ; then |
| 119 | LINK="g++" |
| 120 | else |
| 121 | LINK="gcc" |
| 122 | fi |
| 123 | |
| 124 | # rm any old libs |
| 125 | rm -f ${LIBNAME}.so.${VERSION} |
| 126 | rm -f ${LIBNAME}.so.${MAJOR} |
| 127 | rm -f ${LIBNAME}.so |
| 128 | |
| 129 | # make lib |
| 130 | ${LINK} ${OPTS} -o ${LIBNAME}.so.${VERSION} ${OBJECTS} ${DEPS} |
| 131 | # make usual symlinks |
| 132 | ln -s ${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR} |
| 133 | ln -s ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so |
| 134 | # finish up |
| 135 | FINAL_LIBS="${LIBNAME}.so.${VERSION} ${LIBNAME}.so.${MAJOR} ${LIBNAME}.so" |
| 136 | ;; |
| 137 | |
| 138 | 'SunOS') |
| 139 | LIBNAME="lib${LIBNAME}.so" |
| 140 | echo "mklib: Making SunOS shared library: " ${LIBNAME} |
| 141 | OPTS="-G" |
| 142 | if [ $CPLUSPLUS = 1 ] ; then |
| 143 | # link for C++ |
| 144 | if [ "x${CXX}" = "xg++" ] ; then |
| 145 | LINK="g++" |
| 146 | elif [ `which c++` ] ; then |
| 147 | LINK="c++" |
| 148 | elif [ `which CC` ] ; then |
| 149 | LINK="CC" |
| 150 | elif [ `which g++` ] ; then |
| 151 | LINK="g++" |
| 152 | else |
| 153 | echo "mklib: warning: can't find C++ comiler, trying CC." |
| 154 | LINK="CC" |
| 155 | fi |
| 156 | elif [ "x${CC}" = "xgcc" ] ; then |
| 157 | # use gcc for linking |
| 158 | LINK="gcc" |
| 159 | else |
| 160 | # use native Sun linker |
| 161 | LINK="ld" |
| 162 | fi |
| 163 | rm -f ${LIBNAME} |
| 164 | ${LINK} ${OPTS} -o ${LIBNAME} ${OBJECTS} ${DEPS} |
| 165 | FINAL_LIBS=${LIBNAME} |
| 166 | ;; |
| 167 | |
| 168 | 'FreeBSD') |
| 169 | SHLIB="lib${LIBNAME}.so.${MAJOR}.${MINOR}" |
| 170 | STLIB="lib${LIBNAME}.a" |
| 171 | echo "mklib: Making FreeBSD shared library: " ${SHLIB} |
| 172 | rm -f ${SHLIB} ${STLIB} |
| 173 | ar cq ${STLIB} ${OBJECTS} |
| 174 | ranlib ${STLIB} |
| 175 | ld -Bshareable -o ${SHLIB} ${OBJECTS} |
| 176 | # XXX make lib${LIBNAME}.so.${MAJOR} symlink? |
| 177 | FINAL_LIBS="${SHLIB} ${STLIB}" |
| 178 | ;; |
| 179 | |
| 180 | 'OpenBSD') |
| 181 | LIBNAME="lib${LIBNAME}" |
| 182 | VERSION="${MAJOR}.${MINOR}" |
| 183 | echo "Building OpenBSD PIC library: " ${LIBNAME} |
| 184 | rm -f ${LIBNAME}_pic.a ${LIBNAME}.so.${VERSION} |
| 185 | ar cq ${LIBNAME}_pic.a ${OBJECTS} |
| 186 | ranlib ${LIBNAME}_pic.a |
| 187 | ld -x -Bshareable -Bforcearchive -o ${LIBNAME}.so.${VERSION} ${LIBNAME}_pic.a |
| 188 | ln -s ${LIBNAME}.so.${VERSION} ${LIBNAME}.so |
| 189 | FINAL_LIBS="${LIBNAME}_pic.a ${LIBNAME}.so.${VERSION} ${LIBNAME}.so" |
| 190 | ;; |
| 191 | |
| 192 | 'NetBSD') |
| 193 | LIBNAME="lib${LIBNAME}" |
| 194 | echo "mklib: Making NetBSD PIC shared library: " ${LIBNAME} |
| 195 | VERSION="${MAJOR}.${MINOR}" |
| 196 | rm -f ${LIBNAME}_pic.a ${LIBNAME}.so.${VERSION} |
| 197 | ar cq ${LIBNAME}_pic.a ${OBJECTS} |
| 198 | ranlib ${LIBNAME}_pic.a |
| 199 | ld -x -Bshareable -Bforcearchive -o ${LIBNAME}.so.${VERSION} ${LIBNAME}_pic.a |
| 200 | FINAL_LIBS="${LIBNAME}_pic.a ${LIBNAME}.so.${VERSION}" |
| 201 | ;; |
| 202 | |
| 203 | 'IRIX') |
| 204 | LIBNAME="lib${LIBNAME}.so" # prefix with "lib", suffix with ".so" |
| 205 | if [ $ARCHOPTS = "64" ] ; then |
| 206 | # 64-bit ABI |
| 207 | OPTS="-64 -shared -all" |
| 208 | echo "mklib: Making IRIX 64-bit shared library: " ${LIBNAME} |
| 209 | elif [ $ARCHOPTS = "o32" ] ; then |
| 210 | # old 32-bit ABI |
| 211 | OPTS="-32 -shared -all" |
| 212 | echo "mklib: Making IRIX o32-bit shared library: " ${LIBNAME} |
| 213 | else |
| 214 | # new 32-bit ABI |
| 215 | OPTS="-n32 -shared -all" |
| 216 | echo "mklib: Making IRIX n32-bit shared library: " ${LIBNAME} |
| 217 | fi |
| 218 | ld ${OPTS} -o ${LIBNAME} ${OBJECTS} ${DEPS} |
| 219 | FINAL_LIBS="${LIBNAME}" |
| 220 | ;; |
| 221 | |
| 222 | 'IRIX64') |
| 223 | LIBNAME="lib${LIBNAME}.so" # prefix with "lib", suffix with ".so" |
| 224 | echo "mklib: Making IRIX64 library: " ${LIBNAME} |
| 225 | # 64-bit ABI |
| 226 | OPTS="-64 -shared -all" |
| 227 | ld ${OPTS} -o ${LIBNAME} ${OBJECTS} ${DEPS} |
| 228 | FINAL_LIBS="${LIBNAME}" |
| 229 | ;; |
| 230 | |
| 231 | 'linux-cygwin') |
| 232 | LIBNAME="lib${LIBNAME}.a" |
| 233 | echo "mklib: Making linux-cygwin library: " ${LIBNAME} |
| 234 | gnuwin32ar ruv ${LIBNAME} ${OBJECTS} |
| 235 | FINAL_LIBS=${LIBNAME} |
| 236 | ;; |
| 237 | |
| 238 | 'HPUX') |
| 239 | RUNLIB="lib${LIBNAME}.${MAJOR}" |
| 240 | DEVLIB="lib${LIBNAME}.sl" |
| 241 | echo "mklib: Making HPUX library: " ${RUNLIB} ${DEVLIB} |
| 242 | ld -b -o ${RUNLIB} +b ${RUNLIB} ${OBJECTS} ${DEPS} |
| 243 | ln -s ${RUNLIB} ${DEVLIB} |
| 244 | FINAL_LIBS="{RUNLIB} ${DEVLIB}" |
| 245 | ;; |
| 246 | |
| 247 | 'OpenSTEP') |
| 248 | LIBNAME="lib${LIBNAME}.a" |
| 249 | echo "mklib: Making OpenSTEP static library: " ${LIBNAME} |
| 250 | libtool -static -o ${LIBNAME} - ${OBJECTS} |
| 251 | FINAL_LIBS=${LIBNAME} |
| 252 | ;; |
| 253 | |
| 254 | 'OSF1') |
| 255 | VERSION="${MAJOR}.${MINOR}" |
| 256 | LIBNAME="lib${LIBNAME}.so" |
| 257 | ARNAME="lib${LIBNAME}.a" |
| 258 | echo "mklib: Making OSF/1 library: " ${LIBNAME} |
| 259 | rm -f ${LIBNAME}.${VERSION} |
| 260 | ld -o ${LIBNAME}.${VERSION} -shared -no_archive -set_version ${VERSION} -soname ${LIBNAME}.${VERSION} -expect_unresolved \* -all ${OBJECTS} ${DEPS} |
| 261 | ln -sf ${LIBNAME}.${VERSION} ${LIBNAME} |
| 262 | |
| 263 | # also make static lib |
| 264 | rm -f ${ARNAME} |
| 265 | ar clqz ${ARNAME} ${OBJECTS} |
| 266 | FINAL_LIBS="${ARNAME} ${LIBNAME} ${LIBNAME}.${VERSION}" |
| 267 | ;; |
| 268 | |
| 269 | 'Darwin') |
| 270 | VERSION="${MAJOR}.${MINOR}.${TINY}" |
| 271 | LIBNAME="lib${LIBNAME}.dylib" |
| 272 | ARNAME="lib${LIBNAME}.dylib.a" |
| 273 | echo "mklib: Making Darwin libraries: " ${LIBNAME} ${ARNAME} |
| 274 | FLAGS="-dynamiclib -multiply_defined suppress" |
| 275 | cc ${FLAGS} -o ${LIBNAME} ${OBJECTS} ${DEPS} |
| 276 | # also make regular .a files, |
| 277 | # provided by Danek Duvall (duvall@dhduvall.student.princeton.edu) |
| 278 | ar ruv ${ARNAME} ${OBJECTS} |
| 279 | ranlib ${ARNAME} |
| 280 | FINAL_LIBS="${ARNAME} ${LIBNAME}" |
| 281 | ;; |
| 282 | |
| 283 | 'LynxOS') |
| 284 | LIBNAME="lib${LIBNAME}.a" |
| 285 | echo "mklib: Making LynxOS library: " ${LIBNAME} |
| 286 | ar ru ${LIBNAME} ${OBJECTS} |
| 287 | FINAL_LIBS=${LIBNAME} |
| 288 | ;; |
| 289 | |
| 290 | 'BeOS') |
| 291 | LIBNAME="lib${LIBNAME}.so" |
| 292 | echo "mklib: Making BeOS shared library: " ${LIBNAME} |
| 293 | gcc -nostart -Xlinker -soname=${LIBNAME} -L/Be/develop/lib/x86 ${OBJECTS} -lbe -o ${LIBNAME} |
| 294 | FINAL_LIBS=${LIBNAME} |
| 295 | ;; |
| 296 | |
| 297 | 'QNX') |
| 298 | LIBNAME="lib${LIBNAME}.a" |
| 299 | echo "mklib: Making QNX library: " ${LIBNAME} |
| 300 | wlib ${LIBNAME} ${OBJECTS} |
| 301 | FINAL_LIBS=${LIBNAME} |
| 302 | ;; |
| 303 | |
| 304 | 'example') |
| 305 | # If you're adding support for a new architecture, you can |
| 306 | # start with this: |
| 307 | LIBNAME="lib${LIBNAME}.so" # prefix with "lib" |
| 308 | echo "mklib: Making library for example arch: " ${LIBNAME} |
| 309 | ld -o ${LIBNAME} ${OBJECTS} ${DEPS} |
| 310 | FINAL_LIBS="${LIBNAME}" |
| 311 | ;; |
| 312 | |
| 313 | *) |
| 314 | echo "mklib: WARNING: making library for unknown platform!" |
| 315 | echo "mklib: WARNING: this may not work!" |
| 316 | echo "mklib: WARNING: please update the bin/mklib script!" |
| 317 | # XXX this is a total hack for Mesa - remove someday |
| 318 | # fall-back to an old mklib.* script |
| 319 | ${MAKELIB} "lib${LIBNAME}.a" ${MAJOR} ${MINOR} ${PATCH} ${OBJECTS} |
| 320 | ;; |
| 321 | esac |
| 322 | |
| 323 | |
| 324 | # |
| 325 | # Put library files into installation directory if specified. |
| 326 | # |
| 327 | if [ ${INSTALLDIR} != "." ] ; then |
| 328 | echo "mklib: Installing" ${FINAL_LIBS} "in" ${INSTALLDIR} |
| 329 | mv ${FINAL_LIBS} ${INSTALLDIR}/ |
| 330 | fi |