CMake (Windows): automatically download, build and link with libzippp when running `cmake -B`

82 Views Asked by At

I'm trying to integrate libzippp (a c++ wrapper around libzip) into my project over at https://github.com/gwdevhub/gMod/tree/dev. libzippp requires libzip to be installed and available, libzip in turn requires zlib.

I would not like to "install" zlib or libzip previously to running cmake. If the install step happens while running cmake, that's okay, but I would strongly prefer a build completely without installing to a system directory - installing into build directories is fine.

The build result of my app should have no dynamic dependencies, aka build entirely static.

I can download zlib with

zlib.cmake:

include_guard()
include(FetchContent)

# Set the version of zlib you want to use
set(ZLIB_VERSION "1.3")

# Fetch zlib using FetchContent
FetchContent_Declare(
  zlib
  GIT_REPOSITORY https://github.com/madler/zlib
  GIT_TAG v1.3
)

FetchContent_GetProperties(zlib)
if(NOT zlib_POPULATED)
  FetchContent_Populate(zlib)
  add_subdirectory(${zlib_SOURCE_DIR} ${zlib_BINARY_DIR})
endif()

I can equally download libzip with

libzip.cmake

include_guard()
include(FetchContent)
include(zlib)

FetchContent_Declare(
    libzip
    GIT_REPOSITORY https://github.com/nih-at/libzip
    GIT_TAG v1.10.1)
FetchContent_GetProperties(libzip)
if (libzip_POPULATED)
    return()
endif()

FetchContent_Populate(libzip)

add_library(libzip)
file(GLOB SOURCES
    "${libzip_SOURCE_DIR}/src/*.h"
    "${libzip_SOURCE_DIR}/src/*.c"
    )
source_group(TREE ${libzip_SOURCE_DIR} FILES ${SOURCES})
target_sources(libzip PRIVATE ${SOURCES})
target_include_directories(libzip PRIVATE "${libzip_SOURCE_DIR}")
target_compile_definitions(libzip PRIVATE
    "-DLIBZIP_DO_INSTALL=OFF"
    "-DZLIB_LIBRARY=${libzip_BUILD_DIR}/"
    )


set_target_properties(libzip PROPERTIES FOLDER "Dependencies/")

Trying to build it from VS results in various errors about files being missing, as they only exist in the form of .in files. I tried navigating to the libzip folder and manually following the build/install steps, but cmake fails with

-- Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE)
-- Could NOT find Nettle (missing: Nettle_LIBRARY Nettle_INCLUDE_DIR) (Required is at least version "3.0")
-- Could NOT find GnuTLS (missing: GNUTLS_LIBRARY GNUTLS_INCLUDE_DIR)
-- Could NOT find MbedTLS (missing: MbedTLS_LIBRARY MbedTLS_INCLUDE_DIR) (Required is at least version "1.0")
-- Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY OPENSSL_INCLUDE_DIR)
CMake Error at C:/Program Files/CMake/share/cmake-3.27/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find ZLIB (missing: ZLIB_INCLUDE_DIR) (Required is at least
  version "1.1.2")

ZLIB does correctly build, but there's only a zlibd.dll in my bin/Debug directory.

I would not like to install zlib, libzip or anything else for that matter - the build should preferably happen entirely within the VS solution, but it's fine if I need to run cmake --build before cmake -B.

I attempted to use cmakes ExternalProject_Add, but faced errors because the libzip source only has a libzip-config.cmake**.in** file.

Can anyone explain how to set this up and how to integrate other libraries in the future? It's simple enough with things like miniz that have no further dependencies, but depending on projects that have dependencies, even if they build with cmake, appears unnecessarily complex and isn't described anywhere.

0

There are 0 best solutions below