FindOpenSSL.cmake 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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. The following variables may be set to control search behavior:
  72. ``OPENSSL_ROOT_DIR``
  73. Set to the root directory of an OpenSSL installation.
  74. ``OPENSSL_USE_STATIC_LIBS``
  75. .. versionadded:: 3.4
  76. Set to ``TRUE`` to look for static libraries.
  77. ``OPENSSL_MSVC_STATIC_RT``
  78. .. versionadded:: 3.5
  79. Set to ``TRUE`` to choose the MT version of the lib.
  80. ``ENV{PKG_CONFIG_PATH}``
  81. On UNIX-like systems, ``pkg-config`` is used to locate the system OpenSSL.
  82. Set the ``PKG_CONFIG_PATH`` environment varialbe to look in alternate
  83. locations. Useful on multi-lib systems.
  84. #]=======================================================================]
  85. macro(_OpenSSL_test_and_find_dependencies ssl_library crypto_library)
  86. if((CMAKE_SYSTEM_NAME STREQUAL "Linux") AND
  87. (("${ssl_library}" MATCHES "\\${CMAKE_STATIC_LIBRARY_SUFFIX}$") OR
  88. ("${crypto_library}" MATCHES "\\${CMAKE_STATIC_LIBRARY_SUFFIX}$")))
  89. set(_OpenSSL_has_dependencies TRUE)
  90. find_package(Threads)
  91. else()
  92. set(_OpenSSL_has_dependencies FALSE)
  93. endif()
  94. endmacro()
  95. function(_OpenSSL_add_dependencies libraries_var)
  96. if(CMAKE_THREAD_LIBS_INIT)
  97. list(APPEND ${libraries_var} ${CMAKE_THREAD_LIBS_INIT})
  98. endif()
  99. list(APPEND ${libraries_var} ${CMAKE_DL_LIBS})
  100. set(${libraries_var} ${${libraries_var}} PARENT_SCOPE)
  101. endfunction()
  102. function(_OpenSSL_target_add_dependencies target)
  103. if(_OpenSSL_has_dependencies)
  104. set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES Threads::Threads )
  105. set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${CMAKE_DL_LIBS} )
  106. endif()
  107. if(WIN32 AND OPENSSL_USE_STATIC_LIBS)
  108. if(WINCE)
  109. set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ws2 )
  110. else()
  111. set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ws2_32 )
  112. endif()
  113. set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES crypt32 )
  114. endif()
  115. endfunction()
  116. if (UNIX)
  117. find_package(PkgConfig QUIET)
  118. pkg_check_modules(_OPENSSL QUIET openssl)
  119. endif ()
  120. # Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES
  121. if(OPENSSL_USE_STATIC_LIBS)
  122. set(_openssl_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  123. if(WIN32)
  124. set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
  125. else()
  126. set(CMAKE_FIND_LIBRARY_SUFFIXES .a )
  127. endif()
  128. endif()
  129. if(CMAKE_SYSTEM_NAME STREQUAL "QNX" AND
  130. CMAKE_SYSTEM_VERSION VERSION_GREATER_EQUAL "7.0" AND CMAKE_SYSTEM_VERSION VERSION_LESS "7.1" AND
  131. OpenSSL_FIND_VERSION VERSION_GREATER_EQUAL "1.1" AND OpenSSL_FIND_VERSION VERSION_LESS "1.2")
  132. # QNX 7.0.x provides openssl 1.0.2 and 1.1.1 in parallel:
  133. # * openssl 1.0.2: libcrypto.so.2 and libssl.so.2, headers under usr/include/openssl
  134. # * openssl 1.1.1: libcrypto1_1.so.2.1 and libssl1_1.so.2.1, header under usr/include/openssl1_1
  135. # See http://www.qnx.com/developers/articles/rel_6726_0.html
  136. set(_OPENSSL_FIND_PATH_SUFFIX "openssl1_1")
  137. set(_OPENSSL_NAME_POSTFIX "1_1")
  138. else()
  139. set(_OPENSSL_FIND_PATH_SUFFIX "include")
  140. endif()
  141. if (WIN32)
  142. # http://www.slproweb.com/products/Win32OpenSSL.html
  143. set(_OPENSSL_ROOT_HINTS
  144. ${OPENSSL_ROOT_DIR}
  145. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\OpenSSL (32-bit)_is1;Inno Setup: App Path]"
  146. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\OpenSSL (64-bit)_is1;Inno Setup: App Path]"
  147. ENV OPENSSL_ROOT_DIR
  148. )
  149. if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "8")
  150. set(_arch "Win64")
  151. file(TO_CMAKE_PATH "$ENV{PROGRAMFILES}" _programfiles)
  152. else()
  153. set(_arch "Win32")
  154. set(_progfiles_x86 "ProgramFiles(x86)")
  155. if(NOT "$ENV{${_progfiles_x86}}" STREQUAL "")
  156. # under windows 64 bit machine
  157. file(TO_CMAKE_PATH "$ENV{${_progfiles_x86}}" _programfiles)
  158. else()
  159. # under windows 32 bit machine
  160. file(TO_CMAKE_PATH "$ENV{ProgramFiles}" _programfiles)
  161. endif()
  162. endif()
  163. set(_OPENSSL_ROOT_PATHS
  164. "${_programfiles}/OpenSSL"
  165. "${_programfiles}/OpenSSL-${_arch}"
  166. "C:/OpenSSL/"
  167. "C:/OpenSSL-${_arch}/"
  168. )
  169. unset(_programfiles)
  170. unset(_arch)
  171. else ()
  172. set(_OPENSSL_ROOT_HINTS
  173. ${OPENSSL_ROOT_DIR}
  174. ENV OPENSSL_ROOT_DIR
  175. )
  176. endif ()
  177. set(_OPENSSL_ROOT_HINTS_AND_PATHS
  178. HINTS ${_OPENSSL_ROOT_HINTS}
  179. PATHS ${_OPENSSL_ROOT_PATHS}
  180. )
  181. find_path(OPENSSL_INCLUDE_DIR
  182. NAMES
  183. openssl/ssl.h
  184. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  185. HINTS
  186. ${_OPENSSL_INCLUDEDIR}
  187. ${_OPENSSL_INCLUDE_DIRS}
  188. PATH_SUFFIXES
  189. ${_OPENSSL_FIND_PATH_SUFFIX}
  190. )
  191. if(WIN32 AND NOT CYGWIN)
  192. if(MSVC)
  193. # /MD and /MDd are the standard values - if someone wants to use
  194. # others, the libnames have to change here too
  195. # use also ssl and ssleay32 in debug as fallback for openssl < 0.9.8b
  196. # enable OPENSSL_MSVC_STATIC_RT to get the libs build /MT (Multithreaded no-DLL)
  197. # In Visual C++ naming convention each of these four kinds of Windows libraries has it's standard suffix:
  198. # * MD for dynamic-release
  199. # * MDd for dynamic-debug
  200. # * MT for static-release
  201. # * MTd for static-debug
  202. # Implementation details:
  203. # We are using the libraries located in the VC subdir instead of the parent directory even though :
  204. # libeay32MD.lib is identical to ../libeay32.lib, and
  205. # ssleay32MD.lib is identical to ../ssleay32.lib
  206. # enable OPENSSL_USE_STATIC_LIBS to use the static libs located in lib/VC/static
  207. if (OPENSSL_MSVC_STATIC_RT)
  208. set(_OPENSSL_MSVC_RT_MODE "MT")
  209. else ()
  210. set(_OPENSSL_MSVC_RT_MODE "MD")
  211. endif ()
  212. # Since OpenSSL 1.1, lib names are like libcrypto32MTd.lib and libssl32MTd.lib
  213. if( "${CMAKE_SIZEOF_VOID_P}" STREQUAL "8" )
  214. set(_OPENSSL_MSVC_ARCH_SUFFIX "64")
  215. else()
  216. set(_OPENSSL_MSVC_ARCH_SUFFIX "32")
  217. endif()
  218. if(OPENSSL_USE_STATIC_LIBS)
  219. set(_OPENSSL_STATIC_SUFFIX
  220. "_static"
  221. )
  222. set(_OPENSSL_PATH_SUFFIXES
  223. "lib/VC/static"
  224. "VC/static"
  225. "lib"
  226. )
  227. else()
  228. set(_OPENSSL_STATIC_SUFFIX
  229. ""
  230. )
  231. set(_OPENSSL_PATH_SUFFIXES
  232. "lib/VC"
  233. "VC"
  234. "lib"
  235. )
  236. endif ()
  237. find_library(LIB_EAY_DEBUG
  238. NAMES
  239. # When OpenSSL is built with default options, the static library name is suffixed with "_static".
  240. # Looking the "libcrypto_static.lib" with a higher priority than "libcrypto.lib" which is the
  241. # import library of "libcrypto.dll".
  242. libcrypto${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  243. libcrypto${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  244. libcrypto${_OPENSSL_STATIC_SUFFIX}d
  245. libeay32${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  246. libeay32${_OPENSSL_STATIC_SUFFIX}d
  247. crypto${_OPENSSL_STATIC_SUFFIX}d
  248. # When OpenSSL is built with the "-static" option, only the static build is produced,
  249. # and it is not suffixed with "_static".
  250. libcrypto${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  251. libcrypto${_OPENSSL_MSVC_RT_MODE}d
  252. libcryptod
  253. libeay32${_OPENSSL_MSVC_RT_MODE}d
  254. libeay32d
  255. cryptod
  256. NAMES_PER_DIR
  257. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  258. PATH_SUFFIXES
  259. ${_OPENSSL_PATH_SUFFIXES}
  260. )
  261. find_library(LIB_EAY_RELEASE
  262. NAMES
  263. # When OpenSSL is built with default options, the static library name is suffixed with "_static".
  264. # Looking the "libcrypto_static.lib" with a higher priority than "libcrypto.lib" which is the
  265. # import library of "libcrypto.dll".
  266. libcrypto${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  267. libcrypto${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  268. libcrypto${_OPENSSL_STATIC_SUFFIX}
  269. libeay32${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  270. libeay32${_OPENSSL_STATIC_SUFFIX}
  271. crypto${_OPENSSL_STATIC_SUFFIX}
  272. # When OpenSSL is built with the "-static" option, only the static build is produced,
  273. # and it is not suffixed with "_static".
  274. libcrypto${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  275. libcrypto${_OPENSSL_MSVC_RT_MODE}
  276. libcrypto
  277. libeay32${_OPENSSL_MSVC_RT_MODE}
  278. libeay32
  279. crypto
  280. NAMES_PER_DIR
  281. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  282. PATH_SUFFIXES
  283. ${_OPENSSL_PATH_SUFFIXES}
  284. )
  285. find_library(SSL_EAY_DEBUG
  286. NAMES
  287. # When OpenSSL is built with default options, the static library name is suffixed with "_static".
  288. # Looking the "libssl_static.lib" with a higher priority than "libssl.lib" which is the
  289. # import library of "libssl.dll".
  290. libssl${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  291. libssl${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  292. libssl${_OPENSSL_STATIC_SUFFIX}d
  293. ssleay32${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  294. ssleay32${_OPENSSL_STATIC_SUFFIX}d
  295. ssl${_OPENSSL_STATIC_SUFFIX}d
  296. # When OpenSSL is built with the "-static" option, only the static build is produced,
  297. # and it is not suffixed with "_static".
  298. libssl${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  299. libssl${_OPENSSL_MSVC_RT_MODE}d
  300. libssld
  301. ssleay32${_OPENSSL_MSVC_RT_MODE}d
  302. ssleay32d
  303. ssld
  304. NAMES_PER_DIR
  305. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  306. PATH_SUFFIXES
  307. ${_OPENSSL_PATH_SUFFIXES}
  308. )
  309. find_library(SSL_EAY_RELEASE
  310. NAMES
  311. # When OpenSSL is built with default options, the static library name is suffixed with "_static".
  312. # Looking the "libssl_static.lib" with a higher priority than "libssl.lib" which is the
  313. # import library of "libssl.dll".
  314. libssl${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  315. libssl${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  316. libssl${_OPENSSL_STATIC_SUFFIX}
  317. ssleay32${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  318. ssleay32${_OPENSSL_STATIC_SUFFIX}
  319. ssl${_OPENSSL_STATIC_SUFFIX}
  320. # When OpenSSL is built with the "-static" option, only the static build is produced,
  321. # and it is not suffixed with "_static".
  322. libssl${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  323. libssl${_OPENSSL_MSVC_RT_MODE}
  324. libssl
  325. ssleay32${_OPENSSL_MSVC_RT_MODE}
  326. ssleay32
  327. ssl
  328. NAMES_PER_DIR
  329. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  330. PATH_SUFFIXES
  331. ${_OPENSSL_PATH_SUFFIXES}
  332. )
  333. set(LIB_EAY_LIBRARY_DEBUG "${LIB_EAY_DEBUG}")
  334. set(LIB_EAY_LIBRARY_RELEASE "${LIB_EAY_RELEASE}")
  335. set(SSL_EAY_LIBRARY_DEBUG "${SSL_EAY_DEBUG}")
  336. set(SSL_EAY_LIBRARY_RELEASE "${SSL_EAY_RELEASE}")
  337. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  338. select_library_configurations(LIB_EAY)
  339. select_library_configurations(SSL_EAY)
  340. mark_as_advanced(LIB_EAY_LIBRARY_DEBUG LIB_EAY_LIBRARY_RELEASE
  341. SSL_EAY_LIBRARY_DEBUG SSL_EAY_LIBRARY_RELEASE)
  342. set(OPENSSL_SSL_LIBRARY ${SSL_EAY_LIBRARY} )
  343. set(OPENSSL_CRYPTO_LIBRARY ${LIB_EAY_LIBRARY} )
  344. elseif(MINGW)
  345. # same player, for MinGW
  346. set(LIB_EAY_NAMES crypto libeay32)
  347. set(SSL_EAY_NAMES ssl ssleay32)
  348. find_library(LIB_EAY
  349. NAMES
  350. ${LIB_EAY_NAMES}
  351. NAMES_PER_DIR
  352. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  353. PATH_SUFFIXES
  354. "lib/MinGW"
  355. "lib"
  356. )
  357. find_library(SSL_EAY
  358. NAMES
  359. ${SSL_EAY_NAMES}
  360. NAMES_PER_DIR
  361. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  362. PATH_SUFFIXES
  363. "lib/MinGW"
  364. "lib"
  365. )
  366. mark_as_advanced(SSL_EAY LIB_EAY)
  367. set(OPENSSL_SSL_LIBRARY ${SSL_EAY} )
  368. set(OPENSSL_CRYPTO_LIBRARY ${LIB_EAY} )
  369. unset(LIB_EAY_NAMES)
  370. unset(SSL_EAY_NAMES)
  371. else()
  372. # Not sure what to pick for -say- intel, let's use the toplevel ones and hope someone report issues:
  373. find_library(LIB_EAY
  374. NAMES
  375. libcrypto
  376. libeay32
  377. NAMES_PER_DIR
  378. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  379. HINTS
  380. ${_OPENSSL_LIBDIR}
  381. PATH_SUFFIXES
  382. lib
  383. )
  384. find_library(SSL_EAY
  385. NAMES
  386. libssl
  387. ssleay32
  388. NAMES_PER_DIR
  389. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  390. HINTS
  391. ${_OPENSSL_LIBDIR}
  392. PATH_SUFFIXES
  393. lib
  394. )
  395. mark_as_advanced(SSL_EAY LIB_EAY)
  396. set(OPENSSL_SSL_LIBRARY ${SSL_EAY} )
  397. set(OPENSSL_CRYPTO_LIBRARY ${LIB_EAY} )
  398. endif()
  399. else()
  400. find_library(OPENSSL_SSL_LIBRARY
  401. NAMES
  402. ssl${_OPENSSL_NAME_POSTFIX}
  403. ssleay32
  404. ssleay32MD
  405. NAMES_PER_DIR
  406. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  407. HINTS
  408. ${_OPENSSL_LIBDIR}
  409. ${_OPENSSL_LIBRARY_DIRS}
  410. PATH_SUFFIXES
  411. lib lib64
  412. )
  413. find_library(OPENSSL_CRYPTO_LIBRARY
  414. NAMES
  415. crypto${_OPENSSL_NAME_POSTFIX}
  416. NAMES_PER_DIR
  417. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  418. HINTS
  419. ${_OPENSSL_LIBDIR}
  420. ${_OPENSSL_LIBRARY_DIRS}
  421. PATH_SUFFIXES
  422. lib lib64
  423. )
  424. mark_as_advanced(OPENSSL_CRYPTO_LIBRARY OPENSSL_SSL_LIBRARY)
  425. endif()
  426. set(OPENSSL_SSL_LIBRARIES ${OPENSSL_SSL_LIBRARY})
  427. set(OPENSSL_CRYPTO_LIBRARIES ${OPENSSL_CRYPTO_LIBRARY})
  428. set(OPENSSL_LIBRARIES ${OPENSSL_SSL_LIBRARIES} ${OPENSSL_CRYPTO_LIBRARIES} )
  429. _OpenSSL_test_and_find_dependencies("${OPENSSL_SSL_LIBRARY}" "${OPENSSL_CRYPTO_LIBRARY}")
  430. if(_OpenSSL_has_dependencies)
  431. _OpenSSL_add_dependencies( OPENSSL_SSL_LIBRARIES )
  432. _OpenSSL_add_dependencies( OPENSSL_CRYPTO_LIBRARIES )
  433. _OpenSSL_add_dependencies( OPENSSL_LIBRARIES )
  434. endif()
  435. function(from_hex HEX DEC)
  436. string(TOUPPER "${HEX}" HEX)
  437. set(_res 0)
  438. string(LENGTH "${HEX}" _strlen)
  439. while (_strlen GREATER 0)
  440. math(EXPR _res "${_res} * 16")
  441. string(SUBSTRING "${HEX}" 0 1 NIBBLE)
  442. string(SUBSTRING "${HEX}" 1 -1 HEX)
  443. if (NIBBLE STREQUAL "A")
  444. math(EXPR _res "${_res} + 10")
  445. elseif (NIBBLE STREQUAL "B")
  446. math(EXPR _res "${_res} + 11")
  447. elseif (NIBBLE STREQUAL "C")
  448. math(EXPR _res "${_res} + 12")
  449. elseif (NIBBLE STREQUAL "D")
  450. math(EXPR _res "${_res} + 13")
  451. elseif (NIBBLE STREQUAL "E")
  452. math(EXPR _res "${_res} + 14")
  453. elseif (NIBBLE STREQUAL "F")
  454. math(EXPR _res "${_res} + 15")
  455. else()
  456. math(EXPR _res "${_res} + ${NIBBLE}")
  457. endif()
  458. string(LENGTH "${HEX}" _strlen)
  459. endwhile()
  460. set(${DEC} ${_res} PARENT_SCOPE)
  461. endfunction()
  462. if(OPENSSL_INCLUDE_DIR AND EXISTS "${OPENSSL_INCLUDE_DIR}/openssl/opensslv.h")
  463. file(STRINGS "${OPENSSL_INCLUDE_DIR}/openssl/opensslv.h" openssl_version_str
  464. REGEX "^#[\t ]*define[\t ]+OPENSSL_VERSION_NUMBER[\t ]+0x([0-9a-fA-F])+.*")
  465. if(openssl_version_str)
  466. # The version number is encoded as 0xMNNFFPPS: major minor fix patch status
  467. # The status gives if this is a developer or prerelease and is ignored here.
  468. # Major, minor, and fix directly translate into the version numbers shown in
  469. # the string. The patch field translates to the single character suffix that
  470. # indicates the bug fix state, which 00 -> nothing, 01 -> a, 02 -> b and so
  471. # on.
  472. 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]).*$"
  473. "\\1;\\2;\\3;\\4;\\5" OPENSSL_VERSION_LIST "${openssl_version_str}")
  474. list(GET OPENSSL_VERSION_LIST 0 OPENSSL_VERSION_MAJOR)
  475. list(GET OPENSSL_VERSION_LIST 1 OPENSSL_VERSION_MINOR)
  476. from_hex("${OPENSSL_VERSION_MINOR}" OPENSSL_VERSION_MINOR)
  477. list(GET OPENSSL_VERSION_LIST 2 OPENSSL_VERSION_FIX)
  478. from_hex("${OPENSSL_VERSION_FIX}" OPENSSL_VERSION_FIX)
  479. list(GET OPENSSL_VERSION_LIST 3 OPENSSL_VERSION_PATCH)
  480. if (NOT OPENSSL_VERSION_PATCH STREQUAL "00")
  481. from_hex("${OPENSSL_VERSION_PATCH}" _tmp)
  482. # 96 is the ASCII code of 'a' minus 1
  483. math(EXPR OPENSSL_VERSION_PATCH_ASCII "${_tmp} + 96")
  484. unset(_tmp)
  485. # Once anyone knows how OpenSSL would call the patch versions beyond 'z'
  486. # this should be updated to handle that, too. This has not happened yet
  487. # so it is simply ignored here for now.
  488. string(ASCII "${OPENSSL_VERSION_PATCH_ASCII}" OPENSSL_VERSION_PATCH_STRING)
  489. endif ()
  490. set(OPENSSL_VERSION "${OPENSSL_VERSION_MAJOR}.${OPENSSL_VERSION_MINOR}.${OPENSSL_VERSION_FIX}${OPENSSL_VERSION_PATCH_STRING}")
  491. else ()
  492. # Since OpenSSL 3.0.0, the new version format is MAJOR.MINOR.PATCH and
  493. # a new OPENSSL_VERSION_STR macro contains exactly that
  494. file(STRINGS "${OPENSSL_INCLUDE_DIR}/openssl/opensslv.h" OPENSSL_VERSION_STR
  495. REGEX "^#[\t ]*define[\t ]+OPENSSL_VERSION_STR[\t ]+\"([0-9])+\\.([0-9])+\\.([0-9])+\".*")
  496. string(REGEX REPLACE "^.*OPENSSL_VERSION_STR[\t ]+\"([0-9]+\\.[0-9]+\\.[0-9]+)\".*$"
  497. "\\1" OPENSSL_VERSION_STR "${OPENSSL_VERSION_STR}")
  498. set(OPENSSL_VERSION "${OPENSSL_VERSION_STR}")
  499. # Setting OPENSSL_VERSION_MAJOR OPENSSL_VERSION_MINOR and OPENSSL_VERSION_FIX
  500. string(REGEX MATCHALL "([0-9])+" OPENSSL_VERSION_NUMBER "${OPENSSL_VERSION}")
  501. list(POP_FRONT OPENSSL_VERSION_NUMBER
  502. OPENSSL_VERSION_MAJOR
  503. OPENSSL_VERSION_MINOR
  504. OPENSSL_VERSION_FIX)
  505. unset(OPENSSL_VERSION_NUMBER)
  506. unset(OPENSSL_VERSION_STR)
  507. endif ()
  508. endif ()
  509. foreach(_comp IN LISTS OpenSSL_FIND_COMPONENTS)
  510. if(_comp STREQUAL "Crypto")
  511. if(EXISTS "${OPENSSL_INCLUDE_DIR}" AND
  512. (EXISTS "${OPENSSL_CRYPTO_LIBRARY}" OR
  513. EXISTS "${LIB_EAY_LIBRARY_DEBUG}" OR
  514. EXISTS "${LIB_EAY_LIBRARY_RELEASE}")
  515. )
  516. set(OpenSSL_${_comp}_FOUND TRUE)
  517. else()
  518. set(OpenSSL_${_comp}_FOUND FALSE)
  519. endif()
  520. elseif(_comp STREQUAL "SSL")
  521. if(EXISTS "${OPENSSL_INCLUDE_DIR}" AND
  522. (EXISTS "${OPENSSL_SSL_LIBRARY}" OR
  523. EXISTS "${SSL_EAY_LIBRARY_DEBUG}" OR
  524. EXISTS "${SSL_EAY_LIBRARY_RELEASE}")
  525. )
  526. set(OpenSSL_${_comp}_FOUND TRUE)
  527. else()
  528. set(OpenSSL_${_comp}_FOUND FALSE)
  529. endif()
  530. else()
  531. message(WARNING "${_comp} is not a valid OpenSSL component")
  532. set(OpenSSL_${_comp}_FOUND FALSE)
  533. endif()
  534. endforeach()
  535. unset(_comp)
  536. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  537. find_package_handle_standard_args(OpenSSL
  538. REQUIRED_VARS
  539. OPENSSL_CRYPTO_LIBRARY
  540. OPENSSL_INCLUDE_DIR
  541. VERSION_VAR
  542. OPENSSL_VERSION
  543. HANDLE_VERSION_RANGE
  544. HANDLE_COMPONENTS
  545. FAIL_MESSAGE
  546. "Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR"
  547. )
  548. mark_as_advanced(OPENSSL_INCLUDE_DIR)
  549. if(OPENSSL_FOUND)
  550. if(NOT TARGET OpenSSL::Crypto AND
  551. (EXISTS "${OPENSSL_CRYPTO_LIBRARY}" OR
  552. EXISTS "${LIB_EAY_LIBRARY_DEBUG}" OR
  553. EXISTS "${LIB_EAY_LIBRARY_RELEASE}")
  554. )
  555. add_library(OpenSSL::Crypto UNKNOWN IMPORTED)
  556. set_target_properties(OpenSSL::Crypto PROPERTIES
  557. INTERFACE_INCLUDE_DIRECTORIES "${OPENSSL_INCLUDE_DIR}")
  558. if(EXISTS "${OPENSSL_CRYPTO_LIBRARY}")
  559. set_target_properties(OpenSSL::Crypto PROPERTIES
  560. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  561. IMPORTED_LOCATION "${OPENSSL_CRYPTO_LIBRARY}")
  562. endif()
  563. if(EXISTS "${LIB_EAY_LIBRARY_RELEASE}")
  564. set_property(TARGET OpenSSL::Crypto APPEND PROPERTY
  565. IMPORTED_CONFIGURATIONS RELEASE)
  566. set_target_properties(OpenSSL::Crypto PROPERTIES
  567. IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
  568. IMPORTED_LOCATION_RELEASE "${LIB_EAY_LIBRARY_RELEASE}")
  569. endif()
  570. if(EXISTS "${LIB_EAY_LIBRARY_DEBUG}")
  571. set_property(TARGET OpenSSL::Crypto APPEND PROPERTY
  572. IMPORTED_CONFIGURATIONS DEBUG)
  573. set_target_properties(OpenSSL::Crypto PROPERTIES
  574. IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
  575. IMPORTED_LOCATION_DEBUG "${LIB_EAY_LIBRARY_DEBUG}")
  576. endif()
  577. _OpenSSL_target_add_dependencies(OpenSSL::Crypto)
  578. endif()
  579. if(NOT TARGET OpenSSL::SSL AND
  580. (EXISTS "${OPENSSL_SSL_LIBRARY}" OR
  581. EXISTS "${SSL_EAY_LIBRARY_DEBUG}" OR
  582. EXISTS "${SSL_EAY_LIBRARY_RELEASE}")
  583. )
  584. add_library(OpenSSL::SSL UNKNOWN IMPORTED)
  585. set_target_properties(OpenSSL::SSL PROPERTIES
  586. INTERFACE_INCLUDE_DIRECTORIES "${OPENSSL_INCLUDE_DIR}")
  587. if(EXISTS "${OPENSSL_SSL_LIBRARY}")
  588. set_target_properties(OpenSSL::SSL PROPERTIES
  589. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  590. IMPORTED_LOCATION "${OPENSSL_SSL_LIBRARY}")
  591. endif()
  592. if(EXISTS "${SSL_EAY_LIBRARY_RELEASE}")
  593. set_property(TARGET OpenSSL::SSL APPEND PROPERTY
  594. IMPORTED_CONFIGURATIONS RELEASE)
  595. set_target_properties(OpenSSL::SSL PROPERTIES
  596. IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
  597. IMPORTED_LOCATION_RELEASE "${SSL_EAY_LIBRARY_RELEASE}")
  598. endif()
  599. if(EXISTS "${SSL_EAY_LIBRARY_DEBUG}")
  600. set_property(TARGET OpenSSL::SSL APPEND PROPERTY
  601. IMPORTED_CONFIGURATIONS DEBUG)
  602. set_target_properties(OpenSSL::SSL PROPERTIES
  603. IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
  604. IMPORTED_LOCATION_DEBUG "${SSL_EAY_LIBRARY_DEBUG}")
  605. endif()
  606. if(TARGET OpenSSL::Crypto)
  607. set_target_properties(OpenSSL::SSL PROPERTIES
  608. INTERFACE_LINK_LIBRARIES OpenSSL::Crypto)
  609. endif()
  610. _OpenSSL_target_add_dependencies(OpenSSL::SSL)
  611. endif()
  612. if("${OPENSSL_VERSION_MAJOR}.${OPENSSL_VERSION_MINOR}.${OPENSSL_VERSION_FIX}" VERSION_GREATER_EQUAL "0.9.8")
  613. if(MSVC)
  614. if(EXISTS "${OPENSSL_INCLUDE_DIR}")
  615. set(_OPENSSL_applink_paths PATHS ${OPENSSL_INCLUDE_DIR})
  616. endif()
  617. find_file(OPENSSL_APPLINK_SOURCE
  618. NAMES
  619. openssl/applink.c
  620. ${_OPENSSL_applink_paths}
  621. NO_DEFAULT_PATH)
  622. if(OPENSSL_APPLINK_SOURCE)
  623. set(_OPENSSL_applink_interface_srcs ${OPENSSL_APPLINK_SOURCE})
  624. endif()
  625. endif()
  626. if(NOT TARGET OpenSSL::applink)
  627. add_library(OpenSSL::applink INTERFACE IMPORTED)
  628. set_property(TARGET OpenSSL::applink APPEND
  629. PROPERTY INTERFACE_SOURCES
  630. ${_OPENSSL_applink_interface_srcs})
  631. endif()
  632. endif()
  633. endif()
  634. # Restore the original find library ordering
  635. if(OPENSSL_USE_STATIC_LIBS)
  636. set(CMAKE_FIND_LIBRARY_SUFFIXES ${_openssl_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
  637. endif()
  638. unset(_OPENSSL_FIND_PATH_SUFFIX)
  639. unset(_OPENSSL_NAME_POSTFIX)