Please Help CPMAddPackage install zlib in build directory

Please help,

I have been trying to figure out a way to download and compile zlib in a build directory via CPMAddPackage with Cmake 3.25.1. The reason is that I don’t want the user to need to have zlib previously installed on the system.

For example, say I have a CPMAddPackage in an import_libraries.cmake defined as follows:

set(REPO_ZLIB "https://github.com/madler/zlib.git"
      CACHE STRING "ZLIB repo location.")
CPMAddPackage(
      NAME zlib
      GIT_REPOSITORY ${REPO_ZLIB}
      GIT_TAG "v1.2.13"
      OPTIONS
      "CMAKE_POSITION_INDEPENDENT_CODE True"
)

I would like CMake to use the above zlib rather than any locally defined zlib that may be on the current machine. Also the CMake should be able to work with windows or linux systems.

Can anyone point me in the direction as to how, or even if, this can be accomplished?

Thanks in advance for any help.

Okay. I have been doing some thinking about it and would like to go simpler if possible. Not using CPMAddPackage, just a plain CMakeLists.txt file(s).

How would I build zlib using source files defined within my particular project? Ignoring any currently existing zlib that may or may not be installed on the system, using one or more CMakeList.txt files?

For example in my project, say I have a directory called deps that exists whereby I have downloaded the zlib source previously. I would like to build the zlib source creating the corresponding libraries and then link theses deps based zlib libraries and includes for my project using CMake, e.g. within the definitions of CMakeLists.txt file(s).

long story short, I would like my project to fully contain its own zlib definitions that do not relate back to any system zlib definitions.

If this is possible, can anyone point me in the right direction?