Wyatt Hepler | f9fb90f | 2020-09-30 18:59:33 -0700 | [diff] [blame] | 1 | .. _module-pw_docgen: |
Alexei Frolov | 199045a | 2020-08-28 13:02:30 -0700 | [diff] [blame] | 2 | |
Wyatt Hepler | b82f995 | 2019-11-25 13:56:31 -0800 | [diff] [blame] | 3 | --------- |
| 4 | pw_docgen |
| 5 | --------- |
Alexei Frolov | 8ffcb91 | 2019-11-18 11:00:20 -0800 | [diff] [blame] | 6 | The docgen module provides tools to generate documentation for Pigweed-based |
| 7 | projects, and for Pigweed itself. |
| 8 | |
| 9 | Pigweed-based projects typically use a subset of Pigweed's modules and add their |
| 10 | own product-specific modules on top of that, which may have product-specific |
| 11 | documentation. Docgen provides a convenient way to combine all of the relevant |
| 12 | documentation for a project into one place, allowing downstream consumers of |
| 13 | release bundles (e.g. factory teams, QA teams, beta testers, etc.) to have a |
| 14 | unified source of documentation early on. |
| 15 | |
| 16 | The documentation generation is integrated directly into the build system. Any |
| 17 | build target can depend on documentation, which allows it to be included as part |
| 18 | of a factory release build, for example. Additionally, documentation itself can |
| 19 | depend on other build targets, such as report cards for binary size/profiling. |
| 20 | Any time the code is changed, documentation will be regenerated with the updated |
| 21 | reports. |
| 22 | |
Chad Norvell | 9357f33 | 2022-02-18 12:07:24 -0800 | [diff] [blame] | 23 | Documentation Overview |
Alexei Frolov | 8ffcb91 | 2019-11-18 11:00:20 -0800 | [diff] [blame] | 24 | ====================== |
| 25 | Each Pigweed module provides documentation describing its functionality, use |
| 26 | cases, and programming API. |
| 27 | |
| 28 | Included in a module's documentation are report cards which show an overview of |
| 29 | the module's size cost and performance benchmarks. These allow prospective users |
| 30 | to evaluate the impact of including the module in their projects. |
| 31 | |
Chad Norvell | 9357f33 | 2022-02-18 12:07:24 -0800 | [diff] [blame] | 32 | Build Integration |
Alexei Frolov | 8ffcb91 | 2019-11-18 11:00:20 -0800 | [diff] [blame] | 33 | ================= |
| 34 | |
| 35 | Pigweed documentation files are written in `reStructuredText`_ format and |
| 36 | rendered to HTML using `Sphinx`_ through Pigweed's GN build system. |
| 37 | |
| 38 | .. _reStructuredText: http://docutils.sourceforge.net/rst.html |
Rob Mohr | 7e70000 | 2021-05-22 10:56:54 -0700 | [diff] [blame] | 39 | .. inclusive-language: ignore |
Alexei Frolov | 8ffcb91 | 2019-11-18 11:00:20 -0800 | [diff] [blame] | 40 | .. _Sphinx: http://www.sphinx-doc.org/en/master |
| 41 | |
Anthony DiGirolamo | 389664e | 2021-05-06 16:57:20 -0700 | [diff] [blame] | 42 | There are additonal Sphinx plugins used for rendering diagrams within |
| 43 | reStructuredText files including: |
| 44 | |
Anthony DiGirolamo | 389664e | 2021-05-06 16:57:20 -0700 | [diff] [blame] | 45 | * `mermaid <https://mermaid-js.github.io/>`_ via the `sphinxcontrib-mermaid |
| 46 | <https://pypi.org/project/sphinxcontrib-mermaid/>`_ package. |
| 47 | |
Alexei Frolov | 8ffcb91 | 2019-11-18 11:00:20 -0800 | [diff] [blame] | 48 | Documentation source and asset files are placed alongside code within a module |
Adam MacBeth | d535359 | 2021-07-21 18:02:44 +0000 | [diff] [blame] | 49 | and registered as a ``pw_doc_group`` target within a ``BUILD.gn`` file. These |
Alexei Frolov | 8ffcb91 | 2019-11-18 11:00:20 -0800 | [diff] [blame] | 50 | groups become available for import within a special documentation generation |
| 51 | target, which accumulates all of them and renders the resulting HTML. This |
| 52 | system can either be used directly within Pigweed, or integrated into a |
| 53 | downstream project. |
| 54 | |
Chad Norvell | 9357f33 | 2022-02-18 12:07:24 -0800 | [diff] [blame] | 55 | GN Templates |
Alexei Frolov | 8ffcb91 | 2019-11-18 11:00:20 -0800 | [diff] [blame] | 56 | ------------ |
| 57 | |
| 58 | pw_doc_group |
| 59 | ____________ |
Alexei Frolov | 8ffcb91 | 2019-11-18 11:00:20 -0800 | [diff] [blame] | 60 | The main template for defining documentation files is ``pw_doc_group``. It is |
| 61 | used to logically group a collection of documentation source files and assets. |
| 62 | Each Pigweed module is expected to provide at least one ``pw_doc_group`` target |
| 63 | defining the module's documentation. A ``pw_doc_group`` can depend on other |
| 64 | groups, 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 Frolov | 0f98763 | 2022-08-09 16:23:03 +0000 | [diff] [blame] | 72 | * ``report_deps``: Report card generating targets (e.g. ``pw_size_diff``) on |
Alexei Frolov | 8ffcb91 | 2019-11-18 11:00:20 -0800 | [diff] [blame] | 73 | 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 | |
| 86 | pw_doc_gen |
| 87 | __________ |
Alexei Frolov | 8ffcb91 | 2019-11-18 11:00:20 -0800 | [diff] [blame] | 88 | The ``pw_doc_gen`` template creates a target which renders complete HTML |
| 89 | documentation for a project. It depends on registered ``pw_doc_group`` targets |
| 90 | and creates an action which collects and renders them. |
| 91 | |
| 92 | To generate the complete docs, the template also requires a ``conf.py`` file |
| 93 | configuring Sphinx's output, and a top level ``index.rst`` for the main page of |
| 94 | the documentation. These are added at the root level of the built documentation |
| 95 | to 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 Norvell | 9357f33 | 2022-02-18 12:07:24 -0800 | [diff] [blame] | 117 | Generating Documentation |
Alexei Frolov | 8ffcb91 | 2019-11-18 11:00:20 -0800 | [diff] [blame] | 118 | ------------------------ |
Alexei Frolov | 8ffcb91 | 2019-11-18 11:00:20 -0800 | [diff] [blame] | 119 | All source files listed under a ``pw_doc_gen`` target and its ``pw_doc_group`` |
| 120 | dependencies get copied out into a directory structure mirroring the original |
| 121 | layout of the modules in which the sources appear. This is demonstrated below |
| 122 | using a subset of Pigweed's core documentation. |
| 123 | |
| 124 | Consider 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 | |
| 139 | A documentation tree is created under the output directory. Each of the sources |
| 140 | and inputs in the target's dependency graph is copied under this tree in the |
| 141 | same directory structure as they appear under the root GN build directory |
| 142 | (``$dir_pigweed`` in this case). The ``conf.py`` and ``index.rst`` provided |
| 143 | directly 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 Montanez | 5464d5f | 2020-02-20 10:12:20 -0800 | [diff] [blame] | 151 | │ ├── bloat.rst |
| 152 | │ └── examples |
| 153 | │ └── simple_bloat.rst |
Alexei Frolov | 8ffcb91 | 2019-11-18 11:00:20 -0800 | [diff] [blame] | 154 | ├── pw_docgen |
Armando Montanez | 5464d5f | 2020-02-20 10:12:20 -0800 | [diff] [blame] | 155 | │ └── docgen.rst |
Alexei Frolov | 8ffcb91 | 2019-11-18 11:00:20 -0800 | [diff] [blame] | 156 | └── pw_preprocessor |
| 157 | └── docs.rst |
| 158 | |
| 159 | This is the documentation tree which gets passed to Sphinx to build HTML output. |
| 160 | Imports within documentation files must be relative to this structure. In |
| 161 | practice, relative imports from within modules' documentation groups are |
| 162 | identical to the project's directory structure. The only special case is the |
| 163 | top-level ``index.rst`` file's imports; they must start from the project's build |
| 164 | root. |
Chad Norvell | 9357f33 | 2022-02-18 12:07:24 -0800 | [diff] [blame] | 165 | |
| 166 | Sphinx Extensions |
| 167 | ================= |
| 168 | This module houses Pigweed-specific extensions for the Sphinx documentation |
| 169 | generator. Extensions are included and configured in ``docs/conf.py``. |
| 170 | |
Chad Norvell | 354824c | 2023-03-08 02:23:25 +0000 | [diff] [blame] | 171 | module_metadata |
| 172 | --------------- |
| 173 | Per :ref:`SEED-0102 <seed-0102>`, Pigweed module documentation has a standard |
| 174 | format. The ``pigweed-module`` Sphinx directive provides that format and |
| 175 | registers module metadata that can be used elsewhere in the Sphinx build. |
| 176 | |
| 177 | We need to add the directive after the document title, and add a class *to* |
| 178 | the document title to achieve the title & subtitle formatting. Here's an |
| 179 | example: |
| 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 | |
| 202 | Directive 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 Norvell | 9357f33 | 2022-02-18 12:07:24 -0800 | [diff] [blame] | 217 | google_analytics |
| 218 | ---------------- |
| 219 | When this extension is included and a ``google_analytics_id`` is set in the |
| 220 | Sphinx configuration, a Google Analytics tracking tag will be added to each |
| 221 | page of the documentation when it is rendered to HTML. |
| 222 | |
| 223 | By default, the Sphinx configuration's ``google_analytics_id`` is set |
| 224 | automatically based on the value of the GN argument |
| 225 | ``pw_docs_google_analytics_id``, allowing you to control whether tracking is |
| 226 | enabled or not in your build configuration. Typically, you would only enable |
| 227 | this for documentation builds intended for deployment on the web. |