Parallel Fortran high-level build omits MPI_Fortran_MODULE_DIR and globally applies MPI_C_LINK_FLAGS
- Dominant language
- C
- Stars
- 988
- Forks
- 355
- Avg merge
- 4d 2h
- Merged PRs (30d)
- 12
Description
**Describe the bug**
A parallel HDF5 build with Fortran and the high-level Fortran library fails with an OpenMPI installation whose `mpi.mod` is located in CMake's `MPI_Fortran_MODULE_DIR` but not in `MPI_Fortran_INCLUDE_DIRS`.
The high-level Fortran targets add `MPI_Fortran_INCLUDE_DIRS` but omit `MPI_Fortran_MODULE_DIR`, so compilation fails because `mpi.mod` cannot be found.
The same configuration also copies `MPI_C_LINK_FLAGS` into global `CMAKE_EXE_LINKER_FLAGS`, despite already linking `MPI::MPI_C`. This applies C MPI-wrapper flags to Fortran linker invocations. In this environment, that propagates a raw `-pthread` flag to NAG Fortran, which does not accept it.
**Expected behavior**
A parallel HDF5 build with `HDF5_BUILD_FORTRAN=ON` and `HDF5_BUILD_HL_LIB=ON` should locate the MPI Fortran modules discovered by CMake and should not apply C-specific MPI wrapper linker flags globally to Fortran links.
**Platform (please complete the following information)**
- HDF5 version: `develop`, commit `2888f82e837` (`Add release page for 2.2.0`)
- OS and version: SLES 15 on NASA Discover
- C compiler: GCC 12.3.0, via OpenMPI `mpicc`
- Fortran compiler: NAG Fortran 7.2 build 7238, via OpenMPI `mpif90`
- Build system: CMake 4.3.0, Ninja 1.12.0
- MPI library: OpenMPI 4.1.6, built with GCC 12.3.0 and NAG Fortran 7.2.7238
- Configure options:
```bash
cmake -S . -B build -G Ninja \
-DCMAKE_C_COMPILER=mpicc \
-DCMAKE_Fortran_COMPILER=mpif90 \
-DHDF5_ENABLE_PARALLEL=ON \
-DHDF5_BUILD_FORTRAN=ON \
-DHDF5_BUILD_HL_LIB=ON \
-DHDF5_BUILD_CPP_LIB=OFF \
-DBUILD_SHARED_LIBS=ON \
-DBUILD_TESTING=OFF \
-DHDF5_BUILD_EXAMPLES=OFF \
-DHDF5_ENABLE_Z_LIB_SUPPORT=OFF
```
**Additional context**
CMake 4.3 currently has a separate issue with `FortranCInterface_VERIFY()` and NAG Fortran 7.2. A minimal reproducer has [been reported to Kitware](https://gitlab.kitware.com/cmake/cmake/-/work_items/28008). I applied a temporary local guard for that unrelated CMake failure in order to complete this HDF5 build and isolate the issues described above.
With the proposed HDF5 changes and that temporary CMake workaround, HDF5 `develop` builds successfully in this configuration.
I have a branch with a fix for this here:
https://github.com/HDFGroup/hdf5/compare/develop...mathomp4:hdf5:fix-mpi-fortran-module-dir
I didn't want to make a PR until you all thought it was a good idea.
**AI assistance disclosure**
GPT-5.6 Terra (Codex) assisted with diagnosing the build configuration and drafting the proposed changes. I independently built and verified the result.
Contributor guide
Assessment
This issue has not been assessed yet.