Fixes for AIX, SunOS, Darwin.  -dlopen flag to build dlopen()'able modules
for AIX, Darwin.  (Dan Schikore)
diff --git a/bin/mklib b/bin/mklib
index befda66..b8f7a01 100755
--- a/bin/mklib
+++ b/bin/mklib
@@ -36,6 +36,7 @@
 LINK=""
 CPLUSPLUS=0
 STATIC=0
+DLOPEN=0
 INSTALLDIR="."
 ARCH="auto"
 ARCHOPT=""
@@ -64,6 +65,7 @@
 	    echo '                Not observed on all systems at this time.'
 	    echo '  -cplusplus    link with C++ runtime'
 	    echo '  -static       make a static library (default is dynamic/shared)'
+	    echo '  -dlopen       make a shared library suitable for dynamic loading'
 	    echo '  -install DIR  put resulting library file(s) in DIR'
 	    echo '  -arch ARCH    override using `uname` to determine host system'
 	    echo '  -archopt OPT  specify an extra achitecture-specific option OPT'
@@ -104,6 +106,9 @@
 	'-static')
 	    STATIC=1
 	    ;;
+	'-dlopen')
+	    DLOPEN=1
+	    ;;
 	'-install')
 	    shift 1;
 	    INSTALLDIR=$1
@@ -314,11 +319,17 @@
 		fi
 	    fi
 
+            ARCHOPTS=""
+	    if [ "$ARCHOPT" = "SUNV9" ] ; then
+                ARCHOPTS="-xarch=v9"
+            fi
+ 	    echo "mklib: linker is" ${LINK} ${OPTS} ${ARCHOPTS}
+
 	    # for debug:
 	    #echo "mklib: linker is" ${LINK} ${OPTS}
 
 	    rm -f ${LIBNAME}.${MAJOR} ${LIBNAME}
-	    ${LINK} ${OPTS} -o ${LIBNAME}.${MAJOR} ${OBJECTS} ${DEPS}
+	    ${LINK} ${OPTS} ${ARCHOPTS} -o ${LIBNAME}.${MAJOR} ${OBJECTS} ${DEPS}
 	    ln -s ${LIBNAME}.${MAJOR} ${LIBNAME}
 	    FINAL_LIBS="${LIBNAME}.${MAJOR} ${LIBNAME}"
 	fi
@@ -427,6 +438,10 @@
 	    ar -ruv ${LIBNAME} ${OBJECTS}
 	    FINAL_LIBS=${LIBNAME}
 	else
+            # HP uses a .2 for their current GL/GLU libraries
+	    if [ ${LIBNAME} = "GL" -o ${LIBNAME} = "GLU" ] ; then
+	       MAJOR=2
+	    fi
 	    RUNLIB="lib${LIBNAME}.${MAJOR}"
 	    DEVLIB="lib${LIBNAME}.sl"
 	    echo "mklib: Making HP-UX shared library: " ${RUNLIB} ${DEVLIB}
@@ -449,7 +464,11 @@
             FINAL_LIBS=${LIBNAME}
         else
 	    EXPFILE="lib${LIBNAME}.exp"
-	    OFILE=shr.o  #Want to be consistent with the IBM libGL.a
+	    if [ $ARCH = "AIX64" ] ; then
+		OFILE=shr_64.o
+	    else
+		OFILE=shr.o  #Want to be consistent with the IBM libGL.a
+	    fi
 	    LIBNAME="lib${LIBNAME}.a"  # shared objects are still stored in the .a libraries
 	    if [ $ARCH = "AIX64" ] ; then
 		OPTS="-bE:${EXPFILE} -bM:SRE -bnoentry -q64"
@@ -473,8 +492,15 @@
 			}
 		}
 	    }' | sort -u >> ${EXPFILE}
-	    cc ${OPTS} -o ${OFILE} ${OBJECTS} ${DEPS}
-	    ar ${X64} -r ${LIBNAME} ${OFILE}
+
+            # On AIX a shared library is linked differently when
+            # you want to dlopen the file
+	    if [ $DLOPEN = "1" ] ; then
+		cc -G ${OPTS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
+            else
+		cc ${OPTS} -o ${OFILE} ${OBJECTS} ${DEPS}
+		ar ${X64} -r ${LIBNAME} ${OFILE}
+            fi
             FINAL_LIBS="${LIBNAME}"
         fi
         ;;
@@ -516,23 +542,33 @@
             LIBNAME="lib${LIBNAME}.a"
             echo "mklib: Making Darwin static library: " ${LIBNAME}
             LINK="ar"
-            OPTS="-ruv"
+            OPTS="-ruvs"
             ${LINK} ${OPTS} ${LIBNAME} ${OBJECTS}
             FINAL_LIBS=${LIBNAME}
         else
 	    # may need these:
 	    # CFLAGS += -fno-common
 	    # LDFLAGS += -bundle -flat_namespace -undefined suppress
-            LIBNAME="lib${LIBNAME}.dylib"
+            # On Darwin a .bundle is used for a library that you want to dlopen
+            if [ $DLOPEN = "1" ] ; then
+                LIBSUFFIX="bundle"
+                FLAGS="${ARCHOPT} -bundle -multiply_defined suppress"
+            else
+		LIBSUFFIX="dylib"
+                FLAGS="${ARCHOPT} -dynamiclib -multiply_defined suppress -current_version ${MAJOR}.${MINOR}.0 -compatibility_version ${MAJOR}.${MINOR}.0 -install_name lib${LIBNAME}.${MAJOR}.${LIBSUFFIX}"
+            fi
+            LINKNAME="lib${LIBNAME}.${LIBSUFFIX}"
+            LIBNAME="lib${LIBNAME}.${MAJOR}.${LIBSUFFIX}"
+
             echo "mklib: Making Darwin shared library: " ${LIBNAME}
-            FLAGS="-dynamiclib -multiply_defined suppress -current_version ${MAJOR}.${MINOR}.0 -compatibility_version ${MAJOR}.${MINOR}.0"
 	    if [ $CPLUSPLUS = 1 ] ; then
 		LINK="g++"
 	    else
 		LINK="cc"
 	    fi
             ${LINK} ${FLAGS} -o ${LIBNAME} ${OBJECTS} ${DEPS}
-            FINAL_LIBS=${LIBNAME}
+            ln -s ${LIBNAME} ${LINKNAME}
+            FINAL_LIBS="${LIBNAME} ${LINKNAME}"
         fi
         ;;
 
@@ -574,7 +610,7 @@
 	FINAL_LIBS="${LIBNAME}"
 	;;
 
-    'icc')
+    'icc' | 'icc-istatic')
 	# Intel C compiler
 	# This should get merged into the Linux code, above, since this isn't
 	# really a different architecture.
@@ -589,12 +625,16 @@
             # finish up
             FINAL_LIBS="${LIBNAME}.a"
         else
-            OPTS="-shared"
+            if [ $ARCH = icc-istatic ] ; then
+                 OPTS="-shared -i-static -cxxlib-icc"
+            else
+                 OPTS="-shared"
+            fi
             VERSION="${MAJOR}.${MINOR}.${PATCH}"
             echo "mklib: Making Intel ICC shared library: " ${LIBNAME}.so.${VERSION}
 
             if [ $CPLUSPLUS = 1 ] ; then
-                LINK="icc"
+                LINK="icpc"
             else
                 LINK="icc"
             fi