From 158394724ed9c1558a281552fa274aa1de557362 Mon Sep 17 00:00:00 2001
From: Jorrit Fahlke <joe@dune-project.org>
Date: Mon, 11 Jan 2010 12:12:54 +0000
Subject: [PATCH] LDFLAGS-transition for the MPI stuff:   - Introduce autoconf
 substitutions DUNEMPICPPFLAGS, DUNEMPILDFLAGS and     DUNEMPILIBS.   -
 Deprecate MPI_LDFLAGS and MPI_CPPFLAGS: the automake manual recommends    
 against "_" in names of such variables, MPI_LDFLAGS actually contains    
 mainly libraries and simply changing MPI_LDFLAGS to MPI_LIBS will probably   
  break a lot of stuff.  By deprecating MPI_CPPFLAGS it is possible to make   
  the preprocessor generate a deprecation warning with instructions to    
 change both MPI_CPPFLAGS and MPI_LDFLAGS (though this has yet to be    
 implemented).

[[Imported from SVN: r5806]]
---
 bin/mpi-config                  |  14 +++-
 doc/buildsystem/buildsystem.tex |  72 ++++++++++++++++---
 m4/dune_mpi.m4                  |  24 +++++--
 m4/mpi-config.m4                | 118 ++++++++++++++++----------------
 4 files changed, 151 insertions(+), 77 deletions(-)

diff --git a/bin/mpi-config b/bin/mpi-config
index 64f0ce52e..669be9718 100755
--- a/bin/mpi-config
+++ b/bin/mpi-config
@@ -104,6 +104,9 @@ while test $# -gt 0 ; do
   --cflags)
     tasks="$tasks print_cflags"
     ;;
+  --ldflags)
+    tasks="$tasks print_ldflags"
+    ;;
   --libs)
     tasks="$tasks print_libs"
     ;;
@@ -169,14 +172,19 @@ print_mpiversion() {
 print_cflags() {
   get_mpiparameters
   if test x$disablecxx = xyes; then
-    MPI_CPPFLAGS="$MPI_CPPFLAGS $MPI_NOCXXFLAGS"
+    DUNEMPICPPFLAGS="$DUNEMPICPPFLAGS $MPI_NOCXXFLAGS"
   fi
-  echo $MPI_CPPFLAGS
+  echo $DUNEMPICPPFLAGS
+}
+
+print_ldflags() {
+  get_mpiparameters
+  echo $DUNEMPILDFLAGS
 }
 
 print_libs() {
   get_mpiparameters
-  echo $MPI_LDFLAGS
+  echo $DUNEMPILIBS
 }
 
 for task in $tasks; do
diff --git a/doc/buildsystem/buildsystem.tex b/doc/buildsystem/buildsystem.tex
index f7899cf3f..9d3f4ace1 100644
--- a/doc/buildsystem/buildsystem.tex
+++ b/doc/buildsystem/buildsystem.tex
@@ -677,6 +677,10 @@ $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS)
     \texttt{configure}, it may contain debugging and optimization options per
     default (like \texttt{-DNDEBUG}).  The value of \texttt{CPPFLAGS} always
     appears after the other preprocessor flags.
+  \item[\texttt{\textit{LIB}CPPFLAGS}] Class: {\bf external-library}.
+    Preprocessor flags when building with library \textit{LIB}.  This variable
+    should be include in \texttt{\textit{target}\_CPPFLAGS} or
+    \texttt{AM\_CPPFLAGS} in the \texttt{Makefile.am}.
   \end{description}
 
 \item[C-compiler flags] These flags are passed in any build rule that calls
@@ -743,9 +747,10 @@ $(target_LDFLAGS) $(LDFLAGS)
 \begin{lstlisting}[language=make]
 $(AM_LDFLAGS) $(LDFLAGS)
 \end{lstlisting}
-  is used.  These variables are inapropriate to pass any \texttt{-L} or
-  \texttt{-l} options or library files.  Use a variable from the {\em
-    libraries to link to} set to do that.
+  is used.  These variables are inapropriate to pass any options or parameters
+  that specify libraries of object files, in particular \texttt{-L} or
+  \texttt{-l} or the libtool options \texttt{-dlopen} and \texttt{-dlpreopen}.
+  Use a variable from the {\em libraries to link to} set to do that.
   \begin{description}
   \item[\texttt{\textit{target}\_LDFLAGS}] Class: {\bf target-specific}.
     Target-specific C++ compiler flags.  If this variable exists, it overrides
@@ -760,10 +765,15 @@ $(AM_LDFLAGS) $(LDFLAGS)
     \texttt{configure}, it may contain debugging, optimization and warning
     options per default.  The value of \texttt{LDFLAGS} always appears after
     the other linker flags.
+  \item[\texttt{\textit{LIB}LDFLAGS}] Class: {\bf external-library}.  Linker
+    flags needed when linking to library \textit{LIB}.  This variable should
+    be include in \texttt{\textit{target}\_LDFLAGS} or \texttt{AM\_LDFLAGS} in
+    the \texttt{Makefile.am}.
   \end{description}
 
 \item[libraries to link to] These variables are used to determine the
-  libraries to link to.  They are passed whenever the linker is called.  When
+  libraries and object files to link to.  They are passed whenever the linker
+  is called.  When
   linking a program, extra libraries and objects to link to are given by
   \begin{lstlisting}[language=make]
 $(target_LDADD) $(LIBS)
@@ -771,8 +781,9 @@ $(target_LDADD) $(LIBS)
   If the {\bf target-specific} variable \texttt{\textit{target}\_LDADD} is not
   defined, automake supplies
   \begin{lstlisting}[language=make]
-$(target_LDADD) = $(LDADD)
+target_LDADD = $(LDADD)
   \end{lstlisting}
+  %$\end{lstlisting}
   When linking a library, extra libraries and objects to link to are given by
   \begin{lstlisting}[language=make]
 $(target_LIBADD) $(LIBS)
@@ -780,16 +791,27 @@ $(target_LIBADD) $(LIBS)
   If the {\bf target-specific} variable \texttt{\textit{target}\_LIBADD} is
   not defined, automake defines it empty
   \begin{lstlisting}[language=make]
-$(target_LIBADD) =
+target_LIBADD =
   \end{lstlisting}
-  %$\end{lstlisting}
-  Linker flags except \texttt{-L} or \texttt{-l} are generally inapropriate
-  for there variables.
+  Libraries and objects to link to must be given in reverse order: a library
+  or object file must come before the libraries or object files it depends on
+  on the linker command line.  Thus the value of the \texttt{LIBS} variable is
+  included after the value of the \texttt{\textit{target}\_LDADD} or
+  \texttt{\textit{target}\_LIBADD} variable.
+
+  In general, any linker flags and argument that specify libraries and object
+  files should be included in these variables, and nothing else.  In
+  particular that means library and object file names, the options \texttt{-L}
+  and \texttt{-l}, and the libtool options \texttt{-dlopen} and
+  \texttt{-dlpreopen}.  The option \texttt{-L} should come directly before any
+  \texttt{-l} options it sets the linker path for, otherwise a path set by
+  another \texttt{-L} option may take precedence, which may happen to contain
+  a library by the same name.
   \begin{description}
   \item[\texttt{\textit{target}\_LDADD}] Class: {\bf target-specific}.
     Target-specific libraries and objects to link to {\em for programs}.  If
     this variable does not exist, it defaults to \texttt{\$(LDADD)}.
-  \item[\texttt{LDADD}] Class: {\bf target-specific}.  Libraries and objects
+  \item[\texttt{LDADD}] Class: {\bf automake}.  Libraries and objects
     to link to {\em for programs}.  Default for
     \texttt{\textit{target}\_LDADD}.
   \item[\texttt{\textit{target}\_LIBADD}] Class: {\bf target-specific}.
@@ -799,9 +821,39 @@ $(target_LIBADD) =
     the empty value is assumed.
   \item[\texttt{LIBS}] Class: {\bf automake}, {\bf configure-substituted}.
     Libraries discovered by configure.
+  \item[\texttt{\textit{LIB}LIBS}] Class: {\bf external-library}.  Libraries
+    and object files needed to linking against library \textit{LIB}, including
+    that library itself.  This variable should be include in
+    \texttt{\textit{target}\_LDADD}, \texttt{LDADD}, or
+    \texttt{\textit{target}\_LIBADD} in the \texttt{Makefile.am}.
   \end{description}
 \end{description}
 
+\minisec{Individual library variables}
+
+\begin{description}
+\item[MPI] The \texttt{DUNE\_MPI} macro sets the following variables with the
+  help of the macros \texttt{ACX\_MPI} and \texttt{MPI\_CONFIG}: For
+  compilation with the MPI compiler \texttt{MPICC} and \texttt{MPILIBS}.
+  These are not used in \dune except that \texttt{MPICC} may be set on the
+  configure command line to select which MPI installation to use.  For
+  compilation with the standard compiler it sets \texttt{DUNEMPICPPFLAGS},
+  \texttt{DUNEMPILDFLAGS} and \texttt{DUNEMPILIBS}, and the deprecated
+  variables \texttt{MPI\_CPPFLAGS} and \texttt{MPI\_LDFLAGS} (note there is no
+  \texttt{MPI\_LIBS}).  Unfortunately with most MPI implementations it is
+  impossible to obtain the linker flags seperately from the libraries to link
+  to.  Therefore, this macro stuffs everything into \texttt{DUNEMPILIBS},
+  which has the advantage that it works and the disavantage that users are
+  unable to overwrite the linker flags.  If that is a problem users should set
+  these variables themselves on the configure command line.
+
+  In addition, this macro substitutes \texttt{MPI\_VERSION} a text string
+  identifying the detected version of MPI.  It defines the following
+  preprocessor defines: \texttt{MPI\_2}, defined if the detected MPI supports
+  the MPI-2 standard.  \texttt{HAVE_MPI}, 1 if MPI is detected and enabled.
+  It also defines the automake conditional \texttt{MPI}.
+\end{description}
+
 \minisec{Conditional builds}
 
 Some parts of \dune only make sense if certain addon-packages were
diff --git a/m4/dune_mpi.m4 b/m4/dune_mpi.m4
index 5490a1afb..3e3a172eb 100644
--- a/m4/dune_mpi.m4
+++ b/m4/dune_mpi.m4
@@ -84,7 +84,7 @@ AC_DEFUN([DUNE_MPI],[
       MPICOMP="$MPICC"
 
       MPI_CONFIG()
-      MPI_CPPFLAGS="$MPI_CPPFLAGS $MPI_NOCXXFLAGS -DENABLE_MPI=1"
+      DUNEMPICPPFLAGS="$DUNEMPICPPFLAGS $MPI_NOCXXFLAGS -DENABLE_MPI=1"
 
       with_mpi="yes ($dune_MPI_VERSION)"
     ],[
@@ -99,12 +99,14 @@ AC_DEFUN([DUNE_MPI],[
 
     # store old values
     ac_save_LIBS="$LIBS"
+    ac_save_LDFLAGS="$LDFLAGS"
     ac_save_CPPFLAGS="$CPPFLAGS"
     
     # looks weird but as the -l... are contained in the MPI_LDFLAGS these
     # parameters have to be last on the commandline: with LIBS this is true
-    LIBS="$MPI_LDFLAGS"
-    CPPFLAGS="$CPPFLAGS $MPI_CPPFLAGS"
+    LIBS="$DUNEMPILIBS $LIBS"
+    LDFLAGS="$LDFLAGS $DUNEMPILDFLAGS"
+    CPPFLAGS="$CPPFLAGS $DUNEMPICPPFLAGS"
 
     # try to create MPI program
     AC_LANG_PUSH([C++])
@@ -122,7 +124,7 @@ AC_DEFUN([DUNE_MPI],[
     )
 
     AS_IF([test "x$mpiruntest" != "xyes"],[
-      AC_MSG_WARN([Diabled test whether running with $dune_MPI_VERSION works.])    
+      AC_MSG_WARN([Disabled test whether running with $dune_MPI_VERSION works.])    
     ],[
       AC_MSG_CHECKING([whether running with $dune_MPI_VERSION works])
       AC_RUN_IFELSE(
@@ -158,14 +160,24 @@ AC_DEFUN([DUNE_MPI],[
   ])
     
   # set flags
+  MPI_CPPFLAGS="$DUNEMPICPPFLAGS"
+  MPI_LDFLAGS="$DUNEMPILDFLAGS $DUNEMPILIBS"
   AS_IF([test "x$with_mpi" != "xno"],[
+    AC_SUBST(DUNEMPICPPFLAGS, $DUNEMPICPPFLAGS)
+    AC_SUBST(DUNEMPILDFLAGS, $DUNEMPILDFLAGS)
+    AC_SUBST(DUNEMPILIBS, $DUNEMPILIBS)
+
     AC_SUBST(MPI_CPPFLAGS, $MPI_CPPFLAGS)
     AC_SUBST(MPI_LDFLAGS, $MPI_LDFLAGS)
     AC_SUBST(MPI_VERSION, $dune_MPI_VERSION)
     AC_DEFINE(HAVE_MPI,ENABLE_MPI,[Define if you have the MPI library.
-    This is only true if MPI was found by configure 
-    _and_ if the application uses the MPI_CPPFLAGS])
+    This is only true if MPI was found by configure _and_ if the application
+    uses the DUNEMPICPPFLAGS (or the deprecated MPI_CPPFLAGS)])
   ],[
+    AC_SUBST(DUNEMPICPPFLAGS, "")
+    AC_SUBST(DUNEMPILDFLAGS, "")
+    AC_SUBST(DUNEMPILIBS, "")
+
     AC_SUBST(MPI_CPPFLAGS, "")
     AC_SUBST(MPI_LDFLAGS, "")
   ])
diff --git a/m4/mpi-config.m4 b/m4/mpi-config.m4
index 21f36f5b1..4b09587a4 100644
--- a/m4/mpi-config.m4
+++ b/m4/mpi-config.m4
@@ -52,16 +52,24 @@ _EOF
       # seems like LAM >= 7.1 which supports extraction of parameters without
       # dummy files
       dune_MPI_VERSION="LAM >= 7.1"
-      MPI_CPPFLAGS="$retval"
-      mpi_getflags "-showme:link"
-      MPI_LDFLAGS="$retval"
+      if test x"$DUNEMPICPPFLAGS" = x; then
+        DUNEMPICPPFLAGS="$retval"
+      fi
+      if x"$DUNEMPILIBS" = x; then
+        mpi_getflags "-showme:link"
+        DUNEMPILIBS="$retval"
+      fi
     else
       dune_MPI_VERSION="LAM < 7.1"
       # use -showme and dummy parameters to extract flags        
-      mpi_getflags "-showme" "-c $MPISOURCE"
-      MPI_CPPFLAGS="$retval"
-      mpi_getflags "-showme" "dummy.o -o dummy"
-      MPI_LDFLAGS="$retval"
+      if x"$DUNEMPICPPFLAGS" = x; then
+        mpi_getflags "-showme" "-c $MPISOURCE"
+        DUNEMPICPPFLAGS="$retval"
+      fi
+      if x"$DUNEMPILIBS" = x; then
+        mpi_getflags "-showme" "dummy.o -o dummy"
+        DUNEMPILIBS="$retval"
+      fi
     fi
     # hack in option to disable LAM-C++-bindings...
     # we fake to have mpicxx.h read already
@@ -77,49 +85,55 @@ _EOF
 }
 
 mpi_getmpichflags() {
+  if test x"$DUNEMPICPPFLAGS" = x; then
     # use special commands to extract options      
     mpi_getflags "-compile_info"
-    MPI_CPPFLAGS="$retval"
+    DUNEMPICPPFLAGS="$retval"
     # remove implicitly set -c
-    mpi_remove "$MPI_CPPFLAGS" '-c'
-    MPI_CPPFLAGS="$retval"
+    mpi_remove "$DUNEMPICPPFLAGS" '-c'
+    DUNEMPICPPFLAGS="$retval"
+  fi
     
+  if x"$DUNEMPILIBS" = x; then
     # get linker options
     mpi_getflags "-link_info"
-    MPI_LDFLAGS="$retval"
+    DUNEMPILIBS="$retval"
     # strip -o option
-    mpi_remove "$MPI_LDFLAGS" "-o"
-    MPI_LDFLAGS="$retval"
-    #strip MPI_CPPFLAGS (which are included for mpich2 on jugene)
-    enc=`echo "$MPI_CPPFLAGS" | sed -e 's/\\//\\\\\\//g'`
-    MPI_LDFLAGS=`echo "$retval" | sed -e "s/$enc / /"`
-
+    mpi_remove "$DUNEMPILIBS" "-o"
+    DUNEMPILIBS="$retval"
+    #strip DUNEMPICPPFLAGS (which are included for mpich2 on jugene)
+    enc=`echo "$DUNEMPICPPFLAGS" | sed -e 's/\\//\\\\\\//g'`
+    DUNEMPILIBS=`echo "$retval" | sed -e "s/$enc / /"`
+  fi
 
-    # hack in option to disable MPICH-C++-bindings...
-    MPI_NOCXXFLAGS="-DMPICH_SKIP_MPICXX"
+  # hack in option to disable MPICH-C++-bindings...
+  MPI_NOCXXFLAGS="-DMPICH_SKIP_MPICXX"
 }
 
 mpi_getmpich2flags() {
+  if test x"$DUNEMPICPPFLAGS" = x; then
     # use special commands to extract options      
     mpi_getflags "-show" "-c"
-    MPI_CPPFLAGS="$retval"
+    DUNEMPICPPFLAGS="$retval"
     # remove implicitly set -c
-    mpi_remove "$MPI_CPPFLAGS" '-c'
-    MPI_CPPFLAGS="$retval"
+    mpi_remove "$DUNEMPICPPFLAGS" '-c'
+    DUNEMPICPPFLAGS="$retval"
+  fi
     
+  if x"$DUNEMPILIBS" = x; then
     # get linker options
     mpi_getflags "-show" "-o"
-    MPI_LDFLAGS="$retval"
+    DUNEMPILIBS="$retval"
     # strip -o option
-    mpi_remove "$MPI_LDFLAGS" "-o"
-    MPI_LDFLAGS="$retval"
-    #strip MPI_CPPFLAGS (which are included for mpich2 on jugene)
-    enc=`echo "$MPI_CPPFLAGS" | sed -e 's/\\//\\\\\\//g'`
-    MPI_LDFLAGS=`echo "$retval" | sed -e "s/$enc / /"`
-
+    mpi_remove "$DUNEMPILIBS" "-o"
+    DUNEMPILIBS="$retval"
+    #strip DUNEMPICPPFLAGS (which are included for mpich2 on jugene)
+    enc=`echo "$DUNEMPICPPFLAGS" | sed -e 's/\\//\\\\\\//g'`
+    DUNEMPILIBS=`echo "$retval" | sed -e "s/$enc / /"`
+  fi
 
-    # hack in option to disable MPICH-C++-bindings...
-    MPI_NOCXXFLAGS="-DMPICH_SKIP_MPICXX"
+  # hack in option to disable MPICH-C++-bindings...
+  MPI_NOCXXFLAGS="-DMPICH_SKIP_MPICXX"
 }
 
 test_mpich () {
@@ -178,10 +192,14 @@ _EOF
   if (mpi_preprocess conftest.c | grep -q ompi_communicator_t); then
     dune_MPI_VERSION="OpenMPI"
 
-    mpi_getflags "-showme:compile"
-    MPI_CPPFLAGS="$retval"
-    mpi_getflags "-showme:link"
-    MPI_LDFLAGS="$retval"
+    if test x"$DUNEMPICPPFLAGS" = x; then
+      mpi_getflags "-showme:compile"
+      DUNEMPICPPFLAGS="$retval"
+    fi
+    if x"$DUNEMPILIBS" = x; then
+      mpi_getflags "-showme:link"
+      DUNEMPILIBS="$retval"
+    fi
     MPI_NOCXXFLAGS="-DMPIPP_H"
 
     AC_MSG_RESULT([yes])
@@ -247,30 +265,14 @@ test_ibmmpi() {
     if (echo $retval | grep '^xl[[cC]]'); then
       dune_MPI_VERSION="IBM MPI"
 
-      # get compilation script
-#      AC_LANG_CASE([C],[
-#        MPICOMP="$MPICC"
-#        dune_mpi_isgnu="$GCC"
-#      ],
-#      [C++],[
-#        MPICOMP="$MPICXX"
-#        dune_mpi_isgnu="$GXX"
-#      ])
-      # mpCC assumes xlc is used...
-#      if test x$dune_mpi_isgnu = xyes ; then
-#        # change commandline if GNU compiler is used
-#        retval=`echo $retval | sed -e 's/\(-b[[^ ]]*\)/-Xlinker \1/g'`
-#      fi
-      MPI_CPPFLAGS="$retval"
+      if test x"$DUNEMPICPPFLAGS" = x; then
+        DUNEMPICPPFLAGS="$retval"
+      fi
   
-      mpi_getflags "-v" "dummy.o -o dummy"
-
-#      if test x$dune_mpi_isgnu = xyes ; then
-#        # change commandline if GNU compiler is used
-#        retval=`echo $retval | sed -e 's/\(-b[[^ ]]*\)/-Xlinker \1/g'`
-#      fi
-
-      MPI_LDFLAGS="$retval"
+      if x"$DUNEMPILIBS" = x; then
+        mpi_getflags "-v" "dummy.o -o dummy"
+        DUNEMPILIBS="$retval"
+      fi
 
       AC_MSG_RESULT([yes])
       rm -f conftest*
-- 
GitLab