f842270bc53fa38604a68af4bc1f624604b94e6d
[platform/upstream/cmake.git] / Utilities / cmcurl / CMakeLists.txt
1 # Set curl options as needed for CMake build
2 set(BUILD_CURL_EXE OFF CACHE INTERNAL "No curl exe")
3 set(BUILD_DASHBOARD_REPORTS OFF CACHE INTERNAL "No curl dashboard reports")
4 set(BUILD_RELEASE_DEBUG_DIRS OFF CACHE INTERNAL "No curl release/debug dirs")
5 set(BUILD_SHARED_LIBS OFF CACHE INTERNAL "Build shared libraries")
6 set(CURL_USE_BEARSSL OFF)
7 set(CURL_USE_GSSAPI OFF)
8 set(CURL_USE_LIBSSH2 OFF)
9 set(CURL_USE_LIBSSH OFF)
10 set(CURL_USE_MBEDTLS OFF)
11 set(CURL_USE_NSS OFF)
12 set(CURL_USE_OPENLDAP OFF)
13 set(CURL_USE_OPENSSL "${CMAKE_USE_OPENSSL}")
14 set(CURL_USE_SCHANNEL OFF)
15 set(CURL_USE_SECTRANSP OFF)
16 set(CURL_USE_WOLFSSL OFF)
17 set(CURL_BROTLI OFF)
18 set(CURL_DISABLE_ALTSVC ON)
19 set(CURL_DISABLE_COOKIES OFF CACHE INTERNAL "Do not disable curl cookie support")
20 set(CURL_DISABLE_CRYPTO_AUTH OFF CACHE INTERNAL "Do not disable curl crypto auth")
21 set(CURL_DISABLE_DICT ON CACHE INTERNAL "Disable curl dict protocol?")
22 set(CURL_DISABLE_DOH OFF)
23 set(CURL_DISABLE_FILE OFF CACHE INTERNAL "Disable curl file protocol?")
24 set(CURL_DISABLE_FTP OFF CACHE INTERNAL "Disable curl ftp protocol?")
25 set(CURL_DISABLE_GETOPTIONS OFF)
26 set(CURL_DISABLE_GOPHER ON CACHE INTERNAL "Disable curl gopher protocol?")
27 set(CURL_DISABLE_HSTS ON)
28 set(CURL_DISABLE_HTTP_AUTH OFF)
29 set(CURL_DISABLE_HTTP OFF CACHE INTERNAL "Disable curl http protocol?")
30 set(CURL_DISABLE_IMAP ON CACHE INTERNAL "Disable curl imap protocol?")
31 set(CURL_DISABLE_LDAP ON CACHE INTERNAL "Disable curl ldap protocol?")
32 set(CURL_DISABLE_LDAPS ON CACHE INTERNAL "Disable curl ldaps protocol?")
33 set(CURL_DISABLE_LIBCURL_OPTION OFF)
34 set(CURL_DISABLE_MIME OFF)
35 set(CURL_DISABLE_MQTT ON)
36 set(CURL_DISABLE_NETRC OFF)
37 set(CURL_DISABLE_NTLM OFF)
38 set(CURL_DISABLE_OPENSSL_AUTO_LOAD_CONFIG OFF)
39 set(CURL_DISABLE_PARSEDATE OFF)
40 set(CURL_DISABLE_POP3 ON CACHE INTERNAL "Disable curl pop3 protocol?")
41 set(CURL_DISABLE_PROGRESS_METER OFF)
42 set(CURL_DISABLE_PROXY OFF CACHE INTERNAL "Do not disable curl proxy")
43 set(CURL_DISABLE_RTSP ON CACHE INTERNAL "Disable curl rtsp protocol?")
44 set(CURL_DISABLE_SHUFFLE_DNS OFF)
45 set(CURL_DISABLE_SMB OFF)
46 set(CURL_DISABLE_SMTP ON CACHE INTERNAL "Disable curl smtp protocol?")
47 set(CURL_DISABLE_SOCKETPAIR OFF)
48 set(CURL_DISABLE_TELNET ON CACHE INTERNAL "Disable curl telnet protocol?")
49 set(CURL_DISABLE_TFTP ON CACHE INTERNAL "Disable curl tftp protocol?")
50 set(CURL_DISABLE_VERBOSE_STRINGS OFF CACHE INTERNAL "Do not disable curl verbosity")
51 set(CURL_ENABLE_EXPORT_TARGET OFF)
52 set(CURL_HIDDEN_SYMBOLS OFF CACHE INTERNAL "No curl hidden symbols")
53 set(CURL_LTO OFF CACHE INTERNAL "Turn on compiler Link Time Optimizations")
54 set(CURL_STATIC_CRT OFF CACHE INTERNAL "Set to ON to build libcurl with static CRT on Windows (/MT).")
55 set(CURL_WERROR OFF CACHE INTERNAL "Turn compiler warnings into errors")
56 set(CURL_ZSTD OFF)
57 set(DISABLED_THREADSAFE OFF CACHE INTERNAL "Curl can use thread-safe functions")
58 set(ENABLE_ARES OFF CACHE INTERNAL "No curl c-ares support")
59 set(ENABLE_ALT_SVC OFF)
60 set(ENABLE_CURLDEBUG OFF CACHE INTERNAL "No curl TrackMemory features")
61 set(ENABLE_DEBUG OFF CACHE INTERNAL "No curl debug features")
62 set(ENABLE_INET_PTON OFF CACHE INTERNAL "Set to OFF to prevent usage of inet_pton when building against modern SDKs while still requiring compatibility with older Windows versions, such as Windows XP, Windows Server 2003 etc.")
63 set(ENABLE_IPV6 ON CACHE INTERNAL "Enable curl IPv6 support detection")
64 set(ENABLE_MANUAL OFF CACHE INTERNAL "No curl built-in manual")
65 set(ENABLE_THREADED_RESOLVER OFF CACHE INTERNAL "No curl POSIX threaded DNS lookup")
66 set(ENABLE_UNICODE OFF)
67 set(ENABLE_UNIX_SOCKETS OFF CACHE INTERNAL "No curl Unix domain sockets support")
68 set(HTTP_ONLY OFF CACHE INTERNAL "Curl is not http-only")
69 set(PICKY_COMPILER OFF CACHE INTERNAL "Enable picky compiler options")
70 set(USE_LIBIDN2 ON)
71 set(USE_NGHTTP2 ON)
72 set(USE_NGTCP2 OFF)
73 set(USE_QUICHE OFF)
74 set(USE_WIN32_IDN OFF)
75 set(USE_WIN32_LDAP OFF CACHE INTERNAL "No curl Windows LDAP")
76 if(CURL_USE_OPENSSL)
77 elseif(WIN32)
78   set(CURL_USE_SCHANNEL ON)
79   set(CURL_WINDOWS_SSPI ON)
80 elseif(APPLE)
81   # Use OS X SSL/TLS native implementation if available on target version.
82   if(CMAKE_OSX_DEPLOYMENT_TARGET)
83     set(OSX_VERSION ${CMAKE_OSX_DEPLOYMENT_TARGET})
84   else()
85     execute_process(
86       COMMAND sw_vers -productVersion
87       OUTPUT_VARIABLE OSX_VERSION
88       OUTPUT_STRIP_TRAILING_WHITESPACE
89       )
90   endif()
91   if(NOT OSX_VERSION VERSION_LESS 10.6 AND
92       CMAKE_C_COMPILER_ID MATCHES "GNU|LCC|Clang|AppleClang")
93     set(CURL_USE_SECTRANSP ON)
94   else()
95     set(CURL_USE_SECTRANSP OFF)
96   endif()
97 endif()
98
99 # Windows Vista and above have inet_pton, but this will link on
100 # older versions and then the executable will fail to launch at
101 # runtime on older versions because no DLL provides the symbol.
102 if(WIN32)
103   set(HAVE_INET_PTON 0 CACHE INTERNAL "Do not use inet_pton")
104 endif()
105
106 # Starting with OSX 10.11 there is an unrelated libnetwork library which will
107 # be picked up during curl configuration. Linking against this library is
108 # unnecessary and breaks backward compatibility of the resulting binaries
109 # because libnetwork is unavailable on older OSX versions.
110 if(APPLE)
111   set(HAVE_LIBNETWORK 0 CACHE INTERNAL "Do not use libnetwork")
112 endif(APPLE)
113
114 # Disable warnings to avoid changing 3rd party code.
115 if(CMAKE_C_COMPILER_ID MATCHES
116     "^(GNU|LCC|Clang|AppleClang|IBMClang|XLClang|XL|VisualAge|SunPro|HP|Intel|IntelLLVM|NVHPC)$")
117   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -w")
118 elseif(CMAKE_C_COMPILER_ID STREQUAL "PathScale")
119   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -woffall")
120 endif()
121
122 # Activate POSIX APIs.
123 if(CMAKE_SYSTEM_NAME MATCHES "^(AIX|OS400)$")
124   add_definitions(-D_ALL_SOURCE)
125 endif()
126 if(CMAKE_SYSTEM_NAME MATCHES "^(Linux)$")
127   add_definitions(-D_DEFAULT_SOURCE -D_BSD_SOURCE)
128 endif()
129 if(CMAKE_SYSTEM_NAME MATCHES "^(SunOS)$")
130   add_definitions(-D__EXTENSIONS__)
131 endif()
132 if(NOT CMAKE_SYSTEM_NAME MATCHES "BSD|Darwin|Windows")
133   add_definitions(-D_XOPEN_SOURCE=600)
134 endif()
135
136 #***************************************************************************
137 #                                  _   _ ____  _
138 #  Project                     ___| | | |  _ \| |
139 #                             / __| | | | |_) | |
140 #                            | (__| |_| |  _ <| |___
141 #                             \___|\___/|_| \_\_____|
142 #
143 # Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
144 #
145 # This software is licensed as described in the file COPYING, which
146 # you should have received as part of this distribution. The terms
147 # are also available at https://curl.se/docs/copyright.html.
148 #
149 # You may opt to use, copy, modify, merge, publish, distribute and/or sell
150 # copies of the Software, and permit persons to whom the Software is
151 # furnished to do so, under the terms of the COPYING file.
152 #
153 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
154 # KIND, either express or implied.
155 #
156 ###########################################################################
157 # curl/libcurl CMake script
158 # by Tetetest and Sukender (Benoit Neil)
159
160 # TODO:
161 # The output .so file lacks the soname number which we currently have within the lib/Makefile.am file
162 # Add full (4 or 5 libs) SSL support
163 # Add INSTALL target (EXTRA_DIST variables in Makefile.am may be moved to Makefile.inc so that CMake/CPack is aware of what's to include).
164 # Check on all possible platforms
165 # Test with as many configurations possible (With or without any option)
166 # Create scripts that help keeping the CMake build system up to date (to reduce maintenance). According to Tetetest:
167 #  - lists of headers that 'configure' checks for;
168 #  - curl-specific tests (the ones that are in m4/curl-*.m4 files);
169 #  - (most obvious thing:) curl version numbers.
170 # Add documentation subproject
171 #
172 # To check:
173 # (From Daniel Stenberg) The cmake build selected to run gcc with -fPIC on my box while the plain configure script did not.
174 # (From Daniel Stenberg) The gcc command line use neither -g nor any -O options. As a developer, I also treasure our configure scripts's --enable-debug option that sets a long range of "picky" compiler options.
175 set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake;${CMAKE_MODULE_PATH}")
176 include(Utilities)
177 include(Macros)
178 include(CMakeDependentOption)
179 include(CheckCCompilerFlag)
180
181 project(CURL C)
182
183 file(STRINGS ${CURL_SOURCE_DIR}/include/curl/curlver.h CURL_VERSION_H_CONTENTS REGEX "#define LIBCURL_VERSION( |_NUM )")
184 string(REGEX MATCH "#define LIBCURL_VERSION \"[^\"]*"
185   CURL_VERSION ${CURL_VERSION_H_CONTENTS})
186 string(REGEX REPLACE "[^\"]+\"" "" CURL_VERSION ${CURL_VERSION})
187 string(REGEX MATCH "#define LIBCURL_VERSION_NUM 0x[0-9a-fA-F]+"
188   CURL_VERSION_NUM ${CURL_VERSION_H_CONTENTS})
189 string(REGEX REPLACE "[^0]+0x" "" CURL_VERSION_NUM ${CURL_VERSION_NUM})
190
191
192 # Setup package meta-data
193 # SET(PACKAGE "curl")
194 if(0) # This code not needed for building within CMake.
195 message(STATUS "curl version=[${CURL_VERSION}]")
196 endif()
197 # SET(PACKAGE_TARNAME "curl")
198 # SET(PACKAGE_NAME "curl")
199 # SET(PACKAGE_VERSION "-")
200 # SET(PACKAGE_STRING "curl-")
201 # SET(PACKAGE_BUGREPORT "a suitable curl mailing list => https://curl.se/mail/")
202 set(OPERATING_SYSTEM "${CMAKE_SYSTEM_NAME}")
203 set(OS "\"${CMAKE_SYSTEM_NAME}\"")
204
205 include_directories(${CURL_SOURCE_DIR}/include)
206
207 option(CURL_WERROR "Turn compiler warnings into errors" OFF)
208 option(PICKY_COMPILER "Enable picky compiler options" ON)
209 option(BUILD_CURL_EXE "Set to ON to build curl executable." ON)
210 option(BUILD_SHARED_LIBS "Build shared libraries" ON)
211 option(ENABLE_ARES "Set to ON to enable c-ares support" OFF)
212 if(WIN32)
213   option(CURL_STATIC_CRT "Set to ON to build libcurl with static CRT on Windows (/MT)." OFF)
214   option(ENABLE_INET_PTON "Set to OFF to prevent usage of inet_pton when building against modern SDKs while still requiring compatibility with older Windows versions, such as Windows XP, Windows Server 2003 etc." ON)
215   option(ENABLE_UNICODE "Set to ON to use the Unicode version of the Windows API functions" OFF)
216   if(0) # This code not needed for building within CMake.
217   set(CURL_TARGET_WINDOWS_VERSION "" CACHE STRING "Minimum target Windows version as hex string")
218   if(CURL_TARGET_WINDOWS_VERSION)
219     add_definitions(-D_WIN32_WINNT=${CURL_TARGET_WINDOWS_VERSION})
220     set(CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS} -D_WIN32_WINNT=${CURL_TARGET_WINDOWS_VERSION}")
221   elseif(ENABLE_INET_PTON)
222     # _WIN32_WINNT_VISTA (0x0600)
223     add_definitions(-D_WIN32_WINNT=0x0600)
224     set(CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS} -D_WIN32_WINNT=0x0600")
225   else()
226     # _WIN32_WINNT_WINXP (0x0501)
227     add_definitions(-D_WIN32_WINNT=0x0501)
228     set(CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS} -D_WIN32_WINNT=0x0501")
229   endif()
230   endif()
231   if(ENABLE_UNICODE)
232     add_definitions(-DUNICODE -D_UNICODE)
233     if(MINGW)
234       add_compile_options(-municode)
235     endif()
236   endif()
237 endif()
238 option(CURL_LTO "Turn on compiler Link Time Optimizations" OFF)
239
240 if(0) # This code not needed for building within CMake.
241 cmake_dependent_option(ENABLE_THREADED_RESOLVER "Set to ON to enable threaded DNS lookup"
242         ON "NOT ENABLE_ARES"
243         OFF)
244 endif()
245
246 option(ENABLE_DEBUG "Set to ON to enable curl debug features" OFF)
247 option(ENABLE_CURLDEBUG "Set to ON to build with TrackMemory feature enabled" OFF)
248
249 if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG)
250   if(PICKY_COMPILER)
251     foreach(_CCOPT -pedantic -Wall -W -Wpointer-arith -Wwrite-strings -Wunused -Wshadow -Winline -Wnested-externs -Wmissing-declarations -Wmissing-prototypes -Wfloat-equal -Wsign-compare -Wundef -Wendif-labels -Wstrict-prototypes -Wdeclaration-after-statement -Wstrict-aliasing=3 -Wcast-align -Wtype-limits -Wold-style-declaration -Wmissing-parameter-type -Wempty-body -Wclobbered -Wignored-qualifiers -Wconversion -Wvla -Wdouble-promotion -Wenum-conversion -Warith-conversion)
252       # surprisingly, CHECK_C_COMPILER_FLAG needs a new variable to store each new
253       # test result in.
254       string(MAKE_C_IDENTIFIER "OPT${_CCOPT}" _optvarname)
255       check_c_compiler_flag(${_CCOPT} ${_optvarname})
256       if(${_optvarname})
257         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_CCOPT}")
258       endif()
259     endforeach()
260     foreach(_CCOPT long-long multichar format-nonliteral sign-conversion system-headers pedantic-ms-format)
261       # GCC only warns about unknown -Wno- options if there are also other diagnostic messages,
262       # so test for the positive form instead
263       string(MAKE_C_IDENTIFIER "OPT${_CCOPT}" _optvarname)
264       check_c_compiler_flag("-W${_CCOPT}" ${_optvarname})
265       if(${_optvarname})
266         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-${_CCOPT}")
267       endif()
268     endforeach()
269   endif()
270 endif()
271
272 if(ENABLE_DEBUG)
273   # DEBUGBUILD will be defined only for Debug builds
274   set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:Debug>:DEBUGBUILD>)
275   set(ENABLE_CURLDEBUG ON)
276 endif()
277
278 if(ENABLE_CURLDEBUG)
279   set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS CURLDEBUG)
280 endif()
281
282 if(0) # This code not needed for building within CMake.
283 # For debug libs and exes, add "-d" postfix
284 if(NOT DEFINED CMAKE_DEBUG_POSTFIX)
285   set(CMAKE_DEBUG_POSTFIX "-d")
286 endif()
287 endif()
288
289 # initialize CURL_LIBS
290 set(CURL_LIBS "")
291
292 if(ENABLE_ARES)
293   set(USE_ARES 1)
294   find_package(CARES REQUIRED)
295   list(APPEND CURL_LIBS ${CARES_LIBRARY})
296 endif()
297
298 if(0) # This code not needed for building within CMake.
299 include(CurlSymbolHiding)
300 endif()
301
302 option(CURL_ENABLE_EXPORT_TARGET "to enable cmake export target" ON)
303 mark_as_advanced(CURL_ENABLE_EXPORT_TARGET)
304
305 option(CURL_DISABLE_ALTSVC "disables alt-svc support" OFF)
306 mark_as_advanced(CURL_DISABLE_ALTSVC)
307 option(CURL_DISABLE_COOKIES "disables cookies support" OFF)
308 mark_as_advanced(CURL_DISABLE_COOKIES)
309 option(CURL_DISABLE_CRYPTO_AUTH "disables cryptographic authentication" OFF)
310 mark_as_advanced(CURL_DISABLE_CRYPTO_AUTH)
311 option(CURL_DISABLE_DICT "disables DICT" OFF)
312 mark_as_advanced(CURL_DISABLE_DICT)
313 option(CURL_DISABLE_DOH "disables DNS-over-HTTPS" OFF)
314 mark_as_advanced(CURL_DISABLE_DOH)
315 option(CURL_DISABLE_FILE "disables FILE" OFF)
316 mark_as_advanced(CURL_DISABLE_FILE)
317 option(CURL_DISABLE_FTP "disables FTP" OFF)
318 mark_as_advanced(CURL_DISABLE_FTP)
319 option(CURL_DISABLE_GETOPTIONS "disables curl_easy_options API for existing options to curl_easy_setopt" OFF)
320 mark_as_advanced(CURL_DISABLE_GETOPTIONS)
321 option(CURL_DISABLE_GOPHER "disables Gopher" OFF)
322 mark_as_advanced(CURL_DISABLE_GOPHER)
323 option(CURL_DISABLE_HSTS "disables HSTS support" OFF)
324 mark_as_advanced(CURL_DISABLE_HSTS)
325 option(CURL_DISABLE_HTTP "disables HTTP" OFF)
326 mark_as_advanced(CURL_DISABLE_HTTP)
327 option(CURL_DISABLE_HTTP_AUTH "disables all HTTP authentication methods" OFF)
328 mark_as_advanced(CURL_DISABLE_HTTP_AUTH)
329 option(CURL_DISABLE_IMAP "disables IMAP" OFF)
330 mark_as_advanced(CURL_DISABLE_IMAP)
331 option(CURL_DISABLE_LDAP "disables LDAP" OFF)
332 mark_as_advanced(CURL_DISABLE_LDAP)
333 option(CURL_DISABLE_LDAPS "disables LDAPS" OFF)
334 mark_as_advanced(CURL_DISABLE_LDAPS)
335 option(CURL_DISABLE_LIBCURL_OPTION "disables --libcurl option from the curl tool" OFF)
336 mark_as_advanced(CURL_DISABLE_LIBCURL_OPTION)
337 option(CURL_DISABLE_MIME "disables MIME support" OFF)
338 mark_as_advanced(CURL_DISABLE_MIME)
339 option(CURL_DISABLE_MQTT "disables MQTT" OFF)
340 mark_as_advanced(CURL_DISABLE_MQTT)
341 option(CURL_DISABLE_NETRC "disables netrc parser" OFF)
342 mark_as_advanced(CURL_DISABLE_NETRC)
343 option(CURL_DISABLE_NTLM "disables NTLM support" OFF)
344 mark_as_advanced(CURL_DISABLE_NTLM)
345 option(CURL_DISABLE_PARSEDATE "disables date parsing" OFF)
346 mark_as_advanced(CURL_DISABLE_PARSEDATE)
347 option(CURL_DISABLE_POP3 "disables POP3" OFF)
348 mark_as_advanced(CURL_DISABLE_POP3)
349 option(CURL_DISABLE_PROGRESS_METER "disables built-in progress meter" OFF)
350 mark_as_advanced(CURL_DISABLE_PROGRESS_METER)
351 option(CURL_DISABLE_PROXY "disables proxy support" OFF)
352 mark_as_advanced(CURL_DISABLE_PROXY)
353 option(CURL_DISABLE_RTSP "disables RTSP" OFF)
354 mark_as_advanced(CURL_DISABLE_RTSP)
355 option(CURL_DISABLE_SHUFFLE_DNS "disables shuffle DNS feature" OFF)
356 mark_as_advanced(CURL_DISABLE_SHUFFLE_DNS)
357 option(CURL_DISABLE_SMB "disables SMB" OFF)
358 mark_as_advanced(CURL_DISABLE_SMB)
359 option(CURL_DISABLE_SMTP "disables SMTP" OFF)
360 mark_as_advanced(CURL_DISABLE_SMTP)
361 option(CURL_DISABLE_SOCKETPAIR "disables use of socketpair for curl_multi_poll" OFF)
362 mark_as_advanced(CURL_DISABLE_SOCKETPAIR)
363 option(CURL_DISABLE_TELNET "disables Telnet" OFF)
364 mark_as_advanced(CURL_DISABLE_TELNET)
365 option(CURL_DISABLE_TFTP "disables TFTP" OFF)
366 mark_as_advanced(CURL_DISABLE_TFTP)
367 option(CURL_DISABLE_VERBOSE_STRINGS "disables verbose strings" OFF)
368 mark_as_advanced(CURL_DISABLE_VERBOSE_STRINGS)
369
370 # Corresponds to HTTP_ONLY in lib/curl_setup.h
371 option(HTTP_ONLY "disables all protocols except HTTP (This overrides all CURL_DISABLE_* options)" OFF)
372 mark_as_advanced(HTTP_ONLY)
373
374 if(HTTP_ONLY)
375   set(CURL_DISABLE_DICT ON)
376   set(CURL_DISABLE_FILE ON)
377   set(CURL_DISABLE_FTP ON)
378   set(CURL_DISABLE_GOPHER ON)
379   set(CURL_DISABLE_IMAP ON)
380   set(CURL_DISABLE_LDAP ON)
381   set(CURL_DISABLE_LDAPS ON)
382   set(CURL_DISABLE_MQTT ON)
383   set(CURL_DISABLE_POP3 ON)
384   set(CURL_DISABLE_RTSP ON)
385   set(CURL_DISABLE_SMB ON)
386   set(CURL_DISABLE_SMTP ON)
387   set(CURL_DISABLE_TELNET ON)
388   set(CURL_DISABLE_TFTP ON)
389 endif()
390
391 option(ENABLE_IPV6 "Define if you want to enable IPv6 support" ON)
392 mark_as_advanced(ENABLE_IPV6)
393 if(ENABLE_IPV6 AND NOT WIN32)
394   include(CheckStructHasMember)
395   check_struct_has_member("struct sockaddr_in6" sin6_addr "netinet/in.h"
396                           HAVE_SOCKADDR_IN6_SIN6_ADDR)
397   check_struct_has_member("struct sockaddr_in6" sin6_scope_id "netinet/in.h"
398                           HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID)
399   if(NOT HAVE_SOCKADDR_IN6_SIN6_ADDR)
400     message(WARNING "struct sockaddr_in6 not available, disabling IPv6 support")
401     # Force the feature off as this name is used as guard macro...
402     set(ENABLE_IPV6 OFF
403         CACHE BOOL "Define if you want to enable IPv6 support" FORCE)
404   endif()
405
406   if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND NOT ENABLE_ARES)
407     set(use_core_foundation ON)
408
409     find_library(SYSTEMCONFIGURATION_FRAMEWORK "SystemConfiguration")
410     if(NOT SYSTEMCONFIGURATION_FRAMEWORK)
411       message(FATAL_ERROR "SystemConfiguration framework not found")
412     endif()
413
414     list(APPEND CURL_LIBS "-framework SystemConfiguration")
415   endif()
416 endif()
417
418 if(0) # This code not needed for building within CMake.
419 if(USE_MANUAL)
420     #nroff is currently only used when USE_MANUAL is set, so we can prevent the warning of no *NROFF if USE_MANUAL is OFF (or not defined), by not even looking for NROFF..
421     curl_nroff_check()
422 endif()
423 find_package(Perl)
424
425 cmake_dependent_option(ENABLE_MANUAL "to provide the built-in manual"
426     ON "NROFF_USEFUL;PERL_FOUND"
427     OFF)
428
429 if(ENABLE_MANUAL)
430   set(USE_MANUAL ON)
431 endif()
432 endif()
433
434 if(CURL_STATIC_CRT)
435   set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
436   set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT")
437   set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
438 endif()
439
440 # Disable warnings on Borland to avoid changing 3rd party code.
441 if(BORLAND)
442   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -w-")
443 endif()
444
445 # If we are on AIX, do the _ALL_SOURCE magic
446 if(${CMAKE_SYSTEM_NAME} MATCHES AIX)
447   set(_ALL_SOURCE 1)
448 endif()
449
450 # Include all the necessary files for macros
451 include(CMakePushCheckState)
452 include(CheckFunctionExists)
453 include(CheckIncludeFile)
454 include(CheckIncludeFiles)
455 include(CheckLibraryExists)
456 include(CheckSymbolExists)
457 include(CheckTypeSize)
458 include(CheckCSourceCompiles)
459
460 # On windows preload settings
461 if(WIN32)
462   set(CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS} -D_WINSOCKAPI_=")
463   include(${CMAKE_CURRENT_SOURCE_DIR}/CMake/Platforms/WindowsCache.cmake)
464 endif()
465
466 if(ENABLE_THREADED_RESOLVER)
467   find_package(Threads REQUIRED)
468   if(WIN32)
469     set(USE_THREADS_WIN32 ON)
470   else()
471     set(USE_THREADS_POSIX ${CMAKE_USE_PTHREADS_INIT})
472     set(HAVE_PTHREAD_H ${CMAKE_USE_PTHREADS_INIT})
473   endif()
474   set(CURL_LIBS ${CURL_LIBS} ${CMAKE_THREAD_LIBS_INIT})
475 endif()
476
477 # Check for all needed libraries
478 if(0) # This code not needed for building within CMake.
479 check_library_exists_concat("${CMAKE_DL_LIBS}" dlopen HAVE_LIBDL)
480 else()
481   # Use the cmake-defined dl libs as dl is should not be used
482   # on HPUX, but rather dld this avoids a warning
483   list(APPEND CURL_LIBS ${CMAKE_DL_LIBS})
484 endif()
485 check_library_exists_concat("socket" connect      HAVE_LIBSOCKET)
486 check_library_exists("c" gethostbyname "" NOT_NEED_LIBNSL)
487
488 if(NOT NOT_NEED_LIBNSL)
489   check_library_exists_concat("nsl"    gethostbyname  HAVE_LIBNSL)
490 endif()
491
492 check_function_exists(gethostname HAVE_GETHOSTNAME)
493
494 if(WIN32)
495   check_library_exists_concat("ws2_32" getch        HAVE_LIBWS2_32)
496   check_library_exists_concat("winmm"  getch        HAVE_LIBWINMM)
497 endif()
498
499 if(0) # This code not needed for building within CMake.
500 # This check below for use of deprecated symbols is only temporary and is to
501 # be removed again after a year's service. Remove after November 25, 2022.
502 set(CURL_RECONFIG_REQUIRED 0)
503 foreach(_LIB GSSAPI OPENLDAP LIBSSH LIBSSH2 BEARSSL MBEDTLS NSS OPENSSL
504         SCHANNEL SECTRANSP WOLFSSL)
505   if(CMAKE_USE_${_LIB})
506     set(CURL_RECONFIG_REQUIRED 1)
507     message(SEND_ERROR "The option CMAKE_USE_${_LIB} was renamed to CURL_USE_${_LIB}.")
508   endif()
509 endforeach()
510 if(CMAKE_USE_WINSSL)
511   set(CURL_RECONFIG_REQUIRED 1)
512   message(SEND_ERROR "The option CMAKE_USE_WINSSL was renamed to CURL_USE_SCHANNEL.")
513 endif()
514 if(CURL_RECONFIG_REQUIRED)
515   message(FATAL_ERROR "Reconfig required")
516 endif()
517
518 # check SSL libraries
519 # TODO support GnuTLS
520 option(CURL_ENABLE_SSL "Enable SSL support" ON)
521
522 if(APPLE)
523   cmake_dependent_option(CURL_USE_SECTRANSP "enable Apple OS native SSL/TLS" OFF CURL_ENABLE_SSL OFF)
524 endif()
525 if(WIN32)
526   cmake_dependent_option(CURL_USE_SCHANNEL "enable Windows native SSL/TLS" OFF CURL_ENABLE_SSL OFF)
527   cmake_dependent_option(CURL_WINDOWS_SSPI "Use windows libraries to allow NTLM authentication without openssl" ON
528     CURL_USE_SCHANNEL OFF)
529 endif()
530 cmake_dependent_option(CURL_USE_MBEDTLS "Enable mbedTLS for SSL/TLS" OFF CURL_ENABLE_SSL OFF)
531 cmake_dependent_option(CURL_USE_BEARSSL "Enable BearSSL for SSL/TLS" OFF CURL_ENABLE_SSL OFF)
532 cmake_dependent_option(CURL_USE_NSS "Enable NSS for SSL/TLS" OFF CURL_ENABLE_SSL OFF)
533 cmake_dependent_option(CURL_USE_WOLFSSL "enable wolfSSL for SSL/TLS" OFF CURL_ENABLE_SSL OFF)
534
535 set(openssl_default ON)
536 if(WIN32 OR CURL_USE_SECTRANSP OR CURL_USE_SCHANNEL OR CURL_USE_MBEDTLS OR CURL_USE_NSS OR CURL_USE_WOLFSSL)
537   set(openssl_default OFF)
538 endif()
539 cmake_dependent_option(CURL_USE_OPENSSL "Use OpenSSL code. Experimental" ${openssl_default} CURL_ENABLE_SSL OFF)
540 option(CURL_DISABLE_OPENSSL_AUTO_LOAD_CONFIG "Disable automatic loading of OpenSSL configuration" OFF)
541 endif()
542
543 count_true(enabled_ssl_options_count
544   CURL_USE_SCHANNEL
545   CURL_USE_SECTRANSP
546   CURL_USE_OPENSSL
547   CURL_USE_MBEDTLS
548   CURL_USE_BEARSSL
549   CURL_USE_NSS
550   CURL_USE_WOLFSSL
551 )
552 if(enabled_ssl_options_count GREATER "1")
553   set(CURL_WITH_MULTI_SSL ON)
554 endif()
555
556 if(CURL_USE_SCHANNEL)
557   set(SSL_ENABLED ON)
558   set(USE_SCHANNEL ON) # Windows native SSL/TLS support
559   set(USE_WINDOWS_SSPI ON) # CURL_USE_SCHANNEL implies CURL_WINDOWS_SSPI
560 endif()
561 if(CURL_WINDOWS_SSPI)
562   set(USE_WINDOWS_SSPI ON)
563   set(CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS} -DSECURITY_WIN32")
564 endif()
565
566 if(CURL_USE_SECTRANSP)
567   set(use_core_foundation ON)
568
569   find_library(SECURITY_FRAMEWORK "Security")
570   if(NOT SECURITY_FRAMEWORK)
571      message(FATAL_ERROR "Security framework not found")
572   endif()
573
574   set(SSL_ENABLED ON)
575   set(USE_SECTRANSP ON)
576   list(APPEND CURL_LIBS "-framework Security")
577 endif()
578
579 if(use_core_foundation)
580   find_library(COREFOUNDATION_FRAMEWORK "CoreFoundation")
581   if(NOT COREFOUNDATION_FRAMEWORK)
582       message(FATAL_ERROR "CoreFoundation framework not found")
583   endif()
584
585   list(APPEND CURL_LIBS "-framework CoreFoundation")
586 endif()
587
588 if(CURL_USE_OPENSSL)
589   find_package(OpenSSL)
590   if(NOT OpenSSL_FOUND)
591     message(FATAL_ERROR
592       "Could not find OpenSSL. Install an OpenSSL development package or "
593       "configure CMake with -DCMAKE_USE_OPENSSL=OFF to build without OpenSSL.")
594   endif()
595   set(SSL_ENABLED ON)
596   set(USE_OPENSSL ON)
597   list(APPEND CURL_LIBS ${OPENSSL_LIBRARIES})
598   include_directories(${OPENSSL_INCLUDE_DIR})
599
600   set(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
601   check_include_file("openssl/crypto.h" HAVE_OPENSSL_CRYPTO_H)
602   check_include_file("openssl/err.h"    HAVE_OPENSSL_ERR_H)
603   check_include_file("openssl/pem.h"    HAVE_OPENSSL_PEM_H)
604   check_include_file("openssl/rsa.h"    HAVE_OPENSSL_RSA_H)
605   check_include_file("openssl/ssl.h"    HAVE_OPENSSL_SSL_H)
606   check_include_file("openssl/x509.h"   HAVE_OPENSSL_X509_H)
607   check_include_file("openssl/rand.h"   HAVE_OPENSSL_RAND_H)
608   check_symbol_exists(RAND_status "${CURL_INCLUDES}" HAVE_RAND_STATUS)
609   check_symbol_exists(RAND_screen "${CURL_INCLUDES}" HAVE_RAND_SCREEN)
610   check_symbol_exists(RAND_egd    "${CURL_INCLUDES}" HAVE_RAND_EGD)
611
612   # Optionally build with a specific CA cert bundle.
613   if(CURL_CA_BUNDLE)
614     add_definitions(-DCURL_CA_BUNDLE="${CURL_CA_BUNDLE}")
615   endif()
616   # Optionally build with a specific CA cert dir.
617   if(CURL_CA_PATH)
618     add_definitions(-DCURL_CA_PATH="${CURL_CA_PATH}")
619   endif()
620
621   add_definitions(-DOPENSSL_SUPPRESS_DEPRECATED)
622 endif()
623
624 if(CURL_USE_MBEDTLS)
625   find_package(MbedTLS REQUIRED)
626   set(SSL_ENABLED ON)
627   set(USE_MBEDTLS ON)
628   list(APPEND CURL_LIBS ${MBEDTLS_LIBRARIES})
629   include_directories(${MBEDTLS_INCLUDE_DIRS})
630 endif()
631
632 if(CURL_USE_BEARSSL)
633   find_package(BearSSL REQUIRED)
634   set(SSL_ENABLED ON)
635   set(USE_BEARSSL ON)
636   list(APPEND CURL_LIBS ${BEARSSL_LIBRARY})
637   include_directories(${BEARSSL_INCLUDE_DIRS})
638 endif()
639
640 if(CURL_USE_WOLFSSL)
641   find_package(WolfSSL REQUIRED)
642   set(SSL_ENABLED ON)
643   set(USE_WOLFSSL ON)
644   list(APPEND CURL_LIBS ${WolfSSL_LIBRARIES})
645   include_directories(${WolfSSL_INCLUDE_DIRS})
646 endif()
647
648 if(CURL_USE_NSS)
649   find_package(NSS REQUIRED)
650   include_directories(${NSS_INCLUDE_DIRS})
651   list(APPEND CURL_LIBS ${NSS_LIBRARIES})
652   set(SSL_ENABLED ON)
653   set(USE_NSS ON)
654   cmake_push_check_state()
655   set(CMAKE_REQUIRED_INCLUDES ${NSS_INCLUDE_DIRS})
656   set(CMAKE_REQUIRED_LIBRARIES ${NSS_LIBRARIES})
657   check_symbol_exists(PK11_CreateManagedGenericObject "pk11pub.h" HAVE_PK11_CREATEMANAGEDGENERICOBJECT)
658   cmake_pop_check_state()
659 endif()
660
661 option(USE_NGHTTP2 "Use Nghttp2 library" OFF)
662 if(USE_NGHTTP2)
663   find_package(NGHTTP2 REQUIRED)
664   include_directories(${NGHTTP2_INCLUDE_DIRS})
665   list(APPEND CURL_LIBS ${NGHTTP2_LIBRARIES})
666 endif()
667
668 function(CheckQuicSupportInOpenSSL)
669   # Be sure that the OpenSSL library actually supports QUIC.
670   cmake_push_check_state()
671   set(CMAKE_REQUIRED_INCLUDES   "${OPENSSL_INCLUDE_DIR}")
672   set(CMAKE_REQUIRED_LIBRARIES  "${OPENSSL_LIBRARIES}")
673   check_symbol_exists(SSL_CTX_set_quic_method "openssl/ssl.h" HAVE_SSL_CTX_SET_QUIC_METHOD)
674   if(NOT HAVE_SSL_CTX_SET_QUIC_METHOD)
675     message(FATAL_ERROR "QUIC support is missing in OpenSSL/boringssl. Try setting -DOPENSSL_ROOT_DIR")
676   endif()
677   cmake_pop_check_state()
678 endfunction()
679
680 option(USE_NGTCP2 "Use ngtcp2 and nghttp3 libraries for HTTP/3 support" OFF)
681 if(USE_NGTCP2)
682   if(USE_OPENSSL)
683     find_package(NGTCP2 REQUIRED OpenSSL)
684     CheckQuicSupportInOpenSSL()
685   elseif(USE_GNUTLS)
686     # TODO add GnuTLS support as vtls library.
687     find_package(NGTCP2 REQUIRED GnuTLS)
688   else()
689     message(FATAL_ERROR "ngtcp2 requires OpenSSL or GnuTLS")
690   endif()
691   set(USE_NGTCP2 ON)
692   include_directories(${NGTCP2_INCLUDE_DIRS})
693   list(APPEND CURL_LIBS ${NGTCP2_LIBRARIES})
694
695   find_package(NGHTTP3 REQUIRED)
696   set(USE_NGHTTP3 ON)
697   include_directories(${NGHTTP3_INCLUDE_DIRS})
698   list(APPEND CURL_LIBS ${NGHTTP3_LIBRARIES})
699 endif()
700
701 option(USE_QUICHE "Use quiche library for HTTP/3 support" OFF)
702 if(USE_QUICHE)
703   if(USE_NGTCP2)
704     message(FATAL_ERROR "Only one HTTP/3 backend can be selected!")
705   endif()
706   find_package(QUICHE REQUIRED)
707   CheckQuicSupportInOpenSSL()
708   set(USE_QUICHE ON)
709   include_directories(${QUICHE_INCLUDE_DIRS})
710   list(APPEND CURL_LIBS ${QUICHE_LIBRARIES})
711   cmake_push_check_state()
712   set(CMAKE_REQUIRED_INCLUDES   "${QUICHE_INCLUDE_DIRS}")
713   set(CMAKE_REQUIRED_LIBRARIES  "${QUICHE_LIBRARIES}")
714   check_symbol_exists(quiche_conn_set_qlog_fd "quiche.h" HAVE_QUICHE_CONN_SET_QLOG_FD)
715   cmake_pop_check_state()
716 endif()
717
718 option(USE_MSH3 "Use msquic library for HTTP/3 support" OFF)
719 if(USE_MSH3)
720   if(USE_NGTCP2 OR USE_QUICHE)
721     message(FATAL_ERROR "Only one HTTP/3 backend can be selected!")
722   endif()
723   set(USE_MSH3 ON)
724   include_directories(${MSH3_INCLUDE_DIRS})
725   list(APPEND CURL_LIBS ${MSH3_LIBRARIES})
726 endif()
727
728 if(NOT CURL_DISABLE_LDAP)
729   if(WIN32)
730     option(USE_WIN32_LDAP "Use Windows LDAP implementation" ON)
731     if(USE_WIN32_LDAP)
732       check_library_exists_concat("wldap32" cldap_open HAVE_WLDAP32)
733       if(NOT HAVE_WLDAP32)
734         set(USE_WIN32_LDAP OFF)
735       endif()
736     endif()
737   endif()
738
739   option(CURL_USE_OPENLDAP "Use OpenLDAP code." OFF)
740   mark_as_advanced(CURL_USE_OPENLDAP)
741   set(CMAKE_LDAP_LIB "ldap" CACHE STRING "Name or full path to ldap library")
742   set(CMAKE_LBER_LIB "lber" CACHE STRING "Name or full path to lber library")
743
744   if(CURL_USE_OPENLDAP AND USE_WIN32_LDAP)
745     message(FATAL_ERROR "Cannot use USE_WIN32_LDAP and CURL_USE_OPENLDAP at the same time")
746   endif()
747
748   # Now that we know, we're not using windows LDAP...
749   if(USE_WIN32_LDAP)
750     check_include_file_concat("winldap.h" HAVE_WINLDAP_H)
751     check_include_file_concat("winber.h"  HAVE_WINBER_H)
752   else()
753     # Check for LDAP
754     set(CMAKE_REQUIRED_LIBRARIES ${OPENSSL_LIBRARIES})
755     check_library_exists_concat(${CMAKE_LDAP_LIB} ldap_init HAVE_LIBLDAP)
756     check_library_exists_concat(${CMAKE_LBER_LIB} ber_init HAVE_LIBLBER)
757
758     set(CMAKE_REQUIRED_INCLUDES_BAK ${CMAKE_REQUIRED_INCLUDES})
759     set(CMAKE_LDAP_INCLUDE_DIR "" CACHE STRING "Path to LDAP include directory")
760     if(CMAKE_LDAP_INCLUDE_DIR)
761       list(APPEND CMAKE_REQUIRED_INCLUDES ${CMAKE_LDAP_INCLUDE_DIR})
762     endif()
763     check_include_file_concat("ldap.h"           HAVE_LDAP_H)
764     check_include_file_concat("lber.h"           HAVE_LBER_H)
765
766     if(NOT HAVE_LDAP_H)
767       message(STATUS "LDAP_H not found CURL_DISABLE_LDAP set ON")
768       set(CURL_DISABLE_LDAP ON CACHE BOOL "" FORCE)
769       set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES_BAK}) #LDAP includes won't be used
770     elseif(NOT HAVE_LIBLDAP)
771       message(STATUS "LDAP library '${CMAKE_LDAP_LIB}' not found CURL_DISABLE_LDAP set ON")
772       set(CURL_DISABLE_LDAP ON CACHE BOOL "" FORCE)
773       set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES_BAK}) #LDAP includes won't be used
774     else()
775       if(CURL_USE_OPENLDAP)
776         set(USE_OPENLDAP ON)
777       endif()
778       if(CMAKE_LDAP_INCLUDE_DIR)
779         include_directories(${CMAKE_LDAP_INCLUDE_DIR})
780       endif()
781       set(NEED_LBER_H ON)
782       set(_HEADER_LIST)
783       if(HAVE_WINDOWS_H)
784         list(APPEND _HEADER_LIST "windows.h")
785       endif()
786       if(HAVE_SYS_TYPES_H)
787         list(APPEND _HEADER_LIST "sys/types.h")
788       endif()
789       list(APPEND _HEADER_LIST "ldap.h")
790
791       set(_SRC_STRING "")
792       foreach(_HEADER ${_HEADER_LIST})
793         set(_INCLUDE_STRING "${_INCLUDE_STRING}#include <${_HEADER}>\n")
794       endforeach()
795
796       set(_SRC_STRING
797         "
798         ${_INCLUDE_STRING}
799         int main(int argc, char ** argv)
800         {
801           BerValue *bvp = NULL;
802           BerElement *bep = ber_init(bvp);
803           ber_free(bep, 1);
804           return 0;
805         }"
806       )
807       set(CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS} -DLDAP_DEPRECATED=1")
808       list(APPEND CMAKE_REQUIRED_LIBRARIES ${CMAKE_LDAP_LIB})
809       if(HAVE_LIBLBER)
810         list(APPEND CMAKE_REQUIRED_LIBRARIES ${CMAKE_LBER_LIB})
811       endif()
812       check_c_source_compiles("${_SRC_STRING}" NOT_NEED_LBER_H)
813       unset(CMAKE_REQUIRED_LIBRARIES)
814
815       if(NOT_NEED_LBER_H)
816         set(NEED_LBER_H OFF)
817       else()
818         set(CURL_TEST_DEFINES "${CURL_TEST_DEFINES} -DNEED_LBER_H")
819       endif()
820     endif()
821   endif()
822 endif()
823
824 # No ldap, no ldaps.
825 if(CURL_DISABLE_LDAP)
826   if(NOT CURL_DISABLE_LDAPS)
827     message(STATUS "LDAP needs to be enabled to support LDAPS")
828     set(CURL_DISABLE_LDAPS ON CACHE BOOL "" FORCE)
829   endif()
830 endif()
831
832 if(NOT CURL_DISABLE_LDAPS)
833   check_include_file_concat("ldap_ssl.h" HAVE_LDAP_SSL_H)
834   check_include_file_concat("ldapssl.h"  HAVE_LDAPSSL_H)
835 endif()
836
837 # Check for idn
838 option(USE_LIBIDN2 "Use libidn2 for IDN support" ON)
839 set(HAVE_LIBIDN2 OFF)
840 if(USE_LIBIDN2)
841   check_library_exists_concat("idn2" idn2_lookup_ul HAVE_LIBIDN2)
842 endif()
843
844 if(WIN32)
845   option(USE_WIN32_IDN "Use WinIDN for IDN support" OFF)
846   if(USE_WIN32_IDN)
847     list(APPEND CURL_LIBS "Normaliz")
848     set(WANT_IDN_PROTOTYPES ON)
849   endif()
850 endif()
851
852 # Check for symbol dlopen (same as HAVE_LIBDL)
853 check_library_exists("${CURL_LIBS}" dlopen "" HAVE_DLOPEN)
854
855 if(0) # This code not needed for building within CMake.
856 set(HAVE_LIBZ OFF)
857 set(HAVE_ZLIB_H OFF)
858 set(USE_ZLIB OFF)
859 optional_dependency(ZLIB)
860 if(ZLIB_FOUND)
861   set(HAVE_ZLIB_H ON)
862   set(HAVE_LIBZ ON)
863   set(USE_ZLIB ON)
864
865   # Depend on ZLIB via imported targets if supported by the running
866   # version of CMake.  This allows our dependents to get our dependencies
867   # transitively.
868   if(NOT CMAKE_VERSION VERSION_LESS 3.4)
869     list(APPEND CURL_LIBS ZLIB::ZLIB)
870   else()
871     list(APPEND CURL_LIBS ${ZLIB_LIBRARIES})
872     include_directories(${ZLIB_INCLUDE_DIRS})
873   endif()
874   list(APPEND CMAKE_REQUIRED_INCLUDES ${ZLIB_INCLUDE_DIRS})
875 endif()
876 endif()
877
878 #-----------------------------------------------------------------------------
879 # CMake-specific curl code.
880
881 if(CURL_SPECIAL_LIBZ)
882   set(CURL_LIBS ${CURL_LIBS} "${CURL_SPECIAL_LIBZ}")
883   include_directories(${CURL_SPECIAL_LIBZ_INCLUDES})
884   set(HAVE_LIBZ 0)
885   set(HAVE_ZLIB_H 0)
886 endif()
887
888 option(CURL_BROTLI "Set to ON to enable building curl with brotli support." OFF)
889 set(HAVE_BROTLI OFF)
890 if(CURL_BROTLI)
891   find_package(Brotli QUIET)
892   if(BROTLI_FOUND)
893     set(HAVE_BROTLI ON)
894     list(APPEND CURL_LIBS ${BROTLI_LIBRARIES})
895     include_directories(${BROTLI_INCLUDE_DIRS})
896     list(APPEND CMAKE_REQUIRED_INCLUDES ${BROTLI_INCLUDE_DIRS})
897   endif()
898 endif()
899
900 option(CURL_ZSTD "Set to ON to enable building curl with zstd support." OFF)
901 set(HAVE_ZSTD OFF)
902 if(CURL_ZSTD)
903   find_package(Zstd REQUIRED)
904   cmake_push_check_state()
905   set(CMAKE_REQUIRED_INCLUDES ${Zstd_INCLUDE_DIRS})
906   set(CMAKE_REQUIRED_LIBRARIES ${Zstd_LIBRARIES})
907   check_symbol_exists(ZSTD_createDStream "zstd.h" HAVE_ZSTD_CREATEDSTREAM)
908   cmake_pop_check_state()
909   if(Zstd_FOUND AND HAVE_ZSTD_CREATEDSTREAM)
910     set(HAVE_ZSTD ON)
911     list(APPEND CURL_LIBS ${Zstd_LIBRARIES})
912     include_directories(${Zstd_INCLUDE_DIRS})
913   endif()
914 endif()
915
916 #libSSH2
917 option(CURL_USE_LIBSSH2 "Use libSSH2" ON)
918 mark_as_advanced(CURL_USE_LIBSSH2)
919 set(USE_LIBSSH2 OFF)
920 set(HAVE_LIBSSH2 OFF)
921 set(HAVE_LIBSSH2_H OFF)
922
923 if(CURL_USE_LIBSSH2)
924   find_package(LibSSH2)
925   if(LIBSSH2_FOUND)
926     list(APPEND CURL_LIBS ${LIBSSH2_LIBRARY})
927     set(CMAKE_REQUIRED_LIBRARIES ${LIBSSH2_LIBRARY})
928     list(APPEND CMAKE_REQUIRED_INCLUDES "${LIBSSH2_INCLUDE_DIR}")
929     include_directories("${LIBSSH2_INCLUDE_DIR}")
930     set(HAVE_LIBSSH2 ON)
931     set(USE_LIBSSH2 ON)
932
933     # find_package has already found the headers
934     set(HAVE_LIBSSH2_H ON)
935     set(CURL_INCLUDES ${CURL_INCLUDES} "${LIBSSH2_INCLUDE_DIR}/libssh2.h")
936     set(CURL_TEST_DEFINES "${CURL_TEST_DEFINES} -DHAVE_LIBSSH2_H")
937     unset(CMAKE_REQUIRED_LIBRARIES)
938   endif()
939 endif()
940
941 # libssh
942 option(CURL_USE_LIBSSH "Use libSSH" OFF)
943 mark_as_advanced(CURL_USE_LIBSSH)
944 if(NOT HAVE_LIBSSH2 AND CURL_USE_LIBSSH)
945   find_package(libssh CONFIG)
946   if(libssh_FOUND)
947     message(STATUS "Found libssh ${libssh_VERSION}")
948     # Use imported target for include and library paths.
949     list(APPEND CURL_LIBS ssh)
950     set(USE_LIBSSH ON)
951     set(HAVE_LIBSSH_LIBSSH_H 1)
952   endif()
953 endif()
954
955 option(CURL_USE_GSSAPI "Use GSSAPI implementation (right now only Heimdal is supported with CMake build)" OFF)
956 mark_as_advanced(CURL_USE_GSSAPI)
957
958 if(CURL_USE_GSSAPI)
959   find_package(GSS)
960
961   set(HAVE_GSSAPI ${GSS_FOUND})
962   if(GSS_FOUND)
963
964     message(STATUS "Found ${GSS_FLAVOUR} GSSAPI version: \"${GSS_VERSION}\"")
965
966     list(APPEND CMAKE_REQUIRED_INCLUDES ${GSS_INCLUDE_DIR})
967     check_include_file_concat("gssapi/gssapi.h"  HAVE_GSSAPI_GSSAPI_H)
968     check_include_file_concat("gssapi/gssapi_generic.h" HAVE_GSSAPI_GSSAPI_GENERIC_H)
969     check_include_file_concat("gssapi/gssapi_krb5.h" HAVE_GSSAPI_GSSAPI_KRB5_H)
970
971     if(GSS_FLAVOUR STREQUAL "Heimdal")
972       set(HAVE_GSSHEIMDAL ON)
973     else() # MIT
974       set(HAVE_GSSMIT ON)
975       set(_INCLUDE_LIST "")
976       if(HAVE_GSSAPI_GSSAPI_H)
977         list(APPEND _INCLUDE_LIST "gssapi/gssapi.h")
978       endif()
979       if(HAVE_GSSAPI_GSSAPI_GENERIC_H)
980         list(APPEND _INCLUDE_LIST "gssapi/gssapi_generic.h")
981       endif()
982       if(HAVE_GSSAPI_GSSAPI_KRB5_H)
983         list(APPEND _INCLUDE_LIST "gssapi/gssapi_krb5.h")
984       endif()
985
986       string(REPLACE ";" " " _COMPILER_FLAGS_STR "${GSS_COMPILER_FLAGS}")
987       string(REPLACE ";" " " _LINKER_FLAGS_STR "${GSS_LINKER_FLAGS}")
988
989       foreach(_dir ${GSS_LINK_DIRECTORIES})
990         set(_LINKER_FLAGS_STR "${_LINKER_FLAGS_STR} -L\"${_dir}\"")
991       endforeach()
992
993       set(CMAKE_REQUIRED_FLAGS "${_COMPILER_FLAGS_STR} ${_LINKER_FLAGS_STR}")
994       set(CMAKE_REQUIRED_LIBRARIES ${GSS_LIBRARIES})
995       check_symbol_exists("GSS_C_NT_HOSTBASED_SERVICE" ${_INCLUDE_LIST} HAVE_GSS_C_NT_HOSTBASED_SERVICE)
996       if(NOT HAVE_GSS_C_NT_HOSTBASED_SERVICE)
997         set(HAVE_OLD_GSSMIT ON)
998       endif()
999       unset(CMAKE_REQUIRED_LIBRARIES)
1000
1001     endif()
1002
1003     include_directories(${GSS_INCLUDE_DIR})
1004     link_directories(${GSS_LINK_DIRECTORIES})
1005     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GSS_COMPILER_FLAGS}")
1006     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${GSS_LINKER_FLAGS}")
1007     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GSS_LINKER_FLAGS}")
1008     set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} ${GSS_LINKER_FLAGS}")
1009     list(APPEND CURL_LIBS ${GSS_LIBRARIES})
1010
1011   else()
1012     message(WARNING "GSSAPI support has been requested but no supporting libraries found. Skipping.")
1013   endif()
1014 endif()
1015
1016 option(ENABLE_UNIX_SOCKETS "Define if you want Unix domain sockets support" ON)
1017 if(ENABLE_UNIX_SOCKETS)
1018   include(CheckStructHasMember)
1019   if(WIN32)
1020     set(USE_UNIX_SOCKETS ON)
1021   else()
1022     check_struct_has_member("struct sockaddr_un" sun_path "sys/un.h" USE_UNIX_SOCKETS)
1023   endif()
1024 else()
1025   unset(USE_UNIX_SOCKETS CACHE)
1026 endif()
1027
1028
1029 if(0) # This code not needed for building within CMake.
1030 #
1031 # CA handling
1032 #
1033 set(CURL_CA_BUNDLE "auto" CACHE STRING
1034     "Path to the CA bundle. Set 'none' to disable or 'auto' for auto-detection. Defaults to 'auto'.")
1035 set(CURL_CA_FALLBACK OFF CACHE BOOL
1036     "Set ON to use built-in CA store of TLS backend. Defaults to OFF")
1037 set(CURL_CA_PATH "auto" CACHE STRING
1038     "Location of default CA path. Set 'none' to disable or 'auto' for auto-detection. Defaults to 'auto'.")
1039
1040 if("${CURL_CA_BUNDLE}" STREQUAL "")
1041   message(FATAL_ERROR "Invalid value of CURL_CA_BUNDLE. Use 'none', 'auto' or file path.")
1042 elseif("${CURL_CA_BUNDLE}" STREQUAL "none")
1043   unset(CURL_CA_BUNDLE CACHE)
1044 elseif("${CURL_CA_BUNDLE}" STREQUAL "auto")
1045   unset(CURL_CA_BUNDLE CACHE)
1046   set(CURL_CA_BUNDLE_AUTODETECT TRUE)
1047 else()
1048   set(CURL_CA_BUNDLE_SET TRUE)
1049 endif()
1050
1051 if("${CURL_CA_PATH}" STREQUAL "")
1052   message(FATAL_ERROR "Invalid value of CURL_CA_PATH. Use 'none', 'auto' or directory path.")
1053 elseif("${CURL_CA_PATH}" STREQUAL "none")
1054   unset(CURL_CA_PATH CACHE)
1055 elseif("${CURL_CA_PATH}" STREQUAL "auto")
1056   unset(CURL_CA_PATH CACHE)
1057   if(NOT USE_NSS)
1058     set(CURL_CA_PATH_AUTODETECT TRUE)
1059   endif()
1060 else()
1061   set(CURL_CA_PATH_SET TRUE)
1062 endif()
1063
1064 if(CURL_CA_BUNDLE_SET AND CURL_CA_PATH_AUTODETECT)
1065   # Skip autodetection of unset CA path because CA bundle is set explicitly
1066 elseif(CURL_CA_PATH_SET AND CURL_CA_BUNDLE_AUTODETECT)
1067   # Skip autodetection of unset CA bundle because CA path is set explicitly
1068 elseif(CURL_CA_PATH_AUTODETECT OR CURL_CA_BUNDLE_AUTODETECT)
1069   # first try autodetecting a CA bundle, then a CA path
1070
1071   if(CURL_CA_BUNDLE_AUTODETECT)
1072     set(SEARCH_CA_BUNDLE_PATHS
1073         /etc/ssl/certs/ca-certificates.crt
1074         /etc/pki/tls/certs/ca-bundle.crt
1075         /usr/share/ssl/certs/ca-bundle.crt
1076         /usr/local/share/certs/ca-root-nss.crt
1077         /etc/ssl/cert.pem)
1078
1079     foreach(SEARCH_CA_BUNDLE_PATH ${SEARCH_CA_BUNDLE_PATHS})
1080       if(EXISTS "${SEARCH_CA_BUNDLE_PATH}")
1081         message(STATUS "Found CA bundle: ${SEARCH_CA_BUNDLE_PATH}")
1082         set(CURL_CA_BUNDLE "${SEARCH_CA_BUNDLE_PATH}" CACHE STRING
1083             "Path to the CA bundle. Set 'none' to disable or 'auto' for auto-detection. Defaults to 'auto'.")
1084         set(CURL_CA_BUNDLE_SET TRUE CACHE BOOL "Path to the CA bundle has been set")
1085         break()
1086       endif()
1087     endforeach()
1088   endif()
1089
1090   if(CURL_CA_PATH_AUTODETECT AND (NOT CURL_CA_PATH_SET))
1091     if(EXISTS "/etc/ssl/certs")
1092       set(CURL_CA_PATH "/etc/ssl/certs" CACHE STRING
1093           "Location of default CA path. Set 'none' to disable or 'auto' for auto-detection. Defaults to 'auto'.")
1094       set(CURL_CA_PATH_SET TRUE CACHE BOOL "Path to the CA bundle has been set")
1095     endif()
1096   endif()
1097 endif()
1098
1099 if(CURL_CA_PATH_SET AND NOT USE_OPENSSL AND NOT USE_MBEDTLS)
1100   message(STATUS
1101           "CA path only supported by OpenSSL, GnuTLS or mbed TLS. "
1102           "Set CURL_CA_PATH=none or enable one of those TLS backends.")
1103 endif()
1104 endif()
1105
1106 # Check for header files
1107 if(NOT UNIX)
1108   check_include_file_concat("windows.h"      HAVE_WINDOWS_H)
1109   check_include_file_concat("ws2tcpip.h"     HAVE_WS2TCPIP_H)
1110   check_include_file_concat("winsock2.h"     HAVE_WINSOCK2_H)
1111   check_include_file_concat("wincrypt.h"     HAVE_WINCRYPT_H)
1112 else()
1113   set(HAVE_WINDOWS_H 0)
1114   set(HAVE_WINSOCK_H 0)
1115   set(HAVE_WS2TCPIP_H 0)
1116   set(HAVE_WINSOCK2_H 0)
1117 endif()
1118
1119 check_include_file_concat("stdio.h"          HAVE_STDIO_H)
1120 check_include_file_concat("inttypes.h"       HAVE_INTTYPES_H)
1121 check_include_file_concat("sys/filio.h"      HAVE_SYS_FILIO_H)
1122 check_include_file_concat("sys/ioctl.h"      HAVE_SYS_IOCTL_H)
1123 check_include_file_concat("sys/param.h"      HAVE_SYS_PARAM_H)
1124 check_include_file_concat("sys/poll.h"       HAVE_SYS_POLL_H)
1125 check_include_file_concat("sys/resource.h"   HAVE_SYS_RESOURCE_H)
1126 check_include_file_concat("sys/select.h"     HAVE_SYS_SELECT_H)
1127 check_include_file_concat("sys/socket.h"     HAVE_SYS_SOCKET_H)
1128 check_include_file_concat("sys/sockio.h"     HAVE_SYS_SOCKIO_H)
1129 check_include_file_concat("sys/stat.h"       HAVE_SYS_STAT_H)
1130 check_include_file_concat("sys/time.h"       HAVE_SYS_TIME_H)
1131 check_include_file_concat("sys/types.h"      HAVE_SYS_TYPES_H)
1132 check_include_file_concat("sys/uio.h"        HAVE_SYS_UIO_H)
1133 check_include_file_concat("sys/un.h"         HAVE_SYS_UN_H)
1134 check_include_file_concat("sys/utime.h"      HAVE_SYS_UTIME_H)
1135 check_include_file_concat("sys/xattr.h"      HAVE_SYS_XATTR_H)
1136 check_include_file_concat("alloca.h"         HAVE_ALLOCA_H)
1137 check_include_file_concat("arpa/inet.h"      HAVE_ARPA_INET_H)
1138 check_include_file_concat("arpa/tftp.h"      HAVE_ARPA_TFTP_H)
1139 check_include_file_concat("assert.h"         HAVE_ASSERT_H)
1140 check_include_file_concat("errno.h"          HAVE_ERRNO_H)
1141 check_include_file_concat("fcntl.h"          HAVE_FCNTL_H)
1142 check_include_file_concat("idn2.h"           HAVE_IDN2_H)
1143 check_include_file_concat("ifaddrs.h"        HAVE_IFADDRS_H)
1144 check_include_file_concat("io.h"             HAVE_IO_H)
1145 check_include_file_concat("krb.h"            HAVE_KRB_H)
1146 check_include_file_concat("libgen.h"         HAVE_LIBGEN_H)
1147 check_include_file_concat("locale.h"         HAVE_LOCALE_H)
1148 check_include_file_concat("net/if.h"         HAVE_NET_IF_H)
1149 check_include_file_concat("netdb.h"          HAVE_NETDB_H)
1150 check_include_file_concat("netinet/in.h"     HAVE_NETINET_IN_H)
1151 check_include_file_concat("netinet/tcp.h"    HAVE_NETINET_TCP_H)
1152 check_include_file("linux/tcp.h"      HAVE_LINUX_TCP_H)
1153
1154 check_include_file_concat("pem.h"            HAVE_PEM_H)
1155 check_include_file_concat("poll.h"           HAVE_POLL_H)
1156 check_include_file_concat("pwd.h"            HAVE_PWD_H)
1157 check_include_file_concat("setjmp.h"         HAVE_SETJMP_H)
1158 check_include_file_concat("signal.h"         HAVE_SIGNAL_H)
1159 check_include_file_concat("ssl.h"            HAVE_SSL_H)
1160 check_include_file_concat("stdbool.h"        HAVE_STDBOOL_H)
1161 check_include_file_concat("stdint.h"         HAVE_STDINT_H)
1162 check_include_file_concat("stdio.h"          HAVE_STDIO_H)
1163 check_include_file_concat("stdlib.h"         HAVE_STDLIB_H)
1164 check_include_file_concat("string.h"         HAVE_STRING_H)
1165 check_include_file_concat("strings.h"        HAVE_STRINGS_H)
1166 check_include_file_concat("stropts.h"        HAVE_STROPTS_H)
1167 check_include_file_concat("termio.h"         HAVE_TERMIO_H)
1168 check_include_file_concat("termios.h"        HAVE_TERMIOS_H)
1169 check_include_file_concat("time.h"           HAVE_TIME_H)
1170 check_include_file_concat("unistd.h"         HAVE_UNISTD_H)
1171 check_include_file_concat("utime.h"          HAVE_UTIME_H)
1172 check_include_file_concat("x509.h"           HAVE_X509_H)
1173
1174 check_include_file_concat("process.h"        HAVE_PROCESS_H)
1175 check_include_file_concat("stddef.h"         HAVE_STDDEF_H)
1176 check_include_file_concat("dlfcn.h"          HAVE_DLFCN_H)
1177 check_include_file_concat("malloc.h"         HAVE_MALLOC_H)
1178 check_include_file_concat("memory.h"         HAVE_MEMORY_H)
1179 check_include_file_concat("netinet/if_ether.h" HAVE_NETINET_IF_ETHER_H)
1180 check_include_file_concat("stdint.h"        HAVE_STDINT_H)
1181 check_include_file_concat("sockio.h"        HAVE_SOCKIO_H)
1182 check_include_file_concat("sys/utsname.h"   HAVE_SYS_UTSNAME_H)
1183
1184 check_type_size(size_t  SIZEOF_SIZE_T)
1185 check_type_size(ssize_t  SIZEOF_SSIZE_T)
1186 check_type_size("time_t"  SIZEOF_TIME_T)
1187
1188 find_file(RANDOM_FILE urandom /dev)
1189 mark_as_advanced(RANDOM_FILE)
1190
1191 # Check for some functions that are used
1192 if(HAVE_LIBWS2_32)
1193   set(CMAKE_REQUIRED_LIBRARIES ws2_32)
1194 elseif(HAVE_LIBSOCKET)
1195   set(CMAKE_REQUIRED_LIBRARIES socket)
1196 elseif(HAVE_LIBNETWORK)
1197   set(CMAKE_REQUIRED_LIBRARIES network)
1198 endif()
1199
1200 check_symbol_exists(basename      "${CURL_INCLUDES}" HAVE_BASENAME)
1201 check_symbol_exists(socket        "${CURL_INCLUDES}" HAVE_SOCKET)
1202 check_symbol_exists(select        "${CURL_INCLUDES}" HAVE_SELECT)
1203 check_symbol_exists(poll          "${CURL_INCLUDES}" HAVE_POLL)
1204 check_symbol_exists(strdup        "${CURL_INCLUDES}" HAVE_STRDUP)
1205 check_symbol_exists(strstr        "${CURL_INCLUDES}" HAVE_STRSTR)
1206 check_symbol_exists(strtok_r      "${CURL_INCLUDES}" HAVE_STRTOK_R)
1207 check_symbol_exists(strftime      "${CURL_INCLUDES}" HAVE_STRFTIME)
1208 check_symbol_exists(uname         "${CURL_INCLUDES}" HAVE_UNAME)
1209 check_symbol_exists(strcasecmp    "${CURL_INCLUDES}" HAVE_STRCASECMP)
1210 check_symbol_exists(stricmp       "${CURL_INCLUDES}" HAVE_STRICMP)
1211 check_symbol_exists(strcmpi       "${CURL_INCLUDES}" HAVE_STRCMPI)
1212 check_symbol_exists(strncmpi      "${CURL_INCLUDES}" HAVE_STRNCMPI)
1213 check_symbol_exists(alarm         "${CURL_INCLUDES}" HAVE_ALARM)
1214 if(NOT HAVE_STRNCMPI)
1215   set(HAVE_STRCMPI)
1216 endif()
1217 check_symbol_exists(getppid       "${CURL_INCLUDES}" HAVE_GETPPID)
1218 check_symbol_exists(utimes        "${CURL_INCLUDES}" HAVE_UTIMES)
1219
1220 check_symbol_exists(gettimeofday  "${CURL_INCLUDES}" HAVE_GETTIMEOFDAY)
1221 check_symbol_exists(inet_addr     "${CURL_INCLUDES}" HAVE_INET_ADDR)
1222 check_symbol_exists(closesocket   "${CURL_INCLUDES}" HAVE_CLOSESOCKET)
1223 check_symbol_exists(sigsetjmp     "${CURL_INCLUDES}" HAVE_SIGSETJMP)
1224 check_symbol_exists(getpass_r     "${CURL_INCLUDES}" HAVE_GETPASS_R)
1225 check_symbol_exists(getpwuid      "${CURL_INCLUDES}" HAVE_GETPWUID)
1226 check_symbol_exists(getpwuid_r    "${CURL_INCLUDES}" HAVE_GETPWUID_R)
1227 check_symbol_exists(geteuid       "${CURL_INCLUDES}" HAVE_GETEUID)
1228 check_symbol_exists(usleep        "${CURL_INCLUDES}" HAVE_USLEEP)
1229 check_symbol_exists(utime         "${CURL_INCLUDES}" HAVE_UTIME)
1230 check_symbol_exists(gmtime_r      "${CURL_INCLUDES}" HAVE_GMTIME_R)
1231 check_symbol_exists(localtime_r   "${CURL_INCLUDES}" HAVE_LOCALTIME_R)
1232
1233 check_symbol_exists(gethostbyname   "${CURL_INCLUDES}" HAVE_GETHOSTBYNAME)
1234 check_symbol_exists(gethostbyname_r "${CURL_INCLUDES}" HAVE_GETHOSTBYNAME_R)
1235
1236 check_symbol_exists(signal        "${CURL_INCLUDES}" HAVE_SIGNAL_FUNC)
1237 check_symbol_exists(SIGALRM       "${CURL_INCLUDES}" HAVE_SIGNAL_MACRO)
1238 if(HAVE_SIGNAL_FUNC AND HAVE_SIGNAL_MACRO)
1239   set(HAVE_SIGNAL 1)
1240 endif()
1241 check_symbol_exists(uname          "${CURL_INCLUDES}" HAVE_UNAME)
1242 check_symbol_exists(strtoll        "${CURL_INCLUDES}" HAVE_STRTOLL)
1243 check_symbol_exists(_strtoi64      "${CURL_INCLUDES}" HAVE__STRTOI64)
1244 check_symbol_exists(strerror_r     "${CURL_INCLUDES}" HAVE_STRERROR_R)
1245 check_symbol_exists(siginterrupt   "${CURL_INCLUDES}" HAVE_SIGINTERRUPT)
1246 check_symbol_exists(getaddrinfo    "${CURL_INCLUDES}" HAVE_GETADDRINFO)
1247 check_symbol_exists(freeaddrinfo   "${CURL_INCLUDES}" HAVE_FREEADDRINFO)
1248 check_symbol_exists(pipe           "${CURL_INCLUDES}" HAVE_PIPE)
1249 check_symbol_exists(ftruncate      "${CURL_INCLUDES}" HAVE_FTRUNCATE)
1250 check_symbol_exists(getprotobyname "${CURL_INCLUDES}" HAVE_GETPROTOBYNAME)
1251 check_symbol_exists(getpeername    "${CURL_INCLUDES}" HAVE_GETPEERNAME)
1252 check_symbol_exists(getsockname    "${CURL_INCLUDES}" HAVE_GETSOCKNAME)
1253 check_symbol_exists(if_nametoindex "${CURL_INCLUDES}" HAVE_IF_NAMETOINDEX)
1254 check_symbol_exists(getrlimit      "${CURL_INCLUDES}" HAVE_GETRLIMIT)
1255 check_symbol_exists(setlocale      "${CURL_INCLUDES}" HAVE_SETLOCALE)
1256 check_symbol_exists(setmode        "${CURL_INCLUDES}" HAVE_SETMODE)
1257 check_symbol_exists(setrlimit      "${CURL_INCLUDES}" HAVE_SETRLIMIT)
1258 check_symbol_exists(fcntl          "${CURL_INCLUDES}" HAVE_FCNTL)
1259 check_symbol_exists(ioctl          "${CURL_INCLUDES}" HAVE_IOCTL)
1260 check_symbol_exists(setsockopt     "${CURL_INCLUDES}" HAVE_SETSOCKOPT)
1261 check_function_exists(mach_absolute_time HAVE_MACH_ABSOLUTE_TIME)
1262 check_symbol_exists(inet_pton      "${CURL_INCLUDES}" HAVE_INET_PTON)
1263
1264 check_symbol_exists(fsetxattr "${CURL_INCLUDES}" HAVE_FSETXATTR)
1265 if(HAVE_FSETXATTR)
1266   foreach(CURL_TEST HAVE_FSETXATTR_5 HAVE_FSETXATTR_6)
1267     curl_internal_test(${CURL_TEST})
1268   endforeach()
1269 endif()
1270
1271 set(CMAKE_EXTRA_INCLUDE_FILES   "sys/socket.h")
1272 check_type_size("sa_family_t"   SIZEOF_SA_FAMILY_T)
1273 set(HAVE_SA_FAMILY_T            ${HAVE_SIZEOF_SA_FAMILY_T})
1274 set(CMAKE_EXTRA_INCLUDE_FILES   "")
1275
1276 set(CMAKE_EXTRA_INCLUDE_FILES   "ws2def.h")
1277 check_type_size("ADDRESS_FAMILY"    SIZEOF_ADDRESS_FAMILY)
1278 set(HAVE_ADDRESS_FAMILY         ${HAVE_SIZEOF_ADDRESS_FAMILY})
1279 set(CMAKE_EXTRA_INCLUDE_FILES   "")
1280
1281 # sigaction and sigsetjmp are special. Use special mechanism for
1282 # detecting those, but only if previous attempt failed.
1283 if(HAVE_SIGNAL_H)
1284   check_symbol_exists(sigaction "signal.h" HAVE_SIGACTION)
1285 endif()
1286
1287 if(NOT HAVE_SIGSETJMP)
1288   if(HAVE_SETJMP_H)
1289     check_symbol_exists(sigsetjmp "setjmp.h" HAVE_MACRO_SIGSETJMP)
1290     if(HAVE_MACRO_SIGSETJMP)
1291       set(HAVE_SIGSETJMP 1)
1292     endif()
1293   endif()
1294 endif()
1295
1296 # If there is no stricmp(), do not allow LDAP to parse URLs
1297 if(NOT HAVE_STRICMP)
1298   set(HAVE_LDAP_URL_PARSE 1)
1299 endif()
1300
1301 # Do curl specific tests
1302 foreach(CURL_TEST
1303     HAVE_FCNTL_O_NONBLOCK
1304     HAVE_IOCTLSOCKET
1305     HAVE_IOCTLSOCKET_CAMEL
1306     HAVE_IOCTLSOCKET_CAMEL_FIONBIO
1307     HAVE_IOCTLSOCKET_FIONBIO
1308     HAVE_IOCTL_FIONBIO
1309     HAVE_IOCTL_SIOCGIFADDR
1310     HAVE_SETSOCKOPT_SO_NONBLOCK
1311     HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
1312     TIME_WITH_SYS_TIME
1313     HAVE_O_NONBLOCK
1314     HAVE_GETHOSTBYNAME_R_3
1315     HAVE_GETHOSTBYNAME_R_5
1316     HAVE_GETHOSTBYNAME_R_6
1317     HAVE_GETHOSTBYNAME_R_3_REENTRANT
1318     HAVE_GETHOSTBYNAME_R_5_REENTRANT
1319     HAVE_GETHOSTBYNAME_R_6_REENTRANT
1320     HAVE_IN_ADDR_T
1321     HAVE_BOOL_T
1322     STDC_HEADERS
1323     HAVE_GETADDRINFO
1324     HAVE_FILE_OFFSET_BITS
1325     HAVE_VARIADIC_MACROS_C99
1326     HAVE_VARIADIC_MACROS_GCC
1327     )
1328   curl_internal_test(${CURL_TEST})
1329 endforeach()
1330
1331 if(HAVE_FILE_OFFSET_BITS)
1332   set(_FILE_OFFSET_BITS 64)
1333   set(CMAKE_REQUIRED_FLAGS "-D_FILE_OFFSET_BITS=64")
1334 endif()
1335 check_type_size("off_t"  SIZEOF_OFF_T)
1336
1337 # include this header to get the type
1338 set(CMAKE_REQUIRED_INCLUDES "${CURL_SOURCE_DIR}/include")
1339 set(CMAKE_EXTRA_INCLUDE_FILES "curl/system.h")
1340 check_type_size("curl_off_t"  SIZEOF_CURL_OFF_T)
1341 set(CMAKE_EXTRA_INCLUDE_FILES "")
1342
1343 set(CMAKE_REQUIRED_FLAGS)
1344
1345 foreach(CURL_TEST
1346     HAVE_GLIBC_STRERROR_R
1347     HAVE_POSIX_STRERROR_R
1348     )
1349   curl_internal_test(${CURL_TEST})
1350 endforeach()
1351
1352 # Check for reentrant
1353 foreach(CURL_TEST
1354     HAVE_GETHOSTBYNAME_R_3
1355     HAVE_GETHOSTBYNAME_R_5
1356     HAVE_GETHOSTBYNAME_R_6)
1357   if(NOT ${CURL_TEST})
1358     if(${CURL_TEST}_REENTRANT)
1359       set(NEED_REENTRANT 1)
1360     endif()
1361   endif()
1362 endforeach()
1363
1364 if(NEED_REENTRANT)
1365   foreach(CURL_TEST
1366       HAVE_GETHOSTBYNAME_R_3
1367       HAVE_GETHOSTBYNAME_R_5
1368       HAVE_GETHOSTBYNAME_R_6)
1369     set(${CURL_TEST} 0)
1370     if(${CURL_TEST}_REENTRANT)
1371       set(${CURL_TEST} 1)
1372     endif()
1373   endforeach()
1374 endif()
1375
1376 # Check clock_gettime(CLOCK_MONOTONIC, x) support
1377 curl_internal_test(HAVE_CLOCK_GETTIME_MONOTONIC)
1378
1379 # Check compiler support of __builtin_available()
1380 curl_internal_test(HAVE_BUILTIN_AVAILABLE)
1381
1382 # Some other minor tests
1383
1384 if(NOT HAVE_IN_ADDR_T)
1385   set(in_addr_t "unsigned long")
1386 endif()
1387
1388 # Fix libz / zlib.h
1389
1390 if(NOT CURL_SPECIAL_LIBZ)
1391   if(NOT HAVE_LIBZ)
1392     set(HAVE_ZLIB_H 0)
1393   endif()
1394
1395   if(NOT HAVE_ZLIB_H)
1396     set(HAVE_LIBZ 0)
1397   endif()
1398 endif()
1399
1400 # Check for nonblocking
1401 set(HAVE_DISABLED_NONBLOCKING 1)
1402 if(HAVE_FIONBIO OR
1403     HAVE_IOCTLSOCKET OR
1404     HAVE_IOCTLSOCKET_CASE OR
1405     HAVE_O_NONBLOCK)
1406   set(HAVE_DISABLED_NONBLOCKING)
1407 endif()
1408
1409 if(CMAKE_COMPILER_IS_GNUCC AND APPLE)
1410   include(CheckCCompilerFlag)
1411   check_c_compiler_flag(-Wno-long-double HAVE_C_FLAG_Wno_long_double)
1412   if(HAVE_C_FLAG_Wno_long_double)
1413     # The Mac version of GCC warns about use of long double.  Disable it.
1414     get_source_file_property(MPRINTF_COMPILE_FLAGS mprintf.c COMPILE_FLAGS)
1415     if(MPRINTF_COMPILE_FLAGS)
1416       set(MPRINTF_COMPILE_FLAGS "${MPRINTF_COMPILE_FLAGS} -Wno-long-double")
1417     else()
1418       set(MPRINTF_COMPILE_FLAGS "-Wno-long-double")
1419     endif()
1420     set_source_files_properties(mprintf.c PROPERTIES
1421       COMPILE_FLAGS ${MPRINTF_COMPILE_FLAGS})
1422   endif()
1423 endif()
1424
1425 # TODO test which of these headers are required
1426 if(WIN32)
1427   set(CURL_PULL_WS2TCPIP_H ${HAVE_WS2TCPIP_H})
1428 else()
1429   set(CURL_PULL_SYS_TYPES_H ${HAVE_SYS_TYPES_H})
1430   set(CURL_PULL_SYS_SOCKET_H ${HAVE_SYS_SOCKET_H})
1431   set(CURL_PULL_SYS_POLL_H ${HAVE_SYS_POLL_H})
1432 endif()
1433 set(CURL_PULL_STDINT_H ${HAVE_STDINT_H})
1434 set(CURL_PULL_INTTYPES_H ${HAVE_INTTYPES_H})
1435
1436 include(CMake/OtherTests.cmake)
1437
1438 add_definitions(-DHAVE_CONFIG_H)
1439
1440 # For Windows, all compilers used by CMake should support large files
1441 if(WIN32)
1442   set(USE_WIN32_LARGE_FILES ON)
1443
1444   # Use the manifest embedded in the Windows Resource
1445   set(CMAKE_RC_FLAGS "${CMAKE_RC_FLAGS} -DCURL_EMBED_MANIFEST")
1446
1447   # Check if crypto functions in wincrypt.h are actually available
1448   if(HAVE_WINCRYPT_H)
1449     check_symbol_exists(CryptAcquireContext "${CURL_INCLUDES}" USE_WINCRYPT)
1450   endif()
1451   if(USE_WINCRYPT)
1452     set(USE_WIN32_CRYPTO ON)
1453   endif()
1454
1455   # Link required libraries for USE_WIN32_CRYPTO or USE_SCHANNEL
1456   if(USE_WIN32_CRYPTO OR USE_SCHANNEL)
1457     list(APPEND CURL_LIBS "advapi32" "crypt32")
1458   endif()
1459 endif()
1460
1461 if(MSVC)
1462   # Disable default manifest added by CMake
1463   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /MANIFEST:NO")
1464
1465   add_definitions(-D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE)
1466   if(CMAKE_C_FLAGS MATCHES "/W[0-4]")
1467     string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
1468   else()
1469     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4")
1470   endif()
1471
1472   # Use multithreaded compilation on VS 2008+
1473   if(MSVC_VERSION GREATER_EQUAL 1500)
1474     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP")
1475   endif()
1476 endif()
1477
1478 if(CURL_WERROR)
1479   if(MSVC_VERSION)
1480     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX")
1481   else()
1482     # this assumes clang or gcc style options
1483     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
1484   endif()
1485 endif()
1486
1487 if(CURL_LTO)
1488   if(CMAKE_VERSION VERSION_LESS 3.9)
1489     message(FATAL_ERROR "Requested LTO but your cmake version ${CMAKE_VERSION} is to old. You need at least 3.9")
1490   endif()
1491
1492   cmake_policy(SET CMP0069 NEW)
1493
1494   include(CheckIPOSupported)
1495   check_ipo_supported(RESULT CURL_HAS_LTO OUTPUT CURL_LTO_ERROR LANGUAGES C)
1496   if(CURL_HAS_LTO)
1497     message(STATUS "LTO supported and enabled")
1498   else()
1499     message(FATAL_ERROR "LTO was requested - but compiler doesn't support it\n${CURL_LTO_ERROR}")
1500   endif()
1501 endif()
1502
1503
1504 # Ugly (but functional) way to include "Makefile.inc" by transforming it (= regenerate it).
1505 function(transform_makefile_inc INPUT_FILE OUTPUT_FILE)
1506   file(READ ${INPUT_FILE} MAKEFILE_INC_TEXT)
1507   string(REPLACE "$(top_srcdir)"   "\${CURL_SOURCE_DIR}" MAKEFILE_INC_TEXT ${MAKEFILE_INC_TEXT})
1508   string(REPLACE "$(top_builddir)" "\${CURL_BINARY_DIR}" MAKEFILE_INC_TEXT ${MAKEFILE_INC_TEXT})
1509
1510   string(REGEX REPLACE "\\\\\n" "!Ï€!α!" MAKEFILE_INC_TEXT ${MAKEFILE_INC_TEXT})
1511   string(REGEX REPLACE "([a-zA-Z_][a-zA-Z0-9_]*)[\t ]*=[\t ]*([^\n]*)" "SET(\\1 \\2)" MAKEFILE_INC_TEXT ${MAKEFILE_INC_TEXT})
1512   string(REPLACE "!Ï€!α!" "\n" MAKEFILE_INC_TEXT ${MAKEFILE_INC_TEXT})
1513
1514   string(REGEX REPLACE "\\$\\(([a-zA-Z_][a-zA-Z0-9_]*)\\)" "\${\\1}" MAKEFILE_INC_TEXT ${MAKEFILE_INC_TEXT})    # Replace $() with ${}
1515   string(REGEX REPLACE "@([a-zA-Z_][a-zA-Z0-9_]*)@" "\${\\1}" MAKEFILE_INC_TEXT ${MAKEFILE_INC_TEXT})    # Replace @@ with ${}, even if that may not be read by CMake scripts.
1516   file(WRITE ${OUTPUT_FILE} ${MAKEFILE_INC_TEXT})
1517   set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${INPUT_FILE}")
1518 endfunction()
1519
1520 if(0) # This code not needed for building within CMake.
1521 include(GNUInstallDirs)
1522
1523 set(CURL_INSTALL_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
1524 set(TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets")
1525 set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated")
1526 set(project_config "${generated_dir}/${PROJECT_NAME}Config.cmake")
1527 set(version_config "${generated_dir}/${PROJECT_NAME}ConfigVersion.cmake")
1528 endif()
1529
1530 if(USE_MANUAL)
1531   add_subdirectory(docs)
1532 endif()
1533
1534 add_subdirectory(lib)
1535
1536 if(BUILD_CURL_EXE)
1537   add_subdirectory(src)
1538 endif()
1539
1540 #-----------------------------------------------------------------------------
1541 # CMake-specific curl code.
1542 add_executable(curltest curltest.c)
1543 target_link_libraries(curltest cmcurl)
1544
1545 if(BUILD_TESTING AND CMAKE_CURL_TEST_URL)
1546   add_test(curl curltest ${CMAKE_CURL_TEST_URL})
1547 endif()
1548
1549 install(FILES COPYING DESTINATION ${CMAKE_DOC_DIR}/cmcurl)
1550 #-----------------------------------------------------------------------------
1551
1552 if(0) # This code not needed for building within CMake.
1553 cmake_dependent_option(BUILD_TESTING "Build tests"
1554   ON "PERL_FOUND;NOT CURL_DISABLE_TESTS"
1555   OFF)
1556 if(BUILD_TESTING)
1557   add_subdirectory(tests)
1558 endif()
1559
1560 # Helper to populate a list (_items) with a label when conditions (the remaining
1561 # args) are satisfied
1562 macro(_add_if label)
1563   # needs to be a macro to allow this indirection
1564   if(${ARGN})
1565     set(_items ${_items} "${label}")
1566   endif()
1567 endmacro()
1568
1569 # NTLM support requires crypto function adaptions from various SSL libs
1570 # TODO alternative SSL libs tests for SSP1, GNUTLS, NSS
1571 if(NOT (CURL_DISABLE_CRYPTO_AUTH OR CURL_DISABLE_NTLM) AND
1572     (USE_OPENSSL OR USE_MBEDTLS OR USE_DARWINSSL OR USE_WIN32_CRYPTO))
1573   set(use_curl_ntlm_core ON)
1574 endif()
1575
1576 # Clear list and try to detect available features
1577 set(_items)
1578 _add_if("SSL"           SSL_ENABLED)
1579 _add_if("IPv6"          ENABLE_IPV6)
1580 _add_if("unixsockets"   USE_UNIX_SOCKETS)
1581 _add_if("libz"          HAVE_LIBZ)
1582 _add_if("brotli"        HAVE_BROTLI)
1583 _add_if("zstd"          HAVE_ZSTD)
1584 _add_if("AsynchDNS"     USE_ARES OR USE_THREADS_POSIX OR USE_THREADS_WIN32)
1585 _add_if("IDN"           HAVE_LIBIDN2 OR USE_WIN32_IDN)
1586 _add_if("Largefile"     (SIZEOF_CURL_OFF_T GREATER 4) AND
1587                         ((SIZEOF_OFF_T GREATER 4) OR USE_WIN32_LARGE_FILES))
1588 # TODO SSP1 (Schannel) check is missing
1589 _add_if("SSPI"          USE_WINDOWS_SSPI)
1590 _add_if("GSS-API"       HAVE_GSSAPI)
1591 _add_if("alt-svc"       NOT CURL_DISABLE_ALTSVC)
1592 _add_if("HSTS"          NOT CURL_DISABLE_HSTS)
1593 # TODO SSP1 missing for SPNEGO
1594 _add_if("SPNEGO"        NOT CURL_DISABLE_CRYPTO_AUTH AND
1595                         (HAVE_GSSAPI OR USE_WINDOWS_SSPI))
1596 _add_if("Kerberos"      NOT CURL_DISABLE_CRYPTO_AUTH AND
1597                         (HAVE_GSSAPI OR USE_WINDOWS_SSPI))
1598 # NTLM support requires crypto function adaptions from various SSL libs
1599 # TODO alternative SSL libs tests for SSP1, GNUTLS, NSS
1600 _add_if("NTLM"          NOT (CURL_DISABLE_CRYPTO_AUTH OR CURL_DISABLE_NTLM) AND
1601                         (use_curl_ntlm_core OR USE_WINDOWS_SSPI))
1602 # TODO missing option (autoconf: --enable-ntlm-wb)
1603 _add_if("NTLM_WB"       NOT (CURL_DISABLE_CRYPTO_AUTH OR CURL_DISABLE_NTLM) AND
1604                         (use_curl_ntlm_core OR USE_WINDOWS_SSPI) AND
1605                         NOT CURL_DISABLE_HTTP AND NTLM_WB_ENABLED)
1606 # TODO missing option (--enable-tls-srp), depends on GNUTLS_SRP/OPENSSL_SRP
1607 _add_if("TLS-SRP"       USE_TLS_SRP)
1608 # TODO option --with-nghttp2 tests for nghttp2 lib and nghttp2/nghttp2.h header
1609 _add_if("HTTP2"         USE_NGHTTP2)
1610 _add_if("HTTP3"         USE_NGTCP2 OR USE_QUICHE)
1611 _add_if("MultiSSL"      CURL_WITH_MULTI_SSL)
1612 _add_if("HTTPS-proxy"   SSL_ENABLED AND (USE_OPENSSL OR USE_GNUTLS OR USE_NSS))
1613 _add_if("unicode"       ENABLE_UNICODE)
1614 string(REPLACE ";" " " SUPPORT_FEATURES "${_items}")
1615 message(STATUS "Enabled features: ${SUPPORT_FEATURES}")
1616
1617 # Clear list and try to detect available protocols
1618 set(_items)
1619 _add_if("HTTP"          NOT CURL_DISABLE_HTTP)
1620 _add_if("HTTPS"         NOT CURL_DISABLE_HTTP AND SSL_ENABLED)
1621 _add_if("FTP"           NOT CURL_DISABLE_FTP)
1622 _add_if("FTPS"          NOT CURL_DISABLE_FTP AND SSL_ENABLED)
1623 _add_if("FILE"          NOT CURL_DISABLE_FILE)
1624 _add_if("TELNET"        NOT CURL_DISABLE_TELNET)
1625 _add_if("LDAP"          NOT CURL_DISABLE_LDAP)
1626 # CURL_DISABLE_LDAP implies CURL_DISABLE_LDAPS
1627 # TODO check HAVE_LDAP_SSL (in autoconf this is enabled with --enable-ldaps)
1628 _add_if("LDAPS"         NOT CURL_DISABLE_LDAPS AND
1629                         ((USE_OPENLDAP AND SSL_ENABLED) OR
1630                         (NOT USE_OPENLDAP AND HAVE_LDAP_SSL)))
1631 _add_if("DICT"          NOT CURL_DISABLE_DICT)
1632 _add_if("TFTP"          NOT CURL_DISABLE_TFTP)
1633 _add_if("GOPHER"        NOT CURL_DISABLE_GOPHER)
1634 _add_if("GOPHERS"       NOT CURL_DISABLE_GOPHER AND SSL_ENABLED)
1635 _add_if("POP3"          NOT CURL_DISABLE_POP3)
1636 _add_if("POP3S"         NOT CURL_DISABLE_POP3 AND SSL_ENABLED)
1637 _add_if("IMAP"          NOT CURL_DISABLE_IMAP)
1638 _add_if("IMAPS"         NOT CURL_DISABLE_IMAP AND SSL_ENABLED)
1639 _add_if("SMB"           NOT CURL_DISABLE_SMB AND
1640                         use_curl_ntlm_core AND (SIZEOF_CURL_OFF_T GREATER 4))
1641 _add_if("SMBS"          NOT CURL_DISABLE_SMB AND SSL_ENABLED AND
1642                         use_curl_ntlm_core AND (SIZEOF_CURL_OFF_T GREATER 4))
1643 _add_if("SMTP"          NOT CURL_DISABLE_SMTP)
1644 _add_if("SMTPS"         NOT CURL_DISABLE_SMTP AND SSL_ENABLED)
1645 _add_if("SCP"           USE_LIBSSH2 OR USE_LIBSSH)
1646 _add_if("SFTP"          USE_LIBSSH2 OR USE_LIBSSH)
1647 _add_if("RTSP"          NOT CURL_DISABLE_RTSP)
1648 _add_if("RTMP"          USE_LIBRTMP)
1649 _add_if("MQTT"          NOT CURL_DISABLE_MQTT)
1650 if(_items)
1651   list(SORT _items)
1652 endif()
1653 string(REPLACE ";" " " SUPPORT_PROTOCOLS "${_items}")
1654 message(STATUS "Enabled protocols: ${SUPPORT_PROTOCOLS}")
1655
1656 # Clear list and collect SSL backends
1657 set(_items)
1658 _add_if("Schannel"         SSL_ENABLED AND USE_SCHANNEL)
1659 _add_if("OpenSSL"          SSL_ENABLED AND USE_OPENSSL)
1660 _add_if("Secure Transport" SSL_ENABLED AND USE_SECTRANSP)
1661 _add_if("mbedTLS"          SSL_ENABLED AND USE_MBEDTLS)
1662 _add_if("BearSSL"          SSL_ENABLED AND USE_BEARSSL)
1663 _add_if("NSS"              SSL_ENABLED AND USE_NSS)
1664 _add_if("wolfSSL"          SSL_ENABLED AND USE_WOLFSSL)
1665 if(_items)
1666   list(SORT _items)
1667 endif()
1668 string(REPLACE ";" " " SSL_BACKENDS "${_items}")
1669 message(STATUS "Enabled SSL backends: ${SSL_BACKENDS}")
1670
1671 # curl-config needs the following options to be set.
1672 set(CC                      "${CMAKE_C_COMPILER}")
1673 # TODO probably put a -D... options here?
1674 set(CONFIGURE_OPTIONS       "")
1675 # TODO when to set "-DCURL_STATICLIB" for CPPFLAG_CURL_STATICLIB?
1676 set(CPPFLAG_CURL_STATICLIB  "")
1677 set(CURLVERSION             "${CURL_VERSION}")
1678 set(exec_prefix             "\${prefix}")
1679 set(includedir              "\${prefix}/include")
1680 set(LDFLAGS                 "${CMAKE_SHARED_LINKER_FLAGS}")
1681 set(LIBCURL_LIBS            "")
1682 set(libdir                  "${CMAKE_INSTALL_PREFIX}/lib")
1683 foreach(_lib ${CMAKE_C_IMPLICIT_LINK_LIBRARIES} ${CURL_LIBS})
1684   if(TARGET "${_lib}")
1685     set(_libname "${_lib}")
1686     get_target_property(_imported "${_libname}" IMPORTED)
1687     if(NOT _imported)
1688       # Reading the LOCATION property on non-imported target will error out.
1689       # Assume the user won't need this information in the .pc file.
1690       continue()
1691     endif()
1692     get_target_property(_lib "${_libname}" LOCATION)
1693     if(NOT _lib)
1694       message(WARNING "Bad lib in library list: ${_libname}")
1695       continue()
1696     endif()
1697   endif()
1698   if(_lib MATCHES ".*/.*" OR _lib MATCHES "^-")
1699     set(LIBCURL_LIBS          "${LIBCURL_LIBS} ${_lib}")
1700   else()
1701     set(LIBCURL_LIBS          "${LIBCURL_LIBS} -l${_lib}")
1702   endif()
1703 endforeach()
1704 if(BUILD_SHARED_LIBS)
1705   set(ENABLE_SHARED         "yes")
1706   set(ENABLE_STATIC         "no")
1707   set(LIBCURL_NO_SHARED     "")
1708 else()
1709   set(ENABLE_SHARED         "no")
1710   set(ENABLE_STATIC         "yes")
1711   set(LIBCURL_NO_SHARED     "${LIBCURL_LIBS}")
1712 endif()
1713 # "a" (Linux) or "lib" (Windows)
1714 string(REPLACE "." "" libext "${CMAKE_STATIC_LIBRARY_SUFFIX}")
1715 set(prefix                  "${CMAKE_INSTALL_PREFIX}")
1716 # Set this to "yes" to append all libraries on which -lcurl is dependent
1717 set(REQUIRE_LIB_DEPS        "no")
1718 # SUPPORT_FEATURES
1719 # SUPPORT_PROTOCOLS
1720 set(VERSIONNUM              "${CURL_VERSION_NUM}")
1721
1722 # Finally generate a "curl-config" matching this config
1723 # Use:
1724 # * ENABLE_SHARED
1725 # * ENABLE_STATIC
1726 configure_file("${CURL_SOURCE_DIR}/curl-config.in"
1727                "${CURL_BINARY_DIR}/curl-config" @ONLY)
1728 install(FILES "${CURL_BINARY_DIR}/curl-config"
1729         DESTINATION ${CMAKE_INSTALL_BINDIR}
1730         PERMISSIONS
1731           OWNER_READ OWNER_WRITE OWNER_EXECUTE
1732           GROUP_READ GROUP_EXECUTE
1733           WORLD_READ WORLD_EXECUTE)
1734
1735 # Finally generate a pkg-config file matching this config
1736 configure_file("${CURL_SOURCE_DIR}/libcurl.pc.in"
1737                "${CURL_BINARY_DIR}/libcurl.pc" @ONLY)
1738 install(FILES "${CURL_BINARY_DIR}/libcurl.pc"
1739         DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
1740
1741 # install headers
1742 install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include/curl"
1743     DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
1744     FILES_MATCHING PATTERN "*.h")
1745
1746 include(CMakePackageConfigHelpers)
1747 write_basic_package_version_file(
1748     "${version_config}"
1749     VERSION ${CURL_VERSION}
1750     COMPATIBILITY SameMajorVersion
1751 )
1752
1753 # Use:
1754 # * TARGETS_EXPORT_NAME
1755 # * PROJECT_NAME
1756 configure_package_config_file(CMake/curl-config.cmake.in
1757         "${project_config}"
1758         INSTALL_DESTINATION ${CURL_INSTALL_CMAKE_DIR}
1759 )
1760
1761 if(CURL_ENABLE_EXPORT_TARGET)
1762   install(
1763           EXPORT "${TARGETS_EXPORT_NAME}"
1764           NAMESPACE "${PROJECT_NAME}::"
1765           DESTINATION ${CURL_INSTALL_CMAKE_DIR}
1766   )
1767 endif()
1768
1769 install(
1770         FILES ${version_config} ${project_config}
1771         DESTINATION ${CURL_INSTALL_CMAKE_DIR}
1772 )
1773
1774 # Workaround for MSVS10 to avoid the Dialog Hell
1775 # FIXME: This could be removed with future version of CMake.
1776 if(MSVC_VERSION EQUAL 1600)
1777   set(CURL_SLN_FILENAME "${CMAKE_CURRENT_BINARY_DIR}/CURL.sln")
1778   if(EXISTS "${CURL_SLN_FILENAME}")
1779     file(APPEND "${CURL_SLN_FILENAME}" "\n# This should be regenerated!\n")
1780   endif()
1781 endif()
1782
1783 if(NOT TARGET uninstall)
1784   configure_file(
1785       ${CMAKE_CURRENT_SOURCE_DIR}/CMake/cmake_uninstall.cmake.in
1786       ${CMAKE_CURRENT_BINARY_DIR}/CMake/cmake_uninstall.cmake
1787       IMMEDIATE @ONLY)
1788
1789   add_custom_target(uninstall
1790       COMMAND ${CMAKE_COMMAND} -P
1791       ${CMAKE_CURRENT_BINARY_DIR}/CMake/cmake_uninstall.cmake)
1792 endif()
1793 endif()