From bfde602fed6b96bb31e1ec4e6053dcf9c602cb60 Mon Sep 17 00:00:00 2001
From: Giovanni La Mura <giovanni.lamura@inaf.it>
Date: Tue, 9 Jul 2024 14:49:52 +0200
Subject: [PATCH] Enable optional MPI compilation

---
 build/configure.ac | 93 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 91 insertions(+), 2 deletions(-)

diff --git a/build/configure.ac b/build/configure.ac
index 4fdbb734..92494cc5 100644
--- a/build/configure.ac
+++ b/build/configure.ac
@@ -33,8 +33,95 @@ AM_INIT_AUTOMAKE([-Wall -Werror foreign])
 
 # Compiler detection
 AM_PROG_AR
-AC_PROG_F77([mpif90 gfortran f77])
-AC_PROG_CXX([mpicxx g++ clang])
+AC_ARG_ENABLE(
+	[mpi],
+	[AS_HELP_STRING([--enable-mpi], [use MPI if available (default is yes)])],
+	[
+		if test "x$enableval" = "xno"; then
+		  if test "x$F77" = "xmpif90"; then
+		    gfortran --version > /dev/null
+		    if test "x$?" = "x0"; then
+		      export F77=gfortran
+		    fi
+		  fi
+		  if test "x$F77" = "xmpif90"; then
+		    f77 --version > /dev/null
+		    if test "x$?" = "x0"; then
+		      export F77=f77
+		    fi
+		  fi
+		  if test "x$CXX" = "xmpicxx"; then
+		    g++ --version > /dev/null
+		    if test "x$?" = "x0"; then
+		      export CXX=g++
+		    fi
+		  fi
+		  if test "x$CXX" = "xmpicxx"; then
+		    clang --version > /dev/null
+		    if test "x$?" = "x0"; then
+		      export CXX=clang
+		    fi
+		  fi
+		  if test "x$F77" = "x"; then
+		    gfortran --version > /dev/null
+		    if test "x$?" = "x0"; then
+		      export F77=gfortran
+		    fi
+		  fi
+		  if test "x$F77" = "x"; then
+		    f77 --version > /dev/null
+		    if test "x$?" = "x0"; then
+		      export F77=f77
+		    fi
+		  fi
+		  if test "x$CXX" = "x"; then
+		    g++ --version > /dev/null
+		    if test "x$?" = "x0"; then
+		      export CXX=g++
+		    fi
+		  fi
+		  if test "x$CXX" = "x"; then
+		    clang --version > /dev/null
+		    if test "x$?" = "x0"; then
+		      export CXX=clang
+		    fi
+		  fi
+		else
+		  mpicxx --version > /dev/null
+		  if test "x$?" = "x0"; then
+		    export CXX=mpicxx
+		    export F77=mpif90
+		  else
+		    AC_MSG_ERROR(["MPI was enabled, but no MPI compiler was found!"])
+		  fi
+		fi
+	],
+	[
+		if test "x$CXX" = "x"; then
+		  mpicxx --version > /dev/null
+		  if test "x$?" = "x0"; then
+		    export CXX=mpicxx
+		    export F77=mpif90
+		  fi
+		fi
+		if test "x$CXX" = "x"; then
+		  g++ --version > /dev/null
+		  if test "x$?" = "x0"; then
+		    export CXX=g++
+		  fi
+		fi
+		if test "x$CXX" = "x"; then
+		  clang --version > /dev/null
+		  if test "x$?" = "x0"; then
+		    export CXX=clang
+		  fi
+		fi
+	]
+)
+AC_SUBST([CXX])
+AC_PROG_CXX(CXX)
+AC_SUBST([F77])
+AC_PROG_F77([$F77 mpif90 gfortran f77])
 
 # Libtool initialization
 LT_INIT
@@ -70,6 +157,8 @@ AS_IF(
 )
 AC_SUBST([HDF5_LDFLAGS])
 
+# Configure the optional features
+
 # Send output to Makefiles
 AC_CONFIG_FILES([Makefile])
 
-- 
GitLab