44Tutorial
55********
66
7- Once :ref: `integrated in your project <integration >`, the ``Sphinx::Build ``
8- target and the :func: `sphinx_add_docs ` function are available for using .
7+ Once :ref: `integrated into your project <integration >`, the ``Sphinx::Build ``
8+ target and the :func: `sphinx_add_docs ` function are available for use .
99
1010.. _tutorial/target :
1111
@@ -14,8 +14,9 @@ Using the target
1414
1515Let's consider a project that includes a :file: `doc ` folder containing
1616:term: `Sphinx ` documentation. We need to add a :file: `CMakeLists.txt `
17- configuration file to add Python tests within the same directory. The
18- "sphinx-build" command can be easily implemented using a custom target:
17+ configuration file to build the documentation as part of the project.
18+
19+ The ``sphinx-build `` command can be implemented directly using a custom target:
1920
2021.. code-block :: cmake
2122
@@ -26,29 +27,61 @@ configuration file to add Python tests within the same directory. The
2627 )
2728
2829 Building the project will generate an HTML version of the documentation within
29- the build folder, making it ready for installation.
30+ the build directory, making it ready for installation or deployment.
31+
32+ In some cases, the documentation build may depend on Python modules or shared
33+ libraries produced by the project itself. These must be made available through
34+ the build environment.
35+
36+ This can be achieved by explicitly defining environment variables on the target,
37+ for example:
38+
39+ .. code-block :: cmake
40+
41+ set_target_properties(doc PROPERTIES
42+ ENVIRONMENT
43+ PYTHONPATH=$<TARGET_FILE_DIR:MyLibrary>:$ENV{PYTHONPATH}
44+ )
45+
46+ Similarly, shared libraries required at runtime must be discoverable through the
47+ platform-specific library search path.
48+
49+ .. code-block :: cmake
50+
51+ set_target_properties(doc PROPERTIES
52+ APPEND ENVIRONMENT
53+ LD_LIBRARY_PATH=$<TARGET_FILE_DIR:MyLibrary>:$ENV{LD_LIBRARY_PATH}
54+ )
55+
56+ .. warning ::
57+
58+ The environment variable used to locate shared libraries depends on the
59+ platform. :envvar: `LD_LIBRARY_PATH ` is used on Linux,
60+ :envvar: `DYLD_LIBRARY_PATH ` on macOS, and :envvar: `PATH ` on Windows.
3061
3162.. _tutorial/function :
3263
3364Using the function
3465==================
3566
36- A :func: `sphinx_add_docs ` function is provided to create a target which will
37- generate the documentation. Therefore, the custom target added in the previous
38- section could be replaced by the following:
67+ A :func: `sphinx_add_docs ` function is provided to simplify the creation of a
68+ documentation target. The configuration above can therefore be replaced by the
69+ following:
3970
4071.. code-block :: cmake
4172
4273 sphinx_add_docs(doc ALL)
4374
44- By default, the :term: `builder ` used is " html" . Another builder can be defined
75+ By default, the :term: `builder ` used is `` html `` . Another builder can be selected
4576as follows:
4677
4778.. code-block :: cmake
4879
49- sphinx_add_docs(doc ALL BUILDER latex)
80+ sphinx_add_docs(doc ALL
81+ BUILDER latex
82+ )
5083
51- You can define different source and output directories as follows :
84+ Different source and output directories can be specified :
5285
5386.. code-block :: cmake
5487
@@ -57,26 +90,64 @@ You can define different source and output directories as follows:
5790 OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/sphinx-doc
5891 )
5992
60- You can also define a separate directory to fetch the :file: `conf.py ` file:
93+ A separate directory can also be used to locate the :file: `conf.py ` file:
6194
6295.. code-block :: cmake
6396
6497 sphinx_add_docs(doc ALL
6598 CONFIG_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/config
6699 )
67100
68- Defining configuration setting directly within the :term: `CMake ` configuration
69- file to override the :file: ` conf.py ` file can be done as follows :
101+ Configuration values can be overridden directly from the :term: `CMake `
102+ configuration using the `` DEFINE `` option :
70103
71104.. code-block :: cmake
72105
73106 sphinx_add_docs(doc ALL
74107 DEFINE
75- version=${MAKE_PROJECT_VERSION }
108+ version=${PROJECT_VERSION }
76109 )
77110
78- If necessary, you can also ignore the :file: `conf.py ` file:
111+ The :file: `conf.py ` file can be ignored entirely if required :
79112
80113.. code-block :: cmake
81114
82115 sphinx_add_docs(doc ALL ISOLATED)
116+
117+ When the documentation depends on project-built libraries or Python modules,
118+ the build environment can be configured declaratively using dedicated options.
119+
120+ The ``LIBRARY_PATH_PREPEND `` option prepends directories to the platform-specific
121+ library search path, selecting the appropriate environment variable
122+ automatically:
123+
124+ .. code-block :: cmake
125+
126+ sphinx_add_docs(doc ALL
127+ LIBRARY_PATH_PREPEND
128+ $<TARGET_FILE_DIR:MyLibrary>
129+ )
130+
131+ Python modules can similarly be made available by prepending directories to the
132+ Python module search path:
133+
134+ .. code-block :: cmake
135+
136+ sphinx_add_docs(doc ALL
137+ PYTHON_PATH_PREPEND
138+ ${CMAKE_CURRENT_SOURCE_DIR}/python
139+ )
140+
141+ Custom environment variables can also be defined explicitly:
142+
143+ .. code-block :: cmake
144+
145+ sphinx_add_docs(doc ALL
146+ ENVIRONMENT
147+ "MY_PROJECT_DOCS_MODE=1"
148+ "CUSTOM_FLAG=enabled"
149+ )
150+
151+ These options allow the documentation build environment to be described
152+ declaratively and consistently across platforms, without manually handling
153+ platform-specific environment variables.
0 commit comments