
# Actions to rebuild an out-of-date shared library
#   The library basename is given by LIBNAME

# configure generated targets:
shared_irix6: shared_irix

shared_osf5: shared_alpha

shared_solaris2: shared_solaris
shared_solaris2_gnu: shared_solaris_gnu

shared_freebsd10: shared_freebsd
shared_freebsd9: shared_freebsd
shared_freebsd8: shared_freebsd
shared_freebsd7: shared_freebsd
shared_freebsd6: shared_freebsd
shared_freebsd5: shared_freebsd
shared_freebsd4: shared_freebsd
shared_freebsd3: shared_freebsd

shared_kfreebsd5: shared_kfreebsd
shared_kfreebsd6: shared_kfreebsd

shared_kfreebsd: shared_linux

shared_aix3:
shared_aix4:
shared_aix5:
shared_aix6:

shared_darwin6: 

shared_hpux11: shared_hpux

# rules for building different shared libraries
shared_none:

shared_linux_intel:
	-@cd ${SHARED_LIBRARY_TMPDIR}; \
	${CLINKER} -shared -o ${INSTALL_LIB_DIR}/$$LIBNAME.${SL_LINKER_SUFFIX} *.o ${PETSC_EXTERNAL_LIB_BASIC}

shared_irix:
	-@${PCC} -elf -no_library_replacement -shared -rdata_shared -all ${INSTALL_LIB_DIR}/${LIBNAME}.${AR_LIB_SUFFIX} -o ${INSTALL_LIB_DIR}/${LIBNAME}.${SL_LINKER_SUFFIX}

shared_alpha:
	-@${LD} -shared -all ${INSTALL_LIB_DIR}/${LIBNAME}.${AR_LIB_SUFFIX} -o ${INSTALL_LIB_DIR}/${LIBNAME}.${SL_LINKER_SUFFIX} >/dev/null 2>&1

shared_freebsd:
	-@${LD} -Bshareable --whole-archive ${INSTALL_LIB_DIR}/${LIBNAME}.${AR_LIB_SUFFIX} -o ${INSTALL_LIB_DIR}/${LIBNAME}.${SL_LINKER_SUFFIX}

shared_hpux:
	-@cd ${SHARED_LIBRARY_TMPDIR}; \
	${LD} -b *.o -o ${INSTALL_LIB_DIR}/${LIBNAME}.${SL_LINKER_SUFFIX}

shared_linux:
	-@cd ${SHARED_LIBRARY_TMPDIR}; \
	${CLINKER} -shared -Wl,-soname,${LIBNAME}.${SL_LINKER_SUFFIX} -o ${INSTALL_LIB_DIR}/$$LIBNAME.${SL_LINKER_SUFFIX} *.o ${PETSC_EXTERNAL_LIB_BASIC} ${OTHERSHAREDLIBS}

shared_solaris_gnu:
	-@cd ${SHARED_LIBRARY_TMPDIR}; \
	sys_lib=`echo "${SYS_LIB}" | sed s?-Wl,-rpath?-L?g`; external_lib=`echo "${PETSC_EXTERNAL_LIB_BASIC}" | sed s?-Wl,-rpath?-L?g`;\
	${LD}  -G -h ${LIBNAME}.${SL_LINKER_SUFFIX} *.o -o ${INSTALL_LIB_DIR}/${LIBNAME}.${SL_LINKER_SUFFIX} $${sys_lib} $${external_lib}

shared_solaris:
	-@cd ${SHARED_LIBRARY_TMPDIR}; \
	${CC} -G -h ${LIBNAME}.${SL_LINKER_SUFFIX} *.o -o ${INSTALL_LIB_DIR}/${LIBNAME}.${SL_LINKER_SUFFIX} ${SYS_LIB} ${PETSC_EXTERNAL_LIB_BASIC}

shared_libtool:
	-@cd ${SHARED_LIBRARY_TMPDIR}; \
	${AR} x ${INSTALL_LIB_DIR}/lt_${LIBNAME}.${AR_LIB_SUFFIX}; \
	${LIBTOOL} --mode=link ${CLINKER} ${CFLAGS} ${CCPPFLAGS} -rpath ${INSTALL_LIB_DIR} -o ${LIBNAME}.la *.lo; \
	${LIBTOOL} --mode=install ${PETSC_DIR}/bin/config/install-sh ${LIBNAME}.la ${INSTALL_LIB_DIR}

shared_darwin8: shared_darwin7

shared_darwin7: 
	-@cd ${SHARED_LIBRARY_TMPDIR}; \
        if [ "${LIBNAME}" = "libpetsc" ]; then OTHERLIBS="${PACKAGES_LIBS} ${EXTERNAL_LIB}  ${SYS_LIB} -lm -lc "; fi;\
        if [ "${LIBNAME}" = "libpetscvec" ]; then OTHERLIBS="${PETSC_SYS_LIB_BASIC}"  ; fi;\
        if [ "${LIBNAME}" = "libpetscmat" ]; then OTHERLIBS="${PLAPACK_LIB} ${PETSC_VEC_LIB_BASIC}"  ; fi;\
        if [ "${LIBNAME}" = "libpetscdm" ]; then OTHERLIBS="${PETSC_MAT_LIB_BASIC}"  ; fi;\
        if [ "${LIBNAME}" = "libpetscksp" ]; then OTHERLIBS="${PETSC_DM_LIB_BASIC}"  ; fi;\
        if [ "${LIBNAME}" = "libpetscsnes" ]; then OTHERLIBS="${PETSC_KSP_LIB_BASIC}"  ; fi;\
        if [ "${LIBNAME}" = "libpetscts" ]; then OTHERLIBS="${PETSC_SNES_LIB_BASIC}"  ; fi;\
        if [ "${LIBNAME}" = "libpetscfortran" ]; then OTHERLIBS="${PETSC_TS_LIB_BASIC}"  ; fi;\
        if [ "${LIBNAME}" = "libpetsccontrib" ]; then OTHERLIBS="${PETSC_TS_LIB_BASIC}"  ; fi;\
        if [ "${LIBNAME}" = "libslepc" ]; then OTHERLIBS="${PETSC_KSP_LIB_BASIC}"  ; fi;\
        if [ "${MACOSX_DEPLOYMENT_TARGET}" = "" ]; then MACOSX_DEPLOYMENT_TARGET=10.3; export MACOSX_DEPLOYMENT_TARGET; fi;\
        ${CC}  -dynamiclib -single_module -multiply_defined suppress -undefined dynamic_lookup -o ${INSTALL_LIB_DIR}/$$LIBNAME.dylib *.o -L${PETSC_LIB_DIR}  ${OTHERSHAREDLIBS} ${SL_LINKER_LIBS} $$OTHERLIBS

shared_cygwin:
	-@cd ${SHARED_LIBRARY_TMPDIR}; \
       if [ "${LIBNAME}" = "libpetscvec" ]; then OTHERLIBS="-L$(PETSC_LIB_DIR) $(PETSC_SYS_LIB_BASIC)"  ; fi;\
       if [ "${LIBNAME}" = "libpetscmat" ]; then OTHERLIBS="${PLAPACK_LIB} -L$(PETSC_LIB_DIR) ${PETSC_VEC_LIB_BASIC}"  ; fi;\
       if [ "${LIBNAME}" = "libpetscdm" ]; then OTHERLIBS="-L$(PETSC_LIB_DIR) ${PETSC_MAT_LIB_BASIC}"  ; fi;\
       if [ "${LIBNAME}" = "libpetscksp" ]; then OTHERLIBS="-L$(PETSC_LIB_DIR) ${PETSC_DM_LIB_BASIC}"  ; fi;\
       if [ "${LIBNAME}" = "libpetscsnes" ]; then OTHERLIBS="-L$(PETSC_LIB_DIR) ${PETSC_KSP_LIB_BASIC}"  ; fi;\
       if [ "${LIBNAME}" = "libpetscts" ]; then OTHERLIBS="-L$(PETSC_LIB_DIR) ${PETSC_SNES_LIB_BASIC}"  ; fi;\
       if [ "${LIBNAME}" = "libpetscfortran" ]; then OTHERLIBS="-L$(PETSC_LIB_DIR) ${PETSC_TS_LIB_BASIC}"  ; fi;\
       if [ "${LIBNAME}" = "libpetsccontrib" ]; then OTHERLIBS="-L$(PETSC_LIB_DIR) ${PETSC_TS_LIB_BASIC}"  ; fi;\
       if [ "${LIBNAME}" = "libslepc" ]; then OTHERLIBS="-L$(PETSC_LIB_DIR) ${PETSC_KSP_LIB_BASIC}"  ; fi;\
       ${CLINKER} -shared -o ${INSTALL_LIB_DIR}/$$LIBNAME.${SL_LINKER_SUFFIX} *.o $$OTHERLIBS ${PETSC_EXTERNAL_LIB_BASIC} ${OTHERSHAREDLIBS}

# This is the main target for building shared libraries. It takes the following actions
#   a) Create 'tmp-petsc-shlib' in the library directory
#   b) Loops over the shared libraries in $SHLIBS
#   c) Checks that the corresponding archive or its libtool counterpart exists
#   d) Checks whether an existing archive is newer than the shared library
#   e) Executes target 'shared_arch' for any out-of-date library (The library basename is passed as LIBNAME)
#   c) Removes 'tmp-petsc-shlib'
# The 'shared_arch' target is passed
#   a) LIBNAME (the library basename)
#   b) SHARED_LIBRARY_TMPDIR (where .o files should be manipulated)
# and both the archive and shared library reside in INSTALL_LIB_DIR.
shared: 
	-@if [ "${BUILDSHAREDLIB}" = "no" ]; then \
	    echo "Shared libraries disabled"; \
	  else \
	    echo "making shared libraries in ${INSTALL_LIB_DIR}"; \
	    ${RM} -rf ${INSTALL_LIB_DIR}/tmp-petsc-shlib; \
	    mkdir ${INSTALL_LIB_DIR}/tmp-petsc-shlib; \
            cwd=`pwd`; \
	    for LIBNAME in ${SHLIBS}; \
	    do \
	      if test -f ${INSTALL_LIB_DIR}/$$LIBNAME.${AR_LIB_SUFFIX} -o -f ${INSTALL_LIB_DIR}/lt_$$LIBNAME.${AR_LIB_SUFFIX}; then \
	        if test -f ${INSTALL_LIB_DIR}/$$LIBNAME.${SL_LINKER_SUFFIX}; then \
	          flag=`find ${INSTALL_LIB_DIR} -type f -name $$LIBNAME.${AR_LIB_SUFFIX} -newer ${INSTALL_LIB_DIR}/$$LIBNAME.${SL_LINKER_SUFFIX} -print`; \
	          if [ "$$flag" = "" ]; then \
	            flag=`find ${INSTALL_LIB_DIR} -type f -name lt_$$LIBNAME.${AR_LIB_SUFFIX} -newer ${INSTALL_LIB_DIR}/$$LIBNAME.${SL_LINKER_SUFFIX} -print`; \
	          fi; \
	        else \
	          flag="build"; \
	        fi; \
	        if [ "$$flag" != "" ]; then \
                echo "building $$LIBNAME.${SL_LINKER_SUFFIX}"; \
                  ${RM} -f ${INSTALL_LIB_DIR}/tmp-petsc-shlib/*; \
	          cd  ${INSTALL_LIB_DIR}/tmp-petsc-shlib; \
	          ${AR} x ${INSTALL_LIB_DIR}/$$LIBNAME.${AR_LIB_SUFFIX}; \
                  cd $$cwd;\
	          ${OMAKE} LIBNAME=$$LIBNAME SHARED_LIBRARY_TMPDIR=${INSTALL_LIB_DIR}/tmp-petsc-shlib shared_arch; \
	        fi; \
	      fi; \
	    done; \
	    ${RM} -rf ${INSTALL_LIB_DIR}/tmp-petsc-shlib; \
	  fi
