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