Usage of non-system zlib

I’ve been trying to compile HDF5 as a part of a bigger project. Usually this gives me no issues, but recently I installed zlib-ng-compat-devel on Fedora and since then I can’t link to my own zlib. Instead cmake always wants to link with /usr/lib64/libz.a. This seems to arise from the .cmake that come with the mentioned package. The weird part is that I actually set ZLIB_LIBRARIES and ZLIB_INCLUDE_DIRS correctly.

After checking the CMakeCache.txt I discovered that ZLIB_DIR=/usr/lib64 is added even though it is never specified. I also found out (after a lot of trial and error) that, in HDF5’s CMakeLists.txt, the Paraview example uses H5_ZLIB_HEADER.

When I also set H5_ZLIB_HEADER to the header file located in my own project it works. I just don’t understand why this is required, even though the correct FindZLIB variables are set. Additionally, I wonder if I just found a dirty workaround or if this is actually the way to do that?