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