CMakeLists.txt 11 KB

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