# Process this file with autoconf to produce a configure script.
#   Copyright (C) 1995 Free Software Foundation, Inc.
#   Contributed by Dave Love (d.love@dl.ac.uk).
#
#This file is part of GNU Fortran.
#
#GNU Fortran is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2, or (at your option)
#any later version.
#
#GNU Fortran is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with GNU Fortran; see the file COPYING.  If not, write to
#the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

AC_INIT(libF77/Version.c)

AC_REVISION(1.10)

dnl Checks for programs.
# For g77 we'll set CC to point at the built gcc, but this will get it into
# the makefiles
AC_PROG_CC
dnl AC_C_CROSS
dnl Gives misleading `(cached)' message from the check.
if test "$CROSS";then
  ac_cv_c_cross=yes
else
  ac_cv_c_cross=no
fi

dnl for g77 build maybe use $(RANLIB_FOR_TARGET) always (like wise AR)
if test "$ac_cv_c_cross" = yes; then
  RANLIB=$RANLIB_FOR_TARGET
  AR=$AR_FOR_TARGET
  AC_SUBST(RANLIB)
else
  AC_PROG_RANLIB
  AR=ar
  RANLIB_TEST_FOR_TARGET=true
fi
AC_SUBST(AR)
AC_SUBST(AR_FOR_TARGET)
AC_SUBST(RANLIB_FOR_TARGET)
AC_SUBST(RANLIB_TEST_FOR_TARGET)
dnl not needed for g77?
dnl AC_PROG_MAKE_SET

dnl Checks for libraries.

dnl Checks for header files.
AC_HEADER_STDC
dnl We could do this if we didn't know we were using gcc
dnl AC_MSG_CHECKING(for prototype-savvy compiler)
dnl AC_CACHE_VAL(ac_cv_sys_proto,                
dnl   [AC_TRY_LINK(,                             
dnl dnl looks screwy because TRY_LINK expects a function body
dnl    [return 0;} int foo (int * bar) {],       
dnl    ac_cv_sys_proto=yes,                      
dnl    [ac_cv_sys_proto=no                       
dnl     AC_DEFINE(KR_headers)])])                
dnl AC_MSG_RESULT($ac_cv_sys_proto)              

dnl for U77
dnl AC_CHECK_HEADERS(unistd.h)

AC_MSG_CHECKING(for posix)
AC_CACHE_VAL(ac_cv_header_posix,
  AC_EGREP_CPP(yes,
  [#include <sys/types.h>
#include <unistd.h>
#ifdef _POSIX_VERSION
  yes
#endif
], 
  ac_cv_header_posix=yes,
  ac_cv_header_posix=no))
AC_MSG_RESULT($ac_cv_header_posix)

# We can rely on the GNU library being posix-ish.  I guess checking the 
# header isn't actually like checking the functions, though...
AC_MSG_CHECKING(for GNU library)
AC_CACHE_VAL(ac_cv_lib_gnu,
  AC_EGREP_CPP(yes,
    [#include <features.h>
#ifdef __GNU_LIBRARY__
  yes
#endif
], 
  ac_cv_lib_gnu=yes, ac_cv_lib_gnu=no))
AC_MSG_RESULT($ac_cv_lib_gnu)

AC_CHECK_HEADER(fcntl.h,
   test $ac_cv_header_posix = yes && AC_DEFINE(_POSIX_SOURCE),
   AC_DEFINE(NO_FCNTL) AC_DEFINE(OPEN_DECL))

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_SIZE_T

dnl Checks for library functions.
AC_TYPE_SIGNAL
# we'll get atexit by default
if test $ac_cv_header_stdc != yes; then
AC_CHECK_FUNC(atexit,
  AC_DEFINE(onexit,atexit),dnl just in case
  [AC_DEFINE(NO_ONEXIT)
  AC_CHECK_FUNC(onexit,,
    [AC_CHECK_FUNC(on_exit,
     AC_DEFINE(onexit,on_exit),)])])
else true
fi

# This should always succeed on unix:
dnl Unfortunately, the mesage implies we're just checking for -lm...
AC_CHECK_FUNC(fstat,,AC_DEFINE(NON_UNIX_STDIO))
# This is necessary for e.g. Linux:
AC_MSG_CHECKING([for necessary members of struct FILE])
AC_CACHE_VAL(ac_cv_struct_FILE,
[AC_TRY_COMPILE([#include <stdio.h>],
  [FILE s; s._ptr; s._base; s._flag;],ac_cv_struct_FILE=yes,
  ac_cv_struct_FILE=no)])dnl
AC_MSG_RESULT($ac_cv_struct_FILE)
if test $ac_cv_struct_FILE = no; then
  AC_DEFINE(MISSING_FILE_ELEMS)
fi

dnl perhaps should check also for remainder
AC_CHECK_LIB(m,drem,AC_DEFINE(IEEE_drem))

dnl for U77:
dnl AC_CHECK_FUNCS(symlink getcwd lstat)
dnl test $ac_cv_func_symlink = yes && SYMLNK=symlnk_.o
dnl test $ac_cv_func_lstat = yes && SYMLNK="$SYMLNK lstat_.o"
dnl AC_SUBST(SYMLNK)

# posix will guarantee the right behaviour for sprintf, else we can't be
# sure; HEADER_STDC wouldn't be the right check in sunos4, for instance.
# However, on my sunos4/gcc setup unistd.h leads us wrongly to believe
# we're posix-conformant, so always do the test.
AC_MSG_CHECKING(for ansi/posix sprintf result)
dnl This loses if included as an argument to AC_CACHE_VAL because the
dnl changequote doesn't take effect and the [] vanish.
dnl fixme: use cached value
AC_TRY_RUN(changequote(<<, >>)dnl
  <<#include <stdio.h>
    /* does sprintf return the number of chars transferred? */
    main () {char foo[2]; (sprintf(foo, "1") == 1) ? exit(0) : exit(1);}
>>changequote([, ]),
    ac_cv_sys_sprintf_ansi=yes,
    ac_cv_sys_sprintf_ansi=no,
    ac_cv_sys_sprintf_ansi=no)
AC_CACHE_VAL(ac_cv_sys_sprintf_ansi,
     ac_cv_sys_sprintf_ansi=$ac_cv_sys_sprintf_ansi)
dnl We get a misleading `(cached)' message...
if test $ac_cv_c_cross = no; then
  AC_MSG_RESULT($ac_cv_sys_sprintf_ansi)
else
  AC_MSG_RESULT([can't tell -- assuming no])
fi
if test $ac_cv_sys_sprintf_ansi != yes; then 
  AC_DEFINE(USE_STRLEN)
fi

# define NON_ANSI_RW_MODES on unix (can't hurt)
AC_EGREP_CPP(yes,
[#ifdef unix
  yes
#endif
#ifdef __unix
  yes
#endif
#ifdef __unix__
  yes
#endif
], is_unix=yes, is_unix=no)
test $is_unix = yes && AC_DEFINE(NON_ANSI_RW_MODES)

# We have to firkle with the info in hconfig.h to figure out suitable types
# (via com.h).  proj.h and com.h are in $srcdir/.., config.h which they need
# is in ../.. and the config files are in $srcdir/../../config.
AC_MSG_CHECKING(f2c integer type)
late_ac_cpp=$ac_cpp
ac_cpp="$late_ac_cpp -I$srcdir/.. -I../.. -I$srcdir/../../config"
AC_CACHE_VAL(ac_cv_sys_f2cinteger,
AC_EGREP_CPP(F2C_INTEGER=long int,
[#include "proj.h"
#define FFECOM_DETERMINE_TYPES 1
#include "com.h"
#if FFECOM_f2cINTEGER == FFECOM_f2ccodeLONG
F2C_INTEGER=long int
#elif FFECOM_f2cINTEGER == FFECOM_f2ccodeINT
F2C_INTEGER=int
#else
#  error Cannot find a suitable type for F2C_INTEGER
#endif
],
 ac_cv_sys_f2cinteger="long int",)
if test "$ac_cv_sys_f2cinteger" = ""; then
  AC_EGREP_CPP(F2C_INTEGER=int,
[#include "proj.h"
#define FFECOM_DETERMINE_TYPES 1
#include "com.h"
#if FFECOM_f2cINTEGER == FFECOM_f2ccodeLONG
F2C_INTEGER=long int
#elif FFECOM_f2cINTEGER == FFECOM_f2ccodeINT
F2C_INTEGER=int
#else
#  error Cannot find a suitable type for F2C_INTEGER
#endif
],
   ac_cv_sys_f2cinteger=int,)
fi
if test "$ac_cv_sys_f2cinteger" = ""; then
  AC_MSG_RESULT("")
  AC_MSG_ERROR([Can't determine type for f2c integer; config.log may help.])
fi
)
AC_MSG_RESULT($ac_cv_sys_f2cinteger)
F2C_INTEGER=$ac_cv_sys_f2cinteger
ac_cpp=late_ac_cpp
AC_SUBST(F2C_INTEGER)

dnl maybe check for drem/remainder

AC_SUBST(CROSS)
AC_SUBST(RANLIB_FOR_TARGET)
AC_SUBST(RANLIB_TEST_FOR_TARGET)

dnl Craig had these in f2c.h, but they're only relevant for building libf2c
dnl anyway.

dnl   For GNU Fortran (g77), we always enable the following behaviors for
dnl   libf2c, to make things easy on the programmer.  The alternate
dnl   behaviors have their uses, and g77 might provide them as compiler,
dnl   rather than library, options, so only a single copy of a shared libf2c
dnl   need be built for a system.

dnl   This makes unformatted I/O more consistent in relation to other
dnl   systems.  It is not required by the F77 standard.

AC_DEFINE(Pad_UDread)

dnl   This makes ERR= and IOSTAT= returns work properly in disk-full
dnl   situations, making things work more as expected.  It slows things
dnl   down, so g77 will probably someday choose the original implementation
dnl   on a case-by-case basis when it can be shown to not be necessary
dnl   (e.g. no ERR= or IOSTAT=) or when it is given the appropriate
dnl   compile-time option or, perhaps, source-code directive.

AC_DEFINE(ALWAYS_FLUSH)


# avoid confusion in case the `makefile's from the f2c distribution have
# got put here
test -f libF77/makefile && mv libF77/makefile  libF77/makefile.ori
test -f libI77/makefile && mv libI77/makefile  libI77/makefile.ori

AC_OUTPUT(Makefile ../../include/f2c.h:f2c.h.in libI77/Makefile libF77/Makefile)

dnl We might have configuration options to:
dnl * allow non-standard string concatenation (use libF77 s_catow.o,
dnl   not s_cat.o)
dnl * change unit preconnexion in libI77/err.c (f_init.c)
dnl * -DALWAYS_FLUSH in libI77
dnl * -DOMIT_BLANK_CC in libI77
