HDF5 1.12.1 on MacOS with CMake and Macports zlib
Building HDF5 v1.12.1 using CMake with the Intel compilers on a Mac OS 11 (Big Sur) system with HDF5 v1.12.1 installed via macports errors as follows:
cd /opt/src/vbuilds-hdf5/1.12.1-intel21/fortran/src && /opt/intel/oneapi/compiler/2021.4.0/mac/bin/intel64/icc -I/opt/local/include -I/opt/src/vbuilds-hdf5/1.12.1-intel21/src -I/opt/src/hdf5/src -I/opt/src/vbuilds-hdf5/1.12.1-intel21/fortran -std=c99 -O3 -DNDEBUG -std=gnu99 -MD -MT fortran/src/CMakeFiles/H5match_types.dir/H5match_types.c.o -MF CMakeFiles/H5match_types.dir/H5match_types.c.o.d -o CMakeFiles/H5match_types.dir/H5match_types.c.o -c /opt/src/hdf5/fortran/src/H5match_types.c
/opt/src/hdf5/fortran/src/H5match_types.c(233): error: identifier "H5_PAC_FC_MAX_REAL_PRECISION" is undefined
H5_PAC_FC_MAX_REAL_PRECISION > 28) {
^
/opt/src/hdf5/fortran/src/H5match_types.c(426): error: identifier "H5_PAC_FC_MAX_REAL_PRECISION" is undefined
else if (sizeof(long double) == H5_FORTRAN_NATIVE_DOUBLE_SIZEOF && H5_PAC_FC_MAX_REAL_PRECISION > 28) {
^
compilation aborted for /opt/src/hdf5/fortran/src/H5match_types.c (code 2)
What's happening is that CMake is finding zlib (probably installed as a dependency for HDF5) in /opt/local and setting ZLIB_INCLUDE_DIR to /opt/local/include. Then CMake sets that as a global include directory with INCLUDE_DIRECTORIES in CMakeFilters.cmake.
When making build rules, CMake automatically places all INCLUDE_DIRECTORIES before all directories set with target_include_directories. This results in gmake finding /opt/local/include/H5public.h and opt/local/include/H5pubconf.h (which has H5_PAC_FC_MAX_REAL_PRECISION undefined) instead of /opt/src/hdf5/src/H5public.h and /opt/src/vbuilds-hdf5/1.12.1-intel21/src/H5pubconf.h (which has H5_PAC_FC_MAX_REAL_PRECISION defined). If H5_PAC_FC_MAX_REAL_PRECISION is defined in opt/local/include/H5pubconf.h (probably incorrectly) who knows what could happen.
As far as I know there isn't a way to tell CMake to put INCLUDE_DIRECTORIES after target_include_directories. By default each call to target_include_directories appends to the end of the list. I think the only solution might be to carefully add ZLIB_INCLUDE_DIR to the proper targets after all other target_include_directories have been added to that target, or add it first then make sure all subsequent target_include_directories use the BEFORE option. The caveat with the second option would be, if the current order of include paths is important, it could break other things since the order would be reversed.
The CMake option CMAKE_INCLUDE_DIRECTORIES_BEFORE can be used for prepending but is only effective on INCLUDE_DIRECTORIES, not target_include_directories.
A workaround for me was to comment out line 100 of CMakeFilters.cmake so gmake finds the system zlib (atificially at /usr/lib):
# INCLUDE_DIRECTORIES (${ZLIB_INCLUDE_DIRS})
Is this still a problem in 1.14? Can this be closed?
All global settings for includes have been removed in development source and I believe 1.14.x releases.