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