# --*-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..
# -----------------------------------------------------------------------

# -----------------------------------------------------------------------
# Compiler/linker variables
#
# The following shell variables are used.  They can be redefined as
# appropriate for specific systems.
#
# 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"

# Debugging

if test "$with_debug" = "yes"; then
    with_opt="no"
    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="-O"
    OPT_FLAG_CXX="-O"
    OPT_FLAG_F="-O"
fi

# 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.  Systems that use gcc or f2c
# by default should set shell variables as follows: with_gcc=yes (to get
# gcc), or with_f2c=yes (to get f2c).  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 -ansi"
	;;
	linux*|LINUX*|Linux* ) 
	    with_gcc="yes"
	;;
	next*|NeXT*|NEXT* ) 
	    with_gcc="yes"
	;;
	SunOS-4* )
	    CC="acc"
	;;
	sx*|Sx*|SX*|monte*|Monte*|MONTE* ) 
	    CC="cc -hansi"
	    with_gcc="no"
	;;
	ultrix*|ULTRIX* ) 
	    with_gcc="yes"
	;;
	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 Fortran compiler

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 a cc-like set of command line flags.

	    F77="fort77"
	;;
	linux*|LINUX*|Linux* ) 
	    with_f2c="yes"
	;;
	next*|NeXT*|NEXT* ) 
	    with_f2c="yes"
	;;
	sx*|Sx*|SX*|monte*|Monte*|MONTE* ) 
	    # The f77 front-end uses a cc-like set of 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

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

if test "$with_gcc" = "yes"; then
    CC="gcc"
    OCC="gcc -traditional"

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

    # There are very few platforms on which gcc supports shared libs
    # For now, just disable.

    if test "$with_shlib" = "yes"; then
	echo "Warning: no support for shared libs with gcc yet"
	with_shlib="no"
    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 "$with_f2c" = "yes"; then
    AC_PROGRAM_CHECK(found_fc, fc, "yes", "no")
    if test "$found_fc" = "yes" ; then
	F77="fc"
    else
	AC_PROGRAM_CHECK(found_f77, f77, "yes", "no")
	if test "$found_f77" = "yes" ; then
	    F77="f77"
	else
	    echo "Warning: could not find \"fc\" or \"f77\""
	    with_f2c="no"
	fi
    fi

    DEBUG_FLAG_F=
    OPT_FLAG_F=
    PROF_FLAG_F=
    SYS_FLAGS_F=
fi

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

if test -z "$LDC"; then
    LDC="$CC"
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_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 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

	# 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
	    with_shlib="no"
	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* ) 
    ;;
    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

# -----------------------------------------------------------------------
# Set flags to generate shared libraries for systems that we know about.
# Taken from BLT configure.in with some modifications.
# -----------------------------------------------------------------------

echo "checking how to make shared libraries"
SHLIB_CCFLAGS=""
SHLIB_BUILD=""
SHLIB_SUFFIX=""
case "$system" in 
    SunOS-4* ) 
	SHLIB_CCFLAGS="-pic"
	SHLIB_F77FLAGS="-pic"
	SHLIB_BUILD="ld -assert pure-text -o"
	SHLIB_SUFFIX='.so.$(MAJOR_VERSION).$(MINOR_VERSION)'
	SALIB_SUFFIX='.sa.$(MAJOR_VERSION).$(MINOR_VERSION)'
    ;;
    HP-UX-* )
	SHLIB_CCFLAGS="+z"
	SHLIB_F77FLAGS="+z"
	SHLIB_BUILD="ld -b -o"
	SHLIB_SUFFIX=".sl"
    ;;

    # The rest are probably broken.  Someone please fix them.
    # Remove the 'with_shlib="no"' line, comments, and go wild.

    SunOS-5* )
	with_shlib="no"
#	SHLIB_CCFLAGS="-K pic"
#	SHLIB_F77FLAGS="-K pic"
#	SHLIB_BUILD="$CC '-G -ztext -h $(SHARED_LIBRARY)'"
#	SHLIB_SUFFIX='.so.$(MAJOR_VERSION)'
    ;;
    OSF-1.* )
	with_shlib="no"
#	SHLIB_CCFLAGS="-fpic"
#	SHLIB_F77FLAGS="-fpic"
#	SHLIB_BUILD="$CC -shared"
#	SHLIB_SUFFIX='.so.$(MAJOR_VERSION)'
    ;;
    IRIX-5.* )
	with_shlib="no"
#	SHLIB_CCFLAGS="-KPIC"
#	SHLIB_F77FLAGS="-KPIC"
#	SHLIB_BUILD="$CC -shared"
#	SHLIB_SUFFIX='.so.$(MAJOR_VERSION)'
    ;;
    * )
	echo "Don't know how to make shared libraries for $system"
	with_shlib="no"
    ;;
esac

if test ! -z "SHLIB_CCFLAGS" ; then 
    if test "$compiler" = "gcc" ; then
	SHLIB_CCFLAGS="-fpic"
    fi
fi

AC_SUBST(SHLIB_BUILD)
AC_SUBST(SHLIB_SUFFIX)
AC_SUBST(SALIB_SUFFIX)

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

CC_FLAGS_NOOPT=\
"-c $DBL_FLAG_C $DEBUG_FLAG_C $SYS_FLAGS_C $PROF_FLAG_C"

CXX_FLAGS_NOOPT=\
"-c $DBL_FLAG_CXX $DEBUG_FLAG_CXX $SYS_FLAGS_CXX $PROF_FLAG_CXX"

F77_FLAGS_NOOPT=\
"-c $DBL_FLAG_F $DEBUG_FLAG_F $SYS_FLAGS_F $PROF_FLAG_F"

CC_FLAGS=\
"$CC_FLAGS_NOOPT $OPT_FLAG_C"

CXX_FLAGS=\
"$CXX_FLAGS_NOOPT $OPT_FLAG_CXX"

F77_FLAGS=\
"$F77_FLAGS_NOOPT $OPT_FLAG_F"

LDC_FLAGS=\
"$PROF_FLAG_LC $SYS_FLAGS_LC $DEBUG_FLAG_LC"

LDCXX_FLAGS=\
"$PROF_FLAG_LCXX $SYS_FLAGS_LCXX $DEBUG_FLAG_LCXX"

LDF_FLAGS=\
"$PROF_FLAG_LF $SYS_FLAGS_LF $DEBUG_FLAG_LF"

M4_FLAGS="-S2000 -B8192 -DSYSTEM=$SYSTEM $DBL_FLAG_M4"

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

AC_SUBST(CC_FLAGS)
AC_SUBST(LDC_FLAGS)
AC_SUBST(F77_FLAGS)
AC_SUBST(LDF_FLAGS)
AC_SUBST(SHLIB_CCFLAGS)
AC_SUBST(SHLIB_F77FLAGS)

# -----------------------------------------------------------------------
# Hacks to deal with optimizer failures.
# -----------------------------------------------------------------------

CC_FLAGS_XWIN=$CC_FLAGS
CC_FLAGS_SCCONT=$CC_FLAGS

case "$system" in 
    aix*|AIX*|rs*|RS*|ibm*|IBM* ) 
	CC_FLAGS_XWIN=$CC_FLAGS_NOOPT
    ;;

    sx*|Sx*|SX*|monte*|Monte*|MONTE* ) 
	CC_FLAGS_SCCONT=$CC_FLAGS_NOOPT
    ;;
esac

AC_SUBST(CC_FLAGS_XWIN)
AC_SUBST(CC_FLAGS_SCCONT)

