diff options
author | Ingo Bauersachs <ingo.bauersachs@xovis.com> | 2022-12-01 18:39:52 +0100 |
---|---|---|
committer | Azat Khuzhin <azat@libevent.org> | 2023-05-16 07:46:56 +0200 |
commit | acfac7ae4a3edbbb7ce4ceee7208b4245a6e203e (patch) | |
tree | d5cff6a42f735e390e30a8ededa681b12a35ff09 /cmake | |
parent | 81c6b8823c1b58d7837e827bb1098aa5f9e5956b (diff) | |
download | libevent-acfac7ae4a3edbbb7ce4ceee7208b4245a6e203e.tar.gz |
Make dependency paths relocatable
The generated configurations for both CMake and pkg-config included
absolute paths to dependencies (OpenSSL, MbedTLS). This is contrary
to the general CMake advise to create relocatable packages [1].
Additionally, when building both mbedtls and libevent via CMake's
FetchContent in the same project, loading the project would fail with
INTERFACE_INCLUDE_DIRECTORIES property contains path:
"/home/user/project/cmake-build/_deps/mbedtls-build/include"
which is prefixed in the source directory.
The required changes include:
- Adding the outer includes only to the BUILD_INTERFACE solves the
makes the CMake paths relocatable and thus solves the FetchContent
problem.
- Updates to libevent_*.pc.in fixes the relocatable issues for
pkg-config and properly declares currently missing dependencies.
- Using components for linking to OpenSSL (requiring CMake 3.4)
and MbedTLS. The new MbedTLS target names now match the component
names of the MbedTLS' CMake project.
- Use the Threads CMake library reference instead of a direct
reference to support both built-in pthread and -lpthread.
v2 (azat): get back CMAKE_REQUIRED_LIBRARIES
[1] https://cmake.org/cmake/help/v3.25/manual/cmake-packages.7.html#creating-relocatable-packages
Diffstat (limited to 'cmake')
-rw-r--r-- | cmake/AddEventLibrary.cmake | 31 | ||||
-rw-r--r-- | cmake/FindMbedTLS.cmake | 20 | ||||
-rw-r--r-- | cmake/LibeventConfig.cmake.in | 21 |
3 files changed, 39 insertions, 33 deletions
diff --git a/cmake/AddEventLibrary.cmake b/cmake/AddEventLibrary.cmake index ffd7e888..203081b2 100644 --- a/cmake/AddEventLibrary.cmake +++ b/cmake/AddEventLibrary.cmake @@ -1,4 +1,5 @@ include(CMakeParseArguments) +include(GNUInstallDirs) set(LIBEVENT_SHARED_LIBRARIES "") set(LIBEVENT_STATIC_LIBRARIES "") @@ -11,10 +12,10 @@ macro(set_event_shared_lib_flags LIB_NAME) endmacro() macro(generate_pkgconfig LIB_NAME) - set(prefix ${CMAKE_INSTALL_PREFIX}) - set(exec_prefix ${CMAKE_INSTALL_PREFIX}) - set(libdir ${CMAKE_INSTALL_LIBDIR}) - set(includedir ${CMAKE_INSTALL_INCLUDEDIR}) + set(prefix "${CMAKE_INSTALL_PREFIX}") + set(exec_prefix "\${prefix}") + set(libdir "\${prefix}/${CMAKE_INSTALL_LIBDIR}") + set(includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}") set(VERSION ${EVENT_ABI_LIBVERSION}) @@ -23,11 +24,6 @@ macro(generate_pkgconfig LIB_NAME) set(LIBS "${LIBS} -l${LIB}") endforeach() - set(OPENSSL_LIBS "") - foreach(LIB ${OPENSSL_LIBRARIES}) - set(OPENSSL_LIBS "${OPENSSL_LIBS} -l${LIB}") - endforeach() - configure_file("lib${LIB_NAME}.pc.in" "lib${LIB_NAME}.pc" @ONLY) install( FILES "${CMAKE_CURRENT_BINARY_DIR}/lib${LIB_NAME}.pc" @@ -37,7 +33,7 @@ endmacro() # LIB_NAME maybe event_core, event_extra, event_openssl, event_pthreads or event. # Targets whose LIB_NAME is not 'event' should be exported and installed. -macro(export_install_target TYPE LIB_NAME OUTER_INCLUDES) +macro(export_install_target TYPE LIB_NAME) if("${LIB_NAME}" STREQUAL "event") install(TARGETS "${LIB_NAME}_${TYPE}" LIBRARY DESTINATION "lib" COMPONENT lib @@ -57,7 +53,6 @@ macro(export_install_target TYPE LIB_NAME OUTER_INCLUDES) PUBLIC "$<INSTALL_INTERFACE:include>" "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>" "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>" - ${OUTER_INCS} ) set_target_properties("${LIB_NAME}_${TYPE}" PROPERTIES EXPORT_NAME ${PURE_NAME}) export(TARGETS "${LIB_NAME}_${TYPE}" @@ -81,8 +76,7 @@ endmacro() # - EVENT_ABI_LIBVERSION_REVISION # - EVENT_ABI_LIBVERSION_AGE # - EVENT_PACKAGE_RELEASE -# - CMAKE_THREAD_LIBS_INIT LIB_PLATFORM -# - OPENSSL_LIBRARIES +# - LIB_PLATFORM # - EVENT_SHARED_FLAGS # - EVENT_LIBRARY_STATIC # - EVENT_LIBRARY_SHARED @@ -94,13 +88,10 @@ macro(add_event_library LIB_NAME) cmake_parse_arguments(LIB "" # Options "VERSION" # One val - "SOURCES;LIBRARIES;INNER_LIBRARIES;OUTER_INCLUDES" # Multi val + "SOURCES;LIBRARIES;INNER_LIBRARIES" # Multi val ${ARGN} ) - if ("${LIB_OUTER_INCLUDES}" STREQUAL "") - set(LIB_OUTER_INCLUDES NONE) - endif() set(ADD_EVENT_LIBRARY_INTERFACE) set(INNER_LIBRARIES) @@ -114,12 +105,11 @@ macro(add_event_library LIB_NAME) set(INNER_LIBRARIES "${LIB_INNER_LIBRARIES}_static") endif() target_link_libraries("${LIB_NAME}_static" - ${CMAKE_THREAD_LIBS_INIT} ${LIB_PLATFORM} ${INNER_LIBRARIES} ${LIB_LIBRARIES}) - export_install_target(static "${LIB_NAME}" "${LIB_OUTER_INCLUDES}") + export_install_target(static "${LIB_NAME}") set(ADD_EVENT_LIBRARY_INTERFACE "${LIB_NAME}_static") endif() @@ -131,7 +121,6 @@ macro(add_event_library LIB_NAME) set(INNER_LIBRARIES "${LIB_INNER_LIBRARIES}_shared") endif() target_link_libraries("${LIB_NAME}_shared" - ${CMAKE_THREAD_LIBS_INIT} ${LIB_PLATFORM} ${INNER_LIBRARIES} ${LIB_LIBRARIES}) @@ -174,7 +163,7 @@ macro(add_event_library LIB_NAME) WORKING_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}") endif() - export_install_target(shared "${LIB_NAME}" "${LIB_OUTER_INCLUDES}") + export_install_target(shared "${LIB_NAME}") set(ADD_EVENT_LIBRARY_INTERFACE "${LIB_NAME}_shared") diff --git a/cmake/FindMbedTLS.cmake b/cmake/FindMbedTLS.cmake index 39ff8204..fea0f087 100644 --- a/cmake/FindMbedTLS.cmake +++ b/cmake/FindMbedTLS.cmake @@ -151,24 +151,26 @@ find_package_handle_standard_args(MbedTLS VERSION_VAR MBEDTLS_VERSION) -if(NOT TARGET mbedtls) - add_library(mbedtls UNKNOWN IMPORTED) - set_target_properties(mbedtls PROPERTIES +if(NOT TARGET MbedTLS::mbedtls) + add_library(MbedTLS::mbedtls UNKNOWN IMPORTED) + set_target_properties(MbedTLS::mbedtls PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INCLUDE_DIR}" IMPORTED_LINK_INTERFACE_LANGUAGES "C" + INTERFACE_LINK_LIBRARIES MbedTLS::mbedx509 IMPORTED_LOCATION "${MBEDTLS_LIBRARY}") endif() -if(NOT TARGET mbedcrypto) - add_library(mbedcrypto UNKNOWN IMPORTED) - set_target_properties(mbedcrypto PROPERTIES +if(NOT TARGET MbedTLS::mbedcrypto) + add_library(MbedTLS::mbedcrypto UNKNOWN IMPORTED) + set_target_properties(MbedTLS::mbedcrypto PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C" IMPORTED_LOCATION "${MBEDTLS_CRYPTO_LIBRARY}") endif() -if(NOT TARGET mbedx509) - add_library(mbedx509 UNKNOWN IMPORTED) - set_target_properties(mbedx509 PROPERTIES +if(NOT TARGET MbedTLS::mbedx509) + add_library(MbedTLS::mbedx509 UNKNOWN IMPORTED) + set_target_properties(MbedTLS::mbedx509 PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C" + INTERFACE_LINK_LIBRARIES MbedTLS::mbedcrypto IMPORTED_LOCATION "${MBEDTLS_X509_LIBRARY}") endif() diff --git a/cmake/LibeventConfig.cmake.in b/cmake/LibeventConfig.cmake.in index f9b00363..1ae5e3e3 100644 --- a/cmake/LibeventConfig.cmake.in +++ b/cmake/LibeventConfig.cmake.in @@ -16,8 +16,9 @@ # The following table lists all available components. If none is given, all imported targets will used. # core - the core functons of libevent # extra - extra functions, contains http, dns and rpc -# pthreads - multiple threads for libevent, not exists on Windows -# openssl - openssl support for libevent +# pthreads - multiple threads for libevent, does not exist on Windows +# openssl - OpenSSL support for libevent +# mbedtls - MbedTLS support for libevent # # By default, the shared libraries of libevent will be found. To find the static ones instead, # you must set the LIBEVENT_STATIC_LINK variable to TRUE before calling find_package(Libevent ...). @@ -34,6 +35,14 @@ set(LIBEVENT_VERSION @EVENT_PACKAGE_VERSION@) +# Load the dependencies of all components. As find_dependency propagates the original +# find_package attributes (i.e. required or not), there's no need to repeat this or filter +# by component. +include(CMakeFindDependencyMacro) +find_dependency(Threads) +find_dependency(MbedTLS) +find_dependency(OpenSSL) + # IMPORTED targets from LibeventTargets.cmake set(LIBEVENT_STATIC_LIBRARIES "@LIBEVENT_STATIC_LIBRARIES@") set(LIBEVENT_SHARED_LIBRARIES "@LIBEVENT_SHARED_LIBRARIES@") @@ -46,6 +55,12 @@ endif() if(${LIBEVENT_STATIC_LINK}) set(_LIB_TYPE static) set(_AVAILABLE_LIBS "${LIBEVENT_STATIC_LIBRARIES}") + + # CMake before 3.15 doesn't link OpenSSL to pthread/dl, do it ourselves instead + if (${CMAKE_VERSION} VERSION_LESS "3.15.0" AND ${LIBEVENT_STATIC_LINK} AND ${OPENSSL_FOUND} AND ${Threads_FOUND}) + set_property(TARGET OpenSSL::Crypto APPEND PROPERTY INTERFACE_LINK_LIBRARIES Threads::Threads) + set_property(TARGET OpenSSL::Crypto APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${CMAKE_DL_LIBS}) + endif () else() set(_LIB_TYPE shared) set(_AVAILABLE_LIBS "${LIBEVENT_SHARED_LIBRARIES}") @@ -64,7 +79,7 @@ macro(no_component_msg _comp) set(pthreadlib ", pthreads") endif() message(FATAL_ERROR "Your libevent library does not contain a ${_comp} component!\n" - "The valid components are core, extra${pthreadlib} and openssl.") + "The valid components are core, extra${pthreadlib}, openssl and mbedtls.") else() message_if_needed(WARNING "Your libevent library does not contain a ${_comp} component!") endif() |