Entire-Metadata: .entire/metadata/fb87247f-2a75-407e-b336-cbc8c20ba5a6 Entire-Session: fb87247f-2a75-407e-b336-cbc8c20ba5a6 Entire-Strategy: manual-commit
324 lines
10 KiB
CMake
324 lines
10 KiB
CMake
# (c) 2022 Copyright, Real-Time Innovations, Inc. All rights reserved.
|
|
# No duplications, whole or partial, manual or electronic, may be made
|
|
# without express written permission. Any such copies, or revisions thereof,
|
|
# must display this notice unaltered.
|
|
# This code contains trade secrets of Real-Time Innovations, Inc.
|
|
|
|
#[=======================================================================[.rst:
|
|
FindOpenSSL3
|
|
------------
|
|
|
|
Find the OpenSSL encryption library for version above 3.
|
|
|
|
Optional COMPONENTS
|
|
^^^^^^^^^^^^^^^^^^^
|
|
|
|
This module supports two optional COMPONENTS: ``Crypto`` and ``SSL``. Both
|
|
components have associated imported targets, as described below.
|
|
|
|
Imported Targets
|
|
^^^^^^^^^^^^^^^^
|
|
|
|
This module defines the following ``IMPORTED`` targets:
|
|
|
|
``OpenSSL3::SSL``
|
|
The OpenSSL3 ``ssl`` library, if found.
|
|
``OpenSSL3::Crypto``
|
|
The OpenSSL3 ``crypto`` library, if found.
|
|
|
|
Result Variables
|
|
^^^^^^^^^^^^^^^^
|
|
|
|
This module will set the following variables in your project:
|
|
|
|
``OPENSSL3_FOUND``
|
|
System has the OpenSSL3 library. If no components are requested it only
|
|
requires the crypto library.
|
|
``OPENSSL3_INCLUDE_DIR``
|
|
The OpenSSL3 include directory.
|
|
``OPENSSL3_CRYPTO_LIBRARY``
|
|
The OpenSSL3 crypto library.
|
|
``OPENSSL3_CRYPTO_LIBRARIES``
|
|
The OpenSSL3 crypto library and its dependencies.
|
|
``OPENSSL3_SSL_LIBRARY``
|
|
The OpenSSL3 SSL library.
|
|
``OPENSSL3_SSL_LIBRARIES``
|
|
The OpenSSL3 SSL library and its dependencies.
|
|
``OPENSSL3_LIBRARIES``
|
|
All OpenSSL3 libraries and their dependencies.
|
|
``OPENSSL3_VERSION``
|
|
This is set to ``$major.$minor.$patch`` (e.g. ``3.0.5``).
|
|
|
|
Hints
|
|
^^^^^
|
|
|
|
Set ``OPENSSL3_ROOT_DIR`` to the root directory of an OpenSSL installation.
|
|
Set ``OPENSSL3_USE_STATIC_LIBS`` to ``TRUE`` to look for static libraries.
|
|
Set ``OPENSSL3_MSVC_STATIC_RT`` set ``TRUE`` to choose the MT version of the lib.
|
|
#]=======================================================================]
|
|
|
|
macro(_OpenSSL3_test_and_find_dependencies ssl_library crypto_library)
|
|
if((CMAKE_SYSTEM_NAME STREQUAL "Linux") AND
|
|
(("${ssl_library}" MATCHES "\\${CMAKE_STATIC_LIBRARY_SUFFIX}$") OR
|
|
("${crypto_library}" MATCHES "\\${CMAKE_STATIC_LIBRARY_SUFFIX}$")))
|
|
set(_OpenSSL3_has_dependencies TRUE)
|
|
find_package(Threads)
|
|
else()
|
|
set(_OpenSSL3_has_dependencies FALSE)
|
|
endif()
|
|
endmacro()
|
|
|
|
function(_OpenSSL3_add_dependencies libraries_var)
|
|
if(CMAKE_THREAD_LIBS_INIT)
|
|
list(APPEND ${libraries_var} ${CMAKE_THREAD_LIBS_INIT})
|
|
endif()
|
|
list(APPEND ${libraries_var} ${CMAKE_DL_LIBS})
|
|
set(${libraries_var} ${${libraries_var}} PARENT_SCOPE)
|
|
endfunction()
|
|
|
|
function(_OpenSSL3_target_add_dependencies target)
|
|
if(_OpenSSL3_has_dependencies)
|
|
set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES Threads::Threads )
|
|
set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${CMAKE_DL_LIBS} )
|
|
endif()
|
|
endfunction()
|
|
|
|
if (UNIX)
|
|
find_package(PkgConfig QUIET)
|
|
pkg_check_modules(_OPENSSL3 QUIET openssl3)
|
|
endif ()
|
|
|
|
# Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES
|
|
if(OPENSSL3_USE_STATIC_LIBS)
|
|
set(_openssl3_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
|
|
if(WIN32)
|
|
set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
|
|
else()
|
|
set(CMAKE_FIND_LIBRARY_SUFFIXES .a )
|
|
endif()
|
|
endif()
|
|
|
|
set(_OPENSSL3_ROOT_HINTS
|
|
${OPENSSL3_ROOT_DIR}
|
|
ENV OPENSSL3_ROOT_DIR
|
|
)
|
|
|
|
set(_OPENSSL3_ROOT_HINTS_AND_PATHS
|
|
HINTS ${_OPENSSL3_ROOT_HINTS}
|
|
PATHS ${_OPENSSL3_ROOT_PATHS}
|
|
)
|
|
|
|
find_path(OPENSSL3_INCLUDE_DIR
|
|
NAMES
|
|
openssl/ssl.h
|
|
${_OPENSSL3_ROOT_HINTS_AND_PATHS}
|
|
HINTS
|
|
${_OPENSSL3_ROOT_HINTS}
|
|
PATH_SUFFIXES
|
|
include
|
|
)
|
|
|
|
find_library(OPENSSL3_SSL_LIBRARY
|
|
NAMES
|
|
ssl_3_0
|
|
ssl
|
|
libssl.lib
|
|
ssleay32
|
|
ssleay32MD
|
|
HINTS
|
|
${OPENSSL3_ROOT_DIR}
|
|
PATH_SUFFIXES
|
|
lib
|
|
)
|
|
|
|
find_library(OPENSSL3_CRYPTO_LIBRARY
|
|
NAMES
|
|
crypto_3_0
|
|
crypto
|
|
libcrypto.lib
|
|
HINTS
|
|
${OPENSSL3_ROOT_DIR}
|
|
PATH_SUFFIXES
|
|
lib
|
|
)
|
|
|
|
mark_as_advanced(OPENSSL3_CRYPTO_LIBRARY OPENSSL3_SSL_LIBRARY)
|
|
|
|
|
|
set(OPENSSL3_SSL_LIBRARIES ${OPENSSL3_SSL_LIBRARY})
|
|
set(OPENSSL3_CRYPTO_LIBRARIES ${OPENSSL3_CRYPTO_LIBRARY})
|
|
set(OPENSSL3_LIBRARIES ${OPENSSL3_SSL_LIBRARIES} ${OPENSSL3_CRYPTO_LIBRARIES} )
|
|
_OpenSSL3_test_and_find_dependencies("${OPENSSL3_SSL_LIBRARY}" "${OPENSSL3_CRYPTO_LIBRARY}")
|
|
if(_OpenSSL3_has_dependencies)
|
|
_OpenSSL3_add_dependencies( OPENSSL3_SSL_LIBRARIES )
|
|
_OpenSSL3_add_dependencies( OPENSSL3_CRYPTO_LIBRARIES )
|
|
_OpenSSL3_add_dependencies( OPENSSL3_LIBRARIES )
|
|
endif()
|
|
|
|
function(from_hex HEX DEC)
|
|
string(TOUPPER "${HEX}" HEX)
|
|
set(_res 0)
|
|
string(LENGTH "${HEX}" _strlen)
|
|
|
|
while (_strlen GREATER 0)
|
|
math(EXPR _res "${_res} * 16")
|
|
string(SUBSTRING "${HEX}" 0 1 NIBBLE)
|
|
string(SUBSTRING "${HEX}" 1 -1 HEX)
|
|
if (NIBBLE STREQUAL "A")
|
|
math(EXPR _res "${_res} + 10")
|
|
elseif (NIBBLE STREQUAL "B")
|
|
math(EXPR _res "${_res} + 11")
|
|
elseif (NIBBLE STREQUAL "C")
|
|
math(EXPR _res "${_res} + 12")
|
|
elseif (NIBBLE STREQUAL "D")
|
|
math(EXPR _res "${_res} + 13")
|
|
elseif (NIBBLE STREQUAL "E")
|
|
math(EXPR _res "${_res} + 14")
|
|
elseif (NIBBLE STREQUAL "F")
|
|
math(EXPR _res "${_res} + 15")
|
|
else()
|
|
math(EXPR _res "${_res} + ${NIBBLE}")
|
|
endif()
|
|
|
|
string(LENGTH "${HEX}" _strlen)
|
|
endwhile()
|
|
|
|
set(${DEC} ${_res} PARENT_SCOPE)
|
|
endfunction()
|
|
|
|
if(OPENSSL3_INCLUDE_DIR AND EXISTS "${OPENSSL3_INCLUDE_DIR}/openssl/opensslv.h")
|
|
# Since OpenSSL 3.0.0, the new version format is MAJOR.MINOR.PATCH and
|
|
# a new OPENSSL3_VERSION_STR macro contains exactly that
|
|
file(STRINGS "${OPENSSL3_INCLUDE_DIR}/openssl/opensslv.h" OPENSSL3_VERSION_STR
|
|
REGEX "^#[\t ]*define[\t ]+OPENSSL_VERSION_STR[\t ]+\"([0-9])+\\.([0-9])+\\.([0-9])+\".*")
|
|
|
|
string(REGEX REPLACE "^.*OPENSSL_VERSION_STR[\t ]+\"([0-9]+\\.[0-9]+\\.[0-9]+)\".*$"
|
|
"\\1" OPENSSL3_VERSION_STR "${OPENSSL3_VERSION_STR}")
|
|
|
|
set(OPENSSL3_VERSION "${OPENSSL3_VERSION_STR}")
|
|
|
|
# Setting OPENSSL3_VERSION_MAJOR OPENSSL3_VERSION_MINOR and OPENSSL3_VERSION_FIX
|
|
|
|
string(REGEX MATCH "([0-9]+)\.([0-9]+)\.([0-9]+)" match "${OPENSSL3_VERSION}")
|
|
|
|
set(OPENSSL3_VERSION_MAJOR "${CMAKE_MATCH_1}")
|
|
set(OPENSSL3_VERSION_MINOR "${CMAKE_MATCH_2}")
|
|
set(OPENSSL3_VERSION_FIX "${CMAKE_MATCH_3}")
|
|
|
|
unset(OPENSSL3_VERSION_NUMBER)
|
|
unset(OPENSSL3_VERSION_STR)
|
|
endif ()
|
|
|
|
foreach(_comp IN LISTS OpenSSL3_FIND_COMPONENTS)
|
|
if(_comp STREQUAL "Crypto")
|
|
if(EXISTS "${OPENSSL3_INCLUDE_DIR}" AND
|
|
(EXISTS "${OPENSSL3_CRYPTO_LIBRARY}" OR
|
|
EXISTS "${LIB_EAY_LIBRARY_DEBUG}" OR
|
|
EXISTS "${LIB_EAY_LIBRARY_RELEASE}")
|
|
)
|
|
set(OpenSSL3_${_comp}_FOUND TRUE)
|
|
else()
|
|
set(OpenSSL3_${_comp}_FOUND FALSE)
|
|
endif()
|
|
elseif(_comp STREQUAL "SSL")
|
|
if(EXISTS "${OPENSSL3_INCLUDE_DIR}" AND
|
|
(EXISTS "${OPENSSL3_SSL_LIBRARY}" OR
|
|
EXISTS "${SSL_EAY_LIBRARY_DEBUG}" OR
|
|
EXISTS "${SSL_EAY_LIBRARY_RELEASE}")
|
|
)
|
|
set(OpenSSL3_${_comp}_FOUND TRUE)
|
|
else()
|
|
set(OpenSSL3_${_comp}_FOUND FALSE)
|
|
endif()
|
|
else()
|
|
message(WARNING "${_comp} is not a valid OpenSSL3 component")
|
|
set(OpenSSL3_${_comp}_FOUND FALSE)
|
|
endif()
|
|
endforeach()
|
|
unset(_comp)
|
|
|
|
include(FindPackageHandleStandardArgs)
|
|
find_package_handle_standard_args(OpenSSL3
|
|
REQUIRED_VARS
|
|
OPENSSL3_CRYPTO_LIBRARY
|
|
OPENSSL3_INCLUDE_DIR
|
|
VERSION_VAR
|
|
OPENSSL3_VERSION
|
|
HANDLE_COMPONENTS
|
|
FAIL_MESSAGE
|
|
"Could NOT find OpenSSL3. Try to set the path to OpenSSL3 root folder in the system variable OPENSSL3_ROOT_DIR"
|
|
)
|
|
|
|
mark_as_advanced(OPENSSL3_INCLUDE_DIR OPENSSL3_LIBRARIES)
|
|
|
|
if(OPENSSL3_FOUND)
|
|
if(NOT TARGET OpenSSL3::Crypto AND
|
|
(EXISTS "${OPENSSL3_CRYPTO_LIBRARY}" OR
|
|
EXISTS "${LIB_EAY_LIBRARY_DEBUG}" OR
|
|
EXISTS "${LIB_EAY_LIBRARY_RELEASE}")
|
|
)
|
|
add_library(OpenSSL3::Crypto UNKNOWN IMPORTED)
|
|
set_target_properties(OpenSSL3::Crypto PROPERTIES
|
|
INTERFACE_INCLUDE_DIRECTORIES "${OPENSSL3_INCLUDE_DIR}")
|
|
if(EXISTS "${OPENSSL3_CRYPTO_LIBRARY}")
|
|
set_target_properties(OpenSSL3::Crypto PROPERTIES
|
|
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
|
|
IMPORTED_LOCATION "${OPENSSL3_CRYPTO_LIBRARY}")
|
|
endif()
|
|
if(EXISTS "${LIB_EAY_LIBRARY_RELEASE}")
|
|
set_property(TARGET OpenSSL3::Crypto APPEND PROPERTY
|
|
IMPORTED_CONFIGURATIONS RELEASE)
|
|
set_target_properties(OpenSSL3::Crypto PROPERTIES
|
|
IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
|
|
IMPORTED_LOCATION_RELEASE "${LIB_EAY_LIBRARY_RELEASE}")
|
|
endif()
|
|
if(EXISTS "${LIB_EAY_LIBRARY_DEBUG}")
|
|
set_property(TARGET OpenSSL3::Crypto APPEND PROPERTY
|
|
IMPORTED_CONFIGURATIONS DEBUG)
|
|
set_target_properties(OpenSSL3::Crypto PROPERTIES
|
|
IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
|
|
IMPORTED_LOCATION_DEBUG "${LIB_EAY_LIBRARY_DEBUG}")
|
|
endif()
|
|
_OpenSSL3_target_add_dependencies(OpenSSL3::Crypto)
|
|
endif()
|
|
|
|
if(NOT TARGET OpenSSL3::SSL AND
|
|
(EXISTS "${OPENSSL3_SSL_LIBRARY}" OR
|
|
EXISTS "${SSL_EAY_LIBRARY_DEBUG}" OR
|
|
EXISTS "${SSL_EAY_LIBRARY_RELEASE}")
|
|
)
|
|
add_library(OpenSSL3::SSL UNKNOWN IMPORTED)
|
|
set_target_properties(OpenSSL3::SSL PROPERTIES
|
|
INTERFACE_INCLUDE_DIRECTORIES "${OPENSSL3_INCLUDE_DIR}")
|
|
if(EXISTS "${OPENSSL3_SSL_LIBRARY}")
|
|
set_target_properties(OpenSSL3::SSL PROPERTIES
|
|
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
|
|
IMPORTED_LOCATION "${OPENSSL3_SSL_LIBRARY}")
|
|
endif()
|
|
if(EXISTS "${SSL_EAY_LIBRARY_RELEASE}")
|
|
set_property(TARGET OpenSSL3::SSL APPEND PROPERTY
|
|
IMPORTED_CONFIGURATIONS RELEASE)
|
|
set_target_properties(OpenSSL3::SSL PROPERTIES
|
|
IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
|
|
IMPORTED_LOCATION_RELEASE "${SSL_EAY_LIBRARY_RELEASE}")
|
|
endif()
|
|
if(EXISTS "${SSL_EAY_LIBRARY_DEBUG}")
|
|
set_property(TARGET OpenSSL3::SSL APPEND PROPERTY
|
|
IMPORTED_CONFIGURATIONS DEBUG)
|
|
set_target_properties(OpenSSL3::SSL PROPERTIES
|
|
IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
|
|
IMPORTED_LOCATION_DEBUG "${SSL_EAY_LIBRARY_DEBUG}")
|
|
endif()
|
|
if(TARGET OpenSSL3::Crypto)
|
|
set_target_properties(OpenSSL3::SSL PROPERTIES
|
|
INTERFACE_LINK_LIBRARIES OpenSSL3::Crypto)
|
|
endif()
|
|
_OpenSSL3_target_add_dependencies(OpenSSL3::SSL)
|
|
endif()
|
|
endif()
|
|
|
|
# Restore the original find library ordering
|
|
if(OPENSSL3_USE_STATIC_LIBS)
|
|
set(CMAKE_FIND_LIBRARY_SUFFIXES ${_openssl3_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
|
|
endif()
|