FindOpenSSL.cmake 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindOpenSSL
  5. -----------
  6. Find the OpenSSL encryption library.
  7. This module finds an installed OpenSSL library and determines its version.
  8. .. versionadded:: 3.19
  9. When a version is requested, it can be specified as a simple value or as a
  10. range. For a detailed description of version range usage and capabilities,
  11. refer to the :command:`find_package` command.
  12. .. versionadded:: 3.18
  13. Support for OpenSSL 3.0.
  14. Optional COMPONENTS
  15. ^^^^^^^^^^^^^^^^^^^
  16. .. versionadded:: 3.12
  17. This module supports two optional COMPONENTS: ``Crypto`` and ``SSL``. Both
  18. components have associated imported targets, as described below.
  19. Imported Targets
  20. ^^^^^^^^^^^^^^^^
  21. .. versionadded:: 3.4
  22. This module defines the following :prop_tgt:`IMPORTED` targets:
  23. ``OpenSSL::SSL``
  24. The OpenSSL ``ssl`` library, if found.
  25. ``OpenSSL::Crypto``
  26. The OpenSSL ``crypto`` library, if found.
  27. ``OpenSSL::applink``
  28. .. versionadded:: 3.18
  29. The OpenSSL ``applink`` components that might be need to be compiled into
  30. projects under MSVC. This target is available only if found OpenSSL version
  31. is not less than 0.9.8. By linking this target the above OpenSSL targets can
  32. be linked even if the project has different MSVC runtime configurations with
  33. the above OpenSSL targets. This target has no effect on platforms other than
  34. MSVC.
  35. NOTE: Due to how ``INTERFACE_SOURCES`` are consumed by the consuming target,
  36. unless you certainly know what you are doing, it is always preferred to link
  37. ``OpenSSL::applink`` target as ``PRIVATE`` and to make sure that this target is
  38. linked at most once for the whole dependency graph of any library or
  39. executable:
  40. .. code-block:: cmake
  41. target_link_libraries(myTarget PRIVATE OpenSSL::applink)
  42. Otherwise you would probably encounter unexpected random problems when building
  43. and linking, as both the ISO C and the ISO C++ standard claims almost nothing
  44. about what a link process should be.
  45. Result Variables
  46. ^^^^^^^^^^^^^^^^
  47. This module will set the following variables in your project:
  48. ``OPENSSL_FOUND``
  49. System has the OpenSSL library. If no components are requested it only
  50. requires the crypto library.
  51. ``OPENSSL_INCLUDE_DIR``
  52. The OpenSSL include directory.
  53. ``OPENSSL_CRYPTO_LIBRARY``
  54. The OpenSSL crypto library.
  55. ``OPENSSL_CRYPTO_LIBRARIES``
  56. The OpenSSL crypto library and its dependencies.
  57. ``OPENSSL_SSL_LIBRARY``
  58. The OpenSSL SSL library.
  59. ``OPENSSL_SSL_LIBRARIES``
  60. The OpenSSL SSL library and its dependencies.
  61. ``OPENSSL_LIBRARIES``
  62. All OpenSSL libraries and their dependencies.
  63. ``OPENSSL_VERSION``
  64. This is set to ``$major.$minor.$revision$patch`` (e.g. ``0.9.8s``).
  65. ``OPENSSL_APPLINK_SOURCE``
  66. The sources in the target ``OpenSSL::applink`` that is mentioned above. This
  67. variable shall always be undefined if found openssl version is less than
  68. 0.9.8 or if platform is not MSVC.
  69. Hints
  70. ^^^^^
  71. Set ``OPENSSL_ROOT_DIR`` to the root directory of an OpenSSL installation.
  72. .. versionadded:: 3.4
  73. Set ``OPENSSL_USE_STATIC_LIBS`` to ``TRUE`` to look for static libraries.
  74. .. versionadded:: 3.5
  75. Set ``OPENSSL_MSVC_STATIC_RT`` set ``TRUE`` to choose the MT version of the lib.
  76. #]=======================================================================]
  77. macro(_OpenSSL_test_and_find_dependencies ssl_library crypto_library)
  78. if((CMAKE_SYSTEM_NAME STREQUAL "Linux") AND
  79. (("${ssl_library}" MATCHES "\\${CMAKE_STATIC_LIBRARY_SUFFIX}$") OR
  80. ("${crypto_library}" MATCHES "\\${CMAKE_STATIC_LIBRARY_SUFFIX}$")))
  81. set(_OpenSSL_has_dependencies TRUE)
  82. find_package(Threads)
  83. else()
  84. set(_OpenSSL_has_dependencies FALSE)
  85. endif()
  86. endmacro()
  87. function(_OpenSSL_add_dependencies libraries_var)
  88. if(CMAKE_THREAD_LIBS_INIT)
  89. list(APPEND ${libraries_var} ${CMAKE_THREAD_LIBS_INIT})
  90. endif()
  91. list(APPEND ${libraries_var} ${CMAKE_DL_LIBS})
  92. set(${libraries_var} ${${libraries_var}} PARENT_SCOPE)
  93. endfunction()
  94. function(_OpenSSL_target_add_dependencies target)
  95. if(_OpenSSL_has_dependencies)
  96. set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES Threads::Threads )
  97. set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${CMAKE_DL_LIBS} )
  98. endif()
  99. if(WIN32 AND OPENSSL_USE_STATIC_LIBS)
  100. if(WINCE)
  101. set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ws2 )
  102. else()
  103. set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ws2_32 )
  104. endif()
  105. set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES crypt32 )
  106. endif()
  107. endfunction()
  108. if (UNIX)
  109. find_package(PkgConfig QUIET)
  110. pkg_check_modules(_OPENSSL QUIET openssl)
  111. endif ()
  112. # Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES
  113. if(OPENSSL_USE_STATIC_LIBS)
  114. set(_openssl_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  115. if(WIN32)
  116. set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
  117. else()
  118. set(CMAKE_FIND_LIBRARY_SUFFIXES .a )
  119. endif()
  120. endif()
  121. if (WIN32)
  122. # http://www.slproweb.com/products/Win32OpenSSL.html
  123. set(_OPENSSL_ROOT_HINTS
  124. ${OPENSSL_ROOT_DIR}
  125. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\OpenSSL (32-bit)_is1;Inno Setup: App Path]"
  126. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\OpenSSL (64-bit)_is1;Inno Setup: App Path]"
  127. ENV OPENSSL_ROOT_DIR
  128. )
  129. if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "8")
  130. set(_arch "Win64")
  131. file(TO_CMAKE_PATH "$ENV{PROGRAMFILES}" _programfiles)
  132. else()
  133. set(_arch "Win32")
  134. set(_progfiles_x86 "ProgramFiles(x86)")
  135. if(NOT "$ENV{${_progfiles_x86}}" STREQUAL "")
  136. # under windows 64 bit machine
  137. file(TO_CMAKE_PATH "$ENV{${_progfiles_x86}}" _programfiles)
  138. else()
  139. # under windows 32 bit machine
  140. file(TO_CMAKE_PATH "$ENV{ProgramFiles}" _programfiles)
  141. endif()
  142. endif()
  143. set(_OPENSSL_ROOT_PATHS
  144. "${_programfiles}/OpenSSL"
  145. "${_programfiles}/OpenSSL-${_arch}"
  146. "C:/OpenSSL/"
  147. "C:/OpenSSL-${_arch}/"
  148. )
  149. unset(_programfiles)
  150. unset(_arch)
  151. else ()
  152. set(_OPENSSL_ROOT_HINTS
  153. ${OPENSSL_ROOT_DIR}
  154. ENV OPENSSL_ROOT_DIR
  155. )
  156. endif ()
  157. set(_OPENSSL_ROOT_HINTS_AND_PATHS
  158. HINTS ${_OPENSSL_ROOT_HINTS}
  159. PATHS ${_OPENSSL_ROOT_PATHS}
  160. )
  161. find_path(OPENSSL_INCLUDE_DIR
  162. NAMES
  163. openssl/ssl.h
  164. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  165. HINTS
  166. ${_OPENSSL_INCLUDEDIR}
  167. ${_OPENSSL_INCLUDE_DIRS}
  168. PATH_SUFFIXES
  169. include
  170. )
  171. if(WIN32 AND NOT CYGWIN)
  172. if(MSVC)
  173. # /MD and /MDd are the standard values - if someone wants to use
  174. # others, the libnames have to change here too
  175. # use also ssl and ssleay32 in debug as fallback for openssl < 0.9.8b
  176. # enable OPENSSL_MSVC_STATIC_RT to get the libs build /MT (Multithreaded no-DLL)
  177. # In Visual C++ naming convention each of these four kinds of Windows libraries has it's standard suffix:
  178. # * MD for dynamic-release
  179. # * MDd for dynamic-debug
  180. # * MT for static-release
  181. # * MTd for static-debug
  182. # Implementation details:
  183. # We are using the libraries located in the VC subdir instead of the parent directory even though :
  184. # libeay32MD.lib is identical to ../libeay32.lib, and
  185. # ssleay32MD.lib is identical to ../ssleay32.lib
  186. # enable OPENSSL_USE_STATIC_LIBS to use the static libs located in lib/VC/static
  187. if (OPENSSL_MSVC_STATIC_RT)
  188. set(_OPENSSL_MSVC_RT_MODE "MT")
  189. else ()
  190. set(_OPENSSL_MSVC_RT_MODE "MD")
  191. endif ()
  192. # Since OpenSSL 1.1, lib names are like libcrypto32MTd.lib and libssl32MTd.lib
  193. if( "${CMAKE_SIZEOF_VOID_P}" STREQUAL "8" )
  194. set(_OPENSSL_MSVC_ARCH_SUFFIX "64")
  195. else()
  196. set(_OPENSSL_MSVC_ARCH_SUFFIX "32")
  197. endif()
  198. if(OPENSSL_USE_STATIC_LIBS)
  199. set(_OPENSSL_STATIC_SUFFIX
  200. "_static"
  201. )
  202. set(_OPENSSL_PATH_SUFFIXES
  203. "lib/VC/static"
  204. "VC/static"
  205. "lib"
  206. )
  207. else()
  208. set(_OPENSSL_STATIC_SUFFIX
  209. ""
  210. )
  211. set(_OPENSSL_PATH_SUFFIXES
  212. "lib/VC"
  213. "VC"
  214. "lib"
  215. )
  216. endif ()
  217. find_library(LIB_EAY_DEBUG
  218. NAMES
  219. # When OpenSSL is built with default options, the static library name is suffixed with "_static".
  220. # Looking the "libcrypto_static.lib" with a higher priority than "libcrypto.lib" which is the
  221. # import library of "libcrypto.dll".
  222. libcrypto${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  223. libcrypto${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  224. libcrypto${_OPENSSL_STATIC_SUFFIX}d
  225. libeay32${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  226. libeay32${_OPENSSL_STATIC_SUFFIX}d
  227. crypto${_OPENSSL_STATIC_SUFFIX}d
  228. # When OpenSSL is built with the "-static" option, only the static build is produced,
  229. # and it is not suffixed with "_static".
  230. libcrypto${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  231. libcrypto${_OPENSSL_MSVC_RT_MODE}d
  232. libcryptod
  233. libeay32${_OPENSSL_MSVC_RT_MODE}d
  234. libeay32d
  235. cryptod
  236. NAMES_PER_DIR
  237. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  238. PATH_SUFFIXES
  239. ${_OPENSSL_PATH_SUFFIXES}
  240. )
  241. find_library(LIB_EAY_RELEASE
  242. NAMES
  243. # When OpenSSL is built with default options, the static library name is suffixed with "_static".
  244. # Looking the "libcrypto_static.lib" with a higher priority than "libcrypto.lib" which is the
  245. # import library of "libcrypto.dll".
  246. libcrypto${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  247. libcrypto${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  248. libcrypto${_OPENSSL_STATIC_SUFFIX}
  249. libeay32${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  250. libeay32${_OPENSSL_STATIC_SUFFIX}
  251. crypto${_OPENSSL_STATIC_SUFFIX}
  252. # When OpenSSL is built with the "-static" option, only the static build is produced,
  253. # and it is not suffixed with "_static".
  254. libcrypto${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  255. libcrypto${_OPENSSL_MSVC_RT_MODE}
  256. libcrypto
  257. libeay32${_OPENSSL_MSVC_RT_MODE}
  258. libeay32
  259. crypto
  260. NAMES_PER_DIR
  261. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  262. PATH_SUFFIXES
  263. ${_OPENSSL_PATH_SUFFIXES}
  264. )
  265. find_library(SSL_EAY_DEBUG
  266. NAMES
  267. # When OpenSSL is built with default options, the static library name is suffixed with "_static".
  268. # Looking the "libssl_static.lib" with a higher priority than "libssl.lib" which is the
  269. # import library of "libssl.dll".
  270. libssl${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  271. libssl${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  272. libssl${_OPENSSL_STATIC_SUFFIX}d
  273. ssleay32${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  274. ssleay32${_OPENSSL_STATIC_SUFFIX}d
  275. ssl${_OPENSSL_STATIC_SUFFIX}d
  276. # When OpenSSL is built with the "-static" option, only the static build is produced,
  277. # and it is not suffixed with "_static".
  278. libssl${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  279. libssl${_OPENSSL_MSVC_RT_MODE}d
  280. libssld
  281. ssleay32${_OPENSSL_MSVC_RT_MODE}d
  282. ssleay32d
  283. ssld
  284. NAMES_PER_DIR
  285. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  286. PATH_SUFFIXES
  287. ${_OPENSSL_PATH_SUFFIXES}
  288. )
  289. find_library(SSL_EAY_RELEASE
  290. NAMES
  291. # When OpenSSL is built with default options, the static library name is suffixed with "_static".
  292. # Looking the "libssl_static.lib" with a higher priority than "libssl.lib" which is the
  293. # import library of "libssl.dll".
  294. libssl${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  295. libssl${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  296. libssl${_OPENSSL_STATIC_SUFFIX}
  297. ssleay32${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  298. ssleay32${_OPENSSL_STATIC_SUFFIX}
  299. ssl${_OPENSSL_STATIC_SUFFIX}
  300. # When OpenSSL is built with the "-static" option, only the static build is produced,
  301. # and it is not suffixed with "_static".
  302. libssl${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  303. libssl${_OPENSSL_MSVC_RT_MODE}
  304. libssl
  305. ssleay32${_OPENSSL_MSVC_RT_MODE}
  306. ssleay32
  307. ssl
  308. NAMES_PER_DIR
  309. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  310. PATH_SUFFIXES
  311. ${_OPENSSL_PATH_SUFFIXES}
  312. )
  313. set(LIB_EAY_LIBRARY_DEBUG "${LIB_EAY_DEBUG}")
  314. set(LIB_EAY_LIBRARY_RELEASE "${LIB_EAY_RELEASE}")
  315. set(SSL_EAY_LIBRARY_DEBUG "${SSL_EAY_DEBUG}")
  316. set(SSL_EAY_LIBRARY_RELEASE "${SSL_EAY_RELEASE}")
  317. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  318. select_library_configurations(LIB_EAY)
  319. select_library_configurations(SSL_EAY)
  320. mark_as_advanced(LIB_EAY_LIBRARY_DEBUG LIB_EAY_LIBRARY_RELEASE
  321. SSL_EAY_LIBRARY_DEBUG SSL_EAY_LIBRARY_RELEASE)
  322. set(OPENSSL_SSL_LIBRARY ${SSL_EAY_LIBRARY} )
  323. set(OPENSSL_CRYPTO_LIBRARY ${LIB_EAY_LIBRARY} )
  324. elseif(MINGW)
  325. # same player, for MinGW
  326. set(LIB_EAY_NAMES crypto libeay32)
  327. set(SSL_EAY_NAMES ssl ssleay32)
  328. find_library(LIB_EAY
  329. NAMES
  330. ${LIB_EAY_NAMES}
  331. NAMES_PER_DIR
  332. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  333. PATH_SUFFIXES
  334. "lib/MinGW"
  335. "lib"
  336. )
  337. find_library(SSL_EAY
  338. NAMES
  339. ${SSL_EAY_NAMES}
  340. NAMES_PER_DIR
  341. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  342. PATH_SUFFIXES
  343. "lib/MinGW"
  344. "lib"
  345. )
  346. mark_as_advanced(SSL_EAY LIB_EAY)
  347. set(OPENSSL_SSL_LIBRARY ${SSL_EAY} )
  348. set(OPENSSL_CRYPTO_LIBRARY ${LIB_EAY} )
  349. unset(LIB_EAY_NAMES)
  350. unset(SSL_EAY_NAMES)
  351. else()
  352. # Not sure what to pick for -say- intel, let's use the toplevel ones and hope someone report issues:
  353. find_library(LIB_EAY
  354. NAMES
  355. libcrypto
  356. libeay32
  357. NAMES_PER_DIR
  358. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  359. HINTS
  360. ${_OPENSSL_LIBDIR}
  361. PATH_SUFFIXES
  362. lib
  363. )
  364. find_library(SSL_EAY
  365. NAMES
  366. libssl
  367. ssleay32
  368. NAMES_PER_DIR
  369. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  370. HINTS
  371. ${_OPENSSL_LIBDIR}
  372. PATH_SUFFIXES
  373. lib
  374. )
  375. mark_as_advanced(SSL_EAY LIB_EAY)
  376. set(OPENSSL_SSL_LIBRARY ${SSL_EAY} )
  377. set(OPENSSL_CRYPTO_LIBRARY ${LIB_EAY} )
  378. endif()
  379. else()
  380. find_library(OPENSSL_SSL_LIBRARY
  381. NAMES
  382. ssl
  383. ssleay32
  384. ssleay32MD
  385. NAMES_PER_DIR
  386. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  387. HINTS
  388. ${_OPENSSL_LIBDIR}
  389. ${_OPENSSL_LIBRARY_DIRS}
  390. PATH_SUFFIXES
  391. lib lib64
  392. )
  393. find_library(OPENSSL_CRYPTO_LIBRARY
  394. NAMES
  395. crypto
  396. NAMES_PER_DIR
  397. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  398. HINTS
  399. ${_OPENSSL_LIBDIR}
  400. ${_OPENSSL_LIBRARY_DIRS}
  401. PATH_SUFFIXES
  402. lib lib64
  403. )
  404. mark_as_advanced(OPENSSL_CRYPTO_LIBRARY OPENSSL_SSL_LIBRARY)
  405. endif()
  406. set(OPENSSL_SSL_LIBRARIES ${OPENSSL_SSL_LIBRARY})
  407. set(OPENSSL_CRYPTO_LIBRARIES ${OPENSSL_CRYPTO_LIBRARY})
  408. set(OPENSSL_LIBRARIES ${OPENSSL_SSL_LIBRARIES} ${OPENSSL_CRYPTO_LIBRARIES} )
  409. _OpenSSL_test_and_find_dependencies("${OPENSSL_SSL_LIBRARY}" "${OPENSSL_CRYPTO_LIBRARY}")
  410. if(_OpenSSL_has_dependencies)
  411. _OpenSSL_add_dependencies( OPENSSL_SSL_LIBRARIES )
  412. _OpenSSL_add_dependencies( OPENSSL_CRYPTO_LIBRARIES )
  413. _OpenSSL_add_dependencies( OPENSSL_LIBRARIES )
  414. endif()
  415. function(from_hex HEX DEC)
  416. string(TOUPPER "${HEX}" HEX)
  417. set(_res 0)
  418. string(LENGTH "${HEX}" _strlen)
  419. while (_strlen GREATER 0)
  420. math(EXPR _res "${_res} * 16")
  421. string(SUBSTRING "${HEX}" 0 1 NIBBLE)
  422. string(SUBSTRING "${HEX}" 1 -1 HEX)
  423. if (NIBBLE STREQUAL "A")
  424. math(EXPR _res "${_res} + 10")
  425. elseif (NIBBLE STREQUAL "B")
  426. math(EXPR _res "${_res} + 11")
  427. elseif (NIBBLE STREQUAL "C")
  428. math(EXPR _res "${_res} + 12")
  429. elseif (NIBBLE STREQUAL "D")
  430. math(EXPR _res "${_res} + 13")
  431. elseif (NIBBLE STREQUAL "E")
  432. math(EXPR _res "${_res} + 14")
  433. elseif (NIBBLE STREQUAL "F")
  434. math(EXPR _res "${_res} + 15")
  435. else()
  436. math(EXPR _res "${_res} + ${NIBBLE}")
  437. endif()
  438. string(LENGTH "${HEX}" _strlen)
  439. endwhile()
  440. set(${DEC} ${_res} PARENT_SCOPE)
  441. endfunction()
  442. if(OPENSSL_INCLUDE_DIR AND EXISTS "${OPENSSL_INCLUDE_DIR}/openssl/opensslv.h")
  443. file(STRINGS "${OPENSSL_INCLUDE_DIR}/openssl/opensslv.h" openssl_version_str
  444. REGEX "^#[\t ]*define[\t ]+OPENSSL_VERSION_NUMBER[\t ]+0x([0-9a-fA-F])+.*")
  445. if(openssl_version_str)
  446. # The version number is encoded as 0xMNNFFPPS: major minor fix patch status
  447. # The status gives if this is a developer or prerelease and is ignored here.
  448. # Major, minor, and fix directly translate into the version numbers shown in
  449. # the string. The patch field translates to the single character suffix that
  450. # indicates the bug fix state, which 00 -> nothing, 01 -> a, 02 -> b and so
  451. # on.
  452. 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]).*$"
  453. "\\1;\\2;\\3;\\4;\\5" OPENSSL_VERSION_LIST "${openssl_version_str}")
  454. list(GET OPENSSL_VERSION_LIST 0 OPENSSL_VERSION_MAJOR)
  455. list(GET OPENSSL_VERSION_LIST 1 OPENSSL_VERSION_MINOR)
  456. from_hex("${OPENSSL_VERSION_MINOR}" OPENSSL_VERSION_MINOR)
  457. list(GET OPENSSL_VERSION_LIST 2 OPENSSL_VERSION_FIX)
  458. from_hex("${OPENSSL_VERSION_FIX}" OPENSSL_VERSION_FIX)
  459. list(GET OPENSSL_VERSION_LIST 3 OPENSSL_VERSION_PATCH)
  460. if (NOT OPENSSL_VERSION_PATCH STREQUAL "00")
  461. from_hex("${OPENSSL_VERSION_PATCH}" _tmp)
  462. # 96 is the ASCII code of 'a' minus 1
  463. math(EXPR OPENSSL_VERSION_PATCH_ASCII "${_tmp} + 96")
  464. unset(_tmp)
  465. # Once anyone knows how OpenSSL would call the patch versions beyond 'z'
  466. # this should be updated to handle that, too. This has not happened yet
  467. # so it is simply ignored here for now.
  468. string(ASCII "${OPENSSL_VERSION_PATCH_ASCII}" OPENSSL_VERSION_PATCH_STRING)
  469. endif ()
  470. set(OPENSSL_VERSION "${OPENSSL_VERSION_MAJOR}.${OPENSSL_VERSION_MINOR}.${OPENSSL_VERSION_FIX}${OPENSSL_VERSION_PATCH_STRING}")
  471. else ()
  472. # Since OpenSSL 3.0.0, the new version format is MAJOR.MINOR.PATCH and
  473. # a new OPENSSL_VERSION_STR macro contains exactly that
  474. file(STRINGS "${OPENSSL_INCLUDE_DIR}/openssl/opensslv.h" OPENSSL_VERSION_STR
  475. REGEX "^#[\t ]*define[\t ]+OPENSSL_VERSION_STR[\t ]+\"([0-9])+\\.([0-9])+\\.([0-9])+\".*")
  476. string(REGEX REPLACE "^.*OPENSSL_VERSION_STR[\t ]+\"([0-9]+\\.[0-9]+\\.[0-9]+)\".*$"
  477. "\\1" OPENSSL_VERSION_STR "${OPENSSL_VERSION_STR}")
  478. set(OPENSSL_VERSION "${OPENSSL_VERSION_STR}")
  479. unset(OPENSSL_VERSION_STR)
  480. endif ()
  481. endif ()
  482. foreach(_comp IN LISTS OpenSSL_FIND_COMPONENTS)
  483. if(_comp STREQUAL "Crypto")
  484. if(EXISTS "${OPENSSL_INCLUDE_DIR}" AND
  485. (EXISTS "${OPENSSL_CRYPTO_LIBRARY}" OR
  486. EXISTS "${LIB_EAY_LIBRARY_DEBUG}" OR
  487. EXISTS "${LIB_EAY_LIBRARY_RELEASE}")
  488. )
  489. set(OpenSSL_${_comp}_FOUND TRUE)
  490. else()
  491. set(OpenSSL_${_comp}_FOUND FALSE)
  492. endif()
  493. elseif(_comp STREQUAL "SSL")
  494. if(EXISTS "${OPENSSL_INCLUDE_DIR}" AND
  495. (EXISTS "${OPENSSL_SSL_LIBRARY}" OR
  496. EXISTS "${SSL_EAY_LIBRARY_DEBUG}" OR
  497. EXISTS "${SSL_EAY_LIBRARY_RELEASE}")
  498. )
  499. set(OpenSSL_${_comp}_FOUND TRUE)
  500. else()
  501. set(OpenSSL_${_comp}_FOUND FALSE)
  502. endif()
  503. else()
  504. message(WARNING "${_comp} is not a valid OpenSSL component")
  505. set(OpenSSL_${_comp}_FOUND FALSE)
  506. endif()
  507. endforeach()
  508. unset(_comp)
  509. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  510. find_package_handle_standard_args(OpenSSL
  511. REQUIRED_VARS
  512. OPENSSL_CRYPTO_LIBRARY
  513. OPENSSL_INCLUDE_DIR
  514. VERSION_VAR
  515. OPENSSL_VERSION
  516. HANDLE_VERSION_RANGE
  517. HANDLE_COMPONENTS
  518. FAIL_MESSAGE
  519. "Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR"
  520. )
  521. mark_as_advanced(OPENSSL_INCLUDE_DIR)
  522. if(OPENSSL_FOUND)
  523. if(NOT TARGET OpenSSL::Crypto AND
  524. (EXISTS "${OPENSSL_CRYPTO_LIBRARY}" OR
  525. EXISTS "${LIB_EAY_LIBRARY_DEBUG}" OR
  526. EXISTS "${LIB_EAY_LIBRARY_RELEASE}")
  527. )
  528. add_library(OpenSSL::Crypto UNKNOWN IMPORTED)
  529. set_target_properties(OpenSSL::Crypto PROPERTIES
  530. INTERFACE_INCLUDE_DIRECTORIES "${OPENSSL_INCLUDE_DIR}")
  531. if(EXISTS "${OPENSSL_CRYPTO_LIBRARY}")
  532. set_target_properties(OpenSSL::Crypto PROPERTIES
  533. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  534. IMPORTED_LOCATION "${OPENSSL_CRYPTO_LIBRARY}")
  535. endif()
  536. if(EXISTS "${LIB_EAY_LIBRARY_RELEASE}")
  537. set_property(TARGET OpenSSL::Crypto APPEND PROPERTY
  538. IMPORTED_CONFIGURATIONS RELEASE)
  539. set_target_properties(OpenSSL::Crypto PROPERTIES
  540. IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
  541. IMPORTED_LOCATION_RELEASE "${LIB_EAY_LIBRARY_RELEASE}")
  542. endif()
  543. if(EXISTS "${LIB_EAY_LIBRARY_DEBUG}")
  544. set_property(TARGET OpenSSL::Crypto APPEND PROPERTY
  545. IMPORTED_CONFIGURATIONS DEBUG)
  546. set_target_properties(OpenSSL::Crypto PROPERTIES
  547. IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
  548. IMPORTED_LOCATION_DEBUG "${LIB_EAY_LIBRARY_DEBUG}")
  549. endif()
  550. _OpenSSL_target_add_dependencies(OpenSSL::Crypto)
  551. endif()
  552. if(NOT TARGET OpenSSL::SSL AND
  553. (EXISTS "${OPENSSL_SSL_LIBRARY}" OR
  554. EXISTS "${SSL_EAY_LIBRARY_DEBUG}" OR
  555. EXISTS "${SSL_EAY_LIBRARY_RELEASE}")
  556. )
  557. add_library(OpenSSL::SSL UNKNOWN IMPORTED)
  558. set_target_properties(OpenSSL::SSL PROPERTIES
  559. INTERFACE_INCLUDE_DIRECTORIES "${OPENSSL_INCLUDE_DIR}")
  560. if(EXISTS "${OPENSSL_SSL_LIBRARY}")
  561. set_target_properties(OpenSSL::SSL PROPERTIES
  562. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  563. IMPORTED_LOCATION "${OPENSSL_SSL_LIBRARY}")
  564. endif()
  565. if(EXISTS "${SSL_EAY_LIBRARY_RELEASE}")
  566. set_property(TARGET OpenSSL::SSL APPEND PROPERTY
  567. IMPORTED_CONFIGURATIONS RELEASE)
  568. set_target_properties(OpenSSL::SSL PROPERTIES
  569. IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
  570. IMPORTED_LOCATION_RELEASE "${SSL_EAY_LIBRARY_RELEASE}")
  571. endif()
  572. if(EXISTS "${SSL_EAY_LIBRARY_DEBUG}")
  573. set_property(TARGET OpenSSL::SSL APPEND PROPERTY
  574. IMPORTED_CONFIGURATIONS DEBUG)
  575. set_target_properties(OpenSSL::SSL PROPERTIES
  576. IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
  577. IMPORTED_LOCATION_DEBUG "${SSL_EAY_LIBRARY_DEBUG}")
  578. endif()
  579. if(TARGET OpenSSL::Crypto)
  580. set_target_properties(OpenSSL::SSL PROPERTIES
  581. INTERFACE_LINK_LIBRARIES OpenSSL::Crypto)
  582. endif()
  583. _OpenSSL_target_add_dependencies(OpenSSL::SSL)
  584. endif()
  585. if("${OPENSSL_VERSION_MAJOR}.${OPENSSL_VERSION_MINOR}.${OPENSSL_VERSION_FIX}" VERSION_GREATER_EQUAL "0.9.8")
  586. if(MSVC)
  587. if(EXISTS "${OPENSSL_INCLUDE_DIR}")
  588. set(_OPENSSL_applink_paths PATHS ${OPENSSL_INCLUDE_DIR})
  589. endif()
  590. find_file(OPENSSL_APPLINK_SOURCE
  591. NAMES
  592. openssl/applink.c
  593. ${_OPENSSL_applink_paths}
  594. NO_DEFAULT_PATH)
  595. if(OPENSSL_APPLINK_SOURCE)
  596. set(_OPENSSL_applink_interface_srcs ${OPENSSL_APPLINK_SOURCE})
  597. endif()
  598. endif()
  599. if(NOT TARGET OpenSSL::applink)
  600. add_library(OpenSSL::applink INTERFACE IMPORTED)
  601. set_property(TARGET OpenSSL::applink APPEND
  602. PROPERTY INTERFACE_SOURCES
  603. ${_OPENSSL_applink_interface_srcs})
  604. endif()
  605. endif()
  606. endif()
  607. # Restore the original find library ordering
  608. if(OPENSSL_USE_STATIC_LIBS)
  609. set(CMAKE_FIND_LIBRARY_SUFFIXES ${_openssl_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
  610. endif()