FindCURL.cmake 6.3 KB

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