# --*-sh-*--------------------------------------------------------------------
#
# sysconf.in
#
# Maurice LeBrun
# IFS, University of Texas at Austin
# 14-Jul-1994
#
# This script sets up config variables for a Unix-like system.
# The stuff set here is very system-specific and not easy to automate.
# Believe me, I wish it were!  Stuff like compiler names (ANSI C, Fortran)
# and options are set here.
#
# This treatment is a little bit of overkill for PLplot, but some of it
# comes from other projects, and you never know when it will turn out to
# be useful..
# ----------------------------------------------------------------------------

# ----------------------------------------------------------------------------
# Configure library recognition for various systems.
# ----------------------------------------------------------------------------

LIBEXTNS="a"

case "$system" in 
#    aix*|AIX*|rs*|RS*|ibm*|IBM* ) 
#	CC=xlc
#    ;;
    alpha*|ALPHA*|Alpha*|OSF* ) 
	LIBEXTNS="so a"
    ;;
#    convex*|ConvexOS* ) 
#	CC="cc -std"
#	with_gcc=no
#    ;;
    hp*|HP* ) 
	LIBEXTNS="sl a"
    ;;
    irix*|IRIX*|Irix*|sgi*|SGI* ) 
	LIBEXTNS="so a"
    ;;
    Linux* )
	LIBEXTNS="so a"
    ;;
#    SunOS-4* )
#	CC=acc
#    ;;
esac

# ----------------------------------------------------------------------------
# Compiler/linker variables
#
# The following shell variables are used.  They can be redefined as
# appropriate for specific systems.
#
# CXX		The only compiler worth using.
# CC		ANSI C compiler
# OCC		Traditional C compiler
# F77		Fortran 77 compiler
# LDC		Linker for C programs
# LDF		Linker for Fortran programs
#
# The following are only set if the appropriate capability is selected,
# otherwise are null.  I need to specify one for each compiler used for
# full generality (on some systems the syntax may differ slightly between
# them).  Each is tagged with:
#
#	_C	for the C compiler
#	_CXX	for the C++ compiler
#	_F	for the Fortran 77 compiler
#	_LC	for the C linker
#	_LCXX	for the C++ linker
#	_LF	for the Fortran 77 linker
#
# DEBUG_FLAG	Compile with debugging on
# OPT_FLAG	Compile with optimization on
# DBL_FLAG	Use double-precision
# PROF_FLAG	Compile with profiling on
# SYS_FLAGS	Misc system-specific compiler flags
# ----------------------------------------------------------------------------

# Defaults

M4=m4

# Double precision
# Note that although there is no truly standard way to invoke double
# precision in Fortran from the command line, enough of them use -r8
# that I've decided to make it the default.  

if test "$with_double" = "yes"; then
    DBL_FLAG_C=-DDOUBLE
    DBL_FLAG_CXX=-DDOUBLE
    DBL_FLAG_M4=-DDOUBLE
    DBL_FLAG_F=-r8
fi

# Profiling
# Not a good default for Fortran here, either.

if test "$with_profile" = "yes"; then
    PROF_FLAG_C=-p
    PROF_FLAG_CXX=-p
    PROF_FLAG_LC=-p
fi

# ----------------------------------------------------------------------------
# Set compiler on a system-dependent basis.
# Notes:
#
# - type "configure --with-gcc" to specify gcc from the command line
# - type "configure --with-f2c" to specify f2c from the command line
#
# On some systems, gcc or f2c is the default.  On others, they are not
# supported (e.g. UNICOS, Convex).  The f2c executable is expected to be
# "fc" since some systems may have both programs.  If "fc" can't be found,
# "f77" is tried instead.
#
# On systems where gcc or f2c are actually undesirable (e.g. on Crays --
# gcc isn't ported and why would you want to use f2c?), I set with_gcc
# and/or with_f2c to "no" to prevent their use.
#
# The actual setup for gcc or f2c is done AFTER the case statements.  Done
# this way because they're pretty predictable across platforms, and this
# way we can just override what is set below.  IMPORTANT: the command line
# argument uses a "-" (dash) to separate words, while the shell variable
# uses a "_" (underscore).
# ----------------------------------------------------------------------------

# Set up ANSI C compiler

if test -z "$CC"; then
    CC=cc
    case "$system" in 
	aix*|AIX*|rs*|RS*|ibm*|IBM* ) 
	    CC=xlc
	;;
	alpha*|ALPHA*|Alpha*|OSF* ) 
	    CC="cc -std"
	;;
	convex*|ConvexOS* ) 
	    CC="cc -std"
	    with_gcc=no
	;;
	dg*|DG* ) 
	    CC="cc -ansi"
	;;
	hp*|HP* ) 
	    CC=c89
	;;
	irix*|IRIX*|Irix*|sgi*|SGI* ) 
	    CC="cc"
	    OCC='$(CC)'
	;;
	Linux* )
	    with_gcc=yes
	;;
	SunOS-4* )
	    CC=acc
	;;
	sx*|Sx*|SX*|monte*|Monte*|MONTE* ) 
	    CC="cc -hansi"
	    with_gcc=no
	;;
	CRAY* ) 
	    with_gcc=no
	;;
    esac
fi

# ----------------------------------------------------------------------------
# Set up K&R C compiler

if test -z "$OCC"; then
    OCC=cc
    case "$system" in 
	SunOS-5.* )
	    OCC="cc -Xs"
	;;
    esac
fi

# ----------------------------------------------------------------------------
# Set up C++ compiler
# If gcc has been specified, use that.  Otherwise do NOT use gcc.
# Mixing/matching compilers is best left to the experts.

if test "$with_gcc" = "yes"; then
    if test ! "$enable_cxx" = "no"; then
	enable_cxx=yes
    fi
fi

if test "$enable_cxx" = "yes"; then
    if test -z "$CXX"; then
	if test "$with_gcc" = "yes"; then
	    CXX=g++
	else
	    CXX=CC
	    case "$system" in
		aix*|AIX*|rs*|RS*|ibm*|IBM* ) 
		    CXX=xlC
		;;
		hp*|HP* ) 
		    CXX="CC +a1"
		;;
	    esac
	fi
    fi
fi

found_cxx=no

# If Kuck and Associates, Inc, Photon C++ is available, use that.

if test "$enable_cxx" = "yes"; then
    AC_CHECK_PROG(found_photon, KCC, yes, no)
    if test "$found_photon" = "yes" ; then
	AC_MSG_RESULT([Found butt-kicking Photon C++, using that!])
	CXX=KCC
	found_cxx=yes
    fi
fi

# Try to find C++ compiler.  If not found, switch enable_cxx to "no".

if test "$enable_cxx" = "yes" -a "$found_cxx" = "no"; then
    found_cxx=""
    AC_CHECK_PROG(found_cxx, $CXX, yes, no)
    if test "$found_cxx" = "no" ; then
	AC_MSG_RESULT([warning: cannot find C++ compiler, setting enable_cxx=no])
	enable_cxx=no
    fi
fi

# ----------------------------------------------------------------------------
# Debugging
# Will disable optimization by default unless using gcc.

if test "$with_debug" = "yes"; then
    if test ! "$with_gcc" = "yes"; then
	with_opt=no
    fi
    DEBUG_FLAG_C=-g
    DEBUG_FLAG_CXX=-g
    DEBUG_FLAG_F=-g
    DEBUG_FLAG_LC=-g
    DEBUG_FLAG_LCXX=-g
    DEBUG_FLAG_LF=-g
fi

# Optimization

if test "$with_opt" = "yes"; then
    OPT_FLAG_C=-O2
    OPT_FLAG_CXX=-O
    OPT_FLAG_F=-O
fi

# ----------------------------------------------------------------------------
# Set up Fortran compiler

if test "$enable_f77" = "yes"; then
    if test -z "$F77"; then
	F77=f77
	case "$system" in 
	    aix*|AIX*|rs*|RS*|ibm*|IBM* ) 
		F77=xlf
	    ;;
	    dg*|DG* ) 
		F77=ghf77
	    ;;
	    hp*|HP* ) 
		# The fort77 front-end uses cc-like command line flags.

		F77=fort77
	    ;;
	    sx*|Sx*|SX*|monte*|Monte*|MONTE* ) 
		# The f77 front-end uses cc-like command line flags,
		# but I've had problems with it, so use f77sx here instead.

		F77=f77sx
		with_f2c=no
	    ;;
	    CRAY* ) 
		F77=cf77
		with_f2c=no
	    ;;
	esac
    fi

    # If not using f2c, try locating the executable now.

    if test "$with_f2c" = "no"; then
	AC_CHECK_PROG(found_F77, $F77, yes, no)
	if test "$found_F77" = "no" ; then
	    with_f2c=yes
	fi
    fi
fi

# ----------------------------------------------------------------------------
# gcc
# ----------------------------------------------------------------------------

if test "$with_gcc" = "yes"; then
    CC=gcc
    CXX=g++
    OCC=gcc
    CPP="gcc -E"

    if test "$with_warn" = "yes"; then
	SYS_FLAGS_C=-Wall
    fi
fi

# ----------------------------------------------------------------------------
# f2c
#
# I give preference to the program "fc" for invoking f2c, since there may
# be a vendor-supplied program called "f77".  Only if "fc" isn't found
# do I fall back to "f77".
#
# The only option supported for f2c is --with-double.
# ----------------------------------------------------------------------------

if test "$enable_f77" = "yes"; then
    if test "$with_f2c" = "yes"; then
	AC_CHECK_PROG(found_fc, fc, yes, no)
	if test "$found_fc" = "yes" ; then
	    F77=/usr/bin/fc
	else
	    AC_CHECK_PROG(found_f77, f77, yes, no)
	    if test "$found_f77" = "yes" ; then
		F77=f77
	    else
		AC_MSG_RESULT([warning: cannot find a Fortran compiler, setting enable_f77=no])
		with_f2c=no
		enable_f77=no
	    fi
	fi

	DEBUG_FLAG_F=
	OPT_FLAG_F=
	PROF_FLAG_F=
	SYS_FLAGS_F=
    fi
fi

# ----------------------------------------------------------------------------
# Can finally set linker defaults.
# ----------------------------------------------------------------------------

if test -z "$LDC"; then
    LDC="$F77"
fi
if test -z "$LDCXX"; then
    LDCXX="$CXX"
fi
if test -z "$LDF"; then
    LDF="$F77"
fi

# ----------------------------------------------------------------------------
# Now get system-specific compiler flags.
#
# If your Fortran compiler supports auto-promotion to double precision, it
# is best to set DBL_FLAG_F with the appropriate command option.  This is
# only used for the PLplot demo programs -- the Fortran interface uses m4
# to generate single and double precision versions.  So if your compiler
# can't do command-line promotion to double precision, the only loss is
# that you won't be able to run the Fortran demos in double precision.
# ----------------------------------------------------------------------------

case "$system" in 
    aix*|AIX*|rs*|RS*|ibm*|IBM* ) 
	if test "$with_gcc" = "no"; then
	    SYS_FLAGS_CXX=-+
	fi
	if test "$with_double" = "yes" -a "$with_f2c" = "no"; then
	    DBL_FLAG_F="-qAUTODBL=DBLPAD"
	fi
    ;;
    alpha*|ALPHA*|Alpha*|OSF* ) 

	# Note that the c optimize flag is set to -O1, as higher levels of
	# optimization will mess up some diagonal dashed lines.  

	if test "$with_opt" = "yes"; then
	    OPT_FLAG_C=-O1
	fi
    ;;
    convex*|ConvexOS* ) 
	if test "$with_opt" = "yes"; then
	    OPT_FLAG_C=-O3
	    OPT_FLAG_F=-O3
	fi
    ;;
    dg*|DG* ) 
	if test "$with_f2c" = "no"; then
	    SYS_FLAGS_F77="-novms -f77"
	    if test "$with_debug" = "yes"; then
		DEBUG_FLAG_F="-g -ga -X18"
	    fi
	fi
    ;;
    hp*|HP* ) 

	# Optimization levels higher than 1 may not be worth it.  Also,
	# HP's optimizing preprocessor may not alway be reliable, so use
	# at your own risk.

	if test "$with_opt2" = "yes" -a "$with_gcc" = "no"; then
	    OPT_FLAG_C="+O3 +OS"
	fi

	# When with_warn is set, most or all warnings are enabled.
	# Also use the following:
	#  -z	  turns off run-time dereferencing of NULL pointers (ld option)
	#  +ESlit puts const data and strings in read-only memory (c89 option)

	if test "$with_warn" = "yes" -a "$with_gcc" = "no"; then
	    SYS_FLAGS_LC=-z
	    SYS_FLAGS_C="+w1 +ESlit"
	    SYS_FLAGS_F=-w
	fi

	# Handling of C++ exceptions

	if test "$with_gcc" = "no"; then
	    if test "$CXX" = "KCC"; then
		SYS_FLAGS_CXX=
		SYS_FLAGS_LCXX=
	    else
		SYS_FLAGS_CXX=+eh
		SYS_FLAGS_LCXX=+eh
	    fi
	fi

	# Profiling
	# Should not be used with shared libraries.

	if test "$with_profile" = "yes" -a "$with_gcc" = "no"; then
	    with_shlib=no
	    PROF_FLAG_C=-G
	    PROF_FLAG_F=-G
	    PROF_FLAG_LC=-G
	    PROF_FLAG_LF=-G
	fi

	# Shut off shared libraries if debugging.

	if test "$with_debug" = "yes"; then
	    if test "$with_gcc" = "no"; then
		with_shlib=no
		AC_MSG_RESULT([warning: debug support requires with_shlib=no on HP, setting with_shlib=no])
	    fi
	fi

	# Double precision -- use auto promotion from Fortran.  This works
	# only under 9.x -- the 8.x Fortran compiler only has -R8, which
	# promotes only constants and not variables.  You can use a macro
	# for real or different sets of sources to get both single and
	# double, though, and in fact I do this with the stub interface
	# (for a bit more portability).  So if your Fortran compiler
	# doesn't support promotion of real constants AND variables, the
	# library will still build, but you won't be able to build the
	# Fortran example programs in double precision.

	if test $with_double = "yes" -a "$with_f2c" = "no"; then
	    DBL_FLAG_F=+autodblpad
	fi
    ;;
    irix*|IRIX*|Irix*|sgi*|SGI* ) 
    ;;
    IRIX64-6.2 )
	    SYS_FLAGS_C="-D_BSD_SIGNALS"
    ;;
    linux*|LINUX*|Linux* ) 
    ;;
    next*|NeXT*|NEXT* ) 
    ;;
    SunOS-* )
	if test "$with_profile" = "yes"; then
	    PROF_FLAG_LC="-p -static"
	fi
    ;;
    sx*|Sx*|SX*|monte*|Monte*|MONTE* ) 
	LDF="f77 -w"

	# ALWAYS ALWAYS use the -b option otherwise some things get passed by
	# value instead of by reference (demonstrating once again that truth is
	# stranger than fiction).

	SYS_FLAGS_F="-pvctl nomsg -b"
	if test "$with_warn" = "yes"; then
	    SYS_FLAGS_F="-e1 $SYS_FLAGS_F"
	else
	    SYS_FLAGS_F="-e2 $SYS_FLAGS_F"
	fi

	if test "$with_opt" = "yes"; then
	    OPT_FLAG_F="-O nomsg"
	fi

	if test "$with_double" = "yes"; then
	    DBL_FLAG_F="-A dbl4"
	fi
    ;;
    ultrix*|ULTRIX* ) 

	# Profiling (needs checking)

	if test "$with_profile" = "yes"; then
	    PROF_FLAG_LC="-p -static"
	fi
    ;;
    CRAY* ) 
	machine=`uname -m`
	if test "$with_debug" = "yes" ; then
	    DEBUG_FLAG_F="-Wf\"-ez\""
	fi

	OPT_FLAG_F= 

	case "$machine" in
	    CRAY-2 )
		SYS_FLAGS_C="-h pagelm"
	    ;;
	esac

	if test "$with_profile" = "yes" ; then
	    PROF_FLAG_C=
	    PROF_FLAG_F=
	    PROF_FLAG_LC=
	    PROF_FLAG_LF=
	    DEBUG_FLAG_C=-Gp
	    DEBUG_FLAG_LC=-Gp
	    DEBUG_FLAG_LF= 

	    case "$machine" in
		CRAY-2 )
		    LIBS=$LIBS -lprof -lsci -lu
		;;
		* )
		    LIBS=$LIBS -lprof -lsci
		;;
	    esac
	fi
    ;;
    * ) 	
    ;;
esac

# ----------------------------------------------------------------------------
# Assemble finished compiler flags.
# ----------------------------------------------------------------------------

CC_FLAGS="-c $DBL_FLAG_C $DEBUG_FLAG_C $SYS_FLAGS_C $PROF_FLAG_C $OPT_FLAG_C"
LDC_FLAGS="$PROF_FLAG_LC $SYS_FLAGS_LC $DEBUG_FLAG_LC"

if test "$enable_cxx" = "yes"; then
    CXX_FLAGS="-c $DBL_FLAG_CXX $DEBUG_FLAG_CXX $SYS_FLAGS_CXX $PROF_FLAG_CXX $OPT_FLAG_CXX"
    LDCXX_FLAGS="$PROF_FLAG_LCXX $SYS_FLAGS_LCXX $DEBUG_FLAG_LCXX"
fi

if test "$enable_f77" = "yes"; then
    F77_FLAGS="-c $DBL_FLAG_F $DEBUG_FLAG_F $SYS_FLAGS_F $PROF_FLAG_F $OPT_FLAG_F"
    LDF_FLAGS="$PROF_FLAG_LF $SYS_FLAGS_LF $DEBUG_FLAG_LF"
fi

M4_FLAGS="-S2000 -B8192"

AC_SUBST(CC)
AC_SUBST(CXX)
AC_SUBST(OCC)
AC_SUBST(F77)
AC_SUBST(LDC)
AC_SUBST(LDCXX)
AC_SUBST(LDF)

AC_SUBST(CXX_FLAGS)
AC_SUBST(CC_FLAGS)
AC_SUBST(LDC_FLAGS)
AC_SUBST(LDCXX_FLAGS)
AC_SUBST(F77_FLAGS)
AC_SUBST(LDF_FLAGS)
