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
This commit is contained in:
Ingo Bauersachs 2022-12-01 18:39:52 +01:00 committed by Azat Khuzhin
parent 81c6b8823c
commit acfac7ae4a
11 changed files with 81 additions and 76 deletions

View File

@ -19,7 +19,7 @@
# start libevent.sln
#
cmake_minimum_required(VERSION 3.1.2 FATAL_ERROR)
cmake_minimum_required(VERSION 3.4 FATAL_ERROR)
if (POLICY CMP0054)
cmake_policy(SET CMP0054 NEW)
@ -578,10 +578,9 @@ if (NOT EVENT__DISABLE_THREAD_SUPPORT)
set(PTHREADS_AVAILABLE ON)
set(EVENT__HAVE_PTHREADS 1)
list(APPEND LIB_APPS ${CMAKE_THREAD_LIBS_INIT})
# for CHECK_SYMBOLS_EXIST()
list(APPEND CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
list(APPEND LIB_APPS Threads::Threads)
CHECK_TYPE_SIZE(pthread_t EVENT__SIZEOF_PTHREAD_T)
list(APPEND SYMBOLS_TO_CHECK pthread_mutexattr_setprotocol)
@ -951,15 +950,14 @@ if (NOT EVENT__DISABLE_OPENSSL)
find_package(OpenSSL REQUIRED)
set(EVENT__HAVE_OPENSSL 1)
set(OPENSSL_TARGETS OpenSSL::SSL)
message(STATUS "OpenSSL include: ${OPENSSL_INCLUDE_DIR}")
message(STATUS "OpenSSL lib: ${OPENSSL_LIBRARIES}")
include_directories(${OPENSSL_INCLUDE_DIR})
list(APPEND SRC_OPENSSL bufferevent_openssl.c bufferevent_ssl.c)
list(APPEND HDR_PUBLIC include/event2/bufferevent_ssl.h)
list(APPEND LIB_APPS ${OPENSSL_LIBRARIES})
list(APPEND LIB_APPS ${OPENSSL_TARGETS})
endif()
if (NOT EVENT__DISABLE_MBEDTLS)
@ -980,15 +978,14 @@ if (NOT EVENT__DISABLE_MBEDTLS)
find_package(MbedTLS REQUIRED)
set(EVENT__HAVE_MBEDTLS 1)
set(MBEDTLS_TARGETS MbedTLS::mbedtls MbedTLS::mbedcrypto MbedTLS::mbedx509)
message(STATUS "mbed TLS include: ${MBEDTLS_INCLUDE_DIR}")
message(STATUS "mbed TLS lib: ${MBEDTLS_LIBRARIES}")
include_directories(${MBEDTLS_INCLUDE_DIR})
list(APPEND SRC_MBEDTLS bufferevent_mbedtls.c bufferevent_ssl.c)
list(APPEND HDR_PUBLIC include/event2/bufferevent_ssl.h)
list(APPEND LIB_APPS ${MBEDTLS_LIBRARIES})
list(APPEND LIB_APPS ${MBEDTLS_TARGETS})
endif()
if (NOT EVENT__DISABLE_TESTS)
@ -996,10 +993,8 @@ if (NOT EVENT__DISABLE_TESTS)
find_package(ZLIB)
if (ZLIB_LIBRARY AND ZLIB_INCLUDE_DIR)
include_directories(${ZLIB_INCLUDE_DIRS})
set(EVENT__HAVE_LIBZ 1)
list(APPEND LIB_APPS ${ZLIB_LIBRARIES})
list(APPEND LIB_APPS ZLIB::ZLIB)
endif()
endif()
@ -1011,7 +1006,7 @@ set(SRC_EXTRA
sha1.c
evrpc.c)
if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" LESS 3.20)
if(${CMAKE_VERSION} VERSION_LESS "3.20")
include(TestBigEndian)
TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
if(IS_BIG_ENDIAN)
@ -1089,16 +1084,14 @@ add_event_library(event_extra
if (NOT EVENT__DISABLE_OPENSSL)
add_event_library(event_openssl
INNER_LIBRARIES event_core
OUTER_INCLUDES ${OPENSSL_INCLUDE_DIR}
LIBRARIES ${OPENSSL_LIBRARIES}
LIBRARIES ${OPENSSL_TARGETS}
SOURCES ${SRC_OPENSSL})
endif()
if (NOT EVENT__DISABLE_MBEDTLS)
add_event_library(event_mbedtls
INNER_LIBRARIES event_core
OUTER_INCLUDES ${MBEDTLS_INCLUDE_DIR}
LIBRARIES ${MBEDTLS_LIBRARIES}
LIBRARIES ${MBEDTLS_TARGETS}
SOURCES ${SRC_MBEDTLS})
endif()
@ -1106,6 +1099,7 @@ if (EVENT__HAVE_PTHREADS)
set(SRC_PTHREADS evthread_pthread.c)
add_event_library(event_pthreads
INNER_LIBRARIES event_core
LIBRARIES Threads::Threads
SOURCES ${SRC_PTHREADS})
endif()

View File

@ -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")

View File

@ -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()

View File

@ -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()

View File

@ -8,9 +8,6 @@ includedir=@includedir@
Name: libevent_core
Description: libevent_core
Version: @VERSION@
Requires:
Conflicts:
Libs: -L${libdir} -levent_core
Libs: -L${libdir} -levent_core@CMAKE_DEBUG_POSTFIX@
Libs.private: @LIBS@
Cflags: -I${includedir}

View File

@ -8,9 +8,7 @@ includedir=@includedir@
Name: libevent_extra
Description: libevent_extra
Version: @VERSION@
Requires:
Conflicts:
Libs: -L${libdir} -levent_extra
Requires: libevent_core
Libs: -L${libdir} -levent_extra@CMAKE_DEBUG_POSTFIX@
Libs.private: @LIBS@
Cflags: -I${includedir}

View File

@ -9,8 +9,6 @@ Name: libevent_mbedtls
Description: libevent_mbedtls adds mbedtls-based TLS support to libevent
Version: @VERSION@
Requires: libevent_core
Conflicts:
Libs: -L${libdir} -levent_mbedtls
Libs.private: @LIBS@ @MBEDTLS_LIBS@
Cflags: -I${includedir} @MBEDTLS_INCS@
Libs: -L${libdir} -levent_mbedtls@CMAKE_DEBUG_POSTFIX@
Libs.private: -lmbedtls -lmbedx509 @LIBS@
Cflags: -I${includedir}

View File

@ -9,8 +9,7 @@ Name: libevent_openssl
Description: libevent_openssl adds openssl-based TLS support to libevent
Version: @VERSION@
Requires: libevent_core
Conflicts:
Libs: -L${libdir} -levent_openssl
Libs.private: @LIBS@ @OPENSSL_LIBS@
Cflags: -I${includedir} @OPENSSL_INCS@
Requires.private: libssl
Libs: -L${libdir} -levent_openssl@CMAKE_DEBUG_POSTFIX@
Libs.private: @LIBS@
Cflags: -I${includedir}

View File

@ -9,8 +9,6 @@ Name: libevent_pthreads
Description: libevent_pthreads adds pthreads-based threading support to libevent
Version: @VERSION@
Requires: libevent_core
Conflicts:
Libs: -L${libdir} -levent_pthreads
Libs.private: @LIBS@ @PTHREAD_LIBS@
Cflags: -I${includedir} @PTHREAD_CFLAGS@
Libs: -L${libdir} -levent_pthreads@CMAKE_DEBUG_POSTFIX@
Libs.private: @CMAKE_THREAD_LIBS_INIT@ @LIBS@
Cflags: -I${includedir}

View File

@ -1,7 +1,8 @@
cmake_minimum_required(VERSION 3.1.2)
cmake_minimum_required(VERSION 3.4)
if (POLICY CMP0074)
cmake_policy(SET CMP0074 NEW)
endif()
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../cmake")
project(verify)
# set(CMAKE_VERBOSE_MAKEFILE 1)
if(NOT ${EVENT__CODE_COMPONENT} STREQUAL "")

View File

@ -46,10 +46,24 @@ def link_and_run(link, code):
"""
exec_cmd("cmake --build . -v --target clean", True)
arch = ''
vcpkg = ''
openssldir = ''
mbedtlsdir = ''
if platform.system() == "Windows":
arch = '-A x64'
cmd = 'cmake .. %s -DEVENT__LINK_COMPONENT=%s -DEVENT__CODE_COMPONENT=%s' % (
arch, link, code)
vcpkg_root = os.environ.get('VCPKG_ROOT')
if vcpkg_root is not None:
vcpkg = f"-DCMAKE_TOOLCHAIN_FILE={vcpkg_root}/scripts/buildsystems/vcpkg.cmake"
elif platform.system() == "Darwin":
openssldir = '-DOPENSSL_ROOT_DIR=/usr/local/opt/openssl'
mbedtlsdir = '-DMBEDTLS_ROOT_DIR=/usr/local/opt/mbedtls@2'
cmd = f"cmake .." \
f" {arch}" \
f" {vcpkg}" \
f" -DEVENT__LINK_COMPONENT={link}" \
f" -DEVENT__CODE_COMPONENT={code}" \
f" {openssldir}" \
f" {mbedtlsdir}"
if link_type == "static":
cmd = "".join([cmd, " -DLIBEVENT_STATIC_LINK=1"])
r = exec_cmd(cmd, True)