Imported Upstream version 3.17.1
[platform/upstream/cmake.git] / CMakeLists.txt
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3
4 cmake_minimum_required(VERSION 3.1...3.15 FATAL_ERROR)
5 set(CMAKE_USER_MAKE_RULES_OVERRIDE_C ${CMAKE_CURRENT_SOURCE_DIR}/Source/Modules/OverrideC.cmake)
6 set(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX ${CMAKE_CURRENT_SOURCE_DIR}/Source/Modules/OverrideCXX.cmake)
7 project(CMake)
8 unset(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX)
9 unset(CMAKE_USER_MAKE_RULES_OVERRIDE_C)
10
11 # FIXME: This block should go away after a transition period.
12 if(MSVC AND NOT CMAKE_VERSION VERSION_LESS 3.15)
13   # Filter out MSVC runtime library flags that may have come from
14   # the cache of an existing build tree or from scripts.
15   foreach(l C CXX)
16     foreach(c DEBUG MINSIZEREL RELEASE RELWITHDEBINFO)
17       string(REGEX REPLACE "[-/]M[DT]d?( |$)" "" "CMAKE_${l}_FLAGS_${c}" "${CMAKE_${l}_FLAGS_${c}}")
18     endforeach()
19   endforeach()
20 endif()
21
22 # Make sure we can find internal find_package modules only used for
23 # building CMake and not for shipping externally
24 list(INSERT CMAKE_MODULE_PATH 0 ${CMake_SOURCE_DIR}/Source/Modules)
25
26 if(CMAKE_BOOTSTRAP)
27   # Running from bootstrap script.  Set local variable and remove from cache.
28   set(CMAKE_BOOTSTRAP 1)
29   unset(CMAKE_BOOTSTRAP CACHE)
30 endif()
31
32 if(CMake_TEST_HOST_CMAKE)
33   get_filename_component(CMake_TEST_EXTERNAL_CMAKE "${CMAKE_COMMAND}" DIRECTORY)
34 endif()
35
36 if(NOT CMake_TEST_EXTERNAL_CMAKE)
37   if(CMAKE_SYSTEM_NAME STREQUAL "HP-UX")
38     message(FATAL_ERROR
39       "CMake no longer compiles on HP-UX.  See\n"
40       "  https://gitlab.kitware.com/cmake/cmake/issues/17137\n"
41       "Use CMake 3.9 or lower instead."
42       )
43   endif()
44
45   set(CMake_BIN_DIR ${CMake_BINARY_DIR}/bin)
46 endif()
47
48 if(CMake_GUI_DISTRIBUTE_WITH_Qt_LGPL)
49   if(CMake_GUI_DISTRIBUTE_WITH_Qt_LGPL MATCHES "^3|2\\.1$")
50     set(USE_LGPL "${CMake_GUI_DISTRIBUTE_WITH_Qt_LGPL}")
51   else()
52     set(USE_LGPL "2.1")
53   endif()
54 else()
55   set(USE_LGPL "")
56 endif()
57
58 if("${CMake_SOURCE_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}")
59   # Disallow architecture-specific try_run.  It may not run on the host.
60   macro(TRY_RUN)
61     if(CMAKE_TRY_COMPILE_OSX_ARCHITECTURES)
62       message(FATAL_ERROR "TRY_RUN not allowed with CMAKE_TRY_COMPILE_OSX_ARCHITECTURES=[${CMAKE_TRY_COMPILE_OSX_ARCHITECTURES}]")
63     else()
64       _TRY_RUN(${ARGV})
65     endif()
66   endmacro()
67 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   include(${CMake_SOURCE_DIR}/Source/Checks/cm_c11_thread_local.cmake)
72   if(NOT CMake_C11_THREAD_LOCAL_BROKEN)
73     set(CMAKE_C_STANDARD 11)
74   else()
75     set(CMAKE_C_STANDARD 99)
76   endif()
77 endif()
78 if(NOT DEFINED CMAKE_CXX_STANDARD AND NOT CMake_NO_CXX_STANDARD)
79   if (CMAKE_CXX_COMPILER_ID STREQUAL SunPro AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.14)
80     set(CMAKE_CXX_STANDARD 98)
81   else()
82     if(NOT CMAKE_VERSION VERSION_LESS 3.8)
83       include(${CMake_SOURCE_DIR}/Source/Checks/cm_cxx17_check.cmake)
84     else()
85       set(CMake_CXX17_BROKEN 1)
86     endif()
87     if(NOT CMake_CXX17_BROKEN)
88       set(CMAKE_CXX_STANDARD 17)
89     else()
90       include(${CMake_SOURCE_DIR}/Source/Checks/cm_cxx14_check.cmake)
91       if(NOT CMake_CXX14_BROKEN)
92         set(CMAKE_CXX_STANDARD 14)
93       else()
94         set(CMAKE_CXX_STANDARD 11)
95       endif()
96     endif()
97   endif()
98 endif()
99 if(NOT CMake_TEST_EXTERNAL_CMAKE)
100   # include special compile flags for some compilers
101   include(CompileFlags.cmake)
102
103   # check for available C++ features
104   include(${CMake_SOURCE_DIR}/Source/Checks/cm_cxx_features.cmake)
105
106   if(NOT CMake_HAVE_CXX_UNIQUE_PTR)
107     message(FATAL_ERROR "The C++ compiler does not support C++11 (e.g. std::unique_ptr).")
108   endif()
109 endif()
110
111 # set the internal encoding of CMake to UTF-8
112 set(KWSYS_ENCODING_DEFAULT_CODEPAGE CP_UTF8)
113
114 # option to use COMPONENT with install command
115 option(CMake_INSTALL_COMPONENTS "Using components when installing" OFF)
116 mark_as_advanced(CMake_INSTALL_COMPONENTS)
117 macro(CMake_OPTIONAL_COMPONENT NAME)
118   if(CMake_INSTALL_COMPONENTS)
119     set(COMPONENT COMPONENT ${NAME})
120   else()
121     set(COMPONENT)
122   endif()
123 endmacro()
124
125 # option to disable installing 3rd-party dependencies
126 option(CMake_INSTALL_DEPENDENCIES
127   "Whether to install 3rd-party runtime dependencies" OFF)
128 mark_as_advanced(CMake_INSTALL_DEPENDENCIES)
129
130 # option to build reference for CMake developers
131 option(CMake_BUILD_DEVELOPER_REFERENCE
132   "Build CMake Developer Reference" OFF)
133 mark_as_advanced(CMake_BUILD_DEVELOPER_REFERENCE)
134
135 # option to build using interprocedural optimizations (IPO/LTO)
136 if (NOT CMAKE_VERSION VERSION_LESS 3.12.2)
137   option(CMake_BUILD_LTO "Compile CMake with link-time optimization if supported" OFF)
138   if(CMake_BUILD_LTO)
139     include(CheckIPOSupported)
140     check_ipo_supported(RESULT HAVE_IPO)
141     if(HAVE_IPO)
142       set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
143     endif()
144   endif()
145 endif()
146
147 #-----------------------------------------------------------------------
148 # a macro to deal with system libraries, implemented as a macro
149 # simply to improve readability of the main script
150 #-----------------------------------------------------------------------
151 macro(CMAKE_HANDLE_SYSTEM_LIBRARIES)
152   # Options have dependencies.
153   include(CMakeDependentOption)
154
155   # Allow the user to enable/disable all system utility library options by
156   # 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   foreach(util ${UTILITIES})
159     if(NOT DEFINED CMAKE_USE_SYSTEM_LIBRARY_${util}
160         AND DEFINED CMAKE_USE_SYSTEM_LIBRARIES)
161       set(CMAKE_USE_SYSTEM_LIBRARY_${util} "${CMAKE_USE_SYSTEM_LIBRARIES}")
162     endif()
163     if(DEFINED CMAKE_USE_SYSTEM_LIBRARY_${util})
164       if(CMAKE_USE_SYSTEM_LIBRARY_${util})
165         set(CMAKE_USE_SYSTEM_LIBRARY_${util} ON)
166       else()
167         set(CMAKE_USE_SYSTEM_LIBRARY_${util} OFF)
168       endif()
169       if(CMAKE_BOOTSTRAP)
170         unset(CMAKE_USE_SYSTEM_LIBRARY_${util} CACHE)
171       endif()
172       string(TOLOWER "${util}" lutil)
173       set(CMAKE_USE_SYSTEM_${util} "${CMAKE_USE_SYSTEM_LIBRARY_${util}}"
174         CACHE BOOL "Use system-installed ${lutil}" FORCE)
175     else()
176       set(CMAKE_USE_SYSTEM_LIBRARY_${util} OFF)
177     endif()
178   endforeach()
179   if(CMAKE_BOOTSTRAP)
180     unset(CMAKE_USE_SYSTEM_LIBRARIES CACHE)
181   endif()
182
183   # Optionally use system utility libraries.
184   option(CMAKE_USE_SYSTEM_LIBARCHIVE "Use system-installed libarchive" "${CMAKE_USE_SYSTEM_LIBRARY_LIBARCHIVE}")
185   option(CMAKE_USE_SYSTEM_CURL "Use system-installed curl" "${CMAKE_USE_SYSTEM_LIBRARY_CURL}")
186   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   CMAKE_DEPENDENT_OPTION(CMAKE_USE_SYSTEM_ZSTD "Use system-installed zstd"
192     "${CMAKE_USE_SYSTEM_LIBRARY_ZSTD}" "NOT CMAKE_USE_SYSTEM_LIBARCHIVE" ON)
193   CMAKE_DEPENDENT_OPTION(CMAKE_USE_SYSTEM_LIBLZMA "Use system-installed liblzma"
194     "${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   # For now use system KWIML only if explicitly requested rather
201   # than activating via the general system libs options.
202   option(CMAKE_USE_SYSTEM_KWIML "Use system-installed KWIML" OFF)
203   mark_as_advanced(CMAKE_USE_SYSTEM_KWIML)
204
205   # Mention to the user what system libraries are being used.
206   foreach(util ${UTILITIES} KWIML)
207     if(CMAKE_USE_SYSTEM_${util})
208       message(STATUS "Using system-installed ${util}")
209     endif()
210   endforeach()
211
212   # Inform utility library header wrappers whether to use system versions.
213   configure_file(${CMake_SOURCE_DIR}/Utilities/cmThirdParty.h.in
214     ${CMake_BINARY_DIR}/Utilities/cmThirdParty.h
215     @ONLY)
216
217 endmacro()
218
219 #-----------------------------------------------------------------------
220 # a macro to determine the generator and ctest executable to use
221 # for testing. Simply to improve readability of the main script.
222 #-----------------------------------------------------------------------
223 macro(CMAKE_SETUP_TESTING)
224   if(BUILD_TESTING)
225     set(CMAKE_TEST_SYSTEM_LIBRARIES 0)
226     foreach(util CURL EXPAT ZLIB)
227       if(CMAKE_USE_SYSTEM_${util})
228         set(CMAKE_TEST_SYSTEM_LIBRARIES 1)
229       endif()
230     endforeach()
231
232     # This variable is set by cmake, however to
233     # test cmake we want to make sure that
234     # the ctest from this cmake is used for testing
235     # and not the ctest from the cmake building and testing
236     # cmake.
237     if(CMake_TEST_EXTERNAL_CMAKE)
238       set(CMAKE_CTEST_COMMAND "${CMake_TEST_EXTERNAL_CMAKE}/ctest")
239       set(CMAKE_CMAKE_COMMAND "${CMake_TEST_EXTERNAL_CMAKE}/cmake")
240       set(CMAKE_CPACK_COMMAND "${CMake_TEST_EXTERNAL_CMAKE}/cpack")
241       foreach(exe cmake ctest cpack)
242         add_executable(${exe} IMPORTED)
243         set_property(TARGET ${exe} PROPERTY IMPORTED_LOCATION ${CMake_TEST_EXTERNAL_CMAKE}/${exe})
244       endforeach()
245     else()
246       set(CMAKE_CTEST_COMMAND "${CMake_BIN_DIR}/ctest")
247       set(CMAKE_CMAKE_COMMAND "${CMake_BIN_DIR}/cmake")
248       set(CMAKE_CPACK_COMMAND "${CMake_BIN_DIR}/cpack")
249     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   if(BUILD_TESTING AND DART_ROOT)
263     configure_file(${CMake_SOURCE_DIR}/CMakeLogo.gif
264       ${CMake_BINARY_DIR}/Testing/HTML/TestingResults/Icons/Logo.gif COPYONLY)
265   endif()
266   mark_as_advanced(DART_ROOT)
267 endmacro()
268
269
270 # Provide a way for Visual Studio Express users to turn OFF the new FOLDER
271 # organization feature. Default to ON for non-Express users. Express users must
272 # explicitly turn off this option to build CMake in the Express IDE...
273 #
274 option(CMAKE_USE_FOLDERS "Enable folder grouping of projects in IDEs." ON)
275 mark_as_advanced(CMAKE_USE_FOLDERS)
276
277
278 option(CMake_RUN_CLANG_TIDY "Run clang-tidy with the compiler." OFF)
279 if(CMake_RUN_CLANG_TIDY)
280   if(CMake_SOURCE_DIR STREQUAL CMake_BINARY_DIR)
281     message(FATAL_ERROR "CMake_RUN_CLANG_TIDY requires an out-of-source build!")
282   endif()
283   find_program(CLANG_TIDY_COMMAND NAMES clang-tidy)
284   if(NOT CLANG_TIDY_COMMAND)
285     message(FATAL_ERROR "CMake_RUN_CLANG_TIDY is ON but clang-tidy is not found!")
286   endif()
287   set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_COMMAND}")
288
289   # Create a preprocessor definition that depends on .clang-tidy content so
290   # the compile command will change when .clang-tidy changes.  This ensures
291   # that a subsequent build re-runs clang-tidy on all sources even if they
292   # do not otherwise need to be recompiled.  Nothing actually uses this
293   # definition.  We add it to targets on which we run clang-tidy just to
294   # get the build dependency on the .clang-tidy file.
295   file(SHA1 ${CMAKE_CURRENT_SOURCE_DIR}/.clang-tidy clang_tidy_sha1)
296   set(CLANG_TIDY_DEFINITIONS "CLANG_TIDY_SHA1=${clang_tidy_sha1}")
297   unset(clang_tidy_sha1)
298
299 endif()
300 configure_file(.clang-tidy .clang-tidy COPYONLY)
301
302
303 option(CMake_RUN_IWYU "Run include-what-you-use with the compiler." OFF)
304 if(CMake_RUN_IWYU)
305   find_program(IWYU_COMMAND NAMES include-what-you-use iwyu)
306   if(NOT IWYU_COMMAND)
307     message(FATAL_ERROR "CMake_RUN_IWYU is ON but include-what-you-use is not found!")
308   endif()
309   set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE
310     "${IWYU_COMMAND};-Xiwyu;--mapping_file=${CMake_SOURCE_DIR}/Utilities/IWYU/mapping.imp;-w")
311   list(APPEND CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${CMake_IWYU_OPTIONS})
312 endif()
313
314
315 #-----------------------------------------------------------------------
316 # a macro that only sets the FOLDER target property if it's
317 # "appropriate"
318 #-----------------------------------------------------------------------
319 macro(CMAKE_SET_TARGET_FOLDER tgt folder)
320   if(CMAKE_USE_FOLDERS)
321     set_property(GLOBAL PROPERTY USE_FOLDERS ON)
322     if(MSVC AND TARGET ${tgt})
323       set_property(TARGET "${tgt}" PROPERTY FOLDER "${folder}")
324     endif()
325   else()
326     set_property(GLOBAL PROPERTY USE_FOLDERS OFF)
327   endif()
328 endmacro()
329
330
331 #-----------------------------------------------------------------------
332 # a macro to build the utilities used by CMake
333 # Simply to improve readability of the main script.
334 #-----------------------------------------------------------------------
335 macro (CMAKE_BUILD_UTILITIES)
336   find_package(Threads)
337
338   #---------------------------------------------------------------------
339   # Create the kwsys library for CMake.
340   set(KWSYS_NAMESPACE cmsys)
341   set(KWSYS_USE_SystemTools 1)
342   set(KWSYS_USE_Directory 1)
343   set(KWSYS_USE_RegularExpression 1)
344   set(KWSYS_USE_Base64 1)
345   set(KWSYS_USE_MD5 1)
346   set(KWSYS_USE_Process 1)
347   set(KWSYS_USE_CommandLineArguments 1)
348   set(KWSYS_USE_ConsoleBuf 1)
349   set(KWSYS_HEADER_ROOT ${CMake_BINARY_DIR}/Source)
350   set(KWSYS_INSTALL_DOC_DIR "${CMAKE_DOC_DIR}")
351   if(CMake_NO_CXX_STANDARD)
352     set(KWSYS_CXX_STANDARD "")
353   endif()
354   add_subdirectory(Source/kwsys)
355   set(kwsys_folder "Utilities/KWSys")
356   CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE} "${kwsys_folder}")
357   CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}_c "${kwsys_folder}")
358   if(BUILD_TESTING)
359     CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}TestDynload "${kwsys_folder}")
360     CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}TestProcess "${kwsys_folder}")
361     CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}TestsC "${kwsys_folder}")
362     CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}TestsCxx "${kwsys_folder}")
363     CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}TestSharedForward "${kwsys_folder}")
364   endif()
365
366   #---------------------------------------------------------------------
367   # Setup third-party libraries.
368   # Everything in the tree should be able to include files from the
369   # Utilities directory.
370   if ((CMAKE_SYSTEM_NAME STREQUAL "AIX" OR CMAKE_SYSTEM_NAME STREQUAL "OS400") AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
371     # using -isystem option generate error "template with C linkage"
372     include_directories("${CMake_SOURCE_DIR}/Utilities/std")
373   else()
374     include_directories(SYSTEM "${CMake_SOURCE_DIR}/Utilities/std")
375   endif()
376
377   include_directories(
378     ${CMake_BINARY_DIR}/Utilities
379     ${CMake_SOURCE_DIR}/Utilities
380     )
381
382   #---------------------------------------------------------------------
383   # Build CMake std library for CMake and CTest.
384   set(CMAKE_STD_LIBRARY cmstd)
385   add_subdirectory(Utilities/std)
386   CMAKE_SET_TARGET_FOLDER(cmstd "Utilities/std")
387
388   # check for the use of system libraries versus builtin ones
389   # (a macro defined in this file)
390   CMAKE_HANDLE_SYSTEM_LIBRARIES()
391
392   if(CMAKE_USE_SYSTEM_KWIML)
393     find_package(KWIML 1.0)
394     if(NOT KWIML_FOUND)
395       message(FATAL_ERROR "CMAKE_USE_SYSTEM_KWIML is ON but KWIML is not found!")
396     endif()
397     set(CMake_KWIML_LIBRARIES kwiml::kwiml)
398   else()
399     set(CMake_KWIML_LIBRARIES "")
400     if(BUILD_TESTING)
401       set(KWIML_TEST_ENABLE 1)
402     endif()
403     add_subdirectory(Utilities/KWIML)
404   endif()
405
406   if(CMAKE_USE_SYSTEM_LIBRHASH)
407     find_package(LibRHash)
408     if(NOT LibRHash_FOUND)
409       message(FATAL_ERROR
410         "CMAKE_USE_SYSTEM_LIBRHASH is ON but LibRHash is not found!")
411     endif()
412     set(CMAKE_LIBRHASH_LIBRARIES LibRHash::LibRHash)
413   else()
414     set(CMAKE_LIBRHASH_LIBRARIES cmlibrhash)
415     add_subdirectory(Utilities/cmlibrhash)
416     CMAKE_SET_TARGET_FOLDER(cmlibrhash "Utilities/3rdParty")
417   endif()
418
419   #---------------------------------------------------------------------
420   # Build zlib library for Curl, CMake, and CTest.
421   set(CMAKE_ZLIB_HEADER "cm_zlib.h")
422   if(CMAKE_USE_SYSTEM_ZLIB)
423     find_package(ZLIB)
424     if(NOT ZLIB_FOUND)
425       message(FATAL_ERROR
426         "CMAKE_USE_SYSTEM_ZLIB is ON but a zlib is not found!")
427     endif()
428     set(CMAKE_ZLIB_INCLUDES ${ZLIB_INCLUDE_DIR})
429     set(CMAKE_ZLIB_LIBRARIES ${ZLIB_LIBRARIES})
430   else()
431     set(CMAKE_ZLIB_INCLUDES ${CMake_SOURCE_DIR}/Utilities)
432     set(CMAKE_ZLIB_LIBRARIES cmzlib)
433     add_subdirectory(Utilities/cmzlib)
434     CMAKE_SET_TARGET_FOLDER(cmzlib "Utilities/3rdParty")
435   endif()
436
437   #---------------------------------------------------------------------
438   # Build Curl library for CTest.
439   if(CMAKE_USE_SYSTEM_CURL)
440     find_package(CURL)
441     if(NOT CURL_FOUND)
442       message(FATAL_ERROR
443         "CMAKE_USE_SYSTEM_CURL is ON but a curl is not found!")
444     endif()
445     set(CMAKE_CURL_INCLUDES ${CURL_INCLUDE_DIRS})
446     set(CMAKE_CURL_LIBRARIES ${CURL_LIBRARIES})
447   else()
448     set(CURL_SPECIAL_ZLIB_H ${CMAKE_ZLIB_HEADER})
449     set(CURL_SPECIAL_LIBZ_INCLUDES ${CMAKE_ZLIB_INCLUDES})
450     set(CURL_SPECIAL_LIBZ ${CMAKE_ZLIB_LIBRARIES})
451     set(CMAKE_CURL_INCLUDES)
452     set(CMAKE_CURL_LIBRARIES cmcurl)
453     if(CMAKE_TESTS_CDASH_SERVER)
454       set(CMAKE_CURL_TEST_URL "${CMAKE_TESTS_CDASH_SERVER}/user.php")
455     endif()
456     set(_CMAKE_USE_OPENSSL_DEFAULT OFF)
457     if(NOT DEFINED CMAKE_USE_OPENSSL AND NOT WIN32 AND NOT APPLE
458         AND CMAKE_SYSTEM_NAME MATCHES "(Linux|FreeBSD)")
459       set(_CMAKE_USE_OPENSSL_DEFAULT ON)
460     endif()
461     option(CMAKE_USE_OPENSSL "Use OpenSSL." ${_CMAKE_USE_OPENSSL_DEFAULT})
462     mark_as_advanced(CMAKE_USE_OPENSSL)
463     if(CMAKE_USE_OPENSSL)
464       set(CURL_CA_BUNDLE "" CACHE FILEPATH "Path to SSL CA Certificate Bundle")
465       set(CURL_CA_PATH "" CACHE PATH "Path to SSL CA Certificate Directory")
466       mark_as_advanced(CURL_CA_BUNDLE CURL_CA_PATH)
467     endif()
468     add_subdirectory(Utilities/cmcurl)
469     CMAKE_SET_TARGET_FOLDER(cmcurl "Utilities/3rdParty")
470     CMAKE_SET_TARGET_FOLDER(LIBCURL "Utilities/3rdParty")
471   endif()
472
473   #---------------------------------------------------------------------
474   # Build expat library for CMake, CTest, and libarchive.
475   if(CMAKE_USE_SYSTEM_EXPAT)
476     find_package(EXPAT)
477     if(NOT EXPAT_FOUND)
478       message(FATAL_ERROR
479         "CMAKE_USE_SYSTEM_EXPAT is ON but a expat is not found!")
480     endif()
481     set(CMAKE_EXPAT_INCLUDES ${EXPAT_INCLUDE_DIRS})
482     set(CMAKE_EXPAT_LIBRARIES ${EXPAT_LIBRARIES})
483   else()
484     set(CMAKE_EXPAT_INCLUDES)
485     set(CMAKE_EXPAT_LIBRARIES cmexpat)
486     add_subdirectory(Utilities/cmexpat)
487     CMAKE_SET_TARGET_FOLDER(cmexpat "Utilities/3rdParty")
488   endif()
489
490   #---------------------------------------------------------------------
491   # Build or use system libbz2 for libarchive.
492   if(NOT CMAKE_USE_SYSTEM_LIBARCHIVE)
493     if(CMAKE_USE_SYSTEM_BZIP2)
494       find_package(BZip2)
495     else()
496       set(BZIP2_INCLUDE_DIR
497         "${CMAKE_CURRENT_SOURCE_DIR}/Utilities/cmbzip2")
498       set(BZIP2_LIBRARIES cmbzip2)
499       add_subdirectory(Utilities/cmbzip2)
500       CMAKE_SET_TARGET_FOLDER(cmbzip2 "Utilities/3rdParty")
501     endif()
502   endif()
503
504   #---------------------------------------------------------------------
505   # Build or use system zstd for libarchive.
506   if(NOT CMAKE_USE_SYSTEM_LIBARCHIVE)
507     if(NOT CMAKE_USE_SYSTEM_ZSTD)
508       set(ZSTD_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Utilities/cmzstd")
509       set(ZSTD_LIBRARY cmzstd)
510       add_subdirectory(Utilities/cmzstd)
511       CMAKE_SET_TARGET_FOLDER(cmzstd "Utilities/3rdParty")
512     endif()
513   endif()
514
515   #---------------------------------------------------------------------
516   # Build or use system liblzma for libarchive.
517   if(NOT CMAKE_USE_SYSTEM_LIBARCHIVE)
518     if(CMAKE_USE_SYSTEM_LIBLZMA)
519       find_package(LibLZMA)
520       if(NOT LIBLZMA_FOUND)
521         message(FATAL_ERROR "CMAKE_USE_SYSTEM_LIBLZMA is ON but LibLZMA is not found!")
522       endif()
523     else()
524       add_subdirectory(Utilities/cmliblzma)
525       CMAKE_SET_TARGET_FOLDER(cmliblzma "Utilities/3rdParty")
526       set(LIBLZMA_HAS_AUTO_DECODER 1)
527       set(LIBLZMA_HAS_EASY_ENCODER 1)
528       set(LIBLZMA_HAS_LZMA_PRESET 1)
529       set(LIBLZMA_INCLUDE_DIR
530         "${CMAKE_CURRENT_SOURCE_DIR}/Utilities/cmliblzma/liblzma/api")
531       set(LIBLZMA_LIBRARY cmliblzma)
532     endif()
533   endif()
534
535   #---------------------------------------------------------------------
536   # Build or use system libarchive for CMake and CTest.
537   if(CMAKE_USE_SYSTEM_LIBARCHIVE)
538     find_package(LibArchive 3.3.3)
539     if(NOT LibArchive_FOUND)
540       message(FATAL_ERROR "CMAKE_USE_SYSTEM_LIBARCHIVE is ON but LibArchive is not found!")
541     endif()
542     set(CMAKE_TAR_INCLUDES ${LibArchive_INCLUDE_DIRS})
543     set(CMAKE_TAR_LIBRARIES ${LibArchive_LIBRARIES})
544   else()
545     set(EXPAT_INCLUDE_DIR ${CMAKE_EXPAT_INCLUDES})
546     set(EXPAT_LIBRARY ${CMAKE_EXPAT_LIBRARIES})
547     set(ZLIB_INCLUDE_DIR ${CMAKE_ZLIB_INCLUDES})
548     set(ZLIB_LIBRARY ${CMAKE_ZLIB_LIBRARIES})
549     add_definitions(-DLIBARCHIVE_STATIC)
550     set(ENABLE_NETTLE OFF CACHE INTERNAL "Enable use of Nettle")
551     set(ENABLE_OPENSSL ${CMAKE_USE_OPENSSL} CACHE INTERNAL "Enable use of OpenSSL")
552     set(ENABLE_LZMA ON CACHE INTERNAL "Enable the use of the system LZMA library if found")
553     set(ENABLE_LZ4 OFF CACHE INTERNAL "Enable the use of the system LZ4 library if found")
554     set(ENABLE_LZO OFF CACHE INTERNAL "Enable the use of the system LZO library if found")
555     set(ENABLE_ZLIB ON CACHE INTERNAL "Enable the use of the system ZLIB library if found")
556     set(ENABLE_BZip2 ON CACHE INTERNAL "Enable the use of the system BZip2 library if found")
557     set(ENABLE_LIBXML2 OFF CACHE INTERNAL "Enable the use of the system libxml2 library if found")
558     set(ENABLE_EXPAT ON CACHE INTERNAL "Enable the use of the system EXPAT library if found")
559     set(ENABLE_PCREPOSIX OFF CACHE INTERNAL "Enable the use of the system PCREPOSIX library if found")
560     set(ENABLE_LibGCC OFF CACHE INTERNAL "Enable the use of the system LibGCC library if found")
561     set(ENABLE_XATTR OFF CACHE INTERNAL "Enable extended attribute support")
562     set(ENABLE_ACL OFF CACHE INTERNAL "Enable ACL support")
563     set(ENABLE_ICONV OFF CACHE INTERNAL "Enable iconv support")
564     set(ENABLE_CNG OFF CACHE INTERNAL "Enable the use of CNG(Crypto Next Generation)")
565     add_subdirectory(Utilities/cmlibarchive)
566     CMAKE_SET_TARGET_FOLDER(cmlibarchive "Utilities/3rdParty")
567     set(CMAKE_TAR_LIBRARIES cmlibarchive ${BZIP2_LIBRARIES})
568   endif()
569
570   #---------------------------------------------------------------------
571   # Build jsoncpp library.
572   if(CMAKE_USE_SYSTEM_JSONCPP)
573     find_package(JsonCpp 1.4.1)
574     if(NOT JsonCpp_FOUND)
575       message(FATAL_ERROR
576         "CMAKE_USE_SYSTEM_JSONCPP is ON but a JsonCpp is not found!")
577     endif()
578     if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
579       set_property(TARGET JsonCpp::JsonCpp APPEND PROPERTY
580         INTERFACE_COMPILE_OPTIONS -Wno-deprecated-declarations)
581     endif()
582     set(CMAKE_JSONCPP_LIBRARIES JsonCpp::JsonCpp)
583   else()
584     set(CMAKE_JSONCPP_LIBRARIES cmjsoncpp)
585     add_subdirectory(Utilities/cmjsoncpp)
586     CMAKE_SET_TARGET_FOLDER(cmjsoncpp "Utilities/3rdParty")
587   endif()
588
589   #---------------------------------------------------------------------
590   # Build libuv library.
591   if(CMAKE_USE_SYSTEM_LIBUV)
592     find_package(LibUV 1.10.0)
593     if(NOT LIBUV_FOUND)
594       message(FATAL_ERROR
595         "CMAKE_USE_SYSTEM_LIBUV is ON but a libuv is not found!")
596     endif()
597     set(CMAKE_LIBUV_LIBRARIES LibUV::LibUV)
598   else()
599     set(CMAKE_LIBUV_LIBRARIES cmlibuv)
600     add_subdirectory(Utilities/cmlibuv)
601     CMAKE_SET_TARGET_FOLDER(cmlibuv "Utilities/3rdParty")
602   endif()
603
604   #---------------------------------------------------------------------
605   # Use curses?
606   if (UNIX)
607     if(NOT DEFINED BUILD_CursesDialog)
608       include(${CMake_SOURCE_DIR}/Source/Checks/Curses.cmake)
609       option(BUILD_CursesDialog "Build the CMake Curses Dialog ccmake" "${CMakeCheckCurses_COMPILED}")
610     endif()
611   else ()
612     set(BUILD_CursesDialog 0)
613   endif ()
614   if(BUILD_CursesDialog)
615     set(CURSES_NEED_NCURSES TRUE)
616     find_package(Curses)
617     if(NOT CURSES_FOUND)
618       message(WARNING
619         "'ccmake' will not be built because Curses was not found.\n"
620         "Turn off BUILD_CursesDialog to suppress this message."
621         )
622       set(BUILD_CursesDialog 0)
623     endif()
624   endif()
625   if(BUILD_CursesDialog)
626     if(NOT CMAKE_USE_SYSTEM_FORM)
627       add_subdirectory(Source/CursesDialog/form)
628     elseif(NOT CURSES_FORM_LIBRARY)
629       message( FATAL_ERROR "CMAKE_USE_SYSTEM_FORM in ON but CURSES_FORM_LIBRARY is not set!" )
630     endif()
631   endif()
632 endmacro ()
633
634 #-----------------------------------------------------------------------
635 if(NOT CMake_TEST_EXTERNAL_CMAKE)
636   if(CMAKE_CXX_PLATFORM_ID MATCHES "OpenBSD")
637     execute_process(COMMAND ${CMAKE_CXX_COMPILER}
638       ${CMAKE_CXX_COMPILER_ARG1} -dumpversion
639       OUTPUT_VARIABLE _GXX_VERSION
640     )
641     string(REGEX REPLACE "([0-9])\\.([0-9])(\\.[0-9])?" "\\1\\2"
642       _GXX_VERSION_SHORT ${_GXX_VERSION})
643     if(_GXX_VERSION_SHORT EQUAL 33)
644       message(FATAL_ERROR
645         "GXX 3.3 on OpenBSD is known to cause CPack to Crash.\n"
646         "Please use GXX 4.2 or greater to build CMake on OpenBSD\n"
647         "${CMAKE_CXX_COMPILER} version is: ${_GXX_VERSION}")
648     endif()
649   endif()
650 endif()
651
652 #-----------------------------------------------------------------------
653 # The main section of the CMakeLists file
654 #
655 #-----------------------------------------------------------------------
656 include(Source/CMakeVersion.cmake)
657
658 # Include the standard Dart testing module
659 enable_testing()
660 include (${CMAKE_ROOT}/Modules/Dart.cmake)
661
662 # Set up test-time configuration.
663 set_directory_properties(PROPERTIES
664   TEST_INCLUDE_FILE "${CMake_BINARY_DIR}/Tests/EnforceConfig.cmake")
665
666 if(NOT CMake_TEST_EXTERNAL_CMAKE)
667   # where to write the resulting executables and libraries
668   set(BUILD_SHARED_LIBS OFF)
669   set(EXECUTABLE_OUTPUT_PATH "" CACHE INTERNAL "No configurable exe dir.")
670   set(LIBRARY_OUTPUT_PATH "" CACHE INTERNAL
671     "Where to put the libraries for CMake")
672
673   # Load install destinations.
674   include(Source/CMakeInstallDestinations.cmake)
675
676   if(BUILD_TESTING)
677     include(${CMake_SOURCE_DIR}/Tests/CMakeInstall.cmake)
678   endif()
679
680   # no clue why we are testing for this here
681   include(CheckSymbolExists)
682   CHECK_SYMBOL_EXISTS(unsetenv "stdlib.h" HAVE_UNSETENV)
683   CHECK_SYMBOL_EXISTS(environ "stdlib.h" HAVE_ENVIRON_NOT_REQUIRE_PROTOTYPE)
684 endif()
685
686 # CMAKE_TESTS_CDASH_SERVER: CDash server used by CMake/Tests.
687 #
688 # If not defined or "", this variable defaults to the server at
689 # "http://open.cdash.org".
690 #
691 # If set explicitly to "NOTFOUND", curl tests and ctest tests that use
692 # the network are skipped.
693 #
694 # If set to something starting with "http://localhost/", the CDash is
695 # expected to be an instance of CDash used for CDash testing, pointing
696 # to a cdash4simpletest database. In these cases, the CDash dashboards
697 # should be run first.
698 #
699 if("x${CMAKE_TESTS_CDASH_SERVER}" STREQUAL "x" AND NOT CMake_TEST_NO_NETWORK)
700   set(CMAKE_TESTS_CDASH_SERVER "http://open.cdash.org")
701 endif()
702
703 if(CMake_TEST_EXTERNAL_CMAKE)
704   set(KWIML_TEST_ENABLE 1)
705   add_subdirectory(Utilities/KWIML)
706 endif()
707
708 if(NOT CMake_TEST_EXTERNAL_CMAKE)
709   # build the utilities (a macro defined in this file)
710   CMAKE_BUILD_UTILITIES()
711
712   if(BUILD_QtDialog)
713     if(APPLE)
714       set(CMAKE_BUNDLE_VERSION
715         "${CMake_VERSION_MAJOR}.${CMake_VERSION_MINOR}.${CMake_VERSION_PATCH}")
716       set(CMAKE_BUNDLE_LOCATION "${CMAKE_INSTALL_PREFIX}")
717       # make sure CMAKE_INSTALL_PREFIX ends in /
718       if(NOT CMAKE_INSTALL_PREFIX MATCHES "/$")
719         set(CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}/")
720       endif()
721       set(CMAKE_INSTALL_PREFIX
722         "${CMAKE_INSTALL_PREFIX}CMake.app/Contents")
723     endif()
724   endif()
725
726   if(UNIX)
727     # Install executables with the RPATH set for libraries outside the build tree.
728     # This is also suitable for binaries in the build tree.  Avoid re-link on install.
729     set(CMAKE_INSTALL_RPATH_USE_LINK_PATH ON CACHE BOOL "Install with RPATH set to find custom-built libraries.")
730     set(CMAKE_BUILD_WITH_INSTALL_RPATH ON CACHE BOOL "Build with RPATH set to match install-tree RPATH.")
731     mark_as_advanced(CMAKE_INSTALL_RPATH_USE_LINK_PATH CMAKE_BUILD_WITH_INSTALL_RPATH)
732   endif()
733
734   # add the uninstall support
735   configure_file(
736     "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
737     "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
738     @ONLY)
739   add_custom_target(uninstall
740     "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake")
741
742   include (CMakeCPack.cmake)
743
744 endif()
745
746 # setup some Testing support (a macro defined in this file)
747 CMAKE_SETUP_TESTING()
748
749 if(NOT CMake_TEST_EXTERNAL_CMAKE)
750   if(NOT CMake_VERSION_IS_RELEASE)
751     if(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND
752         NOT "${CMAKE_C_COMPILER_VERSION}" VERSION_LESS 4.2)
753       set(C_FLAGS_LIST -Wcast-align -Werror-implicit-function-declaration -Wchar-subscripts
754                        -Wall -W -Wpointer-arith -Wwrite-strings -Wformat-security
755                        -Wmissing-format-attribute -fno-common -Wundef
756       )
757       set(CXX_FLAGS_LIST -Wnon-virtual-dtor -Wcast-align -Wchar-subscripts -Wall -W
758                          -Wshadow -Wpointer-arith -Wformat-security -Wundef
759       )
760
761       foreach(FLAG_LANG  C CXX)
762         foreach(FLAG ${${FLAG_LANG}_FLAGS_LIST})
763           if(NOT " ${CMAKE_${FLAG_LANG}_FLAGS} " MATCHES " ${FLAG} ")
764             set(CMAKE_${FLAG_LANG}_FLAGS "${CMAKE_${FLAG_LANG}_FLAGS} ${FLAG}")
765           endif()
766         endforeach()
767       endforeach()
768
769       unset(C_FLAGS_LIST)
770       unset(CXX_FLAGS_LIST)
771     endif()
772   endif()
773
774   # build the remaining subdirectories
775   add_subdirectory(Source)
776   add_subdirectory(Utilities)
777 endif()
778
779 add_subdirectory(Tests)
780
781 if(NOT CMake_TEST_EXTERNAL_CMAKE)
782   if(BUILD_TESTING)
783     CMAKE_SET_TARGET_FOLDER(CMakeLibTests "Tests")
784     IF(TARGET CMakeServerLibTests)
785       CMAKE_SET_TARGET_FOLDER(CMakeServerLibTests "Tests")
786     ENDIF()
787   endif()
788   if(TARGET documentation)
789     CMAKE_SET_TARGET_FOLDER(documentation "Documentation")
790   endif()
791 endif()
792
793 if(BUILD_TESTING)
794   add_test(SystemInformationNew "${CMAKE_CMAKE_COMMAND}"
795     --system-information  -G "${CMAKE_GENERATOR}" )
796 endif()
797
798 if(NOT CMake_TEST_EXTERNAL_CMAKE)
799   # Install license file as it requires.
800   install(FILES Copyright.txt DESTINATION ${CMAKE_DOC_DIR})
801
802   # Install script directories.
803   install(
804     DIRECTORY Help Modules Templates
805     DESTINATION ${CMAKE_DATA_DIR}
806     FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ
807     DIRECTORY_PERMISSIONS OWNER_READ OWNER_EXECUTE OWNER_WRITE
808                           GROUP_READ GROUP_EXECUTE
809                           WORLD_READ WORLD_EXECUTE
810     PATTERN "*.sh*" PERMISSIONS OWNER_READ OWNER_EXECUTE OWNER_WRITE
811                                 GROUP_READ GROUP_EXECUTE
812                                 WORLD_READ WORLD_EXECUTE
813     PATTERN "ExportImportList"
814       PERMISSIONS OWNER_READ OWNER_EXECUTE OWNER_WRITE
815                   GROUP_READ GROUP_EXECUTE
816                   WORLD_READ WORLD_EXECUTE
817     REGEX "Help/(dev|guide)($|/)" EXCLUDE
818     )
819
820   # Install auxiliary files integrating with other tools.
821   add_subdirectory(Auxiliary)
822
823   # Optionally sign installed binaries.
824   if(CMake_INSTALL_SIGNTOOL)
825     configure_file(Source/CMakeInstallSignTool.cmake.in Source/CMakeInstallSignTool.cmake @ONLY)
826     install(SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/Source/CMakeInstallSignTool.cmake)
827   endif()
828 endif()