In neural-fortran I use a few different dependencies. Fpm makes this very easy to do via fpm.toml, e.g. see neural-fortran/fpm.toml at main · modern-fortran/neural-fortran · GitHub.
Now, I want to make the HDF5 dependency optional, because a small fraction of users needs it, and because it adds considerable set-up complexity to build the project.
I figured out how to do this with CMake, where I block out sections of code with CPP macros, and tell CMake to not pull the dependencies if that macro is not defined.
How would I best do this with fpm?
I don’t think it’s possible to mark dependencies as optional in fpm, and then use an fpm flag for example.
A possible workaround could be to have two separate fpm.toml files, e.g. fpm.toml and fpm-with-hdf5.toml. Both would need to enable preprocessing, and the latter would need to define the macro. In that case in fpm.toml (no HDF5) I would have:
# no [build] rules
# no [dependencies] rules
[preprocess]
[preprocess.cpp]
suffixes = ["f90"]
and in fpm-with-hdf5.toml I would have:
[build]
external-modules = "hdf5"
link = ["hdf5", "hdf5_fortran"]
[dependencies]
functional = { git = "https://github.com/wavebitscientific/functional-fortran" }
h5fortran = { git = "https://github.com/geospace-code/h5fortran" }
json-fortran = { git = "https://github.com/jacobwilliams/json-fortran" }
[preprocess]
[preprocess.cpp]
macros = ["USE_KERAS_HDF5"]
suffixes = ["f90"]
However, in the first case, I still get
$ fpm build
+ mkdir -p build/dependencies
<ERROR> *cmd_build* Target error: Unable to find source for module dependency: "functional" used by "././src/nf/nf_keras_submodule.f90"
STOP 1
I guess that this is because fpm doesn’t preprocess the source files before establishing the dependencies.
Any advice on how to work around this with the current fpm? This is with v0.10.1.
Thanks!