Debian Codegraph @

CMakeModules/FindCURL.cmake

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