Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Source / Modules / CMakeBuildUtilities.cmake
1 #-----------------------------------------------------------------------
2 # Build the utilities used by CMake
3 #
4 # Originally it was a macro in the root `CMakeLists.txt` with the comment
5 # "Simply to improve readability...".
6 # However, as part of the modernization refactoring it was moved into a
7 # separate file cuz adding library alises wasn't possible inside the
8 # macro.
9 #-----------------------------------------------------------------------
10
11 # Suppress unnecessary checks in third-party code.
12 include(Utilities/cmThirdPartyChecks.cmake)
13
14 #---------------------------------------------------------------------
15 # Create the kwsys library for CMake.
16 set(KWSYS_NAMESPACE cmsys)
17 set(KWSYS_USE_SystemTools 1)
18 set(KWSYS_USE_Directory 1)
19 set(KWSYS_USE_RegularExpression 1)
20 set(KWSYS_USE_Base64 1)
21 set(KWSYS_USE_MD5 1)
22 set(KWSYS_USE_Process 1)
23 set(KWSYS_USE_CommandLineArguments 1)
24 set(KWSYS_USE_ConsoleBuf 1)
25 set(KWSYS_HEADER_ROOT ${CMake_BINARY_DIR}/Source)
26 set(KWSYS_INSTALL_DOC_DIR "${CMAKE_DOC_DIR}")
27 if(CMake_NO_CXX_STANDARD)
28   set(KWSYS_CXX_STANDARD "")
29 endif()
30 if(CMake_NO_SELF_BACKTRACE)
31   set(KWSYS_NO_EXECINFO 1)
32 endif()
33 if(WIN32)
34   # FIXME: Teach KWSys to hard-code these checks on Windows.
35   set(KWSYS_C_HAS_CLOCK_GETTIME_MONOTONIC_COMPILED 0)
36   set(KWSYS_C_HAS_PTRDIFF_T_COMPILED 1)
37   set(KWSYS_CXX_HAS_ENVIRON_IN_STDLIB_H_COMPILED 1)
38   set(KWSYS_CXX_HAS_RLIMIT64_COMPILED 0)
39   set(KWSYS_CXX_HAS_SETENV_COMPILED 0)
40   set(KWSYS_CXX_HAS_UNSETENV_COMPILED 0)
41   set(KWSYS_CXX_HAS_UTIMENSAT_COMPILED 0)
42   set(KWSYS_CXX_HAS_UTIMES_COMPILED 0)
43   set(KWSYS_CXX_STAT_HAS_ST_MTIM_COMPILED 0)
44   set(KWSYS_CXX_STAT_HAS_ST_MTIMESPEC_COMPILED 0)
45   set(KWSYS_STL_HAS_WSTRING_COMPILED 1)
46   set(KWSYS_SYS_HAS_IFADDRS_H 0)
47 endif()
48 add_subdirectory(Source/kwsys)
49 set(kwsys_folder "Utilities/KWSys")
50 CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE} "${kwsys_folder}")
51 CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}_c "${kwsys_folder}")
52 if(BUILD_TESTING)
53   CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}TestDynload "${kwsys_folder}")
54   CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}TestProcess "${kwsys_folder}")
55   CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}TestsC "${kwsys_folder}")
56   CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}TestsCxx "${kwsys_folder}")
57   CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}TestSharedForward "${kwsys_folder}")
58 endif()
59
60 #---------------------------------------------------------------------
61 # Setup third-party libraries.
62 # Everything in the tree should be able to include files from the
63 # Utilities directory.
64 if((CMAKE_SYSTEM_NAME STREQUAL "AIX" OR CMAKE_SYSTEM_NAME STREQUAL "OS400") AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
65   # using -isystem option generate error "template with C linkage"
66   include_directories("${CMake_SOURCE_DIR}/Utilities/std")
67 else()
68   include_directories(SYSTEM "${CMake_SOURCE_DIR}/Utilities/std")
69 endif()
70
71 include_directories("${CMake_BINARY_DIR}/Utilities")
72 if((CMAKE_SYSTEM_NAME STREQUAL "AIX" OR CMAKE_SYSTEM_NAME STREQUAL "OS400") AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
73   # using -isystem option generate error "template with C linkage"
74   include_directories("${CMake_SOURCE_DIR}/Utilities")
75 else()
76   include_directories(SYSTEM "${CMake_SOURCE_DIR}/Utilities")
77 endif()
78
79 #---------------------------------------------------------------------
80 # Build CMake std library for CMake and CTest.
81 add_subdirectory(Utilities/std)
82 CMAKE_SET_TARGET_FOLDER(cmstd "Utilities/std")
83
84 # check for the use of system libraries versus builtin ones
85 # (a macro defined in this file)
86 CMAKE_HANDLE_SYSTEM_LIBRARIES()
87
88 if(CMAKE_USE_SYSTEM_KWIML)
89   find_package(KWIML 1.0)
90   if(NOT KWIML_FOUND)
91     message(FATAL_ERROR "CMAKE_USE_SYSTEM_KWIML is ON but KWIML is not found!")
92   endif()
93 else()
94   if(BUILD_TESTING)
95     set(KWIML_TEST_ENABLE 1)
96   endif()
97   add_subdirectory(Utilities/KWIML)
98 endif()
99
100 if(CMAKE_USE_SYSTEM_LIBRHASH)
101   find_package(LibRHash)
102   if(NOT LibRHash_FOUND)
103     message(FATAL_ERROR
104       "CMAKE_USE_SYSTEM_LIBRHASH is ON but LibRHash is not found!")
105   endif()
106 else()
107   add_subdirectory(Utilities/cmlibrhash)
108   add_library(LibRHash::LibRHash ALIAS cmlibrhash)
109   CMAKE_SET_TARGET_FOLDER(cmlibrhash "Utilities/3rdParty")
110 endif()
111
112 #---------------------------------------------------------------------
113 # Build zlib library for Curl, CMake, and CTest.
114 if(CMAKE_USE_SYSTEM_ZLIB)
115   find_package(ZLIB)
116   if(NOT ZLIB_FOUND)
117     message(FATAL_ERROR
118       "CMAKE_USE_SYSTEM_ZLIB is ON but a zlib is not found!")
119   endif()
120 else()
121   if(NOT POLICY CMP0102) # CMake < 3.17
122     # Store in cache to protect from mark_as_advanced.
123     set(ZLIB_INCLUDE_DIR ${CMake_SOURCE_DIR}/Utilities CACHE PATH "")
124   else()
125     set(ZLIB_INCLUDE_DIR ${CMake_SOURCE_DIR}/Utilities)
126   endif()
127   set(ZLIB_LIBRARY cmzlib)
128   set(WITHOUT_ZLIB_DLL "")
129   set(WITHOUT_ZLIB_DLL_WITH_LIB cmzlib)
130   set(ZLIB_DLL "")
131   set(ZLIB_DLL_WITH_LIB cmzlib)
132   set(ZLIB_WINAPI "")
133   set(ZLIB_WINAPI_COMPILED 0)
134   set(ZLIB_WINAPI_WITH_LIB cmzlib)
135   add_subdirectory(Utilities/cmzlib)
136   add_library(ZLIB::ZLIB ALIAS cmzlib)
137   CMAKE_SET_TARGET_FOLDER(cmzlib "Utilities/3rdParty")
138 endif()
139
140 #---------------------------------------------------------------------
141 # Build Curl library for CTest.
142 if(CMAKE_USE_SYSTEM_CURL)
143   find_package(CURL)
144   if(NOT CURL_FOUND)
145     message(FATAL_ERROR
146       "CMAKE_USE_SYSTEM_CURL is ON but a curl is not found!")
147   endif()
148 else()
149   if(CMAKE_TESTS_CDASH_SERVER)
150     set(CMAKE_CURL_TEST_URL "${CMAKE_TESTS_CDASH_SERVER}/user.php")
151   endif()
152   set(_CMAKE_USE_OPENSSL_DEFAULT OFF)
153   if(NOT DEFINED CMAKE_USE_OPENSSL AND NOT WIN32 AND NOT APPLE
154       AND CMAKE_SYSTEM_NAME MATCHES "(Linux|FreeBSD)")
155     set(_CMAKE_USE_OPENSSL_DEFAULT ON)
156   endif()
157   option(CMAKE_USE_OPENSSL "Use OpenSSL." ${_CMAKE_USE_OPENSSL_DEFAULT})
158   mark_as_advanced(CMAKE_USE_OPENSSL)
159   if(CMAKE_USE_OPENSSL)
160     set(CURL_CA_BUNDLE "" CACHE FILEPATH "Path to SSL CA Certificate Bundle")
161     set(CURL_CA_PATH "" CACHE PATH "Path to SSL CA Certificate Directory")
162     mark_as_advanced(CURL_CA_BUNDLE CURL_CA_PATH)
163   endif()
164   if(NOT CMAKE_USE_SYSTEM_NGHTTP2)
165     # Tell curl's FindNGHTTP2 module to use our library.
166     set(NGHTTP2_LIBRARY cmnghttp2)
167     set(NGHTTP2_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Utilities/cmnghttp2/lib/includes)
168   endif()
169   add_subdirectory(Utilities/cmcurl)
170   add_library(CURL::libcurl ALIAS cmcurl)
171   CMAKE_SET_TARGET_FOLDER(cmcurl "Utilities/3rdParty")
172   CMAKE_SET_TARGET_FOLDER(LIBCURL "Utilities/3rdParty")
173   if(NOT CMAKE_USE_SYSTEM_NGHTTP2)
174     # Configure after curl to re-use some check results.
175     add_subdirectory(Utilities/cmnghttp2)
176     CMAKE_SET_TARGET_FOLDER(cmnghttp2 "Utilities/3rdParty")
177   endif()
178 endif()
179
180 #---------------------------------------------------------------------
181 # Build expat library for CMake, CTest, and libarchive.
182 if(CMAKE_USE_SYSTEM_EXPAT)
183   find_package(EXPAT)
184   if(NOT EXPAT_FOUND)
185     message(FATAL_ERROR
186       "CMAKE_USE_SYSTEM_EXPAT is ON but a expat is not found!")
187   endif()
188   set(CMAKE_EXPAT_INCLUDES ${EXPAT_INCLUDE_DIRS})
189   set(CMAKE_EXPAT_LIBRARIES ${EXPAT_LIBRARIES})
190 else()
191   set(CMAKE_EXPAT_INCLUDES)
192   set(CMAKE_EXPAT_LIBRARIES cmexpat)
193   add_subdirectory(Utilities/cmexpat)
194   add_library(EXPAT::EXPAT ALIAS cmexpat)
195   CMAKE_SET_TARGET_FOLDER(cmexpat "Utilities/3rdParty")
196 endif()
197
198 #---------------------------------------------------------------------
199 # Build or use system libbz2 for libarchive.
200 if(NOT CMAKE_USE_SYSTEM_LIBARCHIVE)
201   if(CMAKE_USE_SYSTEM_BZIP2)
202     find_package(BZip2)
203   else()
204     set(BZIP2_INCLUDE_DIR
205       "${CMAKE_CURRENT_SOURCE_DIR}/Utilities/cmbzip2")
206     set(BZIP2_LIBRARIES cmbzip2)
207     set(BZIP2_NEED_PREFIX "")
208     set(USE_BZIP2_DLL "")
209     set(USE_BZIP2_DLL_WITH_LIB cmbzip2)
210     set(USE_BZIP2_STATIC "")
211     set(USE_BZIP2_STATIC_WITH_LIB cmbzip2)
212     add_subdirectory(Utilities/cmbzip2)
213     CMAKE_SET_TARGET_FOLDER(cmbzip2 "Utilities/3rdParty")
214   endif()
215 endif()
216
217 #---------------------------------------------------------------------
218 # Build or use system zstd for libarchive.
219 if(NOT CMAKE_USE_SYSTEM_LIBARCHIVE)
220   if(NOT CMAKE_USE_SYSTEM_ZSTD)
221     set(ZSTD_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Utilities/cmzstd")
222     set(ZSTD_LIBRARY cmzstd)
223     add_subdirectory(Utilities/cmzstd)
224     CMAKE_SET_TARGET_FOLDER(cmzstd "Utilities/3rdParty")
225   endif()
226 endif()
227
228 #---------------------------------------------------------------------
229 # Build or use system liblzma for libarchive.
230 if(NOT CMAKE_USE_SYSTEM_LIBARCHIVE)
231   if(CMAKE_USE_SYSTEM_LIBLZMA)
232     find_package(LibLZMA)
233     if(NOT LIBLZMA_FOUND)
234       message(FATAL_ERROR "CMAKE_USE_SYSTEM_LIBLZMA is ON but LibLZMA is not found!")
235     endif()
236   else()
237     add_subdirectory(Utilities/cmliblzma)
238     CMAKE_SET_TARGET_FOLDER(cmliblzma "Utilities/3rdParty")
239     set(LIBLZMA_HAS_AUTO_DECODER 1)
240     set(LIBLZMA_HAS_EASY_ENCODER 1)
241     set(LIBLZMA_HAS_LZMA_PRESET 1)
242     set(LIBLZMA_INCLUDE_DIR
243       "${CMAKE_CURRENT_SOURCE_DIR}/Utilities/cmliblzma/liblzma/api")
244     set(LIBLZMA_LIBRARY cmliblzma)
245     set(HAVE_LZMA_STREAM_ENCODER_MT 1)
246   endif()
247 endif()
248
249 #---------------------------------------------------------------------
250 # Build or use system libarchive for CMake and CTest.
251 if(CMAKE_USE_SYSTEM_LIBARCHIVE)
252   find_package(LibArchive 3.3.3)
253   if(NOT LibArchive_FOUND)
254     message(FATAL_ERROR "CMAKE_USE_SYSTEM_LIBARCHIVE is ON but LibArchive is not found!")
255   endif()
256   # NOTE `FindLibArchive` got imported targets support since 3.17
257   if (NOT TARGET LibArchive::LibArchive)
258     add_library(LibArchive::LibArchive UNKNOWN IMPORTED)
259     set_target_properties(LibArchive::LibArchive PROPERTIES
260       IMPORTED_LOCATION "${LibArchive_LIBRARIES}"
261       INTERFACE_INCLUDE_DIRECTORIES "${LibArchive_INCLUDE_DIRS}")
262   endif ()
263 else()
264   set(EXPAT_INCLUDE_DIR ${CMAKE_EXPAT_INCLUDES})
265   set(EXPAT_LIBRARY ${CMAKE_EXPAT_LIBRARIES})
266   set(ENABLE_MBEDTLS OFF)
267   set(ENABLE_NETTLE OFF)
268   if(DEFINED CMAKE_USE_OPENSSL)
269     set(ENABLE_OPENSSL "${CMAKE_USE_OPENSSL}")
270   else()
271     set(ENABLE_OPENSSL OFF)
272   endif()
273   set(ENABLE_LIBB2 OFF)
274   set(ENABLE_LZ4 OFF)
275   set(ENABLE_LZO OFF)
276   set(ENABLE_LZMA ON)
277   set(ENABLE_ZSTD ON)
278   set(ENABLE_ZLIB ON)
279   set(ENABLE_BZip2 ON)
280   set(ENABLE_LIBXML2 OFF)
281   set(ENABLE_EXPAT OFF)
282   set(ENABLE_PCREPOSIX OFF)
283   set(ENABLE_LibGCC OFF)
284   set(ENABLE_CNG OFF)
285   set(ENABLE_TAR OFF)
286   set(ENABLE_TAR_SHARED OFF)
287   set(ENABLE_CPIO OFF)
288   set(ENABLE_CPIO_SHARED OFF)
289   set(ENABLE_CAT OFF)
290   set(ENABLE_CAT_SHARED OFF)
291   set(ENABLE_XATTR OFF)
292   set(ENABLE_ACL OFF)
293   set(ENABLE_ICONV OFF)
294   set(ENABLE_TEST OFF)
295   set(ENABLE_COVERAGE OFF)
296   set(ENABLE_INSTALL OFF)
297   set(POSIX_REGEX_LIB "" CACHE INTERNAL "libarchive: No POSIX regular expression support")
298   set(ENABLE_SAFESEH "" CACHE INTERNAL "libarchive: No /SAFESEH linker flag")
299   set(WINDOWS_VERSION "WIN7" CACHE INTERNAL "libarchive: Set Windows version to use (Windows only)")
300   add_subdirectory(Utilities/cmlibarchive)
301   add_library(LibArchive::LibArchive ALIAS cmlibarchive)
302   target_compile_definitions(cmlibarchive INTERFACE LIBARCHIVE_STATIC)
303   CMAKE_SET_TARGET_FOLDER(cmlibarchive "Utilities/3rdParty")
304 endif()
305
306 #---------------------------------------------------------------------
307 # Build jsoncpp library.
308 if(CMAKE_USE_SYSTEM_JSONCPP)
309   find_package(JsonCpp 1.6.0)
310   if(NOT JsonCpp_FOUND)
311     message(FATAL_ERROR
312       "CMAKE_USE_SYSTEM_JSONCPP is ON but a JsonCpp is not found!")
313   endif()
314   if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|LCC|Clang")
315     set_property(TARGET JsonCpp::JsonCpp APPEND PROPERTY
316       INTERFACE_COMPILE_OPTIONS -Wno-deprecated-declarations)
317   endif()
318 else()
319   add_subdirectory(Utilities/cmjsoncpp)
320   add_library(JsonCpp::JsonCpp ALIAS cmjsoncpp)
321   CMAKE_SET_TARGET_FOLDER(cmjsoncpp "Utilities/3rdParty")
322 endif()
323
324 #---------------------------------------------------------------------
325 # Build libuv library.
326 if(CMAKE_USE_SYSTEM_LIBUV)
327   if(WIN32)
328     find_package(LibUV 1.38.0)
329   else()
330     find_package(LibUV 1.28.0)
331   endif()
332   if(NOT LIBUV_FOUND)
333     message(FATAL_ERROR
334       "CMAKE_USE_SYSTEM_LIBUV is ON but a libuv is not found!")
335   endif()
336 else()
337   add_subdirectory(Utilities/cmlibuv)
338   add_library(LibUV::LibUV ALIAS cmlibuv)
339   CMAKE_SET_TARGET_FOLDER(cmlibuv "Utilities/3rdParty")
340 endif()
341
342 #---------------------------------------------------------------------
343 # Use curses?
344 if(NOT DEFINED BUILD_CursesDialog)
345   if(UNIX)
346     include(${CMake_SOURCE_DIR}/Source/Checks/Curses.cmake)
347     set(BUILD_CursesDialog_DEFAULT "${CMakeCheckCurses_COMPILED}")
348   elseif(WIN32)
349     set(BUILD_CursesDialog_DEFAULT "OFF")
350   endif()
351   option(BUILD_CursesDialog "Build the CMake Curses Dialog ccmake" "${BUILD_CursesDialog_DEFAULT}")
352 endif()
353 if(BUILD_CursesDialog)
354   if(UNIX)
355     set(CURSES_NEED_NCURSES TRUE)
356     find_package(Curses)
357     if(NOT CURSES_FOUND)
358       message(WARNING
359         "'ccmake' will not be built because Curses was not found.\n"
360         "Turn off BUILD_CursesDialog to suppress this message."
361         )
362       set(BUILD_CursesDialog 0)
363     endif()
364   elseif(WIN32)
365     # FIXME: Add support for system-provided pdcurses.
366     add_subdirectory(Utilities/cmpdcurses)
367     set(CURSES_LIBRARY cmpdcurses)
368     set(CURSES_INCLUDE_PATH "") # cmpdcurses has usage requirements
369     set(CMAKE_USE_SYSTEM_FORM 0)
370     set(HAVE_CURSES_USE_DEFAULT_COLORS 1)
371   endif()
372 endif()
373 if(BUILD_CursesDialog)
374   if(NOT CMAKE_USE_SYSTEM_FORM)
375     add_subdirectory(Source/CursesDialog/form)
376   elseif(NOT CURSES_FORM_LIBRARY)
377     message(FATAL_ERROR "CMAKE_USE_SYSTEM_FORM in ON but CURSES_FORM_LIBRARY is not set!")
378   endif()
379 endif()