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