FindOpenSSL.cmake 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindOpenSSL
  5. -----------
  6. Finds the installed OpenSSL encryption library and determines its version:
  7. .. code-block:: cmake
  8. find_package(OpenSSL [<version>] [COMPONENTS <components>...] [...])
  9. .. versionadded:: 3.20
  10. Support for specifying version range when calling the :command:`find_package`
  11. command. When a version is requested, it can be specified as a single value
  12. as before, and now also a version range can be used. For a detailed
  13. description of version range usage and capabilities, refer to the
  14. :command:`find_package` command.
  15. .. versionadded:: 3.18
  16. Support for OpenSSL 3.0.
  17. Components
  18. ^^^^^^^^^^
  19. This module supports the following optional components:
  20. ``Crypto``
  21. .. versionadded:: 3.12
  22. Ensures that the OpenSSL ``crypto`` library is found.
  23. ``SSL``
  24. .. versionadded:: 3.12
  25. Ensures that the OpenSSL ``ssl`` library is found.
  26. Components can be optionally specified using a standard syntax:
  27. .. code-block:: cmake
  28. find_package(OpenSSL [COMPONENTS <components>...])
  29. If no components are requested, module by default searches for the ``Crypto``
  30. as required and ``SSL`` as optional component.
  31. Imported Targets
  32. ^^^^^^^^^^^^^^^^
  33. This module provides the following :ref:`Imported Targets`:
  34. ``OpenSSL::Crypto``
  35. .. versionadded:: 3.4
  36. Target encapsulating the OpenSSL ``crypto`` library usage requirements,
  37. available only if the ``crypto`` library is found.
  38. ``OpenSSL::SSL``
  39. .. versionadded:: 3.4
  40. Target encapsulating the OpenSSL ``ssl`` library usage requirements, available
  41. only if the ``ssl`` library is found. For convenience, this target also links
  42. ``OpenSSL::Crypto``, since the ``ssl`` library depends on the ``crypto``
  43. library.
  44. ``OpenSSL::applink``
  45. .. versionadded:: 3.18
  46. Target encapsulating the OpenSSL application-side interface
  47. (``openssl/applink.c``) usage requirements, available only if OpenSSL is found
  48. and its version is at least 0.9.8.
  49. This interface provides a glue between OpenSSL BIO layer and the Windows
  50. compiler runtime environment and may need to be compiled into projects when
  51. using MSVC. By linking this target, the other OpenSSL imported targets can be
  52. linked even if the project uses different MSVC runtime configuration. Linking
  53. this target on platforms other than MSVC has no effect.
  54. .. note::
  55. The interface file is added using the :prop_tgt:`INTERFACE_SOURCES` target
  56. property. Due to how interface sources are propagated in CMake, it is
  57. recommended to link the ``OpenSSL::applink`` target as
  58. :ref:`PRIVATE <Target Command Scope>` to ensure that it is linked only once
  59. in the entire dependency graph of any library or executable:
  60. .. code-block:: cmake
  61. target_link_libraries(project_target PRIVATE OpenSSL::applink)
  62. Using other scopes for this target specifically can lead to unexpected
  63. issues during the build or link process, as both the ISO C and ISO C++
  64. standards place very few requirements on how linking should behave.
  65. Result Variables
  66. ^^^^^^^^^^^^^^^^
  67. This module defines the following variables:
  68. ``OpenSSL_FOUND``
  69. Boolean indicating whether (the requested version of) OpenSSL library has
  70. been found. For backward compatibility, the ``OPENSSL_FOUND`` variableđ
  71. is also set to the same value.
  72. ``OpenSSL_VERSION``
  73. .. versionadded:: 4.2
  74. The OpenSSL version found. This is set to
  75. ``<major>.<minor>.<revision><patch>`` (e.g., ``0.9.8s``).
  76. ``OPENSSL_INCLUDE_DIR``
  77. The OpenSSL include directory.
  78. ``OPENSSL_CRYPTO_LIBRARY``
  79. The OpenSSL ``crypto`` library.
  80. ``OPENSSL_CRYPTO_LIBRARIES``
  81. The OpenSSL ``crypto`` library and its dependencies.
  82. ``OPENSSL_SSL_LIBRARY``
  83. The OpenSSL ``ssl`` library.
  84. ``OPENSSL_SSL_LIBRARIES``
  85. The OpenSSL ``ssl`` library and its dependencies.
  86. ``OPENSSL_LIBRARIES``
  87. All OpenSSL libraries and their dependencies.
  88. ``OPENSSL_APPLINK_SOURCE``
  89. The sources in the target ``OpenSSL::applink`` mentioned above. This variable
  90. is only defined if found OpenSSL version is at least 0.9.8 and the platform is
  91. MSVC.
  92. Hints
  93. ^^^^^
  94. This module accepts the following variables to control the search behavior:
  95. ``OPENSSL_ROOT_DIR``
  96. Set to the root directory of an OpenSSL installation to search for the OpenSSL
  97. libraries in custom locations.
  98. ``OPENSSL_USE_STATIC_LIBS``
  99. .. versionadded:: 3.4
  100. Set to ``TRUE`` to prefer static OpenSSL libraries over shared ones.
  101. ``OPENSSL_MSVC_STATIC_RT``
  102. .. versionadded:: 3.5
  103. Set to ``TRUE`` to search for the OpenSSL libraries built with the MSVC static
  104. runtime (MT).
  105. ``ENV{PKG_CONFIG_PATH}``
  106. On UNIX-like systems, ``pkg-config`` is used to locate OpenSSL. Set the
  107. ``PKG_CONFIG_PATH`` environment variable to specify alternate locations, which
  108. is useful on systems with multiple library installations.
  109. Deprecated Variables
  110. ^^^^^^^^^^^^^^^^^^^^
  111. The following variables are provided for backward compatibility:
  112. ``OPENSSL_VERSION``
  113. .. deprecated:: 4.2
  114. Superseded by the ``OpenSSL_VERSION``.
  115. The version of OpenSSL found.
  116. Examples
  117. ^^^^^^^^
  118. Finding the OpenSSL ``crypto`` library and linking it to a project target:
  119. .. code-block:: cmake
  120. find_package(OpenSSL)
  121. target_link_libraries(project_target PRIVATE OpenSSL::Crypto)
  122. The following example shows how to find the OpenSSL ``crypto`` and ``ssl``
  123. libraries and link them to a project target. The ``SSL`` component is
  124. explicitly specified to ensure that the find module reports an error if the
  125. ``ssl`` library is not found:
  126. .. code-block:: cmake
  127. find_package(OpenSSL COMPONENTS SSL)
  128. target_link_libraries(project_target PRIVATE OpenSSL::SSL)
  129. #]=======================================================================]
  130. cmake_policy(PUSH)
  131. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  132. macro(_OpenSSL_test_and_find_dependencies ssl_library crypto_library)
  133. unset(_OpenSSL_extra_static_deps)
  134. if(UNIX AND
  135. (("${ssl_library}" MATCHES "\\${CMAKE_STATIC_LIBRARY_SUFFIX}$") OR
  136. ("${crypto_library}" MATCHES "\\${CMAKE_STATIC_LIBRARY_SUFFIX}$")))
  137. set(_OpenSSL_has_dependencies TRUE)
  138. unset(_OpenSSL_has_dependency_zlib)
  139. if(OPENSSL_USE_STATIC_LIBS)
  140. set(_OpenSSL_libs "${_OPENSSL_STATIC_LIBRARIES}")
  141. set(_OpenSSL_ldflags_other "${_OPENSSL_STATIC_LDFLAGS_OTHER}")
  142. else()
  143. set(_OpenSSL_libs "${_OPENSSL_LIBRARIES}")
  144. set(_OpenSSL_ldflags_other "${_OPENSSL_LDFLAGS_OTHER}")
  145. endif()
  146. if(_OpenSSL_libs)
  147. unset(_OpenSSL_has_dependency_dl)
  148. foreach(_OPENSSL_DEP_LIB IN LISTS _OpenSSL_libs)
  149. if (_OPENSSL_DEP_LIB STREQUAL "ssl" OR _OPENSSL_DEP_LIB STREQUAL "crypto")
  150. # ignoring: these are the targets
  151. elseif(_OPENSSL_DEP_LIB STREQUAL CMAKE_DL_LIBS)
  152. set(_OpenSSL_has_dependency_dl TRUE)
  153. elseif(_OPENSSL_DEP_LIB STREQUAL "z")
  154. find_package(ZLIB)
  155. set(_OpenSSL_has_dependency_zlib TRUE)
  156. else()
  157. list(APPEND _OpenSSL_extra_static_deps "${_OPENSSL_DEP_LIB}")
  158. endif()
  159. endforeach()
  160. unset(_OPENSSL_DEP_LIB)
  161. elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  162. set(_OpenSSL_has_dependency_dl TRUE)
  163. endif()
  164. if(_OpenSSL_ldflags_other)
  165. unset(_OpenSSL_has_dependency_threads)
  166. foreach(_OPENSSL_DEP_LDFLAG IN LISTS _OpenSSL_ldflags_other)
  167. if (_OPENSSL_DEP_LDFLAG STREQUAL "-pthread")
  168. set(_OpenSSL_has_dependency_threads TRUE)
  169. find_package(Threads)
  170. endif()
  171. endforeach()
  172. unset(_OPENSSL_DEP_LDFLAG)
  173. elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  174. set(_OpenSSL_has_dependency_threads TRUE)
  175. find_package(Threads)
  176. endif()
  177. unset(_OpenSSL_libs)
  178. unset(_OpenSSL_ldflags_other)
  179. else()
  180. set(_OpenSSL_has_dependencies FALSE)
  181. endif()
  182. endmacro()
  183. function(_OpenSSL_add_dependencies libraries_var)
  184. if(_OpenSSL_has_dependency_zlib)
  185. list(APPEND ${libraries_var} ${ZLIB_LIBRARY})
  186. endif()
  187. if(_OpenSSL_has_dependency_threads)
  188. list(APPEND ${libraries_var} ${CMAKE_THREAD_LIBS_INIT})
  189. endif()
  190. if(_OpenSSL_has_dependency_dl)
  191. list(APPEND ${libraries_var} ${CMAKE_DL_LIBS})
  192. endif()
  193. list(APPEND ${libraries_var} ${_OpenSSL_extra_static_deps})
  194. set(${libraries_var} ${${libraries_var}} PARENT_SCOPE)
  195. endfunction()
  196. function(_OpenSSL_target_add_dependencies target)
  197. if(_OpenSSL_has_dependencies)
  198. if(_OpenSSL_has_dependency_zlib)
  199. set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ZLIB::ZLIB )
  200. endif()
  201. if(_OpenSSL_has_dependency_threads)
  202. set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES Threads::Threads)
  203. endif()
  204. if(_OpenSSL_has_dependency_dl)
  205. set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${CMAKE_DL_LIBS} )
  206. endif()
  207. if(_OpenSSL_extra_static_deps)
  208. set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${_OpenSSL_extra_static_deps})
  209. endif()
  210. endif()
  211. if(WIN32 AND OPENSSL_USE_STATIC_LIBS)
  212. if(WINCE)
  213. set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ws2 )
  214. else()
  215. set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ws2_32 )
  216. endif()
  217. set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES crypt32 )
  218. endif()
  219. endfunction()
  220. if (UNIX)
  221. find_package(PkgConfig QUIET)
  222. if(PKG_CONFIG_FOUND)
  223. pkg_check_modules(_OPENSSL QUIET openssl)
  224. endif()
  225. endif ()
  226. # Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES
  227. if(OPENSSL_USE_STATIC_LIBS)
  228. set(_openssl_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  229. if(MSVC)
  230. set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
  231. else()
  232. set(CMAKE_FIND_LIBRARY_SUFFIXES .a )
  233. endif()
  234. endif()
  235. if(CMAKE_SYSTEM_NAME STREQUAL "QNX" AND
  236. CMAKE_SYSTEM_VERSION VERSION_GREATER_EQUAL "7.0" AND CMAKE_SYSTEM_VERSION VERSION_LESS "7.1" AND
  237. OpenSSL_FIND_VERSION VERSION_GREATER_EQUAL "1.1" AND OpenSSL_FIND_VERSION VERSION_LESS "1.2")
  238. # QNX 7.0.x provides openssl 1.0.2 and 1.1.1 in parallel:
  239. # * openssl 1.0.2: libcrypto.so.2 and libssl.so.2, headers under usr/include/openssl
  240. # * openssl 1.1.1: libcrypto1_1.so.2.1 and libssl1_1.so.2.1, header under usr/include/openssl1_1
  241. # See http://www.qnx.com/developers/articles/rel_6726_0.html
  242. set(_OPENSSL_FIND_PATH_SUFFIX "openssl1_1")
  243. set(_OPENSSL_NAME_POSTFIX "1_1")
  244. else()
  245. set(_OPENSSL_FIND_PATH_SUFFIX "include")
  246. endif()
  247. if (OPENSSL_ROOT_DIR OR NOT "$ENV{OPENSSL_ROOT_DIR}" STREQUAL "")
  248. set(_OPENSSL_ROOT_HINTS HINTS ${OPENSSL_ROOT_DIR} ENV OPENSSL_ROOT_DIR)
  249. set(_OPENSSL_ROOT_PATHS NO_DEFAULT_PATH)
  250. elseif (MSVC)
  251. # http://www.slproweb.com/products/Win32OpenSSL.html
  252. set(_OPENSSL_ROOT_HINTS
  253. HINTS
  254. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\OpenSSL (32-bit)_is1;Inno Setup: App Path]"
  255. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\OpenSSL (64-bit)_is1;Inno Setup: App Path]"
  256. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\OpenSSL for ARM (64-bit)_is1;Inno Setup: App Path]"
  257. )
  258. if ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ARM64")
  259. set(_arch "Win64-ARM")
  260. file(TO_CMAKE_PATH "$ENV{PROGRAMFILES}" _programfiles)
  261. elseif("${CMAKE_SIZEOF_VOID_P}" STREQUAL "8")
  262. set(_arch "Win64")
  263. file(TO_CMAKE_PATH "$ENV{PROGRAMFILES}" _programfiles)
  264. else()
  265. set(_arch "Win32")
  266. set(_progfiles_x86 "ProgramFiles(x86)")
  267. if(NOT "$ENV{${_progfiles_x86}}" STREQUAL "")
  268. # under windows 64 bit machine
  269. file(TO_CMAKE_PATH "$ENV{${_progfiles_x86}}" _programfiles)
  270. else()
  271. # under windows 32 bit machine
  272. file(TO_CMAKE_PATH "$ENV{ProgramFiles}" _programfiles)
  273. endif()
  274. endif()
  275. set(_OPENSSL_ROOT_PATHS
  276. PATHS
  277. "${_programfiles}/OpenSSL"
  278. "${_programfiles}/OpenSSL-${_arch}"
  279. "C:/OpenSSL/"
  280. "C:/OpenSSL-${_arch}/"
  281. )
  282. unset(_programfiles)
  283. unset(_arch)
  284. endif ()
  285. set(_OPENSSL_ROOT_HINTS_AND_PATHS
  286. ${_OPENSSL_ROOT_HINTS}
  287. ${_OPENSSL_ROOT_PATHS}
  288. )
  289. find_path(OPENSSL_INCLUDE_DIR
  290. NAMES
  291. openssl/ssl.h
  292. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  293. HINTS
  294. ${_OPENSSL_INCLUDEDIR}
  295. ${_OPENSSL_INCLUDE_DIRS}
  296. PATH_SUFFIXES
  297. ${_OPENSSL_FIND_PATH_SUFFIX}
  298. )
  299. if(WIN32 AND NOT CYGWIN)
  300. if(MSVC)
  301. # /MD and /MDd are the standard values - if someone wants to use
  302. # others, the libnames have to change here too
  303. # use also ssl and ssleay32 in debug as fallback for openssl < 0.9.8b
  304. # enable OPENSSL_MSVC_STATIC_RT to get the libs build /MT (Multithreaded no-DLL)
  305. # In Visual C++ naming convention each of these four kinds of Windows libraries has it's standard suffix:
  306. # * MD for dynamic-release
  307. # * MDd for dynamic-debug
  308. # * MT for static-release
  309. # * MTd for static-debug
  310. # Implementation details:
  311. # We are using the libraries located in the VC subdir instead of the parent directory even though :
  312. # libeay32MD.lib is identical to ../libeay32.lib, and
  313. # ssleay32MD.lib is identical to ../ssleay32.lib
  314. # enable OPENSSL_USE_STATIC_LIBS to use the static libs located in lib/VC/static
  315. if (OPENSSL_MSVC_STATIC_RT)
  316. set(_OPENSSL_MSVC_RT_MODE "MT")
  317. else ()
  318. set(_OPENSSL_MSVC_RT_MODE "MD")
  319. endif ()
  320. # Since OpenSSL 1.1, lib names are like libcrypto32MTd.lib and libssl32MTd.lib
  321. if ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ARM64")
  322. set(_OPENSSL_MSVC_FOLDER_SUFFIX "arm64")
  323. elseif( "${CMAKE_SIZEOF_VOID_P}" STREQUAL "8" )
  324. set(_OPENSSL_MSVC_ARCH_SUFFIX "64")
  325. set(_OPENSSL_MSVC_FOLDER_SUFFIX "x64")
  326. else()
  327. set(_OPENSSL_MSVC_ARCH_SUFFIX "32")
  328. set(_OPENSSL_MSVC_FOLDER_SUFFIX "x86")
  329. endif()
  330. if(OPENSSL_USE_STATIC_LIBS)
  331. set(_OPENSSL_STATIC_SUFFIX
  332. "_static"
  333. )
  334. set(_OPENSSL_PATH_SUFFIXES_DEBUG
  335. "lib/VC/${_OPENSSL_MSVC_FOLDER_SUFFIX}/${_OPENSSL_MSVC_RT_MODE}d"
  336. "lib/VC/static"
  337. "VC/static"
  338. "lib"
  339. )
  340. set(_OPENSSL_PATH_SUFFIXES_RELEASE
  341. "lib/VC/${_OPENSSL_MSVC_FOLDER_SUFFIX}/${_OPENSSL_MSVC_RT_MODE}"
  342. "lib/VC/static"
  343. "VC/static"
  344. "lib"
  345. )
  346. else()
  347. set(_OPENSSL_STATIC_SUFFIX
  348. ""
  349. )
  350. set(_OPENSSL_PATH_SUFFIXES_DEBUG
  351. "lib/VC/${_OPENSSL_MSVC_FOLDER_SUFFIX}/${_OPENSSL_MSVC_RT_MODE}d"
  352. "lib/VC"
  353. "VC"
  354. "lib"
  355. )
  356. set(_OPENSSL_PATH_SUFFIXES_RELEASE
  357. "lib/VC/${_OPENSSL_MSVC_FOLDER_SUFFIX}/${_OPENSSL_MSVC_RT_MODE}"
  358. "lib/VC"
  359. "VC"
  360. "lib"
  361. )
  362. endif ()
  363. find_library(LIB_EAY_DEBUG
  364. NAMES
  365. # When OpenSSL is built with default options, the static library name is suffixed with "_static".
  366. # Looking the "libcrypto_static.lib" with a higher priority than "libcrypto.lib" which is the
  367. # import library of "libcrypto.dll".
  368. libcrypto${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  369. libcrypto${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  370. libcrypto${_OPENSSL_STATIC_SUFFIX}d
  371. libcrypto${_OPENSSL_STATIC_SUFFIX}
  372. libeay32${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  373. libeay32${_OPENSSL_STATIC_SUFFIX}d
  374. crypto${_OPENSSL_STATIC_SUFFIX}d
  375. # When OpenSSL is built with the "-static" option, only the static build is produced,
  376. # and it is not suffixed with "_static".
  377. libcrypto${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  378. libcrypto${_OPENSSL_MSVC_RT_MODE}d
  379. libcryptod
  380. libeay32${_OPENSSL_MSVC_RT_MODE}d
  381. libeay32d
  382. cryptod
  383. NAMES_PER_DIR
  384. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  385. PATH_SUFFIXES
  386. ${_OPENSSL_PATH_SUFFIXES_DEBUG}
  387. )
  388. find_library(LIB_EAY_RELEASE
  389. NAMES
  390. # When OpenSSL is built with default options, the static library name is suffixed with "_static".
  391. # Looking the "libcrypto_static.lib" with a higher priority than "libcrypto.lib" which is the
  392. # import library of "libcrypto.dll".
  393. libcrypto${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  394. libcrypto${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  395. libcrypto${_OPENSSL_STATIC_SUFFIX}
  396. libeay32${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  397. libeay32${_OPENSSL_STATIC_SUFFIX}
  398. crypto${_OPENSSL_STATIC_SUFFIX}
  399. # When OpenSSL is built with the "-static" option, only the static build is produced,
  400. # and it is not suffixed with "_static".
  401. libcrypto${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  402. libcrypto${_OPENSSL_MSVC_RT_MODE}
  403. libcrypto
  404. libeay32${_OPENSSL_MSVC_RT_MODE}
  405. libeay32
  406. crypto
  407. NAMES_PER_DIR
  408. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  409. PATH_SUFFIXES
  410. ${_OPENSSL_PATH_SUFFIXES_RELEASE}
  411. )
  412. find_library(SSL_EAY_DEBUG
  413. NAMES
  414. # When OpenSSL is built with default options, the static library name is suffixed with "_static".
  415. # Looking the "libssl_static.lib" with a higher priority than "libssl.lib" which is the
  416. # import library of "libssl.dll".
  417. libssl${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  418. libssl${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  419. libssl${_OPENSSL_STATIC_SUFFIX}d
  420. libssl${_OPENSSL_STATIC_SUFFIX}
  421. ssleay32${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  422. ssleay32${_OPENSSL_STATIC_SUFFIX}d
  423. ssl${_OPENSSL_STATIC_SUFFIX}d
  424. # When OpenSSL is built with the "-static" option, only the static build is produced,
  425. # and it is not suffixed with "_static".
  426. libssl${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d
  427. libssl${_OPENSSL_MSVC_RT_MODE}d
  428. libssld
  429. ssleay32${_OPENSSL_MSVC_RT_MODE}d
  430. ssleay32d
  431. ssld
  432. NAMES_PER_DIR
  433. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  434. PATH_SUFFIXES
  435. ${_OPENSSL_PATH_SUFFIXES_DEBUG}
  436. )
  437. find_library(SSL_EAY_RELEASE
  438. NAMES
  439. # When OpenSSL is built with default options, the static library name is suffixed with "_static".
  440. # Looking the "libssl_static.lib" with a higher priority than "libssl.lib" which is the
  441. # import library of "libssl.dll".
  442. libssl${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  443. libssl${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  444. libssl${_OPENSSL_STATIC_SUFFIX}
  445. ssleay32${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  446. ssleay32${_OPENSSL_STATIC_SUFFIX}
  447. ssl${_OPENSSL_STATIC_SUFFIX}
  448. # When OpenSSL is built with the "-static" option, only the static build is produced,
  449. # and it is not suffixed with "_static".
  450. libssl${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}
  451. libssl${_OPENSSL_MSVC_RT_MODE}
  452. libssl
  453. ssleay32${_OPENSSL_MSVC_RT_MODE}
  454. ssleay32
  455. ssl
  456. NAMES_PER_DIR
  457. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  458. PATH_SUFFIXES
  459. ${_OPENSSL_PATH_SUFFIXES_RELEASE}
  460. )
  461. set(LIB_EAY_LIBRARY_DEBUG "${LIB_EAY_DEBUG}")
  462. set(LIB_EAY_LIBRARY_RELEASE "${LIB_EAY_RELEASE}")
  463. set(SSL_EAY_LIBRARY_DEBUG "${SSL_EAY_DEBUG}")
  464. set(SSL_EAY_LIBRARY_RELEASE "${SSL_EAY_RELEASE}")
  465. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  466. select_library_configurations(LIB_EAY)
  467. select_library_configurations(SSL_EAY)
  468. mark_as_advanced(LIB_EAY_LIBRARY_DEBUG LIB_EAY_LIBRARY_RELEASE
  469. SSL_EAY_LIBRARY_DEBUG SSL_EAY_LIBRARY_RELEASE)
  470. set(OPENSSL_SSL_LIBRARY ${SSL_EAY_LIBRARY} )
  471. set(OPENSSL_CRYPTO_LIBRARY ${LIB_EAY_LIBRARY} )
  472. elseif(MINGW)
  473. # same player, for MinGW
  474. set(LIB_EAY_NAMES crypto libeay32)
  475. set(SSL_EAY_NAMES ssl ssleay32)
  476. find_library(LIB_EAY
  477. NAMES
  478. ${LIB_EAY_NAMES}
  479. NAMES_PER_DIR
  480. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  481. PATH_SUFFIXES
  482. "lib/MinGW"
  483. "lib"
  484. "lib64"
  485. )
  486. find_library(SSL_EAY
  487. NAMES
  488. ${SSL_EAY_NAMES}
  489. NAMES_PER_DIR
  490. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  491. PATH_SUFFIXES
  492. "lib/MinGW"
  493. "lib"
  494. "lib64"
  495. )
  496. mark_as_advanced(SSL_EAY LIB_EAY)
  497. set(OPENSSL_SSL_LIBRARY ${SSL_EAY} )
  498. set(OPENSSL_CRYPTO_LIBRARY ${LIB_EAY} )
  499. unset(LIB_EAY_NAMES)
  500. unset(SSL_EAY_NAMES)
  501. else()
  502. # Not sure what to pick for -say- intel, let's use the toplevel ones and hope someone report issues:
  503. find_library(LIB_EAY
  504. NAMES
  505. libcrypto
  506. libeay32
  507. NAMES_PER_DIR
  508. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  509. HINTS
  510. ${_OPENSSL_LIBDIR}
  511. PATH_SUFFIXES
  512. lib
  513. )
  514. find_library(SSL_EAY
  515. NAMES
  516. libssl
  517. ssleay32
  518. NAMES_PER_DIR
  519. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  520. HINTS
  521. ${_OPENSSL_LIBDIR}
  522. PATH_SUFFIXES
  523. lib
  524. )
  525. mark_as_advanced(SSL_EAY LIB_EAY)
  526. set(OPENSSL_SSL_LIBRARY ${SSL_EAY} )
  527. set(OPENSSL_CRYPTO_LIBRARY ${LIB_EAY} )
  528. endif()
  529. else()
  530. find_library(OPENSSL_SSL_LIBRARY
  531. NAMES
  532. ssl${_OPENSSL_NAME_POSTFIX}
  533. ssleay32
  534. ssleay32MD
  535. NAMES_PER_DIR
  536. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  537. HINTS
  538. ${_OPENSSL_LIBDIR}
  539. ${_OPENSSL_LIBRARY_DIRS}
  540. PATH_SUFFIXES
  541. lib lib64
  542. )
  543. find_library(OPENSSL_CRYPTO_LIBRARY
  544. NAMES
  545. crypto${_OPENSSL_NAME_POSTFIX}
  546. NAMES_PER_DIR
  547. ${_OPENSSL_ROOT_HINTS_AND_PATHS}
  548. HINTS
  549. ${_OPENSSL_LIBDIR}
  550. ${_OPENSSL_LIBRARY_DIRS}
  551. PATH_SUFFIXES
  552. lib lib64
  553. )
  554. mark_as_advanced(OPENSSL_CRYPTO_LIBRARY OPENSSL_SSL_LIBRARY)
  555. endif()
  556. set(OPENSSL_SSL_LIBRARIES ${OPENSSL_SSL_LIBRARY})
  557. set(OPENSSL_CRYPTO_LIBRARIES ${OPENSSL_CRYPTO_LIBRARY})
  558. set(OPENSSL_LIBRARIES ${OPENSSL_SSL_LIBRARIES} ${OPENSSL_CRYPTO_LIBRARIES} )
  559. _OpenSSL_test_and_find_dependencies("${OPENSSL_SSL_LIBRARY}" "${OPENSSL_CRYPTO_LIBRARY}")
  560. if(_OpenSSL_has_dependencies)
  561. _OpenSSL_add_dependencies( OPENSSL_SSL_LIBRARIES )
  562. _OpenSSL_add_dependencies( OPENSSL_CRYPTO_LIBRARIES )
  563. _OpenSSL_add_dependencies( OPENSSL_LIBRARIES )
  564. endif()
  565. if(OPENSSL_INCLUDE_DIR AND EXISTS "${OPENSSL_INCLUDE_DIR}/openssl/opensslv.h")
  566. file(STRINGS "${OPENSSL_INCLUDE_DIR}/openssl/opensslv.h" openssl_version_str
  567. REGEX "^#[\t ]*define[\t ]+OPENSSL_VERSION_NUMBER[\t ]+0x([0-9a-fA-F])+.*")
  568. if(openssl_version_str)
  569. # The version number is encoded as 0xMNNFFPPS: major minor fix patch status
  570. # The status gives if this is a developer or prerelease and is ignored here.
  571. # Major, minor, and fix directly translate into the version numbers shown in
  572. # the string. The patch field translates to the single character suffix that
  573. # indicates the bug fix state, which 00 -> nothing, 01 -> a, 02 -> b and so
  574. # on.
  575. 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]).*$"
  576. "\\1;\\2;\\3;\\4;\\5" OPENSSL_VERSION_LIST "${openssl_version_str}")
  577. list(GET OPENSSL_VERSION_LIST 0 OPENSSL_VERSION_MAJOR)
  578. list(GET OPENSSL_VERSION_LIST 1 OPENSSL_VERSION_MINOR)
  579. math(EXPR OPENSSL_VERSION_MINOR "0x${OPENSSL_VERSION_MINOR}")
  580. list(GET OPENSSL_VERSION_LIST 2 OPENSSL_VERSION_FIX)
  581. math(EXPR OPENSSL_VERSION_FIX "0x${OPENSSL_VERSION_FIX}")
  582. list(GET OPENSSL_VERSION_LIST 3 OPENSSL_VERSION_PATCH)
  583. if (NOT OPENSSL_VERSION_PATCH STREQUAL "00")
  584. # 96 is the ASCII code of 'a' minus 1
  585. math(EXPR OPENSSL_VERSION_PATCH_ASCII "0x${OPENSSL_VERSION_PATCH} + 96")
  586. # Once anyone knows how OpenSSL would call the patch versions beyond 'z'
  587. # this should be updated to handle that, too. This has not happened yet
  588. # so it is simply ignored here for now.
  589. string(ASCII "${OPENSSL_VERSION_PATCH_ASCII}" OPENSSL_VERSION_PATCH_STRING)
  590. endif ()
  591. set(OpenSSL_VERSION "${OPENSSL_VERSION_MAJOR}.${OPENSSL_VERSION_MINOR}.${OPENSSL_VERSION_FIX}${OPENSSL_VERSION_PATCH_STRING}")
  592. set(OPENSSL_VERSION "${OpenSSL_VERSION}")
  593. else ()
  594. # Since OpenSSL 3.0.0, the new version format is MAJOR.MINOR.PATCH and
  595. # a new OPENSSL_VERSION_STR macro contains exactly that
  596. file(STRINGS "${OPENSSL_INCLUDE_DIR}/openssl/opensslv.h" OPENSSL_VERSION_STR
  597. REGEX "^#[\t ]*define[\t ]+OPENSSL_VERSION_STR[\t ]+\"([0-9])+\\.([0-9])+\\.([0-9])+\".*")
  598. string(REGEX REPLACE "^.*OPENSSL_VERSION_STR[\t ]+\"([0-9]+\\.[0-9]+\\.[0-9]+)\".*$"
  599. "\\1" OPENSSL_VERSION_STR "${OPENSSL_VERSION_STR}")
  600. set(OpenSSL_VERSION "${OPENSSL_VERSION_STR}")
  601. set(OPENSSL_VERSION "${OpenSSL_VERSION}")
  602. # Setting OPENSSL_VERSION_MAJOR OPENSSL_VERSION_MINOR and OPENSSL_VERSION_FIX
  603. string(REGEX MATCHALL "([0-9])+" OPENSSL_VERSION_NUMBER "${OpenSSL_VERSION}")
  604. list(POP_FRONT OPENSSL_VERSION_NUMBER
  605. OPENSSL_VERSION_MAJOR
  606. OPENSSL_VERSION_MINOR
  607. OPENSSL_VERSION_FIX)
  608. unset(OPENSSL_VERSION_NUMBER)
  609. unset(OPENSSL_VERSION_STR)
  610. endif ()
  611. endif ()
  612. foreach(_comp IN LISTS OpenSSL_FIND_COMPONENTS)
  613. if(_comp STREQUAL "Crypto")
  614. if(EXISTS "${OPENSSL_INCLUDE_DIR}" AND
  615. (EXISTS "${OPENSSL_CRYPTO_LIBRARY}" OR
  616. EXISTS "${LIB_EAY_LIBRARY_DEBUG}" OR
  617. EXISTS "${LIB_EAY_LIBRARY_RELEASE}")
  618. )
  619. set(OpenSSL_${_comp}_FOUND TRUE)
  620. else()
  621. set(OpenSSL_${_comp}_FOUND FALSE)
  622. endif()
  623. elseif(_comp STREQUAL "SSL")
  624. if(EXISTS "${OPENSSL_INCLUDE_DIR}" AND
  625. (EXISTS "${OPENSSL_SSL_LIBRARY}" OR
  626. EXISTS "${SSL_EAY_LIBRARY_DEBUG}" OR
  627. EXISTS "${SSL_EAY_LIBRARY_RELEASE}")
  628. )
  629. set(OpenSSL_${_comp}_FOUND TRUE)
  630. else()
  631. set(OpenSSL_${_comp}_FOUND FALSE)
  632. endif()
  633. else()
  634. message(WARNING "${_comp} is not a valid OpenSSL component")
  635. set(OpenSSL_${_comp}_FOUND FALSE)
  636. endif()
  637. endforeach()
  638. unset(_comp)
  639. include(FindPackageHandleStandardArgs)
  640. find_package_handle_standard_args(OpenSSL
  641. REQUIRED_VARS
  642. OPENSSL_CRYPTO_LIBRARY
  643. OPENSSL_INCLUDE_DIR
  644. VERSION_VAR
  645. OpenSSL_VERSION
  646. HANDLE_VERSION_RANGE
  647. HANDLE_COMPONENTS
  648. FAIL_MESSAGE
  649. "Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR"
  650. )
  651. mark_as_advanced(OPENSSL_INCLUDE_DIR)
  652. if(OpenSSL_FOUND)
  653. if(NOT TARGET OpenSSL::Crypto AND
  654. (EXISTS "${OPENSSL_CRYPTO_LIBRARY}" OR
  655. EXISTS "${LIB_EAY_LIBRARY_DEBUG}" OR
  656. EXISTS "${LIB_EAY_LIBRARY_RELEASE}")
  657. )
  658. add_library(OpenSSL::Crypto UNKNOWN IMPORTED)
  659. set_target_properties(OpenSSL::Crypto PROPERTIES
  660. INTERFACE_INCLUDE_DIRECTORIES "${OPENSSL_INCLUDE_DIR}")
  661. if(EXISTS "${OPENSSL_CRYPTO_LIBRARY}")
  662. set_target_properties(OpenSSL::Crypto PROPERTIES
  663. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  664. IMPORTED_LOCATION "${OPENSSL_CRYPTO_LIBRARY}")
  665. endif()
  666. if(EXISTS "${LIB_EAY_LIBRARY_RELEASE}")
  667. set_property(TARGET OpenSSL::Crypto APPEND PROPERTY
  668. IMPORTED_CONFIGURATIONS RELEASE)
  669. set_target_properties(OpenSSL::Crypto PROPERTIES
  670. IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
  671. IMPORTED_LOCATION_RELEASE "${LIB_EAY_LIBRARY_RELEASE}")
  672. endif()
  673. if(EXISTS "${LIB_EAY_LIBRARY_DEBUG}")
  674. set_property(TARGET OpenSSL::Crypto APPEND PROPERTY
  675. IMPORTED_CONFIGURATIONS DEBUG)
  676. set_target_properties(OpenSSL::Crypto PROPERTIES
  677. IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
  678. IMPORTED_LOCATION_DEBUG "${LIB_EAY_LIBRARY_DEBUG}")
  679. endif()
  680. _OpenSSL_target_add_dependencies(OpenSSL::Crypto)
  681. endif()
  682. if(NOT TARGET OpenSSL::SSL AND
  683. (EXISTS "${OPENSSL_SSL_LIBRARY}" OR
  684. EXISTS "${SSL_EAY_LIBRARY_DEBUG}" OR
  685. EXISTS "${SSL_EAY_LIBRARY_RELEASE}")
  686. )
  687. add_library(OpenSSL::SSL UNKNOWN IMPORTED)
  688. set_target_properties(OpenSSL::SSL PROPERTIES
  689. INTERFACE_INCLUDE_DIRECTORIES "${OPENSSL_INCLUDE_DIR}")
  690. if(EXISTS "${OPENSSL_SSL_LIBRARY}")
  691. set_target_properties(OpenSSL::SSL PROPERTIES
  692. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  693. IMPORTED_LOCATION "${OPENSSL_SSL_LIBRARY}")
  694. endif()
  695. if(EXISTS "${SSL_EAY_LIBRARY_RELEASE}")
  696. set_property(TARGET OpenSSL::SSL APPEND PROPERTY
  697. IMPORTED_CONFIGURATIONS RELEASE)
  698. set_target_properties(OpenSSL::SSL PROPERTIES
  699. IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
  700. IMPORTED_LOCATION_RELEASE "${SSL_EAY_LIBRARY_RELEASE}")
  701. endif()
  702. if(EXISTS "${SSL_EAY_LIBRARY_DEBUG}")
  703. set_property(TARGET OpenSSL::SSL APPEND PROPERTY
  704. IMPORTED_CONFIGURATIONS DEBUG)
  705. set_target_properties(OpenSSL::SSL PROPERTIES
  706. IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
  707. IMPORTED_LOCATION_DEBUG "${SSL_EAY_LIBRARY_DEBUG}")
  708. endif()
  709. if(TARGET OpenSSL::Crypto)
  710. set_target_properties(OpenSSL::SSL PROPERTIES
  711. INTERFACE_LINK_LIBRARIES OpenSSL::Crypto)
  712. endif()
  713. _OpenSSL_target_add_dependencies(OpenSSL::SSL)
  714. endif()
  715. if("${OPENSSL_VERSION_MAJOR}.${OPENSSL_VERSION_MINOR}.${OPENSSL_VERSION_FIX}" VERSION_GREATER_EQUAL "0.9.8")
  716. if(MSVC)
  717. if(EXISTS "${OPENSSL_INCLUDE_DIR}")
  718. set(_OPENSSL_applink_paths PATHS ${OPENSSL_INCLUDE_DIR})
  719. endif()
  720. find_file(OPENSSL_APPLINK_SOURCE
  721. NAMES
  722. openssl/applink.c
  723. ${_OPENSSL_applink_paths}
  724. NO_DEFAULT_PATH)
  725. if(OPENSSL_APPLINK_SOURCE)
  726. set(_OPENSSL_applink_interface_srcs ${OPENSSL_APPLINK_SOURCE})
  727. endif()
  728. endif()
  729. if(NOT TARGET OpenSSL::applink)
  730. add_library(OpenSSL::applink INTERFACE IMPORTED)
  731. set_property(TARGET OpenSSL::applink APPEND
  732. PROPERTY INTERFACE_SOURCES
  733. ${_OPENSSL_applink_interface_srcs})
  734. endif()
  735. endif()
  736. endif()
  737. # Restore the original find library ordering
  738. if(OPENSSL_USE_STATIC_LIBS)
  739. set(CMAKE_FIND_LIBRARY_SUFFIXES ${_openssl_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
  740. endif()
  741. unset(_OPENSSL_FIND_PATH_SUFFIX)
  742. unset(_OPENSSL_NAME_POSTFIX)
  743. unset(_OpenSSL_extra_static_deps)
  744. unset(_OpenSSL_has_dependency_dl)
  745. unset(_OpenSSL_has_dependency_threads)
  746. unset(_OpenSSL_has_dependency_zlib)
  747. cmake_policy(POP)