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