1*61c4878aSAndroid Build Coastguard Worker.. _docs-module-structure: 2*61c4878aSAndroid Build Coastguard Worker 3*61c4878aSAndroid Build Coastguard Worker---------------- 4*61c4878aSAndroid Build Coastguard WorkerModule Structure 5*61c4878aSAndroid Build Coastguard Worker---------------- 6*61c4878aSAndroid Build Coastguard WorkerThe Pigweed :ref:`module <docs-glossary-module>` structure is designed to keep 7*61c4878aSAndroid Build Coastguard Workeras much code as possible for a particular slice of functionality in one place. 8*61c4878aSAndroid Build Coastguard WorkerThat means including the code from multiple languages, as well as all the 9*61c4878aSAndroid Build Coastguard Workerrelated documentation and tests. 10*61c4878aSAndroid Build Coastguard Worker 11*61c4878aSAndroid Build Coastguard WorkerAdditionally, the structure is designed to limit the number of places a file 12*61c4878aSAndroid Build Coastguard Workercould go, so that when reading callsites it is obvious where a header is from. 13*61c4878aSAndroid Build Coastguard WorkerThat is where the duplicated ``<module>`` occurrences in file paths comes from. 14*61c4878aSAndroid Build Coastguard Worker 15*61c4878aSAndroid Build Coastguard WorkerExample module structure 16*61c4878aSAndroid Build Coastguard Worker------------------------ 17*61c4878aSAndroid Build Coastguard Worker.. code-block:: text 18*61c4878aSAndroid Build Coastguard Worker 19*61c4878aSAndroid Build Coastguard Worker pw_foo/... 20*61c4878aSAndroid Build Coastguard Worker 21*61c4878aSAndroid Build Coastguard Worker docs.rst # Docs landing page (required) 22*61c4878aSAndroid Build Coastguard Worker concepts.rst # Conceptual docs (optional) 23*61c4878aSAndroid Build Coastguard Worker design.rst # Design docs (optional) 24*61c4878aSAndroid Build Coastguard Worker guides.rst # How-to guides (optional) 25*61c4878aSAndroid Build Coastguard Worker api.rst # API reference (optional) 26*61c4878aSAndroid Build Coastguard Worker cli.rst # CLI reference (optional) 27*61c4878aSAndroid Build Coastguard Worker gui.rst # GUI reference (optional) 28*61c4878aSAndroid Build Coastguard Worker tutorials/*.rst # Tutorials (optional) 29*61c4878aSAndroid Build Coastguard Worker 30*61c4878aSAndroid Build Coastguard Worker BUILD.gn # GN build required 31*61c4878aSAndroid Build Coastguard Worker BUILD # Bazel build required 32*61c4878aSAndroid Build Coastguard Worker 33*61c4878aSAndroid Build Coastguard Worker # C++ public headers; the repeated module name is required 34*61c4878aSAndroid Build Coastguard Worker public/pw_foo/foo.h 35*61c4878aSAndroid Build Coastguard Worker public/pw_foo/baz.h 36*61c4878aSAndroid Build Coastguard Worker 37*61c4878aSAndroid Build Coastguard Worker # Exposed private headers go under internal/ 38*61c4878aSAndroid Build Coastguard Worker public/pw_foo/internal/bar.h 39*61c4878aSAndroid Build Coastguard Worker public/pw_foo/internal/qux.h 40*61c4878aSAndroid Build Coastguard Worker 41*61c4878aSAndroid Build Coastguard Worker # Public override headers must go in 'public_overrides' 42*61c4878aSAndroid Build Coastguard Worker public_overrides/gtest/gtest.h 43*61c4878aSAndroid Build Coastguard Worker public_overrides/string.h 44*61c4878aSAndroid Build Coastguard Worker 45*61c4878aSAndroid Build Coastguard Worker # Private headers go into <module>_*/... 46*61c4878aSAndroid Build Coastguard Worker pw_foo_internal/zap.h 47*61c4878aSAndroid Build Coastguard Worker pw_foo_private/zip.h 48*61c4878aSAndroid Build Coastguard Worker pw_foo_secret/alxx.h 49*61c4878aSAndroid Build Coastguard Worker 50*61c4878aSAndroid Build Coastguard Worker # C++ implementations go in the root 51*61c4878aSAndroid Build Coastguard Worker foo_impl.cc 52*61c4878aSAndroid Build Coastguard Worker foo.cc 53*61c4878aSAndroid Build Coastguard Worker baz.cc 54*61c4878aSAndroid Build Coastguard Worker bar.cc 55*61c4878aSAndroid Build Coastguard Worker zap.cc 56*61c4878aSAndroid Build Coastguard Worker zip.cc 57*61c4878aSAndroid Build Coastguard Worker alxx.cc 58*61c4878aSAndroid Build Coastguard Worker 59*61c4878aSAndroid Build Coastguard Worker # C++ tests also go in the root 60*61c4878aSAndroid Build Coastguard Worker foo_test.cc 61*61c4878aSAndroid Build Coastguard Worker bar_test.cc 62*61c4878aSAndroid Build Coastguard Worker zip_test.cc 63*61c4878aSAndroid Build Coastguard Worker 64*61c4878aSAndroid Build Coastguard Worker # Python files go into 'py/<module>/...' 65*61c4878aSAndroid Build Coastguard Worker py/BUILD.gn # Python packages are declared in GN using pw_python_package 66*61c4878aSAndroid Build Coastguard Worker py/setup.py # Python files are structured as standard Python packages 67*61c4878aSAndroid Build Coastguard Worker py/foo_test.py # Tests go in py/ but outside of the Python package 68*61c4878aSAndroid Build Coastguard Worker py/bar_test.py 69*61c4878aSAndroid Build Coastguard Worker py/pw_foo/__init__.py 70*61c4878aSAndroid Build Coastguard Worker py/pw_foo/__main__.py 71*61c4878aSAndroid Build Coastguard Worker py/pw_foo/bar.py 72*61c4878aSAndroid Build Coastguard Worker py/pw_foo/py.typed # Indicates that this package has type annotations 73*61c4878aSAndroid Build Coastguard Worker 74*61c4878aSAndroid Build Coastguard Worker # Rust crates go into 'rust/...' 75*61c4878aSAndroid Build Coastguard Worker rust/BUILD.bazel 76*61c4878aSAndroid Build Coastguard Worker rust/crate_one.rs # Single file crates are in rust/<crate_name>.rs 77*61c4878aSAndroid Build Coastguard Worker rust/crate_two/lib.rs # Multi-file crate's top level source in: 78*61c4878aSAndroid Build Coastguard Worker # rust/<crate>/lib.rs 79*61c4878aSAndroid Build Coastguard Worker rust/crate_two/mod_one.rs # Multi-file crate's modules in: 80*61c4878aSAndroid Build Coastguard Worker rust/crate_two/mod_two.rs # rust/<crate>/<module_name>.rs 81*61c4878aSAndroid Build Coastguard Worker # Prefer not using mod.rs files. 82*61c4878aSAndroid Build Coastguard Worker 83*61c4878aSAndroid Build Coastguard Worker # Go files go into 'go/...' 84*61c4878aSAndroid Build Coastguard Worker go/... 85*61c4878aSAndroid Build Coastguard Worker 86*61c4878aSAndroid Build Coastguard Worker # Examples go in examples/, mixing different languages 87*61c4878aSAndroid Build Coastguard Worker examples/demo.py 88*61c4878aSAndroid Build Coastguard Worker examples/demo.cc 89*61c4878aSAndroid Build Coastguard Worker examples/demo.go 90*61c4878aSAndroid Build Coastguard Worker examples/BUILD.gn 91*61c4878aSAndroid Build Coastguard Worker examples/BUILD 92*61c4878aSAndroid Build Coastguard Worker 93*61c4878aSAndroid Build Coastguard Worker # Size reports go under size_report/ 94*61c4878aSAndroid Build Coastguard Worker size_report/BUILD.gn 95*61c4878aSAndroid Build Coastguard Worker size_report/base.cc 96*61c4878aSAndroid Build Coastguard Worker size_report/use_case_a.cc 97*61c4878aSAndroid Build Coastguard Worker size_report/use_case_b.cc 98*61c4878aSAndroid Build Coastguard Worker 99*61c4878aSAndroid Build Coastguard Worker # Protobuf definition files go into <module>_protos/... 100*61c4878aSAndroid Build Coastguard Worker pw_foo_protos/foo.proto 101*61c4878aSAndroid Build Coastguard Worker pw_foo_protos/internal/zap.proto 102*61c4878aSAndroid Build Coastguard Worker 103*61c4878aSAndroid Build Coastguard Worker # Other directories are fine, but should be private. 104*61c4878aSAndroid Build Coastguard Worker data/... 105*61c4878aSAndroid Build Coastguard Worker graphics/... 106*61c4878aSAndroid Build Coastguard Worker collection_of_tests/... 107*61c4878aSAndroid Build Coastguard Worker code_relating_to_subfeature/... 108*61c4878aSAndroid Build Coastguard Worker 109*61c4878aSAndroid Build Coastguard WorkerModule name 110*61c4878aSAndroid Build Coastguard Worker----------- 111*61c4878aSAndroid Build Coastguard WorkerPigweed upstream modules are always named with a prefix ``pw_`` to enforce 112*61c4878aSAndroid Build Coastguard Workernamespacing. Projects using Pigweed that wish to make their own modules can use 113*61c4878aSAndroid Build Coastguard Workerwhatever name they like, but we suggest picking a short prefix to namespace 114*61c4878aSAndroid Build Coastguard Workeryour product (e.g. for an Internet of Toast project, perhaps the prefix could 115*61c4878aSAndroid Build Coastguard Workerbe ``it_``). 116*61c4878aSAndroid Build Coastguard Worker 117*61c4878aSAndroid Build Coastguard WorkerC++ module structure 118*61c4878aSAndroid Build Coastguard Worker-------------------- 119*61c4878aSAndroid Build Coastguard Worker 120*61c4878aSAndroid Build Coastguard WorkerC++ public headers 121*61c4878aSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~ 122*61c4878aSAndroid Build Coastguard WorkerLocated ``{pw_module_dir}/public/<module>``. These headers are the public 123*61c4878aSAndroid Build Coastguard Workerinterface for the module. 124*61c4878aSAndroid Build Coastguard Worker 125*61c4878aSAndroid Build Coastguard Worker**Public headers** should take the form: 126*61c4878aSAndroid Build Coastguard Worker 127*61c4878aSAndroid Build Coastguard Worker``{pw_module_dir}/public/<module>/*.h`` 128*61c4878aSAndroid Build Coastguard Worker 129*61c4878aSAndroid Build Coastguard Worker**Exposed private headers** should take the form: 130*61c4878aSAndroid Build Coastguard Worker 131*61c4878aSAndroid Build Coastguard Worker``{pw_module_dir}/public/<module>/internal/*.h`` 132*61c4878aSAndroid Build Coastguard Worker 133*61c4878aSAndroid Build Coastguard WorkerExamples: 134*61c4878aSAndroid Build Coastguard Worker 135*61c4878aSAndroid Build Coastguard Worker.. code-block:: 136*61c4878aSAndroid Build Coastguard Worker 137*61c4878aSAndroid Build Coastguard Worker pw_foo/... 138*61c4878aSAndroid Build Coastguard Worker public/pw_foo/foo.h 139*61c4878aSAndroid Build Coastguard Worker public/pw_foo/a_header.h 140*61c4878aSAndroid Build Coastguard Worker public/pw_foo/baz.h 141*61c4878aSAndroid Build Coastguard Worker 142*61c4878aSAndroid Build Coastguard WorkerFor headers that must be exposed due to C++ limitations (i.e. are included from 143*61c4878aSAndroid Build Coastguard Workerthe public interface, but are not intended for use), place the headers in a 144*61c4878aSAndroid Build Coastguard Worker``internal`` subfolder under the public headers directory; as 145*61c4878aSAndroid Build Coastguard Worker``{pw_module_dir}/public/<module>/internal/*.h``. For example: 146*61c4878aSAndroid Build Coastguard Worker 147*61c4878aSAndroid Build Coastguard Worker.. code-block:: 148*61c4878aSAndroid Build Coastguard Worker 149*61c4878aSAndroid Build Coastguard Worker pw_foo/... 150*61c4878aSAndroid Build Coastguard Worker public/pw_foo/internal/secret.h 151*61c4878aSAndroid Build Coastguard Worker public/pw_foo/internal/business.h 152*61c4878aSAndroid Build Coastguard Worker 153*61c4878aSAndroid Build Coastguard Worker.. note:: 154*61c4878aSAndroid Build Coastguard Worker 155*61c4878aSAndroid Build Coastguard Worker These headers must not override headers from other modules. For 156*61c4878aSAndroid Build Coastguard Worker that, there is the ``public_overrides/`` directory. 157*61c4878aSAndroid Build Coastguard Worker 158*61c4878aSAndroid Build Coastguard WorkerC++ public override headers 159*61c4878aSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~ 160*61c4878aSAndroid Build Coastguard WorkerLocated ``{pw_module_dir}/public_overrides/<module>``. In general, the Pigweed 161*61c4878aSAndroid Build Coastguard Workerphilosophy is to avoid having "things hiding under rocks", and having header 162*61c4878aSAndroid Build Coastguard Workerfiles with the same name that can override each other is considered a rock 163*61c4878aSAndroid Build Coastguard Workerwhere surprising things can hide. Additionally, a design goal of the Pigweed 164*61c4878aSAndroid Build Coastguard Workermodule structure is to make it so there is ideally exactly one obvious place 165*61c4878aSAndroid Build Coastguard Workerto find a header based on an ``#include``. 166*61c4878aSAndroid Build Coastguard Worker 167*61c4878aSAndroid Build Coastguard WorkerHowever, in some cases header overrides are necessary to enable flexibly 168*61c4878aSAndroid Build Coastguard Workercombining modules. To make this as explicit as possible, headers which override 169*61c4878aSAndroid Build Coastguard Workerother headers must go in 170*61c4878aSAndroid Build Coastguard Worker 171*61c4878aSAndroid Build Coastguard Worker``{pw_module_dir}/public_overrides/...``` 172*61c4878aSAndroid Build Coastguard Worker 173*61c4878aSAndroid Build Coastguard WorkerFor example, the ``pw_unit_test`` module provides a header override for 174*61c4878aSAndroid Build Coastguard Worker``gtest/gtest.h``. The structure of the module is (omitting some files): 175*61c4878aSAndroid Build Coastguard Worker 176*61c4878aSAndroid Build Coastguard Worker.. code-block:: 177*61c4878aSAndroid Build Coastguard Worker 178*61c4878aSAndroid Build Coastguard Worker pw_unit_test/... 179*61c4878aSAndroid Build Coastguard Worker 180*61c4878aSAndroid Build Coastguard Worker light_public_overrides/pw_unit_test/framework_backend.h 181*61c4878aSAndroid Build Coastguard Worker googletest_public_overrides/pw_unit_test/framework_backend.h 182*61c4878aSAndroid Build Coastguard Worker 183*61c4878aSAndroid Build Coastguard Worker public_overrides/gtest 184*61c4878aSAndroid Build Coastguard Worker public_overrides/gtest/gtest.h 185*61c4878aSAndroid Build Coastguard Worker 186*61c4878aSAndroid Build Coastguard Worker public/pw_unit_test 187*61c4878aSAndroid Build Coastguard Worker public/pw_unit_test/simple_printing_event_handler.h 188*61c4878aSAndroid Build Coastguard Worker public/pw_unit_test/event_handler.h 189*61c4878aSAndroid Build Coastguard Worker 190*61c4878aSAndroid Build Coastguard WorkerNote that the overrides are in a separate directory ``public_overrides``. 191*61c4878aSAndroid Build Coastguard Worker 192*61c4878aSAndroid Build Coastguard WorkerC++ implementation files 193*61c4878aSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~ 194*61c4878aSAndroid Build Coastguard WorkerLocated ``{pw_module_dir}/``. C++ implementation files go at the top level of 195*61c4878aSAndroid Build Coastguard Workerthe module. Implementation files must always use "" style includes. 196*61c4878aSAndroid Build Coastguard Worker 197*61c4878aSAndroid Build Coastguard WorkerExample: 198*61c4878aSAndroid Build Coastguard Worker 199*61c4878aSAndroid Build Coastguard Worker.. code-block:: 200*61c4878aSAndroid Build Coastguard Worker 201*61c4878aSAndroid Build Coastguard Worker pw_unit_test/... 202*61c4878aSAndroid Build Coastguard Worker main.cc 203*61c4878aSAndroid Build Coastguard Worker framework.cc 204*61c4878aSAndroid Build Coastguard Worker test.gni 205*61c4878aSAndroid Build Coastguard Worker BUILD.gn 206*61c4878aSAndroid Build Coastguard Worker README.md 207*61c4878aSAndroid Build Coastguard Worker 208*61c4878aSAndroid Build Coastguard Worker.. _module-structure-compile-time-configuration: 209*61c4878aSAndroid Build Coastguard Worker 210*61c4878aSAndroid Build Coastguard WorkerCompile-time configuration 211*61c4878aSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~ 212*61c4878aSAndroid Build Coastguard WorkerPigweed modules are intended to be used in a wide variety of environments. 213*61c4878aSAndroid Build Coastguard WorkerIn support of this, some modules expose compile-time configuration options. 214*61c4878aSAndroid Build Coastguard WorkerPigweed has an established pattern for declaring and overriding module 215*61c4878aSAndroid Build Coastguard Workerconfiguration. 216*61c4878aSAndroid Build Coastguard Worker 217*61c4878aSAndroid Build Coastguard Worker.. tip:: 218*61c4878aSAndroid Build Coastguard Worker 219*61c4878aSAndroid Build Coastguard Worker Compile-time configuration provides flexibility, but also imposes 220*61c4878aSAndroid Build Coastguard Worker restrictions. A module can only have one configuration in a given build. 221*61c4878aSAndroid Build Coastguard Worker This makes testing modules with compile-time configuration more difficult. 222*61c4878aSAndroid Build Coastguard Worker Where appropriate, consider alternatives such as C++ templates or runtime 223*61c4878aSAndroid Build Coastguard Worker configuration. 224*61c4878aSAndroid Build Coastguard Worker 225*61c4878aSAndroid Build Coastguard WorkerDeclaring configuration 226*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^ 227*61c4878aSAndroid Build Coastguard WorkerConfiguration options are declared in a header file as macros. If the macro is 228*61c4878aSAndroid Build Coastguard Workernot already defined, a default definition is provided. Otherwise, nothing is 229*61c4878aSAndroid Build Coastguard Workerdone. Configuration headers may include ``static_assert`` statements to validate 230*61c4878aSAndroid Build Coastguard Workerconfiguration values. 231*61c4878aSAndroid Build Coastguard Worker 232*61c4878aSAndroid Build Coastguard Worker.. code-block:: c++ 233*61c4878aSAndroid Build Coastguard Worker 234*61c4878aSAndroid Build Coastguard Worker // Example configuration header 235*61c4878aSAndroid Build Coastguard Worker 236*61c4878aSAndroid Build Coastguard Worker #ifndef PW_FOO_INPUT_BUFFER_SIZE_BYTES 237*61c4878aSAndroid Build Coastguard Worker #define PW_FOO_INPUT_BUFFER_SIZE_BYTES 128 238*61c4878aSAndroid Build Coastguard Worker #endif // PW_FOO_INPUT_BUFFER_SIZE_BYTES 239*61c4878aSAndroid Build Coastguard Worker 240*61c4878aSAndroid Build Coastguard Worker static_assert(PW_FOO_INPUT_BUFFER_SIZE_BYTES >= 64); 241*61c4878aSAndroid Build Coastguard Worker 242*61c4878aSAndroid Build Coastguard WorkerThe configuration header may go in one of three places in the module, depending 243*61c4878aSAndroid Build Coastguard Workeron whether the header should be exposed by the module or not. 244*61c4878aSAndroid Build Coastguard Worker 245*61c4878aSAndroid Build Coastguard Worker.. code-block:: 246*61c4878aSAndroid Build Coastguard Worker 247*61c4878aSAndroid Build Coastguard Worker pw_foo/... 248*61c4878aSAndroid Build Coastguard Worker 249*61c4878aSAndroid Build Coastguard Worker # Publicly accessible configuration header 250*61c4878aSAndroid Build Coastguard Worker public/pw_foo/config.h 251*61c4878aSAndroid Build Coastguard Worker 252*61c4878aSAndroid Build Coastguard Worker # Internal configuration header that is included by other module headers 253*61c4878aSAndroid Build Coastguard Worker public/pw_foo/internal/config.h 254*61c4878aSAndroid Build Coastguard Worker 255*61c4878aSAndroid Build Coastguard Worker # Internal configuration header 256*61c4878aSAndroid Build Coastguard Worker pw_foo_private/config.h 257*61c4878aSAndroid Build Coastguard Worker 258*61c4878aSAndroid Build Coastguard WorkerThe configuration header is provided by a build system library. This library 259*61c4878aSAndroid Build Coastguard Workeracts as a :ref:`facade<docs-module-structure-facades>`. The details depend on 260*61c4878aSAndroid Build Coastguard Workerthe build system. 261*61c4878aSAndroid Build Coastguard Worker 262*61c4878aSAndroid Build Coastguard WorkerGN compile-time configuration 263*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 264*61c4878aSAndroid Build Coastguard WorkerThe facade uses a variable such as ``pw_foo_CONFIG``. In upstream Pigweed, all 265*61c4878aSAndroid Build Coastguard Workerconfig facades default to the ``pw_build_DEFAULT_MODULE_CONFIG`` backend. The 266*61c4878aSAndroid Build Coastguard Workerconfig facade is declared as follows: 267*61c4878aSAndroid Build Coastguard Worker 268*61c4878aSAndroid Build Coastguard Worker.. code-block:: 269*61c4878aSAndroid Build Coastguard Worker 270*61c4878aSAndroid Build Coastguard Worker declare_args() { 271*61c4878aSAndroid Build Coastguard Worker # The build target that overrides the default configuration options for this 272*61c4878aSAndroid Build Coastguard Worker # module. This should point to a source set that provides defines through a 273*61c4878aSAndroid Build Coastguard Worker # public config (which may -include a file or add defines directly). 274*61c4878aSAndroid Build Coastguard Worker pw_foo_CONFIG = pw_build_DEFAULT_MODULE_CONFIG 275*61c4878aSAndroid Build Coastguard Worker } 276*61c4878aSAndroid Build Coastguard Worker 277*61c4878aSAndroid Build Coastguard Worker # An example source set for each potential config header location follows. 278*61c4878aSAndroid Build Coastguard Worker 279*61c4878aSAndroid Build Coastguard Worker # Publicly accessible configuration header (most common) 280*61c4878aSAndroid Build Coastguard Worker pw_source_set("config") { 281*61c4878aSAndroid Build Coastguard Worker public = [ "public/pw_foo/config.h" ] 282*61c4878aSAndroid Build Coastguard Worker public_configs = [ ":public_include_path" ] 283*61c4878aSAndroid Build Coastguard Worker public_deps = [ pw_foo_CONFIG ] 284*61c4878aSAndroid Build Coastguard Worker } 285*61c4878aSAndroid Build Coastguard Worker 286*61c4878aSAndroid Build Coastguard Worker # Internal configuration header that is included by other module headers 287*61c4878aSAndroid Build Coastguard Worker pw_source_set("config") { 288*61c4878aSAndroid Build Coastguard Worker sources = [ "public/pw_foo/internal/config.h" ] 289*61c4878aSAndroid Build Coastguard Worker public_configs = [ ":public_include_path" ] 290*61c4878aSAndroid Build Coastguard Worker public_deps = [ pw_foo_CONFIG ] 291*61c4878aSAndroid Build Coastguard Worker visibility = [":*"] # Only allow this module to depend on ":config" 292*61c4878aSAndroid Build Coastguard Worker friend = [":*"] # Allow this module to access the config.h header. 293*61c4878aSAndroid Build Coastguard Worker } 294*61c4878aSAndroid Build Coastguard Worker 295*61c4878aSAndroid Build Coastguard Worker # Internal configuration header 296*61c4878aSAndroid Build Coastguard Worker pw_source_set("config") { 297*61c4878aSAndroid Build Coastguard Worker public = [ "pw_foo_private/config.h" ] 298*61c4878aSAndroid Build Coastguard Worker public_deps = [ pw_foo_CONFIG ] 299*61c4878aSAndroid Build Coastguard Worker visibility = [":*"] # Only allow this module to depend on ":config" 300*61c4878aSAndroid Build Coastguard Worker } 301*61c4878aSAndroid Build Coastguard Worker 302*61c4878aSAndroid Build Coastguard WorkerBazel compile-time configuration 303*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 304*61c4878aSAndroid Build Coastguard WorkerThe module that uses configuration depends on a ``label_flag``, conventionally 305*61c4878aSAndroid Build Coastguard Workernamed ``config``, that by default points to the 306*61c4878aSAndroid Build Coastguard Worker``//pw_build:default_module_config``. For example, 307*61c4878aSAndroid Build Coastguard Worker 308*61c4878aSAndroid Build Coastguard Worker.. code-block:: python 309*61c4878aSAndroid Build Coastguard Worker 310*61c4878aSAndroid Build Coastguard Worker # A module with a public config. That config doesn't need to be broken out 311*61c4878aSAndroid Build Coastguard Worker # into a separate cc_library. 312*61c4878aSAndroid Build Coastguard Worker cc_library( 313*61c4878aSAndroid Build Coastguard Worker name = "pw_foo", 314*61c4878aSAndroid Build Coastguard Worker hdrs = ["config.h"], 315*61c4878aSAndroid Build Coastguard Worker deps = [":config"], 316*61c4878aSAndroid Build Coastguard Worker ) 317*61c4878aSAndroid Build Coastguard Worker 318*61c4878aSAndroid Build Coastguard Worker label_flag( 319*61c4878aSAndroid Build Coastguard Worker name = "config", 320*61c4878aSAndroid Build Coastguard Worker build_setting_default = "//pw_build:default_module_config", 321*61c4878aSAndroid Build Coastguard Worker ) 322*61c4878aSAndroid Build Coastguard Worker 323*61c4878aSAndroid Build Coastguard Worker # A module with an internal config that's included by other module headers. 324*61c4878aSAndroid Build Coastguard Worker cc_library( 325*61c4878aSAndroid Build Coastguard Worker name = "pw_bar", 326*61c4878aSAndroid Build Coastguard Worker deps = [":internal_config"], 327*61c4878aSAndroid Build Coastguard Worker ) 328*61c4878aSAndroid Build Coastguard Worker 329*61c4878aSAndroid Build Coastguard Worker cc_library( 330*61c4878aSAndroid Build Coastguard Worker name = "internal_config", 331*61c4878aSAndroid Build Coastguard Worker hdrs = ["config.h"], 332*61c4878aSAndroid Build Coastguard Worker deps = [":config"], 333*61c4878aSAndroid Build Coastguard Worker visibility = ["//visibility:private"], 334*61c4878aSAndroid Build Coastguard Worker ) 335*61c4878aSAndroid Build Coastguard Worker 336*61c4878aSAndroid Build Coastguard Worker label_flag( 337*61c4878aSAndroid Build Coastguard Worker name = "config", 338*61c4878aSAndroid Build Coastguard Worker build_setting_default = "//pw_build:default_module_config", 339*61c4878aSAndroid Build Coastguard Worker ) 340*61c4878aSAndroid Build Coastguard Worker 341*61c4878aSAndroid Build Coastguard Worker # A module with a private config. 342*61c4878aSAndroid Build Coastguard Worker cc_library( 343*61c4878aSAndroid Build Coastguard Worker name = "pw_bar", 344*61c4878aSAndroid Build Coastguard Worker implementation_deps = [":private_config"], 345*61c4878aSAndroid Build Coastguard Worker ) 346*61c4878aSAndroid Build Coastguard Worker 347*61c4878aSAndroid Build Coastguard Worker cc_library( 348*61c4878aSAndroid Build Coastguard Worker name = "private_config", 349*61c4878aSAndroid Build Coastguard Worker hdrs = ["config.h"], 350*61c4878aSAndroid Build Coastguard Worker deps = [":config"], 351*61c4878aSAndroid Build Coastguard Worker visibility = ["//visibility:private"], 352*61c4878aSAndroid Build Coastguard Worker ) 353*61c4878aSAndroid Build Coastguard Worker 354*61c4878aSAndroid Build Coastguard Worker label_flag( 355*61c4878aSAndroid Build Coastguard Worker name = "config", 356*61c4878aSAndroid Build Coastguard Worker build_setting_default = "//pw_build:default_module_config", 357*61c4878aSAndroid Build Coastguard Worker ) 358*61c4878aSAndroid Build Coastguard Worker 359*61c4878aSAndroid Build Coastguard Worker 360*61c4878aSAndroid Build Coastguard Worker 361*61c4878aSAndroid Build Coastguard WorkerOverriding configuration 362*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^ 363*61c4878aSAndroid Build Coastguard WorkerAs noted above, all module configuration facades default to the same backend 364*61c4878aSAndroid Build Coastguard Worker(``pw_build_DEFAULT_MODULE_CONFIG`` in GN, ``//pw_build:default_module_config`` 365*61c4878aSAndroid Build Coastguard Workerin Bazel). This allows projects to override configuration values for multiple 366*61c4878aSAndroid Build Coastguard Workermodules from a single configuration backend, if desired. The configuration 367*61c4878aSAndroid Build Coastguard Workervalues may also be overridden individually by setting backends for the 368*61c4878aSAndroid Build Coastguard Workerindividual module configurations (e.g. in GN, ``pw_foo_CONFIG = 369*61c4878aSAndroid Build Coastguard Worker"//configuration:my_foo_config"``, in Bazel 370*61c4878aSAndroid Build Coastguard Worker``--//pw_foo:config=//configuration:my_foo_config``). 371*61c4878aSAndroid Build Coastguard Worker 372*61c4878aSAndroid Build Coastguard WorkerConfigurations options are overridden by setting macros in the config backend. 373*61c4878aSAndroid Build Coastguard WorkerIn Bazel, the only supported override mechanism are compilation options, such 374*61c4878aSAndroid Build Coastguard Workeras ``-DPW_FOO_INPUT_BUFFER_SIZE_BYTES=256``. In GN and CMake, configuration 375*61c4878aSAndroid Build Coastguard Workermacro definitions may also be set in a header file. The header file is included 376*61c4878aSAndroid Build Coastguard Workerusing the ``-include`` compilation option. 377*61c4878aSAndroid Build Coastguard Worker 378*61c4878aSAndroid Build Coastguard WorkerGN config override example 379*61c4878aSAndroid Build Coastguard Worker.......................... 380*61c4878aSAndroid Build Coastguard WorkerThis example shows two ways to configure a module in the GN build system. 381*61c4878aSAndroid Build Coastguard Worker 382*61c4878aSAndroid Build Coastguard Worker.. code-block:: 383*61c4878aSAndroid Build Coastguard Worker 384*61c4878aSAndroid Build Coastguard Worker # In the toolchain, set either pw_build_DEFAULT_MODULE_CONFIG or pw_foo_CONFIG 385*61c4878aSAndroid Build Coastguard Worker pw_build_DEFAULT_MODULE_CONFIG = get_path_info(":define_overrides", "abspath") 386*61c4878aSAndroid Build Coastguard Worker 387*61c4878aSAndroid Build Coastguard Worker # This configuration sets PW_FOO_INPUT_BUFFER_SIZE_BYTES using the -D flag. 388*61c4878aSAndroid Build Coastguard Worker pw_source_set("define_overrides") { 389*61c4878aSAndroid Build Coastguard Worker public_configs = [ ":define_options" ] 390*61c4878aSAndroid Build Coastguard Worker } 391*61c4878aSAndroid Build Coastguard Worker 392*61c4878aSAndroid Build Coastguard Worker config("define_options") { 393*61c4878aSAndroid Build Coastguard Worker defines = [ "PW_FOO_INPUT_BUFFER_SIZE_BYTES=256" ] 394*61c4878aSAndroid Build Coastguard Worker } 395*61c4878aSAndroid Build Coastguard Worker 396*61c4878aSAndroid Build Coastguard Worker # This configuration sets PW_FOO_INPUT_BUFFER_SIZE_BYTES in a header file. 397*61c4878aSAndroid Build Coastguard Worker pw_source_set("include_overrides") { 398*61c4878aSAndroid Build Coastguard Worker public_configs = [ ":set_options_in_header_file" ] 399*61c4878aSAndroid Build Coastguard Worker 400*61c4878aSAndroid Build Coastguard Worker # Header file with #define PW_FOO_INPUT_BUFFER_SIZE_BYTES 256 401*61c4878aSAndroid Build Coastguard Worker sources = [ "my_config_overrides.h" ] 402*61c4878aSAndroid Build Coastguard Worker } 403*61c4878aSAndroid Build Coastguard Worker 404*61c4878aSAndroid Build Coastguard Worker config("set_options_in_header_file") { 405*61c4878aSAndroid Build Coastguard Worker cflags = [ 406*61c4878aSAndroid Build Coastguard Worker "-include", 407*61c4878aSAndroid Build Coastguard Worker rebase_path("my_config_overrides.h", root_build_dir), 408*61c4878aSAndroid Build Coastguard Worker ] 409*61c4878aSAndroid Build Coastguard Worker } 410*61c4878aSAndroid Build Coastguard Worker 411*61c4878aSAndroid Build Coastguard WorkerBazel config override example 412*61c4878aSAndroid Build Coastguard Worker............................. 413*61c4878aSAndroid Build Coastguard WorkerThis shows the only supported way to configure a module in Bazel. 414*61c4878aSAndroid Build Coastguard Worker 415*61c4878aSAndroid Build Coastguard Worker.. code-block:: python 416*61c4878aSAndroid Build Coastguard Worker 417*61c4878aSAndroid Build Coastguard Worker # To use these overrides for all modules, set the global module config label 418*61c4878aSAndroid Build Coastguard Worker # flag, 419*61c4878aSAndroid Build Coastguard Worker # 420*61c4878aSAndroid Build Coastguard Worker # --@pigweed//pw_build:default_module_config=//path_to:config_overrides 421*61c4878aSAndroid Build Coastguard Worker # 422*61c4878aSAndroid Build Coastguard Worker # To use them just for one module, set the module-specific config label 423*61c4878aSAndroid Build Coastguard Worker # flag, 424*61c4878aSAndroid Build Coastguard Worker # 425*61c4878aSAndroid Build Coastguard Worker # --@pigweed//pw_foo:config_override=//path_to:config_overrides 426*61c4878aSAndroid Build Coastguard Worker cc_library( 427*61c4878aSAndroid Build Coastguard Worker name = "config_overrides", 428*61c4878aSAndroid Build Coastguard Worker defines = [ 429*61c4878aSAndroid Build Coastguard Worker "PW_FOO_INPUT_BUFFER_SIZE_BYTES=256", 430*61c4878aSAndroid Build Coastguard Worker ], 431*61c4878aSAndroid Build Coastguard Worker ) 432*61c4878aSAndroid Build Coastguard Worker 433*61c4878aSAndroid Build Coastguard WorkerTo conditionally enable targets based on whether a particular config override is 434*61c4878aSAndroid Build Coastguard Workerenabled, a ``config_setting`` can be defined that looks at the config_override 435*61c4878aSAndroid Build Coastguard Workerlabel flag value. This allows use of ``target_compatible_with`` to enable 436*61c4878aSAndroid Build Coastguard Workertargets. 437*61c4878aSAndroid Build Coastguard Worker 438*61c4878aSAndroid Build Coastguard Worker.. code-block:: python 439*61c4878aSAndroid Build Coastguard Worker 440*61c4878aSAndroid Build Coastguard Worker # Setup config_setting that is enabled when a particular config override is 441*61c4878aSAndroid Build Coastguard Worker # set. 442*61c4878aSAndroid Build Coastguard Worker config_setting( 443*61c4878aSAndroid Build Coastguard Worker name = "config_override_setting", 444*61c4878aSAndroid Build Coastguard Worker flag_values = { 445*61c4878aSAndroid Build Coastguard Worker "--@pigweed//pw_foo:config_override": ":config_overrides", 446*61c4878aSAndroid Build Coastguard Worker }, 447*61c4878aSAndroid Build Coastguard Worker ) 448*61c4878aSAndroid Build Coastguard Worker 449*61c4878aSAndroid Build Coastguard Worker # For targets that need some specific config setting to build, conditionally 450*61c4878aSAndroid Build Coastguard Worker # enable them. 451*61c4878aSAndroid Build Coastguard Worker pw_cc_test( 452*61c4878aSAndroid Build Coastguard Worker name = "test", 453*61c4878aSAndroid Build Coastguard Worker target_compatible_with = select({ 454*61c4878aSAndroid Build Coastguard Worker ":config_override_setting": [], 455*61c4878aSAndroid Build Coastguard Worker "//conditions:default": ["@platforms//:incompatible"], 456*61c4878aSAndroid Build Coastguard Worker }), 457*61c4878aSAndroid Build Coastguard Worker ... 458*61c4878aSAndroid Build Coastguard Worker ) 459*61c4878aSAndroid Build Coastguard Worker 460*61c4878aSAndroid Build Coastguard Worker 461*61c4878aSAndroid Build Coastguard WorkerWhy this config pattern is preferred 462*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 463*61c4878aSAndroid Build Coastguard WorkerAlternate patterns for configuring a module include overriding the module's 464*61c4878aSAndroid Build Coastguard Workerconfig header or having that header optionally include a header at a known 465*61c4878aSAndroid Build Coastguard Workerpath (e.g. ``pw_foo/config_overrides.h``). There are a few downsides to these 466*61c4878aSAndroid Build Coastguard Workerapproaches: 467*61c4878aSAndroid Build Coastguard Worker 468*61c4878aSAndroid Build Coastguard Worker* The module needs its own config header that defines, provides defaults for, 469*61c4878aSAndroid Build Coastguard Worker and validates the configuration options. Replacing this header with a 470*61c4878aSAndroid Build Coastguard Worker user-defined header would require defining all options in the user's header, 471*61c4878aSAndroid Build Coastguard Worker which is cumbersome and brittle, and would bypass validation in the module's 472*61c4878aSAndroid Build Coastguard Worker header. 473*61c4878aSAndroid Build Coastguard Worker* Including a config override header at a particular path would prevent 474*61c4878aSAndroid Build Coastguard Worker multiple modules from sharing the same configuration file. Multiple headers 475*61c4878aSAndroid Build Coastguard Worker could redirect to the same configuration file, but this would still require 476*61c4878aSAndroid Build Coastguard Worker creating a separate header and setting the config backend variable for each 477*61c4878aSAndroid Build Coastguard Worker module. 478*61c4878aSAndroid Build Coastguard Worker* Optionally including a config override header requires boilerplate code that 479*61c4878aSAndroid Build Coastguard Worker would have to be duplicated in every configurable module. 480*61c4878aSAndroid Build Coastguard Worker* An optional config override header file would silently be excluded if the 481*61c4878aSAndroid Build Coastguard Worker file path were accidentally misspelled. 482*61c4878aSAndroid Build Coastguard Worker 483*61c4878aSAndroid Build Coastguard WorkerPython module structure 484*61c4878aSAndroid Build Coastguard Worker----------------------- 485*61c4878aSAndroid Build Coastguard WorkerPython code is structured as described in the :ref:`docs-python-build-structure` 486*61c4878aSAndroid Build Coastguard Workersection of :ref:`docs-python-build`. 487*61c4878aSAndroid Build Coastguard Worker 488*61c4878aSAndroid Build Coastguard Worker.. _docs-module-structure-facades: 489*61c4878aSAndroid Build Coastguard Worker 490*61c4878aSAndroid Build Coastguard WorkerFacades 491*61c4878aSAndroid Build Coastguard Worker------- 492*61c4878aSAndroid Build Coastguard WorkerIn Pigweed, facades represent a dependency that can be swapped at compile time. 493*61c4878aSAndroid Build Coastguard WorkerFacades are similar in concept to a virtual interface, but the implementation is 494*61c4878aSAndroid Build Coastguard Workerset by the build system. Runtime polymorphism with facades is not 495*61c4878aSAndroid Build Coastguard Workerpossible, and each facade may only have one implementation (backend) per 496*61c4878aSAndroid Build Coastguard Workertoolchain compilation. 497*61c4878aSAndroid Build Coastguard Worker 498*61c4878aSAndroid Build Coastguard WorkerIn the simplest sense, a facade is just a dependency represented by a variable. 499*61c4878aSAndroid Build Coastguard WorkerFor example, the ``pw_log`` facade is represented by the ``pw_log_BACKEND`` 500*61c4878aSAndroid Build Coastguard Workerbuild variable. Facades typically are bundled with a build system library that 501*61c4878aSAndroid Build Coastguard Workerdepends on the backend. 502*61c4878aSAndroid Build Coastguard Worker 503*61c4878aSAndroid Build Coastguard WorkerFacades are essential in some circumstances: 504*61c4878aSAndroid Build Coastguard Worker 505*61c4878aSAndroid Build Coastguard Worker* Low-level, platform-specific features (:ref:`module-pw_cpu_exception`). 506*61c4878aSAndroid Build Coastguard Worker* Features that require a macro or non-virtual function interface 507*61c4878aSAndroid Build Coastguard Worker (:ref:`module-pw_log`, :ref:`module-pw_assert`). 508*61c4878aSAndroid Build Coastguard Worker* Highly leveraged code where a virtual interface or callback is too costly or 509*61c4878aSAndroid Build Coastguard Worker cumbersome (:ref:`module-pw_tokenizer`). 510*61c4878aSAndroid Build Coastguard Worker 511*61c4878aSAndroid Build Coastguard Worker.. caution:: 512*61c4878aSAndroid Build Coastguard Worker 513*61c4878aSAndroid Build Coastguard Worker Modules should only use facades when necessary. Facades are permanently locked 514*61c4878aSAndroid Build Coastguard Worker to a particular implementation at compile time. Multiple backends cannot be 515*61c4878aSAndroid Build Coastguard Worker used in one build, and runtime dependency injection is not possible, which 516*61c4878aSAndroid Build Coastguard Worker makes testing difficult. Where appropriate, modules should use other 517*61c4878aSAndroid Build Coastguard Worker mechanisms, such as virtual interfaces, callbacks, or templates, in place of 518*61c4878aSAndroid Build Coastguard Worker facades. 519*61c4878aSAndroid Build Coastguard Worker 520*61c4878aSAndroid Build Coastguard WorkerThe GN build system provides the 521*61c4878aSAndroid Build Coastguard Worker:ref:`pw_facade template<module-pw_build-facade>` as a convenient way to declare 522*61c4878aSAndroid Build Coastguard Workerfacades. 523*61c4878aSAndroid Build Coastguard Worker 524*61c4878aSAndroid Build Coastguard WorkerMultiple Facades 525*61c4878aSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~ 526*61c4878aSAndroid Build Coastguard WorkerA module may contain multiple facades. Each facade's public override headers 527*61c4878aSAndroid Build Coastguard Workermust be contained in separate folders in the backend implementation, so that 528*61c4878aSAndroid Build Coastguard Workerit's possible to use multiple backends for a module. 529*61c4878aSAndroid Build Coastguard Worker 530*61c4878aSAndroid Build Coastguard Worker.. code-block:: 531*61c4878aSAndroid Build Coastguard Worker 532*61c4878aSAndroid Build Coastguard Worker # pw_foo contains 2 facades, foo and bar 533*61c4878aSAndroid Build Coastguard Worker pw_foo/... 534*61c4878aSAndroid Build Coastguard Worker # Public headers 535*61c4878aSAndroid Build Coastguard Worker # public/pw_foo/foo.h #includes pw_foo_backend/foo.h 536*61c4878aSAndroid Build Coastguard Worker # public/pw_foo/bar.h #includes pw_foo_backend/bar.h 537*61c4878aSAndroid Build Coastguard Worker public/pw_foo/foo.h 538*61c4878aSAndroid Build Coastguard Worker public/pw_foo/bar.h 539*61c4878aSAndroid Build Coastguard Worker 540*61c4878aSAndroid Build Coastguard Worker pw_foo_backend/... 541*61c4878aSAndroid Build Coastguard Worker 542*61c4878aSAndroid Build Coastguard Worker # Public override headers for facade1 and facade2 go in separate folders 543*61c4878aSAndroid Build Coastguard Worker foo_public_overrides/pw_foo_backend/foo.h 544*61c4878aSAndroid Build Coastguard Worker bar_public_overrides/pw_foo_backend/bar.h 545*61c4878aSAndroid Build Coastguard Worker 546*61c4878aSAndroid Build Coastguard WorkerDocumentation 547*61c4878aSAndroid Build Coastguard Worker------------- 548*61c4878aSAndroid Build Coastguard WorkerSee :ref:`seed-0102`. 549*61c4878aSAndroid Build Coastguard Worker 550*61c4878aSAndroid Build Coastguard WorkerCreating a new Pigweed module 551*61c4878aSAndroid Build Coastguard Worker----------------------------- 552*61c4878aSAndroid Build Coastguard WorkerNew Pigweed modules can be easily created using 553*61c4878aSAndroid Build Coastguard Worker``pw module create MODULE_NAME`` (refer to the `Module name`_ guidelines). 554*61c4878aSAndroid Build Coastguard Worker 555*61c4878aSAndroid Build Coastguard Worker.. tip:: 556*61c4878aSAndroid Build Coastguard Worker 557*61c4878aSAndroid Build Coastguard Worker Connect with the Pigweed community (by `mailing the Pigweed list 558*61c4878aSAndroid Build Coastguard Worker <https://groups.google.com/forum/#!forum/pigweed>`_ or `raising your idea 559*61c4878aSAndroid Build Coastguard Worker in the Pigweed chat <https://discord.gg/M9NSeTA>`_) to discuss your module 560*61c4878aSAndroid Build Coastguard Worker idea before getting too far into the implementation. This can prevent 561*61c4878aSAndroid Build Coastguard Worker accidentally duplicating work, or avoiding writing code that won't get 562*61c4878aSAndroid Build Coastguard Worker accepted. 563*61c4878aSAndroid Build Coastguard Worker 564*61c4878aSAndroid Build Coastguard WorkerThis command will create a folder with the provided module name as well as a 565*61c4878aSAndroid Build Coastguard Workernumber of basic build files. After this command has run, new code should be 566*61c4878aSAndroid Build Coastguard Workeradded in the following locations: 567*61c4878aSAndroid Build Coastguard Worker 568*61c4878aSAndroid Build Coastguard Worker1. Add `C++ public headers`_ files in 569*61c4878aSAndroid Build Coastguard Worker ``{pw_module_dir}/public/{pw_module_name}/`` 570*61c4878aSAndroid Build Coastguard Worker2. Add `C++ implementation files`_ files in ``{pw_module_dir}/`` 571*61c4878aSAndroid Build Coastguard Worker3. Add module documentation to ``{pw_module_dir}/docs.rst``. See 572*61c4878aSAndroid Build Coastguard Worker :ref:`seed-0102` for additional documentation options. 573*61c4878aSAndroid Build Coastguard Worker4. Add GN build targets to ``{pw_module_dir}/BUILD.gn``. 574*61c4878aSAndroid Build Coastguard Worker 575*61c4878aSAndroid Build Coastguard Worker - Add new ``pw_test`` targets to the list in ``pw_test_group("tests")``. 576*61c4878aSAndroid Build Coastguard Worker - Add any additional ``.rst`` documentation files to 577*61c4878aSAndroid Build Coastguard Worker ``pw_docs_group("docs")``. 578*61c4878aSAndroid Build Coastguard Worker 579*61c4878aSAndroid Build Coastguard Worker5. Add Bazel build targets to ``{pw_module_dir}/BUILD.bazel``. 580*61c4878aSAndroid Build Coastguard Worker 581*61c4878aSAndroid Build Coastguard Worker6. Add CMake build targets to ``{pw_module_dir}/CMakeLists.txt``. 582*61c4878aSAndroid Build Coastguard Worker 583*61c4878aSAndroid Build Coastguard Worker7. Run :ref:`module-pw_module-module-check` to ensure that the new module has 584*61c4878aSAndroid Build Coastguard Worker been properly configured. 585*61c4878aSAndroid Build Coastguard Worker 586*61c4878aSAndroid Build Coastguard Worker - ``$ pw module check {pw_module_dir}`` 587*61c4878aSAndroid Build Coastguard Worker 588*61c4878aSAndroid Build Coastguard Worker8. Contribute your module to upstream Pigweed (optional but encouraged!) 589