Skip to content

Commit bf84742

Browse files
committed
Add CMake module and basic config to generate documentation with Sphinx
1 parent 66a7a78 commit bf84742

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ set(CMAKE_MODULE_FILES
8484
cmake/modules/WindowsResources.cmake
8585
cmake/modules/TemplateFinder.cmake
8686
cmake/modules/Doxygen.cmake
87+
cmake/modules/Sphinx.cmake
8788
cmake/modules/ListToString.cmake
8889
cmake/modules/ShellCompletion.cmake
8990
cmake/modules/DevelUtilities.cmake
@@ -96,6 +97,7 @@ set(CMAKE_TEMPLATE_FILES
9697
cmake/templates/desktop.in
9798
cmake/templates/appdata.xml.in
9899
cmake/templates/doxygen.in
100+
cmake/templates/sphinx-conf.py.in
99101
cmake/templates/global.h.in
100102
cmake/templates/version.h.in
101103
cmake/templates/template.pc.in)

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ These build instructions apply to `c++utilities` but also to my other projects u
6464
* CMake (at least 3.17.0) and Ninja or GNU Make
6565
* cppunit for unit tests (optional)
6666
* Doxygen for API documentation (optional)
67+
* Sphinx with MyST-Parser for general documentation (optional, under Arch Linux install
68+
`python-myst-parser`)
6769
* Graphviz for diagrams in the API documentation (optional)
6870
* clang-format and cmake-format for tidying (optional)
6971
* llvm-profdata, llvm-cov and cppunit for source-based code coverage analysis (optional)

cmake/modules/Sphinx.cmake

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
cmake_minimum_required(VERSION 3.17.0 FATAL_ERROR)
2+
3+
if (NOT BASIC_PROJECT_CONFIG_DONE)
4+
message(FATAL_ERROR "Before including the Sphinx module, the BasicConfig module must be included.")
5+
endif ()
6+
7+
option(NO_SPHINX "whether creation of Sphinx targets is disabled (enabled by default)" OFF)
8+
if (NO_SPHINX)
9+
return()
10+
endif ()
11+
12+
# find doxygen.h template
13+
include(TemplateFinder)
14+
find_template_file("sphinx-conf.py" CPP_UTILITIES SPHINX_TEMPLATE_FILE)
15+
16+
# find executables
17+
find_program(SPHINX_BIN sphinx-build)
18+
if (NOT SPHINX_BIN)
19+
message(WARNING "Sphinx not found, unable to add target for generating documentation for ${META_TARGET_NAME}")
20+
return()
21+
endif ()
22+
23+
# configure paths and "conf.py"
24+
set(SPHINX_INPUT_FILES ${SPHINX_DOC_FILES})
25+
if (NOT SPHINX_SOURCE_DIR)
26+
set(SPHINX_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
27+
endif ()
28+
set(SPHINX_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}/sphinxconfig")
29+
set(SPHINX_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/doc")
30+
configure_file("${SPHINX_TEMPLATE_FILE}" "${SPHINX_CONFIG_DIR}/conf.py")
31+
32+
# add target for generating documentation
33+
add_custom_target("${META_TARGET_NAME}_sphinxdoc" COMMAND "${SPHINX_BIN}" -c "${SPHINX_CONFIG_DIR}" "${SPHINX_SOURCE_DIR}" "${SPHINX_OUTPUT_DIR}")
34+
if (NOT TARGET sphinxdoc)
35+
add_custom_target(sphinxdoc)
36+
endif ()
37+
add_dependencies(sphinxdoc "${META_TARGET_NAME}_sphinxdoc")
38+
39+
# add install target for API documentation
40+
if (NOT META_NO_INSTALL_TARGETS AND ENABLE_INSTALL_TARGETS)
41+
install(
42+
DIRECTORY "${SPHINX_OUTPUT_DIR}"
43+
DESTINATION "${META_DATA_DIR}"
44+
COMPONENT doc
45+
OPTIONAL)
46+
if (NOT TARGET install-doc)
47+
add_custom_target(install-doc COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=doc -P
48+
"${CMAKE_BINARY_DIR}/cmake_install.cmake")
49+
endif ()
50+
endif ()
51+
52+
message(STATUS "Added target for building documentation for ${META_TARGET_NAME} with Sphinx")

cmake/templates/sphinx-conf.py.in

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -- Project information -----------------------------------------------------
2+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
3+
project = '@META_APP_NAME@'
4+
copyright = '@META_APP_COPYRIGHT@'
5+
author = '@META_APP_AUTHOR@'
6+
version = '@META_VERSION_MAJOR@.@META_VERSION_MINOR@.@META_VERSION_PATCH@'
7+
release = '@META_RELEASE_DATE@'
8+
9+
# -- General configuration ------------------------------------------------
10+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
11+
exclude_patterns = [
12+
'.github', # exclude GitHub issue templates
13+
'syncthing/go/**', # exclude files from Syncthing itself
14+
'CMakeLists*', # avoid CMakeLists.txt from being considered normal txt files
15+
'**/CMakeLists*', # avoid CMakeLists.txt from being considered normal txt files
16+
'LICENSES-*', # exclude license compilations
17+
]
18+
extensions = [
19+
'myst_parser', # enable Markdown support
20+
]
21+
language = 'en'
22+
master_doc = 'index'
23+
pygments_style = 'sphinx'
24+
source_suffix = {
25+
'.rst': 'restructuredtext',
26+
'.md': 'markdown',
27+
}
28+
templates_path = ['_templates']
29+
30+
# -- Options for HTML output ----------------------------------------------
31+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
32+
html_theme = 'alabaster'

0 commit comments

Comments
 (0)