FindCURL.cmake 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. FindCURL
  5. --------
  6. Find the native CURL headers and libraries.
  7. .. versionadded:: 3.14
  8. This module accept optional COMPONENTS to check supported features and
  9. protocols:
  10. ::
  11. PROTOCOLS: ICT FILE FTP FTPS GOPHER HTTP HTTPS IMAP IMAPS LDAP LDAPS POP3
  12. POP3S RTMP RTSP SCP SFTP SMB SMBS SMTP SMTPS TELNET TFTP
  13. FEATURES: SSL IPv6 UnixSockets libz AsynchDNS IDN GSS-API PSL SPNEGO
  14. Kerberos NTLM NTLM_WB TLS-SRP HTTP2 HTTPS-proxy
  15. IMPORTED Targets
  16. ^^^^^^^^^^^^^^^^
  17. .. versionadded:: 3.12
  18. This module defines :prop_tgt:`IMPORTED` target ``CURL::libcurl``, if
  19. curl has been found.
  20. Result Variables
  21. ^^^^^^^^^^^^^^^^
  22. This module defines the following variables:
  23. ``CURL_FOUND``
  24. "True" if ``curl`` found.
  25. ``CURL_INCLUDE_DIRS``
  26. where to find ``curl``/``curl.h``, etc.
  27. ``CURL_LIBRARIES``
  28. List of libraries when using ``curl``.
  29. ``CURL_VERSION_STRING``
  30. The version of ``curl`` found.
  31. .. versionadded:: 3.13
  32. Debug and Release variants are found separately.
  33. CURL CMake
  34. ^^^^^^^^^^
  35. .. versionadded:: 3.17
  36. If CURL was built using the CMake buildsystem then it provides its own
  37. ``CURLConfig.cmake`` file for use with the :command:`find_package` command's
  38. config mode. This module looks for this file and, if found,
  39. returns its results with no further action.
  40. Set ``CURL_NO_CURL_CMAKE`` to ``ON`` to disable this search.
  41. Hints
  42. ^^^^^
  43. ``CURL_USE_STATIC_LIBS``
  44. .. versionadded:: 3.28
  45. Set to ``TRUE`` to use static libraries.
  46. This is meaningful only when CURL is not found via its
  47. CMake Package Configuration file.
  48. #]=======================================================================]
  49. cmake_policy(PUSH)
  50. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  51. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  52. if(NOT CURL_NO_CURL_CMAKE)
  53. # do a find package call to specifically look for the CMake version
  54. # of curl
  55. find_package(CURL QUIET NO_MODULE)
  56. mark_as_advanced(CURL_DIR)
  57. # if we found the CURL cmake package then we are done, and
  58. # can print what we found and return.
  59. if(CURL_FOUND)
  60. find_package_handle_standard_args(CURL HANDLE_COMPONENTS CONFIG_MODE)
  61. # The upstream curl package sets CURL_VERSION, not CURL_VERSION_STRING.
  62. set(CURL_VERSION_STRING "${CURL_VERSION}")
  63. cmake_policy(POP)
  64. return()
  65. endif()
  66. endif()
  67. find_package(PkgConfig QUIET)
  68. if(PKG_CONFIG_FOUND)
  69. pkg_check_modules(PC_CURL QUIET libcurl)
  70. if(PC_CURL_FOUND)
  71. pkg_get_variable(CURL_SUPPORTED_PROTOCOLS_STRING libcurl supported_protocols)
  72. string(REPLACE " " ";" CURL_SUPPORTED_PROTOCOLS "${CURL_SUPPORTED_PROTOCOLS_STRING}")
  73. pkg_get_variable(CURL_SUPPORTED_FEATURES_STRING libcurl supported_features)
  74. string(REPLACE " " ";" CURL_SUPPORTED_FEATURES "${CURL_SUPPORTED_FEATURES_STRING}")
  75. endif()
  76. endif()
  77. # Look for the header file.
  78. find_path(CURL_INCLUDE_DIR
  79. NAMES curl/curl.h
  80. HINTS ${PC_CURL_INCLUDE_DIRS})
  81. mark_as_advanced(CURL_INCLUDE_DIR)
  82. if(NOT CURL_LIBRARY)
  83. # Look for the library (sorted from most current/relevant entry to least).
  84. find_library(CURL_LIBRARY_RELEASE NAMES
  85. curl
  86. # Windows MSVC prebuilts:
  87. curllib
  88. libcurl_imp
  89. curllib_static
  90. # Windows older "Win32 - MSVC" prebuilts (libcurl.lib, e.g. libcurl-7.15.5-win32-msvc.zip):
  91. libcurl
  92. # Some Windows prebuilt versions distribute `libcurl_a.lib` instead of `libcurl.lib`
  93. libcurl_a
  94. NAMES_PER_DIR
  95. HINTS ${PC_CURL_LIBRARY_DIRS}
  96. )
  97. mark_as_advanced(CURL_LIBRARY_RELEASE)
  98. find_library(CURL_LIBRARY_DEBUG NAMES
  99. # Windows MSVC CMake builds in debug configuration on vcpkg:
  100. libcurl-d_imp
  101. libcurl-d
  102. NAMES_PER_DIR
  103. HINTS ${PC_CURL_LIBRARY_DIRS}
  104. )
  105. mark_as_advanced(CURL_LIBRARY_DEBUG)
  106. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  107. select_library_configurations(CURL)
  108. endif()
  109. if(CURL_INCLUDE_DIR)
  110. foreach(_curl_version_header curlver.h curl.h)
  111. if(EXISTS "${CURL_INCLUDE_DIR}/curl/${_curl_version_header}")
  112. file(STRINGS "${CURL_INCLUDE_DIR}/curl/${_curl_version_header}" curl_version_str REGEX "^#define[\t ]+LIBCURL_VERSION[\t ]+\".*\"")
  113. string(REGEX REPLACE "^#define[\t ]+LIBCURL_VERSION[\t ]+\"([^\"]*)\".*" "\\1" CURL_VERSION_STRING "${curl_version_str}")
  114. unset(curl_version_str)
  115. break()
  116. endif()
  117. endforeach()
  118. endif()
  119. if(CURL_FIND_COMPONENTS)
  120. set(CURL_KNOWN_PROTOCOLS ICT FILE FTP FTPS GOPHER HTTP HTTPS IMAP IMAPS LDAP LDAPS POP3 POP3S RTMP RTSP SCP SFTP SMB SMBS SMTP SMTPS TELNET TFTP)
  121. set(CURL_KNOWN_FEATURES SSL IPv6 UnixSockets libz AsynchDNS IDN GSS-API PSL SPNEGO Kerberos NTLM NTLM_WB TLS-SRP HTTP2 HTTPS-proxy)
  122. foreach(component IN LISTS CURL_KNOWN_PROTOCOLS CURL_KNOWN_FEATURES)
  123. set(CURL_${component}_FOUND FALSE)
  124. endforeach()
  125. if(NOT PC_CURL_FOUND)
  126. find_program(CURL_CONFIG_EXECUTABLE NAMES curl-config)
  127. if(CURL_CONFIG_EXECUTABLE)
  128. execute_process(COMMAND ${CURL_CONFIG_EXECUTABLE} --version
  129. OUTPUT_VARIABLE CURL_CONFIG_VERSION_STRING
  130. ERROR_QUIET
  131. OUTPUT_STRIP_TRAILING_WHITESPACE)
  132. execute_process(COMMAND ${CURL_CONFIG_EXECUTABLE} --feature
  133. OUTPUT_VARIABLE CURL_CONFIG_FEATURES_STRING
  134. ERROR_QUIET
  135. OUTPUT_STRIP_TRAILING_WHITESPACE)
  136. string(REPLACE "\n" ";" CURL_SUPPORTED_FEATURES "${CURL_CONFIG_FEATURES_STRING}")
  137. execute_process(COMMAND ${CURL_CONFIG_EXECUTABLE} --protocols
  138. OUTPUT_VARIABLE CURL_CONFIG_PROTOCOLS_STRING
  139. ERROR_QUIET
  140. OUTPUT_STRIP_TRAILING_WHITESPACE)
  141. string(REPLACE "\n" ";" CURL_SUPPORTED_PROTOCOLS "${CURL_CONFIG_PROTOCOLS_STRING}")
  142. endif()
  143. endif()
  144. foreach(component IN LISTS CURL_FIND_COMPONENTS)
  145. list(FIND CURL_KNOWN_PROTOCOLS ${component} _found)
  146. if(NOT _found EQUAL -1)
  147. list(FIND CURL_SUPPORTED_PROTOCOLS ${component} _found)
  148. if(NOT _found EQUAL -1)
  149. set(CURL_${component}_FOUND TRUE)
  150. elseif(CURL_FIND_REQUIRED)
  151. message(FATAL_ERROR "CURL: Required protocol ${component} is not found")
  152. endif()
  153. else()
  154. list(FIND CURL_SUPPORTED_FEATURES ${component} _found)
  155. if(NOT _found EQUAL -1)
  156. set(CURL_${component}_FOUND TRUE)
  157. elseif(CURL_FIND_REQUIRED)
  158. message(FATAL_ERROR "CURL: Required feature ${component} is not found")
  159. endif()
  160. endif()
  161. endforeach()
  162. endif()
  163. find_package_handle_standard_args(CURL
  164. REQUIRED_VARS CURL_LIBRARY CURL_INCLUDE_DIR
  165. VERSION_VAR CURL_VERSION_STRING
  166. HANDLE_COMPONENTS)
  167. if(CURL_FOUND)
  168. set(CURL_LIBRARIES ${CURL_LIBRARY})
  169. set(CURL_INCLUDE_DIRS ${CURL_INCLUDE_DIR})
  170. if(NOT TARGET CURL::libcurl)
  171. add_library(CURL::libcurl UNKNOWN IMPORTED)
  172. set_target_properties(CURL::libcurl PROPERTIES
  173. INTERFACE_INCLUDE_DIRECTORIES "${CURL_INCLUDE_DIRS}")
  174. if(CURL_USE_STATIC_LIBS)
  175. set_property(TARGET CURL::libcurl APPEND PROPERTY
  176. INTERFACE_COMPILE_DEFINITIONS "CURL_STATICLIB")
  177. endif()
  178. if(EXISTS "${CURL_LIBRARY}")
  179. set_target_properties(CURL::libcurl PROPERTIES
  180. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  181. IMPORTED_LOCATION "${CURL_LIBRARY}")
  182. endif()
  183. if(CURL_LIBRARY_RELEASE)
  184. set_property(TARGET CURL::libcurl APPEND PROPERTY
  185. IMPORTED_CONFIGURATIONS RELEASE)
  186. set_target_properties(CURL::libcurl PROPERTIES
  187. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  188. IMPORTED_LOCATION_RELEASE "${CURL_LIBRARY_RELEASE}")
  189. endif()
  190. if(CURL_LIBRARY_DEBUG)
  191. set_property(TARGET CURL::libcurl APPEND PROPERTY
  192. IMPORTED_CONFIGURATIONS DEBUG)
  193. set_target_properties(CURL::libcurl PROPERTIES
  194. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  195. IMPORTED_LOCATION_DEBUG "${CURL_LIBRARY_DEBUG}")
  196. endif()
  197. if(CURL_USE_STATIC_LIBS AND MSVC)
  198. set_target_properties(CURL::libcurl PROPERTIES
  199. INTERFACE_LINK_LIBRARIES "normaliz.lib;ws2_32.lib;wldap32.lib")
  200. endif()
  201. endif()
  202. endif()
  203. cmake_policy(POP)