1 # - Try to find the OpenSSL encryption library
2 # Once done this will define
4 # OPENSSL_ROOT_DIR - Set this variable to the root installation of OpenSSL
7 # OPENSSL_FOUND - system has the OpenSSL library
8 # OPENSSL_INCLUDE_DIR - the OpenSSL include directory
9 # OPENSSL_LIBRARIES - The libraries needed to use OpenSSL
10 # OPENSSL_VERSION - This is set to $major.$minor.$revision$path (eg. 0.9.8s)
12 #=============================================================================
13 # Copyright 2006-2009 Kitware, Inc.
14 # Copyright 2006 Alexander Neundorf <neundorf@kde.org>
15 # Copyright 2009-2011 Mathieu Malaterre <mathieu.malaterre@gmail.com>
17 # Distributed under the OSI-approved BSD License (the "License");
18 # see accompanying file Copyright.txt for details.
20 # This software is distributed WITHOUT ANY WARRANTY; without even the
21 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 # See the License for more information.
23 #=============================================================================
24 # (To distribute this file outside of CMake, substitute the full
25 # License text for the above reference.)
27 if (UNIX AND NOT ANDROID)
28 find_package(PkgConfig QUIET)
29 pkg_check_modules(_OPENSSL QUIET openssl)
30 endif (UNIX AND NOT ANDROID)
32 # http://www.slproweb.com/products/Win32OpenSSL.html
33 SET(_OPENSSL_ROOT_HINTS
34 $ENV{OPENSSL_ROOT_DIR}
36 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\OpenSSL (32-bit)_is1;Inno Setup: App Path]"
37 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\OpenSSL (64-bit)_is1;Inno Setup: App Path]"
39 SET(_OPENSSL_ROOT_PATHS
40 "$ENV{PROGRAMFILES}/OpenSSL"
41 "$ENV{PROGRAMFILES}/OpenSSL-Win32"
42 "$ENV{PROGRAMFILES}/OpenSSL-Win64"
47 "/obj/local/armeabi-v7a/"
49 SET(_OPENSSL_ROOT_HINTS_AND_PATHS
50 HINTS ${_OPENSSL_ROOT_HINTS}
51 PATHS ${_OPENSSL_ROOT_PATHS}
54 FIND_PATH(OPENSSL_INCLUDE_DIR
60 ${_OPENSSL_INCLUDEDIR}
61 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
67 if(${MSVC_RUNTIME} STREQUAL "static")
68 set(MSVC_RUNTIME_SUFFIX "MT")
70 set(MSVC_RUNTIME_SUFFIX "MD")
75 FIND_LIBRARY(SSL_LIBRARY
78 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
83 FIND_LIBRARY(CRYPTO_LIBRARY
86 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
91 list(APPEND OPENSSL_LIBRARIES ${CRYPTO_LIBRARY} ${SSL_LIBRARY})
92 ELSEIF(WIN32 AND NOT CYGWIN)
93 # MINGW should go here too
95 # /MD and /MDd are the standard values - if someone wants to use
96 # others, the libnames have to change here too
97 # use also ssl and ssleay32 in debug as fallback for openssl < 0.9.8b
98 # TODO: handle /MT and static lib
99 # In Visual C++ naming convention each of these four kinds of Windows libraries has it's standard suffix:
100 # * MD for dynamic-release
101 # * MDd for dynamic-debug
102 # * MT for static-release
103 # * MTd for static-debug
105 # Implementation details:
106 # We are using the libraries located in the VC subdir instead of the parent directory even though :
107 # libeay32MD.lib is identical to ../libeay32.lib, and
108 # ssleay32MD.lib is identical to ../ssleay32.lib
110 if(DEFINED OPENSSL_STATIC)
111 set(MSVC_RUNTIME_PATH_SUFFIX "lib/VC/static")
113 set(MSVC_RUNTIME_PATH_SUFFIX "")
116 FIND_LIBRARY(LIB_EAY_DEBUG
118 "libeay32${MSVC_RUNTIME_SUFFIX}d"
122 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
124 ${MSVC_RUNTIME_PATH_SUFFIX}
130 FIND_LIBRARY(LIB_EAY_RELEASE
132 "libeay32${MSVC_RUNTIME_SUFFIX}"
136 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
138 ${MSVC_RUNTIME_PATH_SUFFIX}
144 FIND_LIBRARY(SSL_EAY_DEBUG
146 "ssleay32${MSVC_RUNTIME_SUFFIX}d"
151 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
153 ${MSVC_RUNTIME_PATH_SUFFIX}
159 FIND_LIBRARY(SSL_EAY_RELEASE
161 "ssleay32${MSVC_RUNTIME_SUFFIX}"
166 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
168 ${MSVC_RUNTIME_PATH_SUFFIX}
174 set( OPENSSL_DEBUG_LIBRARIES ${SSL_EAY_DEBUG} ${LIB_EAY_DEBUG} )
175 set( OPENSSL_RELEASE_LIBRARIES ${SSL_EAY_RELEASE} ${LIB_EAY_RELEASE} )
176 set( OPENSSL_LIBRARIES ${OPENSSL_RELEASE_LIBRARIES} )
178 MARK_AS_ADVANCED(SSL_EAY_DEBUG SSL_EAY_RELEASE)
179 MARK_AS_ADVANCED(LIB_EAY_DEBUG LIB_EAY_RELEASE)
181 # same player, for MingW
186 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
196 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
202 MARK_AS_ADVANCED(SSL_EAY LIB_EAY)
203 set( OPENSSL_LIBRARIES ${SSL_EAY} ${LIB_EAY} )
205 # Not sure what to pick for -say- intel, let's use the toplevel ones and hope someone report issues:
213 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
225 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
230 MARK_AS_ADVANCED(SSL_EAY LIB_EAY)
231 set( OPENSSL_LIBRARIES ${SSL_EAY} ${LIB_EAY} )
233 ELSE(WIN32 AND NOT CYGWIN)
235 FIND_LIBRARY(OPENSSL_SSL_LIBRARY
240 "ssleay32${MSVC_RUNTIME_SUFFIX}"
243 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
248 FIND_LIBRARY(OPENSSL_CRYPTO_LIBRARY
254 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
259 MARK_AS_ADVANCED(OPENSSL_CRYPTO_LIBRARY OPENSSL_SSL_LIBRARY)
262 SET(OPENSSL_SSL_LIBRARIES ${OPENSSL_SSL_LIBRARY})
263 SET(OPENSSL_CRYPTO_LIBRARIES ${OPENSSL_CRYPTO_LIBRARY})
265 SET(OPENSSL_LIBRARIES ${OPENSSL_SSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY})
269 function(from_hex HEX DEC)
270 string(TOUPPER "${HEX}" HEX)
272 string(LENGTH "${HEX}" _strlen)
274 while (_strlen GREATER 0)
275 math(EXPR _res "${_res} * 16")
276 string(SUBSTRING "${HEX}" 0 1 NIBBLE)
277 string(SUBSTRING "${HEX}" 1 -1 HEX)
278 if (NIBBLE STREQUAL "A")
279 math(EXPR _res "${_res} + 10")
280 elseif (NIBBLE STREQUAL "B")
281 math(EXPR _res "${_res} + 11")
282 elseif (NIBBLE STREQUAL "C")
283 math(EXPR _res "${_res} + 12")
284 elseif (NIBBLE STREQUAL "D")
285 math(EXPR _res "${_res} + 13")
286 elseif (NIBBLE STREQUAL "E")
287 math(EXPR _res "${_res} + 14")
288 elseif (NIBBLE STREQUAL "F")
289 math(EXPR _res "${_res} + 15")
291 math(EXPR _res "${_res} + ${NIBBLE}")
294 string(LENGTH "${HEX}" _strlen)
297 set(${DEC} ${_res} PARENT_SCOPE)
298 endfunction(from_hex)
300 if (OPENSSL_INCLUDE_DIR)
301 if (_OPENSSL_VERSION)
302 set(OPENSSL_VERSION "${_OPENSSL_VERSION}")
303 elseif(OPENSSL_INCLUDE_DIR AND EXISTS "${OPENSSL_INCLUDE_DIR}/openssl/opensslv.h")
304 file(STRINGS "${OPENSSL_INCLUDE_DIR}/openssl/opensslv.h" openssl_version_str
305 REGEX "^#.?define[\t ]+OPENSSL_VERSION_NUMBER[\t ]+0x([0-9a-fA-F])+.*")
307 # The version number is encoded as 0xMNNFFPPS: major minor fix patch status
308 # The status gives if this is a developer or prerelease and is ignored here.
309 # Major, minor, and fix directly translate into the version numbers shown in
310 # the string. The patch field translates to the single character suffix that
311 # indicates the bug fix state, which 00 -> nothing, 01 -> a, 02 -> b and so
314 string(REGEX REPLACE "^.*OPENSSL_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]).*$"
315 "\\1;\\2;\\3;\\4;\\5" OPENSSL_VERSION_LIST "${openssl_version_str}")
316 list(GET OPENSSL_VERSION_LIST 0 OPENSSL_VERSION_MAJOR)
317 list(GET OPENSSL_VERSION_LIST 1 OPENSSL_VERSION_MINOR)
318 from_hex("${OPENSSL_VERSION_MINOR}" OPENSSL_VERSION_MINOR)
319 list(GET OPENSSL_VERSION_LIST 2 OPENSSL_VERSION_FIX)
320 from_hex("${OPENSSL_VERSION_FIX}" OPENSSL_VERSION_FIX)
321 list(GET OPENSSL_VERSION_LIST 3 OPENSSL_VERSION_PATCH)
323 if (NOT OPENSSL_VERSION_PATCH STREQUAL "00")
324 from_hex("${OPENSSL_VERSION_PATCH}" _tmp)
325 # 96 is the ASCII code of 'a' minus 1
326 math(EXPR OPENSSL_VERSION_PATCH_ASCII "${_tmp} + 96")
328 # Once anyone knows how OpenSSL would call the patch versions beyond 'z'
329 # this should be updated to handle that, too. This has not happened yet
330 # so it is simply ignored here for now.
331 string(ASCII "${OPENSSL_VERSION_PATCH_ASCII}" OPENSSL_VERSION_PATCH_STRING)
332 endif (NOT OPENSSL_VERSION_PATCH STREQUAL "00")
334 set(OPENSSL_VERSION "${OPENSSL_VERSION_MAJOR}.${OPENSSL_VERSION_MINOR}.${OPENSSL_VERSION_FIX}${OPENSSL_VERSION_PATCH_STRING}")
335 endif (_OPENSSL_VERSION)
336 endif (OPENSSL_INCLUDE_DIR)
338 include(FindPackageHandleStandardArgs)
341 find_package_handle_standard_args(OpenSSL
348 "Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR"
350 else (OPENSSL_VERSION)
351 find_package_handle_standard_args(OpenSSL "Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR"
355 endif (OPENSSL_VERSION)
357 MARK_AS_ADVANCED(OPENSSL_INCLUDE_DIR OPENSSL_LIBRARIES OPENSSL_DEBUG_LIBRARIES OPENSSL_RELEASE_LIBRARIES)