1717"""Sphinx configuration for the tvm-ffi documentation site."""
1818
1919# -*- coding: utf-8 -*-
20+ from __future__ import annotations
21+
2022import os
2123import shutil
2224import subprocess
3739# -- Path constants -------------------------------------------------------
3840_DOCS_DIR = Path (__file__ ).resolve ().parent
3941_RUST_DIR = _DOCS_DIR .parent / "rust"
40- _RUST_OUTPUT_DIR = _DOCS_DIR / "reference" / "rust" / "generated"
4142
4243# -- General configuration ------------------------------------------------
4344# Load version from pyproject.toml
@@ -201,12 +202,7 @@ def _build_rust_docs() -> None:
201202 env = {** os .environ , "RUSTDOCFLAGS" : "--cfg docsrs" },
202203 )
203204
204- # Copy generated documentation
205- if _RUST_OUTPUT_DIR .exists ():
206- shutil .rmtree (_RUST_OUTPUT_DIR )
207- shutil .copytree (target_doc , _RUST_OUTPUT_DIR )
208-
209- print (f"Rust documentation built successfully at { _RUST_OUTPUT_DIR } " )
205+ print (f"Rust documentation built successfully at { target_doc } " )
210206 except subprocess .CalledProcessError as e :
211207 print (f"Warning: Failed to build Rust documentation: { e } " )
212208 except FileNotFoundError :
@@ -219,13 +215,33 @@ def _apply_config_overrides(_: object, config: object) -> None:
219215 config .build_rust_docs = build_rust_docs
220216
221217
218+ def _copy_rust_docs_to_output (app : sphinx .application .Sphinx , exception : Exception | None ) -> None :
219+ """Copy Rust documentation to the HTML output directory after build completes."""
220+ if exception is not None or not build_rust_docs :
221+ return
222+
223+ src_dir = _RUST_DIR / "target" / "doc"
224+ dst_dir = Path (app .outdir ) / "reference" / "rust" / "generated"
225+
226+ if src_dir .exists ():
227+ if dst_dir .exists ():
228+ shutil .rmtree (dst_dir )
229+ shutil .copytree (src_dir , dst_dir )
230+ print (f"Copied Rust documentation from { src_dir } to { dst_dir } " )
231+ else :
232+ print (
233+ f"Warning: Rust documentation source directory not found at { src_dir } . Skipping copy."
234+ )
235+
236+
222237def setup (app : sphinx .application .Sphinx ) -> None :
223238 """Register custom Sphinx configuration values."""
224239 _prepare_stub_files ()
225240 _build_rust_docs ()
226241 app .add_config_value ("build_exhale" , build_exhale , "env" )
227242 app .add_config_value ("build_rust_docs" , build_rust_docs , "env" )
228243 app .connect ("config-inited" , _apply_config_overrides )
244+ app .connect ("build-finished" , _copy_rust_docs_to_output )
229245
230246
231247autodoc_mock_imports = ["torch" ]
0 commit comments