#!/bin/sh 
##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
## 
##    http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************

#  THIS SCRIPT IS BEST VIEWED WITH TAB STOPS IN YOUR EDITOR AT 4 SPACES
#	Author Todd Tannenbaum, 6/97

# This is an env var used to see if a condor specific ld call had
# been invoked. It is set to an environment variable that if it is not 
# defined means that this is the "root" call of a possibly recursive
# invocation of condor_compile(true via a full install). If it is defined
# then it represents a file which must exist when the root invocation is
# finished. If the file doesn't exist, it means that our ld didn't get called.
#
# This algorithm only checks to see if our ld got called AT LEAST ONCE, 
# not that it got called correctly in every instance of say a 
# "condor_compile make" or something like that.
CONDOR_LD_IN_USE_FILE=$CONDOR_LD_IN_USE_FILE
# if the above is not set, then we know we are the root invocation, and later
# this has meaning when we create and remove this file.
if [ "X$CONDOR_LD_IN_USE_FILE" = "X" ]; then
	CONDOR_LD_ROOT_INVOCATION=yes
	# if it was undefined, then set it right here.
	CONDOR_LD_IN_USE_FILE="/tmp/.condor_ld_in_use.$$"
else
	# we are some child of another condor_compile.
	CONDOR_LD_ROOT_INVOCATION=no
	# we will transparently transfer the in use file name to this invocation
	# as well.
fi
# make it available to all future children of condor_compile
export CONDOR_LD_IN_USE_FILE

# Sometimes, a user can invoke condor_compile in such a manner as it'll never
# invoke an ld, but be correct. An example of this is: condor_compile gcc
# -print-prog-name=ld So, if we discover a situation like that, we'll set this
# to 'no' and not die with the unable to invoke ld error message. This allows
# condor_compile to be called under configure and libtool properly.  The
# default for this is to emit it.
DIE_WITH_LD_NOT_IN_USE=yes

# When we invoke a compiler/linker/whatever, make sure it eventually ends
# up calling our linker. We check for that by seeing if the file we exported
# into the environment is touched. This feature doesn't check all cases of
# our linker being called correctly, it only checks if our linker
# wasn't called AT LEAST ONCE.
invoke_linker () {

	# trap on various signals to not leave around the in use file.
	trap 'echo "Signal Caught... cleaning up."; test "X$CONDOR_LD_IN_USE_FILE" != "X" && rm -f $CONDOR_LD_IN_USE_FILE; exit 1' 1 2 3 15
	
	# invoke the program specified, but don't use exec
	eval "$@"
	linker_ret_code=$?

	# if I am the root node, then simply invoke whatever linker I was asked to
	# and when it returns, check to see if the in use file was touched. If
	# it was, then our linker was called at least once

	if [ "X$CONDOR_LD_ROOT_INVOCATION" = "Xyes" ]; then
		# I'm the root invocation, see if the file my children made exists,
		# and raise an error if it doesn't. However, other invocations of
		# condor_compile legitimately ask information from the compiler and
		# don't expect to produce an executable. So, in those cases, emit
		# nothing and don't die with a failure.
		if [ ! -f "$CONDOR_LD_IN_USE_FILE" -a \
			"$DIE_WITH_LD_NOT_IN_USE" = "yes" ];
		then

			echo 'ERROR: Internal ld was not invoked!'
			echo 'ERROR: Executable may not be linked properly for Condor!'
			echo 'ERROR: For users of Condor on Linux, we highly recommend'
			echo 'ERROR: using the default compiler that came with the'
			echo 'ERROR: distribution. Usually placing /usr/bin first in'
			echo 'ERROR: your path will remedy this error.'
			echo 'ERROR: To learn more about this error, visit this web page:'
			echo 'ERROR: http://www.cs.wisc.edu/condor/manual/faq.html'
			echo 'ERROR: and read the FAQ entry about this problem for your'
			echo 'ERROR: revision of Condor.'
			rm -f $CONDOR_LD_IN_USE_FILE
			exit 1;
		fi
		
		# get rid of the in use file.
		rm -f $CONDOR_LD_IN_USE_FILE
	fi
	
	# if we weren't the root node, then just transparently fall backwards up
	# the process call chain. I'm assuming some children underneath me had
	# touched the file in question and the root invocation will know for sure.

	exit $linker_ret_code
}

# --------------------------------------------------

# Tell our "ld" to link to with the Condor libs
CONDOR_COMPILE=yes
export CONDOR_COMPILE

# Handle any command-line args
while [ A = A ]
do
	if [ $# = 0 ]; then
		break;
	fi


	if [ $1 = "-condor_syscall_lib" ]; then
		shift
		CONDOR_SPECIAL_CLIB=$1
		shift
		continue
	fi

	if [ $1 = "-condor_rt0" ]; then
		shift
		CONDOR_RT0=$1
		shift
		continue
	fi

	if [ $1 = "-condor_c++_support" ]; then
		shift
		CONDOR_CPLUS=$1
		shift
		continue
	fi

	if [ $1 = "-condor_ld_dir" ]; then
		shift
		CONDOR_LD_DIR=$1
		shift
		continue
	fi

	if [ $1 = "-condor_standalone" ]; then
		#
		# This option is ignored.  Standalone checkpointing
		# has the same build procedure as standard jobs now.
		# This option is kept for compatibility.
		#
		shift
		continue
	fi

	if [ $1 = "-condor_lib" ]; then
		shift
		CONDOR_LIBDIR=$1
		shift
		# Reset anything based off of the libdir by default
		CONDOR_CLIB=$CONDOR_LIBDIR/libcondorsyscall.a
		CONDOR_ZCLIB=$CONDOR_LIBDIR/libcondorzsyscall.a
		STATIC_ZLIB=$CONDOR_LIBDIR/libcondor_z.a
		CONDOR_RT0=$CONDOR_LIBDIR/condor_rt0.o
		CONDOR_CPLUS=$CONDOR_LIBDIR/libcondorc++support.a
		CONDOR_LD_DIR=$CONDOR_LIBDIR
		continue
	fi

	# if we made it here, there are no more command-line options
	break
done

#
# If the user didn't tell us which library directory to use, it's OK
# to fail if we can't find one using condor_config_val.
#
if [ "X$CONDOR_LIBDIR" = "X" ]; then
    CONDOR_LIBDIR=`condor_config_val LIB 2> /dev/null`

    if [ $? != 0 ]; then
        echo "ERROR: Failed to find LIB using condor_config_val."
        echo "You may need to add the directory containing condor_config_val"
        echo "to your \$PATH, or you may need to set \$CONDOR_CONFIG in"
        echo "your environment."
        echo
        echo "Re-running 'condor_config_val LIB' so you can see the error..."
        condor_config_val LIB

        exit 1
    fi                                    
fi

if [ "X$CONDOR_CLIB" = "X" ]; then
    CONDOR_CLIB=$CONDOR_LIBDIR/libcondorsyscall.a
fi
    
if [ "X$CONDOR_ZLIB" = "X" ]; then
    STATIC_ZLIB=$CONDOR_LIBDIR/libcondor_z.a
fi
    
if [ "X$CONDOR_ZCLIB" = "X" ]; then
    CONDOR_ZCLIB=$CONDOR_LIBDIR/libcondorzsyscall.a
fi
    
if [ "X$CONDOR_RT0" = "X" ]; then        
    CONDOR_RT0=$CONDOR_LIBDIR/condor_rt0.o
fi

if [ "X$CONDOR_CPLUS" = "X" ]; then
    CONDOR_CPLUS=$CONDOR_LIBDIR/libcondorc++support.a
fi

if [ "X$CONDOR_LD_DIR" = "X" ]; then
    CONDOR_LD_DIR=$CONDOR_LIBDIR
fi
    
if [ "X$CONDOR_SPECIAL_CLIB" = "X" ]; then        
    CONDOR_SPECIAL_CLIB=/not-there-bull
fi

#
# If the compressed ckpt library is available, use it.
#

if [ -r $CONDOR_ZCLIB ]; then
	CONDOR_CLIB="$CONDOR_ZCLIB"
fi

#
# However, we still always need the compression library for other purposes.
#

CONDOR_CLIB="$CONDOR_CLIB $STATIC_ZLIB"

if [ -r $CONDOR_SPECIAL_CLIB ]; then
	CONDOR_CLIB="$CONDOR_SPECIAL_CLIB"
fi
	
# Export these so our special "ld" script can find them
export CONDOR_LIBDIR
export CONDOR_CLIB
export CONDOR_RT0
export CONDOR_CPLUS
export CONDOR_LD_DIR

# Since uname and awk live in different places on various
# platforms, use a PATH.  NOTE: we want to add these things to the END
# of the PATH, to make sure people get the version of the compiler
# they thought they were using. 
PATH=$PATH:/bin:/usr/bin:/usr/bsd

# Figure out what architecture we're running on 

os=`uname -s`

osver=`uname -r`
if [ $os = "HP-UX" ]; then
	osmajver=`uname -r | awk -F. '{print $2}' -`
else
	osmajver=`uname -r | awk -F. '{print $1}' -`
fi

osfull="$os$osmajver"
abi=""

# Determine if the system ld has been replaced with our ld script
# or not.
CONDOR_FULLINSTALL=false

case $os in
	HP-UX )
		if [ $osmajver = "10" ]; then
			if [ -x /usr/ccs/bin/ld.real ]; then
				CONDOR_FULLINSTALL=true
			fi 
		fi
		if [ $osmajver = "09" ]; then
			if [ -x /bin/ld.real ]; then
				CONDOR_FULLINSTALL=true
			fi 
		fi
		;;
	SunOS )
		if [ $osmajver = 5 ]; then
			if [ -x /usr/ccs/bin/ld.real ]; then
				CONDOR_FULLINSTALL=true
			fi 
		fi
		if [ $osmajver = 4 ]; then
			if [ -x /bin/ld.real ]; then
				CONDOR_FULLINSTALL=true
			fi 
		fi
		;;
	Linux )
		if [ -x /usr/bin/ld.real ]; then
			CONDOR_FULLINSTALL=true
		fi 
		;;
	* )
		if [ -x /bin/ld.real ]; then
			CONDOR_FULLINSTALL=true
		fi 
		;;
esac

# Usage info should go here...
if [ $# = 0 ]; then
	echo "Usage: condor_compile <command> [options/files .... ]"
	if [ $CONDOR_FULLINSTALL = true ]; then
		echo "  where <commmand> is whatever you enter to compile/link your application."
		exit 1
	fi
	echo "  where <command> is one of the following:"
	echo "  gcc, g++, g77, gfortran, cc, acc, c89, CC, f77, fort77, ld, "
	echo "  pgcc, pgf77, pgf90, pghpf, or icc."
	echo "  (on some platforms, f90 is also allowed)"
	exit 1
fi

# if fully installed, then just run the command - eventually we'll 
# hit our special ld

if [ $CONDOR_FULLINSTALL = true ]; then
	invoke_linker $*
fi

ARGS=$*

# check to see if the compiler is passed things like -print-prog-name=...
# If so, we don't want to die with the ld no in use error message since
# the compiler produces good output in this case but no executable is expected.
for i in $ARGS
do
	case $i in
		-print-prog-name* | -print-file-name* | --version)
			DIE_WITH_LD_NOT_IN_USE=no
			;;
	esac
done

# If we were passed a -c, we're not going to be used for linking, so
# we don't have to do anything special to make sure our special ld is
# used.  
#
# Create warning for pthreads in Standard Universe. Pth library must be installed
# with the --enable-pthread flag. Pth seems to be the best Green Threads library
# that does POSIX Thread (pthread) emulation as of 01/10/2013.
print_warning=0
NAME=$1
for j_pthread in $(seq 1 $#)
do
    if [ $# = 0 ]; then
	break;
    fi
    if [ "$1" = "-c" ]; then
	exec $ARGS;
    fi
    if [ "$1" = "-lpthread" ]; then
	print_warning=1
    fi
    if [ "$1" = "-l" ] && [ "$2" = "pthread" ]; then
	print_warning=1
    fi
    shift
done
if [ $print_warning -eq 1 ]; then
    echo "WARNING: Using pthreads with condor_compile."
    echo "WARNING: condor_compile must know how to find the Pth library."
    echo "WARNING: Users should add /path/to/pth-library to the environment"
    echo "WARNING: variable LD_LIBRARY_PATH or add -L/path/to/pth-library"
    echo "WARNING: to the command line when invoking condor_compile."
    echo "WARNING: Pth must be installed with the --enable-pthread"
    echo "WARNING: flag. See Pth INSTALL file for more information."
fi


# If not fully installed, and we didn't see a -c, add options to force
# the compiler to use our special ld

# First of all, we need to strip off any path from our (now) first
# argument, so we can compare it different names, even if the user
# specified a full path to the program.
myname=`echo $NAME | sed "s/.*\///"`

# Handle Intel compilers
if [ $myname = "icc" ]; then
	invoke_linker $ARGS -static -B$CONDOR_LD_DIR/
fi

# Handle GNU compilers

if [ $myname = "gcc" ]; then
	invoke_linker $ARGS -B$CONDOR_LD_DIR/
fi 

if [ $myname = "g++" ]; then
	invoke_linker $ARGS -B$CONDOR_LD_DIR/
fi 

if [ $myname = "g77" ]; then
	invoke_linker $ARGS -B$CONDOR_LD_DIR/
fi 

if [ $myname = "gfortran" ]; then
	invoke_linker $ARGS -B$CONDOR_LD_DIR/
fi 

# Handle Portland compilers.
# Notice that we explicitly do _not_ support Portland C++.
# Their run-time support libraries conflict with libcondorc++support.a

if [ $myname = "pgcc" ]; then
	invoke_linker $ARGS -Yl,$CONDOR_LD_DIR/
fi 

if [ $myname = "pgf77" ]; then
	invoke_linker $ARGS -Yl,$CONDOR_LD_DIR/
fi 

if [ $myname = "pgf90" ]; then
	invoke_linker $ARGS -Yl,$CONDOR_LD_DIR/
fi 

if [ $myname = "pghpf" ]; then
	invoke_linker $ARGS -Yl,$CONDOR_LD_DIR/
fi 

# Handle ld (linker).  Since the user might have specified a full path
# to ld, we want to shift (to get rid of the ld we were passed), and
# call our ld directly with the remaining arguments.
if [ $myname = "ld" ]; then
	shift;
	invoke_linker $CONDOR_LD_DIR/ld $ARGS
fi

# Handle all the vendor system compilers ---------------
# the idea here is to simply append whatever command line
# option(s) allows us to use $CONDOR_LIBDIR/ld instead
# of the default path for ld.

case $os in 

	HP-UX )
		if [ $myname = "cc" ]; then
			invoke_linker $ARGS -tl,$CONDOR_LD_DIR/ld
		fi

		if [ $myname = "CC" ]; then
			invoke_linker $ARGS +A -tl,$CONDOR_LD_DIR/ld
		fi

		if [ $myname = "aCC" ]; then
			invoke_linker $ARGS +A -tl,$CONDOR_LD_DIR/ld
		fi

		if [ $myname = "c89" ]; then
			invoke_linker $ARGS -tl,$CONDOR_LD_DIR/ld
		fi

		if [ $myname = "f77" ]; then
			invoke_linker $ARGS -tl,$CONDOR_LD_DIR/ld
		fi 

		if [ $myname = "fort77" ]; then
			invoke_linker $ARGS -tl,$CONDOR_LD_DIR/ld
		fi 
		
		;;

	SunOS )
		use_qpath=`cc -help -flags | grep Qpath | wc -l`
		if [ $myname = "cc" -a $use_qpath = 0 ]; then
			invoke_linker $ARGS -Yl,$CONDOR_LD_DIR
		fi
		if [ $myname = "cc" -a $use_qpath != 0 ]; then
			invoke_linker $ARGS -Qpath $CONDOR_LD_DIR
		fi

		if [ $myname = "acc" ]; then
			invoke_linker $ARGS -Qpath $CONDOR_LD_DIR
		fi

		if [ $myname = "CC" ]; then
			invoke_linker $ARGS -Qpath $CONDOR_LD_DIR
		fi

		if [ $myname = "f77" ]; then
			invoke_linker $ARGS -Qpath $CONDOR_LD_DIR
		fi 
		
		if [ $myname = "f90" ]; then
			invoke_linker $ARGS -Qpath $CONDOR_LD_DIR
		fi 
		
		;;

	Linux )
		# Linux's system compilers are GNU 
		if [ $myname = "cc" ]; then
			invoke_linker $ARGS -B$CONDOR_LD_DIR/
		fi 

		if [ $myname = "CC" ]; then
			invoke_linker $ARGS -B$CONDOR_LD_DIR/
		fi 

		if [ $myname = "c++" ]; then
			invoke_linker $ARGS -B$CONDOR_LD_DIR/
		fi 

		if [ $myname = "f77" ]; then
			invoke_linker $ARGS -B$CONDOR_LD_DIR/
		fi 

		if [ $myname = "gfortran" ]; then
			invoke_linker $ARGS -B$CONDOR_LD_DIR/
		fi 
		
		;;
esac

# If we made it here, we did not do anything, so print out usage
	echo "Usage: condor_compile <command> [options/files .... ]"
	if [ $CONDOR_FULLINSTALL = true ]; then
		echo "  where <commmand> is whatever you enter to compile/link your application."
		exit 1
	fi
	echo "  where <command> is one of the following:"
	echo "  gcc, g++, g77, gfortran, cc, acc, c89, CC, f77, fort77, ld, "
	echo "  pgcc, pgf77, pgf90, pghpf, or icc."
	echo "  (on some platforms, f90 is also allowed)"
	exit 1
