Branch
Hash :
acfac7ae
Author :
Date :
2022-12-01T18:39:52
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
# Copyright 2017-2019 AVSystem <avsystem@avsystem.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#.rst:
# FindMbedTLS
# -----------
#
# Find the mbedTLS encryption library.
#
# Imported Targets
# ^^^^^^^^^^^^^^^^
#
# This module defines the following :prop_tgt:`IMPORTED` targets:
#
# ``mbedtls``
# The mbedTLS ``mbedtls`` library, if found.
# ``mbedcrypto``
# The mbedtls ``crypto`` library, if found.
# ``mbedx509``
# The mbedtls ``x509`` library, if found.
#
# Result Variables
# ^^^^^^^^^^^^^^^^
#
# This module will set the following variables in your project:
#
# ``MBEDTLS_FOUND``
# System has the mbedTLS library.
# ``MBEDTLS_INCLUDE_DIR``
# The mbedTLS include directory.
# ``MBEDTLS_LIBRARY``
# The mbedTLS SSL library.
# ``MBEDTLS_CRYPTO_LIBRARY``
# The mbedTLS crypto library.
# ``MBEDTLS_X509_LIBRARY``
# The mbedTLS x509 library.
# ``MBEDTLS_LIBRARIES``
# All mbedTLS libraries.
# ``MBEDTLS_VERSION``
# This is set to ``$major.$minor.$patch``.
# ``MBEDTLS_VERSION_MAJOR``
# Set to major mbedTLS version number.
# ``MBEDTLS_VERSION_MINOR``
# Set to minor mbedTLS version number.
# ``MBEDTLS_VERSION_PATCH``
# Set to patch mbedTLS version number.
#
# Hints
# ^^^^^
#
# Set ``MBEDTLS_ROOT_DIR`` to the root directory of an mbedTLS installation.
# Set ``MBEDTLS_USE_STATIC_LIBS`` to ``TRUE`` to look for static libraries.
if(MBEDTLS_ROOT_DIR)
# Disable re-rooting paths in find_path/find_library.
# This assumes MBEDTLS_ROOT_DIR is an absolute path.
set(_EXTRA_FIND_ARGS "NO_CMAKE_FIND_ROOT_PATH")
endif()
find_path(MBEDTLS_INCLUDE_DIR
NAMES mbedtls/ssl.h
PATH_SUFFIXES include
HINTS ${MBEDTLS_ROOT_DIR}
${_EXTRA_FIND_ARGS})
# based on https://github.com/ARMmbed/mbedtls/issues/298
function(mbedtls_get_version_numbers FILE)
file(STRINGS "${MBEDTLS_INCLUDE_DIR}/${FILE}" VERSION_STRING_LINE REGEX "^#define MBEDTLS_VERSION_STRING[ \\t\\n\\r]+\"[^\"]*\"$")
file(STRINGS "${MBEDTLS_INCLUDE_DIR}/${FILE}" VERSION_MAJOR_LINE REGEX "^#define MBEDTLS_VERSION_MAJOR[ \\t\\n\\r]+[0-9]+$")
file(STRINGS "${MBEDTLS_INCLUDE_DIR}/${FILE}" VERSION_MINOR_LINE REGEX "^#define MBEDTLS_VERSION_MINOR[ \\t\\n\\r]+[0-9]+$")
file(STRINGS "${MBEDTLS_INCLUDE_DIR}/${FILE}" VERSION_PATCH_LINE REGEX "^#define MBEDTLS_VERSION_PATCH[ \\t\\n\\r]+[0-9]+$")
string(REGEX REPLACE "^#define MBEDTLS_VERSION_STRING[ \\t\\n\\r]+\"([^\"]*)\"$" "\\1" MBEDTLS_VERSION "${VERSION_STRING_LINE}")
string(REGEX REPLACE "^#define MBEDTLS_VERSION_MAJOR[ \\t\\n\\r]+([0-9]+)$" "\\1" MBEDTLS_VERSION_MAJOR "${VERSION_MAJOR_LINE}")
string(REGEX REPLACE "^#define MBEDTLS_VERSION_MINOR[ \\t\\n\\r]+([0-9]+)$" "\\1" MBEDTLS_VERSION_MINOR "${VERSION_MINOR_LINE}")
string(REGEX REPLACE "^#define MBEDTLS_VERSION_PATCH[ \\t\\n\\r]+([0-9]+)$" "\\1" MBEDTLS_VERSION_PATCH "${VERSION_PATCH_LINE}")
set(MBEDTLS_VERSION "${MBEDTLS_VERSION}" PARENT_SCOPE)
set(MBEDTLS_VERSION_MAJOR "${MBEDTLS_VERSION_MAJOR}" PARENT_SCOPE)
set(MBEDTLS_VERSION_MINOR "${MBEDTLS_VERSION_MINOR}" PARENT_SCOPE)
set(MBEDTLS_VERSION_PATCH "${MBEDTLS_VERSION_PATCH}" PARENT_SCOPE)
endfunction()
if(MBEDTLS_INCLUDE_DIR AND EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h")
mbedtls_get_version_numbers("mbedtls/version.h")
if ("${MBEDTLS_VERSION}" STREQUAL "")
mbedtls_get_version_numbers("mbedtls/build_info.h")
endif()
endif()
if(MBEDTLS_USE_STATIC_LIBS)
set(_MBEDTLS_LIB_NAME libmbedtls.a)
set(_MBEDTLS_CRYPTO_LIB_NAME libmbedcrypto.a)
set(_MBEDTLS_X509_LIB_NAME libmbedx509.a)
else()
set(_MBEDTLS_LIB_NAME mbedtls)
set(_MBEDTLS_CRYPTO_LIB_NAME mbedcrypto)
set(_MBEDTLS_X509_LIB_NAME mbedx509)
endif()
# lib - standard path suffix
# library - is the suffix that is used in build tree
find_library(MBEDTLS_LIBRARY
NAMES ${_MBEDTLS_LIB_NAME}
PATH_SUFFIXES lib library
HINTS ${MBEDTLS_ROOT_DIR}
${_EXTRA_FIND_ARGS})
find_library(MBEDTLS_CRYPTO_LIBRARY
NAMES ${_MBEDTLS_CRYPTO_LIB_NAME}
PATH_SUFFIXES lib library
HINTS ${MBEDTLS_ROOT_DIR}
${_EXTRA_FIND_ARGS})
find_library(MBEDTLS_X509_LIBRARY
NAMES ${_MBEDTLS_X509_LIB_NAME}
PATH_SUFFIXES lib library
HINTS ${MBEDTLS_ROOT_DIR}
${_EXTRA_FIND_ARGS})
set(MBEDTLS_LIBRARIES ${MBEDTLS_LIBRARY} ${MBEDTLS_CRYPTO_LIBRARY} ${MBEDTLS_X509_LIBRARY})
if(MBEDTLS_INCLUDE_DIR)
set(MBEDTLS_FOUND TRUE)
endif()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(MbedTLS
FOUND_VAR MBEDTLS_FOUND
REQUIRED_VARS
MBEDTLS_INCLUDE_DIR
MBEDTLS_LIBRARY
MBEDTLS_CRYPTO_LIBRARY
MBEDTLS_X509_LIBRARY
MBEDTLS_LIBRARIES
MBEDTLS_VERSION
VERSION_VAR MBEDTLS_VERSION)
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 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 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()