CMakeBuildUtilities.cmake 13 KB

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