blob: 6971384a34b72b5334a62424f91b53e7ae0f92b7 [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
Chad Norvell9357f332022-02-18 12:07:24 -080023Documentation Overview
Alexei Frolov8ffcb912019-11-18 11:00:20 -080024======================
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
Chad Norvell9357f332022-02-18 12:07:24 -080032Build Integration
Alexei Frolov8ffcb912019-11-18 11:00:20 -080033=================
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
Anthony DiGirolamo389664e2021-05-06 16:57:20 -070045* `mermaid <https://mermaid-js.github.io/>`_ via the `sphinxcontrib-mermaid
46 <https://pypi.org/project/sphinxcontrib-mermaid/>`_ package.
47
Alexei Frolov8ffcb912019-11-18 11:00:20 -080048Documentation source and asset files are placed alongside code within a module
Adam MacBethd5353592021-07-21 18:02:44 +000049and registered as a ``pw_doc_group`` target within a ``BUILD.gn`` file. These
Alexei Frolov8ffcb912019-11-18 11:00:20 -080050groups become available for import within a special documentation generation
51target, which accumulates all of them and renders the resulting HTML. This
52system can either be used directly within Pigweed, or integrated into a
53downstream project.
54
Chad Norvell9357f332022-02-18 12:07:24 -080055GN Templates
Alexei Frolov8ffcb912019-11-18 11:00:20 -080056------------
57
58pw_doc_group
59____________
Alexei Frolov8ffcb912019-11-18 11:00:20 -080060The main template for defining documentation files is ``pw_doc_group``. It is
61used to logically group a collection of documentation source files and assets.
62Each Pigweed module is expected to provide at least one ``pw_doc_group`` target
63defining the module's documentation. A ``pw_doc_group`` can depend on other
64groups, causing them to be built with it.
65
66**Arguments**
67
68* ``sources``: RST documentation source files.
69* ``inputs``: Additional resources required for the docs (images, data files,
70 etc.)
71* ``group_deps``: Other ``pw_doc_group`` targets required by this one.
Alexei Frolov0f987632022-08-09 16:23:03 +000072* ``report_deps``: Report card generating targets (e.g. ``pw_size_diff``) on
Alexei Frolov8ffcb912019-11-18 11:00:20 -080073 which the docs depend.
74
75**Example**
76
77.. code::
78
79 pw_doc_group("my_doc_group") {
80 sources = [ "docs.rst" ]
81 inputs = [ "face-with-tears-of-joy-emoji.svg" ]
82 group_deps = [ ":sub_doc_group" ]
83 report_deps = [ ":my_size_report" ]
84 }
85
86pw_doc_gen
87__________
Alexei Frolov8ffcb912019-11-18 11:00:20 -080088The ``pw_doc_gen`` template creates a target which renders complete HTML
89documentation for a project. It depends on registered ``pw_doc_group`` targets
90and creates an action which collects and renders them.
91
92To generate the complete docs, the template also requires a ``conf.py`` file
93configuring Sphinx's output, and a top level ``index.rst`` for the main page of
94the documentation. These are added at the root level of the built documentation
95to tie everything together.
96
97**Arguments**
98
99* ``conf``: Path to the ``conf.py`` to use for Sphinx.
100* ``index``: Path to the top-level ``index.rst`` file.
101* ``output_directory``: Directory in which to render HTML output.
102* ``deps``: List of all ``pw_doc_group`` targets required for the documentation.
103
104**Example**
105
106.. code::
107
108 pw_doc_gen("my_docs") {
109 conf = "//my_docs/conf.py"
110 index = "//my_docs/index.rst"
111 output_directory = target_gen_dir
112 deps = [
113 "//my_module:my_doc_group",
114 ]
115 }
116
Chad Norvell9357f332022-02-18 12:07:24 -0800117Generating Documentation
Alexei Frolov8ffcb912019-11-18 11:00:20 -0800118------------------------
Alexei Frolov8ffcb912019-11-18 11:00:20 -0800119All source files listed under a ``pw_doc_gen`` target and its ``pw_doc_group``
120dependencies get copied out into a directory structure mirroring the original
121layout of the modules in which the sources appear. This is demonstrated below
122using a subset of Pigweed's core documentation.
123
124Consider the following target in ``$dir_pigweed/docs/BUILD.gn``:
125
126.. code::
127
128 pw_doc_gen("docs") {
129 conf = "conf.py"
130 index = "index.rst"
131 output_directory = target_gen_dir
132 deps = [
133 "$dir_pw_bloat:docs",
134 "$dir_pw_docgen:docs",
135 "$dir_pw_preprocessor:docs",
136 ]
137 }
138
139A documentation tree is created under the output directory. Each of the sources
140and inputs in the target's dependency graph is copied under this tree in the
141same directory structure as they appear under the root GN build directory
142(``$dir_pigweed`` in this case). The ``conf.py`` and ``index.rst`` provided
143directly to the ``pw_doc_gen`` template are copied in at the root of the tree.
144
145.. code::
146
147 out/gen/docs/pw_docgen_tree/
148 ├── conf.py
149 ├── index.rst
150 ├── pw_bloat
Armando Montanez5464d5f2020-02-20 10:12:20 -0800151 ├── bloat.rst
152 └── examples
153 └── simple_bloat.rst
Alexei Frolov8ffcb912019-11-18 11:00:20 -0800154 ├── pw_docgen
Armando Montanez5464d5f2020-02-20 10:12:20 -0800155 └── docgen.rst
Alexei Frolov8ffcb912019-11-18 11:00:20 -0800156 └── pw_preprocessor
157 └── docs.rst
158
159This is the documentation tree which gets passed to Sphinx to build HTML output.
160Imports within documentation files must be relative to this structure. In
161practice, relative imports from within modules' documentation groups are
162identical to the project's directory structure. The only special case is the
163top-level ``index.rst`` file's imports; they must start from the project's build
164root.
Chad Norvell9357f332022-02-18 12:07:24 -0800165
166Sphinx Extensions
167=================
168This module houses Pigweed-specific extensions for the Sphinx documentation
169generator. Extensions are included and configured in ``docs/conf.py``.
170
Chad Norvell354824c2023-03-08 02:23:25 +0000171module_metadata
172---------------
173Per :ref:`SEED-0102 <seed-0102>`, Pigweed module documentation has a standard
174format. The ``pigweed-module`` Sphinx directive provides that format and
175registers module metadata that can be used elsewhere in the Sphinx build.
176
177We need to add the directive after the document title, and add a class *to*
178the document title to achieve the title & subtitle formatting. Here's an
179example:
180
181.. code-block:: rst
182
183 .. rst-class:: with-subtitle
184
185 =========
186 pw_string
187 =========
188
189 .. pigweed-module::
190 :name: pw_string
191 :tagline: Efficient, easy, and safe string manipulation
192 :status: stable
193 :languages: C++14, C++17
194 :code-size-impact: 500 to 1500 bytes
195 :get-started: module-pw_string-get-started
196 :design: module-pw_string-design
197 :guides: module-pw_string-guide
198 :api: module-pw_string-api
199
200 Module sales pitch goes here!
201
202Directive options
203_________________
204- ``name``: The module name (required)
205- ``tagline``: A very short tagline that summarizes the module (required)
206- ``status``: One of ``experimental``, ``unstable``, and ``stable`` (required)
207- ``is-deprecated``: A flag indicating that the module is deprecated
208- ``languages``: A comma-separated list of languages the module supports
209- ``code-size-impact``: A summarize of the average code size impact
210- ``get-started``: A reference to the getting started section (required)
211- ``tutorials``: A reference to the tutorials section
212- ``guides``: A reference to the guides section
213- ``design``: A reference to the design considerations section (required)
214- ``concepts``: A reference to the concepts documentation
215- ``api``: A reference to the API documentation
216
Chad Norvell9357f332022-02-18 12:07:24 -0800217google_analytics
218----------------
219When this extension is included and a ``google_analytics_id`` is set in the
220Sphinx configuration, a Google Analytics tracking tag will be added to each
221page of the documentation when it is rendered to HTML.
222
223By default, the Sphinx configuration's ``google_analytics_id`` is set
224automatically based on the value of the GN argument
225``pw_docs_google_analytics_id``, allowing you to control whether tracking is
226enabled or not in your build configuration. Typically, you would only enable
227this for documentation builds intended for deployment on the web.