FindOpenSSL.cmake 29 KB

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