autoconf-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

AC_FC_CHECK_BOUNDS for Fortran array bounds checking. (was: flags for di


From: Ralf Wildenhues
Subject: AC_FC_CHECK_BOUNDS for Fortran array bounds checking. (was: flags for different fortran compilers)
Date: Sun, 27 Feb 2011 17:10:28 +0100
User-agent: Mutt/1.5.20 (2010-08-04)

[ moving from autoconf@ to -patches@ ]

Hello,

here's a first macro to improve Fortran support in Autoconf.
It uses Fortran 90 interface declarations, so a F77 version
would have to be different (and I'm not quite sure whether
this works as well in Fortran 77).  I only have access to a
few of the listed compilers, the other flags are taken from
manuals only.  I gather fixes will come from users over time.

OK to push?  Eve-Marie, OK to add your name and email to THANKS?

Thanks,
Ralf

    New macro AC_FC_CHECK_BOUNDS to enable Fortran array bounds checking.
    
    * lib/autoconf/fortran.m4 (AC_FC_CHECK_BOUNDS): New macro.
    * doc/autoconf.texi (Fortran Compiler): Document it.
    * tests/fortran.at (AC_FC_CHECK_BOUNDS): New test.
    * NEWS, THANKS: Update.
    Prompted by report from Eve-Marie Devaliere.

diff --git a/NEWS b/NEWS
index 3368d87..534bb17 100644
--- a/NEWS
+++ b/NEWS
@@ -14,6 +14,9 @@ GNU Autoconf NEWS - User visible changes.
    subdirectories if they have not been configured yet, so that an initial
    makefile can be generated for a new sub package.
 
+** New macro AC_FC_CHECK_BOUNDS to enable array bounds checking in the Fortran
+   compiler.
+
 
 ** Macros
 
diff --git a/doc/autoconf.texi b/doc/autoconf.texi
index 1ccfcd3..a9bd40e 100644
--- a/doc/autoconf.texi
+++ b/doc/autoconf.texi
@@ -8246,6 +8246,16 @@ Fortran Compiler
 message).
 @end defmac
 
address@hidden AC_FC_CHECK_BOUNDS (@ovar{action-if-success}, 
@ovar{action-if-failure})
address@hidden
+
+The @code{AC_FC_CHECK_BOUNDS} macro tries to enable array bounds checking
+in the Fortran compiler.  If successful, the @var{action-if-success}
+is called and any needed flags are added to @code{FCFLAGS}.  Otherwise,
address@hidden is called, which defaults to failing with an error
+message.
address@hidden defmac
+
 
 @node Go Compiler
 @subsection Go Compiler Characteristics
diff --git a/lib/autoconf/fortran.m4 b/lib/autoconf/fortran.m4
index d5afbb9..de56323 100644
--- a/lib/autoconf/fortran.m4
+++ b/lib/autoconf/fortran.m4
@@ -1357,3 +1357,75 @@ else
 fi
 AC_LANG_POP([Fortran])dnl
 ])# AC_FC_LINE_LENGTH
+
+
+# AC_FC_CHECK_BOUNDS([ACTION-IF-SUCCESS], [ACTION-IF-FAILURE = FAILURE])
+# ----------------------------------------------------------------------
+# Look for a compiler flag to turn on array bounds checking for the
+# Fortran (FC) compiler, and adds it to FCFLAGS.  Call
+# ACTION-IF-SUCCESS (defaults to nothing) if successful (i.e. can
+# compile code using new extension) and ACTION-IF-FAILURE (defaults to
+# failing with an error message) if not.  (Defined via DEFUN_ONCE to
+# prevent flag from being added to FCFLAGS multiple times.)
+#
+# The known flags are:
+#     -fbounds-check: GNU g77, gfortran
+# -CB, -check bounds: Intel compiler (icc, ecc, ifort)
+#                 -C: Sun/Oracle compiler (f95)
+#        -C, -qcheck: IBM compiler (xlf)
+#           -Mbounds: Portland Group compiler
+#  -C, -check_bounds: SGI compiler
+# -check_bounds, +check=all: HP Fortran
+#        -C, -Rb -Rc: Absoft (-Rb: array boundaries, -Rc: array conformance)
+#            --check: Lahey
+AC_DEFUN_ONCE([AC_FC_CHECK_BOUNDS],
+[AC_LANG_PUSH([Fortran])dnl
+AC_CACHE_CHECK([for Fortran flag to enable array-bounds checking],
+               [ac_cv_fc_check_bounds],
+[ac_cv_fc_check_bounds=unknown
+ac_fc_check_bounds_FCFLAGS_save=$FCFLAGS
+for ac_flag in -fbounds-check -check_bounds -Mbounds -qcheck \
+               '-check bounds' +check=all --check '-Rb -Rc' -CB -C
+do
+  FCFLAGS="$ac_fc_check_bounds_FCFLAGS_save $ac_flag"
+  # We should be able to link a correct program.
+  AC_LINK_IFELSE([AC_LANG_PROGRAM([], [])],
+    [AC_LINK_IFELSE([[
+      subroutine sub(a)
+      integer a(:)
+      a(8) = 0
+      end subroutine
+
+      program main
+      integer a(1:7)
+      interface
+         subroutine sub(a)
+         integer a(:)
+         end subroutine
+      end interface
+
+      call sub(a)
+      end program]],
+       [# If we can run the program, require failure at run time.
+       # In cross-compiling mode, we rely on the compiler not accepting
+       # unknown options.
+       AS_IF([test "$cross_compiling" = yes],
+         [ac_cv_fc_check_bounds=$ac_flag; break],
+         [AS_IF([_AC_DO_TOKENS(./conftest$ac_exeext)],
+            [],
+            [ac_cv_fc_check_bounds=$ac_flag; break])])])])
+done
+rm -f conftest$ac_exeext conftest.err conftest.$ac_objext conftest.$ac_ext
+FCFLAGS=$ac_fc_check_bounds_FCFLAGS_save
+])
+if test "x$ac_cv_fc_check_bounds" = xunknown; then
+  m4_default([$2],
+             [AC_MSG_ERROR([no Fortran flag for bounds checking found], 77)])
+else
+  if test "x$ac_cv_fc_check_bounds" != xnone; then
+    FCFLAGS="$FCFLAGS $ac_cv_fc_check_bounds"
+  fi
+  $1
+fi
+AC_LANG_POP([Fortran])dnl
+])# AC_FC_CHECK_BOUNDS
diff --git a/tests/fortran.at b/tests/fortran.at
index f0fec7c..c4dd68e 100644
--- a/tests/fortran.at
+++ b/tests/fortran.at
@@ -930,3 +930,62 @@ EOF
 done
 
 AT_CLEANUP
+
+
+## ------------------- ##
+## AC_FC_CHECK_BOUNDS. ##
+## ------------------- ##
+
+AT_SETUP([AC_FC_CHECK_BOUNDS])
+
+AT_DATA([Makefile.in],
address@hidden@: address@hidden@
+       @FC@ @FCFLAGS@ -o $@ address@hidden@ @LIBS@
+
+.SUFFIXES: .f address@hidden@
address@hidden@:
+       @FC@ @FCFLAGS@ -c @FCFLAGS_f@ $<
+
+clean:
+       rm -f address@hidden@ address@hidden@
+]])
+
+cat >configure.ac <<EOF
+AC_INIT
+AC_PROG_FC
+AC_FC_SRCEXT([f])
+AC_FC_CHECK_BOUNDS
+AC_CONFIG_FILES([Makefile])
+AC_OUTPUT
+EOF
+
+AT_DATA([prog.f],
+[[
+      subroutine copy(a,b,n)
+      integer a(:), b(:), n, i
+      do i = 1, n
+         a(i) = b(i)
+      end do
+      end subroutine
+
+      program main
+      integer, parameter :: n = 20
+      integer a(1:n), b(1:n-1), i
+      interface
+         subroutine copy(a,b,n)
+         integer a(:), b(:), i
+         end subroutine
+      end interface
+
+      call copy(a,b,n)
+      end program
+]])
+
+AT_CHECK_AUTOCONF
+AT_CHECK_CONFIGURE
+: "${MAKE=make}"
+AT_CHECK([$MAKE], [], [ignore], [ignore])
+AT_CHECK([./prog || exit 1], [1], [ignore], [ignore])
+AT_CHECK([$MAKE clean], [], [ignore], [ignore])
+
+AT_CLEANUP



reply via email to

[Prev in Thread] Current Thread [Next in Thread]