CMakeLists.txt 32.1 KB
Newer Older
1 2 3
# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.

4
cmake_minimum_required(VERSION 3.1...3.15 FATAL_ERROR)
5 6
set(CMAKE_USER_MAKE_RULES_OVERRIDE_C ${CMAKE_CURRENT_SOURCE_DIR}/Source/Modules/OverrideC.cmake)
set(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX ${CMAKE_CURRENT_SOURCE_DIR}/Source/Modules/OverrideCXX.cmake)
7
project(CMake)
8 9
unset(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX)
unset(CMAKE_USER_MAKE_RULES_OVERRIDE_C)
10

11 12 13 14 15 16 17 18 19 20 21
# FIXME: This block should go away after a transition period.
if(MSVC AND NOT CMAKE_VERSION VERSION_LESS 3.15)
  # Filter out MSVC runtime library flags that may have come from
  # the cache of an existing build tree or from scripts.
  foreach(l C CXX)
    foreach(c DEBUG MINSIZEREL RELEASE RELWITHDEBINFO)
      string(REGEX REPLACE "[-/]M[DT]d?( |$)" "" "CMAKE_${l}_FLAGS_${c}" "${CMAKE_${l}_FLAGS_${c}}")
    endforeach()
  endforeach()
endif()

22 23 24 25
# Make sure we can find internal find_package modules only used for
# building CMake and not for shipping externally
list(INSERT CMAKE_MODULE_PATH 0 ${CMake_SOURCE_DIR}/Source/Modules)

26
if(CMAKE_BOOTSTRAP)
27
  # Running from bootstrap script.  Set local variable and remove from cache.
28 29 30
  set(CMAKE_BOOTSTRAP 1)
  unset(CMAKE_BOOTSTRAP CACHE)
endif()
31

32 33 34 35
if(CMake_TEST_HOST_CMAKE)
  get_filename_component(CMake_TEST_EXTERNAL_CMAKE "${CMAKE_COMMAND}" DIRECTORY)
endif()

36
if(NOT CMake_TEST_EXTERNAL_CMAKE)
37 38 39 40 41 42 43 44
  if(CMAKE_SYSTEM_NAME STREQUAL "HP-UX")
    message(FATAL_ERROR
      "CMake no longer compiles on HP-UX.  See\n"
      "  https://gitlab.kitware.com/cmake/cmake/issues/17137\n"
      "Use CMake 3.9 or lower instead."
      )
  endif()

45 46
  set(CMake_BIN_DIR ${CMake_BINARY_DIR}/bin)
endif()
47

48 49 50 51 52 53 54 55 56 57
if(CMake_GUI_DISTRIBUTE_WITH_Qt_LGPL)
  if(CMake_GUI_DISTRIBUTE_WITH_Qt_LGPL MATCHES "^3|2\\.1$")
    set(USE_LGPL "${CMake_GUI_DISTRIBUTE_WITH_Qt_LGPL}")
  else()
    set(USE_LGPL "2.1")
  endif()
else()
  set(USE_LGPL "")
endif()

58
if("${CMake_SOURCE_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}")
59
  # Disallow architecture-specific try_run.  It may not run on the host.
60 61 62 63
  macro(TRY_RUN)
    if(CMAKE_TRY_COMPILE_OSX_ARCHITECTURES)
      message(FATAL_ERROR "TRY_RUN not allowed with CMAKE_TRY_COMPILE_OSX_ARCHITECTURES=[${CMAKE_TRY_COMPILE_OSX_ARCHITECTURES}]")
    else()
64
      _TRY_RUN(${ARGV})
65 66 67
    endif()
  endmacro()
endif()
68

69
# Use most-recent available language dialects with GNU and Clang
70
if(NOT DEFINED CMAKE_C_STANDARD AND NOT CMake_NO_C_STANDARD)
71 72 73 74 75 76
  include(${CMake_SOURCE_DIR}/Source/Checks/cm_c11_thread_local.cmake)
  if(NOT CMake_C11_THREAD_LOCAL_BROKEN)
    set(CMAKE_C_STANDARD 11)
  else()
    set(CMAKE_C_STANDARD 99)
  endif()
77
endif()
78
if(NOT DEFINED CMAKE_CXX_STANDARD AND NOT CMake_NO_CXX_STANDARD)
79 80
  if (CMAKE_CXX_COMPILER_ID STREQUAL SunPro AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.14)
    set(CMAKE_CXX_STANDARD 98)
81
  else()
82 83
    if(NOT CMAKE_VERSION VERSION_LESS 3.8)
      include(${CMake_SOURCE_DIR}/Source/Checks/cm_cxx17_check.cmake)
84
    else()
85 86 87 88 89 90 91 92 93 94 95
      set(CMake_CXX17_BROKEN 1)
    endif()
    if(NOT CMake_CXX17_BROKEN)
      set(CMAKE_CXX_STANDARD 17)
    else()
      include(${CMake_SOURCE_DIR}/Source/Checks/cm_cxx14_check.cmake)
      if(NOT CMake_CXX14_BROKEN)
        set(CMAKE_CXX_STANDARD 14)
      else()
        set(CMAKE_CXX_STANDARD 11)
      endif()
96
    endif()
97
  endif()
98
endif()
99
if(NOT CMake_TEST_EXTERNAL_CMAKE)
100 101 102 103
  # include special compile flags for some compilers
  include(CompileFlags.cmake)

  # check for available C++ features
104
  include(${CMake_SOURCE_DIR}/Source/Checks/cm_cxx_features.cmake)
105 106 107 108

  if(NOT CMake_HAVE_CXX_UNIQUE_PTR)
    message(FATAL_ERROR "The C++ compiler does not support C++11 (e.g. std::unique_ptr).")
  endif()
109
endif()
110

111 112
# set the internal encoding of CMake to UTF-8
set(KWSYS_ENCODING_DEFAULT_CODEPAGE CP_UTF8)
113

114 115 116 117 118 119 120 121 122 123 124
# option to use COMPONENT with install command
option(CMake_INSTALL_COMPONENTS "Using components when installing" OFF)
mark_as_advanced(CMake_INSTALL_COMPONENTS)
macro(CMake_OPTIONAL_COMPONENT NAME)
  if(CMake_INSTALL_COMPONENTS)
    set(COMPONENT COMPONENT ${NAME})
  else()
    set(COMPONENT)
  endif()
endmacro()

125 126
# option to disable installing 3rd-party dependencies
option(CMake_INSTALL_DEPENDENCIES
127
  "Whether to install 3rd-party runtime dependencies" OFF)
128 129
mark_as_advanced(CMake_INSTALL_DEPENDENCIES)

130 131 132 133 134
# option to build reference for CMake developers
option(CMake_BUILD_DEVELOPER_REFERENCE
  "Build CMake Developer Reference" OFF)
mark_as_advanced(CMake_BUILD_DEVELOPER_REFERENCE)

135 136 137 138 139 140 141 142 143 144 145 146
# option to build using interprocedural optimizations (IPO/LTO)
if (NOT CMAKE_VERSION VERSION_LESS 3.12.2)
  option(CMake_BUILD_LTO "Compile CMake with link-time optimization if supported" OFF)
  if(CMake_BUILD_LTO)
    include(CheckIPOSupported)
    check_ipo_supported(RESULT HAVE_IPO)
    if(HAVE_IPO)
      set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
    endif()
  endif()
endif()

147 148 149 150
#-----------------------------------------------------------------------
# a macro to deal with system libraries, implemented as a macro
# simply to improve readability of the main script
#-----------------------------------------------------------------------
151
macro(CMAKE_HANDLE_SYSTEM_LIBRARIES)
152
  # Options have dependencies.
153
  include(CMakeDependentOption)
154

155 156
  # Allow the user to enable/disable all system utility library options by
  # defining CMAKE_USE_SYSTEM_LIBRARIES or CMAKE_USE_SYSTEM_LIBRARY_${util}.
157
  set(UTILITIES BZIP2 CURL EXPAT FORM JSONCPP LIBARCHIVE LIBLZMA LIBRHASH LIBUV ZLIB ZSTD)
158 159
  foreach(util ${UTILITIES})
    if(NOT DEFINED CMAKE_USE_SYSTEM_LIBRARY_${util}
160
        AND DEFINED CMAKE_USE_SYSTEM_LIBRARIES)
161 162 163 164 165 166 167 168 169 170 171 172 173
      set(CMAKE_USE_SYSTEM_LIBRARY_${util} "${CMAKE_USE_SYSTEM_LIBRARIES}")
    endif()
    if(DEFINED CMAKE_USE_SYSTEM_LIBRARY_${util})
      if(CMAKE_USE_SYSTEM_LIBRARY_${util})
        set(CMAKE_USE_SYSTEM_LIBRARY_${util} ON)
      else()
        set(CMAKE_USE_SYSTEM_LIBRARY_${util} OFF)
      endif()
      if(CMAKE_BOOTSTRAP)
        unset(CMAKE_USE_SYSTEM_LIBRARY_${util} CACHE)
      endif()
      string(TOLOWER "${util}" lutil)
      set(CMAKE_USE_SYSTEM_${util} "${CMAKE_USE_SYSTEM_LIBRARY_${util}}"
174
        CACHE BOOL "Use system-installed ${lutil}" FORCE)
175 176 177
    else()
      set(CMAKE_USE_SYSTEM_LIBRARY_${util} OFF)
    endif()
178
  endforeach()
179 180 181
  if(CMAKE_BOOTSTRAP)
    unset(CMAKE_USE_SYSTEM_LIBRARIES CACHE)
  endif()
182 183

  # Optionally use system utility libraries.
184
  option(CMAKE_USE_SYSTEM_LIBARCHIVE "Use system-installed libarchive" "${CMAKE_USE_SYSTEM_LIBRARY_LIBARCHIVE}")
185 186
  option(CMAKE_USE_SYSTEM_CURL "Use system-installed curl" "${CMAKE_USE_SYSTEM_LIBRARY_CURL}")
  option(CMAKE_USE_SYSTEM_EXPAT "Use system-installed expat" "${CMAKE_USE_SYSTEM_LIBRARY_EXPAT}")
187
  CMAKE_DEPENDENT_OPTION(CMAKE_USE_SYSTEM_ZLIB "Use system-installed zlib"
188
    "${CMAKE_USE_SYSTEM_LIBRARY_ZLIB}" "NOT CMAKE_USE_SYSTEM_LIBARCHIVE;NOT CMAKE_USE_SYSTEM_CURL" ON)
189
  CMAKE_DEPENDENT_OPTION(CMAKE_USE_SYSTEM_BZIP2 "Use system-installed bzip2"
190
    "${CMAKE_USE_SYSTEM_LIBRARY_BZIP2}" "NOT CMAKE_USE_SYSTEM_LIBARCHIVE" ON)
191 192
  CMAKE_DEPENDENT_OPTION(CMAKE_USE_SYSTEM_ZSTD "Use system-installed zstd"
    "${CMAKE_USE_SYSTEM_LIBRARY_ZSTD}" "NOT CMAKE_USE_SYSTEM_LIBARCHIVE" ON)
193 194
  CMAKE_DEPENDENT_OPTION(CMAKE_USE_SYSTEM_LIBLZMA "Use system-installed liblzma"
    "${CMAKE_USE_SYSTEM_LIBRARY_LIBLZMA}" "NOT CMAKE_USE_SYSTEM_LIBARCHIVE" ON)
195
  option(CMAKE_USE_SYSTEM_FORM "Use system-installed libform" "${CMAKE_USE_SYSTEM_LIBRARY_FORM}")
196
  option(CMAKE_USE_SYSTEM_JSONCPP "Use system-installed jsoncpp" "${CMAKE_USE_SYSTEM_LIBRARY_JSONCPP}")
197
  option(CMAKE_USE_SYSTEM_LIBRHASH "Use system-installed librhash" "${CMAKE_USE_SYSTEM_LIBRARY_LIBRHASH}")
198
  option(CMAKE_USE_SYSTEM_LIBUV "Use system-installed libuv" "${CMAKE_USE_SYSTEM_LIBRARY_LIBUV}")
199

200 201 202 203 204
  # For now use system KWIML only if explicitly requested rather
  # than activating via the general system libs options.
  option(CMAKE_USE_SYSTEM_KWIML "Use system-installed KWIML" OFF)
  mark_as_advanced(CMAKE_USE_SYSTEM_KWIML)

205
  # Mention to the user what system libraries are being used.
206
  foreach(util ${UTILITIES} KWIML)
207 208
    if(CMAKE_USE_SYSTEM_${util})
      message(STATUS "Using system-installed ${util}")
209 210
    endif()
  endforeach()
211 212

  # Inform utility library header wrappers whether to use system versions.
213
  configure_file(${CMake_SOURCE_DIR}/Utilities/cmThirdParty.h.in
214 215 216
    ${CMake_BINARY_DIR}/Utilities/cmThirdParty.h
    @ONLY)

217
endmacro()
218 219 220 221 222

#-----------------------------------------------------------------------
# a macro to determine the generator and ctest executable to use
# for testing. Simply to improve readability of the main script.
#-----------------------------------------------------------------------
223 224 225
macro(CMAKE_SETUP_TESTING)
  if(BUILD_TESTING)
    set(CMAKE_TEST_SYSTEM_LIBRARIES 0)
226
    foreach(util CURL EXPAT ZLIB)
227 228
      if(CMAKE_USE_SYSTEM_${util})
        set(CMAKE_TEST_SYSTEM_LIBRARIES 1)
229 230
      endif()
    endforeach()
231

232 233 234 235 236
    # This variable is set by cmake, however to
    # test cmake we want to make sure that
    # the ctest from this cmake is used for testing
    # and not the ctest from the cmake building and testing
    # cmake.
237 238 239 240 241 242 243 244 245 246 247 248 249
    if(CMake_TEST_EXTERNAL_CMAKE)
      set(CMAKE_CTEST_COMMAND "${CMake_TEST_EXTERNAL_CMAKE}/ctest")
      set(CMAKE_CMAKE_COMMAND "${CMake_TEST_EXTERNAL_CMAKE}/cmake")
      set(CMAKE_CPACK_COMMAND "${CMake_TEST_EXTERNAL_CMAKE}/cpack")
      foreach(exe cmake ctest cpack)
        add_executable(${exe} IMPORTED)
        set_property(TARGET ${exe} PROPERTY IMPORTED_LOCATION ${CMake_TEST_EXTERNAL_CMAKE}/${exe})
      endforeach()
    else()
      set(CMAKE_CTEST_COMMAND "${CMake_BIN_DIR}/ctest")
      set(CMAKE_CMAKE_COMMAND "${CMake_BIN_DIR}/cmake")
      set(CMAKE_CPACK_COMMAND "${CMake_BIN_DIR}/cpack")
    endif()
250
  endif()
251 252

  # configure some files for testing
253
  configure_file("${CMAKE_CURRENT_SOURCE_DIR}/Templates/CTestScript.cmake.in"
254
    "${CMAKE_CURRENT_BINARY_DIR}/CTestScript.cmake"
255
    @ONLY)
256
  configure_file(${CMake_SOURCE_DIR}/Tests/.NoDartCoverage
257
    ${CMake_BINARY_DIR}/Tests/.NoDartCoverage)
258
  configure_file(${CMake_SOURCE_DIR}/Tests/.NoDartCoverage
259
    ${CMake_BINARY_DIR}/Modules/.NoDartCoverage)
260
  configure_file(${CMake_SOURCE_DIR}/CTestCustom.cmake.in
261
    ${CMake_BINARY_DIR}/CTestCustom.cmake @ONLY)
262 263
  if(BUILD_TESTING AND DART_ROOT)
    configure_file(${CMake_SOURCE_DIR}/CMakeLogo.gif
Ken Martin's avatar
Ken Martin committed
264
      ${CMake_BINARY_DIR}/Testing/HTML/TestingResults/Icons/Logo.gif COPYONLY)
265
  endif()
266 267
  mark_as_advanced(DART_ROOT)
  mark_as_advanced(CURL_TESTING)
268
endmacro()
269 270


271 272 273 274
# Provide a way for Visual Studio Express users to turn OFF the new FOLDER
# organization feature. Default to ON for non-Express users. Express users must
# explicitly turn off this option to build CMake in the Express IDE...
#
275 276
option(CMAKE_USE_FOLDERS "Enable folder grouping of projects in IDEs." ON)
mark_as_advanced(CMAKE_USE_FOLDERS)
277 278


279 280 281 282 283 284 285 286 287 288
option(CMake_RUN_CLANG_TIDY "Run clang-tidy with the compiler." OFF)
if(CMake_RUN_CLANG_TIDY)
  if(CMake_SOURCE_DIR STREQUAL CMake_BINARY_DIR)
    message(FATAL_ERROR "CMake_RUN_CLANG_TIDY requires an out-of-source build!")
  endif()
  find_program(CLANG_TIDY_COMMAND NAMES clang-tidy)
  if(NOT CLANG_TIDY_COMMAND)
    message(FATAL_ERROR "CMake_RUN_CLANG_TIDY is ON but clang-tidy is not found!")
  endif()
  set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_COMMAND}")
289

290 291 292 293 294 295
  # Create a preprocessor definition that depends on .clang-tidy content so
  # the compile command will change when .clang-tidy changes.  This ensures
  # that a subsequent build re-runs clang-tidy on all sources even if they
  # do not otherwise need to be recompiled.  Nothing actually uses this
  # definition.  We add it to targets on which we run clang-tidy just to
  # get the build dependency on the .clang-tidy file.
296 297 298 299
  file(SHA1 ${CMAKE_CURRENT_SOURCE_DIR}/.clang-tidy clang_tidy_sha1)
  set(CLANG_TIDY_DEFINITIONS "CLANG_TIDY_SHA1=${clang_tidy_sha1}")
  unset(clang_tidy_sha1)

300 301 302 303
endif()
configure_file(.clang-tidy .clang-tidy COPYONLY)


304 305 306 307 308 309 310
option(CMake_RUN_IWYU "Run include-what-you-use with the compiler." OFF)
if(CMake_RUN_IWYU)
  find_program(IWYU_COMMAND NAMES include-what-you-use iwyu)
  if(NOT IWYU_COMMAND)
    message(FATAL_ERROR "CMake_RUN_IWYU is ON but include-what-you-use is not found!")
  endif()
  set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE
311 312
    "${IWYU_COMMAND};-Xiwyu;--mapping_file=${CMake_SOURCE_DIR}/Utilities/IWYU/mapping.imp;-w")
  list(APPEND CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${CMake_IWYU_OPTIONS})
313 314 315
endif()


316 317 318 319
#-----------------------------------------------------------------------
# a macro that only sets the FOLDER target property if it's
# "appropriate"
#-----------------------------------------------------------------------
320 321 322
macro(CMAKE_SET_TARGET_FOLDER tgt folder)
  if(CMAKE_USE_FOLDERS)
    set_property(GLOBAL PROPERTY USE_FOLDERS ON)
323 324
    if(MSVC AND TARGET ${tgt})
      set_property(TARGET "${tgt}" PROPERTY FOLDER "${folder}")
325 326 327 328
    endif()
  else()
    set_property(GLOBAL PROPERTY USE_FOLDERS OFF)
  endif()
329
endmacro()
330

331 332 333 334 335

#-----------------------------------------------------------------------
# a macro to build the utilities used by CMake
# Simply to improve readability of the main script.
#-----------------------------------------------------------------------
336
macro (CMAKE_BUILD_UTILITIES)
337 338
  find_package(Threads)

339 340
  #---------------------------------------------------------------------
  # Create the kwsys library for CMake.
341 342 343 344 345 346 347 348
  set(KWSYS_NAMESPACE cmsys)
  set(KWSYS_USE_SystemTools 1)
  set(KWSYS_USE_Directory 1)
  set(KWSYS_USE_RegularExpression 1)
  set(KWSYS_USE_Base64 1)
  set(KWSYS_USE_MD5 1)
  set(KWSYS_USE_Process 1)
  set(KWSYS_USE_CommandLineArguments 1)
349
  set(KWSYS_USE_ConsoleBuf 1)
350
  set(KWSYS_HEADER_ROOT ${CMake_BINARY_DIR}/Source)
351
  set(KWSYS_INSTALL_DOC_DIR "${CMAKE_DOC_DIR}")
352 353 354
  if(CMake_NO_CXX_STANDARD)
    set(KWSYS_CXX_STANDARD "")
  endif()
355 356
  add_subdirectory(Source/kwsys)
  set(kwsys_folder "Utilities/KWSys")
357 358
  CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE} "${kwsys_folder}")
  CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}_c "${kwsys_folder}")
359 360 361 362 363 364 365
  if(BUILD_TESTING)
    CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}TestDynload "${kwsys_folder}")
    CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}TestProcess "${kwsys_folder}")
    CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}TestsC "${kwsys_folder}")
    CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}TestsCxx "${kwsys_folder}")
    CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}TestSharedForward "${kwsys_folder}")
  endif()
366

367 368 369 370
  #---------------------------------------------------------------------
  # Setup third-party libraries.
  # Everything in the tree should be able to include files from the
  # Utilities directory.
371 372 373 374 375 376 377
  if (CMAKE_SYSTEM_NAME STREQUAL "AIX" AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    # using -isystem option generate error "template with C linkage"
    include_directories("${CMake_SOURCE_DIR}/Utilities/std")
  else()
    include_directories(SYSTEM "${CMake_SOURCE_DIR}/Utilities/std")
  endif()

378
  include_directories(
379
    ${CMake_BINARY_DIR}/Utilities
380
    ${CMake_SOURCE_DIR}/Utilities
381
    )
382

383 384 385 386 387 388
  #---------------------------------------------------------------------
  # Build CMake std library for CMake and CTest.
  set(CMAKE_STD_LIBRARY cmstd)
  add_subdirectory(Utilities/std)
  CMAKE_SET_TARGET_FOLDER(cmstd "Utilities/std")

389
  # check for the use of system libraries versus builtin ones
Ken Martin's avatar
Ken Martin committed
390
  # (a macro defined in this file)
391
  CMAKE_HANDLE_SYSTEM_LIBRARIES()
392

393 394 395 396 397 398 399 400 401 402 403 404 405 406
  if(CMAKE_USE_SYSTEM_KWIML)
    find_package(KWIML 1.0)
    if(NOT KWIML_FOUND)
      message(FATAL_ERROR "CMAKE_USE_SYSTEM_KWIML is ON but KWIML is not found!")
    endif()
    set(CMake_KWIML_LIBRARIES kwiml::kwiml)
  else()
    set(CMake_KWIML_LIBRARIES "")
    if(BUILD_TESTING)
      set(KWIML_TEST_ENABLE 1)
    endif()
    add_subdirectory(Utilities/KWIML)
  endif()

407
  if(CMAKE_USE_SYSTEM_LIBRHASH)
408
    find_package(LibRHash)
409 410 411 412 413 414 415 416 417 418
    if(NOT LibRHash_FOUND)
      message(FATAL_ERROR
        "CMAKE_USE_SYSTEM_LIBRHASH is ON but LibRHash is not found!")
    endif()
    set(CMAKE_LIBRHASH_LIBRARIES LibRHash::LibRHash)
  else()
    set(CMAKE_LIBRHASH_LIBRARIES cmlibrhash)
    add_subdirectory(Utilities/cmlibrhash)
    CMAKE_SET_TARGET_FOLDER(cmlibrhash "Utilities/3rdParty")
  endif()
419

420 421
  #---------------------------------------------------------------------
  # Build zlib library for Curl, CMake, and CTest.
422 423 424 425 426
  set(CMAKE_ZLIB_HEADER "cm_zlib.h")
  if(CMAKE_USE_SYSTEM_ZLIB)
    find_package(ZLIB)
    if(NOT ZLIB_FOUND)
      message(FATAL_ERROR
427
        "CMAKE_USE_SYSTEM_ZLIB is ON but a zlib is not found!")
428
    endif()
429 430
    set(CMAKE_ZLIB_INCLUDES ${ZLIB_INCLUDE_DIR})
    set(CMAKE_ZLIB_LIBRARIES ${ZLIB_LIBRARIES})
431
  else()
432 433 434
    set(CMAKE_ZLIB_INCLUDES ${CMake_SOURCE_DIR}/Utilities)
    set(CMAKE_ZLIB_LIBRARIES cmzlib)
    add_subdirectory(Utilities/cmzlib)
435
    CMAKE_SET_TARGET_FOLDER(cmzlib "Utilities/3rdParty")
436
  endif()
437

438 439
  #---------------------------------------------------------------------
  # Build Curl library for CTest.
440 441 442 443
  if(CMAKE_USE_SYSTEM_CURL)
    find_package(CURL)
    if(NOT CURL_FOUND)
      message(FATAL_ERROR
444
        "CMAKE_USE_SYSTEM_CURL is ON but a curl is not found!")
445
    endif()
446 447
    set(CMAKE_CURL_INCLUDES ${CURL_INCLUDE_DIRS})
    set(CMAKE_CURL_LIBRARIES ${CURL_LIBRARIES})
448
  else()
449 450 451 452 453 454 455
    set(CURL_SPECIAL_ZLIB_H ${CMAKE_ZLIB_HEADER})
    set(CURL_SPECIAL_LIBZ_INCLUDES ${CMAKE_ZLIB_INCLUDES})
    set(CURL_SPECIAL_LIBZ ${CMAKE_ZLIB_LIBRARIES})
    set(CMAKE_CURL_INCLUDES)
    set(CMAKE_CURL_LIBRARIES cmcurl)
    if(CMAKE_TESTS_CDASH_SERVER)
      set(CMAKE_CURL_TEST_URL "${CMAKE_TESTS_CDASH_SERVER}/user.php")
456
    endif()
457 458 459
    set(_CMAKE_USE_OPENSSL_DEFAULT OFF)
    if(NOT DEFINED CMAKE_USE_OPENSSL AND NOT WIN32 AND NOT APPLE
        AND CMAKE_SYSTEM_NAME MATCHES "(Linux|FreeBSD)")
460
      set(_CMAKE_USE_OPENSSL_DEFAULT ON)
461 462
    endif()
    option(CMAKE_USE_OPENSSL "Use OpenSSL." ${_CMAKE_USE_OPENSSL_DEFAULT})
463 464 465 466 467 468
    mark_as_advanced(CMAKE_USE_OPENSSL)
    if(CMAKE_USE_OPENSSL)
      set(CURL_CA_BUNDLE "" CACHE FILEPATH "Path to SSL CA Certificate Bundle")
      set(CURL_CA_PATH "" CACHE PATH "Path to SSL CA Certificate Directory")
      mark_as_advanced(CURL_CA_BUNDLE CURL_CA_PATH)
    endif()
469
    add_subdirectory(Utilities/cmcurl)
470 471
    CMAKE_SET_TARGET_FOLDER(cmcurl "Utilities/3rdParty")
    CMAKE_SET_TARGET_FOLDER(LIBCURL "Utilities/3rdParty")
472
  endif()
473

474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
  #---------------------------------------------------------------------
  # Build expat library for CMake, CTest, and libarchive.
  if(CMAKE_USE_SYSTEM_EXPAT)
    find_package(EXPAT)
    if(NOT EXPAT_FOUND)
      message(FATAL_ERROR
        "CMAKE_USE_SYSTEM_EXPAT is ON but a expat is not found!")
    endif()
    set(CMAKE_EXPAT_INCLUDES ${EXPAT_INCLUDE_DIRS})
    set(CMAKE_EXPAT_LIBRARIES ${EXPAT_LIBRARIES})
  else()
    set(CMAKE_EXPAT_INCLUDES)
    set(CMAKE_EXPAT_LIBRARIES cmexpat)
    add_subdirectory(Utilities/cmexpat)
    CMAKE_SET_TARGET_FOLDER(cmexpat "Utilities/3rdParty")
  endif()

491 492 493 494 495 496 497 498 499 500 501 502
  #---------------------------------------------------------------------
  # Build or use system libbz2 for libarchive.
  if(NOT CMAKE_USE_SYSTEM_LIBARCHIVE)
    if(CMAKE_USE_SYSTEM_BZIP2)
      find_package(BZip2)
    else()
      set(BZIP2_INCLUDE_DIR
        "${CMAKE_CURRENT_SOURCE_DIR}/Utilities/cmbzip2")
      set(BZIP2_LIBRARIES cmbzip2)
      add_subdirectory(Utilities/cmbzip2)
      CMAKE_SET_TARGET_FOLDER(cmbzip2 "Utilities/3rdParty")
    endif()
503
  endif()
504

505 506 507 508 509 510 511 512 513 514 515
  #---------------------------------------------------------------------
  # Build or use system zstd for libarchive.
  if(NOT CMAKE_USE_SYSTEM_LIBARCHIVE)
    if(NOT CMAKE_USE_SYSTEM_ZSTD)
      set(ZSTD_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Utilities/cmzstd")
      set(ZSTD_LIBRARY cmzstd)
      add_subdirectory(Utilities/cmzstd)
      CMAKE_SET_TARGET_FOLDER(cmzstd "Utilities/3rdParty")
    endif()
  endif()

516 517
  #---------------------------------------------------------------------
  # Build or use system liblzma for libarchive.
518 519 520 521 522 523 524 525 526
  if(NOT CMAKE_USE_SYSTEM_LIBARCHIVE)
    if(CMAKE_USE_SYSTEM_LIBLZMA)
      find_package(LibLZMA)
      if(NOT LIBLZMA_FOUND)
        message(FATAL_ERROR "CMAKE_USE_SYSTEM_LIBLZMA is ON but LibLZMA is not found!")
      endif()
    else()
      add_subdirectory(Utilities/cmliblzma)
      CMAKE_SET_TARGET_FOLDER(cmliblzma "Utilities/3rdParty")
527 528 529 530
      set(LIBLZMA_HAS_AUTO_DECODER 1)
      set(LIBLZMA_HAS_EASY_ENCODER 1)
      set(LIBLZMA_HAS_LZMA_PRESET 1)
      set(LIBLZMA_INCLUDE_DIR
531
        "${CMAKE_CURRENT_SOURCE_DIR}/Utilities/cmliblzma/liblzma/api")
532
      set(LIBLZMA_LIBRARY cmliblzma)
533 534 535
    endif()
  endif()

536 537
  #---------------------------------------------------------------------
  # Build or use system libarchive for CMake and CTest.
538
  if(CMAKE_USE_SYSTEM_LIBARCHIVE)
539
    find_package(LibArchive 3.3.3)
540 541 542 543 544
    if(NOT LibArchive_FOUND)
      message(FATAL_ERROR "CMAKE_USE_SYSTEM_LIBARCHIVE is ON but LibArchive is not found!")
    endif()
    set(CMAKE_TAR_INCLUDES ${LibArchive_INCLUDE_DIRS})
    set(CMAKE_TAR_LIBRARIES ${LibArchive_LIBRARIES})
545
  else()
546 547
    set(EXPAT_INCLUDE_DIR ${CMAKE_EXPAT_INCLUDES})
    set(EXPAT_LIBRARY ${CMAKE_EXPAT_LIBRARIES})
548 549 550 551 552
    set(ZLIB_INCLUDE_DIR ${CMAKE_ZLIB_INCLUDES})
    set(ZLIB_LIBRARY ${CMAKE_ZLIB_LIBRARIES})
    add_definitions(-DLIBARCHIVE_STATIC)
    set(ENABLE_NETTLE OFF CACHE INTERNAL "Enable use of Nettle")
    set(ENABLE_OPENSSL ${CMAKE_USE_OPENSSL} CACHE INTERNAL "Enable use of OpenSSL")
553
    set(ENABLE_LZMA ON CACHE INTERNAL "Enable the use of the system LZMA library if found")
554
    set(ENABLE_LZ4 OFF CACHE INTERNAL "Enable the use of the system LZ4 library if found")
555 556 557 558 559 560 561
    set(ENABLE_LZO OFF CACHE INTERNAL "Enable the use of the system LZO library if found")
    set(ENABLE_ZLIB ON CACHE INTERNAL "Enable the use of the system ZLIB library if found")
    set(ENABLE_BZip2 ON CACHE INTERNAL "Enable the use of the system BZip2 library if found")
    set(ENABLE_LIBXML2 OFF CACHE INTERNAL "Enable the use of the system libxml2 library if found")
    set(ENABLE_EXPAT ON CACHE INTERNAL "Enable the use of the system EXPAT library if found")
    set(ENABLE_PCREPOSIX OFF CACHE INTERNAL "Enable the use of the system PCREPOSIX library if found")
    set(ENABLE_LibGCC OFF CACHE INTERNAL "Enable the use of the system LibGCC library if found")
562 563 564
    set(ENABLE_XATTR OFF CACHE INTERNAL "Enable extended attribute support")
    set(ENABLE_ACL OFF CACHE INTERNAL "Enable ACL support")
    set(ENABLE_ICONV OFF CACHE INTERNAL "Enable iconv support")
565
    set(ENABLE_CNG OFF CACHE INTERNAL "Enable the use of CNG(Crypto Next Generation)")
566
    add_subdirectory(Utilities/cmlibarchive)
567
    CMAKE_SET_TARGET_FOLDER(cmlibarchive "Utilities/3rdParty")
568
    set(CMAKE_TAR_LIBRARIES cmlibarchive ${BZIP2_LIBRARIES})
569
  endif()
570

571 572
  #---------------------------------------------------------------------
  # Build jsoncpp library.
573
  if(CMAKE_USE_SYSTEM_JSONCPP)
574
    find_package(JsonCpp 1.4.1)
575 576 577 578
    if(NOT JsonCpp_FOUND)
      message(FATAL_ERROR
        "CMAKE_USE_SYSTEM_JSONCPP is ON but a JsonCpp is not found!")
    endif()
579 580 581 582
    if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
      set_property(TARGET JsonCpp::JsonCpp APPEND PROPERTY
        INTERFACE_COMPILE_OPTIONS -Wno-deprecated-declarations)
    endif()
583 584 585 586 587 588
    set(CMAKE_JSONCPP_LIBRARIES JsonCpp::JsonCpp)
  else()
    set(CMAKE_JSONCPP_LIBRARIES cmjsoncpp)
    add_subdirectory(Utilities/cmjsoncpp)
    CMAKE_SET_TARGET_FOLDER(cmjsoncpp "Utilities/3rdParty")
  endif()
589

590 591
  #---------------------------------------------------------------------
  # Build libuv library.
592
  if(CMAKE_USE_SYSTEM_LIBUV)
593
    find_package(LibUV 1.10.0)
594 595 596 597 598
    if(NOT LIBUV_FOUND)
      message(FATAL_ERROR
        "CMAKE_USE_SYSTEM_LIBUV is ON but a libuv is not found!")
    endif()
    set(CMAKE_LIBUV_LIBRARIES LibUV::LibUV)
599
  else()
600 601 602
    set(CMAKE_LIBUV_LIBRARIES cmlibuv)
    add_subdirectory(Utilities/cmlibuv)
    CMAKE_SET_TARGET_FOLDER(cmlibuv "Utilities/3rdParty")
603 604
  endif()

605 606
  #---------------------------------------------------------------------
  # Use curses?
607
  if (UNIX)
608 609 610
    if(NOT DEFINED BUILD_CursesDialog)
      include(${CMake_SOURCE_DIR}/Source/Checks/Curses.cmake)
      option(BUILD_CursesDialog "Build the CMake Curses Dialog ccmake" "${CMakeCheckCurses_COMPILED}")
611 612
    endif()
  else ()
613
    set(BUILD_CursesDialog 0)
614
  endif ()
615 616 617 618 619 620 621 622 623 624 625
  if(BUILD_CursesDialog)
    set(CURSES_NEED_NCURSES TRUE)
    find_package(Curses)
    if(NOT CURSES_FOUND)
      message(WARNING
        "'ccmake' will not be built because Curses was not found.\n"
        "Turn off BUILD_CursesDialog to suppress this message."
        )
      set(BUILD_CursesDialog 0)
    endif()
  endif()
626
  if(BUILD_CursesDialog)
627 628 629 630 631
    if(NOT CMAKE_USE_SYSTEM_FORM)
      add_subdirectory(Source/CursesDialog/form)
    elseif(NOT CURSES_FORM_LIBRARY)
      message( FATAL_ERROR "CMAKE_USE_SYSTEM_FORM in ON but CURSES_FORM_LIBRARY is not set!" )
    endif()
632 633
  endif()
endmacro ()
634

635
#-----------------------------------------------------------------------
636 637 638 639 640 641 642 643 644 645 646 647 648 649
if(NOT CMake_TEST_EXTERNAL_CMAKE)
  if(CMAKE_CXX_PLATFORM_ID MATCHES "OpenBSD")
    execute_process(COMMAND ${CMAKE_CXX_COMPILER}
      ${CMAKE_CXX_COMPILER_ARG1} -dumpversion
      OUTPUT_VARIABLE _GXX_VERSION
    )
    string(REGEX REPLACE "([0-9])\\.([0-9])(\\.[0-9])?" "\\1\\2"
      _GXX_VERSION_SHORT ${_GXX_VERSION})
    if(_GXX_VERSION_SHORT EQUAL 33)
      message(FATAL_ERROR
        "GXX 3.3 on OpenBSD is known to cause CPack to Crash.\n"
        "Please use GXX 4.2 or greater to build CMake on OpenBSD\n"
        "${CMAKE_CXX_COMPILER} version is: ${_GXX_VERSION}")
    endif()
650 651
  endif()
endif()
652 653 654 655 656

#-----------------------------------------------------------------------
# The main section of the CMakeLists file
#
#-----------------------------------------------------------------------
657
include(Source/CMakeVersion.cmake)
658

659
# Include the standard Dart testing module
660 661
enable_testing()
include (${CMAKE_ROOT}/Modules/Dart.cmake)
662

663
# Set up test-time configuration.
664
set_directory_properties(PROPERTIES
665 666
  TEST_INCLUDE_FILE "${CMake_BINARY_DIR}/Tests/EnforceConfig.cmake")

667 668 669 670 671 672
if(NOT CMake_TEST_EXTERNAL_CMAKE)
  # where to write the resulting executables and libraries
  set(BUILD_SHARED_LIBS OFF)
  set(EXECUTABLE_OUTPUT_PATH "" CACHE INTERNAL "No configurable exe dir.")
  set(LIBRARY_OUTPUT_PATH "" CACHE INTERNAL
    "Where to put the libraries for CMake")
673

674 675 676
  # The CMake executables usually do not need any rpath to run in the build or
  # install tree.
  set(CMAKE_SKIP_RPATH ON CACHE INTERNAL "CMake does not need RPATHs.")
677

678 679
  # Load install destinations.
  include(Source/CMakeInstallDestinations.cmake)
680

681 682 683
  if(BUILD_TESTING)
    include(${CMake_SOURCE_DIR}/Tests/CMakeInstall.cmake)
  endif()
684

685 686 687 688 689
  # no clue why we are testing for this here
  include(CheckSymbolExists)
  CHECK_SYMBOL_EXISTS(unsetenv "stdlib.h" HAVE_UNSETENV)
  CHECK_SYMBOL_EXISTS(environ "stdlib.h" HAVE_ENVIRON_NOT_REQUIRE_PROTOTYPE)
endif()
Andy Cedilnik's avatar
Andy Cedilnik committed
690

691 692 693
# CMAKE_TESTS_CDASH_SERVER: CDash server used by CMake/Tests.
#
# If not defined or "", this variable defaults to the server at
Brad King's avatar
Brad King committed
694
# "http://open.cdash.org".
695 696 697 698 699 700 701 702 703
#
# If set explicitly to "NOTFOUND", curl tests and ctest tests that use
# the network are skipped.
#
# If set to something starting with "http://localhost/", the CDash is
# expected to be an instance of CDash used for CDash testing, pointing
# to a cdash4simpletest database. In these cases, the CDash dashboards
# should be run first.
#
704
if("x${CMAKE_TESTS_CDASH_SERVER}" STREQUAL "x" AND NOT CMake_TEST_NO_NETWORK)
Brad King's avatar
Brad King committed
705
  set(CMAKE_TESTS_CDASH_SERVER "http://open.cdash.org")
706
endif()
707

708
if(CMake_TEST_EXTERNAL_CMAKE)
709
  set(KWIML_TEST_ENABLE 1)
710
  add_subdirectory(Utilities/KWIML)
711
endif()
712

713 714 715
if(NOT CMake_TEST_EXTERNAL_CMAKE)
  # build the utilities (a macro defined in this file)
  CMAKE_BUILD_UTILITIES()
716

717 718 719 720 721 722 723 724 725 726
  # On NetBSD ncurses is required, since curses doesn't have the wsyncup()
  # function. ncurses is installed via pkgsrc, so the library is in /usr/pkg/lib,
  # which isn't in the default linker search path. So without RPATH ccmake
  # doesn't run and the build doesn't succeed since ccmake is executed for
  # generating the documentation.
  if(BUILD_CursesDialog)
    get_filename_component(_CURSES_DIR "${CURSES_LIBRARY}" PATH)
    set(CURSES_NEED_RPATH FALSE)
    if(NOT "${_CURSES_DIR}" STREQUAL "/lib" AND NOT "${_CURSES_DIR}" STREQUAL "/usr/lib" AND NOT "${_CURSES_DIR}" STREQUAL "/lib64" AND NOT "${_CURSES_DIR}" STREQUAL "/usr/lib64")
      set(CURSES_NEED_RPATH TRUE)
727 728
    endif()
  endif()
729

730 731 732 733 734 735
  if(BUILD_QtDialog)
    if(APPLE)
      set(CMAKE_BUNDLE_VERSION
        "${CMake_VERSION_MAJOR}.${CMake_VERSION_MINOR}.${CMake_VERSION_PATCH}")
      set(CMAKE_BUNDLE_LOCATION "${CMAKE_INSTALL_PREFIX}")
      # make sure CMAKE_INSTALL_PREFIX ends in /
736
      if(NOT CMAKE_INSTALL_PREFIX MATCHES "/$")
737 738 739 740 741 742 743 744 745 746
        set(CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}/")
      endif()
      set(CMAKE_INSTALL_PREFIX
        "${CMAKE_INSTALL_PREFIX}CMake.app/Contents")
    endif()

    set(QT_NEED_RPATH FALSE)
    if(NOT "${QT_LIBRARY_DIR}" STREQUAL "/lib" AND NOT "${QT_LIBRARY_DIR}" STREQUAL "/usr/lib" AND NOT "${QT_LIBRARY_DIR}" STREQUAL "/lib64" AND NOT "${QT_LIBRARY_DIR}" STREQUAL "/usr/lib64")
      set(QT_NEED_RPATH TRUE)
    endif()
747
  endif()
748 749


750 751 752 753 754 755 756
  # The same might be true on other systems for other libraries.
  # Then only enable RPATH if we have are building at least with cmake 2.4,
  # since this one has much better RPATH features than cmake 2.2.
  # The executables are then built with the RPATH for the libraries outside
  # the build tree, which is both the build and the install RPATH.
  if (UNIX)
    if(   CMAKE_USE_SYSTEM_CURL   OR  CMAKE_USE_SYSTEM_ZLIB
757
          OR  CMAKE_USE_SYSTEM_EXPAT  OR  CURSES_NEED_RPATH  OR  QT_NEED_RPATH)
758 759 760 761 762
      set(CMAKE_SKIP_RPATH OFF CACHE INTERNAL "CMake built with RPATH.")
      set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
      set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
    endif()
  endif ()
763 764


765 766 767 768 769 770 771
  # add the uninstall support
  configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
    @ONLY)
  add_custom_target(uninstall
    "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake")
772

773 774 775
  include (CMakeCPack.cmake)

endif()
776

777 778
# setup some Testing support (a macro defined in this file)
CMAKE_SETUP_TESTING()
779

780 781
if(NOT CMake_TEST_EXTERNAL_CMAKE)
  if(NOT CMake_VERSION_IS_RELEASE)
782
    if(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
        NOT "${CMAKE_C_COMPILER_VERSION}" VERSION_LESS 4.2)
      set(C_FLAGS_LIST -Wcast-align -Werror-implicit-function-declaration -Wchar-subscripts
                       -Wall -W -Wpointer-arith -Wwrite-strings -Wformat-security
                       -Wmissing-format-attribute -fno-common -Wundef
      )
      set(CXX_FLAGS_LIST -Wnon-virtual-dtor -Wcast-align -Wchar-subscripts -Wall -W
                         -Wshadow -Wpointer-arith -Wformat-security -Wundef
      )

      foreach(FLAG_LANG  C CXX)
        foreach(FLAG ${${FLAG_LANG}_FLAGS_LIST})
          if(NOT " ${CMAKE_${FLAG_LANG}_FLAGS} " MATCHES " ${FLAG} ")
            set(CMAKE_${FLAG_LANG}_FLAGS "${CMAKE_${FLAG_LANG}_FLAGS} ${FLAG}")
          endif()
        endforeach()
798 799
      endforeach()

800 801 802
      unset(C_FLAGS_LIST)
      unset(CXX_FLAGS_LIST)
    endif()
803
  endif()
804 805 806 807

  # build the remaining subdirectories
  add_subdirectory(Source)
  add_subdirectory(Utilities)
808
endif()
809

810
add_subdirectory(Tests)
811

812 813 814
if(NOT CMake_TEST_EXTERNAL_CMAKE)
  if(BUILD_TESTING)
    CMAKE_SET_TARGET_FOLDER(CMakeLibTests "Tests")
815 816 817
    IF(TARGET CMakeServerLibTests)
      CMAKE_SET_TARGET_FOLDER(CMakeServerLibTests "Tests")
    ENDIF()
818 819 820 821
  endif()
  if(TARGET documentation)
    CMAKE_SET_TARGET_FOLDER(documentation "Documentation")
  endif()
822
endif()
823

824 825 826 827
if(BUILD_TESTING)
  add_test(SystemInformationNew "${CMAKE_CMAKE_COMMAND}"
    --system-information  -G "${CMAKE_GENERATOR}" )
endif()
828

829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
if(NOT CMake_TEST_EXTERNAL_CMAKE)
  # Install license file as it requires.
  install(FILES Copyright.txt DESTINATION ${CMAKE_DOC_DIR})

  # Install script directories.
  install(
    DIRECTORY Help Modules Templates
    DESTINATION ${CMAKE_DATA_DIR}
    FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ
    DIRECTORY_PERMISSIONS OWNER_READ OWNER_EXECUTE OWNER_WRITE
                          GROUP_READ GROUP_EXECUTE
                          WORLD_READ WORLD_EXECUTE
    PATTERN "*.sh*" PERMISSIONS OWNER_READ OWNER_EXECUTE OWNER_WRITE
                                GROUP_READ GROUP_EXECUTE
                                WORLD_READ WORLD_EXECUTE
844
    REGEX "Help/(dev|guide)($|/)" EXCLUDE
845 846 847 848
    )

  # Install auxiliary files integrating with other tools.
  add_subdirectory(Auxiliary)
849 850 851 852 853 854

  # Optionally sign installed binaries.
  if(CMake_INSTALL_SIGNTOOL)
    configure_file(Source/CMakeInstallSignTool.cmake.in Source/CMakeInstallSignTool.cmake @ONLY)
    install(SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/Source/CMakeInstallSignTool.cmake)
  endif()
855
endif()