#! /bin/sh
#*******************************************************************************
# E.S.O. - VLT project
#
# "@(#) $Id: vltMakeExecutableDependencies,v 1.8 2012/01/13 18:54:47 tstaig Exp $" 
#
# who       when      what
# --------  --------  ----------------------------------------------
# gfilippi  08/07/93  created
# gfilippi  25/08/93  changed LPATH into L_PATH (see vltMakefile)
# gfilippi  26/11/93  added VxWorks
# gfilippi  26/11/93  use only $(LD) for linking VxWorks files
# gfilippi  01/12/93  no libraries in the dependency list 
# gfilippi  22/04/94  treatment of RTAP library keyword added 
# gfilippi  26/04/94  considering libraries in the dependency
# gfilippi  07/07/94  CCU--> allows -L for VxWorks modules
# gfilippi  07/09/94  makeXxxx utility renamed into vltMakeXxxx
# gfilippi  06/10/94  make output nicer as from SPR940175
# gfilippi  14/10/94  CCS changed to include RTAP
# gfilippi  24/10/94  external definition od RTAP&CCS_LIBLIST (see vltMakefile)
# gfilippi  25/10/94  stop on error ("-" removed from the make command)
# gfilippi  19/10/94  RTAP_FLAG to be able to shared/static-link RTAP appl.
# gfilippi  02/11/94  strip binary when optimization is asked
# gfilippi  01/02/95  PURIFY added
# gfilippi  16/11/95  C++ added
# gfilippi  28/11/95  g++|iostream|iberty added to libList
# gfilippi  16/04/96  stdc++ added to libList      (SPR960154)
# gfilippi  16/04/96  iostream removed  to libList (SPR960199)
# gfilippi  16/04/96  improved message (SPR960152)
# gfilippi  11/05/96  fixed $ --> \$  for HP-UX10
# gfilippi  11/05/96  PURECOV added
# gfilippi  15/10/96  strip action removed
# gfilippi  15/10/96  use $(LD) for linker: LD is defined in vltMakefile
# gfilippi  29/10/96  NOSHARED added
# gfilippi  30/10/96  "${RTAPexe}" = "yes" -->  "${rtap}" = "yes"
# gfilippi  16/04/97  CCS_NOX11 added (EAL request)
# rschmutz 1999-04-03 if Linux (RedHat 5.2): do not use g++ library.
# gfilippi 1999-07-06 egcs support: do not use g++ library, use dce for HP
# psivera  2000-04-13 added pthread library for HPUX-11
# psivera  2001-09-17 SPR 20010390: safer "if" statement is used when checking 
#                     environment variables.
# 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
# mzampare  28/02/03  workaround for stdc++ library (shared, but not .so)
#

#************************************************************************
#   NAME
#   vltMakeExecutableDependencies - create the makefile to build an executable
# 
#   SYNOPSIS
#
#   vltMakeExecutableDependencies [/vw] <exeName> <objectList> <ldFlags> <libList>
# 
#   DESCRIPTION
#   Utility used by vltMakefile to create the makefile to build an executable.
#   It is not intended to be used as a standalone command.
#
#   An executable depends on the objects and the libraries(1) that 
#   it uses, so the final target rule is:
#
#   ../bin/<exeName>: ... ../object/<obj-i>.o ... -l<lib-i> ...
#   <TAB>   $(CC) $(LDFLAGS) $(ldFlags) [-r] \
#                   ...objects...  ...libraries...  -o ../bin/<exeName>
#
#                                       [-r] is used with VxWorks
#
#   (1) see also GNU Make 3.64, 4.3.5 pag 37
#
#
#   This rule implies to check the state of update of each object and of
#   libraries. How to make an object is given by the implicit pattern rule 
#   defined in vltMakefile. How to make a library is given by the explicit
#   rule created by vltMakeLibraryDependencies.
#
#   The .dx itself depends to Makefile.
#
#   The rules is written to standard output.
#
#   <exeName>     The name of the  executable 
#                 (Without directory)
#
#   <objectList>  The list of the object used to build the executable
#                 (Without neither directory nor .a suffix)
#
#   <ldFlags>     additional link flags,
#
#   <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="yes"
    shift
else
    MAKE_VXWORKS="no"
fi

exeName=$1
objectList=$2
ldFlags=$3
libList=$4

echo "# Dependency file for program: ${exeName}"
echo "# Created automatically by vltMakeExecutableDependencies -  `date '+%d.%m.%y %T'`"
echo "# DO NOT EDIT THIS FILE"

#
# define the dependency file dependent to the Makefile
echo "../object/${exeName}.dx: 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 exe both to be produced and to be installed).
echo ".PHONY: ${exeName} "

#
# if the list of objects is not empty, the rule to build the exe is written on output.
if [ "${objectList}" != "" ] || [ "${libList}" != "" ]
then
    #
    # prepare the list of all objects (full filename)
    for member in ${objectList}
    do
        oList="${oList} ../object/${member}.o"
    done

    #
    # 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_${exeName}_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++ "
                ;;
            $CYGWIN_VER):
                MESSAGE="$MESSAGE -- error: this Operating System is not fully supported"
                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_${exeName} := \$(foreach member, \$(xyz_${exeName}_DEF), \$(find_def))"
        echo "lList_${exeName} := \$(foreach member, \$(xyz_${exeName}_DEF), \$(check_dll))"
        echo ""
    fi

    # if we are on SunOS we enrich $libList with   -lstdc++
    if [ "`uname`" = "SunOS" -a "$PURIFY" != "" ] 
    then
	lList="${lList} -lstdc++ -lposix4"
	if [ "X$GNU_ROOT" != "X" ]
	then
	    gccLib=`find $GNU_ROOT -name libgcc.a`
	    gccDir="-L`dirname $gccLib` -L/usr/lib"
	fi
    fi

    #
    # create a target with the <name> of the executable (make <name>)
    echo "${exeName}: ../bin/${exeName} "
    echo ""

    # Workaround to the ugly problem of 
    # the difference betweem the linker 'ld' handles the -l<lib> command
    # line argument and the way the makefile does.
    # The GNU make, (but not the version we are using) allows for 
    # specified what the <lib> above should be mapped to, typically
    # lib<lib>.a and lib<lib>.so which are then searched in vpath paths
    # plus trusted directories of /usr/lib and /lib
    # 
    # The problem is if the library is not simply called 
    # lib<lib>.so but lib<lib>.so..2.7.2.8 or whatever
    # One temporary workaround do this 'disagreement' between
    # 'ld' and 'make' is to avoid having such libraries specified as a
    # dependency, but only in the linker command line call.
    
    aList=`echo $lList | sed -e 's/-lstdc++//'`

    #
    # output the rule to build the executable file
    #
    echo "../bin/${exeName}: ${oList} ${aList} "
    echo "	-@echo == Building executable: ../bin/${exeName} "  

    #
    # if any, print wanings
    if [ "${MESSAGE}" != "" ]
    then
        echo "	-@echo"
        echo "	-@echo $MESSAGE"
        echo "	-@echo"
    fi

    echo "	-@echo"

    if [ $MAKE_VXWORKS = "yes" ]
    then 
        #
        # for VxWorks LD has the meaning of a pre-link, the real link is at run time.
        echo "	\$(AT)\$(LD)            \$(LDFLAGS) \$(L_PATH) ${ldFlags} -r ${oList} ${lList}                -o ../bin/${exeName}"
    else
        #
        # 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

	# change requested by Gianluca for upgrade to GCC 3.0 for Solaris which seems
	# to require STDC++ library and RT.
	#
	if [ "`uname`" = "SunOS" ] 
	    then
	    libraryList="${libraryList} -lstdc++ -lposix4 -lrt"
	fi

		
        # The target is build as a conditional statement controlled by variables
        # that can also be specified on the command line         
        echo "ifeq (\$(strip \$(MAKE_NOSHARED) \$(${exeName}_NOSHARED)),)"
        if [ "´uname´" = "$CYGWIN_VER" ]; then
            echo "	\$(AT)\$(PURIFY) \$(PURECOV) \$(LD) \$(CFLAGS) -Wl,--export-all-symbols -Wl,--enable-auto-import -Wl,--enable-runtime-pseudo-reloc -Wl,--whole-archive ${oList} -Wl,--no-whole-archive \$(LDFLAGS) \$(L_PATH) $gccDir ${ldFlags} ${libraryList} \$(lList_${exeName}) \$(defList_${exeName}) -o ../bin/${exeName}"
        else
            echo "	\$(AT)\$(PURIFY) \$(PURECOV) \$(LD) \$(CFLAGS) \$(LDFLAGS) \$(L_PATH) $gccDir ${ldFlags} ${oList} ${libraryList}         -o ../bin/${exeName}"
        fi
        echo "else"
        echo "	\$(AT)\$(PURIFY) \$(PURECOV) \$(LD) \$(CFLAGS) \$(LDFLAGS) \$(L_PATH) $gccDir ${ldFlags} ${oList} ${libraryListNoshared} -o ../bin/${exeName}"
        echo "endif"

        #
        # on RTAP files, execute the licence unlock of RTAP executables
        if [ "${rtap}" = "yes" ]
        then 
            echo "	RtapUnlockExe ../bin/${exeName}"
        fi
    fi

    echo "	-@echo"

else
    echo "${exeName}:"
    echo "	@echo "
    echo "	@echo \"WARNING >>>>> ${exeName}_OBJECTS and ${exeName}_LIBS  not defined. \" "
    echo "	@echo \"Makefile should define the action for target  '${exeName}:'\" "
    echo "	@echo "
fi

#
# ___oOo___

