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