FindCURL.cmake 7.3 KB

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