@@ -82,55 +82,87 @@ if (LLVM_ENABLE_DOXYGEN)
8282endif ()
8383endif ()
8484
85- function (gen_rst_file_from_td output_file td_option source docs_target )
85+ function (gen_rst_file_from_td output_file td_option source )
8686 if (NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR} /${source} " )
8787 message (FATAL_ERROR "Cannot find source file: ${source} in ${CMAKE_CURRENT_SOURCE_DIR} " )
8888 endif ()
8989 get_filename_component (TABLEGEN_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR} /${source} " DIRECTORY )
9090 list (APPEND LLVM_TABLEGEN_FLAGS "-I${TABLEGEN_INCLUDE_DIR} " )
9191 list (APPEND LLVM_TABLEGEN_FLAGS "-I${CMAKE_CURRENT_SOURCE_DIR} /../../clang/include/clang/Driver/" )
9292 clang_tablegen(Source /${output_file} ${td_option} SOURCE ${source} TARGET "gen-${output_file} " )
93- add_dependencies (${docs_target} "gen-${output_file} " )
94-
9593 # clang_tablegen() does not create the output directory automatically,
96- # so we have to create it explicitly. Note that copy-flang-src-docs below
97- # does create the output directory, but it is not necessarily run
98- # before RST generation.
94+ # so we have to create it explicitly.
9995 add_custom_target (create-flang-rst-output -dir
10096 COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR} /Source
10197 )
10298 add_dependencies ("gen-${output_file} " create-flang-rst-output -dir)
10399endfunction ()
104100
105101if (LLVM_ENABLE_SPHINX)
102+ set (FLANG_DOCS_HTML_DIR "${CMAKE_CURRENT_BINARY_DIR} /SourceHtml" )
103+ set (FLANG_DOCS_MAN_DIR "${CMAKE_CURRENT_BINARY_DIR} /SourceMan" )
106104 include (AddSphinxTarget)
107105 if (SPHINX_FOUND)
108- if (${SPHINX_OUTPUT_HTML} )
109- add_sphinx_target(html flang SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR} /Source" )
110106
111- add_dependencies (docs-flang-html copy-flang-src-docs)
107+ # CLANG_TABLEGEN_EXE variable needs to be set for clang_tablegen to run without error
108+ find_program (CLANG_TABLEGEN_EXE "clang-tblgen" ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
112109
113- # Copy the flang/docs directory and the generated FIRLangRef.md file to a place in the binary directory.
114- # Having all the files in a single directory makes it possible for Sphinx to process them together.
115- # Add a dependency to the flang-doc target to ensure that the FIRLangRef.md file is generated before the copying happens.
116- add_custom_target (copy-flang-src-docs
117- COMMAND "${CMAKE_COMMAND} " -E copy_directory
118- "${CMAKE_CURRENT_SOURCE_DIR} "
119- "${CMAKE_CURRENT_BINARY_DIR} /Source"
120- DEPENDS flang-doc )
110+ # Generate the RST file from TableGen (shared by both HTML and MAN builds)
111+ gen_rst_file_from_td(FlangCommandLineReference.rst -gen-opt-docs FlangOptionsDocs.td)
121112
122- # Runs a python script prior to HTML generation to prepend a header to FIRLangRef,
123- # Without the header, the page is incorrectly formatted, as it assumes the first entry is the page title.
124- add_custom_command (TARGET copy-flang-src-docs
113+ if (${SPHINX_OUTPUT_HTML} )
114+ message (STATUS "Using index.md for html build" )
115+
116+ # Copy the entire flang/docs directory to the build Source dir,
117+ # then remove the index.rst file, to avoid clash with index.md
118+ # which is used for the HTML build.
119+ add_custom_target (copy-flang-src-docs-html
120+ COMMAND "${CMAKE_COMMAND} " -E copy_directory
121+ "${CMAKE_CURRENT_SOURCE_DIR} "
122+ "${FLANG_DOCS_HTML_DIR} "
123+ COMMAND "${CMAKE_COMMAND} " -E remove
124+ "${FLANG_DOCS_HTML_DIR} /CommandGuide/index.rst"
125+ COMMAND "${CMAKE_COMMAND} " -E copy
126+ "${CMAKE_CURRENT_BINARY_DIR} /Source/FlangCommandLineReference.rst"
127+ "${FLANG_DOCS_HTML_DIR} /FlangCommandLineReference.rst"
128+ DEPENDS flang-doc gen-FlangCommandLineReference.rst)
129+
130+ # Run Python preprocessing ONLY for HTML build
131+ # This script prepends headers to FIRLangRef.md for proper formatting
132+ add_custom_command (TARGET copy-flang-src-docs-html
125133 COMMAND "${Python3_EXECUTABLE} "
126- ARGS ${CMAKE_CURRENT_BINARY_DIR} / Source / FIR/CreateFIRLangRef.py)
134+ ARGS " ${FLANG_DOCS_HTML_DIR} / FIR/CreateFIRLangRef.py" )
127135
128- # CLANG_TABLEGEN_EXE variable needs to be set for clang_tablegen to run without error
129- find_program (CLANG_TABLEGEN_EXE "clang-tblgen" ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
130- gen_rst_file_from_td(FlangCommandLineReference.rst -gen-opt-docs FlangOptionsDocs.td docs-flang-html)
136+ add_sphinx_target(html flang SOURCE_DIR "${FLANG_DOCS_HTML_DIR} " )
137+ add_dependencies (docs-flang-html copy-flang-src-docs-html)
131138 endif ()
139+
140+ # ----------------------------
141+ # MAN BUILD SETUP
142+ # ----------------------------
132143 if (${SPHINX_OUTPUT_MAN} )
133- add_sphinx_target(man flang)
144+ message (STATUS "NOTE: The Flang man page is currently a placeholder with a TODO. See docs/CommandGuide/index.rst for details" )
145+
146+ # Create minimal Source dir with ONLY the files needed for man build:
147+ # - conf.py (Sphinx config)
148+ # - index.rst (top-level man page)
149+ # - FlangCommandLineReference.rst (generated reference)
150+ add_custom_target (copy-flang-src-docs-man
151+ COMMAND "${CMAKE_COMMAND} " -E make_directory
152+ "${FLANG_DOCS_MAN_DIR} "
153+ COMMAND "${CMAKE_COMMAND} " -E copy
154+ "${CMAKE_CURRENT_SOURCE_DIR} /conf.py"
155+ "${FLANG_DOCS_MAN_DIR} /conf.py"
156+ COMMAND "${CMAKE_COMMAND} " -E copy
157+ "${CMAKE_CURRENT_BINARY_DIR} /Source/FlangCommandLineReference.rst"
158+ "${FLANG_DOCS_MAN_DIR} /FlangCommandLineReference.rst"
159+ COMMAND "${CMAKE_COMMAND} " -E copy
160+ "${CMAKE_CURRENT_SOURCE_DIR} /CommandGuide/index.rst"
161+ "${FLANG_DOCS_MAN_DIR} /index.rst"
162+ DEPENDS flang-doc gen-FlangCommandLineReference.rst)
163+
164+ add_sphinx_target(man flang SOURCE_DIR "${FLANG_DOCS_MAN_DIR} " )
165+ add_dependencies (docs-flang-man copy-flang-src-docs-man)
134166 endif ()
135167 endif ()
136168endif ()
0 commit comments