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