1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
8 Find the native CURL headers and libraries.
10 .. versionadded:: 3.14
11 This module accept optional COMPONENTS to check supported features and
16 PROTOCOLS: ICT FILE FTP FTPS GOPHER HTTP HTTPS IMAP IMAPS LDAP LDAPS POP3
17 POP3S RTMP RTSP SCP SFTP SMB SMBS SMTP SMTPS TELNET TFTP
18 FEATURES: SSL IPv6 UnixSockets libz AsynchDNS IDN GSS-API PSL SPNEGO
19 Kerberos NTLM NTLM_WB TLS-SRP HTTP2 HTTPS-proxy
24 .. versionadded:: 3.12
26 This module defines :prop_tgt:`IMPORTED` target ``CURL::libcurl``, if
32 This module defines the following variables:
35 "True" if ``curl`` found.
38 where to find ``curl``/``curl.h``, etc.
41 List of libraries when using ``curl``.
43 ``CURL_VERSION_STRING``
44 The version of ``curl`` found.
46 .. versionadded:: 3.13
47 Debug and Release variants are found separately.
52 .. versionadded:: 3.17
54 If CURL was built using the CMake buildsystem then it provides its own
55 ``CURLConfig.cmake`` file for use with the :command:`find_package` command's
56 config mode. This module looks for this file and, if found,
57 returns its results with no further action.
59 Set ``CURL_NO_CURL_CMAKE`` to ``ON`` to disable this search.
64 ``CURL_USE_STATIC_LIBS``
66 .. versionadded:: 3.28
68 Set to ``TRUE`` to use static libraries.
70 This is meaningful only when CURL is not found via its
71 CMake Package Configuration file.
73 #]=======================================================================]
76 cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
78 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
80 if(NOT CURL_NO_CURL_CMAKE)
81 # do a find package call to specifically look for the CMake version
83 find_package(CURL QUIET NO_MODULE)
84 mark_as_advanced(CURL_DIR)
86 # if we found the CURL cmake package then we are done, and
87 # can print what we found and return.
89 find_package_handle_standard_args(CURL HANDLE_COMPONENTS CONFIG_MODE)
90 # The upstream curl package sets CURL_VERSION, not CURL_VERSION_STRING.
91 set(CURL_VERSION_STRING "${CURL_VERSION}")
98 find_package(PkgConfig QUIET)
100 pkg_check_modules(PC_CURL QUIET libcurl)
102 pkg_get_variable(CURL_SUPPORTED_PROTOCOLS_STRING libcurl supported_protocols)
103 string(REPLACE " " ";" CURL_SUPPORTED_PROTOCOLS "${CURL_SUPPORTED_PROTOCOLS_STRING}")
104 pkg_get_variable(CURL_SUPPORTED_FEATURES_STRING libcurl supported_features)
105 string(REPLACE " " ";" CURL_SUPPORTED_FEATURES "${CURL_SUPPORTED_FEATURES_STRING}")
109 # Look for the header file.
110 find_path(CURL_INCLUDE_DIR
112 HINTS ${PC_CURL_INCLUDE_DIRS})
113 mark_as_advanced(CURL_INCLUDE_DIR)
116 # Look for the library (sorted from most current/relevant entry to least).
117 find_library(CURL_LIBRARY_RELEASE NAMES
119 # Windows MSVC prebuilts:
123 # Windows older "Win32 - MSVC" prebuilts (libcurl.lib, e.g. libcurl-7.15.5-win32-msvc.zip):
125 # Some Windows prebuilt versions distribute `libcurl_a.lib` instead of `libcurl.lib`
128 HINTS ${PC_CURL_LIBRARY_DIRS}
130 mark_as_advanced(CURL_LIBRARY_RELEASE)
132 find_library(CURL_LIBRARY_DEBUG NAMES
133 # Windows MSVC CMake builds in debug configuration on vcpkg:
137 HINTS ${PC_CURL_LIBRARY_DIRS}
139 mark_as_advanced(CURL_LIBRARY_DEBUG)
141 include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
142 select_library_configurations(CURL)
146 foreach(_curl_version_header curlver.h curl.h)
147 if(EXISTS "${CURL_INCLUDE_DIR}/curl/${_curl_version_header}")
148 file(STRINGS "${CURL_INCLUDE_DIR}/curl/${_curl_version_header}" curl_version_str REGEX "^#define[\t ]+LIBCURL_VERSION[\t ]+\".*\"")
150 string(REGEX REPLACE "^#define[\t ]+LIBCURL_VERSION[\t ]+\"([^\"]*)\".*" "\\1" CURL_VERSION_STRING "${curl_version_str}")
151 unset(curl_version_str)
157 if(CURL_FIND_COMPONENTS)
158 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)
159 set(CURL_KNOWN_FEATURES SSL IPv6 UnixSockets libz AsynchDNS IDN GSS-API PSL SPNEGO Kerberos NTLM NTLM_WB TLS-SRP HTTP2 HTTPS-proxy)
160 foreach(component IN LISTS CURL_KNOWN_PROTOCOLS CURL_KNOWN_FEATURES)
161 set(CURL_${component}_FOUND FALSE)
163 if(NOT PC_CURL_FOUND)
164 find_program(CURL_CONFIG_EXECUTABLE NAMES curl-config)
165 if(CURL_CONFIG_EXECUTABLE)
166 execute_process(COMMAND ${CURL_CONFIG_EXECUTABLE} --version
167 OUTPUT_VARIABLE CURL_CONFIG_VERSION_STRING
169 OUTPUT_STRIP_TRAILING_WHITESPACE)
170 execute_process(COMMAND ${CURL_CONFIG_EXECUTABLE} --feature
171 OUTPUT_VARIABLE CURL_CONFIG_FEATURES_STRING
173 OUTPUT_STRIP_TRAILING_WHITESPACE)
174 string(REPLACE "\n" ";" CURL_SUPPORTED_FEATURES "${CURL_CONFIG_FEATURES_STRING}")
175 execute_process(COMMAND ${CURL_CONFIG_EXECUTABLE} --protocols
176 OUTPUT_VARIABLE CURL_CONFIG_PROTOCOLS_STRING
178 OUTPUT_STRIP_TRAILING_WHITESPACE)
179 string(REPLACE "\n" ";" CURL_SUPPORTED_PROTOCOLS "${CURL_CONFIG_PROTOCOLS_STRING}")
183 foreach(component IN LISTS CURL_FIND_COMPONENTS)
184 list(FIND CURL_KNOWN_PROTOCOLS ${component} _found)
185 if(NOT _found EQUAL -1)
186 list(FIND CURL_SUPPORTED_PROTOCOLS ${component} _found)
187 if(NOT _found EQUAL -1)
188 set(CURL_${component}_FOUND TRUE)
189 elseif(CURL_FIND_REQUIRED)
190 message(FATAL_ERROR "CURL: Required protocol ${component} is not found")
193 list(FIND CURL_SUPPORTED_FEATURES ${component} _found)
194 if(NOT _found EQUAL -1)
195 set(CURL_${component}_FOUND TRUE)
196 elseif(CURL_FIND_REQUIRED)
197 message(FATAL_ERROR "CURL: Required feature ${component} is not found")
203 find_package_handle_standard_args(CURL
204 REQUIRED_VARS CURL_LIBRARY CURL_INCLUDE_DIR
205 VERSION_VAR CURL_VERSION_STRING
209 set(CURL_LIBRARIES ${CURL_LIBRARY})
210 set(CURL_INCLUDE_DIRS ${CURL_INCLUDE_DIR})
212 if(NOT TARGET CURL::libcurl)
213 add_library(CURL::libcurl UNKNOWN IMPORTED)
214 set_target_properties(CURL::libcurl PROPERTIES
215 INTERFACE_INCLUDE_DIRECTORIES "${CURL_INCLUDE_DIRS}")
217 if(CURL_USE_STATIC_LIBS)
218 set_property(TARGET CURL::libcurl APPEND PROPERTY
219 INTERFACE_COMPILE_DEFINITIONS "CURL_STATICLIB")
222 if(EXISTS "${CURL_LIBRARY}")
223 set_target_properties(CURL::libcurl PROPERTIES
224 IMPORTED_LINK_INTERFACE_LANGUAGES "C"
225 IMPORTED_LOCATION "${CURL_LIBRARY}")
227 if(CURL_LIBRARY_RELEASE)
228 set_property(TARGET CURL::libcurl APPEND PROPERTY
229 IMPORTED_CONFIGURATIONS RELEASE)
230 set_target_properties(CURL::libcurl PROPERTIES
231 IMPORTED_LINK_INTERFACE_LANGUAGES "C"
232 IMPORTED_LOCATION_RELEASE "${CURL_LIBRARY_RELEASE}")
234 if(CURL_LIBRARY_DEBUG)
235 set_property(TARGET CURL::libcurl APPEND PROPERTY
236 IMPORTED_CONFIGURATIONS DEBUG)
237 set_target_properties(CURL::libcurl PROPERTIES
238 IMPORTED_LINK_INTERFACE_LANGUAGES "C"
239 IMPORTED_LOCATION_DEBUG "${CURL_LIBRARY_DEBUG}")
242 if(CURL_USE_STATIC_LIBS AND MSVC)
243 set_target_properties(CURL::libcurl PROPERTIES
244 INTERFACE_LINK_LIBRARIES "normaliz.lib;ws2_32.lib;wldap32.lib")