1*61c4878aSAndroid Build Coastguard Worker.. _docs-pw-style: 2*61c4878aSAndroid Build Coastguard Worker 3*61c4878aSAndroid Build Coastguard Worker============ 4*61c4878aSAndroid Build Coastguard WorkerStyle guides 5*61c4878aSAndroid Build Coastguard Worker============ 6*61c4878aSAndroid Build Coastguard Worker.. tip:: 7*61c4878aSAndroid Build Coastguard Worker 8*61c4878aSAndroid Build Coastguard Worker Pigweed runs ``pw format`` as part of ``pw presubmit`` to perform some code 9*61c4878aSAndroid Build Coastguard Worker formatting checks. To speed up the review process, consider adding ``pw 10*61c4878aSAndroid Build Coastguard Worker presubmit`` as a git push hook using the following command: 11*61c4878aSAndroid Build Coastguard Worker ``pw presubmit --install`` 12*61c4878aSAndroid Build Coastguard Worker 13*61c4878aSAndroid Build Coastguard Worker.. grid:: 2 14*61c4878aSAndroid Build Coastguard Worker 15*61c4878aSAndroid Build Coastguard Worker .. grid-item-card:: :octicon:`diff-added` C++ style 16*61c4878aSAndroid Build Coastguard Worker :link: docs-pw-style-cpp 17*61c4878aSAndroid Build Coastguard Worker :link-type: ref 18*61c4878aSAndroid Build Coastguard Worker :class-item: sales-pitch-cta-primary 19*61c4878aSAndroid Build Coastguard Worker 20*61c4878aSAndroid Build Coastguard Worker Our C++ style guide: an extension of the Google C++ style with further 21*61c4878aSAndroid Build Coastguard Worker restrictions and guidance for embedded 22*61c4878aSAndroid Build Coastguard Worker 23*61c4878aSAndroid Build Coastguard Worker 24*61c4878aSAndroid Build Coastguard Worker .. grid-item-card:: :octicon:`pencil` Commit messages 25*61c4878aSAndroid Build Coastguard Worker :link: docs-pw-style-commit-message 26*61c4878aSAndroid Build Coastguard Worker :link-type: ref 27*61c4878aSAndroid Build Coastguard Worker :class-item: sales-pitch-cta-secondary 28*61c4878aSAndroid Build Coastguard Worker 29*61c4878aSAndroid Build Coastguard Worker How to format commit messages for Pigweed 30*61c4878aSAndroid Build Coastguard Worker 31*61c4878aSAndroid Build Coastguard Worker.. grid:: 2 32*61c4878aSAndroid Build Coastguard Worker 33*61c4878aSAndroid Build Coastguard Worker .. grid-item-card:: :octicon:`code-square` Protobuf 34*61c4878aSAndroid Build Coastguard Worker :link: docs-pw-style-protobuf 35*61c4878aSAndroid Build Coastguard Worker :link-type: ref 36*61c4878aSAndroid Build Coastguard Worker :class-item: sales-pitch-cta-secondary 37*61c4878aSAndroid Build Coastguard Worker 38*61c4878aSAndroid Build Coastguard Worker How to structure reference documentation for C++ code 39*61c4878aSAndroid Build Coastguard Worker 40*61c4878aSAndroid Build Coastguard Worker .. grid-item-card:: :octicon:`terminal` CLI style 41*61c4878aSAndroid Build Coastguard Worker :link: docs-pw-style-cli 42*61c4878aSAndroid Build Coastguard Worker :link-type: ref 43*61c4878aSAndroid Build Coastguard Worker :class-item: sales-pitch-cta-secondary 44*61c4878aSAndroid Build Coastguard Worker 45*61c4878aSAndroid Build Coastguard Worker How to style your CLI program so that it behaves 46*61c4878aSAndroid Build Coastguard Worker consistently with other Pigweed CLI programs 47*61c4878aSAndroid Build Coastguard Worker 48*61c4878aSAndroid Build Coastguard Worker-------------------------- 49*61c4878aSAndroid Build Coastguard WorkerDocumentation style guides 50*61c4878aSAndroid Build Coastguard Worker-------------------------- 51*61c4878aSAndroid Build Coastguard WorkerSee the :ref:`documentation contributors homepage <docs-contrib-docs>`. 52*61c4878aSAndroid Build Coastguard Worker 53*61c4878aSAndroid Build Coastguard Worker.. todo-check: disable 54*61c4878aSAndroid Build Coastguard Worker 55*61c4878aSAndroid Build Coastguard Worker.. _docs-pw-todo-style: 56*61c4878aSAndroid Build Coastguard Worker 57*61c4878aSAndroid Build Coastguard Worker---------- 58*61c4878aSAndroid Build Coastguard WorkerTODO style 59*61c4878aSAndroid Build Coastguard Worker---------- 60*61c4878aSAndroid Build Coastguard WorkerPigweed expects TODO annotations to adhere to the following style: 61*61c4878aSAndroid Build Coastguard Worker 62*61c4878aSAndroid Build Coastguard Worker.. todo-check: disable 63*61c4878aSAndroid Build Coastguard Worker 64*61c4878aSAndroid Build Coastguard Worker.. code-block:: py 65*61c4878aSAndroid Build Coastguard Worker 66*61c4878aSAndroid Build Coastguard Worker # TODO: https://pwbug.dev/123456789 - Some explanation of the problem at 67*61c4878aSAndroid Build Coastguard Worker # hand, which may span multiple lines if necessary. 68*61c4878aSAndroid Build Coastguard Worker 69*61c4878aSAndroid Build Coastguard Worker.. todo-check: enable 70*61c4878aSAndroid Build Coastguard Worker 71*61c4878aSAndroid Build Coastguard Worker.. admonition:: Note 72*61c4878aSAndroid Build Coastguard Worker 73*61c4878aSAndroid Build Coastguard Worker Please include the ``https://`` to make it easier for code editors to 74*61c4878aSAndroid Build Coastguard Worker identify the bugs as URLs. 75*61c4878aSAndroid Build Coastguard Worker 76*61c4878aSAndroid Build Coastguard WorkerIn Markdown docs like 77*61c4878aSAndroid Build Coastguard Worker`Rustdoc <https://doc.rust-lang.org/rustdoc/what-is-rustdoc.html>`_ 78*61c4878aSAndroid Build Coastguard Workerthe following format is preferred: 79*61c4878aSAndroid Build Coastguard Worker 80*61c4878aSAndroid Build Coastguard Worker.. todo-check: disable 81*61c4878aSAndroid Build Coastguard Worker 82*61c4878aSAndroid Build Coastguard Worker.. code-block:: rs 83*61c4878aSAndroid Build Coastguard Worker 84*61c4878aSAndroid Build Coastguard Worker //! TODO: <pwbug.dev/123456789> - Explanation. 85*61c4878aSAndroid Build Coastguard Worker 86*61c4878aSAndroid Build Coastguard Worker.. todo-check: enable 87*61c4878aSAndroid Build Coastguard Worker 88*61c4878aSAndroid Build Coastguard Worker.. admonition:: Note 89*61c4878aSAndroid Build Coastguard Worker 90*61c4878aSAndroid Build Coastguard Worker For Rustdoc, omit ``https://``. The additional Markdown syntax makes the 91*61c4878aSAndroid Build Coastguard Worker link explicit, and including ``https://`` makes the preamble disruptively 92*61c4878aSAndroid Build Coastguard Worker lengthy. 93*61c4878aSAndroid Build Coastguard Worker 94*61c4878aSAndroid Build Coastguard WorkerSome older forms are still allowed but discouraged. We allow the following 95*61c4878aSAndroid Build Coastguard Workerformats, ordered by preference with the preferred patterns at the top: 96*61c4878aSAndroid Build Coastguard Worker 97*61c4878aSAndroid Build Coastguard Worker.. todo-check: disable 98*61c4878aSAndroid Build Coastguard Worker 99*61c4878aSAndroid Build Coastguard Worker.. code-block:: py 100*61c4878aSAndroid Build Coastguard Worker 101*61c4878aSAndroid Build Coastguard Worker # TODO: https://pwbug.dev/1234 - Explanation. 102*61c4878aSAndroid Build Coastguard Worker # TODO: b/1234 - Explanation. 103*61c4878aSAndroid Build Coastguard Worker # TODO: username@ - Explanation. 104*61c4878aSAndroid Build Coastguard Worker # TODO: [email protected] - Explanation. 105*61c4878aSAndroid Build Coastguard Worker # TODO(b/1234): Explanation. 106*61c4878aSAndroid Build Coastguard Worker # TODO(username): Explanation. 107*61c4878aSAndroid Build Coastguard Worker 108*61c4878aSAndroid Build Coastguard Worker.. todo-check: enable 109*61c4878aSAndroid Build Coastguard Worker 110*61c4878aSAndroid Build Coastguard Worker.. todo-check: enable 111*61c4878aSAndroid Build Coastguard Worker 112*61c4878aSAndroid Build Coastguard Worker----------------- 113*61c4878aSAndroid Build Coastguard WorkerCopyright headers 114*61c4878aSAndroid Build Coastguard Worker----------------- 115*61c4878aSAndroid Build Coastguard WorkerEvery Pigweed file containing source code must include copyright and license 116*61c4878aSAndroid Build Coastguard Workerinformation. This includes any JS/CSS files that you might be serving out to 117*61c4878aSAndroid Build Coastguard Workerbrowsers. 118*61c4878aSAndroid Build Coastguard Worker 119*61c4878aSAndroid Build Coastguard WorkerApache header source code files that support ``//`` comments: 120*61c4878aSAndroid Build Coastguard Worker 121*61c4878aSAndroid Build Coastguard Worker.. code-block:: none 122*61c4878aSAndroid Build Coastguard Worker 123*61c4878aSAndroid Build Coastguard Worker // Copyright 2023 The Pigweed Authors 124*61c4878aSAndroid Build Coastguard Worker // 125*61c4878aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); you may not 126*61c4878aSAndroid Build Coastguard Worker // use this file except in compliance with the License. You may obtain a copy of 127*61c4878aSAndroid Build Coastguard Worker // the License at 128*61c4878aSAndroid Build Coastguard Worker // 129*61c4878aSAndroid Build Coastguard Worker // https://www.apache.org/licenses/LICENSE-2.0 130*61c4878aSAndroid Build Coastguard Worker // 131*61c4878aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software 132*61c4878aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 133*61c4878aSAndroid Build Coastguard Worker // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 134*61c4878aSAndroid Build Coastguard Worker // License for the specific language governing permissions and limitations under 135*61c4878aSAndroid Build Coastguard Worker // the License. 136*61c4878aSAndroid Build Coastguard Worker 137*61c4878aSAndroid Build Coastguard WorkerApache header for source code files that support ``#`` comments: 138*61c4878aSAndroid Build Coastguard Worker 139*61c4878aSAndroid Build Coastguard Worker.. code-block:: none 140*61c4878aSAndroid Build Coastguard Worker 141*61c4878aSAndroid Build Coastguard Worker # Copyright 2023 The Pigweed Authors 142*61c4878aSAndroid Build Coastguard Worker # 143*61c4878aSAndroid Build Coastguard Worker # Licensed under the Apache License, Version 2.0 (the "License"); you may not 144*61c4878aSAndroid Build Coastguard Worker # use this file except in compliance with the License. You may obtain a copy of 145*61c4878aSAndroid Build Coastguard Worker # the License at 146*61c4878aSAndroid Build Coastguard Worker # 147*61c4878aSAndroid Build Coastguard Worker # https://www.apache.org/licenses/LICENSE-2.0 148*61c4878aSAndroid Build Coastguard Worker # 149*61c4878aSAndroid Build Coastguard Worker # Unless required by applicable law or agreed to in writing, software 150*61c4878aSAndroid Build Coastguard Worker # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 151*61c4878aSAndroid Build Coastguard Worker # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 152*61c4878aSAndroid Build Coastguard Worker # License for the specific language governing permissions and limitations under 153*61c4878aSAndroid Build Coastguard Worker # the License. 154*61c4878aSAndroid Build Coastguard Worker 155*61c4878aSAndroid Build Coastguard Worker 156*61c4878aSAndroid Build Coastguard Worker.. _python-style: 157*61c4878aSAndroid Build Coastguard Worker 158*61c4878aSAndroid Build Coastguard Worker------------ 159*61c4878aSAndroid Build Coastguard WorkerPython style 160*61c4878aSAndroid Build Coastguard Worker------------ 161*61c4878aSAndroid Build Coastguard WorkerPigweed uses the standard Python style: PEP8, which is available on the web at 162*61c4878aSAndroid Build Coastguard Workerhttps://www.python.org/dev/peps/pep-0008/. All Pigweed Python code should pass 163*61c4878aSAndroid Build Coastguard Worker``pw format``, which invokes ``black`` with a couple options. 164*61c4878aSAndroid Build Coastguard Worker 165*61c4878aSAndroid Build Coastguard WorkerPython versions 166*61c4878aSAndroid Build Coastguard Worker=============== 167*61c4878aSAndroid Build Coastguard WorkerPigweed officially supports :ref:`a few Python versions 168*61c4878aSAndroid Build Coastguard Worker<docs-concepts-python-version>`. Upstream Pigweed code must support those Python 169*61c4878aSAndroid Build Coastguard Workerversions. The only exception is :ref:`module-pw_env_setup`, which must also 170*61c4878aSAndroid Build Coastguard Workersupport Python 2 and 3.6. 171*61c4878aSAndroid Build Coastguard Worker 172*61c4878aSAndroid Build Coastguard Worker--------------- 173*61c4878aSAndroid Build Coastguard WorkerBuild files: GN 174*61c4878aSAndroid Build Coastguard Worker--------------- 175*61c4878aSAndroid Build Coastguard WorkerEach Pigweed source module requires a GN build file named BUILD.gn. This 176*61c4878aSAndroid Build Coastguard Workerencapsulates the build targets and specifies their sources and dependencies. 177*61c4878aSAndroid Build Coastguard WorkerGN build files use a format similar to `Bazel's BUILD files 178*61c4878aSAndroid Build Coastguard Worker<https://docs.bazel.build/versions/main/build-ref.html>`_ 179*61c4878aSAndroid Build Coastguard Worker(see the `Bazel style guide 180*61c4878aSAndroid Build Coastguard Worker<https://docs.bazel.build/versions/main/skylark/build-style.html>`_). 181*61c4878aSAndroid Build Coastguard Worker 182*61c4878aSAndroid Build Coastguard WorkerC/C++ build targets include a list of fields. The primary fields are: 183*61c4878aSAndroid Build Coastguard Worker 184*61c4878aSAndroid Build Coastguard Worker* ``<public>`` -- public header files 185*61c4878aSAndroid Build Coastguard Worker* ``<sources>`` -- source files and private header files 186*61c4878aSAndroid Build Coastguard Worker* ``<public_configs>`` -- public build configuration 187*61c4878aSAndroid Build Coastguard Worker* ``<configs>`` -- private build configuration 188*61c4878aSAndroid Build Coastguard Worker* ``<public_deps>`` -- public dependencies 189*61c4878aSAndroid Build Coastguard Worker* ``<deps>`` -- private dependencies 190*61c4878aSAndroid Build Coastguard Worker 191*61c4878aSAndroid Build Coastguard WorkerAssets within each field must be listed in alphabetical order. 192*61c4878aSAndroid Build Coastguard Worker 193*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp 194*61c4878aSAndroid Build Coastguard Worker 195*61c4878aSAndroid Build Coastguard Worker # Here is a brief example of a GN build file. 196*61c4878aSAndroid Build Coastguard Worker 197*61c4878aSAndroid Build Coastguard Worker import("$dir_pw_unit_test/test.gni") 198*61c4878aSAndroid Build Coastguard Worker 199*61c4878aSAndroid Build Coastguard Worker config("public_include_path") { 200*61c4878aSAndroid Build Coastguard Worker include_dirs = [ "public" ] 201*61c4878aSAndroid Build Coastguard Worker visibility = [":*"] 202*61c4878aSAndroid Build Coastguard Worker } 203*61c4878aSAndroid Build Coastguard Worker 204*61c4878aSAndroid Build Coastguard Worker pw_source_set("pw_sample_module") { 205*61c4878aSAndroid Build Coastguard Worker public = [ "public/pw_sample_module/sample_module.h" ] 206*61c4878aSAndroid Build Coastguard Worker sources = [ 207*61c4878aSAndroid Build Coastguard Worker "public/pw_sample_module/internal/secret_header.h", 208*61c4878aSAndroid Build Coastguard Worker "sample_module.cc", 209*61c4878aSAndroid Build Coastguard Worker "used_by_sample_module.cc", 210*61c4878aSAndroid Build Coastguard Worker ] 211*61c4878aSAndroid Build Coastguard Worker public_configs = [ ":public_include_path" ] 212*61c4878aSAndroid Build Coastguard Worker public_deps = [ dir_pw_status ] 213*61c4878aSAndroid Build Coastguard Worker deps = [ dir_pw_varint ] 214*61c4878aSAndroid Build Coastguard Worker } 215*61c4878aSAndroid Build Coastguard Worker 216*61c4878aSAndroid Build Coastguard Worker pw_test_group("tests") { 217*61c4878aSAndroid Build Coastguard Worker tests = [ ":sample_module_test" ] 218*61c4878aSAndroid Build Coastguard Worker } 219*61c4878aSAndroid Build Coastguard Worker 220*61c4878aSAndroid Build Coastguard Worker pw_test("sample_module_test") { 221*61c4878aSAndroid Build Coastguard Worker sources = [ "sample_module_test.cc" ] 222*61c4878aSAndroid Build Coastguard Worker deps = [ ":sample_module" ] 223*61c4878aSAndroid Build Coastguard Worker } 224*61c4878aSAndroid Build Coastguard Worker 225*61c4878aSAndroid Build Coastguard Worker pw_doc_group("docs") { 226*61c4878aSAndroid Build Coastguard Worker sources = [ "docs.rst" ] 227*61c4878aSAndroid Build Coastguard Worker } 228*61c4878aSAndroid Build Coastguard Worker 229*61c4878aSAndroid Build Coastguard Worker------------------ 230*61c4878aSAndroid Build Coastguard WorkerBuild files: Bazel 231*61c4878aSAndroid Build Coastguard Worker------------------ 232*61c4878aSAndroid Build Coastguard WorkerBuild files for the Bazel build system must be named ``BUILD.bazel``. Bazel can 233*61c4878aSAndroid Build Coastguard Workerinterpret files named just ``BUILD``, but Pigweed uses ``BUILD.bazel`` to avoid 234*61c4878aSAndroid Build Coastguard Workerambiguity with other build systems or tooling. 235*61c4878aSAndroid Build Coastguard Worker 236*61c4878aSAndroid Build Coastguard WorkerPigweed's Bazel files follow the `Bazel style guide 237*61c4878aSAndroid Build Coastguard Worker<https://docs.bazel.build/versions/main/skylark/build-style.html>`_. 238*61c4878aSAndroid Build Coastguard Worker 239*61c4878aSAndroid Build Coastguard Worker------------------ 240*61c4878aSAndroid Build Coastguard WorkerBuild files: Soong 241*61c4878aSAndroid Build Coastguard Worker------------------ 242*61c4878aSAndroid Build Coastguard WorkerBuild files (blueprints) for the Soong build system must be named 243*61c4878aSAndroid Build Coastguard Worker``Android.bp``. The way Pigweed modules and backends are used is documented in 244*61c4878aSAndroid Build Coastguard Worker:ref:`module-pw_build_android`. 245*61c4878aSAndroid Build Coastguard Worker 246*61c4878aSAndroid Build Coastguard Worker.. _owners-style: 247*61c4878aSAndroid Build Coastguard Worker 248*61c4878aSAndroid Build Coastguard Worker-------------------- 249*61c4878aSAndroid Build Coastguard WorkerCode Owners (OWNERS) 250*61c4878aSAndroid Build Coastguard Worker-------------------- 251*61c4878aSAndroid Build Coastguard WorkerIf you use Gerrit for source code hosting and have the 252*61c4878aSAndroid Build Coastguard Worker`code-owners <https://android-review.googlesource.com/plugins/code-owners/Documentation/backend-find-owners.html>`__ 253*61c4878aSAndroid Build Coastguard Workerplug-in enabled Pigweed can help you enforce consistent styling on those files 254*61c4878aSAndroid Build Coastguard Workerand also detects some errors. 255*61c4878aSAndroid Build Coastguard Worker 256*61c4878aSAndroid Build Coastguard WorkerThe styling follows these rules. 257*61c4878aSAndroid Build Coastguard Worker 258*61c4878aSAndroid Build Coastguard Worker#. Content is grouped by type of line (Access grant, include, etc). 259*61c4878aSAndroid Build Coastguard Worker#. Each grouping is sorted alphabetically. 260*61c4878aSAndroid Build Coastguard Worker#. Groups are placed the following order with a blank line separating each 261*61c4878aSAndroid Build Coastguard Worker grouping. 262*61c4878aSAndroid Build Coastguard Worker 263*61c4878aSAndroid Build Coastguard Worker * "set noparent" line 264*61c4878aSAndroid Build Coastguard Worker * "include" lines 265*61c4878aSAndroid Build Coastguard Worker * "file:" lines 266*61c4878aSAndroid Build Coastguard Worker * user grants (some examples: "*", "[email protected]") 267*61c4878aSAndroid Build Coastguard Worker * "per-file:" lines 268*61c4878aSAndroid Build Coastguard Worker 269*61c4878aSAndroid Build Coastguard WorkerThis plugin will, by default, act upon any file named "OWNERS". 270*61c4878aSAndroid Build Coastguard Worker 271*61c4878aSAndroid Build Coastguard Worker.. toctree:: 272*61c4878aSAndroid Build Coastguard Worker :hidden: 273*61c4878aSAndroid Build Coastguard Worker 274*61c4878aSAndroid Build Coastguard Worker C++ <style/cpp> 275*61c4878aSAndroid Build Coastguard Worker Commit message <style/commit_message> 276*61c4878aSAndroid Build Coastguard Worker CLI <style/cli> 277*61c4878aSAndroid Build Coastguard Worker Protobuf <style/protobuf> 278*61c4878aSAndroid Build Coastguard Worker reStructuredText <style/rest> 279*61c4878aSAndroid Build Coastguard Worker Doxygen <style/doxygen> 280*61c4878aSAndroid Build Coastguard Worker Writing <style/writing> 281