How to compile a self-contained static libhdf5.a with CMake?

I am building a C library that uses HDF5 as an internal dependency to read data from an HDF5 file with zlib compressed data. I need to ship my library as a self-contained static library on Linux and therefore I would like to compile libhdf5.a self-contained including zlib. Can someone tell me how to use CMake to achieve this?

Thanks!

I’ve created a minimal program that first builds libz.a and libhdf5.a and then tries to compile a very simple test program. Find the source code here

and a log of the compilation process here: log.txt

There is a second branch dynamic where I use shared objects and this one works as expected.

How do I have to modify the CMakeLists.txt?