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