# Conditionally linking LAPACK with fpm

**URL:** <https://fortran-lang.discourse.group/t/conditionally-linking-lapack-with-fpm/4425>\
**Category:** fpm\
**Created:** [September 28, 2022, 4:19pm UTC](https://fortran-lang.discourse.group/t/conditionally-linking-lapack-with-fpm/4425 "2022-09-28T16:19:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicholaswogan](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@nicholaswogan](https://fortran-lang.discourse.group/u/nicholaswogan)\
**Post date:** [September 28, 2022, 4:19pm UTC](https://fortran-lang.discourse.group/t/conditionally-linking-lapack-with-fpm/4425/1 "2022-09-28T16:19:42Z")

</div>

@awvwgk or anyone else that knows `fpm` pretty well. Is there any way to conditionally link LAPACK?

I’ve added the following CMake to this project to link to lapack if it is found, or to compile the necessary parts of lapack if it is not found (see below). Can this be done with fpm?

```nohighlight
find_package(LAPACK)
if (${LAPACK_FOUND})
  target_link_libraries(odepack ${LAPACK_LIBRARIES})
else()
  # We compile lapack files downloaded from netlib
  file(GLOB LAPACK_SRC_FILES lapack/*.f)
  add_library(lapack ${LAPACK_SRC_FILES})
  target_link_libraries(odepack lapack)
endif()

```

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [September 28, 2022, 7:22pm UTC](https://fortran-lang.discourse.group/t/conditionally-linking-lapack-with-fpm/4425/2 "2022-09-28T19:22:04Z")

</div>

We unfortunately do not (yet) have conditional compilation features in fpm. We’ve recently started adding support for preprocessing, but that probably won’t be sufficient for your needs here. Sorry.

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [September 28, 2022, 8:37pm UTC](https://fortran-lang.discourse.group/t/conditionally-linking-lapack-with-fpm/4425/3 "2022-09-28T20:37:33Z")

</div>

Linking LAPACK is one of the largest challenges for fpm. I hope that we can arrive at a solution that allows to sanely depend on LAPACK via a standard dependency. What is needed for this are (among other things):

- feature / optional support in fpm
  - support several different providers (MKL, OpenBLAS, Accelerate.framework, …)
  - support compiling netlib LAPACK from source as fallback (very last resort)

- automatically discover available providers
  - pkg-config is somewhat robust for some providers
  - needs custom logic (maybe bi-directional build script support)

- fpm option to select LAPACK (via feature of transient dependency)

* * *

The alternative is that fpm knows how to link LAPACK and you can just use

```auto
[dependencies]
lapack = "*"

```

The whole detection logic from above must be hard-coded into fpm (similar as the shipped FindLAPACK.cmake file in the CMake distribution). The disadvantage of this is of course that the LAPACK support in fpm will evolve with time, having a versioned meta package which implements the LAPACK support would be preferable (for example in [https://github.com/fortran-lang/lapack-fpm](https://github.com/fortran-lang/lapack-fpm)), also since there are more packages like LAPACK which are difficult to link correctly (ScaLAPACK, HDF5, NetCDF4, …).

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [September 28, 2022, 9:30pm UTC](https://fortran-lang.discourse.group/t/conditionally-linking-lapack-with-fpm/4425/4 "2022-09-28T21:30:18Z")

</div>

There are a few threads about LAPACK support in fpm

- [Support Lapack natively · Issue #368 · fortran-lang/fpm · GitHub](https://github.com/fortran-lang/fpm/issues/368)
- [Linking with Intel MKL · Discussion #330 · fortran-lang/fpm · GitHub](https://github.com/fortran-lang/fpm/discussions/330)

If you can volunteer to some time to brainstorm or even prototype this, that would be appreciated. It might be worth looking at how Spack tackles this. Similar to @awvwgk’s proposed meta-packages, Spack has the concept of [virtual packages](https://spack.readthedocs.io/en/latest/packaging_guide.html#virtual-dependencies) which implement an interface. They also have some special logic for [BLAS, LAPACK and ScaLAPACK libraries](https://spack.readthedocs.io/en/latest/packaging_guide.html#blas-lapack-and-scalapack-libraries).

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [September 28, 2022, 9:33pm UTC](https://fortran-lang.discourse.group/t/conditionally-linking-lapack-with-fpm/4425/5 "2022-09-28T21:33:26Z")

</div>

You could also try [libblastrampoline](https://github.com/JuliaLinearAlgebra/libblastrampoline) as the entry point to multiple libraries, configurable via environment variables. This is used in Julia.
