You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

342 lines
10 KiB

  1. #.rst:
  2. # FindOpenSSL
  3. # -----------
  4. #
  5. # Try to find the OpenSSL encryption library
  6. #
  7. # Once done this will define
  8. #
  9. # ::
  10. #
  11. # OPENSSL_ROOT_DIR - Set this variable to the root installation of OpenSSL
  12. #
  13. #
  14. #
  15. # Read-Only variables:
  16. #
  17. # ::
  18. #
  19. # OPENSSL_FOUND - system has the OpenSSL library
  20. # OPENSSL_INCLUDE_DIR - the OpenSSL include directory
  21. # OPENSSL_LIBRARIES - The libraries needed to use OpenSSL
  22. # OPENSSL_VERSION - This is set to $major.$minor.$revision$path (eg. 0.9.8s)
  23. #=============================================================================
  24. # Copyright 2006-2009 Kitware, Inc.
  25. # Copyright 2006 Alexander Neundorf <neundorf@kde.org>
  26. # Copyright 2009-2011 Mathieu Malaterre <mathieu.malaterre@gmail.com>
  27. #
  28. # Distributed under the OSI-approved BSD License (the "License");
  29. # see accompanying file Copyright.txt for details.
  30. #
  31. # This software is distributed WITHOUT ANY WARRANTY; without even the
  32. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  33. # See the License for more information.
  34. #=============================================================================
  35. # (To distribute this file outside of CMake, substitute the full
  36. # License text for the above reference.)
  37. if (UNIX)
  38. find_package(PkgConfig QUIET)
  39. pkg_check_modules(_OPENSSL QUIET openssl)
  40. endif ()
  41. if (WIN32)
  42. # http://www.slproweb.com/products/Win32OpenSSL.html
  43. set(_OPENSSL_ROOT_HINTS
  44. ${OPENSSL_ROOT_DIR}
  45. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\OpenSSL (32-bit)_is1;Inno Setup: App Path]"
  46. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\OpenSSL (64-bit)_is1;Inno Setup: App Path]"
  47. ENV OPENSSL_ROOT_DIR
  48. )
  49. file(TO_CMAKE_PATH "$ENV{PROGRAMFILES}" _programfiles)
  50. set(_OPENSSL_ROOT_PATHS
  51. "${_programfiles}/OpenSSL"
  52. "${_programfiles}/OpenSSL-Win32"
  53. "${_programfiles}/OpenSSL-Win64"
  54. "C:/OpenSSL/"
  55. "C:/OpenSSL-Win32/"
  56. "C:/OpenSSL-Win64/"
  57. )
  58. unset(_programfiles)
  59. else ()
  60. set(_OPENSSL_ROOT_HINTS
  61. ${OPENSSL_ROOT_DIR}
  62. ENV OPENSSL_ROOT_DIR
  63. )
  64. endif ()
  65. set(_OPENSSL_ROOT_HINTS_AND_PATHS
  66. HINTS ${_OPENSSL_ROOT_HINTS}
  67. PATHS ${_OPENSSL_ROOT_PATHS}
  68. )
  69. find_path(OPENSSL_INCLUDE_DIR
  70. NAMES
  71. openssl/ssl.h
  72. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  73. HINTS
  74. ${_OPENSSL_INCLUDEDIR}
  75. PATH_SUFFIXES
  76. include
  77. )
  78. if(WIN32 AND NOT CYGWIN)
  79. if(MSVC)
  80. # /MD and /MDd are the standard values - if someone wants to use
  81. # others, the libnames have to change here too
  82. # use also ssl and ssleay32 in debug as fallback for openssl < 0.9.8b
  83. # TODO: handle /MT and static lib
  84. # In Visual C++ naming convention each of these four kinds of Windows libraries has it's standard suffix:
  85. # * MD for dynamic-release
  86. # * MDd for dynamic-debug
  87. # * MT for static-release
  88. # * MTd for static-debug
  89. # Implementation details:
  90. # We are using the libraries located in the VC subdir instead of the parent directory eventhough :
  91. # libeay32MD.lib is identical to ../libeay32.lib, and
  92. # ssleay32MD.lib is identical to ../ssleay32.lib
  93. find_library(LIB_EAY_DEBUG
  94. NAMES
  95. libeay32MDd
  96. libeay32d
  97. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  98. PATH_SUFFIXES
  99. "lib"
  100. "VC"
  101. "lib/VC"
  102. )
  103. find_library(LIB_EAY_RELEASE
  104. NAMES
  105. libeay32MD
  106. libeay32
  107. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  108. PATH_SUFFIXES
  109. "lib"
  110. "VC"
  111. "lib/VC"
  112. )
  113. find_library(SSL_EAY_DEBUG
  114. NAMES
  115. ssleay32MDd
  116. ssleay32d
  117. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  118. PATH_SUFFIXES
  119. "lib"
  120. "VC"
  121. "lib/VC"
  122. )
  123. find_library(SSL_EAY_RELEASE
  124. NAMES
  125. ssleay32MD
  126. ssleay32
  127. ssl
  128. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  129. PATH_SUFFIXES
  130. "lib"
  131. "VC"
  132. "lib/VC"
  133. )
  134. set(LIB_EAY_LIBRARY_DEBUG "${LIB_EAY_DEBUG}")
  135. set(LIB_EAY_LIBRARY_RELEASE "${LIB_EAY_RELEASE}")
  136. set(SSL_EAY_LIBRARY_DEBUG "${SSL_EAY_DEBUG}")
  137. set(SSL_EAY_LIBRARY_RELEASE "${SSL_EAY_RELEASE}")
  138. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  139. select_library_configurations(LIB_EAY)
  140. select_library_configurations(SSL_EAY)
  141. mark_as_advanced(LIB_EAY_LIBRARY_DEBUG LIB_EAY_LIBRARY_RELEASE
  142. SSL_EAY_LIBRARY_DEBUG SSL_EAY_LIBRARY_RELEASE)
  143. set( OPENSSL_LIBRARIES ${SSL_EAY_LIBRARY} ${LIB_EAY_LIBRARY} )
  144. elseif(MINGW)
  145. message( STATUS "Searching for OpenSSL in MinGW." )
  146. # same player, for MinGW
  147. set(LIB_EAY_NAMES libeay32)
  148. set(SSL_EAY_NAMES ssleay32)
  149. list(APPEND LIB_EAY_NAMES crypto)
  150. list(APPEND SSL_EAY_NAMES ssl)
  151. find_library(LIB_EAY
  152. NAMES
  153. ${LIB_EAY_NAMES}
  154. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  155. PATH_SUFFIXES
  156. "lib"
  157. "lib/MinGW"
  158. # Do not search system path. Otherwise the DLL will be found rather than the link library.
  159. NO_SYSTEM_ENVIRONMENT_PATH
  160. NO_CMAKE_SYSTEM_PATH
  161. )
  162. find_library(SSL_EAY
  163. NAMES
  164. ${SSL_EAY_NAMES}
  165. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  166. PATH_SUFFIXES
  167. "lib"
  168. "lib/MinGW"
  169. # Do not search system path. Otherwise the DLL will be found rather than the link library.
  170. NO_SYSTEM_ENVIRONMENT_PATH
  171. NO_CMAKE_SYSTEM_PATH
  172. )
  173. mark_as_advanced(SSL_EAY LIB_EAY)
  174. set( OPENSSL_LIBRARIES ${SSL_EAY} ${LIB_EAY} )
  175. unset(LIB_EAY_NAMES)
  176. unset(SSL_EAY_NAMES)
  177. else()
  178. # Not sure what to pick for -say- intel, let's use the toplevel ones and hope someone report issues:
  179. find_library(LIB_EAY
  180. NAMES
  181. libeay32
  182. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  183. HINTS
  184. ${_OPENSSL_LIBDIR}
  185. PATH_SUFFIXES
  186. lib
  187. )
  188. find_library(SSL_EAY
  189. NAMES
  190. ssleay32
  191. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  192. HINTS
  193. ${_OPENSSL_LIBDIR}
  194. PATH_SUFFIXES
  195. lib
  196. )
  197. mark_as_advanced(SSL_EAY LIB_EAY)
  198. set( OPENSSL_LIBRARIES ${SSL_EAY} ${LIB_EAY} )
  199. endif()
  200. else()
  201. find_library(OPENSSL_SSL_LIBRARY
  202. NAMES
  203. ssl
  204. ssleay32
  205. ssleay32MD
  206. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  207. HINTS
  208. ${_OPENSSL_LIBDIR}
  209. PATH_SUFFIXES
  210. lib
  211. )
  212. find_library(OPENSSL_CRYPTO_LIBRARY
  213. NAMES
  214. crypto
  215. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  216. HINTS
  217. ${_OPENSSL_LIBDIR}
  218. PATH_SUFFIXES
  219. lib
  220. )
  221. mark_as_advanced(OPENSSL_CRYPTO_LIBRARY OPENSSL_SSL_LIBRARY)
  222. # compat defines
  223. set(OPENSSL_SSL_LIBRARIES ${OPENSSL_SSL_LIBRARY})
  224. set(OPENSSL_CRYPTO_LIBRARIES ${OPENSSL_CRYPTO_LIBRARY})
  225. set(OPENSSL_LIBRARIES ${OPENSSL_SSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY})
  226. endif()
  227. function(from_hex HEX DEC)
  228. string(TOUPPER "${HEX}" HEX)
  229. set(_res 0)
  230. string(LENGTH "${HEX}" _strlen)
  231. while (_strlen GREATER 0)
  232. math(EXPR _res "${_res} * 16")
  233. string(SUBSTRING "${HEX}" 0 1 NIBBLE)
  234. string(SUBSTRING "${HEX}" 1 -1 HEX)
  235. if (NIBBLE STREQUAL "A")
  236. math(EXPR _res "${_res} + 10")
  237. elseif (NIBBLE STREQUAL "B")
  238. math(EXPR _res "${_res} + 11")
  239. elseif (NIBBLE STREQUAL "C")
  240. math(EXPR _res "${_res} + 12")
  241. elseif (NIBBLE STREQUAL "D")
  242. math(EXPR _res "${_res} + 13")
  243. elseif (NIBBLE STREQUAL "E")
  244. math(EXPR _res "${_res} + 14")
  245. elseif (NIBBLE STREQUAL "F")
  246. math(EXPR _res "${_res} + 15")
  247. else()
  248. math(EXPR _res "${_res} + ${NIBBLE}")
  249. endif()
  250. string(LENGTH "${HEX}" _strlen)
  251. endwhile()
  252. set(${DEC} ${_res} PARENT_SCOPE)
  253. endfunction()
  254. if (OPENSSL_INCLUDE_DIR)
  255. if (_OPENSSL_VERSION)
  256. set(OPENSSL_VERSION "${_OPENSSL_VERSION}")
  257. elseif(OPENSSL_INCLUDE_DIR AND EXISTS "${OPENSSL_INCLUDE_DIR}/openssl/opensslv.h")
  258. file(STRINGS "${OPENSSL_INCLUDE_DIR}/openssl/opensslv.h" openssl_version_str
  259. REGEX "^#[\t ]*define[\t ]+OPENSSL_VERSION_NUMBER[\t ]+0x([0-9a-fA-F])+.*")
  260. # The version number is encoded as 0xMNNFFPPS: major minor fix patch status
  261. # The status gives if this is a developer or prerelease and is ignored here.
  262. # Major, minor, and fix directly translate into the version numbers shown in
  263. # the string. The patch field translates to the single character suffix that
  264. # indicates the bug fix state, which 00 -> nothing, 01 -> a, 02 -> b and so
  265. # on.
  266. message(STATUS "OPENSSL_VERSION_STR=${openssl_version_str}")
  267. 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]).*$"
  268. "\\1;\\2;\\3;\\4;\\5" OPENSSL_VERSION_LIST "${openssl_version_str}")
  269. list(GET OPENSSL_VERSION_LIST 0 OPENSSL_VERSION_MAJOR)
  270. list(GET OPENSSL_VERSION_LIST 1 OPENSSL_VERSION_MINOR)
  271. from_hex("${OPENSSL_VERSION_MINOR}" OPENSSL_VERSION_MINOR)
  272. list(GET OPENSSL_VERSION_LIST 2 OPENSSL_VERSION_FIX)
  273. from_hex("${OPENSSL_VERSION_FIX}" OPENSSL_VERSION_FIX)
  274. list(GET OPENSSL_VERSION_LIST 3 OPENSSL_VERSION_PATCH)
  275. if (NOT OPENSSL_VERSION_PATCH STREQUAL "00")
  276. from_hex("${OPENSSL_VERSION_PATCH}" _tmp)
  277. # 96 is the ASCII code of 'a' minus 1
  278. math(EXPR OPENSSL_VERSION_PATCH_ASCII "${_tmp} + 96")
  279. unset(_tmp)
  280. # Once anyone knows how OpenSSL would call the patch versions beyond 'z'
  281. # this should be updated to handle that, too. This has not happened yet
  282. # so it is simply ignored here for now.
  283. string(ASCII "${OPENSSL_VERSION_PATCH_ASCII}" OPENSSL_VERSION_PATCH_STRING)
  284. endif ()
  285. set(OPENSSL_VERSION "${OPENSSL_VERSION_MAJOR}.${OPENSSL_VERSION_MINOR}.${OPENSSL_VERSION_FIX}${OPENSSL_VERSION_PATCH_STRING}")
  286. endif ()
  287. endif ()
  288. include(FindPackageHandleStandardArgs)
  289. if (OPENSSL_VERSION)
  290. find_package_handle_standard_args(OpenSSL
  291. REQUIRED_VARS
  292. OPENSSL_LIBRARIES
  293. OPENSSL_INCLUDE_DIR
  294. VERSION_VAR
  295. OPENSSL_VERSION
  296. FAIL_MESSAGE
  297. "Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR"
  298. )
  299. else ()
  300. 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"
  301. OPENSSL_LIBRARIES
  302. OPENSSL_INCLUDE_DIR
  303. )
  304. endif ()
  305. mark_as_advanced(OPENSSL_INCLUDE_DIR OPENSSL_LIBRARIES)