blob: bcb1d6193330968304c4b423acb13aae89adaf23 [file] [log] [blame]
Wyatt Heplerf9fb90f2020-09-30 18:59:33 -07001.. _module-pw_docgen:
Alexei Frolov199045a2020-08-28 13:02:30 -07002
Wyatt Heplerb82f9952019-11-25 13:56:31 -08003---------
4pw_docgen
5---------
Alexei Frolov8ffcb912019-11-18 11:00:20 -08006The docgen module provides tools to generate documentation for Pigweed-based
7projects, and for Pigweed itself.
8
9Pigweed-based projects typically use a subset of Pigweed's modules and add their
10own product-specific modules on top of that, which may have product-specific
11documentation. Docgen provides a convenient way to combine all of the relevant
12documentation for a project into one place, allowing downstream consumers of
13release bundles (e.g. factory teams, QA teams, beta testers, etc.) to have a
14unified source of documentation early on.
15
16The documentation generation is integrated directly into the build system. Any
17build target can depend on documentation, which allows it to be included as part
18of a factory release build, for example. Additionally, documentation itself can
19depend on other build targets, such as report cards for binary size/profiling.
20Any time the code is changed, documentation will be regenerated with the updated
21reports.
22
23Documentation overview
24======================
25Each Pigweed module provides documentation describing its functionality, use
26cases, and programming API.
27
28Included in a module's documentation are report cards which show an overview of
29the module's size cost and performance benchmarks. These allow prospective users
30to evaluate the impact of including the module in their projects.
31
32Build integration
33=================
34
35Pigweed documentation files are written in `reStructuredText`_ format and
36rendered to HTML using `Sphinx`_ through Pigweed's GN build system.
37
38.. _reStructuredText: http://docutils.sourceforge.net/rst.html
Rob Mohr7e700002021-05-22 10:56:54 -070039.. inclusive-language: ignore
Alexei Frolov8ffcb912019-11-18 11:00:20 -080040.. _Sphinx: http://www.sphinx-doc.org/en/master
41
Anthony DiGirolamo389664e2021-05-06 16:57:20 -070042There are additonal Sphinx plugins used for rendering diagrams within
43reStructuredText files including:
44
45* `Blockdiag <http://blockdiag.com/>`_ via these sphinxcontrib packages:
46
47 * `sphinxcontrib-blockdiag
48 <https://pypi.org/project/sphinxcontrib-blockdiag/>`_
49 * `sphinxcontrib-actdiag <https://pypi.org/project/sphinxcontrib-actdiag/>`_
50 * `sphinxcontrib-nwdiag <https://pypi.org/project/sphinxcontrib-nwdiag/>`_
51 * `sphinxcontrib-seqdiag <https://pypi.org/project/sphinxcontrib-seqdiag/>`_
52
53* `mermaid <https://mermaid-js.github.io/>`_ via the `sphinxcontrib-mermaid
54 <https://pypi.org/project/sphinxcontrib-mermaid/>`_ package.
55
Alexei Frolov8ffcb912019-11-18 11:00:20 -080056Documentation source and asset files are placed alongside code within a module
57and registered as a ``pw_docgen_group`` target within a ``BUILD.gn`` file. These
58groups become available for import within a special documentation generation
59target, which accumulates all of them and renders the resulting HTML. This
60system can either be used directly within Pigweed, or integrated into a
61downstream project.
62
63GN templates
64------------
65
66pw_doc_group
67____________
68
69The main template for defining documentation files is ``pw_doc_group``. It is
70used to logically group a collection of documentation source files and assets.
71Each Pigweed module is expected to provide at least one ``pw_doc_group`` target
72defining the module's documentation. A ``pw_doc_group`` can depend on other
73groups, causing them to be built with it.
74
75**Arguments**
76
77* ``sources``: RST documentation source files.
78* ``inputs``: Additional resources required for the docs (images, data files,
79 etc.)
80* ``group_deps``: Other ``pw_doc_group`` targets required by this one.
81* ``report_deps``: Report card generating targets (e.g. ``pw_size_report``) on
82 which the docs depend.
83
84**Example**
85
86.. code::
87
88 pw_doc_group("my_doc_group") {
89 sources = [ "docs.rst" ]
90 inputs = [ "face-with-tears-of-joy-emoji.svg" ]
91 group_deps = [ ":sub_doc_group" ]
92 report_deps = [ ":my_size_report" ]
93 }
94
95pw_doc_gen
96__________
97
98The ``pw_doc_gen`` template creates a target which renders complete HTML
99documentation for a project. It depends on registered ``pw_doc_group`` targets
100and creates an action which collects and renders them.
101
102To generate the complete docs, the template also requires a ``conf.py`` file
103configuring Sphinx's output, and a top level ``index.rst`` for the main page of
104the documentation. These are added at the root level of the built documentation
105to tie everything together.
106
107**Arguments**
108
109* ``conf``: Path to the ``conf.py`` to use for Sphinx.
110* ``index``: Path to the top-level ``index.rst`` file.
111* ``output_directory``: Directory in which to render HTML output.
112* ``deps``: List of all ``pw_doc_group`` targets required for the documentation.
113
114**Example**
115
116.. code::
117
118 pw_doc_gen("my_docs") {
119 conf = "//my_docs/conf.py"
120 index = "//my_docs/index.rst"
121 output_directory = target_gen_dir
122 deps = [
123 "//my_module:my_doc_group",
124 ]
125 }
126
127Generating documentation
128------------------------
129
130All source files listed under a ``pw_doc_gen`` target and its ``pw_doc_group``
131dependencies get copied out into a directory structure mirroring the original
132layout of the modules in which the sources appear. This is demonstrated below
133using a subset of Pigweed's core documentation.
134
135Consider the following target in ``$dir_pigweed/docs/BUILD.gn``:
136
137.. code::
138
139 pw_doc_gen("docs") {
140 conf = "conf.py"
141 index = "index.rst"
142 output_directory = target_gen_dir
143 deps = [
144 "$dir_pw_bloat:docs",
145 "$dir_pw_docgen:docs",
146 "$dir_pw_preprocessor:docs",
147 ]
148 }
149
150A documentation tree is created under the output directory. Each of the sources
151and inputs in the target's dependency graph is copied under this tree in the
152same directory structure as they appear under the root GN build directory
153(``$dir_pigweed`` in this case). The ``conf.py`` and ``index.rst`` provided
154directly to the ``pw_doc_gen`` template are copied in at the root of the tree.
155
156.. code::
157
158 out/gen/docs/pw_docgen_tree/
159 ├── conf.py
160 ├── index.rst
161 ├── pw_bloat
Armando Montanez5464d5f2020-02-20 10:12:20 -0800162 ├── bloat.rst
163 └── examples
164 └── simple_bloat.rst
Alexei Frolov8ffcb912019-11-18 11:00:20 -0800165 ├── pw_docgen
Armando Montanez5464d5f2020-02-20 10:12:20 -0800166 └── docgen.rst
Alexei Frolov8ffcb912019-11-18 11:00:20 -0800167 └── pw_preprocessor
168 └── docs.rst
169
170This is the documentation tree which gets passed to Sphinx to build HTML output.
171Imports within documentation files must be relative to this structure. In
172practice, relative imports from within modules' documentation groups are
173identical to the project's directory structure. The only special case is the
174top-level ``index.rst`` file's imports; they must start from the project's build
175root.