resolve cyclic dependency with zstd
[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.13...3.23 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
8 if(POLICY CMP0129)
9   cmake_policy(SET CMP0129 NEW) # CMake 3.23
10 endif()
11
12 project(CMake)
13 unset(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX)
14 unset(CMAKE_USER_MAKE_RULES_OVERRIDE_C)
15
16 # FIXME: This block should go away after a transition period.
17 if(MSVC AND NOT CMAKE_VERSION VERSION_LESS 3.15)
18   # Filter out MSVC runtime library flags that may have come from
19   # the cache of an existing build tree or from scripts.
20   foreach(l IN ITEMS C CXX)
21     foreach(c IN ITEMS DEBUG MINSIZEREL RELEASE RELWITHDEBINFO)
22       string(REGEX REPLACE "[-/]M[DT]d?( |$)" "" "CMAKE_${l}_FLAGS_${c}" "${CMAKE_${l}_FLAGS_${c}}")
23     endforeach()
24   endforeach()
25 endif()
26
27 # Make sure we can find internal find_package modules only used for
28 # building CMake and not for shipping externally
29 list(INSERT CMAKE_MODULE_PATH 0 ${CMake_SOURCE_DIR}/Source/Modules)
30
31 if(CMAKE_BOOTSTRAP)
32   # Running from bootstrap script.  Set local variable and remove from cache.
33   set(CMAKE_BOOTSTRAP 1)
34   unset(CMAKE_BOOTSTRAP CACHE)
35 endif()
36
37 if(CMake_TEST_HOST_CMAKE)
38   get_filename_component(CMake_TEST_EXTERNAL_CMAKE "${CMAKE_COMMAND}" DIRECTORY)
39 endif()
40
41 if(NOT CMake_TEST_EXTERNAL_CMAKE)
42   if(CMAKE_SYSTEM_NAME STREQUAL "HP-UX")
43     message(FATAL_ERROR
44       "CMake no longer compiles on HP-UX.  See\n"
45       "  https://gitlab.kitware.com/cmake/cmake/-/issues/17137\n"
46       "Use CMake 3.9 or lower instead."
47       )
48   endif()
49
50   set(CMake_BIN_DIR ${CMake_BINARY_DIR}/bin)
51 endif()
52
53 if(CMake_GUI_DISTRIBUTE_WITH_Qt_LGPL)
54   if(CMake_GUI_DISTRIBUTE_WITH_Qt_LGPL MATCHES "^3|2\\.1$")
55     set(USE_LGPL "${CMake_GUI_DISTRIBUTE_WITH_Qt_LGPL}")
56   else()
57     set(USE_LGPL "2.1")
58   endif()
59 else()
60   set(USE_LGPL "")
61 endif()
62
63 # Use most-recent available language dialects with GNU and Clang
64 if(NOT DEFINED CMAKE_C_STANDARD AND NOT CMake_NO_C_STANDARD)
65   include(${CMake_SOURCE_DIR}/Source/Checks/cm_c11_thread_local.cmake)
66   if(NOT CMake_C11_THREAD_LOCAL_BROKEN)
67     set(CMAKE_C_STANDARD 11)
68   else()
69     set(CMAKE_C_STANDARD 99)
70   endif()
71 endif()
72 if(NOT DEFINED CMAKE_CXX_STANDARD AND NOT CMake_NO_CXX_STANDARD)
73   if(CMAKE_CXX_COMPILER_ID STREQUAL SunPro AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.14)
74     set(CMAKE_CXX_STANDARD 98)
75   else()
76     if(NOT CMAKE_VERSION VERSION_LESS 3.8)
77       include(${CMake_SOURCE_DIR}/Source/Checks/cm_cxx17_check.cmake)
78     else()
79       set(CMake_CXX17_BROKEN 1)
80     endif()
81     if(NOT CMake_CXX17_BROKEN)
82       set(CMAKE_CXX_STANDARD 17)
83     else()
84       include(${CMake_SOURCE_DIR}/Source/Checks/cm_cxx14_check.cmake)
85       if(NOT CMake_CXX14_BROKEN)
86         set(CMAKE_CXX_STANDARD 14)
87       else()
88         set(CMAKE_CXX_STANDARD 11)
89       endif()
90     endif()
91   endif()
92 endif()
93 if(NOT CMake_TEST_EXTERNAL_CMAKE)
94   # include special compile flags for some compilers
95   include(CompileFlags.cmake)
96
97   # check for available C++ features
98   include(${CMake_SOURCE_DIR}/Source/Checks/cm_cxx_features.cmake)
99
100   if(NOT CMake_HAVE_CXX_UNIQUE_PTR)
101     message(FATAL_ERROR "The C++ compiler does not support C++11 (e.g. std::unique_ptr).")
102   endif()
103 endif()
104
105 # Inform STL library header wrappers whether to use system versions.
106 configure_file(Utilities/std/cmSTL.hxx.in Utilities/cmSTL.hxx @ONLY)
107
108 # set the internal encoding of CMake to UTF-8
109 set(KWSYS_ENCODING_DEFAULT_CODEPAGE CP_UTF8)
110
111 # option to use COMPONENT with install command
112 option(CMake_INSTALL_COMPONENTS "Using components when installing" OFF)
113 mark_as_advanced(CMake_INSTALL_COMPONENTS)
114 macro(CMake_OPTIONAL_COMPONENT NAME)
115   if(CMake_INSTALL_COMPONENTS)
116     set(COMPONENT COMPONENT ${NAME})
117   else()
118     set(COMPONENT)
119   endif()
120 endmacro()
121
122 # option to disable installing 3rd-party dependencies
123 option(CMake_INSTALL_DEPENDENCIES
124   "Whether to install 3rd-party runtime dependencies" OFF)
125 mark_as_advanced(CMake_INSTALL_DEPENDENCIES)
126
127 # option to build reference for CMake developers
128 option(CMake_BUILD_DEVELOPER_REFERENCE
129   "Build CMake Developer Reference" OFF)
130 mark_as_advanced(CMake_BUILD_DEVELOPER_REFERENCE)
131
132 # option to build using interprocedural optimizations (IPO/LTO)
133 if(NOT CMAKE_VERSION VERSION_LESS 3.12.2)
134   option(CMake_BUILD_LTO "Compile CMake with link-time optimization if supported" OFF)
135   if(CMake_BUILD_LTO)
136     include(CheckIPOSupported)
137     check_ipo_supported(RESULT HAVE_IPO)
138     if(HAVE_IPO)
139       set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
140     endif()
141   endif()
142 endif()
143
144 #-----------------------------------------------------------------------
145 # a macro to deal with system libraries, implemented as a macro
146 # simply to improve readability of the main script
147 #-----------------------------------------------------------------------
148 macro(CMAKE_HANDLE_SYSTEM_LIBRARIES)
149   # Options have dependencies.
150   include(CMakeDependentOption)
151
152   # Allow the user to enable/disable all system utility library options by
153   # defining CMAKE_USE_SYSTEM_LIBRARIES or CMAKE_USE_SYSTEM_LIBRARY_${util}.
154   set(UTILITIES BZIP2 CURL EXPAT FORM JSONCPP LIBARCHIVE LIBLZMA LIBRHASH LIBUV NGHTTP2 ZLIB ZSTD)
155   foreach(util IN LISTS UTILITIES)
156     if(NOT DEFINED CMAKE_USE_SYSTEM_LIBRARY_${util}
157         AND DEFINED CMAKE_USE_SYSTEM_LIBRARIES)
158       set(CMAKE_USE_SYSTEM_LIBRARY_${util} "${CMAKE_USE_SYSTEM_LIBRARIES}")
159     endif()
160     if(DEFINED CMAKE_USE_SYSTEM_LIBRARY_${util})
161       if(CMAKE_USE_SYSTEM_LIBRARY_${util})
162         set(CMAKE_USE_SYSTEM_LIBRARY_${util} ON)
163       else()
164         set(CMAKE_USE_SYSTEM_LIBRARY_${util} OFF)
165       endif()
166       if(CMAKE_BOOTSTRAP)
167         unset(CMAKE_USE_SYSTEM_LIBRARY_${util} CACHE)
168       endif()
169       string(TOLOWER "${util}" lutil)
170       set(CMAKE_USE_SYSTEM_${util} "${CMAKE_USE_SYSTEM_LIBRARY_${util}}"
171         CACHE BOOL "Use system-installed ${lutil}" FORCE)
172     else()
173       set(CMAKE_USE_SYSTEM_LIBRARY_${util} OFF)
174     endif()
175   endforeach()
176   if(CMAKE_BOOTSTRAP)
177     unset(CMAKE_USE_SYSTEM_LIBRARIES CACHE)
178   endif()
179
180   # Optionally use system utility libraries.
181   option(CMAKE_USE_SYSTEM_LIBARCHIVE "Use system-installed libarchive" "${CMAKE_USE_SYSTEM_LIBRARY_LIBARCHIVE}")
182   option(CMAKE_USE_SYSTEM_CURL "Use system-installed curl" "${CMAKE_USE_SYSTEM_LIBRARY_CURL}")
183   option(CMAKE_USE_SYSTEM_EXPAT "Use system-installed expat" "${CMAKE_USE_SYSTEM_LIBRARY_EXPAT}")
184   CMAKE_DEPENDENT_OPTION(CMAKE_USE_SYSTEM_ZLIB "Use system-installed zlib"
185     "${CMAKE_USE_SYSTEM_LIBRARY_ZLIB}" "NOT CMAKE_USE_SYSTEM_LIBARCHIVE;NOT CMAKE_USE_SYSTEM_CURL" ON)
186   CMAKE_DEPENDENT_OPTION(CMAKE_USE_SYSTEM_BZIP2 "Use system-installed bzip2"
187     "${CMAKE_USE_SYSTEM_LIBRARY_BZIP2}" "NOT CMAKE_USE_SYSTEM_LIBARCHIVE" ON)
188   CMAKE_DEPENDENT_OPTION(CMAKE_USE_SYSTEM_ZSTD "Use system-installed zstd"
189     "${CMAKE_USE_SYSTEM_LIBRARY_ZSTD}" "NOT CMAKE_USE_SYSTEM_LIBARCHIVE" ON)
190   CMAKE_DEPENDENT_OPTION(CMAKE_USE_SYSTEM_LIBLZMA "Use system-installed liblzma"
191     "${CMAKE_USE_SYSTEM_LIBRARY_LIBLZMA}" "NOT CMAKE_USE_SYSTEM_LIBARCHIVE" ON)
192   CMAKE_DEPENDENT_OPTION(CMAKE_USE_SYSTEM_NGHTTP2 "Use system-installed nghttp2"
193     "${CMAKE_USE_SYSTEM_LIBRARY_NGHTTP2}" "NOT CMAKE_USE_SYSTEM_CURL" ON)
194   option(CMAKE_USE_SYSTEM_FORM "Use system-installed libform" "${CMAKE_USE_SYSTEM_LIBRARY_FORM}")
195   option(CMAKE_USE_SYSTEM_JSONCPP "Use system-installed jsoncpp" "${CMAKE_USE_SYSTEM_LIBRARY_JSONCPP}")
196   option(CMAKE_USE_SYSTEM_LIBRHASH "Use system-installed librhash" "${CMAKE_USE_SYSTEM_LIBRARY_LIBRHASH}")
197   option(CMAKE_USE_SYSTEM_LIBUV "Use system-installed libuv" "${CMAKE_USE_SYSTEM_LIBRARY_LIBUV}")
198
199   # For now use system KWIML only if explicitly requested rather
200   # than activating via the general system libs options.
201   option(CMAKE_USE_SYSTEM_KWIML "Use system-installed KWIML" OFF)
202   mark_as_advanced(CMAKE_USE_SYSTEM_KWIML)
203
204   # Mention to the user what system libraries are being used.
205   foreach(util IN LISTS UTILITIES ITEMS KWIML)
206     if(CMAKE_USE_SYSTEM_${util})
207       message(STATUS "Using system-installed ${util}")
208     endif()
209   endforeach()
210
211   # Inform utility library header wrappers whether to use system versions.
212   configure_file(Utilities/cmThirdParty.h.in Utilities/cmThirdParty.h @ONLY)
213
214 endmacro()
215
216 #-----------------------------------------------------------------------
217 # a macro to determine the generator and ctest executable to use
218 # for testing. Simply to improve readability of the main script.
219 #-----------------------------------------------------------------------
220 macro(CMAKE_SETUP_TESTING)
221   if(BUILD_TESTING)
222     set(CMAKE_TEST_SYSTEM_LIBRARIES 0)
223     foreach(util IN ITEMS CURL EXPAT ZLIB)
224       if(CMAKE_USE_SYSTEM_${util})
225         set(CMAKE_TEST_SYSTEM_LIBRARIES 1)
226       endif()
227     endforeach()
228
229     # This variable is set by cmake, however to
230     # test cmake we want to make sure that
231     # the ctest from this cmake is used for testing
232     # and not the ctest from the cmake building and testing
233     # cmake.
234     if(CMake_TEST_EXTERNAL_CMAKE)
235       set(CMAKE_CTEST_COMMAND "${CMake_TEST_EXTERNAL_CMAKE}/ctest")
236       set(CMAKE_CMAKE_COMMAND "${CMake_TEST_EXTERNAL_CMAKE}/cmake")
237       set(CMAKE_CPACK_COMMAND "${CMake_TEST_EXTERNAL_CMAKE}/cpack")
238       foreach(exe IN ITEMS cmake ctest cpack)
239         add_executable(${exe} IMPORTED)
240         set_property(TARGET ${exe} PROPERTY IMPORTED_LOCATION ${CMake_TEST_EXTERNAL_CMAKE}/${exe})
241       endforeach()
242     else()
243       set(CMAKE_CTEST_COMMAND "${CMake_BIN_DIR}/ctest")
244       set(CMAKE_CMAKE_COMMAND "${CMake_BIN_DIR}/cmake")
245       set(CMAKE_CPACK_COMMAND "${CMake_BIN_DIR}/cpack")
246     endif()
247   endif()
248
249   # configure some files for testing
250   configure_file(Templates/CTestScript.cmake.in CTestScript.cmake @ONLY)
251   configure_file(Tests/.NoDartCoverage Tests/.NoDartCoverage)
252   configure_file(Tests/.NoDartCoverage Modules/.NoDartCoverage)
253   configure_file(CTestCustom.cmake.in CTestCustom.cmake @ONLY)
254   if(BUILD_TESTING AND DART_ROOT)
255     configure_file(CMakeLogo.gif Testing/HTML/TestingResults/Icons/Logo.gif COPYONLY)
256   endif()
257   mark_as_advanced(DART_ROOT)
258 endmacro()
259
260
261 # Provide a way for Visual Studio Express users to turn OFF the new FOLDER
262 # organization feature. Default to ON for non-Express users. Express users must
263 # explicitly turn off this option to build CMake in the Express IDE...
264 #
265 option(CMAKE_USE_FOLDERS "Enable folder grouping of projects in IDEs." ON)
266 mark_as_advanced(CMAKE_USE_FOLDERS)
267
268
269 option(CMake_RUN_CLANG_TIDY "Run clang-tidy with the compiler." OFF)
270 if(CMake_RUN_CLANG_TIDY)
271   if(CMake_SOURCE_DIR STREQUAL CMake_BINARY_DIR)
272     message(FATAL_ERROR "CMake_RUN_CLANG_TIDY requires an out-of-source build!")
273   endif()
274   find_program(CLANG_TIDY_COMMAND NAMES clang-tidy)
275   if(NOT CLANG_TIDY_COMMAND)
276     message(FATAL_ERROR "CMake_RUN_CLANG_TIDY is ON but clang-tidy is not found!")
277   endif()
278   set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_COMMAND}")
279
280   # Create a preprocessor definition that depends on .clang-tidy content so
281   # the compile command will change when .clang-tidy changes.  This ensures
282   # that a subsequent build re-runs clang-tidy on all sources even if they
283   # do not otherwise need to be recompiled.  Nothing actually uses this
284   # definition.  We add it to targets on which we run clang-tidy just to
285   # get the build dependency on the .clang-tidy file.
286   file(SHA1 ${CMAKE_CURRENT_SOURCE_DIR}/.clang-tidy clang_tidy_sha1)
287   set(CLANG_TIDY_DEFINITIONS "CLANG_TIDY_SHA1=${clang_tidy_sha1}")
288   unset(clang_tidy_sha1)
289
290 endif()
291 configure_file(.clang-tidy .clang-tidy COPYONLY)
292
293
294 option(CMake_RUN_IWYU "Run include-what-you-use with the compiler." OFF)
295 if(CMake_RUN_IWYU)
296   find_program(IWYU_COMMAND NAMES include-what-you-use iwyu)
297   if(NOT IWYU_COMMAND)
298     message(FATAL_ERROR "CMake_RUN_IWYU is ON but include-what-you-use is not found!")
299   endif()
300   set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE
301     "${IWYU_COMMAND};-Xiwyu;--mapping_file=${CMake_SOURCE_DIR}/Utilities/IWYU/mapping.imp;-w")
302   list(APPEND CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${CMake_IWYU_OPTIONS})
303 endif()
304
305
306 #-----------------------------------------------------------------------
307 # a macro that only sets the FOLDER target property if it's
308 # "appropriate"
309 #-----------------------------------------------------------------------
310 macro(CMAKE_SET_TARGET_FOLDER tgt folder)
311   if(CMAKE_USE_FOLDERS)
312     set_property(GLOBAL PROPERTY USE_FOLDERS ON)
313     if(MSVC AND TARGET ${tgt})
314       set_property(TARGET "${tgt}" PROPERTY FOLDER "${folder}")
315     endif()
316   else()
317     set_property(GLOBAL PROPERTY USE_FOLDERS OFF)
318   endif()
319 endmacro()
320
321 #-----------------------------------------------------------------------
322 if(NOT CMake_TEST_EXTERNAL_CMAKE)
323   if(CMAKE_CXX_PLATFORM_ID MATCHES "OpenBSD")
324     execute_process(COMMAND ${CMAKE_CXX_COMPILER}
325       ${CMAKE_CXX_COMPILER_ARG1} -dumpversion
326       OUTPUT_VARIABLE _GXX_VERSION
327     )
328     string(REGEX REPLACE "([0-9])\\.([0-9])(\\.[0-9])?" "\\1\\2"
329       _GXX_VERSION_SHORT ${_GXX_VERSION})
330     if(_GXX_VERSION_SHORT EQUAL 33)
331       message(FATAL_ERROR
332         "GXX 3.3 on OpenBSD is known to cause CPack to Crash.\n"
333         "Please use GXX 4.2 or greater to build CMake on OpenBSD\n"
334         "${CMAKE_CXX_COMPILER} version is: ${_GXX_VERSION}")
335     endif()
336   endif()
337 endif()
338
339 #-----------------------------------------------------------------------
340 # The main section of the CMakeLists file
341 #
342 #-----------------------------------------------------------------------
343 include(Source/CMakeVersion.cmake)
344
345 # Include the standard Dart testing module
346 enable_testing()
347 include(${CMAKE_ROOT}/Modules/Dart.cmake)
348
349 # Set up test-time configuration.
350 set_directory_properties(PROPERTIES
351   TEST_INCLUDE_FILE "${CMake_BINARY_DIR}/Tests/EnforceConfig.cmake")
352
353 if(NOT CMake_TEST_EXTERNAL_CMAKE)
354   # where to write the resulting executables and libraries
355   set(BUILD_SHARED_LIBS OFF)
356   set(EXECUTABLE_OUTPUT_PATH "" CACHE INTERNAL "No configurable exe dir.")
357   set(LIBRARY_OUTPUT_PATH "" CACHE INTERNAL
358     "Where to put the libraries for CMake")
359
360   # Load install destinations.
361   include(Source/CMakeInstallDestinations.cmake)
362
363   if(BUILD_TESTING)
364     include(${CMake_SOURCE_DIR}/Tests/CMakeInstall.cmake)
365   endif()
366
367   # Checks for cmSystemTools.
368   if(WIN32)
369     set(HAVE_UNSETENV 0)
370     set(HAVE_ENVIRON_NOT_REQUIRE_PROTOTYPE 1)
371   else()
372     include(CheckSymbolExists)
373     CHECK_SYMBOL_EXISTS(unsetenv "stdlib.h" HAVE_UNSETENV)
374     CHECK_SYMBOL_EXISTS(environ "stdlib.h" HAVE_ENVIRON_NOT_REQUIRE_PROTOTYPE)
375   endif()
376 endif()
377
378 # CMAKE_TESTS_CDASH_SERVER: CDash server used by CMake/Tests.
379 #
380 # If not defined or "", this variable defaults to the server at
381 # "http://open.cdash.org".
382 #
383 # If set explicitly to "NOTFOUND", curl tests and ctest tests that use
384 # the network are skipped.
385 #
386 # If set to something starting with "http://localhost/", the CDash is
387 # expected to be an instance of CDash used for CDash testing, pointing
388 # to a cdash4simpletest database. In these cases, the CDash dashboards
389 # should be run first.
390 #
391 if("x${CMAKE_TESTS_CDASH_SERVER}" STREQUAL "x" AND NOT CMake_TEST_NO_NETWORK)
392   set(CMAKE_TESTS_CDASH_SERVER "http://open.cdash.org")
393 endif()
394
395 if(CMake_TEST_EXTERNAL_CMAKE)
396   set(KWIML_TEST_ENABLE 1)
397   add_subdirectory(Utilities/KWIML)
398 endif()
399
400 if(NOT CMake_TEST_EXTERNAL_CMAKE)
401   find_package(Threads)
402   # build the utilities
403   include(CMakeBuildUtilities)
404
405   if(BUILD_QtDialog)
406     if(APPLE)
407       set(CMAKE_BUNDLE_VERSION
408         "${CMake_VERSION_MAJOR}.${CMake_VERSION_MINOR}.${CMake_VERSION_PATCH}")
409       set(CMAKE_BUNDLE_LOCATION "${CMAKE_INSTALL_PREFIX}")
410       # make sure CMAKE_INSTALL_PREFIX ends in /
411       if(NOT CMAKE_INSTALL_PREFIX MATCHES "/$")
412         string(APPEND CMAKE_INSTALL_PREFIX "/")
413       endif()
414       string(APPEND CMAKE_INSTALL_PREFIX "CMake.app/Contents")
415     endif()
416   endif()
417
418   if(UNIX)
419     # Install executables with the RPATH set for libraries outside the build tree.
420     # This is also suitable for binaries in the build tree.  Avoid re-link on install.
421     set(CMAKE_INSTALL_RPATH_USE_LINK_PATH ON CACHE BOOL "Install with RPATH set to find custom-built libraries.")
422     set(CMAKE_BUILD_WITH_INSTALL_RPATH ON CACHE BOOL "Build with RPATH set to match install-tree RPATH.")
423     mark_as_advanced(CMAKE_INSTALL_RPATH_USE_LINK_PATH CMAKE_BUILD_WITH_INSTALL_RPATH)
424   endif()
425
426   # add the uninstall support
427   configure_file(cmake_uninstall.cmake.in cmake_uninstall.cmake @ONLY)
428   add_custom_target(uninstall
429     "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake")
430
431   include(CMakeCPack.cmake)
432
433 endif()
434
435 # setup some Testing support (a macro defined in this file)
436 CMAKE_SETUP_TESTING()
437
438 if(NOT CMake_TEST_EXTERNAL_CMAKE)
439   if(NOT CMake_VERSION_IS_RELEASE)
440     if((CMAKE_C_COMPILER_ID STREQUAL "GNU" AND
441         NOT "${CMAKE_C_COMPILER_VERSION}" VERSION_LESS 4.2) OR
442        (CMAKE_C_COMPILER_ID STREQUAL "Clang" AND
443         NOT "${CMAKE_C_COMPILER_VERSION}" VERSION_LESS 3.0 AND
444         NOT "x${CMAKE_C_SIMULATE_ID}" STREQUAL "xMSVC") OR
445        CMAKE_C_COMPILER_ID STREQUAL "AppleClang" OR
446        CMAKE_C_COMPILER_ID STREQUAL "LCC")
447       set(C_FLAGS_LIST -Wcast-align -Werror-implicit-function-declaration -Wchar-subscripts
448                        -Wall -W -Wpointer-arith -Wwrite-strings -Wformat-security
449                        -Wmissing-format-attribute -fno-common -Wundef
450       )
451       set(CXX_FLAGS_LIST -Wnon-virtual-dtor -Wcast-align -Wchar-subscripts -Wall -W
452                          -Wshadow -Wpointer-arith -Wformat-security -Wundef
453       )
454
455       foreach(FLAG_LANG IN ITEMS C CXX)
456         foreach(FLAG IN LISTS ${FLAG_LANG}_FLAGS_LIST)
457           if(NOT " ${CMAKE_${FLAG_LANG}_FLAGS} " MATCHES " ${FLAG} ")
458             string(APPEND CMAKE_${FLAG_LANG}_FLAGS " ${FLAG}")
459           endif()
460         endforeach()
461       endforeach()
462
463       unset(C_FLAGS_LIST)
464       unset(CXX_FLAGS_LIST)
465     endif()
466   endif()
467
468   # build the remaining subdirectories
469   add_subdirectory(Source)
470   add_subdirectory(Utilities)
471 endif()
472
473 add_subdirectory(Tests)
474
475 if(NOT CMake_TEST_EXTERNAL_CMAKE)
476   if(BUILD_TESTING)
477     CMAKE_SET_TARGET_FOLDER(CMakeLibTests "Tests")
478   endif()
479   if(TARGET documentation)
480     CMAKE_SET_TARGET_FOLDER(documentation "Documentation")
481   endif()
482 endif()
483
484 if(BUILD_TESTING)
485   add_test(SystemInformationNew "${CMAKE_CMAKE_COMMAND}"
486     --system-information  -G "${CMAKE_GENERATOR}"
487   )
488 endif()
489
490 if(NOT CMake_TEST_EXTERNAL_CMAKE)
491   # Install license file as it requires.
492   install(FILES Copyright.txt DESTINATION ${CMAKE_DOC_DIR})
493
494   # Install script directories.
495   install(
496     DIRECTORY Help Modules Templates
497     DESTINATION ${CMAKE_DATA_DIR}
498     FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ
499     DIRECTORY_PERMISSIONS OWNER_READ OWNER_EXECUTE OWNER_WRITE
500                           GROUP_READ GROUP_EXECUTE
501                           WORLD_READ WORLD_EXECUTE
502     PATTERN "*.sh*" PERMISSIONS OWNER_READ OWNER_EXECUTE OWNER_WRITE
503                                 GROUP_READ GROUP_EXECUTE
504                                 WORLD_READ WORLD_EXECUTE
505     REGEX "/(ExportImportList|cpp)$"
506       PERMISSIONS OWNER_READ OWNER_EXECUTE OWNER_WRITE
507                   GROUP_READ GROUP_EXECUTE
508                   WORLD_READ WORLD_EXECUTE
509     REGEX "Help/(dev|guide)($|/)" EXCLUDE
510     )
511
512   # Install auxiliary files integrating with other tools.
513   add_subdirectory(Auxiliary)
514
515   # Optionally sign installed binaries.
516   if(CMake_INSTALL_SIGNTOOL)
517     configure_file(Source/CMakeInstallSignTool.cmake.in Source/CMakeInstallSignTool.cmake @ONLY)
518     install(SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/Source/CMakeInstallSignTool.cmake)
519   endif()
520 endif()