From f01f72f913c578fe2b1ece955f3f257853c2e1d6 Mon Sep 17 00:00:00 2001 From: kakwa Date: Fri, 21 Apr 2017 08:52:25 +0200 Subject: [PATCH] adding small cmake module to detect LibreSSL and using it in CMakeLists.txt --- CMakeLists.txt | 14 ++++++---- cmake/FindLibreSSL.cmake | 56 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 cmake/FindLibreSSL.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index ee6e8d4..1a0caa3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,12 +55,16 @@ MESSAGE(STATUS "OpenSSL libraries: ${OPENSSL_LIBRARIES}") MESSAGE(STATUS "OpenSSL version: ${OPENSSL_VERSION}") MESSAGE(STATUS "OS Detected: ${CMAKE_SYSTEM_NAME}") + +find_package(LibreSSL) +MESSAGE(STATUS "LibreSSL Detected: ${IS_LIBRESSL}") + if(NOT(DEFINED OPENSSL_API_1_1)) -if(${OPENSSL_VERSION} VERSION_GREATER 1.0.99) - set(OPENSSL_API_1_1 ON) -else() - set(OPENSSL_API_1_1 OFF) -endif() + if(${OPENSSL_VERSION} VERSION_GREATER 1.0.99 AND NOT(${IS_LIBRESSL})) + set(OPENSSL_API_1_1 ON) + else() + set(OPENSSL_API_1_1 OFF) + endif() endif() diff --git a/cmake/FindLibreSSL.cmake b/cmake/FindLibreSSL.cmake new file mode 100644 index 0000000..8e14799 --- /dev/null +++ b/cmake/FindLibreSSL.cmake @@ -0,0 +1,56 @@ +#.rst +# FindLibreSSL +# ------------ +# +# Detect if OpenSSL is in fact LibreSSL, and recovers libressl version. +# +# Requires running FindOpenSSL previously +# +# Result Variables +# ^^^^^^^^^^^^^^^^ +# +# ``LIBRESSL_VERSION`` +# This is set to ``$major.$minor.$revision$patch`` (e.g. ``2.3.1f``). +# +# ``IS_LIBRESSL`` +# Boolean, set to true if LibreSSL +# + +if(OPENSSL_INCLUDE_DIR AND EXISTS "${OPENSSL_INCLUDE_DIR}/openssl/opensslv.h") + file(STRINGS "${OPENSSL_INCLUDE_DIR}/openssl/opensslv.h" libressl_version_str + REGEX "^#[\t ]*define[\t ]+LIBRESSL_VERSION_NUMBER[\t ]+0x([0-9a-fA-F])+.*") + + if(libressl_version_str) + # The version number is encoded as 0xMNNFFPPS: major minor fix patch status + # The status gives if this is a developer or prerelease and is ignored here. + # Major, minor, and fix directly translate into the version numbers shown in + # the string. The patch field translates to the single character suffix that + # indicates the bug fix state, which 00 -> nothing, 01 -> a, 02 -> b and so + # on. + + string(REGEX REPLACE "^.*LIBRESSL_VERSION_NUMBER[\t ]+0x([0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F]).*$" + "\\1;\\2;\\3;\\4;\\5" LIBRESSL_VERSION_LIST "${libressl_version_str}") + list(GET LIBRESSL_VERSION_LIST 0 LIBRESSL_VERSION_MAJOR) + list(GET LIBRESSL_VERSION_LIST 1 LIBRESSL_VERSION_MINOR) + from_hex("${LIBRESSL_VERSION_MINOR}" LIBRESSL_VERSION_MINOR) + list(GET LIBRESSL_VERSION_LIST 2 LIBRESSL_VERSION_FIX) + from_hex("${LIBRESSL_VERSION_FIX}" LIBRESSL_VERSION_FIX) + list(GET LIBRESSL_VERSION_LIST 3 LIBRESSL_VERSION_PATCH) + + if (NOT LIBRESSL_VERSION_PATCH STREQUAL "00") + from_hex("${LIBRESSL_VERSION_PATCH}" _tmp) + # 96 is the ASCII code of 'a' minus 1 + math(EXPR LIBRESSL_VERSION_PATCH_ASCII "${_tmp} + 96") + unset(_tmp) + # Once anyone knows how OpenSSL would call the patch versions beyond 'z' + # this should be updated to handle that, too. This has not happened yet + # so it is simply ignored here for now. + string(ASCII "${LIBRESSL_VERSION_PATCH_ASCII}" LIBRESSL_VERSION_PATCH_STRING) + endif () + + set(LIBRESSL_VERSION "${LIBRESSL_VERSION_MAJOR}.${LIBRESSL_VERSION_MINOR}.${LIBRESSL_VERSION_FIX}${LIBRESSL_VERSION_PATCH_STRING}") + set(IS_LIBRESSL ON) + else () + set(IS_LIBRESSL OFF) + endif () +endif ()