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