Skip to content
Snippets Groups Projects
FindZeroMQ.cmake 4.38 KiB
Newer Older
# - Try to find ZeroMQ
#
# Usage:
#
#  find_package(ZeroMQ [version] [REQUIRED|QUIET])
#
#  Any (optional) [version] given to find_package() will be tested
#
# Once done this will define
#
#     ZeroMQ_FOUND   - System has ZeroMQ
#     ZeroMQ_VERSION - ZeroMQ version
#
# and the IMPORTED target libzmq will be defined, with the required
# INTERFACE definitions to find zmq_compat.h etc. etc.
#
#    target_link_libraries(MyTarget [PRIVATE|PUBLIC|INTERFACE] libzmq)
#
# should be sufficient to compile and link any code using the ZeroMQ C++ bindings.
#
# 'zmq_compat.h'
#
# We provide this utility header in order to handle deprecation warnings when using
# newer versions of ZeroMQ. It defines the following symbols:
#
#~~~{.cpp}
#define ZMQ_SETSOCKOPT_DEPRECATED
#define ZMQ_USE_SEND_FLAGS
#define ZMQ_USE_RECV_WITH_REFERENCE
#~~~
#
# The target libzmq contains the necessary include_directory path.
# The file will be automatically installed in CMAKE_INSTALL_PKGINCDIR (if defined), or in
# CMAKE_INSTALL_INCLUDEDIR
#
# NOTES:
#
# - By default the usual system directories are searched before all others
#
# - Any installation path in CMAKE_PREFIX_PATH will take precedence over system directories
# 
# - Or you can set either of the following with the installation path :
#
#     ZeroMQ_DIR
#     ZeroMQ_ROOT
#

# CMake minimum version 3.12 required to use ZeroMQ_ROOT in find_path/library

cmake_minimum_required(VERSION 3.12)

if(NOT ZeroMQ_FOUND)

if(ZeroMQ_DIR)

   find_path (ZeroMQ_INCLUDE_DIR
      NAMES zmq.hpp
      PATHS ${ZeroMQ_DIR}/include
      NO_DEFAULT_PATH
      )
      
   find_library (ZeroMQ_LIBRARY
      NAMES zmq
      PATHS ${ZeroMQ_DIR}/lib ${ZeroMQ_DIR}/lib64
      NO_DEFAULT_PATH
      )

else(ZeroMQ_DIR)

   find_path (ZeroMQ_INCLUDE_DIR
      NAMES zmq.hpp
      )
      
   find_library (ZeroMQ_LIBRARY
      NAMES zmq
      )

endif(ZeroMQ_DIR)

      
if((NOT ZeroMQ_INCLUDE_DIR) OR (NOT ZeroMQ_LIBRARY))
   ## load in pkg-config support
   find_package(PkgConfig QUIET)
   
   if(PkgConfig_FOUND)
      ## use pkg-config to get hints for 0mq locations
      pkg_check_modules(PC_ZeroMQ QUIET libzmq)

      ## use the hint from above to find where 'zmq.hpp' is located
      find_path(ZeroMQ_INCLUDE_DIR
        NAMES zmq.hpp
        PATHS ${PC_ZeroMQ_INCLUDE_DIRS}
        )

      ## use the hint from above to find the location of libzmq
      find_library(ZeroMQ_LIBRARY
        NAMES zmq
        PATHS ${PC_ZeroMQ_LIBRARY_DIRS}
        )
   endif(PkgConfig_FOUND)
   
endif((NOT ZeroMQ_INCLUDE_DIR) OR (NOT ZeroMQ_LIBRARY))

if(ZeroMQ_INCLUDE_DIR AND ZeroMQ_LIBRARY)
   # get libzmq version from zmq.h header file
   file(READ "${ZeroMQ_INCLUDE_DIR}/zmq.h" _ZMQ_H_CONTENTS)
   string(REGEX REPLACE ".*#define ZMQ_VERSION_MAJOR ([0-9]+).*" "\\1" DETECTED_ZMQ_VERSION_MAJOR "${_ZMQ_H_CONTENTS}")
   string(REGEX REPLACE ".*#define ZMQ_VERSION_MINOR ([0-9]+).*" "\\1" DETECTED_ZMQ_VERSION_MINOR "${_ZMQ_H_CONTENTS}")
   string(REGEX REPLACE ".*#define ZMQ_VERSION_PATCH ([0-9]+).*" "\\1" DETECTED_ZMQ_VERSION_PATCH "${_ZMQ_H_CONTENTS}")
   set(ZeroMQ_VERSION "${DETECTED_ZMQ_VERSION_MAJOR}.${DETECTED_ZMQ_VERSION_MINOR}.${DETECTED_ZMQ_VERSION_PATCH}")
endif(ZeroMQ_INCLUDE_DIR AND ZeroMQ_LIBRARY)

include ( FindPackageHandleStandardArgs )
# handle the QUIETLY and REQUIRED arguments and set ZMQ_FOUND to TRUE
# if all listed variables are TRUE
find_package_handle_standard_args (ZeroMQ
   VERSION_VAR ZeroMQ_VERSION
   REQUIRED_VARS ZeroMQ_LIBRARY ZeroMQ_INCLUDE_DIR
)

if(NOT TARGET libzmq)
   # Create IMPORTED targets with all correct attributes
   add_library(libzmq SHARED IMPORTED)
   set_property(TARGET libzmq APPEND PROPERTY IMPORTED_CONFIGURATIONS NOCONFIG)
   get_filename_component(ZeroMQ_LIB_SO ${ZeroMQ_LIBRARY} NAME)
   set_target_properties(libzmq PROPERTIES
      INTERFACE_INCLUDE_DIRECTORIES "${ZeroMQ_INCLUDE_DIR}"
      INTERFACE_INCLUDE_DIRECTORIES $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>
      IMPORTED_LOCATION_NOCONFIG    "${ZeroMQ_LIBRARY}"
      IMPORTED_SONAME_NOCONFIG      "${ZeroMQ_LIB_SO}"
   )
endif()

# Install zmq_compat.h if it exists
if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/zmq_compat.h)
   if(CMAKE_INSTALL_PKGINCDIR)
      install(FILES ${CMAKE_CURRENT_LIST_DIR}/zmq_compat.h DESTINATION ${CMAKE_INSTALL_PKGINCDIR})
   elseif(CMAKE_INSTALL_INCLUDEDIR)
      install(FILES ${CMAKE_CURRENT_LIST_DIR}/zmq_compat.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
   endif()
endif()

endif(NOT ZeroMQ_FOUND)