CMakeLists.txt 11 KB

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