1.. _module-pw_docgen: 2 3--------- 4pw_docgen 5--------- 6The 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 39.. inclusive-language: ignore 40.. _Sphinx: http://www.sphinx-doc.org/en/master 41 42There are additonal Sphinx plugins used for rendering diagrams within 43reStructuredText files including: 44 45* `mermaid <https://mermaid-js.github.io/>`_ via the `sphinxcontrib-mermaid 46 <https://pypi.org/project/sphinxcontrib-mermaid/>`_ package. 47 48Documentation source and asset files are placed alongside code within a module 49and registered as a ``pw_doc_group`` target within a ``BUILD.gn`` file. These 50groups 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 55GN Templates 56------------ 57 58pw_doc_group 59____________ 60The 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. 72* ``report_deps``: Report card generating targets (e.g. ``pw_size_diff``) on 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 86pw_doc_gen 87__________ 88The ``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 117Generating Documentation 118------------------------ 119All 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 151 │ ├── bloat.rst 152 │ └── examples 153 │ └── simple_bloat.rst 154 ├── pw_docgen 155 │ └── docgen.rst 156 └── 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. 165 166Sphinx Extensions 167================= 168This module houses Pigweed-specific extensions for the Sphinx documentation 169generator. Extensions are included and configured in ``docs/conf.py``. 170 171module_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 217google_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. 228