CMakeLists.txt 11 KB

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