From f9367359480d215ba59defbfb9676606c43fb0c9 Mon Sep 17 00:00:00 2001
From: "Kurt A. O'Hearn" <ohearnk@msu.edu>
Date: Tue, 6 Nov 2018 12:18:55 -0500
Subject: [PATCH] Build systems updates for sPuReMD, PuReMD-GPU, and PG-PuReMD
 (use safer optimization level defaults).

---
 PG-PuReMD/Makefile.am   |   3 +-
 PG-PuReMD/configure.ac  |  56 ++++++++++++---------
 PuReMD-GPU/Makefile.am  |  35 +++++--------
 PuReMD-GPU/configure.ac | 106 +++++++++++++---------------------------
 configure.ac            |  13 ++---
 sPuReMD/Makefile.am     |   7 +++
 sPuReMD/configure.ac    |  55 ++++++++++++---------
 7 files changed, 121 insertions(+), 154 deletions(-)

diff --git a/PG-PuReMD/Makefile.am b/PG-PuReMD/Makefile.am
index 49ba36ff..2c878ef1 100644
--- a/PG-PuReMD/Makefile.am
+++ b/PG-PuReMD/Makefile.am
@@ -10,8 +10,7 @@ if USE_CUDA
 #   Note: cc 13 for Tesla, cc 20 for Fermi, cc 30 for Kepler K10, cc 35 for Kepler K20/K40, etc.
 #NVCCFLAGS += -use_fast_math -gencode arch=compute_35,code=sm_35
 NVCCFLAGS += -use_fast_math $(NVCC_OPT_CODE)
-#NVCCFLAGS += --compiler-options "$(DEFS) -D__SM_35__ -O3 -funroll-loops $(MPI_CFLAGS)"
-NVCCFLAGS += --compiler-options "$(DEFS) $(NVCC_OPT_CODE_DEFS) -O3 -funroll-loops $(MPI_CFLAGS)"
+NVCCFLAGS += --compiler-options "$(DEFS) $(NVCC_OPT_CODE_DEFS) -O2 -funroll-loops $(MPI_CFLAGS)"
 #NVCCFLAGS += --ptxas-options -v
 endif
 
diff --git a/PG-PuReMD/configure.ac b/PG-PuReMD/configure.ac
index 7694620e..9545d8e6 100644
--- a/PG-PuReMD/configure.ac
+++ b/PG-PuReMD/configure.ac
@@ -5,7 +5,7 @@ AC_PREREQ([2.69])
 
 AC_INIT([PG-PuReMD], [1.0], [ohearnku@msu.edu hma@msu.edu])
 # Do not allow AC_PROG_CC to set CFLAGS (this line must be after AC_INIT but before AC_PROG_CC)
-sav_CFLAGS="$CFLAGS"
+save_CFLAGS="$CFLAGS"
 : ${CFLAGS=""}
 AM_INIT_AUTOMAKE([1.15 subdir-objects -Wall -Werror foreign])
 # Enable silent build rules by default.
@@ -24,13 +24,13 @@ AC_DEFUN([CONFIGURE_HEADLINE],
 
 AC_LANG([C])
 
+AC_CONFIG_SRCDIR([src/torsion_angles.h])
+AC_CONFIG_HEADERS([src/config.h])
+
 # Checks for programs.
 AC_PROG_CC([icc gcc clang cc])
 AC_PROG_CPP
-CFLAGS="$sav_CFLAGS"
-
-AC_CONFIG_SRCDIR([src/torsion_angles.h])
-AC_CONFIG_HEADERS([src/config.h])
+CFLAGS="${save_CFLAGS}"
 
 # Checks for libraries.
 AC_SEARCH_LIBS([exp], [m])
@@ -53,18 +53,26 @@ AC_FUNC_REALLOC
 AC_FUNC_STRTOD
 AC_CHECK_FUNCS([gettimeofday memset])
 
-# Check for compiler vendor
+# Check for compiler vendor. If the compiler is recognized,
+#   the variable ax_cv_c_compiler_vendor is set accordingly.
 AX_COMPILER_VENDOR
 if test "x${ax_cv_c_compiler_vendor}" = "xgnu"; then
-	if test "x${BUILD_DEBUG}" = "x"; then
-		CFLAGS="${CFLAGS} -Wall -O3 -funroll-loops"
+	if test "x${BUILD_DEBUG}" = "xyes"; then
+		CFLAGS="-Wall -O0 -g2 -D_GLIBCXX_DEBUG"
 	else
-		CFLAGS="${CFLAGS} -Wall -O0 -g"
+		CFLAGS="-Wall -O2 -funroll-loops"
 	fi
-fi
-if test "x${ax_cv_c_compiler_vendor}" = "xintel"; then
-	if test "x${BUILD_DEBUG}" = "x"; then
-		CFLAGS="$CFLAGS -fast"
+	if test "x${BUILD_GPROF}" = "xyes"; then
+		CFLAGS="${CFLAGS} -pg"
+	fi
+elif test "x${ax_cv_c_compiler_vendor}" = "xintel"; then
+	if test "x${BUILD_DEBUG}" = "xyes"; then
+		CFLAGS="-Wall -O0"
+	else
+		CFLAGS="-Wall -O2 -funroll-loops"
+	fi
+	if test "x${BUILD_GPROF}" = "xyes"; then
+		CFLAGS="${CFLAGS} -p"
 	fi
 fi
 
@@ -75,8 +83,8 @@ AC_CHECK_PROG(MPIRUN, mpirun, mpirun)
 AC_SUBST(MPIRUN)
 
 # try to find if we are using OpenMPI / MPICH by looking inside mpi.h
-sav_CC="${CC}"
-sav_CFLAGS="${CFLAGS}"
+save_CC="${CC}"
+save_CFLAGS="${CFLAGS}"
 CC="${MPICC}"
 CFLAGS="${CFLAGS}"
 AC_CHECK_DECL([OPEN_MPI], [mpi_vendor="OpenMPI"],
@@ -87,8 +95,8 @@ AC_CHECK_DECL([MPICH2], [mpi_vendor="MPICH2"],
 # MPICH v3
 AC_CHECK_DECL([MPICH_VERSION], [mpi_vendor="MPICH3"],
 	     [], [#include "mpi.h"])
-CC="${sav_CC}"
-CFLAGS="${sav_CFLAGS}"
+CC="${save_CC}"
+CFLAGS="${save_CFLAGS}"
 
 # try to set MPI_CFLAGS and MPI_LIBS
 MPI_CFLAGS=
@@ -105,9 +113,9 @@ then
 	# build MPI_CFLAGS
 	tmp=`${MPICC} -compile-info | awk '{$1=""; print $0 }'`
 	MPI_CFLAGS=
-	for i in $tmp
+	for i in ${tmp}
 	do 
-		case $i in 
+		case ${i} in 
 			-[[DIUbi]]*)
 				MPI_CFLAGS="${MPI_CFLAGS} ${i}"
 				;;
@@ -115,9 +123,9 @@ then
 	done
 	# build MPI_LIBS
 	tmp=`${MPICC} -link-info | awk '{$1=""; print $0 }'`
-	for i in $tmp
+	for i in ${tmp}
 	do 
-		case $i in 
+		case ${i} in 
 			[[\\/]]*.a | ?:[[\\/]]*.a | -[[lLRu]]* | -Wl* )
 				MPI_LIBS="${MPI_LIBS} ${i}"
 				;;
@@ -176,7 +184,7 @@ if test "x${BUILD_GPU}" = "xyes"; then
 
         NVCCFLAGS=
 	NVCCFLAGS+=" -ccbin=${CXX}"
-	if test "x${BUILD_DEBUG}" = "xtrue"
+	if test "x${BUILD_DEBUG}" = "xyes"
 	then
 		NVCCFLAGS+=" -g -G"
 
@@ -185,9 +193,9 @@ if test "x${BUILD_GPU}" = "xyes"; then
 else
 	AM_CONDITIONAL(USE_CUDA, test "x" = "xyes")
 fi
-AM_CONDITIONAL(DEBUG, test "x${BUILD_DEBUG}" = "xtrue")
+AM_CONDITIONAL(DEBUG, test "x${BUILD_DEBUG}" = "xyes")
 
-if test "BUILD_PROF" = "true"
+if test "x${BUILD_GPROF}" = "xyes"
 then
 	if test "x${BUILD_GPU}" = "xyes"; then
 		NVCCFLAGS+=" --compiler-options ${gprof_flags}"
diff --git a/PuReMD-GPU/Makefile.am b/PuReMD-GPU/Makefile.am
index f57bed63..90d70db8 100644
--- a/PuReMD-GPU/Makefile.am
+++ b/PuReMD-GPU/Makefile.am
@@ -5,19 +5,11 @@ SUFFIXES = .cu
 include ../cuda.am
 endif
 
-AM_CFLAGS = -Wall -O3 -funroll-loops -fstrict-aliasing -m64
-AM_CPPFLAGS =
-AM_LDFLAGS =
-
-# default CUDA nvcc flags
-#   Note: cc 13 for Tesla
-#   Note: cc 20 for Fermi
-#   Note: cc 30 for Kepler K10
-#   Note: cc 35 for Kepler K20
-NVCCFLAGS += -use_fast_math 
-NVCCFLAGS += -gencode arch=compute_35,code=sm_35
-NVCCFLAGS += --compiler-options "$(DEFS) -D__SM_35__ -O3 -funroll-loops -fstrict-aliasing"
-#NVCCFLAGS += -Xcompiler -fPIC -dc
+# flags for CUDA compilation via NVCC (see cuda.am)
+#   Note: cc 13 for Tesla, cc 20 for Fermi, cc 30 for Kepler K10, cc 35 for Kepler K20/K40, etc.
+#NVCCFLAGS += -use_fast_math -gencode arch=compute_35,code=sm_35
+NVCCFLAGS += -use_fast_math $(NVCC_OPT_CODE)
+NVCCFLAGS += --compiler-options "$(DEFS) $(NVCC_OPT_CODE_DEFS) -O2 -funroll-loops"
 #NVCCFLAGS += --ptxas-options -v
 
 bin_PROGRAMS = bin/spuremd
@@ -31,8 +23,8 @@ bin_spuremd_SOURCES = src/analyze.c src/print_utils.c \
 	src/reset_utils.c src/single_body_interactions.c \
 	src/system_props.c src/three_body_interactions.c \
 	src/traj.c src/two_body_interactions.c src/vector.c \
-	src/testmd.c
-include_HEADERS = src/mytypes.h src/analyze.h src/print_utils.h \
+	src/testmd.c \
+	src/mytypes.h src/analyze.h src/print_utils.h \
         src/restart.h src/tool_box.c src/control.h src/ffield.c \
 	src/geo_tools.h src/box.h \
 	src/lin_alg.h src/qeq.h src/allocate.h src/bond_orders.h \
@@ -55,8 +47,8 @@ bin_spuremd_SOURCES += src/cuda_utils.cu src/cuda_copy.cu \
 	src/cuda_reset_utils.cu src/cuda_single_body_interactions.cu \
         src/cuda_system_props.cu src/cuda_three_body_interactions.cu \
 	src/cuda_two_body_interactions.cu src/cuda_environment.cu \
-	src/cuda_post_evolve.cu
-include_HEADERS += src/cuda_utils.h src/cuda_copy.h \
+	src/cuda_post_evolve.cu \
+	src/cuda_utils.h src/cuda_copy.h \
 	src/cuda_init.h src/cuda_reduction.h \
 	src/cuda_center_mass.h src/cuda_box.h src/cuda_validation.h \
         src/cuda_allocate.h src/cuda_bond_orders.h \
@@ -74,12 +66,7 @@ nodist_EXTRA_bin_spuremd_SOURCES = src/dummy.c
 
 endif
 
-
-bin_spuremd_CFLAGS = $(AM_CFLAGS) $(CFLAGS)
-bin_spuremd_CPPFLAGS = $(AM_CPPFLAGS) $(CPPFLAGS)
-bin_spuremd_LDFLAGS = $(AM_LDFLAGS) $(LDFLAGS)
-
 if USE_CUDA
-bin_spuremd_CFLAGS += $(CUDA_CFLAGS)
-bin_spuremd_LDFLAGS += $(CUDA_LIBS)
+bin_spuremd_CFLAGS = $(CUDA_CFLAGS)
+bin_spuremd_LDFLAGS = $(CUDA_LIBS)
 endif
diff --git a/PuReMD-GPU/configure.ac b/PuReMD-GPU/configure.ac
index 1fb73efd..07c96867 100644
--- a/PuReMD-GPU/configure.ac
+++ b/PuReMD-GPU/configure.ac
@@ -4,6 +4,8 @@
 AC_PREREQ([2.69])
 
 AC_INIT([PuReMD-GPU], [1.0], [ohearnku@msu.edu hma@msu.edu])
+# Do not allow AC_PROG_CC to set CFLAGS (this line must be after AC_INIT but before AC_PROG_CC)
+save_CFLAGS="${CFLAGS}"
 : ${CFLAGS=""}
 AM_INIT_AUTOMAKE([1.15 subdir-objects -Wall -Werror foreign])
 # Enable silent build rules by default.
@@ -13,20 +15,21 @@ m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])], [AC_SUBST([AM_DEFAULT_VERB
 
 AC_CONFIG_MACRO_DIR([../m4])
 
-AC_LANG([C])
-
-AC_CONFIG_SRCDIR([src/analyze.c])
-AC_CONFIG_HEADERS([src/config.h])
-
 # Headline formatter
 AC_DEFUN([CONFIGURE_HEADLINE],
 [
         echo; echo "+++ $1 +++"
 ])
 
+AC_LANG([C])
+
+AC_CONFIG_SRCDIR([src/analyze.c])
+AC_CONFIG_HEADERS([src/config.h])
+
 # Checks for programs.
 AC_PROG_CC([icc gcc cc])
 AC_PROG_CPP
+CFLAGS="${save_CFLAGS}"
 
 # Checks for libraries.
 AC_SEARCH_LIBS([exp], [m])
@@ -52,26 +55,35 @@ AC_CHECK_TYPES([gzFile], [],
 
 # Checks for library functions.
 AC_FUNC_MALLOC
+AC_FUNC_REALLOC
 AC_FUNC_STRTOD
 AC_CHECK_FUNCS([gettimeofday memset pow sqrt])
 
-# Check for compiler vendor
+# Check for compiler vendor. If the compiler is recognized,
+#   the variable ax_cv_c_compiler_vendor is set accordingly.
 AX_COMPILER_VENDOR
-if test "x$ax_cv_c_compiler_vendor" = "xgnu"; then
-	if test "x$BUILD_DEBUG" = "x"; then
-		CFLAGS="$CFLAGS -Wall -O3 -funroll-loops -fstrict-aliasing"
+if test "x${ax_cv_c_compiler_vendor}" = "xgnu"; then
+	if test "x${BUILD_DEBUG}" = "xyes"; then
+		CFLAGS="-Wall -O0 -g2 -D_GLIBCXX_DEBUG"
 	else
-		CFLAGS="$CFLAGS -Wall"
+		CFLAGS="-Wall -O2 -funroll-loops"
 	fi
-fi
-if test "x$ax_cv_c_compiler_vendor" = "xintel"; then
-	if test "x$BUILD_DEBUG" = "x"; then
-		CFLAGS="$CFLAGS -fast"
+	if test "x${BUILD_GPROF}" = "xyes"; then
+		CFLAGS="${CFLAGS} -pg"
+	fi
+elif test "x${ax_cv_c_compiler_vendor}" = "xintel"; then
+	if test "x${BUILD_DEBUG}" = "xyes"; then
+		CFLAGS="-Wall -O0"
+	else
+		CFLAGS="-Wall -O2 -funroll-loops"
+	fi
+	if test "x${BUILD_GPROF}" = "xyes"; then
+		CFLAGS="${CFLAGS} -p"
 	fi
 fi
 
 # Check for OpenMP support.
-if test "x$BUILD_OPENMP" = "xyes"; then
+if test "x${BUILD_OPENMP}" = "xyes"; then
 	AC_OPENMP
 	if test "x${OPENMP_CFLAGS}" = "x"; then
 		AC_MSG_WARN([
@@ -81,62 +93,22 @@ if test "x$BUILD_OPENMP" = "xyes"; then
 	  -----------------------------------------------])
 	else
 		# bug due to recent Intel compiler change (?)
-		if test "x$ax_cv_c_compiler_vendor" = "xintel"; then
+		if test "x${ax_cv_c_compiler_vendor}" = "xintel"; then
 			OPENMP_CFLAGS="-qopenmp"
 		fi
-		AC_SUBST(CFLAGS, "$OPENMP_CFLAGS")
-		AC_SUBST(CPPFLAGS, "$OPENMP_CFLAGS")
+		AC_SUBST(CFLAGS, "${OPENMP_CFLAGS}")
+		AC_SUBST(CPPFLAGS, "${OPENMP_CFLAGS}")
+		LIBS="${LIBS} -lgomp"
 	fi
 fi
 
-if test "x$BUILD_SUPERLU_MT" != "x"
-then
-	CPPFLAGS="${CPPFLAGS} -I${BUILD_SUPERLU_MT}/include"
-	LDFLAGS="${LDFLAGS} -L${BUILD_SUPERLU_MT}/lib"
-	#TODO: implement better BLAS detection
-	LIBS="${LIBS} -lblas"
-#	BLAS_FOUND_LIBS="yes"
-#	AC_SEARCH_LIBS([dtrsv_], [blas blas_OPENMP],
-#		        [], [BLAS_FOUND_LIBS="no"], [])
-#	AS_IF([test "x${BLAS_FOUND_LIBS}" != "xyes"],
-#	      [AC_MSG_ERROR([Unable to find BLAS library.])])
-	AC_CHECK_HEADERS([slu_mt_ddefs.h], [SUPERLU_MT_FOUND_HEADERS="yes"])
-	AS_IF([test "x${SUPERLU_MT_FOUND_HEADERS}" != "xyes"],
-	      [AC_MSG_ERROR([Unable to find SuperLU MT headers.])])
-	SUPERLU_MT_FOUND_LIBS="yes"
-	#TODO: fix issue where multiple -l flags added, one for each call below
-	AC_SEARCH_LIBS([intMalloc], [superlu_mt superlu_mt_OPENMP],
-		        [], [SUPERLU_MT_FOUND_LIBS="no"], [-lgomp])
-	AC_SEARCH_LIBS([get_perm_c], [superlu_mt superlu_mt_OPENMP],
-		        [], [SUPERLU_MT_FOUND_LIBS="no"], [-lgomp])
-	AC_SEARCH_LIBS([pdgstrf_init], [superlu_mt superlu_mt_OPENMP],
-		        [], [SUPERLU_MT_FOUND_LIBS="no"], [-lgomp -lblas -lblas_OPENMP])
-	AC_SEARCH_LIBS([pdgstrf], [superlu_mt superlu_mt_OPENMP],
-		        [], [SUPERLU_MT_FOUND_LIBS="no"], [-lgomp -lblas -lblas_OPENMP])
-	AC_SEARCH_LIBS([pxgstrf_finalize], [superlu_mt superlu_mt_OPENMP],
-		        [], [SUPERLU_MT_FOUND_LIBS="no"], [-lgomp -lblas -lblas_OPENMP])
-	AC_SEARCH_LIBS([StatAlloc], [superlu_mt superlu_mt_OPENMP],
-		        [], [SUPERLU_MT_FOUND_LIBS="no"], [-lgomp])
-	AC_SEARCH_LIBS([StatInit], [superlu_mt superlu_mt_OPENMP],
-		        [], [SUPERLU_MT_FOUND_LIBS="no"], [-lgomp])
-	AC_SEARCH_LIBS([StatFree], [superlu_mt superlu_mt_OPENMP],
-		        [], [SUPERLU_MT_FOUND_LIBS="no"], [-lgomp])
-	AC_SEARCH_LIBS([Destroy_SuperNode_SCP], [superlu_mt superlu_mt_OPENMP],
-		        [], [SUPERLU_MT_FOUND_LIBS="no"], [-lgomp])
-	AC_SEARCH_LIBS([Destroy_CompCol_NCP], [superlu_mt superlu_mt_OPENMP],
-		        [], [SUPERLU_MT_FOUND_LIBS="no"], [-lgomp])
-	AS_IF([test "x${SUPERLU_MT_FOUND_LIBS}" != "xyes"],
-	      [AC_MSG_ERROR([Unable to find SuperLU MT library.])])
-	AC_DEFINE([HAVE_SUPERLU_MT], [1], [Define to 1 if you have SuperLU_MT support enabled.])
-fi
-
 # Check for CUDA support.
-if test "x$BUILD_GPU" = "xyes"
+if test "x${BUILD_GPU}" = "xyes"
 then
 	CONFIGURE_HEADLINE([ CUDA support ])
 	AX_CUDA
 	NVCCFLAGS=
-	if test "BUILD_DEBUG" = "true"
+	if test "x${BUILD_DEBUG}" = "xyes"
 	then
 		NVCCFLAGS+=" -g -G"
 	fi
@@ -175,17 +147,7 @@ else
 	AM_CONDITIONAL(USE_CUDA, test "x" = "xyes")
 fi
 
-if test "x$BUILD_DEBUG" = "xyes"
-then
-	CFLAGS="${CFLAGS} ${DEBUG_FLAGS}"
-fi
-
-if test "x$BUILD_GPROF" = "xyes"
-then
-	CFLAGS="${CFLAGS} ${GPROF_FLAGS}"
-fi
-
-if test "x$BUILD_PROF" = "xtrue"
+if test "x${BUILD_GPROF}" = "xtrue"
 then
 	NVCCFLAGS+=" --compiler-options ${gprof_flags}"
 fi
diff --git a/configure.ac b/configure.ac
index e013d4fc..8d0243d4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -92,20 +92,13 @@ AC_ARG_ENABLE([gprof],
 	[AS_HELP_STRING([--enable-gprof],
 		[enable support for profiling with gprof @<:@default: no@:>@])],
 	[case "${enableval}" in
-		gnu | yes) 
-			# GNU
-			export BUILD_GPROF="true"
-			export GPROF_FLAGS="-pg"
-			;;
-		intel)
-			# Intel
-			export BUILD_GPROF="true"
-			export GPROF_FLAGS="-p"
+		yes) 
+			export BUILD_GPROF="yes"
 			;;
 		no)
 			;;
 		*)
-			AC_MSG_ERROR([bad value ${enableval} for --enable-gprof (only yes, gnu or intel are possible)]) ;;
+			AC_MSG_ERROR([bad value ${enableval} for --enable-gprof (only yes or no are possible)]) ;;
 	esac],
 	[]	      
 )
diff --git a/sPuReMD/Makefile.am b/sPuReMD/Makefile.am
index 92d61d1f..cabbb008 100644
--- a/sPuReMD/Makefile.am
+++ b/sPuReMD/Makefile.am
@@ -8,6 +8,13 @@ lib_libspuremd_la_SOURCES = src/ffield.c src/grid.c src/list.c src/lookup.c src/
 		  src/single_body_interactions.c src/two_body_interactions.c \
 		  src/three_body_interactions.c src/four_body_interactions.c src/forces.c \
 		  src/integrate.c src/init_md.c src/spuremd.c
+		  src/ffield.h src/grid.h src/list.h src/lookup.h src/print_utils.h \
+		  src/reset_utils.h src/restart.h src/random.h src/tool_box.h src/traj.h \
+		  src/vector.h src/allocate.h src/analyze.h src/box.h src/system_props.h src/control.h \
+		  src/geo_tools.h src/neighbors.h src/lin_alg.h src/charges.h src/bond_orders.h \
+		  src/single_body_interactions.h src/two_body_interactions.h \
+		  src/three_body_interactions.h src/four_body_interactions.h src/forces.h \
+		  src/integrate.h src/init_md.h src/spuremd.h src/reax_types.h
 lib_libspuremd_la_CFLAGS = -I src
 lib_libspuremd_la_LDFLAGS = -version-info 1:0:0
 
diff --git a/sPuReMD/configure.ac b/sPuReMD/configure.ac
index 093a415a..2e0fe879 100644
--- a/sPuReMD/configure.ac
+++ b/sPuReMD/configure.ac
@@ -4,6 +4,8 @@
 AC_PREREQ([2.69])
 
 AC_INIT([sPuReMD], [1.0], [ohearnku@msu.edu hma@msu.edu])
+# Do not allow AC_PROG_CC to set CFLAGS (this line must be after AC_INIT but before AC_PROG_CC)
+save_CFLAGS="${CFLAGS}"
 : ${CFLAGS=""}
 AM_INIT_AUTOMAKE([1.15 subdir-objects -Wall -Werror foreign])
 # Enable silent build rules by default.
@@ -12,6 +14,14 @@ AM_PROG_AR
 LT_PREREQ([2.2])
 LT_INIT([dlopen])
 
+AC_CONFIG_MACRO_DIR([../m4])
+
+# Headline formatter
+AC_DEFUN([CONFIGURE_HEADLINE],
+[
+        echo; echo "+++ $1 +++"
+])
+
 AC_LANG([C])
 
 AC_CONFIG_SRCDIR([src/traj.h])
@@ -20,6 +30,7 @@ AC_CONFIG_HEADERS([src/config.h])
 # Checks for programs.
 AC_PROG_CC([icc gcc cc])
 AC_PROG_CPP
+CFLAGS="${save_CFLAGS}"
 
 # Checks for libraries.
 AC_SEARCH_LIBS([exp], [m])
@@ -38,25 +49,35 @@ AC_SEARCH_LIBS([gzclose], [z])
 
 # Checks for typedefs, structures, and compiler characteristics.
 AC_C_INLINE
-AC_CHECK_TYPES([gzFile], [], [AC_MSG_FAILURE([gzFile type not found in zlib.h], [1])], [#include<zlib.h>])
+AC_CHECK_TYPES([gzFile], [],
+	       [AC_MSG_FAILURE([gzFile type not found in zlib.h], [1])], [#include<zlib.h>])
 
 # Checks for library functions.
 AC_FUNC_MALLOC
+AC_FUNC_REALLOC
 AC_FUNC_STRTOD
 AC_CHECK_FUNCS([gettimeofday memset])
 
-# Check for compiler vendor
+# Check for compiler vendor. If the compiler is recognized,
+#   the variable ax_cv_c_compiler_vendor is set accordingly.
 AX_COMPILER_VENDOR
-if test "x$ax_cv_c_compiler_vendor" = "xgnu"; then
-	if test "x$BUILD_DEBUG" = "x"; then
-		CFLAGS="$CFLAGS -Wall -O3 -funroll-loops -fstrict-aliasing"
+if test "x${ax_cv_c_compiler_vendor}" = "xgnu"; then
+	if test "x${BUILD_DEBUG}" = "xyes"; then
+		CFLAGS="-Wall -O0 -g2 -D_GLIBCXX_DEBUG"
 	else
-		CFLAGS="$CFLAGS -Wall -O0 -g"
+		CFLAGS="-Wall -O2 -funroll-loops"
 	fi
-fi
-if test "x$ax_cv_c_compiler_vendor" = "xintel"; then
-	if test "x$BUILD_DEBUG" = "x"; then
-		CFLAGS="$CFLAGS -fast"
+	if test "x${BUILD_GPROF}" = "xyes"; then
+		CFLAGS="${CFLAGS} -pg"
+	fi
+elif test "x${ax_cv_c_compiler_vendor}" = "xintel"; then
+	if test "x${BUILD_DEBUG}" = "xyes"; then
+		CFLAGS="-Wall -O0"
+	else
+		CFLAGS="-Wall -O2 -funroll-loops"
+	fi
+	if test "x${BUILD_GPROF}" = "xyes"; then
+		CFLAGS="${CFLAGS} -p"
 	fi
 fi
 
@@ -74,22 +95,12 @@ if test "x${BUILD_OPENMP}" = "xyes"; then
 		if test "x${ax_cv_c_compiler_vendor}" = "xintel"; then
 			OPENMP_CFLAGS="-qopenmp"
 		fi
-		AC_SUBST(AM_CFLAGS, "$OPENMP_CFLAGS")
-		AC_SUBST(AM_CPPFLAGS, "$OPENMP_CFLAGS")
+		AC_SUBST(AM_CFLAGS, "${OPENMP_CFLAGS}")
+		AC_SUBST(AM_CPPFLAGS, "${OPENMP_CFLAGS}")
 		LIBS="${LIBS} -lgomp"
 	fi
 fi
 
-if test "x$BUILD_DEBUG" != "x"
-then
-	CFLAGS="${CFLAGS} ${DEBUG_FLAGS}"
-fi
-
-if test "x$BUILD_GPROF" != "x"
-then
-	CFLAGS="${CFLAGS} ${GPROF_FLAGS}"
-fi
-
 # Check for LAPACKE
 AC_CHECK_HEADERS([mkl.h], [MKL_FOUND_HEADERS="yes"])
 if test "x${MKL_FOUND_HEADERS}" = "xyes"
-- 
GitLab