CMakeBuildUtilities.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. 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. else()
  244. set(DONT_FAIL_ON_CRC_ERROR OFF)
  245. set(EXPAT_INCLUDE_DIR ${CMAKE_EXPAT_INCLUDES})
  246. set(EXPAT_LIBRARY ${CMAKE_EXPAT_LIBRARIES})
  247. set(ENABLE_MBEDTLS OFF)
  248. set(ENABLE_NETTLE OFF)
  249. if(DEFINED CMAKE_USE_OPENSSL)
  250. set(ENABLE_OPENSSL "${CMAKE_USE_OPENSSL}")
  251. else()
  252. set(ENABLE_OPENSSL OFF)
  253. endif()
  254. set(ENABLE_LIBB2 OFF)
  255. set(ENABLE_LZ4 OFF)
  256. set(ENABLE_LZO OFF)
  257. set(ENABLE_LZMA ON)
  258. set(ENABLE_ZSTD ON)
  259. set(ENABLE_ZLIB ON)
  260. set(ENABLE_BZip2 ON)
  261. set(ENABLE_LIBXML2 OFF)
  262. set(ENABLE_EXPAT OFF)
  263. set(ENABLE_PCREPOSIX OFF)
  264. set(ENABLE_PCRE2POSIX OFF)
  265. set(ENABLE_LIBGCC OFF)
  266. set(ENABLE_CNG OFF)
  267. set(ENABLE_TAR OFF)
  268. set(ENABLE_TAR_SHARED OFF)
  269. set(ENABLE_CPIO OFF)
  270. set(ENABLE_CPIO_SHARED OFF)
  271. set(ENABLE_CAT OFF)
  272. set(ENABLE_CAT_SHARED OFF)
  273. set(ENABLE_UNZIP OFF)
  274. set(ENABLE_UNZIP_SHARED OFF)
  275. set(ENABLE_WIN32_XMLLITE OFF)
  276. set(ENABLE_XATTR OFF)
  277. set(ENABLE_ACL OFF)
  278. set(ENABLE_ICONV OFF)
  279. set(ENABLE_TEST OFF)
  280. set(ENABLE_COVERAGE OFF)
  281. set(ENABLE_INSTALL OFF)
  282. set(POSIX_REGEX_LIB "" CACHE INTERNAL "libarchive: No POSIX regular expression support")
  283. set(ENABLE_SAFESEH "" CACHE INTERNAL "libarchive: No /SAFESEH linker flag")
  284. set(WINDOWS_VERSION "WIN7" CACHE INTERNAL "libarchive: Set Windows version to use (Windows only)")
  285. add_subdirectory(Utilities/cmlibarchive)
  286. add_library(LibArchive::LibArchive ALIAS cmlibarchive)
  287. target_compile_definitions(cmlibarchive INTERFACE LIBARCHIVE_STATIC)
  288. CMAKE_SET_TARGET_FOLDER(cmlibarchive "Utilities/3rdParty")
  289. endif()
  290. #---------------------------------------------------------------------
  291. # Build jsoncpp library.
  292. if(CMAKE_USE_SYSTEM_JSONCPP)
  293. find_package(JsonCpp 1.6.0)
  294. if(NOT JsonCpp_FOUND)
  295. message(FATAL_ERROR
  296. "CMAKE_USE_SYSTEM_JSONCPP is ON but a JsonCpp is not found!")
  297. endif()
  298. if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|LCC|Clang")
  299. set_property(TARGET JsonCpp::JsonCpp APPEND PROPERTY
  300. INTERFACE_COMPILE_OPTIONS -Wno-deprecated-declarations)
  301. endif()
  302. else()
  303. add_subdirectory(Utilities/cmjsoncpp)
  304. add_library(JsonCpp::JsonCpp ALIAS cmjsoncpp)
  305. CMAKE_SET_TARGET_FOLDER(cmjsoncpp "Utilities/3rdParty")
  306. endif()
  307. #---------------------------------------------------------------------
  308. # Build libuv library.
  309. if(CMAKE_USE_SYSTEM_LIBUV)
  310. if(WIN32)
  311. find_package(LibUV 1.38.0)
  312. else()
  313. find_package(LibUV 1.28.0)
  314. endif()
  315. if(NOT LIBUV_FOUND)
  316. message(FATAL_ERROR
  317. "CMAKE_USE_SYSTEM_LIBUV is ON but a libuv is not found!")
  318. endif()
  319. else()
  320. add_subdirectory(Utilities/cmlibuv)
  321. add_library(LibUV::LibUV ALIAS cmlibuv)
  322. CMAKE_SET_TARGET_FOLDER(cmlibuv "Utilities/3rdParty")
  323. endif()
  324. #---------------------------------------------------------------------
  325. # Use curses?
  326. if(NOT DEFINED BUILD_CursesDialog)
  327. if(UNIX)
  328. include(${CMake_SOURCE_DIR}/Source/Checks/Curses.cmake)
  329. set(BUILD_CursesDialog_DEFAULT "${CMakeCheckCurses_COMPILED}")
  330. elseif(WIN32)
  331. set(BUILD_CursesDialog_DEFAULT "OFF")
  332. endif()
  333. option(BUILD_CursesDialog "Build the CMake Curses Dialog ccmake" "${BUILD_CursesDialog_DEFAULT}")
  334. endif()
  335. if(BUILD_CursesDialog)
  336. if(UNIX)
  337. set(CURSES_NEED_NCURSES TRUE)
  338. find_package(Curses)
  339. if(NOT Curses_FOUND)
  340. message(WARNING
  341. "'ccmake' will not be built because Curses was not found.\n"
  342. "Turn off BUILD_CursesDialog to suppress this message."
  343. )
  344. set(BUILD_CursesDialog 0)
  345. endif()
  346. elseif(WIN32)
  347. # FIXME: Add support for system-provided pdcurses.
  348. add_subdirectory(Utilities/cmpdcurses)
  349. set(CURSES_LIBRARY cmpdcurses)
  350. set(CURSES_INCLUDE_PATH "") # cmpdcurses has usage requirements
  351. set(CMAKE_USE_SYSTEM_FORM 0)
  352. set(HAVE_CURSES_USE_DEFAULT_COLORS 1)
  353. endif()
  354. endif()
  355. if(BUILD_CursesDialog)
  356. if(NOT CMAKE_USE_SYSTEM_FORM)
  357. add_subdirectory(Source/CursesDialog/form)
  358. elseif(NOT CURSES_FORM_LIBRARY)
  359. message(FATAL_ERROR "CMAKE_USE_SYSTEM_FORM in ON but CURSES_FORM_LIBRARY is not set!")
  360. endif()
  361. endif()
  362. #---------------------------------------------------------------------
  363. # Build cppdap library.
  364. if(CMake_ENABLE_DEBUGGER)
  365. if(CMAKE_USE_SYSTEM_CPPDAP)
  366. find_package(cppdap CONFIG)
  367. if(NOT cppdap_FOUND)
  368. message(FATAL_ERROR
  369. "CMAKE_USE_SYSTEM_CPPDAP is ON but a cppdap is not found!")
  370. endif()
  371. else()
  372. add_subdirectory(Utilities/cmcppdap)
  373. add_library(cppdap::cppdap ALIAS cmcppdap)
  374. CMAKE_SET_TARGET_FOLDER(cppdap "Utilities/3rdParty")
  375. endif()
  376. endif()
  377. #---------------------------------------------------------------------
  378. # Build llpkgc library.
  379. add_subdirectory(Utilities/cmllpkgc)
  380. add_library(llpkgc::llpkgc ALIAS cmllpkgc)
  381. CMAKE_SET_TARGET_FOLDER(cmllpkgc "Utilities/3rdParty")