CMakeBuildUtilities.cmake 14 KB

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