Merge topic 'cxx-checks-tolerate-unused-arguments'
[kiteware-cmake.git] / Modules / FindCURL.cmake
blob5ce8a9046bbfca861700b93bdadee194ec70e7bc
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:
5 FindCURL
6 --------
8 Find the native CURL headers and libraries.
10 .. versionadded:: 3.14
11   This module accept optional COMPONENTS to check supported features and
12   protocols:
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
21 IMPORTED Targets
22 ^^^^^^^^^^^^^^^^
24 .. versionadded:: 3.12
26 This module defines :prop_tgt:`IMPORTED` target ``CURL::libcurl``, if
27 curl has been found.
29 Result Variables
30 ^^^^^^^^^^^^^^^^
32 This module defines the following variables:
34 ``CURL_FOUND``
35   "True" if ``curl`` found.
37 ``CURL_INCLUDE_DIRS``
38   where to find ``curl``/``curl.h``, etc.
40 ``CURL_LIBRARIES``
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.
49 CURL CMake
50 ^^^^^^^^^^
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.
61 Hints
62 ^^^^^
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 #]=======================================================================]
75 cmake_policy(PUSH)
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
82   # of curl
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.
88   if(CURL_FOUND)
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}")
93     cmake_policy(POP)
94     return()
95   endif()
96 endif()
98 find_package(PkgConfig QUIET)
99 if(PKG_CONFIG_FOUND)
100   pkg_check_modules(PC_CURL QUIET libcurl)
101   if(PC_CURL_FOUND)
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}")
106   endif()
107 endif()
109 # Look for the header file.
110 find_path(CURL_INCLUDE_DIR
111           NAMES curl/curl.h
112           HINTS ${PC_CURL_INCLUDE_DIRS})
113 mark_as_advanced(CURL_INCLUDE_DIR)
115 if(NOT CURL_LIBRARY)
116   # Look for the library (sorted from most current/relevant entry to least).
117   find_library(CURL_LIBRARY_RELEASE NAMES
118       curl
119     # Windows MSVC prebuilts:
120       curllib
121       libcurl_imp
122       curllib_static
123     # Windows older "Win32 - MSVC" prebuilts (libcurl.lib, e.g. libcurl-7.15.5-win32-msvc.zip):
124       libcurl
125     # Some Windows prebuilt versions distribute `libcurl_a.lib` instead of `libcurl.lib`
126       libcurl_a
127       NAMES_PER_DIR
128       HINTS ${PC_CURL_LIBRARY_DIRS}
129   )
130   mark_as_advanced(CURL_LIBRARY_RELEASE)
132   find_library(CURL_LIBRARY_DEBUG NAMES
133     # Windows MSVC CMake builds in debug configuration on vcpkg:
134       libcurl-d_imp
135       libcurl-d
136       NAMES_PER_DIR
137       HINTS ${PC_CURL_LIBRARY_DIRS}
138   )
139   mark_as_advanced(CURL_LIBRARY_DEBUG)
141   include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
142   select_library_configurations(CURL)
143 endif()
145 if(CURL_INCLUDE_DIR)
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)
152       break()
153     endif()
154   endforeach()
155 endif()
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)
162   endforeach()
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
168                       ERROR_QUIET
169                       OUTPUT_STRIP_TRAILING_WHITESPACE)
170       execute_process(COMMAND ${CURL_CONFIG_EXECUTABLE} --feature
171                       OUTPUT_VARIABLE CURL_CONFIG_FEATURES_STRING
172                       ERROR_QUIET
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
177                       ERROR_QUIET
178                       OUTPUT_STRIP_TRAILING_WHITESPACE)
179       string(REPLACE "\n" ";" CURL_SUPPORTED_PROTOCOLS "${CURL_CONFIG_PROTOCOLS_STRING}")
180     endif()
182   endif()
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")
191       endif()
192     else()
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")
198       endif()
199     endif()
200   endforeach()
201 endif()
203 find_package_handle_standard_args(CURL
204                                   REQUIRED_VARS CURL_LIBRARY CURL_INCLUDE_DIR
205                                   VERSION_VAR CURL_VERSION_STRING
206                                   HANDLE_COMPONENTS)
208 if(CURL_FOUND)
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")
220     endif()
222     if(EXISTS "${CURL_LIBRARY}")
223       set_target_properties(CURL::libcurl PROPERTIES
224         IMPORTED_LINK_INTERFACE_LANGUAGES "C"
225         IMPORTED_LOCATION "${CURL_LIBRARY}")
226     endif()
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}")
233     endif()
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}")
240     endif()
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")
245     endif()
247   endif()
248 endif()
250 cmake_policy(POP)