Repository: easybuilders/easybuild-easyblocks

Issue:
existence of static BLAS and LAPACK libraries is assumed in GROMACS easyblock, which is not the case for FlexiBLAS
While working on GROMACS 2021.2 for foss 2021a, I noticed that GROMACS EB assumes the existence of a static FlexiBLAS library for both, BLAS and LAPACK which is not the case. Thus it blows up with:
```
-- Checking that user BLAS library /home/apps/USE/easybuild/staging/2021.1/software/FlexiBLAS/3.0.4-GCC-10.3.0/lib/libflexiblas.a works
-- Looking for dgemm_
-- Looking for dgemm_ - not found
CMake Warning at cmake/gmxManageLinearAlgebraLibraries.cmake:78 (message):
  GMX_BLAS_USER library
  /home/apps/USE/easybuild/staging/2021.1/software/FlexiBLAS/3.0.4-GCC-10.3.0/lib/libflexiblas.a
  was specified, but it may not provide BLAS.  We are proceeding by assuming
  you know what you are doing and that linking F77-style to this library will
  work.
```

The underlying reason comes from GROMACS EB as it creates -DGMX_BLAS_USER and -DGMX_LAPACK_USER which contain:
```
# -DGMX_BLAS_USER & -DGMX_LAPACK_USER require full path to library
libs = os.getenv('%s_STATIC_LIBS' % libname).split(',')
libpaths = [os.path.join(libdir, lib) for lib in libs if lib != 'libgfortran.a']
self.cfg.update('configopts', '-DGMX_%s_USER="%s"' % (libname, ';'.join(libpaths)))
```

Open questions:
1. Does GROMACS really require static BLAS libraries or not? This needs to be clarified first by checking the docs, asking the devs etc.; or
2. Is it possible to build static FlexiBLAS?
3. Where it would be a good place to check for the existence of BLAS.

Note:
There is LIBBLAS but it contains linking flags and not only the actual name of the BLAS library.


Discussion:
I've implemented support for also defining `$LAPACK_SHARED_LIBS` in the build environment in https://github.com/easybuilders/easybuild-framework/pull/3800, we should probably update the GROMACS easyblock to use that rather than `$LAPACK_STATIC_LIBS`, at least when FlexiBLAS is involved?

I would rather not switch to always linking to the shared libraries for GROMACS, only when needed (i.e. when FlexiBLAS is involved)...

Resolving patch:
diff --git a/easybuild/easyblocks/g/gromacs.py b/easybuild/easyblocks/g/gromacs.py
--- a/easybuild/easyblocks/g/gromacs.py
+++ b/easybuild/easyblocks/g/gromacs.py
@@ -356,8 +356,13 @@ def configure_step(self):
                             raise EasyBuildError("Failed to find libsci library to link with for %s", libname)
                     else:
                         # -DGMX_BLAS_USER & -DGMX_LAPACK_USER require full path to library
-                        libs = os.getenv('%s_STATIC_LIBS' % libname).split(',')
-                        libpaths = [os.path.join(libdir, lib) for lib in libs if lib != 'libgfortran.a']
+                        # prefer shared libraries when using FlexiBLAS-based toolchain
+                        if self.toolchain.blas_family() == toolchain.FLEXIBLAS:
+                            libs = os.getenv('%s_SHARED_LIBS' % libname).split(',')
+                        else:
+                            libs = os.getenv('%s_STATIC_LIBS' % libname).split(',')
+
+                        libpaths = [os.path.join(libdir, lib) for lib in libs if not lib.startswith('libgfortran')]
                         self.cfg.update('configopts', '-DGMX_%s_USER="%s"' % (libname, ';'.join(libpaths)))
                         # if libgfortran.a is listed, make sure it gets linked in too to avoiding linking issues
                         if 'libgfortran.a' in libs:


Test patch: