FindCURL.cmake 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  50. if(NOT CURL_NO_CURL_CMAKE)
  51. # do a find package call to specifically look for the CMake version
  52. # of curl
  53. find_package(CURL QUIET NO_MODULE)
  54. mark_as_advanced(CURL_DIR)
  55. # if we found the CURL cmake package then we are done, and
  56. # can print what we found and return.
  57. if(CURL_FOUND)
  58. find_package_handle_standard_args(CURL HANDLE_COMPONENTS CONFIG_MODE)
  59. # The upstream curl package sets CURL_VERSION, not CURL_VERSION_STRING.
  60. set(CURL_VERSION_STRING "${CURL_VERSION}")
  61. return()
  62. endif()
  63. endif()
  64. find_package(PkgConfig QUIET)
  65. if(PKG_CONFIG_FOUND)
  66. pkg_check_modules(PC_CURL QUIET libcurl)
  67. if(PC_CURL_FOUND)
  68. pkg_get_variable(CURL_SUPPORTED_PROTOCOLS libcurl supported_protocols)
  69. pkg_get_variable(CURL_SUPPORTED_FEATURES libcurl supported_features)
  70. endif()
  71. endif()
  72. # Look for the header file.
  73. find_path(CURL_INCLUDE_DIR
  74. NAMES curl/curl.h
  75. HINTS ${PC_CURL_INCLUDE_DIRS})
  76. mark_as_advanced(CURL_INCLUDE_DIR)
  77. if(NOT CURL_LIBRARY)
  78. # Look for the library (sorted from most current/relevant entry to least).
  79. find_library(CURL_LIBRARY_RELEASE NAMES
  80. curl
  81. # Windows MSVC prebuilts:
  82. curllib
  83. libcurl_imp
  84. curllib_static
  85. # Windows older "Win32 - MSVC" prebuilts (libcurl.lib, e.g. libcurl-7.15.5-win32-msvc.zip):
  86. libcurl
  87. NAMES_PER_DIR
  88. HINTS ${PC_CURL_LIBRARY_DIRS}
  89. )
  90. mark_as_advanced(CURL_LIBRARY_RELEASE)
  91. find_library(CURL_LIBRARY_DEBUG NAMES
  92. # Windows MSVC CMake builds in debug configuration on vcpkg:
  93. libcurl-d_imp
  94. libcurl-d
  95. NAMES_PER_DIR
  96. HINTS ${PC_CURL_LIBRARY_DIRS}
  97. )
  98. mark_as_advanced(CURL_LIBRARY_DEBUG)
  99. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  100. select_library_configurations(CURL)
  101. endif()
  102. if(CURL_INCLUDE_DIR)
  103. foreach(_curl_version_header curlver.h curl.h)
  104. if(EXISTS "${CURL_INCLUDE_DIR}/curl/${_curl_version_header}")
  105. file(STRINGS "${CURL_INCLUDE_DIR}/curl/${_curl_version_header}" curl_version_str REGEX "^#define[\t ]+LIBCURL_VERSION[\t ]+\".*\"")
  106. string(REGEX REPLACE "^#define[\t ]+LIBCURL_VERSION[\t ]+\"([^\"]*)\".*" "\\1" CURL_VERSION_STRING "${curl_version_str}")
  107. unset(curl_version_str)
  108. break()
  109. endif()
  110. endforeach()
  111. endif()
  112. if(CURL_FIND_COMPONENTS)
  113. 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)
  114. set(CURL_KNOWN_FEATURES SSL IPv6 UnixSockets libz AsynchDNS IDN GSS-API PSL SPNEGO Kerberos NTLM NTLM_WB TLS-SRP HTTP2 HTTPS-proxy)
  115. foreach(component IN LISTS CURL_KNOWN_PROTOCOLS CURL_KNOWN_FEATURES)
  116. set(CURL_${component}_FOUND FALSE)
  117. endforeach()
  118. if(NOT PC_CURL_FOUND)
  119. find_program(CURL_CONFIG_EXECUTABLE NAMES curl-config)
  120. if(CURL_CONFIG_EXECUTABLE)
  121. execute_process(COMMAND ${CURL_CONFIG_EXECUTABLE} --version
  122. OUTPUT_VARIABLE CURL_CONFIG_VERSION_STRING
  123. ERROR_QUIET
  124. OUTPUT_STRIP_TRAILING_WHITESPACE)
  125. execute_process(COMMAND ${CURL_CONFIG_EXECUTABLE} --feature
  126. OUTPUT_VARIABLE CURL_CONFIG_FEATURES_STRING
  127. ERROR_QUIET
  128. OUTPUT_STRIP_TRAILING_WHITESPACE)
  129. string(REPLACE "\n" ";" CURL_SUPPORTED_FEATURES "${CURL_CONFIG_FEATURES_STRING}")
  130. execute_process(COMMAND ${CURL_CONFIG_EXECUTABLE} --protocols
  131. OUTPUT_VARIABLE CURL_CONFIG_PROTOCOLS_STRING
  132. ERROR_QUIET
  133. OUTPUT_STRIP_TRAILING_WHITESPACE)
  134. string(REPLACE "\n" ";" CURL_SUPPORTED_PROTOCOLS "${CURL_CONFIG_PROTOCOLS_STRING}")
  135. endif()
  136. endif()
  137. foreach(component IN LISTS CURL_FIND_COMPONENTS)
  138. list(FIND CURL_KNOWN_PROTOCOLS ${component} _found)
  139. if(NOT _found EQUAL -1)
  140. list(FIND CURL_SUPPORTED_PROTOCOLS ${component} _found)
  141. if(NOT _found EQUAL -1)
  142. set(CURL_${component}_FOUND TRUE)
  143. elseif(CURL_FIND_REQUIRED)
  144. message(FATAL_ERROR "CURL: Required protocol ${component} is not found")
  145. endif()
  146. else()
  147. list(FIND CURL_SUPPORTED_FEATURES ${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 feature ${component} is not found")
  152. endif()
  153. endif()
  154. endforeach()
  155. endif()
  156. find_package_handle_standard_args(CURL
  157. REQUIRED_VARS CURL_LIBRARY CURL_INCLUDE_DIR
  158. VERSION_VAR CURL_VERSION_STRING
  159. HANDLE_COMPONENTS)
  160. if(CURL_FOUND)
  161. set(CURL_LIBRARIES ${CURL_LIBRARY})
  162. set(CURL_INCLUDE_DIRS ${CURL_INCLUDE_DIR})
  163. if(NOT TARGET CURL::libcurl)
  164. add_library(CURL::libcurl UNKNOWN IMPORTED)
  165. set_target_properties(CURL::libcurl PROPERTIES
  166. INTERFACE_INCLUDE_DIRECTORIES "${CURL_INCLUDE_DIRS}")
  167. if(CURL_USE_STATIC_LIBS)
  168. set_property(TARGET CURL::libcurl APPEND PROPERTY
  169. INTERFACE_COMPILE_DEFINITIONS "CURL_STATICLIB")
  170. endif()
  171. if(EXISTS "${CURL_LIBRARY}")
  172. set_target_properties(CURL::libcurl PROPERTIES
  173. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  174. IMPORTED_LOCATION "${CURL_LIBRARY}")
  175. endif()
  176. if(CURL_LIBRARY_RELEASE)
  177. set_property(TARGET CURL::libcurl APPEND PROPERTY
  178. IMPORTED_CONFIGURATIONS RELEASE)
  179. set_target_properties(CURL::libcurl PROPERTIES
  180. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  181. IMPORTED_LOCATION_RELEASE "${CURL_LIBRARY_RELEASE}")
  182. endif()
  183. if(CURL_LIBRARY_DEBUG)
  184. set_property(TARGET CURL::libcurl APPEND PROPERTY
  185. IMPORTED_CONFIGURATIONS DEBUG)
  186. set_target_properties(CURL::libcurl PROPERTIES
  187. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  188. IMPORTED_LOCATION_DEBUG "${CURL_LIBRARY_DEBUG}")
  189. endif()
  190. if(CURL_USE_STATIC_LIBS AND MSVC)
  191. set_target_properties(CURL::libcurl PROPERTIES
  192. INTERFACE_LINK_LIBRARIES "normaliz.lib;ws2_32.lib;wldap32.lib")
  193. endif()
  194. endif()
  195. endif()