#! /bin/sh
#*******************************************************************************
# E.S.O. - VLT project
#
# "@(#) $Id: vltMakeLibraryDependencies,v 1.7 2012/01/13 18:54:47 tstaig Exp $" 
#
# who       when        what
# --------  ----------  ----------------------------------------------
# gfilippi  08/07/93    created
# gfilippi  29/11/93    modified to include VxWork
# gfilippi  29/11/93    modified the rule to make libraries (always from scratch)
# gfilippi  07/09/94    makeXxxx utility renamed into vltMakeXxxx
# gfilippi  10/04/97    support shared libraries
# gfilippi  17/04/97    on SUN, give the name of the library (-h)
# gfilippi  19/04/97    create sh lib -w on ../lib too
# gfilippi  23/04/97    support <lib>_LDFLAGS for libraries
# ealalert  1999-03-16  use -Xlinker options for sharedLibName
# rschmutz  1999-04-03  use $CC instead of gcc
# mzampare  2001-12-03  added support for library-specific libraries ALMASW2001037 
# psivera   07/06/02  removed iberty from libList
# psivera   31/07/02  introduced GEN_LIBLIST variable: list of lib that have always
#                     to be linked
# pmurphy   2007-03-05 Added check for unresolved symbols in shared libraries

#************************************************************************
#   NAME
#   vltMakeLibraryDependencies - create the makefile to build a library
# 
#   SYNOPSIS
#
#        vltMakeLibraryDependencies  [/vw] <libName> <objectList> <ldFlags> [<noshared>]
#
# 
#   DESCRIPTION
#   Utility used by vltMakefile to create the makefile to build a library.
#   It is not intended to be used as a standalone command.
#
#   Unless <noshared> is specified, both static and shared libraries 
#   are produced.
# 
#   The library depends on library member (object files).
#   How to make a library member is given from an implicit pattern rule
#   defined in vltMakefile, so there is no need here to be specified.
#
#   The .da itself depends to Makefile.
#
#   The rules is written to standard output.
#
#   <libName>     The name of the library 
#                 (Without neither directory nor .a suffix)
#
#   <object List> The list of the object forming the library
#                 (Without neither directory nor .a suffix)
#
#   <ldFlags>     passed to the linker (for shared only!)
#
#   <noshared>    if defined, the shared library is NOT produced
#
#   <libList>     the list of libraries needed to link.
#                 The list can contain "conventional" names (like RTAP)
#                 that direct the procedure to create the needed generation
#                 rules
#
#   FILES
#   $VLTROOT/include/vltMakefile   
#
#   ENVIRONMENT
#
#   RETURN VALUES
#
#   SEE ALSO 
#   vltMakefile, Makefile, (GNU) make
#
#   GNU make - Edition 0.41, for make Version 3.64 Beta, April 93
#   VLT Software - Programming Standard - 1.0 10/03/93
#
#   BUGS    
#
#----------------------------------------------------------------------

if [ "$1" = "/vw" ]
then
    MAKE_VXWORKS=TRUE
    VWFLAG="-r"
    shift
else
    MAKE_UNIX=TRUE
fi

libName=$1
objectList=${2}
ldFlags=${3}
noshared=${4}
libList=${5}

staticLib=../lib/lib${libName}.a

if [ "$MAKE_UNIX" = "TRUE" ]
then
    #
    # shared libraries have different extension on SUN and HP
    if [ "`uname`" = "HP-UX" ]
    then
        sharedLib=../lib/lib${libName}.sl
        # on HP, the way it works by default does not require any explicit name
        sharedLibName=""
    else
        sharedLib=../lib/lib${libName}.${SHLIB_EXT}
        # on SUN, by default the library is created with "../lib/lib...."
        # as internal name. This gives problems at run time in finding the 
        # the correct file (see ldd output) 
        # Here I force the name without parent directory 
        sharedLibName="-Xlinker -h -Xlinker lib${libName}.${SHLIB_EXT}"
    fi 
fi

echo "# Dependency file for library: ${libName}"
echo "# Created automatically by vltMakeLibraryDependencies -  `date '+%d.%m.%y %T'`"
echo "# DO NOT EDIT THIS FILE"
echo "../object/${libName}.da: Makefile"
echo ""

#
# define PHONY the target, so make does not try to make the target
# when no object are specified. (due to the fact that the same list of objects is
# used to build the list of lib both to be produced and to be installed).
echo ".PHONY: ${libName} "
echo ""

#
# if the list of objects is not empty, the rule to build the lib is written on output.
if [ "${objectList}" != "" ] || [ "${libList}" != "" ]
then
    # prepare full-pathname object file list

    echo "xyz_${libName}_OBJ = \\"
    for member in ${objectList}
    do
        echo "                 ../object/${member}.o \\"
    done
    echo ""

    #
    # prepare the lists in the "-l<name>" format of all libraries used in linking
    # some library names can be conventional names that are expanded in a series of
    # predefined libraries or flags
    if [ "´uname´" = "$CYGWIN_VER" ]; then
        echo "xyz_${libName}_DEF = \\"
    fi
    for member in ${libList}
    do
        case $member in

        CCS):
            ccs="yes"
            if [ "X$RTAPROOT" != "X" ]
            then
                rtap="yes"
            fi
            ;;

        CCS_NOX11):
            ccs="yes"
            noX11="yes"
            if [ "X$RTAPROOT" != "X" ]
            then
                rtap="yes"
            fi
            ;;


        RTAP):
            if [ "X$RTAPROOT" = "X" ]
            then
                MESSAGE="$MESSAGE $MESSAGE -- warning: in ${exeName}_LIB: RTAP flag ignored (RTAPROOT not defined)."
            else
                if [ "${rtap}" != "yes" ]
                then
                    rtap=yes
                    MESSAGE=""
                else
                    MESSAGE="$MESSAGE -- warning: in ${exeName}_LIB: RTAP is implied by CCS. You can remove it"
                fi
            fi
            ;;

        C++):
            case "`uname`" in

            HP-UX):
                case "`uname -r`" in
                B.10.20):
                      lList="${lList} -lstdc++ -ldce"
                      ;;
                B.11.00):
                      lList="${lList} -lstdc++ -lpthread"
                      ;;
                esac
                ;;

            SunOS):
                lList="${lList} -lstdc++ "
                ;;

            Linux):
                lList="${lList} -lstdc++ "
                ;;

            *):
                MESSAGE="$MESSAGE -- error: this Operating System is not supported"
                ;;
            esac
            ;;

        stdc++):
            lList="${lList} -l${member}"
            MESSAGE="$MESSAGE -- warning: in ${exeName}_LIB: please remove ${member} and use  C++  to link c++ libraries"
            ;;

        g++):
            MESSAGE="$MESSAGE -- warning: in ${exeName}_LIB: egcs does not provide g++ anylonger. Please remove ${member} and use  C++  to link c++ libraries"
            ;;

        iostream):
            MESSAGE="$MESSAGE -- warning: in ${exeName}_LIB: ${member} is not supported since GCC 2.7, please remove it."
            ;;

        *):
            if [ "´uname´" = "$CYGWIN_VER" ]; then
                echo "                 ${member} \\"
            else
                lList="${lList} -l${member}"
            fi
            ;;
        esac
    done
    if [ "´uname´" = "$CYGWIN_VER" ]; then
        echo ""
        echo "find_acsroot = \$(if \$(wildcard \$(ACSROOT)/lib/lib\$(member).dll.def), \`cat \$(ACSROOT)/lib/lib\$(member).dll.def\`,)"
        echo "find_vlttop = \$(if \$(wildcard \$(VLTTOP)/lib/lib\$(member).dll.def), \`cat \$(VLTTOP)/lib/lib\$(member).dll.def\`, \$(find_acsroot))"
        echo "find_lib = \$(if \$(wildcard ../lib/lib\$(member).dll.def), \`cat ../lib/lib\$(member).dll.def\`, \$(find_vlttop))"
        echo "find_def = \$(find_lib)"
        echo ""
        echo "check_acsroot = \$(if \$(wildcard \$(ACSROOT)/lib/lib\$(member).dll.def), ,-l\$(member))"
        echo "check_vlttop = \$(if \$(wildcard \$(VLTTOP)/lib/lib\$(member).dll.def), , \$(check_acsroot))"
        echo "check_lib = \$(if \$(wildcard ../lib/lib\$(member).dll.def), , \$(check_vlttop))"
        echo "check_dll = \$(check_lib)"
        echo ""
        echo "defList_${libName} := \$(foreach member, \$(xyz_${libName}_DEF), \$(find_def))"
        echo "lList_${libName} := \$(foreach member, \$(xyz_${libName}_DEF), \$(check_dll))"
        echo ""
        for member in ${objectList}
        do
            echo "${member}_BUILD_DLL = \$(${libName}_CFLAGS)"
        done
        echo ""
    fi

    #
    # from NOV96, there is the possibility to use or to use not shared libraries.
    # The dependency file is build only when either the source or Makefile is touched, therefore
    # to allow the user to chose at run time, make variables are used so they get the
    # actual value and something like  make  MAKE_NOSHARED=on  works.
    
    # build up the list of libraries with and without directive to use share lib.
    
    libraryList="\$(GEN_LIBLIST)"
    libraryListNoshared="\$(GEN_LIBLIST_NOSHARED)"
    
    if [ "${lList}" != "" ]
    then
        libraryList="${libraryList} ${lList}"
        libraryListNoshared="${libraryListNoshared} \$(NOSHARED_ON) ${lList} \$(NOSHARED_OFF)"
    fi
    
    if [ "${ccs}" = "yes" ]
    then 
        libraryList="${libraryList} \$(CCS_LIBLIST)"
        libraryListNoshared="${libraryListNoshared} \$(CCS_LIBLIST_NOSHARED)"
    fi
    
    if [ "${rtap}" = "yes" ]
    then 
        if [ "${noX11}" = "yes" ]
        then
            libraryList="${libraryList} \$(RTAP_NOX11_FLAGS)"
            libraryListNoshared="${libraryListNoshared} \$(RTAP_NOX11_FLAGS_NOSHARED)"
        else
            libraryList="${libraryList} \$(RTAP_FLAGS)"
            libraryListNoshared="${libraryListNoshared} \$(RTAP_FLAGS_NOSHARED)"
        fi
    fi

    #
    # create a target with the <name> of the library/ies to be produced
    # (make <name>)

    if [ "$MAKE_UNIX" = "TRUE" ]
    then 
        # if <lib>_NOSHARED is defined no shared library is produced
        if [ "${noshared}" != "" ]
        then
            echo "${libName}: ${staticLib}"
        else
            echo "${libName}: ${staticLib} ${sharedLib}"
        fi
    fi
    
    if [ "$MAKE_VXWORKS" = "TRUE" ]
    then 
        echo "${libName}: ${staticLib}"
    fi
    
    echo ""

    # if we are on SunOS and PURIFY is on, 
    # we enrich $libList with the location of libgcc.a
    #
    if [ "`uname`" = "SunOS" -a "$PURIFY" != "" ] 
    then
	if [ "X$GNU_ROOT" != "X" ]
	then
	    gccLib=`find $GNU_ROOT -name libgcc.a`
	    gccDir="-L`dirname $gccLib`"
	fi
    fi


    # output the rule to build the static library   
    # for the time being, libraries are always regenerated from scratch 
    # (See also vltMakefile)
    echo "${staticLib}: \$(xyz_${libName}_OBJ)"
    echo "	@echo \"== Making library: ${staticLib}\" "
    echo "	-\$(AT)\$(RM) ${staticLib} "
    echo "	\$(AT)\$(AR) rc  ${staticLib} \$(xyz_${libName}_OBJ)"
    echo "	\$(AT)\$(RANLIB) ${staticLib}"

    #
    # shared only for UNIX and when required
    if [ "$MAKE_UNIX" = "TRUE" -a "${noshared}" = "" ]
    then
        echo ""
        # output the rule to build the shared library (.sl or .so)
        echo "${sharedLib}: \$(xyz_${libName}_OBJ) ${lList}"
        echo "	@echo \"== Making library: ${sharedLib}\" "
        echo "	-\$(AT)\$(RM) ${sharedLib} "
        if [ "´uname´" = "$CYGWIN_VER" ]; then
            echo "	\$(AT)\$(LD) -shared -fPIC ${sharedLibName} -Wl,--enable-auto-image-base -Wl,--export-all-symbols -Wl,--enable-auto-import -Wl,--enable-runtime-pseudo-reloc -Wl,--whole-archive \$(xyz_${libName}_OBJ) -Wl,--no-whole-archive \$(L_PATH) $gccDir ${libraryList} \$(lList_${libName}) \$(defList_${libName}) ${ldFlags} -o ${sharedLib}"
            echo "	@echo \"-l${libName} ${libraryList} \$(lList_${libName}) \$(defList_${libName})\" |sed \"s/ \+/\n/g\" |sort -u | tr '\n' ' ' > ${sharedLib}.def"
            echo "	@cd ../lib/; rm ${sharedLib}.a; ln -s ${sharedLib} ${sharedLib}.a"
        else
            echo "	\$(AT)\$(CXX) -shared -fPIC ${sharedLibName} \$(L_PATH) $gccDir ${libraryList} ${ldFlags} -o ${sharedLib} \$(xyz_${libName}_OBJ)"
        fi
        # PPM 2007.01.30 - check for unresolved symbols.  
        # The -w flag means just complain, don't cause an error
        # Without -w, it should cause a fatal error
        echo "	\$(AT) acsMakeCheckUnresolvedSymbols -w ${sharedLib}"
        echo "	\$(AT) chmod a-w ${sharedLib} "
    fi
    echo "	@echo "
    
else
    echo "${libName}:"
    echo "	@echo "
    echo "	@echo \"WARNING >>>>> ${libName}_OBJECTS and ${libName}_LIBS  not defined. \" "
    echo "	@echo \"Makefile should define the action for target  '${libName}:'\" "
    echo "	@echo \"             check your Makefile \" "
    echo "	@echo "
fi
#
# ___oOo___
