Skip to content

Commit 87d8ae7

Browse files
committed
[clang][cmake] Include generated rst files in html built by docs-clang-html target
Summary: This is an attempt to simply the process of building the clang documentation, which should help avoid some of the recent issues we've had generating the documentation for the website. The html documentation for clang is generated by sphinx from the reStructuredText (rst) files we have in the clang/docs directory. There are also some rst files that need to be generated by TableGen, before they can be passed to sphinx. Prior to this patch we were not generating those rst files as part with the build system and they had to be generated manually. This patch enables the automatic generation of these rst files, but since they are generated at build time the cannot be placed in the clang/docs directory and must go into the cmake build directory. Unfortunately sphinx does not currently support multiple source directories[1], so in order to be able to generate the full documentation, we need to work around this by copying the rst files from the clang/docs into the build directory before generating the html documentation. [1] sphinx-doc/sphinx#3132 Reviewers: rsmith, aaron.ballman, beanz, smeenai, phosek, compnerd, mgorny, delcypher Reviewed By: mgorny, delcypher Subscribers: delcypher, merge_guards_bot, mgorny, llvm-commits, cfe-commits Tags: #clang, #llvm Differential Revision: https://reviews.llvm.org/D72875
1 parent 621d969 commit 87d8ae7

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

clang/docs/AttributeReference.rst

Lines changed: 0 additions & 13 deletions
This file was deleted.

clang/docs/CMakeLists.txt

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,43 @@ if (LLVM_ENABLE_DOXYGEN)
9090
endif()
9191
endif()
9292

93+
function (gen_rst_file_from_td output_file td_option source docs_target)
94+
if (NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${source}")
95+
message(FATAL_ERROR "Cannot find source file: ${source} in ${CMAKE_CURRENT_SOURCE_DIR}")
96+
endif()
97+
get_filename_component(TABLEGEN_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${source}" DIRECTORY)
98+
list(APPEND LLVM_TABLEGEN_FLAGS "-I${TABLEGEN_INCLUDE_DIR}")
99+
clang_tablegen(${output_file} ${td_option} SOURCE ${source} TARGET "gen-${output_file}")
100+
add_dependencies(${docs_target} "gen-${output_file}")
101+
endfunction()
102+
93103
if (LLVM_ENABLE_SPHINX)
94104
include(AddSphinxTarget)
95105
if (SPHINX_FOUND)
96106
if (${SPHINX_OUTPUT_HTML})
97-
add_sphinx_target(html clang)
107+
add_sphinx_target(html clang SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}")
108+
109+
# Copy rst files to build directory before generating the html
110+
# documentation. Some of the rst files are generated, so they
111+
# only exist in the build directory. Sphinx needs all files in
112+
# the same directory in order to genrate the html, so we need to
113+
# copy all the non-gnerated rst files from the source to the build
114+
# directory before we run sphinx.
115+
add_custom_target(copy-clang-rst-docs
116+
COMMAND "${CMAKE_COMMAND}" -E copy_directory
117+
"${CMAKE_CURRENT_SOURCE_DIR}"
118+
"${CMAKE_CURRENT_BINARY_DIR}")
119+
add_dependencies(docs-clang-html copy-clang-rst-docs)
120+
98121
add_custom_command(TARGET docs-clang-html POST_BUILD
99-
COMMAND ${CMAKE_COMMAND} -E copy
122+
COMMAND "${CMAKE_COMMAND}" -E copy
100123
"${CMAKE_CURRENT_SOURCE_DIR}/LibASTMatchersReference.html"
101124
"${CMAKE_CURRENT_BINARY_DIR}/html/LibASTMatchersReference.html")
125+
126+
# Generated files
127+
gen_rst_file_from_td(AttributeReference.rst -gen-attr-docs ../include/clang/Basic/Attr.td docs-clang-html)
128+
gen_rst_file_from_td(DiagnosticsReference.rst -gen-diag-docs ../include/clang/Basic/Diagnostic.td docs-clang-html)
129+
gen_rst_file_from_td(ClangCommandLineReference.rst -gen-opt-docs ../include/clang/Driver/ClangOptionDocs.td docs-clang-html)
102130
endif()
103131
if (${SPHINX_OUTPUT_MAN})
104132
add_sphinx_target(man clang)

llvm/cmake/modules/AddSphinxTarget.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ endif()
1818
#
1919
# ``project`` should be the project name
2020
function (add_sphinx_target builder project)
21+
cmake_parse_arguments(ARG "" "SOURCE_DIR" "" ${ARGN})
2122
set(SPHINX_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/${builder}")
2223
set(SPHINX_DOC_TREE_DIR "${CMAKE_CURRENT_BINARY_DIR}/_doctrees-${project}-${builder}")
2324
set(SPHINX_TARGET_NAME docs-${project}-${builder})
@@ -28,13 +29,17 @@ function (add_sphinx_target builder project)
2829
set(SPHINX_WARNINGS_AS_ERRORS_FLAG "")
2930
endif()
3031

32+
if (NOT ARG_SOURCE_DIR)
33+
set(ARG_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
34+
endif()
35+
3136
add_custom_target(${SPHINX_TARGET_NAME}
3237
COMMAND ${SPHINX_EXECUTABLE}
3338
-b ${builder}
3439
-d "${SPHINX_DOC_TREE_DIR}"
3540
-q # Quiet: no output other than errors and warnings.
3641
${SPHINX_WARNINGS_AS_ERRORS_FLAG} # Treat warnings as errors if requested
37-
"${CMAKE_CURRENT_SOURCE_DIR}" # Source
42+
"${ARG_SOURCE_DIR}" # Source
3843
"${SPHINX_BUILD_DIR}" # Output
3944
COMMENT
4045
"Generating ${builder} Sphinx documentation for ${project} into \"${SPHINX_BUILD_DIR}\"")

0 commit comments

Comments
 (0)