summaryrefslogtreecommitdiff
path: root/Source/Modules/CMakeBuildUtilities.cmake
blob: dad4bc1e454c6df2479e5e9695698d253d646ce6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#-----------------------------------------------------------------------
# Build the utilities used by CMake
#
# Originally it was a macro in the root `CMakeLists.txt` with the comment
# "Simply to improve readability...".
# However, as part of the modernization refactoring it was moved into a
# separate file cuz adding library alises wasn't possible inside the
# macro.
#-----------------------------------------------------------------------

# Suppress unnecessary checks in third-party code.
include(Utilities/cmThirdPartyChecks.cmake)

#---------------------------------------------------------------------
# Create the kwsys library for CMake.
set(KWSYS_NAMESPACE cmsys)
set(KWSYS_USE_SystemTools 1)
set(KWSYS_USE_Directory 1)
set(KWSYS_USE_RegularExpression 1)
set(KWSYS_USE_Base64 1)
set(KWSYS_USE_MD5 1)
set(KWSYS_USE_Process 1)
set(KWSYS_USE_CommandLineArguments 1)
set(KWSYS_USE_ConsoleBuf 1)
set(KWSYS_HEADER_ROOT ${CMake_BINARY_DIR}/Source)
set(KWSYS_INSTALL_DOC_DIR "${CMAKE_DOC_DIR}")
if(CMake_NO_CXX_STANDARD)
  set(KWSYS_CXX_STANDARD "")
endif()
if(CMake_NO_SELF_BACKTRACE)
  set(KWSYS_NO_EXECINFO 1)
endif()
if(WIN32)
  # FIXME: Teach KWSys to hard-code these checks on Windows.
  set(KWSYS_C_HAS_CLOCK_GETTIME_MONOTONIC_COMPILED 0)
  set(KWSYS_C_HAS_PTRDIFF_T_COMPILED 1)
  set(KWSYS_CXX_HAS_ENVIRON_IN_STDLIB_H_COMPILED 1)
  set(KWSYS_CXX_HAS_RLIMIT64_COMPILED 0)
  set(KWSYS_CXX_HAS_SETENV_COMPILED 0)
  set(KWSYS_CXX_HAS_UNSETENV_COMPILED 0)
  set(KWSYS_CXX_HAS_UTIMENSAT_COMPILED 0)
  set(KWSYS_CXX_HAS_UTIMES_COMPILED 0)
  set(KWSYS_CXX_STAT_HAS_ST_MTIM_COMPILED 0)
  set(KWSYS_CXX_STAT_HAS_ST_MTIMESPEC_COMPILED 0)
  set(KWSYS_STL_HAS_WSTRING_COMPILED 1)
  set(KWSYS_SYS_HAS_IFADDRS_H 0)
endif()
add_subdirectory(Source/kwsys)
set(kwsys_folder "Utilities/KWSys")
CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE} "${kwsys_folder}")
CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}_c "${kwsys_folder}")
if(BUILD_TESTING)
  CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}TestDynload "${kwsys_folder}")
  CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}TestProcess "${kwsys_folder}")
  CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}TestsC "${kwsys_folder}")
  CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}TestsCxx "${kwsys_folder}")
  CMAKE_SET_TARGET_FOLDER(${KWSYS_NAMESPACE}TestSharedForward "${kwsys_folder}")
endif()

#---------------------------------------------------------------------
# Setup third-party libraries.
# Everything in the tree should be able to include files from the
# Utilities directory.
if((CMAKE_SYSTEM_NAME STREQUAL "AIX" OR CMAKE_SYSTEM_NAME STREQUAL "OS400") AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  # using -isystem option generate error "template with C linkage"
  include_directories("${CMake_SOURCE_DIR}/Utilities/std")
else()
  include_directories(SYSTEM "${CMake_SOURCE_DIR}/Utilities/std")
endif()

include_directories("${CMake_BINARY_DIR}/Utilities")
if((CMAKE_SYSTEM_NAME STREQUAL "AIX" OR CMAKE_SYSTEM_NAME STREQUAL "OS400") AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  # using -isystem option generate error "template with C linkage"
  include_directories("${CMake_SOURCE_DIR}/Utilities")
else()
  include_directories(SYSTEM "${CMake_SOURCE_DIR}/Utilities")
endif()

#---------------------------------------------------------------------
# Build CMake std library for CMake and CTest.
add_subdirectory(Utilities/std)
CMAKE_SET_TARGET_FOLDER(cmstd "Utilities/std")

# check for the use of system libraries versus builtin ones
# (a macro defined in this file)
CMAKE_HANDLE_SYSTEM_LIBRARIES()

if(CMAKE_USE_SYSTEM_KWIML)
  find_package(KWIML 1.0)
  if(NOT KWIML_FOUND)
    message(FATAL_ERROR "CMAKE_USE_SYSTEM_KWIML is ON but KWIML is not found!")
  endif()
  set(CMake_KWIML_LIBRARIES kwiml::kwiml)
else()
  set(CMake_KWIML_LIBRARIES "")
  if(BUILD_TESTING)
    set(KWIML_TEST_ENABLE 1)
  endif()
  add_subdirectory(Utilities/KWIML)
endif()

if(CMAKE_USE_SYSTEM_LIBRHASH)
  find_package(LibRHash)
  if(NOT LibRHash_FOUND)
    message(FATAL_ERROR
      "CMAKE_USE_SYSTEM_LIBRHASH is ON but LibRHash is not found!")
  endif()
  set(CMAKE_LIBRHASH_LIBRARIES LibRHash::LibRHash)
else()
  set(CMAKE_LIBRHASH_LIBRARIES cmlibrhash)
  add_subdirectory(Utilities/cmlibrhash)
  CMAKE_SET_TARGET_FOLDER(cmlibrhash "Utilities/3rdParty")
endif()

#---------------------------------------------------------------------
# Build zlib library for Curl, CMake, and CTest.
if(CMAKE_USE_SYSTEM_ZLIB)
  find_package(ZLIB)
  if(NOT ZLIB_FOUND)
    message(FATAL_ERROR
      "CMAKE_USE_SYSTEM_ZLIB is ON but a zlib is not found!")
  endif()
else()
  if(NOT POLICY CMP0102) # CMake < 3.17
    # Store in cache to protect from mark_as_advanced.
    set(ZLIB_INCLUDE_DIR ${CMake_SOURCE_DIR}/Utilities CACHE PATH "")
  else()
    set(ZLIB_INCLUDE_DIR ${CMake_SOURCE_DIR}/Utilities)
  endif()
  set(ZLIB_LIBRARY cmzlib)
  set(WITHOUT_ZLIB_DLL "")
  set(WITHOUT_ZLIB_DLL_WITH_LIB cmzlib)
  set(ZLIB_DLL "")
  set(ZLIB_DLL_WITH_LIB cmzlib)
  set(ZLIB_WINAPI "")
  set(ZLIB_WINAPI_COMPILED 0)
  set(ZLIB_WINAPI_WITH_LIB cmzlib)
  add_subdirectory(Utilities/cmzlib)
  add_library(ZLIB::ZLIB ALIAS cmzlib)
  CMAKE_SET_TARGET_FOLDER(cmzlib "Utilities/3rdParty")
endif()

#---------------------------------------------------------------------
# Build Curl library for CTest.
if(CMAKE_USE_SYSTEM_CURL)
  find_package(CURL)
  if(NOT CURL_FOUND)
    message(FATAL_ERROR
      "CMAKE_USE_SYSTEM_CURL is ON but a curl is not found!")
  endif()
  set(CMAKE_CURL_INCLUDES ${CURL_INCLUDE_DIRS})
  set(CMAKE_CURL_LIBRARIES ${CURL_LIBRARIES})
else()
  set(CMAKE_CURL_INCLUDES)
  set(CMAKE_CURL_LIBRARIES cmcurl)
  if(CMAKE_TESTS_CDASH_SERVER)
    set(CMAKE_CURL_TEST_URL "${CMAKE_TESTS_CDASH_SERVER}/user.php")
  endif()
  set(_CMAKE_USE_OPENSSL_DEFAULT OFF)
  if(NOT DEFINED CMAKE_USE_OPENSSL AND NOT WIN32 AND NOT APPLE
      AND CMAKE_SYSTEM_NAME MATCHES "(Linux|FreeBSD)")
    set(_CMAKE_USE_OPENSSL_DEFAULT ON)
  endif()
  option(CMAKE_USE_OPENSSL "Use OpenSSL." ${_CMAKE_USE_OPENSSL_DEFAULT})
  mark_as_advanced(CMAKE_USE_OPENSSL)
  if(CMAKE_USE_OPENSSL)
    set(CURL_CA_BUNDLE "" CACHE FILEPATH "Path to SSL CA Certificate Bundle")
    set(CURL_CA_PATH "" CACHE PATH "Path to SSL CA Certificate Directory")
    mark_as_advanced(CURL_CA_BUNDLE CURL_CA_PATH)
  endif()
  if(NOT CMAKE_USE_SYSTEM_NGHTTP2)
    # Tell curl's FindNGHTTP2 module to use our library.
    set(NGHTTP2_LIBRARY cmnghttp2)
    set(NGHTTP2_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Utilities/cmnghttp2/lib/includes)
  endif()
  add_subdirectory(Utilities/cmcurl)
  CMAKE_SET_TARGET_FOLDER(cmcurl "Utilities/3rdParty")
  CMAKE_SET_TARGET_FOLDER(LIBCURL "Utilities/3rdParty")
  if(NOT CMAKE_USE_SYSTEM_NGHTTP2)
    # Configure after curl to re-use some check results.
    add_subdirectory(Utilities/cmnghttp2)
    CMAKE_SET_TARGET_FOLDER(cmnghttp2 "Utilities/3rdParty")
  endif()
endif()

#---------------------------------------------------------------------
# Build expat library for CMake, CTest, and libarchive.
if(CMAKE_USE_SYSTEM_EXPAT)
  find_package(EXPAT)
  if(NOT EXPAT_FOUND)
    message(FATAL_ERROR
      "CMAKE_USE_SYSTEM_EXPAT is ON but a expat is not found!")
  endif()
  set(CMAKE_EXPAT_INCLUDES ${EXPAT_INCLUDE_DIRS})
  set(CMAKE_EXPAT_LIBRARIES ${EXPAT_LIBRARIES})
else()
  set(CMAKE_EXPAT_INCLUDES)
  set(CMAKE_EXPAT_LIBRARIES cmexpat)
  add_subdirectory(Utilities/cmexpat)
  add_library(EXPAT::EXPAT ALIAS cmexpat)
  CMAKE_SET_TARGET_FOLDER(cmexpat "Utilities/3rdParty")
endif()

#---------------------------------------------------------------------
# Build or use system libbz2 for libarchive.
if(NOT CMAKE_USE_SYSTEM_LIBARCHIVE)
  if(CMAKE_USE_SYSTEM_BZIP2)
    find_package(BZip2)
  else()
    set(BZIP2_INCLUDE_DIR
      "${CMAKE_CURRENT_SOURCE_DIR}/Utilities/cmbzip2")
    set(BZIP2_LIBRARIES cmbzip2)
    set(BZIP2_NEED_PREFIX "")
    set(USE_BZIP2_DLL "")
    set(USE_BZIP2_DLL_WITH_LIB cmbzip2)
    set(USE_BZIP2_STATIC "")
    set(USE_BZIP2_STATIC_WITH_LIB cmbzip2)
    add_subdirectory(Utilities/cmbzip2)
    CMAKE_SET_TARGET_FOLDER(cmbzip2 "Utilities/3rdParty")
  endif()
endif()

#---------------------------------------------------------------------
# Build or use system zstd for libarchive.
if(NOT CMAKE_USE_SYSTEM_LIBARCHIVE)
  if(NOT CMAKE_USE_SYSTEM_ZSTD)
    set(ZSTD_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Utilities/cmzstd")
    set(ZSTD_LIBRARY cmzstd)
    add_subdirectory(Utilities/cmzstd)
    CMAKE_SET_TARGET_FOLDER(cmzstd "Utilities/3rdParty")
  endif()
endif()

#---------------------------------------------------------------------
# Build or use system liblzma for libarchive.
if(NOT CMAKE_USE_SYSTEM_LIBARCHIVE)
  if(CMAKE_USE_SYSTEM_LIBLZMA)
    find_package(LibLZMA)
    if(NOT LIBLZMA_FOUND)
      message(FATAL_ERROR "CMAKE_USE_SYSTEM_LIBLZMA is ON but LibLZMA is not found!")
    endif()
  else()
    add_subdirectory(Utilities/cmliblzma)
    CMAKE_SET_TARGET_FOLDER(cmliblzma "Utilities/3rdParty")
    set(LIBLZMA_HAS_AUTO_DECODER 1)
    set(LIBLZMA_HAS_EASY_ENCODER 1)
    set(LIBLZMA_HAS_LZMA_PRESET 1)
    set(LIBLZMA_INCLUDE_DIR
      "${CMAKE_CURRENT_SOURCE_DIR}/Utilities/cmliblzma/liblzma/api")
    set(LIBLZMA_LIBRARY cmliblzma)
    set(HAVE_LZMA_STREAM_ENCODER_MT 1)
  endif()
endif()

#---------------------------------------------------------------------
# Build or use system libarchive for CMake and CTest.
if(CMAKE_USE_SYSTEM_LIBARCHIVE)
  find_package(LibArchive 3.3.3)
  if(NOT LibArchive_FOUND)
    message(FATAL_ERROR "CMAKE_USE_SYSTEM_LIBARCHIVE is ON but LibArchive is not found!")
  endif()
  set(CMAKE_TAR_INCLUDES ${LibArchive_INCLUDE_DIRS})
  set(CMAKE_TAR_LIBRARIES ${LibArchive_LIBRARIES})
else()
  set(EXPAT_INCLUDE_DIR ${CMAKE_EXPAT_INCLUDES})
  set(EXPAT_LIBRARY ${CMAKE_EXPAT_LIBRARIES})
  add_compile_definitions(LIBARCHIVE_STATIC)
  set(ENABLE_MBEDTLS OFF)
  set(ENABLE_NETTLE OFF)
  if(DEFINED CMAKE_USE_OPENSSL)
    set(ENABLE_OPENSSL "${CMAKE_USE_OPENSSL}")
  else()
    set(ENABLE_OPENSSL OFF)
  endif()
  set(ENABLE_LIBB2 OFF)
  set(ENABLE_LZ4 OFF)
  set(ENABLE_LZO OFF)
  set(ENABLE_LZMA ON)
  set(ENABLE_ZSTD ON)
  set(ENABLE_ZLIB ON)
  set(ENABLE_BZip2 ON)
  set(ENABLE_LIBXML2 OFF)
  set(ENABLE_EXPAT OFF)
  set(ENABLE_PCREPOSIX OFF)
  set(ENABLE_LibGCC OFF)
  set(ENABLE_CNG OFF)
  set(ENABLE_TAR OFF)
  set(ENABLE_TAR_SHARED OFF)
  set(ENABLE_CPIO OFF)
  set(ENABLE_CPIO_SHARED OFF)
  set(ENABLE_CAT OFF)
  set(ENABLE_CAT_SHARED OFF)
  set(ENABLE_XATTR OFF)
  set(ENABLE_ACL OFF)
  set(ENABLE_ICONV OFF)
  set(ENABLE_TEST OFF)
  set(ENABLE_COVERAGE OFF)
  set(ENABLE_INSTALL OFF)
  set(POSIX_REGEX_LIB "" CACHE INTERNAL "libarchive: No POSIX regular expression support")
  set(ENABLE_SAFESEH "" CACHE INTERNAL "libarchive: No /SAFESEH linker flag")
  set(WINDOWS_VERSION "WIN7" CACHE INTERNAL "libarchive: Set Windows version to use (Windows only)")
  add_subdirectory(Utilities/cmlibarchive)
  CMAKE_SET_TARGET_FOLDER(cmlibarchive "Utilities/3rdParty")
  set(CMAKE_TAR_LIBRARIES cmlibarchive ${BZIP2_LIBRARIES})
endif()

#---------------------------------------------------------------------
# Build jsoncpp library.
if(CMAKE_USE_SYSTEM_JSONCPP)
  find_package(JsonCpp 1.6.0)
  if(NOT JsonCpp_FOUND)
    message(FATAL_ERROR
      "CMAKE_USE_SYSTEM_JSONCPP is ON but a JsonCpp is not found!")
  endif()
  if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|LCC|Clang")
    set_property(TARGET JsonCpp::JsonCpp APPEND PROPERTY
      INTERFACE_COMPILE_OPTIONS -Wno-deprecated-declarations)
  endif()
  set(CMAKE_JSONCPP_LIBRARIES JsonCpp::JsonCpp)
else()
  set(CMAKE_JSONCPP_LIBRARIES cmjsoncpp)
  add_subdirectory(Utilities/cmjsoncpp)
  CMAKE_SET_TARGET_FOLDER(cmjsoncpp "Utilities/3rdParty")
endif()

#---------------------------------------------------------------------
# Build libuv library.
if(CMAKE_USE_SYSTEM_LIBUV)
  if(WIN32)
    find_package(LibUV 1.38.0)
  else()
    find_package(LibUV 1.28.0)
  endif()
  if(NOT LIBUV_FOUND)
    message(FATAL_ERROR
      "CMAKE_USE_SYSTEM_LIBUV is ON but a libuv is not found!")
  endif()
  set(CMAKE_LIBUV_LIBRARIES LibUV::LibUV)
else()
  set(CMAKE_LIBUV_LIBRARIES cmlibuv)
  add_subdirectory(Utilities/cmlibuv)
  CMAKE_SET_TARGET_FOLDER(cmlibuv "Utilities/3rdParty")
endif()

#---------------------------------------------------------------------
# Use curses?
if(NOT DEFINED BUILD_CursesDialog)
  if(UNIX)
    include(${CMake_SOURCE_DIR}/Source/Checks/Curses.cmake)
    set(BUILD_CursesDialog_DEFAULT "${CMakeCheckCurses_COMPILED}")
  elseif(WIN32)
    set(BUILD_CursesDialog_DEFAULT "OFF")
  endif()
  option(BUILD_CursesDialog "Build the CMake Curses Dialog ccmake" "${BUILD_CursesDialog_DEFAULT}")
endif()
if(BUILD_CursesDialog)
  if(UNIX)
    set(CURSES_NEED_NCURSES TRUE)
    find_package(Curses)
    if(NOT CURSES_FOUND)
      message(WARNING
        "'ccmake' will not be built because Curses was not found.\n"
        "Turn off BUILD_CursesDialog to suppress this message."
        )
      set(BUILD_CursesDialog 0)
    endif()
  elseif(WIN32)
    # FIXME: Add support for system-provided pdcurses.
    add_subdirectory(Utilities/cmpdcurses)
    set(CURSES_LIBRARY cmpdcurses)
    set(CURSES_INCLUDE_PATH "") # cmpdcurses has usage requirements
    set(CMAKE_USE_SYSTEM_FORM 0)
    set(HAVE_CURSES_USE_DEFAULT_COLORS 1)
  endif()
endif()
if(BUILD_CursesDialog)
  if(NOT CMAKE_USE_SYSTEM_FORM)
    add_subdirectory(Source/CursesDialog/form)
  elseif(NOT CURSES_FORM_LIBRARY)
    message(FATAL_ERROR "CMAKE_USE_SYSTEM_FORM in ON but CURSES_FORM_LIBRARY is not set!")
  endif()
endif()