13 February 2009

Using ACX_BLAS before AX_PATH_GSL with gfortran 4.3.2

While autotooling a project that uses GSL, I wanted GSL to first attempt to detect and use a system BLAS.  If none can be found, then I wanted GSL to fall back to its own gslcblas implementation.  The rational being that a savvy user could eke out some performance, but that for everyone else the code just builds and runs without being too fussy.

First I tried this in configure.ac:
ACX_BLAS([
   export GSL_CBLAS_LIB="${BLAS_LIBS}"
   AC_SUBST(GSL_CBLAS_LIB)
],AC_MSG_WARN([Will use gslcblas if it is present.  Try --with-blas=<lib>.]))
AX_PATH_GSL(1.12,,AC_MSG_
ERROR([Could not find required GSL version.]))
This works beautifully with Intel's compilers, but it dies for gcc/gfortran 4.3.2 during configure:
checking for GSL - version >= 1.12... no
*** Could not run GSL test program, checking why...
*** The test program failed to compile or link.

with the linking problems in config.log looking like
configure:7272: gcc -o conftest -I/org/centers/pecos/LIBRARIES/GSL/gsl-1.12-gcc-4.3.1-ubuntu-amd64/include -I/h2/rhys/include -L/h2/rhys/lib conftest.c -L/org/centers/pecos/LIBRARIES/GSL/gsl-1.12-gcc-4.3.1-ubuntu-amd64/lib -lgsl -lcblas -lf77blas -latlas -lm >&5
/usr/lib/../lib64/libf77blas.
so: undefined reference to `_gfortran_st_write_done'
which a quick Google tells me is because -lgfortran isn't present in the linking line.  I tried to obtain the required gfortran libraries in ${FLIBS} by using AC_FC_LIBRARY_LDFLAGS and then use them via
export GSL_CBLAS_LIB="${BLAS_LIBS} ${FLIBS}"
but later in the build I started hitting up against duplicate main issues because -lgfortranbegin winds up in ${FLIBS}.

More random Googling found the answer buried in lapack++'s configure script.  The total solution looks like:
AC_PROG_SED
ACX_BLAS([
   dnl Workaround for bogus FLIBS
   FLIBS=`echo ${FLIBS} | ${SED} 's/-lgfortranbegin//'`
   export GSL_CBLAS_LIB="${BLAS_LIBS} ${FLIBS}"
   AC_SUBST(GSL_CBLAS_LIB)
],AC_MSG_WARN([Will use gslcblas if it is present.  Try --with-blas=<lib>.]))
AX_PATH_GSL(1.12,,AC_MSG_
ERROR([Could not find required GSL version.]))
where there's an explicit hack to remove -lgfortranbegin from FLIBS. Not pretty, but it seems to work.


No comments:

Subscribe Subscribe to The Return of Agent Zlerich