Hi,

I have attempted a rewrite of the above two macros, after several
people asserted that there was a (although harmless) logical glitch.

The macro now distinguishes 3 independent types of behaviour:
  - case mangling
  - underscore appending (normal)
  - additional underscore for symbols already containing one.

I went for a complete rewrite with for- loops instead of recursive
macros. This makes for more lengthy code, but it is IMO more readable than
before. The code doesn't use non-"ac" shell variables any more (before:
f77_case and f77_underscore). 

F77_WRAPPERS remains similar, but $ac_cv_f77_mangling is directly
evaluated in the case statement (and there are 2 possibilities which
weren't there before).

I am sending this code not as a patch but just my new version, because
I changed quite a bit and it's more readable this way. Anyway, I guess 
people need to look over it.

I tested the new macros on Linux, AIX, and IRIX.

Regards,
Martin

# AC_F77_NAME_MANGLING
# --------------------
# Test for the name mangling scheme used by the Fortran 77 compiler.
# 
# Sets ac_cv_f77_mangling. The value contains three fields, separated by commas:
#
# lower case / upper case: 
#    case translation of the Fortan 77 symbols
# underscore / no underscore: 
#    whether the compiler appends "_" to symbol names 
# extra underscore / no extra underscore:
#    whether the compiler appends an extra "_" to symbol names already 
#    containing at least one underscore
#
AC_DEFUN([AC_F77_NAME_MANGLING],
[AC_REQUIRE([AC_PROG_CC])dnl
AC_REQUIRE([AC_PROG_F77])dnl
AC_REQUIRE([AC_F77_LIBRARY_LDFLAGS])dnl
AC_CACHE_CHECK([for Fortran 77 name-mangling scheme],
               ac_cv_f77_mangling,
[AC_LANG_PUSH(Fortran 77)
AC_COMPILE_IFELSE(
[      subroutine foobar()
      return
      end
      subroutine foo_bar()
      return
      end],
[mv conftest.${ac_objext} cf77_test.${ac_objext}

  AC_LANG_PUSH(C)

  ac_save_LIBS=$LIBS
  LIBS="cf77_test.${ac_objext} $FLIBS $LIBS"

  ac_success=no
  for ac_foobar in foobar FOOBAR; do
    for ac_underscore in "" "_"; do
      ac_func="$ac_foobar$ac_underscore"
      AC_TRY_LINK_FUNC($ac_func,
         [ac_success=yes; break 2])
    done
  done
  
  if test "$ac_success" = "yes"; then
     case $ac_foobar in
        foobar)
           ac_case=lower
           ac_foo_bar=foo_bar
           ;;
        FOOBAR)
           ac_case=upper
           ac_foo_bar=FOO_BAR
           ;;
     esac       

     ac_success_double=no
     for ac_double in "" "_"; do
        ac_func="$ac_foo_bar$ac_underscore$ac_double"
        AC_TRY_LINK_FUNC($ac_func,
        [ac_success_double=yes; break])
     done

     if test "$ac_success_double" = "yes"; then
        ac_cv_f77_mangling="$ac_case case"
        if test -z "$ac_underscore"; then
           ac_cv_f77_mangling="$ac_cv_f77_mangling, no underscore"
        else
           ac_cv_f77_mangling="$ac_cv_f77_mangling, underscore"
        fi
        if test -z "$ac_double"; then
           ac_cv_f77_mangling="$ac_cv_f77_mangling, no extra underscore"
        else
           ac_cv_f77_mangling="$ac_cv_f77_mangling, extra underscore"
        fi
      else             
        ac_cv_f77_mangling="unknown"
      fi
  else
     ac_cv_f77_mangling="unknown"
  fi

  LIBS=$ac_save_LIBS
  AC_LANG_POP()dnl
  rm -f cf77_test*])
AC_LANG_POP()dnl
])
])# AC_F77_NAME_MANGLING


# AC_F77_WRAPPERS
# ---------------
# Defines C macros F77_FUNC(name,NAME) and F77_FUNC_(name,NAME) to
# properly mangle the names of C identifiers, and C identifiers with
# underscores, respectively, so that they match the name mangling
# scheme used by the Fortran 77 compiler.
#
AC_DEFUN([AC_F77_WRAPPERS],
[AC_REQUIRE([AC_F77_NAME_MANGLING])dnl
AH_TEMPLATE([F77_FUNC],
    [Define to a macro mangling the given C identifier (in lower and upper
     case), which must not contain underscores, for linking with Fortran.])dnl
AH_TEMPLATE([F77_FUNC_],
    [As F77_FUNC, but for C identifiers containing underscores.])dnl
case $ac_cv_f77_mangling in
  "lower case, no underscore, no extra underscore")
          AC_DEFINE([F77_FUNC(name,NAME)],  [name])
          AC_DEFINE([F77_FUNC_(name,NAME)], [name]) ;;
  "lower case, no underscore, extra underscore")
          AC_DEFINE([F77_FUNC(name,NAME)],  [name])
          AC_DEFINE([F77_FUNC_(name,NAME)], [name ## _]) ;;
  "lower case, underscore, no extra underscore")
          AC_DEFINE([F77_FUNC(name,NAME)],  [name ## _])
          AC_DEFINE([F77_FUNC_(name,NAME)], [name ## _]) ;;
  "lower case, underscore, extra underscore")
          AC_DEFINE([F77_FUNC(name,NAME)],  [name ## _])
          AC_DEFINE([F77_FUNC_(name,NAME)], [name ## __]) ;;
  "upper case, no underscore, no extra underscore")
          AC_DEFINE([F77_FUNC(name,NAME)],  [NAME])
          AC_DEFINE([F77_FUNC_(name,NAME)], [NAME]) ;;
  "upper case, no underscore, extra underscore")
          AC_DEFINE([F77_FUNC(name,NAME)],  [NAME])
          AC_DEFINE([F77_FUNC_(name,NAME)], [NAME ## _]) ;;
  "upper case, underscore, no extra underscore")
          AC_DEFINE([F77_FUNC(name,NAME)],  [NAME ## _])
          AC_DEFINE([F77_FUNC_(name,NAME)], [NAME ## _]) ;;
  "upper case, underscore, extra underscore")
          AC_DEFINE([F77_FUNC(name,NAME)],  [NAME ## _])
          AC_DEFINE([F77_FUNC_(name,NAME)], [NAME ## __]) ;;
  *)
          AC_MSG_WARN(unknown Fortran 77 name-mangling scheme)
          ;;
esac
])# AC_F77_WRAPPERS

-- 
Martin Wilck <[EMAIL PROTECTED]>
Institute for Tropospheric Research, Permoserstr. 15, D-04318 Leipzig, Germany
Tel. +49-341-2352151 / Fax +49-341-2352361

Reply via email to