FindOpenSSL.cmake 26 KB

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