zeromq / cppzmq

Header-only C++ binding for libzmq
http://www.zeromq.org
MIT License
1.94k stars 757 forks source link

attempt automatic cmake libzmq dependency resolution #437

Open hsgreen opened 4 years ago

hsgreen commented 4 years ago

I'm trying to accommodate a situation where I am building an application with cmake on a custom docker image to minimize extra tools that are installed. I'm also trying to simplify my installation process by having as many dependencies as possible to be built with cmake as part of my main application build so my application can have everything statically linked and not install any development/header files from the dependent libraries. It also eliminates having to customize installation scripts for various architectures of docker images I might be targeting.

I was hoping to use cppzmq, however, right now the cmake requires ZeroMQ to be preinstalled on the system and does not accommodate attempting to just pull it in and build through cmake if it is not present. https://github.com/zeromq/cppzmq/blob/master/CMakeLists.txt#L10-L17 It would be nice to not have to have a process outside of cmake that goes and installs ZeroMQ when it can be done easily from within CMake using the relatively new FetchContent... methods.

I would like to propose something similar to the following snippet be included in the CMakeLists.txt:

find_package(ZeroMQ)
if(NOT ${ZeroMQ_FOUND})
    message(STATUS "Pulling ZeroMQ from git...")
    # If not found, then fetch remote
    FetchContent_Declare(
        ZeroMQ
        GIT_REPOSITORY https://github.com/zeromq/libzmq
        GIT_TAG v4.3.2
    )
    # Set config properties
    set(ZMQ_BUILD_TESTS OFF CACHE BOOL "")
    set(WITH_PERF_TOOL OFF CACHE BOOL "")
    set(ENABLE_CPACK OFF CACHE BOOL "")
    FetchContent_MakeAvailable(ZeroMQ)
endif()
...
# add appropriate install(... EXPORTs) statements so the installed libzmq is bundled to calling package.
...

Thank you for your consideration!

gummif commented 4 years ago

Just wondering, could the snippet you posted not be part of the application cmake, and fetches libzmq before importing cppzmq?

hsgreen commented 4 years ago

Just wondering, could the snippet you posted not be part of the application cmake, and fetches libzmq before importing cppzmq?

I have tried that. It does not work because the find_package(ZeroMQ) does not search for the "staged"/"to be built" library that was just pulled and WILL be built during the build process. It wants to find the fully installed ZeroMQ.

Is there another cmake mechanism/command that will search for a dependency as part of the "build/_deps" directory?