FindCURL.cmake 7.0 KB

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