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 | |
| 23 | Documentation overview |
| 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 | |
| 32 | Build integration |
| 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 | |
| 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 Frolov | 8ffcb91 | 2019-11-18 11:00:20 -0800 | [diff] [blame] | 56 | Documentation source and asset files are placed alongside code within a module |
| 57 | and registered as a ``pw_docgen_group`` target within a ``BUILD.gn`` file. These |
| 58 | groups become available for import within a special documentation generation |
| 59 | target, which accumulates all of them and renders the resulting HTML. This |
| 60 | system can either be used directly within Pigweed, or integrated into a |
| 61 | downstream project. |
| 62 | |
| 63 | GN templates |
| 64 | ------------ |
| 65 | |
| 66 | pw_doc_group |
| 67 | ____________ |
| 68 | |
| 69 | The main template for defining documentation files is ``pw_doc_group``. It is |
| 70 | used to logically group a collection of documentation source files and assets. |
| 71 | Each Pigweed module is expected to provide at least one ``pw_doc_group`` target |
| 72 | defining the module's documentation. A ``pw_doc_group`` can depend on other |
| 73 | groups, 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 | |
| 95 | pw_doc_gen |
| 96 | __________ |
| 97 | |
| 98 | The ``pw_doc_gen`` template creates a target which renders complete HTML |
| 99 | documentation for a project. It depends on registered ``pw_doc_group`` targets |
| 100 | and creates an action which collects and renders them. |
| 101 | |
| 102 | To generate the complete docs, the template also requires a ``conf.py`` file |
| 103 | configuring Sphinx's output, and a top level ``index.rst`` for the main page of |
| 104 | the documentation. These are added at the root level of the built documentation |
| 105 | to 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 | |
| 127 | Generating documentation |
| 128 | ------------------------ |
| 129 | |
| 130 | All source files listed under a ``pw_doc_gen`` target and its ``pw_doc_group`` |
| 131 | dependencies get copied out into a directory structure mirroring the original |
| 132 | layout of the modules in which the sources appear. This is demonstrated below |
| 133 | using a subset of Pigweed's core documentation. |
| 134 | |
| 135 | Consider 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 | |
| 150 | A documentation tree is created under the output directory. Each of the sources |
| 151 | and inputs in the target's dependency graph is copied under this tree in the |
| 152 | same directory structure as they appear under the root GN build directory |
| 153 | (``$dir_pigweed`` in this case). The ``conf.py`` and ``index.rst`` provided |
| 154 | directly 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 Montanez | 5464d5f | 2020-02-20 10:12:20 -0800 | [diff] [blame] | 162 | │ ├── bloat.rst |
| 163 | │ └── examples |
| 164 | │ └── simple_bloat.rst |
Alexei Frolov | 8ffcb91 | 2019-11-18 11:00:20 -0800 | [diff] [blame] | 165 | ├── pw_docgen |
Armando Montanez | 5464d5f | 2020-02-20 10:12:20 -0800 | [diff] [blame] | 166 | │ └── docgen.rst |
Alexei Frolov | 8ffcb91 | 2019-11-18 11:00:20 -0800 | [diff] [blame] | 167 | └── pw_preprocessor |
| 168 | └── docs.rst |
| 169 | |
| 170 | This is the documentation tree which gets passed to Sphinx to build HTML output. |
| 171 | Imports within documentation files must be relative to this structure. In |
| 172 | practice, relative imports from within modules' documentation groups are |
| 173 | identical to the project's directory structure. The only special case is the |
| 174 | top-level ``index.rst`` file's imports; they must start from the project's build |
| 175 | root. |