mirror of https://github.com/oxen-io/oxen-mq.git
More linking overhaul
- Don't try to use cppzmq, just find libzmq ourselves. - Allow existing `libzmq` and `sodium` targets to be used to control how we link to libzmq and/or sodium. - Use PkgConfig:: targets instead of the older bunch-of-variables approach (requires cmake >= 3.6).
This commit is contained in:
parent
bc0e6be801
commit
a36e53d409
|
@ -1,4 +1,4 @@
|
||||||
cmake_minimum_required(VERSION 3.5)
|
cmake_minimum_required(VERSION 3.7)
|
||||||
|
|
||||||
project(liblokimq CXX)
|
project(liblokimq CXX)
|
||||||
|
|
||||||
|
@ -38,26 +38,36 @@ find_package(Threads REQUIRED)
|
||||||
target_link_libraries(lokimq PRIVATE Threads::Threads)
|
target_link_libraries(lokimq PRIVATE Threads::Threads)
|
||||||
|
|
||||||
# libzmq is nearly impossible to link statically from a system-installed static library: it depends
|
# libzmq is nearly impossible to link statically from a system-installed static library: it depends
|
||||||
# on a ton of other libraries which typically are not all statically available. So if the caller
|
# on a ton of other libraries, some of which are not all statically available. If the caller wants
|
||||||
# asked us to do things statically, don't even try to find a system lib and just build it. If they
|
# to mess with this, so be it: they can set up a libzmq target and we'll use it. Otherwise if they
|
||||||
# really want it, they can set up the libzmq_FOUND etc. variables themselves.
|
# asked us to do things statically, don't even try to find a system lib and just build it.
|
||||||
if(BUILD_SHARED_LIBS)
|
set(lokimq_build_static_libzmq OFF)
|
||||||
|
if(TARGET libzmq)
|
||||||
|
target_link_libraries(lokimq PUBLIC libzmq)
|
||||||
|
elseif(BUILD_SHARED_LIBS)
|
||||||
include(FindPkgConfig)
|
include(FindPkgConfig)
|
||||||
pkg_search_module(libzmq QUIET libzmq>=4.3)
|
pkg_check_modules(libzmq libzmq>=4.3 IMPORTED_TARGET)
|
||||||
endif()
|
if(libzmq_FOUND)
|
||||||
if(NOT libzmq_FOUND)
|
target_link_libraries(lokimq PUBLIC PkgConfig::libzmq)
|
||||||
message(STATUS "libzmq >= 4.3 not found, building bundled 4.3.2")
|
else()
|
||||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/local-libzmq")
|
set(lokimq_build_static_libzmq ON)
|
||||||
include(LocalLibzmq)
|
endif()
|
||||||
|
else()
|
||||||
|
set(lokimq_build_static_libzmq ON)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
set(CPPZMQ_BUILD_TESTS OFF CACHE BOOL "Disable cppzmq tests")
|
if(lokimq_build_static_libzmq)
|
||||||
add_subdirectory(cppzmq EXCLUDE_FROM_ALL)
|
message(STATUS "libzmq >= 4.3 not found or static build requested, building bundled 4.3.2")
|
||||||
|
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/local-libzmq")
|
||||||
|
include(LocalLibzmq)
|
||||||
|
target_link_libraries(lokimq PUBLIC libzmq_vendor)
|
||||||
|
endif()
|
||||||
|
|
||||||
target_include_directories(lokimq
|
target_include_directories(lokimq
|
||||||
PUBLIC
|
PUBLIC
|
||||||
$<INSTALL_INTERFACE:>
|
$<INSTALL_INTERFACE:>
|
||||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
|
||||||
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/cppzmq>
|
||||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/mapbox-variant/include>
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/mapbox-variant/include>
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -69,49 +79,39 @@ set_target_properties(lokimq PROPERTIES
|
||||||
POSITION_INDEPENDENT_CODE ON
|
POSITION_INDEPENDENT_CODE ON
|
||||||
)
|
)
|
||||||
|
|
||||||
if(NOT SODIUM_LIBRARIES)
|
|
||||||
pkg_search_module(SODIUM REQUIRED libsodium)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if(NOT ${CMAKE_VERSION} VERSION_LESS 3.13)
|
|
||||||
# allow adding links to non-local targets (which is allowed even under old policy as long as the
|
|
||||||
# target is an interface, but under new cmake it spews out warnings).
|
|
||||||
cmake_policy(SET CMP0079 NEW)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
function(link_dep_libs target linktype libdirs)
|
function(link_dep_libs target linktype libdirs)
|
||||||
foreach(lib ${ARGN})
|
foreach(lib ${ARGN})
|
||||||
find_library(link_lib-${lib} NAMES ${lib} PATHS ${libdirs})
|
find_library(link_lib-${lib} NAMES ${lib} PATHS ${libdirs})
|
||||||
|
message(STATUS "FIND ${lib} FOUND ${link_lib-${lib}}")
|
||||||
if(link_lib-${lib})
|
if(link_lib-${lib})
|
||||||
target_link_libraries(${target} ${linktype} ${link_lib-${lib}})
|
target_link_libraries(${target} ${linktype} ${link_lib-${lib}})
|
||||||
endif()
|
endif()
|
||||||
endforeach()
|
endforeach()
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
# Really want to just use SODIUM_LINK_LIBRARIES here, but that isn't in older cmake:
|
# If the caller has already set up a sodium target then we will just link to it, otherwise we go
|
||||||
if(BUILD_SHARED_LIBS)
|
# looking for it.
|
||||||
link_dep_libs(lokimq PRIVATE ${SODIUM_LIBRARY_DIRS} ${SODIUM_LIBRARIES})
|
if(TARGET sodium)
|
||||||
target_include_directories(lokimq PRIVATE ${SODIUM_INCLUDE_DIRS})
|
target_link_libraries(lokimq PRIVATE sodium)
|
||||||
|
if(lokimq_build_static_libzmq)
|
||||||
if(TARGET libzmq_vendor)
|
target_link_libraries(libzmq_vendor INTERFACE sodium)
|
||||||
link_dep_libs(libzmq_vendor INTERFACE "${SODIUM_LIBRARY_DIRS}" ${SODIUM_LIBRARIES})
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
target_link_libraries(lokimq PUBLIC cppzmq)
|
|
||||||
else()
|
else()
|
||||||
link_dep_libs(lokimq PRIVATE "${SODIUM_STATIC_LIBRARY_DIRS}" ${SODIUM_STATIC_LIBRARIES})
|
pkg_check_modules(sodium REQUIRED libsodium IMPORTED_TARGET)
|
||||||
target_include_directories(lokimq PRIVATE ${SODIUM_STATIC_INCLUDE_DIRS})
|
|
||||||
|
|
||||||
if (libzmq_FOUND)
|
if(BUILD_SHARED_LIBS)
|
||||||
# cppzmq doesn't properly set up its libzmq target dependencies so do it here (this isn't the
|
target_link_libraries(lokimq PRIVATE PkgConfig::sodium)
|
||||||
# normal case: we only get here if someone pre-set libzmq_FOUND etc. *and* wants static libs)
|
if(lokimq_build_static_libzmq)
|
||||||
link_dep_libs(cppzmq-static INTERFACE "${PC_LIBZMQ_STATIC_LIBRARY_DIRS}" ${PC_LIBZMQ_STATIC_LIBRARIES})
|
target_link_libraries(libzmq_vendor INTERFACE PkgConfig::sodium)
|
||||||
else()
|
endif()
|
||||||
# Otherwise we built a static vendored one, so make sure it links to the static libsodium
|
else()
|
||||||
link_dep_libs(libzmq_vendor INTERFACE "${SODIUM_STATIC_LIBRARY_DIRS}" ${SODIUM_STATIC_LIBRARIES})
|
link_dep_libs(lokimq PRIVATE "${sodium_STATIC_LIBRARY_DIRS}" ${sodium_STATIC_LIBRARIES})
|
||||||
|
target_include_directories(lokimq PRIVATE ${sodium_STATIC_INCLUDE_DIRS})
|
||||||
|
if(lokimq_build_static_libzmq)
|
||||||
|
link_dep_libs(libzmq_vendor INTERFACE "${sodium_STATIC_LIBRARY_DIRS}" ${sodium_STATIC_LIBRARIES})
|
||||||
|
target_link_libraries(libzmq_vendor INTERFACE ${sodium_STATIC_INCLUDE_DIRS})
|
||||||
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
target_link_libraries(lokimq PUBLIC cppzmq-static)
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
add_library(lokimq::lokimq ALIAS lokimq)
|
add_library(lokimq::lokimq ALIAS lokimq)
|
||||||
|
|
Loading…
Reference in New Issue