1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_docgen: 2*61c4878aSAndroid Build Coastguard Worker 3*61c4878aSAndroid Build Coastguard Worker========= 4*61c4878aSAndroid Build Coastguard Workerpw_docgen 5*61c4878aSAndroid Build Coastguard Worker========= 6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module:: 7*61c4878aSAndroid Build Coastguard Worker :name: pw_docgen 8*61c4878aSAndroid Build Coastguard Worker 9*61c4878aSAndroid Build Coastguard Worker``pw_docgen`` provides tools to generate documentation for Pigweed-based 10*61c4878aSAndroid Build Coastguard Workerprojects. 11*61c4878aSAndroid Build Coastguard Worker 12*61c4878aSAndroid Build Coastguard Worker.. note:: 13*61c4878aSAndroid Build Coastguard Worker 14*61c4878aSAndroid Build Coastguard Worker Pigweed itself uses ``pw_docgen`` to generate ``pigweed.dev``. 15*61c4878aSAndroid Build Coastguard Worker 16*61c4878aSAndroid Build Coastguard Worker-------- 17*61c4878aSAndroid Build Coastguard WorkerOverview 18*61c4878aSAndroid Build Coastguard Worker-------- 19*61c4878aSAndroid Build Coastguard WorkerPigweed-based projects typically use a subset of Pigweed's modules and add their 20*61c4878aSAndroid Build Coastguard Workerown product-specific modules on top of that, which may have product-specific 21*61c4878aSAndroid Build Coastguard Workerdocumentation. ``pw_docgen`` provides a convenient way to combine all of the 22*61c4878aSAndroid Build Coastguard Workerrelevant documentation for a project into one place, allowing downstream 23*61c4878aSAndroid Build Coastguard Workerconsumers of release bundles (e.g. factory teams, QA teams, beta testers, etc.) 24*61c4878aSAndroid Build Coastguard Workerto have a unified source of documentation early on. 25*61c4878aSAndroid Build Coastguard Worker 26*61c4878aSAndroid Build Coastguard WorkerThe documentation generation is integrated directly into the build system. Any 27*61c4878aSAndroid Build Coastguard Workerbuild target can depend on documentation, which allows it to be included as part 28*61c4878aSAndroid Build Coastguard Workerof a factory release build, for example. Additionally, documentation itself can 29*61c4878aSAndroid Build Coastguard Workerdepend on other build targets, such as :ref:`report cards <module-pw_bloat>` for 30*61c4878aSAndroid Build Coastguard Workerbinary size/profiling. Any time the code is changed, documentation will be 31*61c4878aSAndroid Build Coastguard Workerregenerated with the updated reports. 32*61c4878aSAndroid Build Coastguard Worker 33*61c4878aSAndroid Build Coastguard WorkerEach Pigweed module provides documentation describing its functionality, use 34*61c4878aSAndroid Build Coastguard Workercases, and programming API. 35*61c4878aSAndroid Build Coastguard Worker 36*61c4878aSAndroid Build Coastguard WorkerIncluded in a module's documentation are report cards which show an overview of 37*61c4878aSAndroid Build Coastguard Workerthe module's size cost and performance benchmarks. These allow prospective users 38*61c4878aSAndroid Build Coastguard Workerto evaluate the impact of including the module in their projects. 39*61c4878aSAndroid Build Coastguard Worker 40*61c4878aSAndroid Build Coastguard Worker----------------- 41*61c4878aSAndroid Build Coastguard WorkerBuild integration 42*61c4878aSAndroid Build Coastguard Worker----------------- 43*61c4878aSAndroid Build Coastguard WorkerPigweed documentation files are written in `reStructuredText`_ format and 44*61c4878aSAndroid Build Coastguard Workerrendered to HTML using `Sphinx`_ through Pigweed's GN build system. 45*61c4878aSAndroid Build Coastguard Worker 46*61c4878aSAndroid Build Coastguard Worker.. _reStructuredText: http://docutils.sourceforge.net/rst.html 47*61c4878aSAndroid Build Coastguard Worker.. inclusive-language: ignore 48*61c4878aSAndroid Build Coastguard Worker.. _Sphinx: http://www.sphinx-doc.org/en/master 49*61c4878aSAndroid Build Coastguard Worker 50*61c4878aSAndroid Build Coastguard WorkerThere are additonal Sphinx plugins used for rendering diagrams within 51*61c4878aSAndroid Build Coastguard WorkerreStructuredText files including: 52*61c4878aSAndroid Build Coastguard Worker 53*61c4878aSAndroid Build Coastguard Worker* `mermaid <https://mermaid-js.github.io/>`_ via the `sphinxcontrib-mermaid 54*61c4878aSAndroid Build Coastguard Worker <https://pypi.org/project/sphinxcontrib-mermaid/>`_ package. 55*61c4878aSAndroid Build Coastguard Worker 56*61c4878aSAndroid Build Coastguard WorkerDocumentation source and asset files are placed alongside code within a module 57*61c4878aSAndroid Build Coastguard Workerand registered as a ``pw_doc_group`` target within a ``BUILD.gn`` file. These 58*61c4878aSAndroid Build Coastguard Workergroups become available for import within a special documentation generation 59*61c4878aSAndroid Build Coastguard Workertarget, which accumulates all of them and renders the resulting HTML. This 60*61c4878aSAndroid Build Coastguard Workersystem can either be used directly within Pigweed, or integrated into a 61*61c4878aSAndroid Build Coastguard Workerdownstream project. 62*61c4878aSAndroid Build Coastguard Worker 63*61c4878aSAndroid Build Coastguard WorkerGN templates 64*61c4878aSAndroid Build Coastguard Worker============ 65*61c4878aSAndroid Build Coastguard Worker 66*61c4878aSAndroid Build Coastguard Workerpw_doc_group 67*61c4878aSAndroid Build Coastguard Worker------------ 68*61c4878aSAndroid Build Coastguard WorkerThe main template for defining documentation files is ``pw_doc_group``. It is 69*61c4878aSAndroid Build Coastguard Workerused to logically group a collection of documentation source files and assets. 70*61c4878aSAndroid Build Coastguard WorkerEach Pigweed module is expected to provide at least one ``pw_doc_group`` target 71*61c4878aSAndroid Build Coastguard Workerdefining the module's documentation. A ``pw_doc_group`` can depend on other 72*61c4878aSAndroid Build Coastguard Workergroups, causing them to be built with it. 73*61c4878aSAndroid Build Coastguard Worker 74*61c4878aSAndroid Build Coastguard WorkerArguments 75*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^ 76*61c4878aSAndroid Build Coastguard Worker* ``sources``: RST documentation source files. 77*61c4878aSAndroid Build Coastguard Worker* ``inputs``: Additional resources required for the docs (images, data files, 78*61c4878aSAndroid Build Coastguard Worker etc.) 79*61c4878aSAndroid Build Coastguard Worker* ``group_deps``: Other ``pw_doc_group`` targets required by this one. 80*61c4878aSAndroid Build Coastguard Worker* ``report_deps``: Report card generating targets (e.g. ``pw_size_diff``) on 81*61c4878aSAndroid Build Coastguard Worker which the docs depend. 82*61c4878aSAndroid Build Coastguard Worker* ``other_deps``: Any other GN targets that should be run before this 83*61c4878aSAndroid Build Coastguard Worker ``pw_doc_group`` runs that is not included in one of the above ``dep`` 84*61c4878aSAndroid Build Coastguard Worker categories. 85*61c4878aSAndroid Build Coastguard Worker 86*61c4878aSAndroid Build Coastguard WorkerExample 87*61c4878aSAndroid Build Coastguard Worker^^^^^^^ 88*61c4878aSAndroid Build Coastguard Worker.. code-block:: 89*61c4878aSAndroid Build Coastguard Worker 90*61c4878aSAndroid Build Coastguard Worker pw_doc_group("my_doc_group") { 91*61c4878aSAndroid Build Coastguard Worker sources = [ "docs.rst" ] 92*61c4878aSAndroid Build Coastguard Worker inputs = [ "face-with-tears-of-joy-emoji.svg" ] 93*61c4878aSAndroid Build Coastguard Worker group_deps = [ ":sub_doc_group" ] 94*61c4878aSAndroid Build Coastguard Worker report_deps = [ ":my_size_report" ] 95*61c4878aSAndroid Build Coastguard Worker } 96*61c4878aSAndroid Build Coastguard Worker 97*61c4878aSAndroid Build Coastguard Workerpw_doc_gen 98*61c4878aSAndroid Build Coastguard Worker---------- 99*61c4878aSAndroid Build Coastguard WorkerThe ``pw_doc_gen`` template creates a target which renders complete HTML 100*61c4878aSAndroid Build Coastguard Workerdocumentation for a project. It depends on registered ``pw_doc_group`` targets 101*61c4878aSAndroid Build Coastguard Workerand creates an action which collects and renders them. 102*61c4878aSAndroid Build Coastguard Worker 103*61c4878aSAndroid Build Coastguard WorkerTo generate the complete docs, the template also requires a ``conf.py`` file 104*61c4878aSAndroid Build Coastguard Workerconfiguring Sphinx's output, and a top level ``index.rst`` for the main page of 105*61c4878aSAndroid Build Coastguard Workerthe documentation. These are added at the root level of the built documentation 106*61c4878aSAndroid Build Coastguard Workerto tie everything together. 107*61c4878aSAndroid Build Coastguard Worker 108*61c4878aSAndroid Build Coastguard WorkerArguments 109*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^ 110*61c4878aSAndroid Build Coastguard Worker* ``conf``: Path to the ``conf.py`` to use for Sphinx. 111*61c4878aSAndroid Build Coastguard Worker* ``index``: Path to the top-level ``index.rst`` file. 112*61c4878aSAndroid Build Coastguard Worker* ``output_directory``: Directory in which to render HTML output. 113*61c4878aSAndroid Build Coastguard Worker* ``deps``: List of all ``pw_doc_group`` targets required for the documentation. 114*61c4878aSAndroid Build Coastguard Worker* ``python_metadata_deps``: Python-related dependencies that are only used as 115*61c4878aSAndroid Build Coastguard Worker deps for generating Python package metadata list, not the overall 116*61c4878aSAndroid Build Coastguard Worker documentation generation. This should rarely be used by non-Pigweed code. 117*61c4878aSAndroid Build Coastguard Worker 118*61c4878aSAndroid Build Coastguard WorkerExample 119*61c4878aSAndroid Build Coastguard Worker^^^^^^^ 120*61c4878aSAndroid Build Coastguard Worker.. code-block:: 121*61c4878aSAndroid Build Coastguard Worker 122*61c4878aSAndroid Build Coastguard Worker pw_doc_gen("my_docs") { 123*61c4878aSAndroid Build Coastguard Worker conf = "//my_docs/conf.py" 124*61c4878aSAndroid Build Coastguard Worker index = "//my_docs/index.rst" 125*61c4878aSAndroid Build Coastguard Worker output_directory = target_gen_dir 126*61c4878aSAndroid Build Coastguard Worker deps = [ 127*61c4878aSAndroid Build Coastguard Worker "//my_module:my_doc_group", 128*61c4878aSAndroid Build Coastguard Worker ] 129*61c4878aSAndroid Build Coastguard Worker } 130*61c4878aSAndroid Build Coastguard Worker 131*61c4878aSAndroid Build Coastguard WorkerGenerating documentation 132*61c4878aSAndroid Build Coastguard Worker------------------------ 133*61c4878aSAndroid Build Coastguard WorkerAll source files listed under a ``pw_doc_gen`` target and its ``pw_doc_group`` 134*61c4878aSAndroid Build Coastguard Workerdependencies get copied out into a directory structure mirroring the original 135*61c4878aSAndroid Build Coastguard Workerlayout of the modules in which the sources appear. This is demonstrated below 136*61c4878aSAndroid Build Coastguard Workerusing a subset of Pigweed's core documentation. 137*61c4878aSAndroid Build Coastguard Worker 138*61c4878aSAndroid Build Coastguard WorkerConsider the following target in ``$dir_pigweed/docs/BUILD.gn``: 139*61c4878aSAndroid Build Coastguard Worker 140*61c4878aSAndroid Build Coastguard Worker.. code-block:: 141*61c4878aSAndroid Build Coastguard Worker 142*61c4878aSAndroid Build Coastguard Worker pw_doc_gen("docs") { 143*61c4878aSAndroid Build Coastguard Worker conf = "conf.py" 144*61c4878aSAndroid Build Coastguard Worker index = "index.rst" 145*61c4878aSAndroid Build Coastguard Worker output_directory = target_gen_dir 146*61c4878aSAndroid Build Coastguard Worker deps = [ 147*61c4878aSAndroid Build Coastguard Worker "$dir_pw_bloat:docs", 148*61c4878aSAndroid Build Coastguard Worker "$dir_pw_docgen:docs", 149*61c4878aSAndroid Build Coastguard Worker "$dir_pw_preprocessor:docs", 150*61c4878aSAndroid Build Coastguard Worker ] 151*61c4878aSAndroid Build Coastguard Worker } 152*61c4878aSAndroid Build Coastguard Worker 153*61c4878aSAndroid Build Coastguard WorkerA documentation tree is created under the output directory. Each of the sources 154*61c4878aSAndroid Build Coastguard Workerand inputs in the target's dependency graph is copied under this tree in the 155*61c4878aSAndroid Build Coastguard Workersame directory structure as they appear under the root GN build directory 156*61c4878aSAndroid Build Coastguard Worker(``$dir_pigweed`` in this case). The ``conf.py`` and ``index.rst`` provided 157*61c4878aSAndroid Build Coastguard Workerdirectly to the ``pw_doc_gen`` template are copied in at the root of the tree. 158*61c4878aSAndroid Build Coastguard Worker 159*61c4878aSAndroid Build Coastguard Worker.. code-block:: 160*61c4878aSAndroid Build Coastguard Worker 161*61c4878aSAndroid Build Coastguard Worker out/gen/docs/pw_docgen_tree/ 162*61c4878aSAndroid Build Coastguard Worker ├── conf.py 163*61c4878aSAndroid Build Coastguard Worker ├── index.rst 164*61c4878aSAndroid Build Coastguard Worker ├── pw_bloat 165*61c4878aSAndroid Build Coastguard Worker │ ├── bloat.rst 166*61c4878aSAndroid Build Coastguard Worker │ └── examples 167*61c4878aSAndroid Build Coastguard Worker │ └── simple_bloat.rst 168*61c4878aSAndroid Build Coastguard Worker ├── pw_docgen 169*61c4878aSAndroid Build Coastguard Worker │ └── docgen.rst 170*61c4878aSAndroid Build Coastguard Worker └── pw_preprocessor 171*61c4878aSAndroid Build Coastguard Worker └── docs.rst 172*61c4878aSAndroid Build Coastguard Worker 173*61c4878aSAndroid Build Coastguard WorkerThis is the documentation tree which gets passed to Sphinx to build HTML output. 174*61c4878aSAndroid Build Coastguard WorkerImports within documentation files must be relative to this structure. In 175*61c4878aSAndroid Build Coastguard Workerpractice, relative imports from within modules' documentation groups are 176*61c4878aSAndroid Build Coastguard Workeridentical to the project's directory structure. The only special case is the 177*61c4878aSAndroid Build Coastguard Workertop-level ``index.rst`` file's imports; they must start from the project's build 178*61c4878aSAndroid Build Coastguard Workerroot. 179*61c4878aSAndroid Build Coastguard Worker 180*61c4878aSAndroid Build Coastguard WorkerViewing documentation 181*61c4878aSAndroid Build Coastguard Worker--------------------- 182*61c4878aSAndroid Build Coastguard Worker``pw_docgen`` includes a web server that serves locally-generated documentation 183*61c4878aSAndroid Build Coastguard Workerat ``pw_docgen.docserver``. It supports hot-reloading, so the rendered docs in 184*61c4878aSAndroid Build Coastguard Workeryour browser will refresh as you make changes to the source files. 185*61c4878aSAndroid Build Coastguard Worker 186*61c4878aSAndroid Build Coastguard WorkerIn most cases, you will not need to run the docs server directly. Instead, it 187*61c4878aSAndroid Build Coastguard Workerwill be run via :ref:`module-pw_watch`. 188*61c4878aSAndroid Build Coastguard Worker 189*61c4878aSAndroid Build Coastguard Worker----------------------------- 190*61c4878aSAndroid Build Coastguard Workerpigweed.dev Sphinx extensions 191*61c4878aSAndroid Build Coastguard Worker----------------------------- 192*61c4878aSAndroid Build Coastguard Worker.. note:: The topics in this section only apply to ``pigweed.dev``. 193*61c4878aSAndroid Build Coastguard Worker This section isn't relevant to downstream users of ``pw_docgen``. 194*61c4878aSAndroid Build Coastguard Worker 195*61c4878aSAndroid Build Coastguard WorkerThis module houses Pigweed-specific extensions for the Sphinx documentation 196*61c4878aSAndroid Build Coastguard Workergenerator. Extensions are included and configured in ``docs/conf.py``. 197*61c4878aSAndroid Build Coastguard Worker 198*61c4878aSAndroid Build Coastguard Workermodule_metadata 199*61c4878aSAndroid Build Coastguard Worker=============== 200*61c4878aSAndroid Build Coastguard WorkerSee :ref:`docs-contrib-docs-modules-metadata`. 201*61c4878aSAndroid Build Coastguard Worker 202*61c4878aSAndroid Build Coastguard WorkerCanonical URL configuration 203*61c4878aSAndroid Build Coastguard Worker--------------------------- 204*61c4878aSAndroid Build Coastguard Worker``module_metadata`` fixes the canonical URLs for ``*/docs.html`` pages. By 205*61c4878aSAndroid Build Coastguard Workerdefault Sphinx assumes that a page's canonical URL is its full URL. E.g. the 206*61c4878aSAndroid Build Coastguard Workerdefault canonical URL for ``//pw_string/docs.rst`` is 207*61c4878aSAndroid Build Coastguard Worker``https://pigweed.dev/pw_string/docs.html``. The ``pigweed.dev`` 208*61c4878aSAndroid Build Coastguard Workerserver treats ``https://pigweed.dev/pw_string/`` as the canonical URL however. 209*61c4878aSAndroid Build Coastguard WorkerThis problem is not limited to module homepages; it occurs on any page that 210*61c4878aSAndroid Build Coastguard Workerends in ``/docs.html`` such as 211*61c4878aSAndroid Build Coastguard Worker``https://pigweed.dev/third_party/emboss/docs.html``. ``module_metadata`` fixes 212*61c4878aSAndroid Build Coastguard Workerthis problem by ensuring that the ``<link rel="canonical" href="..."/>`` tag 213*61c4878aSAndroid Build Coastguard Workergenerated in the HTML is aligned with the server's configuration. 214*61c4878aSAndroid Build Coastguard Worker 215*61c4878aSAndroid Build Coastguard WorkerAfter building the docs, the canonical URLs for all HTML pages can be verified 216*61c4878aSAndroid Build Coastguard Workerby running the following command in a terminal from the root directory of the 217*61c4878aSAndroid Build Coastguard Workerupstream Pigweed repo: 218*61c4878aSAndroid Build Coastguard Worker 219*61c4878aSAndroid Build Coastguard Worker.. code-block:: console 220*61c4878aSAndroid Build Coastguard Worker 221*61c4878aSAndroid Build Coastguard Worker grep '<link rel="canonical' out/docs/gen/docs/html/* -R 222*61c4878aSAndroid Build Coastguard Worker 223*61c4878aSAndroid Build Coastguard WorkerContext: :bug:`323077749`. 224*61c4878aSAndroid Build Coastguard Worker 225*61c4878aSAndroid Build Coastguard Workerbug 226*61c4878aSAndroid Build Coastguard Worker--- 227*61c4878aSAndroid Build Coastguard WorkerThis extension simplifies adding references to issues (bugs) in the Pigweed 228*61c4878aSAndroid Build Coastguard Workerissue tracker. It defines a `Docutils role 229*61c4878aSAndroid Build Coastguard Worker<https://docutils.sourceforge.io/docs/ref/rst/roles.html>`__ that can be 230*61c4878aSAndroid Build Coastguard Workerused as follows. 231*61c4878aSAndroid Build Coastguard Worker 232*61c4878aSAndroid Build Coastguard Worker.. code-block:: rst 233*61c4878aSAndroid Build Coastguard Worker 234*61c4878aSAndroid Build Coastguard Worker For more details see :bug:`323077749`. 235*61c4878aSAndroid Build Coastguard Worker 236*61c4878aSAndroid Build Coastguard WorkerThis becomes a hyperlink to https://pwbug.dev/323077749. 237*61c4878aSAndroid Build Coastguard Worker 238*61c4878aSAndroid Build Coastguard Workergoogle_analytics 239*61c4878aSAndroid Build Coastguard Worker---------------- 240*61c4878aSAndroid Build Coastguard WorkerWhen this extension is included and a ``google_analytics_id`` is set in the 241*61c4878aSAndroid Build Coastguard WorkerSphinx configuration, a Google Analytics tracking tag will be added to each 242*61c4878aSAndroid Build Coastguard Workerpage of the documentation when it is rendered to HTML. 243*61c4878aSAndroid Build Coastguard Worker 244*61c4878aSAndroid Build Coastguard WorkerBy default, the Sphinx configuration's ``google_analytics_id`` is set 245*61c4878aSAndroid Build Coastguard Workerautomatically based on the value of the GN argument 246*61c4878aSAndroid Build Coastguard Worker``pw_docs_google_analytics_id``, allowing you to control whether tracking is 247*61c4878aSAndroid Build Coastguard Workerenabled or not in your build configuration. Typically, you would only enable 248*61c4878aSAndroid Build Coastguard Workerthis for documentation builds intended for deployment on the web. 249*61c4878aSAndroid Build Coastguard Worker 250*61c4878aSAndroid Build Coastguard WorkerDebugging Pigweed's Sphinx extensions 251*61c4878aSAndroid Build Coastguard Worker------------------------------------- 252*61c4878aSAndroid Build Coastguard WorkerTo step through your Pigweed extension code with 253*61c4878aSAndroid Build Coastguard Worker`pdb <https://docs.python.org/3/library/pdb.html>`_: 254*61c4878aSAndroid Build Coastguard Worker 255*61c4878aSAndroid Build Coastguard Worker#. Set a breakpoint in your extension code: 256*61c4878aSAndroid Build Coastguard Worker 257*61c4878aSAndroid Build Coastguard Worker .. code-block:: 258*61c4878aSAndroid Build Coastguard Worker 259*61c4878aSAndroid Build Coastguard Worker breakpoint() 260*61c4878aSAndroid Build Coastguard Worker 261*61c4878aSAndroid Build Coastguard Worker#. Build ``python.install`` to install the code change into the bootstrap venv 262*61c4878aSAndroid Build Coastguard Worker (``environment/pigweed-venv/lib/python3.8/site-packages/pw_docgen``): 263*61c4878aSAndroid Build Coastguard Worker 264*61c4878aSAndroid Build Coastguard Worker .. code-block:: 265*61c4878aSAndroid Build Coastguard Worker 266*61c4878aSAndroid Build Coastguard Worker ninja -C out python.install 267*61c4878aSAndroid Build Coastguard Worker 268*61c4878aSAndroid Build Coastguard Worker#. Manually invoke Sphinx to build the docs and trigger your breakpoint: 269*61c4878aSAndroid Build Coastguard Worker 270*61c4878aSAndroid Build Coastguard Worker .. code-block:: 271*61c4878aSAndroid Build Coastguard Worker 272*61c4878aSAndroid Build Coastguard Worker cd out 273*61c4878aSAndroid Build Coastguard Worker sphinx-build -W -b html -d docs/gen/docs/help docs/gen/docs/pw_docgen_tree docs/gen/docs/html -v -v -v 274*61c4878aSAndroid Build Coastguard Worker 275*61c4878aSAndroid Build Coastguard Worker You should see build output from Sphinx. The build should pause at your 276*61c4878aSAndroid Build Coastguard Worker breakpoint and you should then see pdb's prompt (``(Pdb)``). 277