CMakeBuildUtilities.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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_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. # check for the use of system libraries versus builtin ones
  76. # (a macro defined in this file)
  77. CMAKE_HANDLE_SYSTEM_LIBRARIES()
  78. if(CMAKE_USE_SYSTEM_KWIML)
  79. find_package(KWIML 1.0)
  80. if(NOT KWIML_FOUND)
  81. message(FATAL_ERROR "CMAKE_USE_SYSTEM_KWIML is ON but KWIML is not found!")
  82. endif()
  83. else()
  84. if(BUILD_TESTING)
  85. set(KWIML_TEST_ENABLE 1)
  86. endif()
  87. add_subdirectory(Utilities/KWIML)
  88. endif()
  89. if(CMAKE_USE_SYSTEM_LIBRHASH)
  90. find_package(LibRHash)
  91. if(NOT LibRHash_FOUND)
  92. message(FATAL_ERROR
  93. "CMAKE_USE_SYSTEM_LIBRHASH is ON but LibRHash is not found!")
  94. endif()
  95. else()
  96. add_subdirectory(Utilities/cmlibrhash)
  97. add_library(LibRHash::LibRHash ALIAS cmlibrhash)
  98. CMAKE_SET_TARGET_FOLDER(cmlibrhash "Utilities/3rdParty")
  99. endif()
  100. #---------------------------------------------------------------------
  101. # Build zlib library for Curl, CMake, and CTest.
  102. if(CMAKE_USE_SYSTEM_ZLIB)
  103. find_package(ZLIB)
  104. if(NOT ZLIB_FOUND)
  105. message(FATAL_ERROR
  106. "CMAKE_USE_SYSTEM_ZLIB is ON but a zlib is not found!")
  107. endif()
  108. else()
  109. if(NOT POLICY CMP0102) # CMake < 3.17
  110. # Store in cache to protect from mark_as_advanced.
  111. set(ZLIB_INCLUDE_DIR ${CMake_SOURCE_DIR}/Utilities CACHE PATH "")
  112. else()
  113. set(ZLIB_INCLUDE_DIR ${CMake_SOURCE_DIR}/Utilities)
  114. endif()
  115. set(ZLIB_LIBRARY cmzlib)
  116. set(WITHOUT_ZLIB_DLL "")
  117. set(WITHOUT_ZLIB_DLL_WITH_LIB cmzlib)
  118. set(ZLIB_DLL "")
  119. set(ZLIB_DLL_WITH_LIB cmzlib)
  120. set(ZLIB_WINAPI "")
  121. set(ZLIB_WINAPI_COMPILED 0)
  122. set(ZLIB_WINAPI_WITH_LIB cmzlib)
  123. add_subdirectory(Utilities/cmzlib)
  124. add_library(ZLIB::ZLIB ALIAS cmzlib)
  125. CMAKE_SET_TARGET_FOLDER(cmzlib "Utilities/3rdParty")
  126. endif()
  127. #---------------------------------------------------------------------
  128. # Build Curl library for CTest.
  129. if(CMAKE_USE_SYSTEM_CURL)
  130. find_package(CURL)
  131. if(NOT CURL_FOUND)
  132. message(FATAL_ERROR
  133. "CMAKE_USE_SYSTEM_CURL is ON but a curl is not found!")
  134. endif()
  135. else()
  136. if(CMAKE_TESTS_CDASH_SERVER)
  137. set(CMAKE_CURL_TEST_URL "${CMAKE_TESTS_CDASH_SERVER}/user.php")
  138. endif()
  139. set(_CMAKE_USE_OPENSSL_DEFAULT OFF)
  140. if(NOT DEFINED CMAKE_USE_OPENSSL AND NOT WIN32 AND NOT APPLE
  141. AND CMAKE_SYSTEM_NAME MATCHES "(Linux|FreeBSD|CYGWIN|MSYS)")
  142. set(_CMAKE_USE_OPENSSL_DEFAULT ON)
  143. endif()
  144. option(CMAKE_USE_OPENSSL "Use OpenSSL." ${_CMAKE_USE_OPENSSL_DEFAULT})
  145. mark_as_advanced(CMAKE_USE_OPENSSL)
  146. if(CMAKE_USE_OPENSSL)
  147. set(CURL_CA_BUNDLE "" CACHE FILEPATH "Path to SSL CA Certificate Bundle")
  148. set(CURL_CA_PATH "" CACHE PATH "Path to SSL CA Certificate Directory")
  149. mark_as_advanced(CURL_CA_BUNDLE CURL_CA_PATH)
  150. find_package(OpenSSL)
  151. endif()
  152. if(NOT CMAKE_USE_SYSTEM_NGHTTP2)
  153. # Tell curl's FindNGHTTP2 module to use our library.
  154. set(NGHTTP2_LIBRARY cmnghttp2)
  155. set(NGHTTP2_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Utilities/cmnghttp2/lib/includes)
  156. endif()
  157. add_subdirectory(Utilities/cmcurl)
  158. add_library(CURL::libcurl ALIAS cmcurl)
  159. CMAKE_SET_TARGET_FOLDER(cmcurl "Utilities/3rdParty")
  160. CMAKE_SET_TARGET_FOLDER(LIBCURL "Utilities/3rdParty")
  161. if(NOT CMAKE_USE_SYSTEM_NGHTTP2)
  162. # Configure after curl to reuse some check results.
  163. add_subdirectory(Utilities/cmnghttp2)
  164. CMAKE_SET_TARGET_FOLDER(cmnghttp2 "Utilities/3rdParty")
  165. endif()
  166. endif()
  167. #---------------------------------------------------------------------
  168. # Build expat library for CMake, CTest, and libarchive.
  169. if(CMAKE_USE_SYSTEM_EXPAT)
  170. find_package(EXPAT)
  171. if(NOT EXPAT_FOUND)
  172. message(FATAL_ERROR
  173. "CMAKE_USE_SYSTEM_EXPAT is ON but a expat is not found!")
  174. endif()
  175. set(CMAKE_EXPAT_INCLUDES ${EXPAT_INCLUDE_DIRS})
  176. set(CMAKE_EXPAT_LIBRARIES ${EXPAT_LIBRARIES})
  177. else()
  178. set(CMAKE_EXPAT_INCLUDES)
  179. set(CMAKE_EXPAT_LIBRARIES cmexpat)
  180. add_subdirectory(Utilities/cmexpat)
  181. add_library(EXPAT::EXPAT ALIAS cmexpat)
  182. CMAKE_SET_TARGET_FOLDER(cmexpat "Utilities/3rdParty")
  183. endif()
  184. #---------------------------------------------------------------------
  185. # Build or use system libbz2 for libarchive.
  186. if(NOT CMAKE_USE_SYSTEM_LIBARCHIVE)
  187. if(CMAKE_USE_SYSTEM_BZIP2)
  188. find_package(BZip2)
  189. else()
  190. set(BZIP2_INCLUDE_DIR
  191. "${CMAKE_CURRENT_SOURCE_DIR}/Utilities/cmbzip2")
  192. set(BZIP2_LIBRARIES cmbzip2)
  193. set(BZIP2_NEED_PREFIX "")
  194. set(USE_BZIP2_DLL "")
  195. set(USE_BZIP2_DLL_WITH_LIB cmbzip2)
  196. set(USE_BZIP2_STATIC "")
  197. set(USE_BZIP2_STATIC_WITH_LIB cmbzip2)
  198. add_subdirectory(Utilities/cmbzip2)
  199. CMAKE_SET_TARGET_FOLDER(cmbzip2 "Utilities/3rdParty")
  200. endif()
  201. endif()
  202. #---------------------------------------------------------------------
  203. # Build or use system zstd for libarchive.
  204. if(NOT CMAKE_USE_SYSTEM_LIBARCHIVE)
  205. if(NOT CMAKE_USE_SYSTEM_ZSTD)
  206. set(ZSTD_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Utilities/cmzstd")
  207. set(ZSTD_LIBRARY cmzstd)
  208. add_subdirectory(Utilities/cmzstd)
  209. CMAKE_SET_TARGET_FOLDER(cmzstd "Utilities/3rdParty")
  210. endif()
  211. endif()
  212. #---------------------------------------------------------------------
  213. # Build or use system liblzma for libarchive.
  214. if(NOT CMAKE_USE_SYSTEM_LIBARCHIVE)
  215. if(CMAKE_USE_SYSTEM_LIBLZMA)
  216. find_package(LibLZMA)
  217. if(NOT LibLZMA_FOUND)
  218. message(FATAL_ERROR "CMAKE_USE_SYSTEM_LIBLZMA is ON but LibLZMA is not found!")
  219. endif()
  220. else()
  221. add_subdirectory(Utilities/cmliblzma)
  222. CMAKE_SET_TARGET_FOLDER(cmliblzma "Utilities/3rdParty")
  223. set(LIBLZMA_HAS_AUTO_DECODER 1)
  224. set(LIBLZMA_HAS_EASY_ENCODER 1)
  225. set(LIBLZMA_HAS_LZMA_PRESET 1)
  226. set(LIBLZMA_INCLUDE_DIR
  227. "${CMAKE_CURRENT_SOURCE_DIR}/Utilities/cmliblzma/liblzma/api")
  228. set(LIBLZMA_LIBRARY cmliblzma)
  229. set(HAVE_LZMA_STREAM_ENCODER_MT 1)
  230. endif()
  231. endif()
  232. #---------------------------------------------------------------------
  233. # Build or use system libarchive for CMake and CTest.
  234. if(CMAKE_USE_SYSTEM_LIBARCHIVE)
  235. find_package(LibArchive 3.3.3)
  236. if(NOT LibArchive_FOUND)
  237. message(FATAL_ERROR "CMAKE_USE_SYSTEM_LIBARCHIVE is ON but LibArchive is not found!")
  238. endif()
  239. # NOTE `FindLibArchive` got imported targets support since 3.17
  240. if (NOT TARGET LibArchive::LibArchive)
  241. add_library(LibArchive::LibArchive UNKNOWN IMPORTED)
  242. set_target_properties(LibArchive::LibArchive PROPERTIES
  243. IMPORTED_LOCATION "${LibArchive_LIBRARIES}"
  244. INTERFACE_INCLUDE_DIRECTORIES "${LibArchive_INCLUDE_DIRS}")
  245. endif ()
  246. else()
  247. set(DONT_FAIL_ON_CRC_ERROR OFF)
  248. set(EXPAT_INCLUDE_DIR ${CMAKE_EXPAT_INCLUDES})
  249. set(EXPAT_LIBRARY ${CMAKE_EXPAT_LIBRARIES})
  250. set(ENABLE_MBEDTLS OFF)
  251. set(ENABLE_NETTLE OFF)
  252. if(DEFINED CMAKE_USE_OPENSSL)
  253. set(ENABLE_OPENSSL "${CMAKE_USE_OPENSSL}")
  254. else()
  255. set(ENABLE_OPENSSL OFF)
  256. endif()
  257. set(ENABLE_LIBB2 OFF)
  258. set(ENABLE_LZ4 OFF)
  259. set(ENABLE_LZO OFF)
  260. set(ENABLE_LZMA ON)
  261. set(ENABLE_ZSTD ON)
  262. set(ENABLE_ZLIB ON)
  263. set(ENABLE_BZip2 ON)
  264. set(ENABLE_LIBXML2 OFF)
  265. set(ENABLE_EXPAT OFF)
  266. set(ENABLE_PCREPOSIX OFF)
  267. set(ENABLE_PCRE2POSIX OFF)
  268. set(ENABLE_LIBGCC OFF)
  269. set(ENABLE_CNG OFF)
  270. set(ENABLE_TAR OFF)
  271. set(ENABLE_TAR_SHARED OFF)
  272. set(ENABLE_CPIO OFF)
  273. set(ENABLE_CPIO_SHARED OFF)
  274. set(ENABLE_CAT OFF)
  275. set(ENABLE_CAT_SHARED OFF)
  276. set(ENABLE_UNZIP OFF)
  277. set(ENABLE_UNZIP_SHARED OFF)
  278. set(ENABLE_WIN32_XMLLITE 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. else()
  323. add_subdirectory(Utilities/cmlibuv)
  324. add_library(LibUV::LibUV ALIAS cmlibuv)
  325. CMAKE_SET_TARGET_FOLDER(cmlibuv "Utilities/3rdParty")
  326. endif()
  327. #---------------------------------------------------------------------
  328. # Use curses?
  329. if(NOT DEFINED BUILD_CursesDialog)
  330. if(UNIX)
  331. include(${CMake_SOURCE_DIR}/Source/Checks/Curses.cmake)
  332. set(BUILD_CursesDialog_DEFAULT "${CMakeCheckCurses_COMPILED}")
  333. elseif(WIN32)
  334. set(BUILD_CursesDialog_DEFAULT "OFF")
  335. endif()
  336. option(BUILD_CursesDialog "Build the CMake Curses Dialog ccmake" "${BUILD_CursesDialog_DEFAULT}")
  337. endif()
  338. if(BUILD_CursesDialog)
  339. if(UNIX)
  340. set(CURSES_NEED_NCURSES TRUE)
  341. find_package(Curses)
  342. if(NOT Curses_FOUND)
  343. message(WARNING
  344. "'ccmake' will not be built because Curses was not found.\n"
  345. "Turn off BUILD_CursesDialog to suppress this message."
  346. )
  347. set(BUILD_CursesDialog 0)
  348. endif()
  349. elseif(WIN32)
  350. # FIXME: Add support for system-provided pdcurses.
  351. add_subdirectory(Utilities/cmpdcurses)
  352. set(CURSES_LIBRARY cmpdcurses)
  353. set(CURSES_INCLUDE_PATH "") # cmpdcurses has usage requirements
  354. set(CMAKE_USE_SYSTEM_FORM 0)
  355. set(HAVE_CURSES_USE_DEFAULT_COLORS 1)
  356. endif()
  357. endif()
  358. if(BUILD_CursesDialog)
  359. if(NOT CMAKE_USE_SYSTEM_FORM)
  360. add_subdirectory(Source/CursesDialog/form)
  361. elseif(NOT CURSES_FORM_LIBRARY)
  362. message(FATAL_ERROR "CMAKE_USE_SYSTEM_FORM in ON but CURSES_FORM_LIBRARY is not set!")
  363. endif()
  364. endif()
  365. #---------------------------------------------------------------------
  366. # Build cppdap library.
  367. if(CMake_ENABLE_DEBUGGER)
  368. if(CMAKE_USE_SYSTEM_CPPDAP)
  369. find_package(cppdap CONFIG)
  370. if(NOT cppdap_FOUND)
  371. message(FATAL_ERROR
  372. "CMAKE_USE_SYSTEM_CPPDAP is ON but a cppdap is not found!")
  373. endif()
  374. else()
  375. add_subdirectory(Utilities/cmcppdap)
  376. add_library(cppdap::cppdap ALIAS cmcppdap)
  377. CMAKE_SET_TARGET_FOLDER(cppdap "Utilities/3rdParty")
  378. endif()
  379. endif()
  380. #---------------------------------------------------------------------
  381. # Build llpkgc library.
  382. add_subdirectory(Utilities/cmllpkgc)
  383. add_library(llpkgc::llpkgc ALIAS cmllpkgc)
  384. CMAKE_SET_TARGET_FOLDER(cmllpkgc "Utilities/3rdParty")