CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. if(NOT CMake_SOURCE_DIR)
  4. set(CMakeHelp_STANDALONE 1)
  5. cmake_minimum_required(VERSION 3.13...3.26 FATAL_ERROR)
  6. get_filename_component(tmp "${CMAKE_CURRENT_SOURCE_DIR}" PATH)
  7. get_filename_component(CMake_SOURCE_DIR "${tmp}" PATH)
  8. include(${CMake_SOURCE_DIR}/Modules/CTestUseLaunchers.cmake)
  9. include(${CMake_SOURCE_DIR}/Source/CMakeVersion.cmake)
  10. include(${CMake_SOURCE_DIR}/Source/CMakeInstallDestinations.cmake)
  11. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/CTestCustom.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/CTestCustom.cmake @ONLY)
  12. unset(CMAKE_DATA_DIR)
  13. unset(CMAKE_DATA_DIR CACHE)
  14. macro(CMake_OPTIONAL_COMPONENT)
  15. set(COMPONENT "")
  16. endmacro()
  17. endif()
  18. project(CMakeHelp NONE)
  19. option(SPHINX_INFO "Build Info manual with Sphinx" OFF)
  20. option(SPHINX_MAN "Build man pages with Sphinx" OFF)
  21. option(SPHINX_HTML "Build html help with Sphinx" OFF)
  22. option(SPHINX_SINGLEHTML "Build html single page help with Sphinx" OFF)
  23. option(SPHINX_LINKCHECK "Check external links mentioned in documentation" OFF)
  24. option(SPHINX_QTHELP "Build Qt help with Sphinx" OFF)
  25. option(SPHINX_LATEXPDF "Build PDF help with Sphinx using LaTeX" OFF)
  26. option(SPHINX_TEXT "Build text help with Sphinx (not installed)" OFF)
  27. find_program(SPHINX_EXECUTABLE
  28. NAMES sphinx-build
  29. DOC "Sphinx Documentation Builder (sphinx-doc.org)"
  30. )
  31. set(SPHINX_FLAGS "" CACHE STRING "Flags to pass to sphinx-build")
  32. separate_arguments(sphinx_flags UNIX_COMMAND "${SPHINX_FLAGS}")
  33. mark_as_advanced(SPHINX_TEXT)
  34. mark_as_advanced(SPHINX_FLAGS)
  35. if(NOT (SPHINX_INFO
  36. OR SPHINX_MAN
  37. OR SPHINX_HTML
  38. OR SPHINX_SINGLEHTML
  39. OR SPHINX_LINKCHECK
  40. OR SPHINX_QTHELP
  41. OR SPHINX_TEXT
  42. OR SPHINX_LATEXPDF
  43. ))
  44. return()
  45. elseif(NOT SPHINX_EXECUTABLE)
  46. message(FATAL_ERROR "SPHINX_EXECUTABLE (sphinx-build) is not found!")
  47. endif()
  48. set(copyright_line_regex "^Copyright (2000-20[0-9][0-9] Kitware.*)")
  49. file(STRINGS "${CMake_SOURCE_DIR}/Copyright.txt" copyright_line
  50. LIMIT_COUNT 1 REGEX "${copyright_line_regex}")
  51. if(copyright_line MATCHES "${copyright_line_regex}")
  52. set(conf_copyright "${CMAKE_MATCH_1}")
  53. else()
  54. set(conf_copyright "Kitware, Inc.")
  55. endif()
  56. if(CMake_SPHINX_CMAKE_ORG)
  57. set(conf_baseurl "https://cmake.org/cmake/help/latest")
  58. else()
  59. set(conf_baseurl "")
  60. endif()
  61. set(conf_docs "${CMake_SOURCE_DIR}/Help")
  62. set(conf_path "${CMAKE_CURRENT_SOURCE_DIR}")
  63. set(conf_version "${CMake_VERSION_MAJOR}.${CMake_VERSION_MINOR}.${CMake_VERSION_PATCH}")
  64. set(conf_release "${CMake_VERSION}")
  65. configure_file(conf.py.in conf.py @ONLY)
  66. set(doc_formats "")
  67. if(SPHINX_HTML)
  68. list(APPEND doc_formats html)
  69. # we provide the path to the produced html output in the console
  70. # for tools that support URI protocol schemes
  71. set(html_post_commands
  72. COMMAND ${CMAKE_COMMAND} -E echo "sphinx-build html: HTML documentation generated in file://${CMAKE_CURRENT_BINARY_DIR}/html/index.html"
  73. )
  74. endif()
  75. if(SPHINX_MAN)
  76. list(APPEND doc_formats man)
  77. endif()
  78. if(SPHINX_SINGLEHTML)
  79. list(APPEND doc_formats singlehtml)
  80. endif()
  81. if(SPHINX_LINKCHECK)
  82. list(APPEND doc_formats linkcheck)
  83. #
  84. set(linkcheck_post_commands
  85. COMMAND ${CMAKE_COMMAND} -E echo "sphinx-build linkcheck: see checking status in file://${CMAKE_CURRENT_BINARY_DIR}/linkcheck/output.txt"
  86. )
  87. endif()
  88. if(SPHINX_TEXT)
  89. list(APPEND doc_formats text)
  90. endif()
  91. if(SPHINX_INFO)
  92. find_program(MAKEINFO_EXECUTABLE
  93. NAMES makeinfo
  94. DOC "makeinfo tool"
  95. )
  96. if (NOT MAKEINFO_EXECUTABLE)
  97. message(FATAL_ERROR "MAKEINFO_EXECUTABLE (makeinfo) not found!")
  98. endif()
  99. list(APPEND doc_formats texinfo)
  100. # Sphinx texinfo builder supports .info, .txt, .html and .pdf output.
  101. # SPHINX_INFO controls the .info output.
  102. set(texinfo_post_commands
  103. COMMAND ${MAKEINFO_EXECUTABLE} --no-split -o
  104. ${CMAKE_CURRENT_BINARY_DIR}/texinfo/cmake.info
  105. ${CMAKE_CURRENT_BINARY_DIR}/texinfo/cmake.texi
  106. )
  107. endif()
  108. if(SPHINX_QTHELP)
  109. find_package(Python REQUIRED)
  110. find_program(QHELPGENERATOR_EXECUTABLE
  111. NAMES qhelpgenerator-qt5 qhelpgenerator
  112. DOC "qhelpgenerator tool"
  113. )
  114. if(NOT QHELPGENERATOR_EXECUTABLE)
  115. message(FATAL_ERROR "QHELPGENERATOR_EXECUTABLE (qhelpgenerator) not found!")
  116. endif()
  117. list(APPEND doc_formats qthelp)
  118. set(qthelp_post_commands
  119. # Workaround for assistant prior to
  120. # https://codereview.qt-project.org/#change,82250 in Qt 4.
  121. COMMAND ${CMAKE_COMMAND} "-DCSS_DIR=${CMAKE_CURRENT_BINARY_DIR}/qthelp/_static"
  122. -P "${CMAKE_CURRENT_SOURCE_DIR}/apply_qthelp_css_workaround.cmake"
  123. # Workaround sphinx configurability:
  124. # https://github.com/sphinx-doc/sphinx/issues/1448
  125. COMMAND ${CMAKE_COMMAND} "-DQTHELP_DIR=${CMAKE_CURRENT_BINARY_DIR}/qthelp/"
  126. -P "${CMAKE_CURRENT_SOURCE_DIR}/fixup_qthelp_names.cmake"
  127. # Create proper identifiers. Workaround for
  128. # https://github.com/sphinx-doc/sphinx/issues/1491
  129. COMMAND "${Python_EXECUTABLE}"
  130. "${CMAKE_CURRENT_SOURCE_DIR}/create_identifiers.py"
  131. "${CMAKE_CURRENT_BINARY_DIR}/qthelp/"
  132. COMMAND ${QHELPGENERATOR_EXECUTABLE}
  133. ${CMAKE_CURRENT_BINARY_DIR}/qthelp/CMake.qhcp
  134. )
  135. endif()
  136. if(SPHINX_LATEXPDF)
  137. list(APPEND doc_formats latexpdf)
  138. endif()
  139. set(doc_html_opts "")
  140. if(CMake_SPHINX_CMAKE_ORG)
  141. list(APPEND doc_html_opts
  142. -A googleanalytics=1
  143. -A opensearch=1
  144. -A versionswitch=1
  145. )
  146. if(CMake_SPHINX_CMAKE_ORG_OUTDATED)
  147. list(APPEND doc_html_opts -A outdated=1)
  148. endif()
  149. list(APPEND html_pre_commands
  150. COMMAND ${CMAKE_COMMAND} -Dversion=${CMake_VERSION} -P ${CMAKE_CURRENT_SOURCE_DIR}/tutorial_archive.cmake
  151. )
  152. list(APPEND qthelp_post_commands
  153. COMMAND ${CMAKE_COMMAND} -E copy
  154. "${CMAKE_CURRENT_BINARY_DIR}/qthelp/CMake.qch"
  155. "${CMAKE_CURRENT_BINARY_DIR}/html/CMake.qch"
  156. )
  157. endif()
  158. # Redirect `sphinx-build` output to `build-<format>.log` file?
  159. set(sphinx_use_build_log TRUE)
  160. set(sphinx_verbose_levels "DEBUG;TRACE")
  161. set(sphinx_no_redirect_levels "VERBOSE;${sphinx_verbose_levels}")
  162. # NOTE There is no generic verbosity level for all supported generators,
  163. # so lets use CMake verbosity level to control if `sphinx-build` should
  164. # redirect it's output to a file or a user wants to see it at build time.
  165. if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.25)
  166. cmake_language(GET_MESSAGE_LOG_LEVEL verbose_level)
  167. else()
  168. # If building under CMake < 3.25, fallback to `CMAKE_MESSAGE_LOG_LEVEL`
  169. # variable. It was added in 3.17 but it's OK to set it even for older
  170. # versions (w/o any effect on `message()` command of course).
  171. set(verbose_level ${CMAKE_MESSAGE_LOG_LEVEL})
  172. endif()
  173. if(DEFINED ENV{VERBOSE} OR CMAKE_VERBOSE_MAKEFILE OR verbose_level IN_LIST sphinx_no_redirect_levels)
  174. set(sphinx_use_build_log FALSE)
  175. if(verbose_level IN_LIST sphinx_verbose_levels)
  176. # NOTE Sphinx accept multiple `-v` options for more verbosity
  177. # but the output mostly for Sphinx developers...
  178. list(APPEND sphinx_flags "-v")
  179. endif()
  180. endif()
  181. set(doc_format_outputs "")
  182. set(doc_format_last "")
  183. foreach(format IN LISTS doc_formats)
  184. set(doc_format_output "doc_format_${format}")
  185. set(doc_format_log "")
  186. set(build_comment_tail " ...")
  187. if(sphinx_use_build_log)
  188. set(doc_format_log "build-${format}.log")
  189. set(build_comment_tail ": see Utilities/Sphinx/${doc_format_log}")
  190. list(PREPEND doc_format_log ">")
  191. endif()
  192. if(CMake_SPHINX_CMAKE_ORG)
  193. set(doctrees "doctrees/${format}")
  194. else()
  195. set(doctrees "doctrees")
  196. endif()
  197. if(format STREQUAL "latexpdf")
  198. # This format does not use builder (-b) but make_mode (-M) which expects
  199. # arguments in peculiar order
  200. set(_args
  201. -M ${format}
  202. ${CMake_SOURCE_DIR}/Help
  203. ${CMAKE_CURRENT_BINARY_DIR}/${format}
  204. -c ${CMAKE_CURRENT_BINARY_DIR}
  205. -d ${CMAKE_CURRENT_BINARY_DIR}/${doctrees}
  206. ${sphinx_flags}
  207. ${doc_${format}_opts}
  208. )
  209. else()
  210. # other formats use standard builder (-b) mode
  211. set(_args
  212. -c ${CMAKE_CURRENT_BINARY_DIR}
  213. -d ${CMAKE_CURRENT_BINARY_DIR}/${doctrees}
  214. -b ${format}
  215. ${sphinx_flags}
  216. ${doc_${format}_opts}
  217. ${CMake_SOURCE_DIR}/Help
  218. ${CMAKE_CURRENT_BINARY_DIR}/${format}
  219. )
  220. endif()
  221. add_custom_command(
  222. OUTPUT ${doc_format_output}
  223. ${${format}_pre_commands}
  224. COMMAND ${SPHINX_EXECUTABLE} ${_args} ${doc_format_log}
  225. ${${format}_post_commands}
  226. DEPENDS ${doc_format_last}
  227. COMMENT "sphinx-build ${format}${build_comment_tail}"
  228. VERBATIM
  229. )
  230. set_property(SOURCE ${doc_format_output} PROPERTY SYMBOLIC 1)
  231. list(APPEND doc_format_outputs ${doc_format_output})
  232. if(NOT CMake_SPHINX_CMAKE_ORG)
  233. set(doc_format_last ${doc_format_output})
  234. endif()
  235. endforeach()
  236. add_custom_target(documentation ALL DEPENDS ${doc_format_outputs})
  237. if(CMake_SPHINX_DEPEND_ON_EXECUTABLES)
  238. foreach(t IN ITEMS cmake ccmake cmake-gui cpack ctest)
  239. if(TARGET ${t})
  240. # Build documentation after main executables.
  241. add_dependencies(documentation ${t})
  242. endif()
  243. endforeach()
  244. endif()
  245. if(CMake_SPHINX_CMAKE_ORG)
  246. return()
  247. endif()
  248. if(SPHINX_INFO)
  249. CMake_OPTIONAL_COMPONENT(sphinx-info)
  250. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/texinfo/cmake.info
  251. DESTINATION ${CMAKE_INFO_DIR}
  252. ${COMPONENT}
  253. )
  254. endif()
  255. if(SPHINX_MAN)
  256. file(GLOB man_rst RELATIVE ${CMake_SOURCE_DIR}/Help/manual
  257. ${CMake_SOURCE_DIR}/Help/manual/*.[1-9].rst)
  258. foreach(m IN LISTS man_rst)
  259. if("x${m}" MATCHES "^x(.+)\\.([1-9])\\.rst$")
  260. set(name "${CMAKE_MATCH_1}")
  261. set(sec "${CMAKE_MATCH_2}")
  262. set(skip FALSE)
  263. if(NOT CMakeHelp_STANDALONE)
  264. if(name STREQUAL "ccmake" AND NOT BUILD_CursesDialog)
  265. set(skip TRUE)
  266. elseif(name STREQUAL "cmake-gui" AND NOT BUILD_QtDialog)
  267. set(skip TRUE)
  268. endif()
  269. endif()
  270. if(NOT skip)
  271. CMake_OPTIONAL_COMPONENT(sphinx-man)
  272. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/man/${name}.${sec}
  273. DESTINATION ${CMAKE_MAN_DIR}/man${sec}
  274. ${COMPONENT})
  275. endif()
  276. unset(skip)
  277. endif()
  278. endforeach()
  279. endif()
  280. if(SPHINX_HTML)
  281. CMake_OPTIONAL_COMPONENT(sphinx-html)
  282. install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html
  283. DESTINATION ${CMAKE_DOC_DIR}
  284. ${COMPONENT}
  285. PATTERN .buildinfo EXCLUDE
  286. )
  287. endif()
  288. if(SPHINX_SINGLEHTML)
  289. CMake_OPTIONAL_COMPONENT(sphinx-singlehtml)
  290. install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/singlehtml
  291. DESTINATION ${CMAKE_DOC_DIR}
  292. ${COMPONENT}
  293. PATTERN .buildinfo EXCLUDE
  294. )
  295. endif()
  296. if(SPHINX_QTHELP)
  297. CMake_OPTIONAL_COMPONENT(sphinx-qthelp)
  298. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/qthelp/CMake.qch
  299. DESTINATION ${CMAKE_DOC_DIR} ${COMPONENT}
  300. )
  301. endif()
  302. if(SPHINX_LATEXPDF)
  303. CMake_OPTIONAL_COMPONENT(sphinx-latexpdf)
  304. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/latexpdf/latex/CMake.pdf
  305. DESTINATION ${CMAKE_DOC_DIR} ${COMPONENT}
  306. )
  307. endif()