Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Modules / FindCURL.cmake
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3
4 #[=======================================================================[.rst:
5 FindCURL
6 --------
7
8 Find the native CURL headers and libraries.
9
10 .. versionadded:: 3.14
11   This module accept optional COMPONENTS to check supported features and
12   protocols:
13
14 ::
15
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
20
21 IMPORTED Targets
22 ^^^^^^^^^^^^^^^^
23
24 .. versionadded:: 3.12
25
26 This module defines :prop_tgt:`IMPORTED` target ``CURL::libcurl``, if
27 curl has been found.
28
29 Result Variables
30 ^^^^^^^^^^^^^^^^
31
32 This module defines the following variables:
33
34 ``CURL_FOUND``
35   "True" if ``curl`` found.
36
37 ``CURL_INCLUDE_DIRS``
38   where to find ``curl``/``curl.h``, etc.
39
40 ``CURL_LIBRARIES``
41   List of libraries when using ``curl``.
42
43 ``CURL_VERSION_STRING``
44   The version of ``curl`` found.
45
46 .. versionadded:: 3.13
47   Debug and Release variants are found separately.
48
49 CURL CMake
50 ^^^^^^^^^^
51
52 .. versionadded:: 3.17
53
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.
58
59 Set ``CURL_NO_CURL_CMAKE`` to ``ON`` to disable this search.
60
61 #]=======================================================================]
62
63 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
64
65 if(NOT CURL_NO_CURL_CMAKE)
66   # do a find package call to specifically look for the CMake version
67   # of curl
68   find_package(CURL QUIET NO_MODULE)
69   mark_as_advanced(CURL_DIR)
70
71   # if we found the CURL cmake package then we are done, and
72   # can print what we found and return.
73   if(CURL_FOUND)
74     find_package_handle_standard_args(CURL HANDLE_COMPONENTS CONFIG_MODE)
75     # The upstream curl package sets CURL_VERSION, not CURL_VERSION_STRING.
76     set(CURL_VERSION_STRING "${CURL_VERSION}")
77     return()
78   endif()
79 endif()
80
81 find_package(PkgConfig QUIET)
82 if(PKG_CONFIG_FOUND)
83   pkg_check_modules(PC_CURL QUIET libcurl)
84   if(PC_CURL_FOUND)
85     pkg_get_variable(CURL_SUPPORTED_PROTOCOLS libcurl supported_protocols)
86     pkg_get_variable(CURL_SUPPORTED_FEATURES libcurl supported_features)
87   endif()
88 endif()
89
90 # Look for the header file.
91 find_path(CURL_INCLUDE_DIR
92           NAMES curl/curl.h
93           HINTS ${PC_CURL_INCLUDE_DIRS})
94 mark_as_advanced(CURL_INCLUDE_DIR)
95
96 if(NOT CURL_LIBRARY)
97   # Look for the library (sorted from most current/relevant entry to least).
98   find_library(CURL_LIBRARY_RELEASE NAMES
99       curl
100     # Windows MSVC prebuilts:
101       curllib
102       libcurl_imp
103       curllib_static
104     # Windows older "Win32 - MSVC" prebuilts (libcurl.lib, e.g. libcurl-7.15.5-win32-msvc.zip):
105       libcurl
106       NAMES_PER_DIR
107       HINTS ${PC_CURL_LIBRARY_DIRS}
108   )
109   mark_as_advanced(CURL_LIBRARY_RELEASE)
110
111   find_library(CURL_LIBRARY_DEBUG NAMES
112     # Windows MSVC CMake builds in debug configuration on vcpkg:
113       libcurl-d_imp
114       libcurl-d
115       NAMES_PER_DIR
116       HINTS ${PC_CURL_LIBRARY_DIRS}
117   )
118   mark_as_advanced(CURL_LIBRARY_DEBUG)
119
120   include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
121   select_library_configurations(CURL)
122 endif()
123
124 if(CURL_INCLUDE_DIR)
125   foreach(_curl_version_header curlver.h curl.h)
126     if(EXISTS "${CURL_INCLUDE_DIR}/curl/${_curl_version_header}")
127       file(STRINGS "${CURL_INCLUDE_DIR}/curl/${_curl_version_header}" curl_version_str REGEX "^#define[\t ]+LIBCURL_VERSION[\t ]+\".*\"")
128
129       string(REGEX REPLACE "^#define[\t ]+LIBCURL_VERSION[\t ]+\"([^\"]*)\".*" "\\1" CURL_VERSION_STRING "${curl_version_str}")
130       unset(curl_version_str)
131       break()
132     endif()
133   endforeach()
134 endif()
135
136 if(CURL_FIND_COMPONENTS)
137   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)
138   set(CURL_KNOWN_FEATURES  SSL IPv6 UnixSockets libz AsynchDNS IDN GSS-API PSL SPNEGO Kerberos NTLM NTLM_WB TLS-SRP HTTP2 HTTPS-proxy)
139   foreach(component IN LISTS CURL_KNOWN_PROTOCOLS CURL_KNOWN_FEATURES)
140     set(CURL_${component}_FOUND FALSE)
141   endforeach()
142   if(NOT PC_CURL_FOUND)
143     find_program(CURL_CONFIG_EXECUTABLE NAMES curl-config)
144     if(CURL_CONFIG_EXECUTABLE)
145       execute_process(COMMAND ${CURL_CONFIG_EXECUTABLE} --version
146                       OUTPUT_VARIABLE CURL_CONFIG_VERSION_STRING
147                       ERROR_QUIET
148                       OUTPUT_STRIP_TRAILING_WHITESPACE)
149       execute_process(COMMAND ${CURL_CONFIG_EXECUTABLE} --feature
150                       OUTPUT_VARIABLE CURL_CONFIG_FEATURES_STRING
151                       ERROR_QUIET
152                       OUTPUT_STRIP_TRAILING_WHITESPACE)
153       string(REPLACE "\n" ";" CURL_SUPPORTED_FEATURES "${CURL_CONFIG_FEATURES_STRING}")
154       execute_process(COMMAND ${CURL_CONFIG_EXECUTABLE} --protocols
155                       OUTPUT_VARIABLE CURL_CONFIG_PROTOCOLS_STRING
156                       ERROR_QUIET
157                       OUTPUT_STRIP_TRAILING_WHITESPACE)
158       string(REPLACE "\n" ";" CURL_SUPPORTED_PROTOCOLS "${CURL_CONFIG_PROTOCOLS_STRING}")
159     endif()
160
161   endif()
162   foreach(component IN LISTS CURL_FIND_COMPONENTS)
163     list(FIND CURL_KNOWN_PROTOCOLS ${component} _found)
164     if(NOT _found EQUAL -1)
165       list(FIND CURL_SUPPORTED_PROTOCOLS ${component} _found)
166       if(NOT _found EQUAL -1)
167         set(CURL_${component}_FOUND TRUE)
168       elseif(CURL_FIND_REQUIRED)
169         message(FATAL_ERROR "CURL: Required protocol ${component} is not found")
170       endif()
171     else()
172       list(FIND CURL_SUPPORTED_FEATURES ${component} _found)
173       if(NOT _found EQUAL -1)
174         set(CURL_${component}_FOUND TRUE)
175       elseif(CURL_FIND_REQUIRED)
176         message(FATAL_ERROR "CURL: Required feature ${component} is not found")
177       endif()
178     endif()
179   endforeach()
180 endif()
181
182 find_package_handle_standard_args(CURL
183                                   REQUIRED_VARS CURL_LIBRARY CURL_INCLUDE_DIR
184                                   VERSION_VAR CURL_VERSION_STRING
185                                   HANDLE_COMPONENTS)
186
187 if(CURL_FOUND)
188   set(CURL_LIBRARIES ${CURL_LIBRARY})
189   set(CURL_INCLUDE_DIRS ${CURL_INCLUDE_DIR})
190
191   if(NOT TARGET CURL::libcurl)
192     add_library(CURL::libcurl UNKNOWN IMPORTED)
193     set_target_properties(CURL::libcurl PROPERTIES
194       INTERFACE_INCLUDE_DIRECTORIES "${CURL_INCLUDE_DIRS}")
195
196     if(EXISTS "${CURL_LIBRARY}")
197       set_target_properties(CURL::libcurl PROPERTIES
198         IMPORTED_LINK_INTERFACE_LANGUAGES "C"
199         IMPORTED_LOCATION "${CURL_LIBRARY}")
200     endif()
201     if(CURL_LIBRARY_RELEASE)
202       set_property(TARGET CURL::libcurl APPEND PROPERTY
203         IMPORTED_CONFIGURATIONS RELEASE)
204       set_target_properties(CURL::libcurl PROPERTIES
205         IMPORTED_LINK_INTERFACE_LANGUAGES "C"
206         IMPORTED_LOCATION_RELEASE "${CURL_LIBRARY_RELEASE}")
207     endif()
208     if(CURL_LIBRARY_DEBUG)
209       set_property(TARGET CURL::libcurl APPEND PROPERTY
210         IMPORTED_CONFIGURATIONS DEBUG)
211       set_target_properties(CURL::libcurl PROPERTIES
212         IMPORTED_LINK_INTERFACE_LANGUAGES "C"
213         IMPORTED_LOCATION_DEBUG "${CURL_LIBRARY_DEBUG}")
214     endif()
215   endif()
216 endif()