xref: /aosp_15_r20/external/pigweed/docs/build_system.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _docs-build-system:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker=============
4*61c4878aSAndroid Build Coastguard WorkerBuild systems
5*61c4878aSAndroid Build Coastguard Worker=============
6*61c4878aSAndroid Build Coastguard WorkerBuilding software for embedded devices is a complex process. Projects often have
7*61c4878aSAndroid Build Coastguard Workercustom toolchains, target different hardware platforms, and require additional
8*61c4878aSAndroid Build Coastguard Workerconfiguration and post-processing of artifacts.
9*61c4878aSAndroid Build Coastguard Worker
10*61c4878aSAndroid Build Coastguard WorkerAs a modern embedded framework, Pigweed's goal is to collect these embedded use
11*61c4878aSAndroid Build Coastguard Workercases into a powerful and flexible build system, then extend it with support for
12*61c4878aSAndroid Build Coastguard Workermodern software development practices.
13*61c4878aSAndroid Build Coastguard Worker
14*61c4878aSAndroid Build Coastguard WorkerSee :ref:`docs-python-build` for information about Python build automation with
15*61c4878aSAndroid Build Coastguard WorkerPigweed.
16*61c4878aSAndroid Build Coastguard Worker
17*61c4878aSAndroid Build Coastguard Worker.. toctree::
18*61c4878aSAndroid Build Coastguard Worker  :hidden:
19*61c4878aSAndroid Build Coastguard Worker
20*61c4878aSAndroid Build Coastguard Worker  bazel_compatibility
21*61c4878aSAndroid Build Coastguard Worker  python_build
22*61c4878aSAndroid Build Coastguard Worker
23*61c4878aSAndroid Build Coastguard WorkerWhat's in a build system?
24*61c4878aSAndroid Build Coastguard Worker=========================
25*61c4878aSAndroid Build Coastguard WorkerA quality build system provides a variety of features beyond compiling code.
26*61c4878aSAndroid Build Coastguard WorkerThroughout our experience with embedded development, we've found several build
27*61c4878aSAndroid Build Coastguard Workerfeatures to be especially useful, and designed Pigweed's build system with them
28*61c4878aSAndroid Build Coastguard Workerin mind.
29*61c4878aSAndroid Build Coastguard Worker
30*61c4878aSAndroid Build Coastguard WorkerSimple toolchain configuration
31*61c4878aSAndroid Build Coastguard Worker------------------------------
32*61c4878aSAndroid Build Coastguard WorkerEmbedded projects often use custom build toolchains for their specific hardware.
33*61c4878aSAndroid Build Coastguard WorkerConfiguring these should be a simple process, both in their initial setup and
34*61c4878aSAndroid Build Coastguard Workerlater adjustments.
35*61c4878aSAndroid Build Coastguard Worker
36*61c4878aSAndroid Build Coastguard WorkerMulti-target builds
37*61c4878aSAndroid Build Coastguard Worker-------------------
38*61c4878aSAndroid Build Coastguard WorkerVirtually every consumer product has firmware that targets different boards or
39*61c4878aSAndroid Build Coastguard WorkerMCUs during development. While building for a single board is simple enough, the
40*61c4878aSAndroid Build Coastguard Workercomplexity of supporting different targets ranges from changing compiler flags
41*61c4878aSAndroid Build Coastguard Workerto swapping out entire libraries of firmware and drivers. This is often done by
42*61c4878aSAndroid Build Coastguard Workerrunning multiple builds, configuring each one accordingly. In Pigweed, we've
43*61c4878aSAndroid Build Coastguard Workerdesigned our build system with first-class multi-target support in mind,
44*61c4878aSAndroid Build Coastguard Workerallowing any number of target configurations to be built simultaneously.
45*61c4878aSAndroid Build Coastguard Worker
46*61c4878aSAndroid Build Coastguard WorkerMulti-language support
47*61c4878aSAndroid Build Coastguard Worker----------------------
48*61c4878aSAndroid Build Coastguard WorkerEmbedded projects are typically written in C, C++, and assembly. However, it is
49*61c4878aSAndroid Build Coastguard Workerpossible to have firmware written in other languages, such as Rust.
50*61c4878aSAndroid Build Coastguard WorkerAdditionally, projects may have host-side tooling written in a wide variety of
51*61c4878aSAndroid Build Coastguard Workerlanguages. Having all of these build together proves to be a large time saver.
52*61c4878aSAndroid Build Coastguard Worker
53*61c4878aSAndroid Build Coastguard WorkerCustom scripting
54*61c4878aSAndroid Build Coastguard Worker----------------
55*61c4878aSAndroid Build Coastguard WorkerEmbedded projects often require post-processing of build artifacts; these may
56*61c4878aSAndroid Build Coastguard Workerinclude:
57*61c4878aSAndroid Build Coastguard Worker
58*61c4878aSAndroid Build Coastguard Worker* Extracting ELF sections into a different container
59*61c4878aSAndroid Build Coastguard Worker* Injecting metadata into firmware images
60*61c4878aSAndroid Build Coastguard Worker* Image signing
61*61c4878aSAndroid Build Coastguard Worker* Creating databases of symbols for debugging
62*61c4878aSAndroid Build Coastguard Worker* Extracting string tokens into a database (for example, with
63*61c4878aSAndroid Build Coastguard Worker  :ref:`module-pw_tokenizer`)
64*61c4878aSAndroid Build Coastguard Worker
65*61c4878aSAndroid Build Coastguard WorkerThese are run as steps during a build, facilitated by the build system.
66*61c4878aSAndroid Build Coastguard Worker
67*61c4878aSAndroid Build Coastguard WorkerSee also
68*61c4878aSAndroid Build Coastguard Worker^^^^^^^^
69*61c4878aSAndroid Build Coastguard Worker
70*61c4878aSAndroid Build Coastguard Worker* :ref:`module-pw_build-python-action`
71*61c4878aSAndroid Build Coastguard Worker
72*61c4878aSAndroid Build Coastguard WorkerPython
73*61c4878aSAndroid Build Coastguard Worker------
74*61c4878aSAndroid Build Coastguard WorkerPython is a favorite scripting language of many development teams, and here at
75*61c4878aSAndroid Build Coastguard WorkerPigweed, we're no exception. Much of Pigweed's host-side tooling is written in
76*61c4878aSAndroid Build Coastguard WorkerPython. While Python works great for local development, problems can arise when
77*61c4878aSAndroid Build Coastguard Workerscripts need to be packaged and distributed for vendors or factory teams. Having
78*61c4878aSAndroid Build Coastguard Workerproper support for packaging Python within a build system allows teams to focus
79*61c4878aSAndroid Build Coastguard Workeron writing code instead of worrying about distribution.
80*61c4878aSAndroid Build Coastguard Worker
81*61c4878aSAndroid Build Coastguard WorkerSize reporting
82*61c4878aSAndroid Build Coastguard Worker--------------
83*61c4878aSAndroid Build Coastguard WorkerOn embedded devices, memory is everything. Most projects have some sort of
84*61c4878aSAndroid Build Coastguard Workercustom tooling to determine how much flash and RAM space their firmware uses.
85*61c4878aSAndroid Build Coastguard WorkerBeing able to run size reports as part of a build ensures that they are always
86*61c4878aSAndroid Build Coastguard Workerup-to-date and allows space usage to be tracked over time.
87*61c4878aSAndroid Build Coastguard Worker
88*61c4878aSAndroid Build Coastguard WorkerSee also
89*61c4878aSAndroid Build Coastguard Worker^^^^^^^^
90*61c4878aSAndroid Build Coastguard Worker
91*61c4878aSAndroid Build Coastguard Worker* :ref:`module-pw_bloat`
92*61c4878aSAndroid Build Coastguard Worker
93*61c4878aSAndroid Build Coastguard WorkerDocumentation
94*61c4878aSAndroid Build Coastguard Worker-------------
95*61c4878aSAndroid Build Coastguard WorkerAn oft-neglected part of software development, documentation is invaluable for
96*61c4878aSAndroid Build Coastguard Workerfuture maintainers of a project. As such, Pigweed has integrated documentation
97*61c4878aSAndroid Build Coastguard Workerwhich builds alongside its code and combines with other build features, such as
98*61c4878aSAndroid Build Coastguard Workersize reports, to provide high quality, up-to-date references for developers.
99*61c4878aSAndroid Build Coastguard Worker
100*61c4878aSAndroid Build Coastguard WorkerSee also
101*61c4878aSAndroid Build Coastguard Worker^^^^^^^^
102*61c4878aSAndroid Build Coastguard Worker
103*61c4878aSAndroid Build Coastguard Worker* :ref:`module-pw_docgen`
104*61c4878aSAndroid Build Coastguard Worker
105*61c4878aSAndroid Build Coastguard WorkerUnit testing
106*61c4878aSAndroid Build Coastguard Worker------------
107*61c4878aSAndroid Build Coastguard WorkerUnit tests are essential to ensure that the functionality of code remains
108*61c4878aSAndroid Build Coastguard Workerconsistent as changes are made to avoid accidental regressions. Running unit
109*61c4878aSAndroid Build Coastguard Workertests as part of a build keeps developers constantly aware of the impact of
110*61c4878aSAndroid Build Coastguard Workertheir changes.
111*61c4878aSAndroid Build Coastguard Worker
112*61c4878aSAndroid Build Coastguard WorkerHost-side unit tests
113*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^
114*61c4878aSAndroid Build Coastguard WorkerThough Pigweed targets embedded devices, a lot of its code can be run and tested
115*61c4878aSAndroid Build Coastguard Workeron a host desktop by swapping out backends to host platform libraries. This is
116*61c4878aSAndroid Build Coastguard Workerhighly beneficial during development, as it allows tests to consistently run
117*61c4878aSAndroid Build Coastguard Workerwithout having to go through the process of flashing a device.
118*61c4878aSAndroid Build Coastguard Worker
119*61c4878aSAndroid Build Coastguard WorkerDevice-side unit tests
120*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^
121*61c4878aSAndroid Build Coastguard WorkerAs useful as host-side tests are, they are not sufficient for developing actual
122*61c4878aSAndroid Build Coastguard Workerfirmware, and it is critical to run tests on the actual hardware. Pigweed has
123*61c4878aSAndroid Build Coastguard Workerinvested into creating a test framework and build integration for running tests
124*61c4878aSAndroid Build Coastguard Workeracross physical devices as part of a build.
125*61c4878aSAndroid Build Coastguard Worker
126*61c4878aSAndroid Build Coastguard WorkerSee also
127*61c4878aSAndroid Build Coastguard Worker^^^^^^^^
128*61c4878aSAndroid Build Coastguard Worker
129*61c4878aSAndroid Build Coastguard Worker* :ref:`module-pw_unit_test`
130*61c4878aSAndroid Build Coastguard Worker* :ref:`module-pw_target_runner`
131*61c4878aSAndroid Build Coastguard Worker
132*61c4878aSAndroid Build Coastguard WorkerBonus: pw watch
133*61c4878aSAndroid Build Coastguard Worker---------------
134*61c4878aSAndroid Build Coastguard WorkerIn web development, it is common to have a file system watcher listening for
135*61c4878aSAndroid Build Coastguard Workersource file changes and triggering a build for quick iteration. When combined
136*61c4878aSAndroid Build Coastguard Workerwith a fast incremental build system, this becomes a powerful feature, allowing
137*61c4878aSAndroid Build Coastguard Workerthings such as unit tests and size reports to re-run whenever any dependent
138*61c4878aSAndroid Build Coastguard Workercode is modified.
139*61c4878aSAndroid Build Coastguard Worker
140*61c4878aSAndroid Build Coastguard WorkerWhile initially seen as somewhat of a gimmick, Pigweed's watcher has become a
141*61c4878aSAndroid Build Coastguard Workerstaple of Pigweed development, with most Pigweed users having it permanently
142*61c4878aSAndroid Build Coastguard Workerrunning in a terminal window.
143*61c4878aSAndroid Build Coastguard Worker
144*61c4878aSAndroid Build Coastguard WorkerSee also
145*61c4878aSAndroid Build Coastguard Worker^^^^^^^^
146*61c4878aSAndroid Build Coastguard Worker
147*61c4878aSAndroid Build Coastguard Worker* :ref:`module-pw_watch`
148*61c4878aSAndroid Build Coastguard Worker
149*61c4878aSAndroid Build Coastguard WorkerPigweed's build systems
150*61c4878aSAndroid Build Coastguard Worker=======================
151*61c4878aSAndroid Build Coastguard WorkerPigweed can be used either as a monolith or à la carte, slotting into an
152*61c4878aSAndroid Build Coastguard Workerexisting project. To this end, Pigweed supports multiple build systems, allowing
153*61c4878aSAndroid Build Coastguard WorkerPigweed-based projects to choose the most suitable one for them.
154*61c4878aSAndroid Build Coastguard Worker
155*61c4878aSAndroid Build Coastguard WorkerOf the supported build systems, GN is the most full-featured, followed by CMake,
156*61c4878aSAndroid Build Coastguard Workerand finally Bazel.
157*61c4878aSAndroid Build Coastguard Worker
158*61c4878aSAndroid Build Coastguard Worker.. note::
159*61c4878aSAndroid Build Coastguard Worker  A quick note on terminology: the word "target" is overloaded within GN/Bazel (and
160*61c4878aSAndroid Build Coastguard Worker  Pigweed)---it can refer to either a GN/Bazel build target, such as a ``source_set``
161*61c4878aSAndroid Build Coastguard Worker  or ``executable``, or to an output platform (e.g. a specific board, device, or
162*61c4878aSAndroid Build Coastguard Worker  system).
163*61c4878aSAndroid Build Coastguard Worker
164*61c4878aSAndroid Build Coastguard Worker  To avoid confusing the two, we refer to the former as "GN/Bazel targets" and the
165*61c4878aSAndroid Build Coastguard Worker  latter as "Pigweed targets".
166*61c4878aSAndroid Build Coastguard Worker
167*61c4878aSAndroid Build Coastguard Worker.. _docs-build-system-gn:
168*61c4878aSAndroid Build Coastguard Worker
169*61c4878aSAndroid Build Coastguard WorkerGN
170*61c4878aSAndroid Build Coastguard Worker--
171*61c4878aSAndroid Build Coastguard WorkerA perhaps unfamiliar name, `GN (Generate Ninja)`_ is a meta-build system that
172*61c4878aSAndroid Build Coastguard Workeroutputs `Ninja`_ build files, originally designed for use in Chromium. Pigweed
173*61c4878aSAndroid Build Coastguard Workerfirst experimented with GN after hearing about it from another team, and we
174*61c4878aSAndroid Build Coastguard Workerquickly came to appreciate its speed and simplicity. GN has become Pigweed's
175*61c4878aSAndroid Build Coastguard Workerprimary build system; it is used for all upstream development and strongly
176*61c4878aSAndroid Build Coastguard Workerrecommended for Pigweed-based projects where possible.
177*61c4878aSAndroid Build Coastguard Worker
178*61c4878aSAndroid Build Coastguard Worker.. _CMake: https://cmake.org/
179*61c4878aSAndroid Build Coastguard Worker.. _Bazel: https://bazel.build/
180*61c4878aSAndroid Build Coastguard Worker.. _GN (Generate Ninja): https://gn.googlesource.com/gn
181*61c4878aSAndroid Build Coastguard Worker.. _Ninja: https://ninja-build.org/
182*61c4878aSAndroid Build Coastguard Worker
183*61c4878aSAndroid Build Coastguard WorkerThe GN build
184*61c4878aSAndroid Build Coastguard Worker============
185*61c4878aSAndroid Build Coastguard WorkerThis section describes Pigweed's GN build structure, how it is used upstream,
186*61c4878aSAndroid Build Coastguard Workerbuild conventions, and recommendations for Pigweed-based projects. While
187*61c4878aSAndroid Build Coastguard Workercontaining some details about how GN works in general, this section is not
188*61c4878aSAndroid Build Coastguard Workerintended to be a guide on how to use GN. To learn more about the tool itself,
189*61c4878aSAndroid Build Coastguard Workerrefer to the official `GN reference`_.
190*61c4878aSAndroid Build Coastguard Worker
191*61c4878aSAndroid Build Coastguard Worker.. _GN reference: https://gn.googlesource.com/gn/+/HEAD/docs/reference.md
192*61c4878aSAndroid Build Coastguard Worker
193*61c4878aSAndroid Build Coastguard WorkerEntrypoint: .gn
194*61c4878aSAndroid Build Coastguard Worker---------------
195*61c4878aSAndroid Build Coastguard WorkerThe entrypoint to a GN build is the ``.gn`` file, which defines a project's root
196*61c4878aSAndroid Build Coastguard Workerdirectory (henceforth ``//``).
197*61c4878aSAndroid Build Coastguard Worker
198*61c4878aSAndroid Build Coastguard Worker``.gn`` must point to the location of a ``BUILDCONFIG.gn`` file for the project.
199*61c4878aSAndroid Build Coastguard WorkerIn Pigweed upstream, this is its only purpose.
200*61c4878aSAndroid Build Coastguard Worker
201*61c4878aSAndroid Build Coastguard WorkerDownstream projects may additionally use ``.gn`` to set global overrides for
202*61c4878aSAndroid Build Coastguard WorkerPigweed's build arguments, which apply across all Pigweed targets. For example,
203*61c4878aSAndroid Build Coastguard Workera project could configure the protobuf libraries that it uses. This is done by
204*61c4878aSAndroid Build Coastguard Workerdefining a ``default_args`` scope containing the overrides.
205*61c4878aSAndroid Build Coastguard Worker
206*61c4878aSAndroid Build Coastguard Worker.. code-block::
207*61c4878aSAndroid Build Coastguard Worker
208*61c4878aSAndroid Build Coastguard Worker   # The location of the BUILDCONFIG file.
209*61c4878aSAndroid Build Coastguard Worker   buildconfig = "//BUILDCONFIG.gn"
210*61c4878aSAndroid Build Coastguard Worker
211*61c4878aSAndroid Build Coastguard Worker   # Build arguments set across all Pigweed targets.
212*61c4878aSAndroid Build Coastguard Worker   default_args = {
213*61c4878aSAndroid Build Coastguard Worker     dir_pw_third_party_nanopb = "//third_party/nanopb-0.4.2"
214*61c4878aSAndroid Build Coastguard Worker   }
215*61c4878aSAndroid Build Coastguard Worker
216*61c4878aSAndroid Build Coastguard WorkerConfiguration: BUILDCONFIG.gn
217*61c4878aSAndroid Build Coastguard Worker-----------------------------
218*61c4878aSAndroid Build Coastguard WorkerThe file ``BUILDCONFIG.gn`` configures the GN build by defining any desired
219*61c4878aSAndroid Build Coastguard Workerglobal variables/options. It can be located anywhere in the build tree, but is
220*61c4878aSAndroid Build Coastguard Workerconventionally placed at the root. ``.gn`` points GN to this file.
221*61c4878aSAndroid Build Coastguard Worker
222*61c4878aSAndroid Build Coastguard Worker``BUILDCONFIG.gn`` is evaluated before any other GN files, and variables defined
223*61c4878aSAndroid Build Coastguard Workerwithin it are placed into GN's global scope, becoming available in every file
224*61c4878aSAndroid Build Coastguard Workerwithout requiring imports.
225*61c4878aSAndroid Build Coastguard Worker
226*61c4878aSAndroid Build Coastguard WorkerThe options configured in this file differ from those in ``.gn`` in two ways:
227*61c4878aSAndroid Build Coastguard Worker
228*61c4878aSAndroid Build Coastguard Worker1. ``BUILDCONFIG.gn`` is evaluated for every GN toolchain (and Pigweed target),
229*61c4878aSAndroid Build Coastguard Worker   whereas ``.gn`` is only evaluated once. This allows ``BUILDCONFIG.gn`` to set
230*61c4878aSAndroid Build Coastguard Worker   different options for each Pigweed target.
231*61c4878aSAndroid Build Coastguard Worker2. In ``.gn``, only GN build arguments can be overridden. ``BUILDCONFIG.gn``
232*61c4878aSAndroid Build Coastguard Worker   allows defining arbitrary variables.
233*61c4878aSAndroid Build Coastguard Worker
234*61c4878aSAndroid Build Coastguard WorkerGenerally, it is preferable to expose configuration options through build args
235*61c4878aSAndroid Build Coastguard Workerinstead of globals in ``BUILDCONFIG.gn`` (something Pigweed's build previously
236*61c4878aSAndroid Build Coastguard Workerdid), as they are more flexible, greppable, and easier to manage. However, it
237*61c4878aSAndroid Build Coastguard Workermay make sense to define project-specific constants in ``BUILDCONFIG.gn``.
238*61c4878aSAndroid Build Coastguard Worker
239*61c4878aSAndroid Build Coastguard WorkerPigweed's upstream ``BUILDCONFIG.gn`` does not define any variables; it just
240*61c4878aSAndroid Build Coastguard Workersets Pigweed's default toolchain, which GN requires.
241*61c4878aSAndroid Build Coastguard Worker
242*61c4878aSAndroid Build Coastguard Worker.. _top-level-build:
243*61c4878aSAndroid Build Coastguard Worker
244*61c4878aSAndroid Build Coastguard WorkerTop-level GN targets: //BUILD.gn
245*61c4878aSAndroid Build Coastguard Worker--------------------------------
246*61c4878aSAndroid Build Coastguard WorkerThe root ``BUILD.gn`` file defines all of the libraries, images, tests, and
247*61c4878aSAndroid Build Coastguard Workerbinaries built by a Pigweed project. This file is evaluated immediately after
248*61c4878aSAndroid Build Coastguard Worker``BUILDCONFIG.gn``, with the active toolchain (which is the default toolchain
249*61c4878aSAndroid Build Coastguard Workerat the start of a build).
250*61c4878aSAndroid Build Coastguard Worker
251*61c4878aSAndroid Build Coastguard Worker``//BUILD.gn`` is responsible for enumerating each of the Pigweed targets built
252*61c4878aSAndroid Build Coastguard Workerby a project. This is done by instantiating a version of each of the project's
253*61c4878aSAndroid Build Coastguard WorkerGN target groups with each Pigweed target's toolchain. For example, in upstream,
254*61c4878aSAndroid Build Coastguard Workerall of Pigweed's GN targets are contained within the ``pigweed_default`` group.
255*61c4878aSAndroid Build Coastguard WorkerThis group is instantiated multiple times, with different Pigweed target
256*61c4878aSAndroid Build Coastguard Workertoolchains.
257*61c4878aSAndroid Build Coastguard Worker
258*61c4878aSAndroid Build Coastguard WorkerThese groups include the following:
259*61c4878aSAndroid Build Coastguard Worker
260*61c4878aSAndroid Build Coastguard Worker* ``host`` -- builds ``pigweed_default`` with Clang or GCC, depending on the
261*61c4878aSAndroid Build Coastguard Worker  platform
262*61c4878aSAndroid Build Coastguard Worker* ``host_clang`` -- builds ``pigweed_default`` for the host with Clang
263*61c4878aSAndroid Build Coastguard Worker* ``host_gcc`` -- builds ``pigweed_default`` for the host with GCC
264*61c4878aSAndroid Build Coastguard Worker* ``stm32f429i`` -- builds ``pigweed_default`` for STM32F429i Discovery board
265*61c4878aSAndroid Build Coastguard Worker* ``docs`` -- builds the Pigweed documentation and size reports
266*61c4878aSAndroid Build Coastguard Worker
267*61c4878aSAndroid Build Coastguard WorkerPigweed projects are recommended to follow this pattern, creating a top-level
268*61c4878aSAndroid Build Coastguard Workergroup for each of their Pigweed targets that builds a common GN target with the
269*61c4878aSAndroid Build Coastguard Workerappropriate toolchain.
270*61c4878aSAndroid Build Coastguard Worker
271*61c4878aSAndroid Build Coastguard WorkerIt is important that no dependencies are listed under the default toolchain
272*61c4878aSAndroid Build Coastguard Workerwithin ``//BUILD.gn``, as it does not configure any build parameters, and
273*61c4878aSAndroid Build Coastguard Workertherefore should not evaluate any other GN files. The pattern that Pigweed uses
274*61c4878aSAndroid Build Coastguard Workerto achieve this is to wrap all dependencies within a condition checking the
275*61c4878aSAndroid Build Coastguard Workertoolchain.
276*61c4878aSAndroid Build Coastguard Worker
277*61c4878aSAndroid Build Coastguard Worker.. code-block::
278*61c4878aSAndroid Build Coastguard Worker
279*61c4878aSAndroid Build Coastguard Worker   group("my_application_images") {
280*61c4878aSAndroid Build Coastguard Worker     deps = []  # Empty in the default toolchain.
281*61c4878aSAndroid Build Coastguard Worker
282*61c4878aSAndroid Build Coastguard Worker     if (current_toolchain != default_toolchain) {
283*61c4878aSAndroid Build Coastguard Worker       # This is only evaluated by Pigweed target toolchains, which configure
284*61c4878aSAndroid Build Coastguard Worker       # all of the required options to build Pigweed code.
285*61c4878aSAndroid Build Coastguard Worker       deps += [ "//images:evt" ]
286*61c4878aSAndroid Build Coastguard Worker     }
287*61c4878aSAndroid Build Coastguard Worker   }
288*61c4878aSAndroid Build Coastguard Worker
289*61c4878aSAndroid Build Coastguard Worker   # The images group is instantiated for each of the project's Pigweed targets.
290*61c4878aSAndroid Build Coastguard Worker   group("my_pigweed_target") {
291*61c4878aSAndroid Build Coastguard Worker     deps = [ ":my_application_images(//toolchains:my_pigweed_target)" ]
292*61c4878aSAndroid Build Coastguard Worker   }
293*61c4878aSAndroid Build Coastguard Worker
294*61c4878aSAndroid Build Coastguard Worker.. warning::
295*61c4878aSAndroid Build Coastguard Worker  Pigweed's default toolchain is never used, so it is set to an empty toolchain
296*61c4878aSAndroid Build Coastguard Worker  which doesn't define any tools. ``//BUILD.gn`` contains conditions which check
297*61c4878aSAndroid Build Coastguard Worker  that the current toolchain is not the default before declaring any GN target
298*61c4878aSAndroid Build Coastguard Worker  dependencies to prevent the default toolchain from evaluating any other BUILD
299*61c4878aSAndroid Build Coastguard Worker  files. All GN targets added to the build must be placed under one of these
300*61c4878aSAndroid Build Coastguard Worker  conditional scopes.
301*61c4878aSAndroid Build Coastguard Worker
302*61c4878aSAndroid Build Coastguard Worker"default" group
303*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^
304*61c4878aSAndroid Build Coastguard WorkerThe root ``BUILD.gn`` file can define a special group named ``default``. If
305*61c4878aSAndroid Build Coastguard Workerpresent, Ninja will build this group when invoked without arguments.
306*61c4878aSAndroid Build Coastguard Worker
307*61c4878aSAndroid Build Coastguard Worker.. tip::
308*61c4878aSAndroid Build Coastguard Worker  Defining a ``default`` group makes using ``pw watch`` simple!
309*61c4878aSAndroid Build Coastguard Worker
310*61c4878aSAndroid Build Coastguard WorkerOptimization levels
311*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^
312*61c4878aSAndroid Build Coastguard WorkerPigweed's ``//BUILD.gn`` defines the ``pw_DEFAULT_C_OPTIMIZATION_LEVEL`` build
313*61c4878aSAndroid Build Coastguard Workerarg, which specifies the optimization level to use for the default groups
314*61c4878aSAndroid Build Coastguard Worker(``host``, ``stm32f429i``, etc.). The supported values for
315*61c4878aSAndroid Build Coastguard Worker``pw_DEFAULT_C_OPTIMIZATION_LEVEL`` are:
316*61c4878aSAndroid Build Coastguard Worker
317*61c4878aSAndroid Build Coastguard Worker* ``debug`` -- create debugging-friendly binaries (``-Og``)
318*61c4878aSAndroid Build Coastguard Worker* ``size_optimized`` -- optimize for size (``-Os``)
319*61c4878aSAndroid Build Coastguard Worker* ``speed_optimized`` -- optimized for speed, without increasing code size
320*61c4878aSAndroid Build Coastguard Worker  (``-O2``)
321*61c4878aSAndroid Build Coastguard Worker
322*61c4878aSAndroid Build Coastguard WorkerPigweed defines versions of its groups in ``//BUILD.gn`` for each optimization
323*61c4878aSAndroid Build Coastguard Workerlevel specified in the ``pw_C_OPTIMIZATION_LEVELS`` list. Rather than relying
324*61c4878aSAndroid Build Coastguard Workeron ``pw_DEFAULT_C_OPTIMIZATION_LEVEL``, you may directly build a group at the
325*61c4878aSAndroid Build Coastguard Workerdesired optimization level: ``<group>_<optimization>``. Examples include
326*61c4878aSAndroid Build Coastguard Worker``host_clang_debug``, ``host_gcc_size_optimized``, and
327*61c4878aSAndroid Build Coastguard Worker``stm32f429i_speed_optimized``.
328*61c4878aSAndroid Build Coastguard Worker
329*61c4878aSAndroid Build Coastguard WorkerUpstream GN target groups
330*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^
331*61c4878aSAndroid Build Coastguard WorkerIn upstream, Pigweed splits its top-level GN targets into a few logical groups,
332*61c4878aSAndroid Build Coastguard Workerwhich are described below. In order to build a GN target, it *must* be listed in
333*61c4878aSAndroid Build Coastguard Workerone of the groups in this file.
334*61c4878aSAndroid Build Coastguard Worker
335*61c4878aSAndroid Build Coastguard Worker.. important::
336*61c4878aSAndroid Build Coastguard Worker
337*61c4878aSAndroid Build Coastguard Worker  Pigweed's top-level ``BUILD.gn`` file should not be used by downstream
338*61c4878aSAndroid Build Coastguard Worker  projects. Projects that wish to pull all of Pigweed's code into their build
339*61c4878aSAndroid Build Coastguard Worker  may use the ``pw_modules`` and ``pw_module_tests`` variables in
340*61c4878aSAndroid Build Coastguard Worker  ``modules.gni``.
341*61c4878aSAndroid Build Coastguard Worker
342*61c4878aSAndroid Build Coastguard Workerapps
343*61c4878aSAndroid Build Coastguard Worker~~~~
344*61c4878aSAndroid Build Coastguard WorkerThis group defines the application images built in Pigweed. It lists all of the
345*61c4878aSAndroid Build Coastguard Workercommon images built across all Pigweed targets, such as modules' example
346*61c4878aSAndroid Build Coastguard Workerexecutables. Each Pigweed target can additionally provide its own specific
347*61c4878aSAndroid Build Coastguard Workerimages through the ``pw_TARGET_APPLICATIONS`` build arg, which is included by
348*61c4878aSAndroid Build Coastguard Workerthis group.
349*61c4878aSAndroid Build Coastguard Worker
350*61c4878aSAndroid Build Coastguard Workerhost_tools
351*61c4878aSAndroid Build Coastguard Worker~~~~~~~~~~
352*61c4878aSAndroid Build Coastguard WorkerThis group defines host-side tooling binaries built for Pigweed.
353*61c4878aSAndroid Build Coastguard Worker
354*61c4878aSAndroid Build Coastguard Workerruntime_sanitizers
355*61c4878aSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~
356*61c4878aSAndroid Build Coastguard WorkerThis group defines host-side build targets for Clang runtime sanitizers.
357*61c4878aSAndroid Build Coastguard WorkerNext runtime sanitizers supported:
358*61c4878aSAndroid Build Coastguard Worker
359*61c4878aSAndroid Build Coastguard Worker* ``asan`` -- `AddressSanitizer`_ is a fast memory error detector.
360*61c4878aSAndroid Build Coastguard Worker* ``msan`` -- `MemorySanitizer`_ is a detector of uninitialized reads.
361*61c4878aSAndroid Build Coastguard Worker* ``ubsan`` -- `UndefinedBehaviorSanitizer`_ is a fast undefined behavior detector.
362*61c4878aSAndroid Build Coastguard Worker* ``ubsan_heuristic`` -- `UndefinedBehaviorSanitizer`_ with the following
363*61c4878aSAndroid Build Coastguard Worker  additional checks enabled:
364*61c4878aSAndroid Build Coastguard Worker
365*61c4878aSAndroid Build Coastguard Worker  * ``integer``: Checks for undefined or suspicious integer behavior.
366*61c4878aSAndroid Build Coastguard Worker  * ``float-divide-by-zero``: Checks for floating point division by zero.
367*61c4878aSAndroid Build Coastguard Worker  * ``implicit-conversion``: Checks for suspicious behavior of implicit conversions.
368*61c4878aSAndroid Build Coastguard Worker  * ``nullability``: Checks for null as function arg, lvalue and return type.
369*61c4878aSAndroid Build Coastguard Worker
370*61c4878aSAndroid Build Coastguard Worker  These additional checks are heuristic and may not correspond to undefined
371*61c4878aSAndroid Build Coastguard Worker  behavior.
372*61c4878aSAndroid Build Coastguard Worker* ``tsan`` -- `ThreadSanitizer`_ is a tool that detects data races.
373*61c4878aSAndroid Build Coastguard Worker
374*61c4878aSAndroid Build Coastguard WorkerResults of building this group are ``host_clang_<sanitizer>`` build directories
375*61c4878aSAndroid Build Coastguard Workerwith ``pw_module_tests`` per supported sanitizer.
376*61c4878aSAndroid Build Coastguard Worker
377*61c4878aSAndroid Build Coastguard Worker.. _AddressSanitizer: https://clang.llvm.org/docs/AddressSanitizer.html
378*61c4878aSAndroid Build Coastguard Worker.. _MemorySanitizer: https://clang.llvm.org/docs/MemorySanitizer.html
379*61c4878aSAndroid Build Coastguard Worker.. _UndefinedBehaviorSanitizer: https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
380*61c4878aSAndroid Build Coastguard Worker.. _ThreadSanitizer: https://clang.llvm.org/docs/ThreadSanitizer.html
381*61c4878aSAndroid Build Coastguard Worker
382*61c4878aSAndroid Build Coastguard Workercoverage
383*61c4878aSAndroid Build Coastguard Worker~~~~~~~~~~
384*61c4878aSAndroid Build Coastguard WorkerThis group defines host-side build target for Clang source-based code coverage.
385*61c4878aSAndroid Build Coastguard Worker
386*61c4878aSAndroid Build Coastguard Workerpw_modules
387*61c4878aSAndroid Build Coastguard Worker~~~~~~~~~~
388*61c4878aSAndroid Build Coastguard WorkerThis group lists the main libraries for all of Pigweed's modules.
389*61c4878aSAndroid Build Coastguard Worker
390*61c4878aSAndroid Build Coastguard WorkerThe modules in the ``pw_modules`` group are listed in the ``pw_modules``
391*61c4878aSAndroid Build Coastguard Workervariable, which is provided by ``modules.gni``.
392*61c4878aSAndroid Build Coastguard Worker
393*61c4878aSAndroid Build Coastguard Workerpw_module_tests
394*61c4878aSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~
395*61c4878aSAndroid Build Coastguard WorkerAll modules' unit tests are collected here, so that they can all be run at once.
396*61c4878aSAndroid Build Coastguard Worker
397*61c4878aSAndroid Build Coastguard WorkerThe test groups in ``pw_module_tests`` group are listed in the
398*61c4878aSAndroid Build Coastguard Worker``pw_module_tests`` variable, which is provided by ``modules.gni``.
399*61c4878aSAndroid Build Coastguard Worker
400*61c4878aSAndroid Build Coastguard Workerpigweed_default
401*61c4878aSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~
402*61c4878aSAndroid Build Coastguard WorkerThis group defines everything built in a Pigweed build invocation by collecting
403*61c4878aSAndroid Build Coastguard Workerthe above groups and conditionally depending on them based on the active Pigweed
404*61c4878aSAndroid Build Coastguard Workertarget's configuration. Generally, new dependencies should not be added here;
405*61c4878aSAndroid Build Coastguard Workerinstead, use one of the groups listed above.
406*61c4878aSAndroid Build Coastguard Worker
407*61c4878aSAndroid Build Coastguard WorkerThe ``pigweed_default`` group is instantiated for each upstream Pigweed target's
408*61c4878aSAndroid Build Coastguard Workertoolchain.
409*61c4878aSAndroid Build Coastguard Worker
410*61c4878aSAndroid Build Coastguard WorkerPigweed target instantiations
411*61c4878aSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
412*61c4878aSAndroid Build Coastguard WorkerThese groups wrap ``pigweed_default`` with a specific target toolchain. They are
413*61c4878aSAndroid Build Coastguard Workernamed after the Pigweed target, e.g. ``host_clang``, ``stm32f429i``, etc.
414*61c4878aSAndroid Build Coastguard Worker
415*61c4878aSAndroid Build Coastguard WorkerOther BUILD files: //\*\*/BUILD.gn
416*61c4878aSAndroid Build Coastguard Worker----------------------------------
417*61c4878aSAndroid Build Coastguard WorkerThe rest of the ``BUILD.gn`` files in the tree define libraries, configs, and
418*61c4878aSAndroid Build Coastguard Workerbuild args for each of the modules in a Pigweed project.
419*61c4878aSAndroid Build Coastguard Worker
420*61c4878aSAndroid Build Coastguard WorkerProject configuration: //build_overrides/pigweed.gni
421*61c4878aSAndroid Build Coastguard Worker----------------------------------------------------
422*61c4878aSAndroid Build Coastguard WorkerEach Pigweed project must contain a Pigweed configuration file at a known
423*61c4878aSAndroid Build Coastguard Workerlocation in the GN build tree. Currently, this file only contains a single build
424*61c4878aSAndroid Build Coastguard Workerargument, which must be set to the GN build path to the root of the Pigweed
425*61c4878aSAndroid Build Coastguard Workerrepository within the project.
426*61c4878aSAndroid Build Coastguard Worker
427*61c4878aSAndroid Build Coastguard WorkerModule variables
428*61c4878aSAndroid Build Coastguard Worker----------------
429*61c4878aSAndroid Build Coastguard WorkerAs Pigweed is intended to be a subcomponent of a larger project, it cannot assume
430*61c4878aSAndroid Build Coastguard Workerwhere it or its modules is located. Therefore, Pigweed's upstream BUILD.gn files
431*61c4878aSAndroid Build Coastguard Workerdo not use absolute paths; instead, variables are defined pointing to each of
432*61c4878aSAndroid Build Coastguard WorkerPigweed's modules, set relative to a project-specific ``dir_pigweed``.
433*61c4878aSAndroid Build Coastguard Worker
434*61c4878aSAndroid Build Coastguard WorkerTo depend on Pigweed modules from GN code, import Pigweed's overrides file and
435*61c4878aSAndroid Build Coastguard Workerreference these module variables.
436*61c4878aSAndroid Build Coastguard Worker
437*61c4878aSAndroid Build Coastguard Worker.. code-block::
438*61c4878aSAndroid Build Coastguard Worker
439*61c4878aSAndroid Build Coastguard Worker   # This must be imported before .gni files from any other Pigweed modules. To
440*61c4878aSAndroid Build Coastguard Worker   # prevent gn format from reordering this import, it must be separated by a
441*61c4878aSAndroid Build Coastguard Worker   # blank line from other imports.
442*61c4878aSAndroid Build Coastguard Worker
443*61c4878aSAndroid Build Coastguard Worker   import("//build_overrides/pigweed.gni")
444*61c4878aSAndroid Build Coastguard Worker
445*61c4878aSAndroid Build Coastguard WorkerGN target type wrappers
446*61c4878aSAndroid Build Coastguard Worker-----------------------
447*61c4878aSAndroid Build Coastguard WorkerTo facilitate injecting global configuration options, Pigweed defines wrappers
448*61c4878aSAndroid Build Coastguard Workeraround builtin GN target types such as ``source_set`` and ``executable``. These
449*61c4878aSAndroid Build Coastguard Workerare defined within ``$dir_pw_build/target_types.gni``.
450*61c4878aSAndroid Build Coastguard Worker
451*61c4878aSAndroid Build Coastguard Worker.. note::
452*61c4878aSAndroid Build Coastguard Worker  To take advantage of Pigweed's flexible target configuration system, use
453*61c4878aSAndroid Build Coastguard Worker  ``pw_*`` target types (e.g. ``pw_source_set``) in your BUILD.gn files instead
454*61c4878aSAndroid Build Coastguard Worker  of GN builtins.
455*61c4878aSAndroid Build Coastguard Worker
456*61c4878aSAndroid Build Coastguard WorkerPigweed targets
457*61c4878aSAndroid Build Coastguard Worker---------------
458*61c4878aSAndroid Build Coastguard WorkerTo build for a specific hardware platform, builds define Pigweed targets. These
459*61c4878aSAndroid Build Coastguard Workerare essentially GN toolchains which set special arguments telling Pigweed how to
460*61c4878aSAndroid Build Coastguard Workerbuild. For information on Pigweed's target system, refer to
461*61c4878aSAndroid Build Coastguard Worker:ref:`docs-targets`.
462*61c4878aSAndroid Build Coastguard Worker
463*61c4878aSAndroid Build Coastguard WorkerThe empty toolchain
464*61c4878aSAndroid Build Coastguard Worker-------------------
465*61c4878aSAndroid Build Coastguard WorkerPigweed's ``BUILDCONFIG.gn`` sets the project's default toolchain to an "empty"
466*61c4878aSAndroid Build Coastguard Workertoolchain which does not specify any compilers or override any build arguments.
467*61c4878aSAndroid Build Coastguard WorkerDownstream projects are recommended to do the same, following the steps
468*61c4878aSAndroid Build Coastguard Workerdescribed in :ref:`top-level-build` to configure builds for each of their
469*61c4878aSAndroid Build Coastguard WorkerPigweed targets.
470*61c4878aSAndroid Build Coastguard Worker
471*61c4878aSAndroid Build Coastguard Worker.. admonition:: Why use an empty toolchain?
472*61c4878aSAndroid Build Coastguard Worker
473*61c4878aSAndroid Build Coastguard Worker  To support some of its advanced (and useful!) build features, Pigweed requires
474*61c4878aSAndroid Build Coastguard Worker  the ability to generate new toolchains on the fly. This requires having
475*61c4878aSAndroid Build Coastguard Worker  knowledge of the full configuration of the current toolchain (which is easy if
476*61c4878aSAndroid Build Coastguard Worker  it's all defined within a scope), something which is impractical to achieve
477*61c4878aSAndroid Build Coastguard Worker  using the default toolchain.
478*61c4878aSAndroid Build Coastguard Worker
479*61c4878aSAndroid Build Coastguard Worker  Additionally, there are some cases where GN treats default and non-default
480*61c4878aSAndroid Build Coastguard Worker  toolchains differently. By not using the default toolchain, we avoid having
481*61c4878aSAndroid Build Coastguard Worker  to deal with these inconsistencies.
482*61c4878aSAndroid Build Coastguard Worker
483*61c4878aSAndroid Build Coastguard Worker  It is possible to build Pigweed using only the default toolchain, but it
484*61c4878aSAndroid Build Coastguard Worker  requires a more complicated setup to get everything working and should be
485*61c4878aSAndroid Build Coastguard Worker  avoided unless necessary (for example, when integrating with a large existing
486*61c4878aSAndroid Build Coastguard Worker  GN-based project).
487*61c4878aSAndroid Build Coastguard Worker
488*61c4878aSAndroid Build Coastguard WorkerUpstream development examples
489*61c4878aSAndroid Build Coastguard Worker-----------------------------
490*61c4878aSAndroid Build Coastguard WorkerIf developing for upstream Pigweed, some common build use cases are described
491*61c4878aSAndroid Build Coastguard Workerbelow.
492*61c4878aSAndroid Build Coastguard Worker
493*61c4878aSAndroid Build Coastguard WorkerBuilding a custom executable/app image
494*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
495*61c4878aSAndroid Build Coastguard Worker
496*61c4878aSAndroid Build Coastguard Worker1. Define your executable GN target using the ``pw_executable`` template.
497*61c4878aSAndroid Build Coastguard Worker
498*61c4878aSAndroid Build Coastguard Worker   .. code-block::
499*61c4878aSAndroid Build Coastguard Worker
500*61c4878aSAndroid Build Coastguard Worker      # //foo/BUILD.gn
501*61c4878aSAndroid Build Coastguard Worker      pw_executable("foo") {
502*61c4878aSAndroid Build Coastguard Worker        sources = [ "main.cc" ]
503*61c4878aSAndroid Build Coastguard Worker        deps = [ ":libfoo" ]
504*61c4878aSAndroid Build Coastguard Worker      }
505*61c4878aSAndroid Build Coastguard Worker
506*61c4878aSAndroid Build Coastguard Worker2. In the root ``BUILD.gn`` file, add the executable's GN target to the ``apps``
507*61c4878aSAndroid Build Coastguard Worker   group.
508*61c4878aSAndroid Build Coastguard Worker
509*61c4878aSAndroid Build Coastguard Worker   .. code-block::
510*61c4878aSAndroid Build Coastguard Worker
511*61c4878aSAndroid Build Coastguard Worker      # //BUILD.gn
512*61c4878aSAndroid Build Coastguard Worker      group("apps") {
513*61c4878aSAndroid Build Coastguard Worker        deps = [
514*61c4878aSAndroid Build Coastguard Worker          # ...
515*61c4878aSAndroid Build Coastguard Worker          "//foo",  # Shorthand for //foo:foo
516*61c4878aSAndroid Build Coastguard Worker        ]
517*61c4878aSAndroid Build Coastguard Worker      }
518*61c4878aSAndroid Build Coastguard Worker
519*61c4878aSAndroid Build Coastguard Worker3. Run the ninja build to compile your executable. The apps group is built by
520*61c4878aSAndroid Build Coastguard Worker   default, so there's no need to provide a target. The executable will be
521*61c4878aSAndroid Build Coastguard Worker   compiled for every supported Pigweed target.
522*61c4878aSAndroid Build Coastguard Worker
523*61c4878aSAndroid Build Coastguard Worker   .. code-block::
524*61c4878aSAndroid Build Coastguard Worker
525*61c4878aSAndroid Build Coastguard Worker      ninja -C out
526*61c4878aSAndroid Build Coastguard Worker
527*61c4878aSAndroid Build Coastguard Worker   Alternatively, build your executable by itself by specifying its path to
528*61c4878aSAndroid Build Coastguard Worker   Ninja. When building a GN target manually, the Pigweed target for which it
529*61c4878aSAndroid Build Coastguard Worker   is built must be specified on the Ninja command line.
530*61c4878aSAndroid Build Coastguard Worker
531*61c4878aSAndroid Build Coastguard Worker   For example, to build for the Pigweed target ``host_gcc_debug``:
532*61c4878aSAndroid Build Coastguard Worker
533*61c4878aSAndroid Build Coastguard Worker   .. code-block::
534*61c4878aSAndroid Build Coastguard Worker
535*61c4878aSAndroid Build Coastguard Worker      ninja -C out host_gcc_debug/obj/foo/bin/foo
536*61c4878aSAndroid Build Coastguard Worker
537*61c4878aSAndroid Build Coastguard Worker   .. note::
538*61c4878aSAndroid Build Coastguard Worker
539*61c4878aSAndroid Build Coastguard Worker     The path passed to Ninja is a filesystem path within the ``out`` directory,
540*61c4878aSAndroid Build Coastguard Worker     rather than a GN path. This path can be found by running ``gn outputs``.
541*61c4878aSAndroid Build Coastguard Worker
542*61c4878aSAndroid Build Coastguard Worker4. Retrieve your compiled binary from the out directory. It is located at the
543*61c4878aSAndroid Build Coastguard Worker   path
544*61c4878aSAndroid Build Coastguard Worker
545*61c4878aSAndroid Build Coastguard Worker   .. code-block::
546*61c4878aSAndroid Build Coastguard Worker
547*61c4878aSAndroid Build Coastguard Worker      out/<pw_target>/obj/<gn_path>/{bin,test}/<executable>
548*61c4878aSAndroid Build Coastguard Worker
549*61c4878aSAndroid Build Coastguard Worker   where ``pw_target`` is the Pigweed target for which the binary was built,
550*61c4878aSAndroid Build Coastguard Worker   ``gn_path`` is the GN path to the BUILD.gn file defining the executable,
551*61c4878aSAndroid Build Coastguard Worker   and ``executable`` is the executable's GN target name (potentially with an
552*61c4878aSAndroid Build Coastguard Worker   extension). Note that the executable is located within a ``bin`` subdirectory
553*61c4878aSAndroid Build Coastguard Worker   in the module (or ``test`` for unit tests defined with ``pw_test``).
554*61c4878aSAndroid Build Coastguard Worker
555*61c4878aSAndroid Build Coastguard Worker   For example, the ``foo`` executable defined above and compiled for the
556*61c4878aSAndroid Build Coastguard Worker   Pigweed target stm32f429i_disc1_debug is found at:
557*61c4878aSAndroid Build Coastguard Worker
558*61c4878aSAndroid Build Coastguard Worker   .. code-block::
559*61c4878aSAndroid Build Coastguard Worker
560*61c4878aSAndroid Build Coastguard Worker      out/stm32f429i_disc1_debug/obj/foo/bin/foo
561*61c4878aSAndroid Build Coastguard Worker
562*61c4878aSAndroid Build Coastguard WorkerThe CMake build
563*61c4878aSAndroid Build Coastguard Worker===============
564*61c4878aSAndroid Build Coastguard WorkerA well-known name in C/C++ development, `CMake`_ is widely used by all kinds of
565*61c4878aSAndroid Build Coastguard Workerprojects, including embedded devices. Pigweed's CMake support is provided
566*61c4878aSAndroid Build Coastguard Workerprimarily for projects that have an existing CMake build and wish to integrate
567*61c4878aSAndroid Build Coastguard WorkerPigweed modules.
568*61c4878aSAndroid Build Coastguard Worker
569*61c4878aSAndroid Build Coastguard Worker.. _docs-build-system-bazel:
570*61c4878aSAndroid Build Coastguard Worker
571*61c4878aSAndroid Build Coastguard WorkerThe Bazel build
572*61c4878aSAndroid Build Coastguard Worker===============
573*61c4878aSAndroid Build Coastguard WorkerThis section describes Pigweed's Bazel build structure, how it is used upstream,
574*61c4878aSAndroid Build Coastguard Workerbuild conventions, and recommendations for Pigweed-based projects. While
575*61c4878aSAndroid Build Coastguard Workercontaining some details about how Bazel works in general, this section is not
576*61c4878aSAndroid Build Coastguard Workerintended to be a guide on how to use Bazel. To learn more about the tool itself,
577*61c4878aSAndroid Build Coastguard Workerrefer to the official `Bazel reference`_.
578*61c4878aSAndroid Build Coastguard Worker
579*61c4878aSAndroid Build Coastguard Worker.. _Bazel reference: https://www.bazel.build/
580*61c4878aSAndroid Build Coastguard Worker
581*61c4878aSAndroid Build Coastguard WorkerGeneral usage
582*61c4878aSAndroid Build Coastguard Worker-------------
583*61c4878aSAndroid Build Coastguard WorkerWhile described in more detail in the Bazel docs there a few Bazel features that
584*61c4878aSAndroid Build Coastguard Workerare of particular importance when targeting embedded platforms. The most
585*61c4878aSAndroid Build Coastguard Workercommonly used commands used in bazel are;
586*61c4878aSAndroid Build Coastguard Worker
587*61c4878aSAndroid Build Coastguard Worker.. code-block:: sh
588*61c4878aSAndroid Build Coastguard Worker
589*61c4878aSAndroid Build Coastguard Worker   bazelisk build //your:target
590*61c4878aSAndroid Build Coastguard Worker   bazelisk test //your:target
591*61c4878aSAndroid Build Coastguard Worker   bazelisk coverage //your:target
592*61c4878aSAndroid Build Coastguard Worker
593*61c4878aSAndroid Build Coastguard Worker.. note:: Code coverage support is only available on the host for now.
594*61c4878aSAndroid Build Coastguard Worker
595*61c4878aSAndroid Build Coastguard WorkerBuilding
596*61c4878aSAndroid Build Coastguard Worker^^^^^^^^
597*61c4878aSAndroid Build Coastguard WorkerWhen it comes to building/testing your build target for a specific target
598*61c4878aSAndroid Build Coastguard Workerplatform (e.g. stm32f429i-discovery) a slight variation is required.
599*61c4878aSAndroid Build Coastguard Worker
600*61c4878aSAndroid Build Coastguard Worker.. code-block:: sh
601*61c4878aSAndroid Build Coastguard Worker
602*61c4878aSAndroid Build Coastguard Worker   bazelisk build //your:target \
603*61c4878aSAndroid Build Coastguard Worker     --platforms=@pigweed//pw_build/platforms:lm3s6965evb
604*61c4878aSAndroid Build Coastguard Worker
605*61c4878aSAndroid Build Coastguard WorkerFor more information on how to create your own platforms refer to the official
606*61c4878aSAndroid Build Coastguard Worker`Bazel platforms reference`_. You may also find helpful examples of constraints
607*61c4878aSAndroid Build Coastguard Workerand platforms in the ``//pw_build/platforms`` and ``//pw_build/constraints``
608*61c4878aSAndroid Build Coastguard Workerdirectories.
609*61c4878aSAndroid Build Coastguard Worker
610*61c4878aSAndroid Build Coastguard Worker.. _Bazel platforms reference: https://docs.bazel.build/versions/main/platforms.html
611*61c4878aSAndroid Build Coastguard Worker
612*61c4878aSAndroid Build Coastguard WorkerTesting
613*61c4878aSAndroid Build Coastguard Worker^^^^^^^
614*61c4878aSAndroid Build Coastguard WorkerRunning tests on an embedded target with Bazel is possible although support for
615*61c4878aSAndroid Build Coastguard Workerthis is experimental. The easiest way of achieving this at the moment is to use
616*61c4878aSAndroid Build Coastguard WorkerBazel's ``--run_under`` flag. To make this work create a Bazel target
617*61c4878aSAndroid Build Coastguard Worker(``//your_handler``) that:
618*61c4878aSAndroid Build Coastguard Worker
619*61c4878aSAndroid Build Coastguard Worker1. Takes a single argument (the path to the elf) and uploads the elf to your
620*61c4878aSAndroid Build Coastguard Worker   Pigweed target.
621*61c4878aSAndroid Build Coastguard Worker2. Connects with your target using serial or other communication method.
622*61c4878aSAndroid Build Coastguard Worker3. Listens to the communication transport for the keywords ("PASSED", "FAIL")
623*61c4878aSAndroid Build Coastguard Worker   and returns (0, 1) respectively if one of the keywords is intercepted. (This
624*61c4878aSAndroid Build Coastguard Worker   step assumes you are using the pw_unit_test package and it is configured for
625*61c4878aSAndroid Build Coastguard Worker   your target).
626*61c4878aSAndroid Build Coastguard Worker
627*61c4878aSAndroid Build Coastguard WorkerThen, run:
628*61c4878aSAndroid Build Coastguard Worker
629*61c4878aSAndroid Build Coastguard Worker   .. code-block:: sh
630*61c4878aSAndroid Build Coastguard Worker
631*61c4878aSAndroid Build Coastguard Worker      bazelisk test //your:test --platforms=//your/platform --run_under=//your_handler
632*61c4878aSAndroid Build Coastguard Worker
633*61c4878aSAndroid Build Coastguard WorkerTag conventions
634*61c4878aSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~
635*61c4878aSAndroid Build Coastguard WorkerPigweed observes the standard Bazel test `tag conventions
636*61c4878aSAndroid Build Coastguard Worker<https://bazel.build/reference/test-encyclopedia#tag-conventions>`_. We also
637*61c4878aSAndroid Build Coastguard Workeruse the following additional tags:
638*61c4878aSAndroid Build Coastguard Worker
639*61c4878aSAndroid Build Coastguard Worker*  ``integration``: large, slow integration tests in upstream Pigweed are given
640*61c4878aSAndroid Build Coastguard Worker   the ``integration`` tag.  You can skip running these tests using
641*61c4878aSAndroid Build Coastguard Worker   `--test_tag_filters
642*61c4878aSAndroid Build Coastguard Worker   <https://bazel.build/docs/user-manual#test-tag-filters>`_. For example,
643*61c4878aSAndroid Build Coastguard Worker
644*61c4878aSAndroid Build Coastguard Worker   .. code-block:: sh
645*61c4878aSAndroid Build Coastguard Worker
646*61c4878aSAndroid Build Coastguard Worker      bazelisk test --test_tag_filters=-integration //...
647*61c4878aSAndroid Build Coastguard Worker
648*61c4878aSAndroid Build Coastguard Worker   will run all tests *except* for these integration tests.
649*61c4878aSAndroid Build Coastguard Worker
650*61c4878aSAndroid Build Coastguard Worker*  ``do_not_build``: targets which should not be built in wildcard builds. Any
651*61c4878aSAndroid Build Coastguard Worker   use of this tag should be associated with a TODO pointing to a bug. Prefer
652*61c4878aSAndroid Build Coastguard Worker   ``do_not_build`` to ``manual`` if the target's build graph is correct, and
653*61c4878aSAndroid Build Coastguard Worker   it just fails at the compilation stage.
654*61c4878aSAndroid Build Coastguard Worker
655*61c4878aSAndroid Build Coastguard Worker*  ``do_not_run_test``: test targets which should be built but not executed.
656*61c4878aSAndroid Build Coastguard Worker   Prefer this to ``manual`` or ``do_not_build`` if the code compiles, but the
657*61c4878aSAndroid Build Coastguard Worker   test fails at runtime. Any use of this tag should be associated with a TODO
658*61c4878aSAndroid Build Coastguard Worker   pointing to a bug.
659*61c4878aSAndroid Build Coastguard Worker
660*61c4878aSAndroid Build Coastguard Worker.. _docs-build_system-bazel_coverage:
661*61c4878aSAndroid Build Coastguard Worker
662*61c4878aSAndroid Build Coastguard WorkerCode Coverage
663*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^
664*61c4878aSAndroid Build Coastguard WorkerTODO(`b/304833225 <https://issues.pigweed.dev/issues/304833225>`_): Fix code
665*61c4878aSAndroid Build Coastguard Workercoverage when using the (default) hermetic toolchains.
666*61c4878aSAndroid Build Coastguard Worker
667*61c4878aSAndroid Build Coastguard WorkerMaking use of the code coverage functionality in Bazel is straightforward.
668*61c4878aSAndroid Build Coastguard Worker
669*61c4878aSAndroid Build Coastguard Worker1. Add the following lines to your '.bazelrc'.
670*61c4878aSAndroid Build Coastguard Worker
671*61c4878aSAndroid Build Coastguard Worker   .. code-block:: sh
672*61c4878aSAndroid Build Coastguard Worker
673*61c4878aSAndroid Build Coastguard Worker      coverage --experimental_generate_llvm_lcov
674*61c4878aSAndroid Build Coastguard Worker      coverage --combined_report=lcov
675*61c4878aSAndroid Build Coastguard Worker
676*61c4878aSAndroid Build Coastguard Worker2. Generate a combined lcov coverage report. This will produce a combined lcov
677*61c4878aSAndroid Build Coastguard Worker   coverage report at the path 'bazel-out/_coverage/_coverage_report.dat'. e.g.
678*61c4878aSAndroid Build Coastguard Worker
679*61c4878aSAndroid Build Coastguard Worker   .. code-block:: sh
680*61c4878aSAndroid Build Coastguard Worker
681*61c4878aSAndroid Build Coastguard Worker      bazelisk coverage //pw_log/...
682*61c4878aSAndroid Build Coastguard Worker
683*61c4878aSAndroid Build Coastguard Worker3. View the results using the command line utility 'lcov'.
684*61c4878aSAndroid Build Coastguard Worker
685*61c4878aSAndroid Build Coastguard Worker   .. code-block:: sh
686*61c4878aSAndroid Build Coastguard Worker
687*61c4878aSAndroid Build Coastguard Worker      lcov --list bazel-out/_coverage/_coverage_report.dat
688*61c4878aSAndroid Build Coastguard Worker
689*61c4878aSAndroid Build Coastguard Worker.. _docs-build_system-bazel_link-extra-lib:
690*61c4878aSAndroid Build Coastguard Worker
691*61c4878aSAndroid Build Coastguard WorkerLibraries required at linktime
692*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
693*61c4878aSAndroid Build Coastguard WorkerCertain low-level libraries (:ref:`module-pw_assert`, :ref:`module-pw_log`) are
694*61c4878aSAndroid Build Coastguard Workerprone to cyclic dependencies. Handling assertions and logging requires using
695*61c4878aSAndroid Build Coastguard Workerother libraries, which themselves may use assertions or logging. To remove
696*61c4878aSAndroid Build Coastguard Workerthese cycles, the full implementations of these libraries are placed in special
697*61c4878aSAndroid Build Coastguard Worker*implementation targets* that are not added to their dependencies. Instead,
698*61c4878aSAndroid Build Coastguard Workerevery binary with a dependency on these libraries (direct or indirect) must
699*61c4878aSAndroid Build Coastguard Workerlink against the implementation targets.
700*61c4878aSAndroid Build Coastguard Worker
701*61c4878aSAndroid Build Coastguard WorkerWhat this means in practice is that any ``cc_binary`` that depends on Pigweed
702*61c4878aSAndroid Build Coastguard Workerlibraries should have a dependency on ``//pw_build:default_link_extra_lib``.
703*61c4878aSAndroid Build Coastguard WorkerThis can be added in a couple ways:
704*61c4878aSAndroid Build Coastguard Worker
705*61c4878aSAndroid Build Coastguard Worker#.  Add ``@pigweed//pw_build:default_link_extra_lib`` directly to the ``deps``
706*61c4878aSAndroid Build Coastguard Worker    of every embedded ``cc_binary`` in your project.
707*61c4878aSAndroid Build Coastguard Worker
708*61c4878aSAndroid Build Coastguard Worker    The con is that you may forget to add the dependency to some targets,
709*61c4878aSAndroid Build Coastguard Worker    and will then encounter puzzling linker errors.
710*61c4878aSAndroid Build Coastguard Worker
711*61c4878aSAndroid Build Coastguard Worker#.  Use `link_extra_lib
712*61c4878aSAndroid Build Coastguard Worker    <https://bazel.build/reference/be/c-cpp#cc_binary.link_extra_lib>`_. Set
713*61c4878aSAndroid Build Coastguard Worker    the ``@bazel_tools//tools/cpp:link_extra_libs`` label flag to point to
714*61c4878aSAndroid Build Coastguard Worker    ``@pigweed//pw_build:default_link_extra_lib``, probably using `bazelrc
715*61c4878aSAndroid Build Coastguard Worker    <https://bazel.build/run/bazelrc>`_. This is only likely to work if you
716*61c4878aSAndroid Build Coastguard Worker    specify the ``--experimental_exclude_starlark_flags_from_exec_config``
717*61c4878aSAndroid Build Coastguard Worker    flag, available in Bazel 8 and newer; see `bazelbuild/bazel#22457
718*61c4878aSAndroid Build Coastguard Worker    <https://github.com/bazelbuild/bazel/issues/22457>`__.
719*61c4878aSAndroid Build Coastguard Worker
720*61c4878aSAndroid Build Coastguard Worker    The con is that these libraries are linked into *all* C++ binaries that are
721*61c4878aSAndroid Build Coastguard Worker    part of your project's build, including ones that have no dependencies on
722*61c4878aSAndroid Build Coastguard Worker    Pigweed.
723*61c4878aSAndroid Build Coastguard Worker
724*61c4878aSAndroid Build Coastguard Worker
725*61c4878aSAndroid Build Coastguard WorkerNote that depending on ``@pigweed//pw_build:link_extra_lib`` will
726*61c4878aSAndroid Build Coastguard Worker*unconditionally* include the symbols in the implementation targets in your
727*61c4878aSAndroid Build Coastguard Workerbinary, even if the binary does not use them. If this is a concern (e.g., due
728*61c4878aSAndroid Build Coastguard Workerto the binary size increase), depend only on the individual implementation
729*61c4878aSAndroid Build Coastguard Workertargets you actually require.
730*61c4878aSAndroid Build Coastguard Worker
731*61c4878aSAndroid Build Coastguard WorkerSee :ref:`module-pw_log-circular-deps` and
732*61c4878aSAndroid Build Coastguard Worker:ref:`module-pw_assert-circular-deps` for more information about the specific
733*61c4878aSAndroid Build Coastguard Workermodules that have link-time dependencies.
734*61c4878aSAndroid Build Coastguard Worker
735*61c4878aSAndroid Build Coastguard WorkerConfiguration
736*61c4878aSAndroid Build Coastguard Worker-------------
737*61c4878aSAndroid Build Coastguard WorkerGenerally speaking there are three primary concepts that make up Bazel's
738*61c4878aSAndroid Build Coastguard Workerconfiguration API.
739*61c4878aSAndroid Build Coastguard Worker
740*61c4878aSAndroid Build Coastguard Worker1. Selects
741*61c4878aSAndroid Build Coastguard Worker2. Compatibility lists
742*61c4878aSAndroid Build Coastguard Worker3. Flags/Build settings
743*61c4878aSAndroid Build Coastguard Worker
744*61c4878aSAndroid Build Coastguard WorkerSelects
745*61c4878aSAndroid Build Coastguard Worker^^^^^^^
746*61c4878aSAndroid Build Coastguard WorkerSelects are useful for specifying different dependencies/source depending on the
747*61c4878aSAndroid Build Coastguard Workerplatform that is currently being targeted. For more information on this please
748*61c4878aSAndroid Build Coastguard Workersee the `Bazel selects reference`_. e.g.
749*61c4878aSAndroid Build Coastguard Worker
750*61c4878aSAndroid Build Coastguard Worker.. code-block:: py
751*61c4878aSAndroid Build Coastguard Worker
752*61c4878aSAndroid Build Coastguard Worker   cc_library(
753*61c4878aSAndroid Build Coastguard Worker     name = "some_platform_dependant_library",
754*61c4878aSAndroid Build Coastguard Worker     deps = select({
755*61c4878aSAndroid Build Coastguard Worker       "@platforms//cpu:armv7e-m": [":arm_libs"],
756*61c4878aSAndroid Build Coastguard Worker       "//conditions:default": [":host_libs"],
757*61c4878aSAndroid Build Coastguard Worker     }),
758*61c4878aSAndroid Build Coastguard Worker   )
759*61c4878aSAndroid Build Coastguard Worker
760*61c4878aSAndroid Build Coastguard WorkerCompatibility lists
761*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^
762*61c4878aSAndroid Build Coastguard WorkerCompatibility lists allow you to specify which platforms your targets are
763*61c4878aSAndroid Build Coastguard Workercompatible with. Consider an example where you want to specify that a target is
764*61c4878aSAndroid Build Coastguard Workercompatible with only a host os;
765*61c4878aSAndroid Build Coastguard Worker
766*61c4878aSAndroid Build Coastguard Worker.. code-block:: py
767*61c4878aSAndroid Build Coastguard Worker
768*61c4878aSAndroid Build Coastguard Worker   cc_library(
769*61c4878aSAndroid Build Coastguard Worker     name = "some_host_only_lib",
770*61c4878aSAndroid Build Coastguard Worker     srcs = ["host.cc"],
771*61c4878aSAndroid Build Coastguard Worker     target_compatible_with = select({
772*61c4878aSAndroid Build Coastguard Worker       "@platforms//os:windows": [],
773*61c4878aSAndroid Build Coastguard Worker       "@platforms//os:linux": [],
774*61c4878aSAndroid Build Coastguard Worker       "@platforms//os:ios": [],
775*61c4878aSAndroid Build Coastguard Worker       "@platforms//os:macos": [],
776*61c4878aSAndroid Build Coastguard Worker       "//conditions:default": ["@platforms//:incompatible"],
777*61c4878aSAndroid Build Coastguard Worker     }),
778*61c4878aSAndroid Build Coastguard Worker   )
779*61c4878aSAndroid Build Coastguard Worker
780*61c4878aSAndroid Build Coastguard WorkerIn this case building from or for either Windows/Linux/Mac will be supported, but
781*61c4878aSAndroid Build Coastguard Workerother OS's will fail if this target is explicitly depended on. However if
782*61c4878aSAndroid Build Coastguard Workerbuilding with a wild card for a non-host platform this target will be skipped
783*61c4878aSAndroid Build Coastguard Workerand the build will continue. e.g.
784*61c4878aSAndroid Build Coastguard Worker
785*61c4878aSAndroid Build Coastguard Worker.. code-block:: sh
786*61c4878aSAndroid Build Coastguard Worker
787*61c4878aSAndroid Build Coastguard Worker   bazelisk build //... --platforms=@pigweed//pw_build/platforms:lm3s6965evb
788*61c4878aSAndroid Build Coastguard Worker
789*61c4878aSAndroid Build Coastguard WorkerThis allows for you to easily create compatibility matricies without adversely
790*61c4878aSAndroid Build Coastguard Workeraffecting your ability build your entire repo for a given Pigweed target.
791*61c4878aSAndroid Build Coastguard WorkerFor more detailed information on how to use the target_compatible_with attribute
792*61c4878aSAndroid Build Coastguard Workerplease see `Bazel target_compatible_with reference`_.
793*61c4878aSAndroid Build Coastguard Worker
794*61c4878aSAndroid Build Coastguard Worker.. _docs-build_system-bazel_flags:
795*61c4878aSAndroid Build Coastguard Worker
796*61c4878aSAndroid Build Coastguard WorkerFlags/build settings
797*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^
798*61c4878aSAndroid Build Coastguard WorkerFlags/build settings are particularly useful in scenarios where you may want to
799*61c4878aSAndroid Build Coastguard Workerbe able to quickly inject a dependency from the command line but don't
800*61c4878aSAndroid Build Coastguard Workernecessarily want to create an entirely new set of constraints to use with a
801*61c4878aSAndroid Build Coastguard Workerselect statement.
802*61c4878aSAndroid Build Coastguard Worker
803*61c4878aSAndroid Build Coastguard Worker.. note::
804*61c4878aSAndroid Build Coastguard Worker  The scope for what is possible with build flags/settings goes well beyond
805*61c4878aSAndroid Build Coastguard Worker  what will be described here. For more detailed information on flags/settings
806*61c4878aSAndroid Build Coastguard Worker  please see `Bazel config reference`_.
807*61c4878aSAndroid Build Coastguard Worker
808*61c4878aSAndroid Build Coastguard WorkerA simple example of when it is useful to use a label_flag is when you want to
809*61c4878aSAndroid Build Coastguard Workerswap out a single dependency from the command line. e.g.
810*61c4878aSAndroid Build Coastguard Worker
811*61c4878aSAndroid Build Coastguard Worker.. code-block:: py
812*61c4878aSAndroid Build Coastguard Worker
813*61c4878aSAndroid Build Coastguard Worker   cc_library(
814*61c4878aSAndroid Build Coastguard Worker     name = "some_default_io",
815*61c4878aSAndroid Build Coastguard Worker     srcs = ["default_io.cc"],
816*61c4878aSAndroid Build Coastguard Worker   )
817*61c4878aSAndroid Build Coastguard Worker
818*61c4878aSAndroid Build Coastguard Worker   cc_library(
819*61c4878aSAndroid Build Coastguard Worker     name = "some_other_io",
820*61c4878aSAndroid Build Coastguard Worker     srcs = ["other_io.cc"],
821*61c4878aSAndroid Build Coastguard Worker   )
822*61c4878aSAndroid Build Coastguard Worker
823*61c4878aSAndroid Build Coastguard Worker   label_flag(
824*61c4878aSAndroid Build Coastguard Worker     name = "io",
825*61c4878aSAndroid Build Coastguard Worker     default_build_setting = ":some_default_io",
826*61c4878aSAndroid Build Coastguard Worker   )
827*61c4878aSAndroid Build Coastguard Worker
828*61c4878aSAndroid Build Coastguard Worker   cc_library(
829*61c4878aSAndroid Build Coastguard Worker     name = "some_target_that_needs_io",
830*61c4878aSAndroid Build Coastguard Worker     deps = [":io"],
831*61c4878aSAndroid Build Coastguard Worker   )
832*61c4878aSAndroid Build Coastguard Worker
833*61c4878aSAndroid Build Coastguard WorkerFrom here the label_flag by default redirects to the target ":some_default_io",
834*61c4878aSAndroid Build Coastguard Workerhowever it is possible to override this from the command line. e.g.
835*61c4878aSAndroid Build Coastguard Worker
836*61c4878aSAndroid Build Coastguard Worker.. code-block:: sh
837*61c4878aSAndroid Build Coastguard Worker
838*61c4878aSAndroid Build Coastguard Worker   bazelisk build //:some_target_that_needs_io --//:io=//:some_other_io
839*61c4878aSAndroid Build Coastguard Worker
840*61c4878aSAndroid Build Coastguard Worker
841*61c4878aSAndroid Build Coastguard Worker
842*61c4878aSAndroid Build Coastguard Worker.. _Bazel selects reference: https://docs.bazel.build/versions/main/configurable-attributes.html#select-and-dependencies
843*61c4878aSAndroid Build Coastguard Worker
844*61c4878aSAndroid Build Coastguard Worker.. _Bazel target_compatible_with reference: https://docs.bazel.build/versions/main/platforms.html#skipping-incompatible-targets
845*61c4878aSAndroid Build Coastguard Worker
846*61c4878aSAndroid Build Coastguard Worker.. _Bazel config reference: https://docs.bazel.build/versions/main/skylark/config.html
847*61c4878aSAndroid Build Coastguard Worker
848*61c4878aSAndroid Build Coastguard Worker.. _docs-build_system-bazel_configuration:
849*61c4878aSAndroid Build Coastguard Worker
850*61c4878aSAndroid Build Coastguard WorkerFacades and backends tutorial
851*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
852*61c4878aSAndroid Build Coastguard WorkerThis section walks you through an example of configuring :ref:`facade
853*61c4878aSAndroid Build Coastguard Worker<module-pw_build-bazel-pw_facade>` backends in a Pigweed Bazel
854*61c4878aSAndroid Build Coastguard Workerproject.
855*61c4878aSAndroid Build Coastguard Worker
856*61c4878aSAndroid Build Coastguard WorkerConsider a scenario that you are building a flight controller for a spacecraft.
857*61c4878aSAndroid Build Coastguard WorkerBut you have very little experience with Pigweed and have just landed here.
858*61c4878aSAndroid Build Coastguard WorkerFirst things first, you would set up your WORKSPACE to fetch Pigweed
859*61c4878aSAndroid Build Coastguard Workerrepository. Then, add the dependencies that you need from Pigweed's WORKSPACE.
860*61c4878aSAndroid Build Coastguard Worker
861*61c4878aSAndroid Build Coastguard WorkerMaybe you want to try using the :ref:`pw_chrono <module-pw_chrono>` module. So
862*61c4878aSAndroid Build Coastguard Workeryou create a target in your repository like so:
863*61c4878aSAndroid Build Coastguard Worker
864*61c4878aSAndroid Build Coastguard Worker.. code-block:: python
865*61c4878aSAndroid Build Coastguard Worker
866*61c4878aSAndroid Build Coastguard Worker   # BUILD.bazel
867*61c4878aSAndroid Build Coastguard Worker   cc_library(
868*61c4878aSAndroid Build Coastguard Worker     name = "time_is_relative",
869*61c4878aSAndroid Build Coastguard Worker     srcs = ["relative_time_on_earth.cc"],
870*61c4878aSAndroid Build Coastguard Worker     deps = ["@pigweed//pw_chrono:system_clock"],
871*61c4878aSAndroid Build Coastguard Worker   )
872*61c4878aSAndroid Build Coastguard Worker
873*61c4878aSAndroid Build Coastguard WorkerThis should work out of the box for any host operating system. E.g., running,
874*61c4878aSAndroid Build Coastguard Worker
875*61c4878aSAndroid Build Coastguard Worker.. code-block:: console
876*61c4878aSAndroid Build Coastguard Worker
877*61c4878aSAndroid Build Coastguard Worker   bazelisk build //:time_is_relative
878*61c4878aSAndroid Build Coastguard Worker
879*61c4878aSAndroid Build Coastguard Workerwill produce a working library for your host OS.
880*61c4878aSAndroid Build Coastguard Worker
881*61c4878aSAndroid Build Coastguard WorkerUsing Pigweed-provided backends
882*61c4878aSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
883*61c4878aSAndroid Build Coastguard WorkerBut you're probably here because Pigweed offers a set of embedded libraries,
884*61c4878aSAndroid Build Coastguard Workerand might be interested in running your code on some micro-controller/FPGA
885*61c4878aSAndroid Build Coastguard Workercombined with an RTOS.  For now let's assume you are using FreeRTOS and are
886*61c4878aSAndroid Build Coastguard Workerhappy to make use of our default ``//pw_chrono`` backend for FreeRTOS. You
887*61c4878aSAndroid Build Coastguard Workercould build your library with:
888*61c4878aSAndroid Build Coastguard Worker
889*61c4878aSAndroid Build Coastguard Worker.. code-block:: console
890*61c4878aSAndroid Build Coastguard Worker
891*61c4878aSAndroid Build Coastguard Worker   bazelisk build \
892*61c4878aSAndroid Build Coastguard Worker     --@pigweed/pw_chrono:system_clock_backend=@pigweed//pw_chrono_freertos:system_clock \
893*61c4878aSAndroid Build Coastguard Worker     //:time_is_relative
894*61c4878aSAndroid Build Coastguard Worker
895*61c4878aSAndroid Build Coastguard WorkerThen, ``//pw_chrono:system_clock`` will use the FreeRTOS backend
896*61c4878aSAndroid Build Coastguard Worker``//pw_chrono_freertos:system_clock``.
897*61c4878aSAndroid Build Coastguard Worker
898*61c4878aSAndroid Build Coastguard WorkerHow does this work?  Here's the relevant part of the dependency tree for your
899*61c4878aSAndroid Build Coastguard Workertarget:
900*61c4878aSAndroid Build Coastguard Worker
901*61c4878aSAndroid Build Coastguard Worker.. code-block::
902*61c4878aSAndroid Build Coastguard Worker
903*61c4878aSAndroid Build Coastguard Worker   //:time_is_relative
904*61c4878aSAndroid Build Coastguard Worker    |
905*61c4878aSAndroid Build Coastguard Worker    v
906*61c4878aSAndroid Build Coastguard Worker   @pigweed//pw_chrono:system_clock  -------> @pigweed//pw_chrono:system_clock_backend
907*61c4878aSAndroid Build Coastguard Worker    |                                                    (Injectable)
908*61c4878aSAndroid Build Coastguard Worker    |                                                         |
909*61c4878aSAndroid Build Coastguard Worker    |                                                         v
910*61c4878aSAndroid Build Coastguard Worker    |                                         @pigweed//pw_chrono_freertos:system_clock
911*61c4878aSAndroid Build Coastguard Worker    |                                                   (Actual backend)
912*61c4878aSAndroid Build Coastguard Worker    v                                                         |
913*61c4878aSAndroid Build Coastguard Worker   @pigweed//pw_chrono:system_clock.facade <------------------.
914*61c4878aSAndroid Build Coastguard Worker
915*61c4878aSAndroid Build Coastguard WorkerWhen building ``//:time_is_relative``, Bazel checks the dependencies of
916*61c4878aSAndroid Build Coastguard Worker``@pigweed//pw_chrono:system_clock`` and finds that it depends on
917*61c4878aSAndroid Build Coastguard Worker``@pigweed//pw_chrono:system_clock_backend``, which looks like this:
918*61c4878aSAndroid Build Coastguard Worker
919*61c4878aSAndroid Build Coastguard Worker.. code-block:: python
920*61c4878aSAndroid Build Coastguard Worker
921*61c4878aSAndroid Build Coastguard Worker   # @pigweed//pw_chrono/BUILD.bazel
922*61c4878aSAndroid Build Coastguard Worker
923*61c4878aSAndroid Build Coastguard Worker   label_flag(
924*61c4878aSAndroid Build Coastguard Worker       name = "system_clock_backend",
925*61c4878aSAndroid Build Coastguard Worker       build_setting_default = ":unspecified system_clock_backend",
926*61c4878aSAndroid Build Coastguard Worker   )
927*61c4878aSAndroid Build Coastguard Worker
928*61c4878aSAndroid Build Coastguard WorkerThis is a  `label_flag
929*61c4878aSAndroid Build Coastguard Worker<https://bazel.build/extending/config#label-typed-build-settings>`_: a
930*61c4878aSAndroid Build Coastguard Workerdependency edge in the build graph that can be overridden by command line
931*61c4878aSAndroid Build Coastguard Workerflags. By setting
932*61c4878aSAndroid Build Coastguard Worker
933*61c4878aSAndroid Build Coastguard Worker.. code-block:: console
934*61c4878aSAndroid Build Coastguard Worker
935*61c4878aSAndroid Build Coastguard Worker   --@pigweed//pw_chrono:system_clock_backend=\
936*61c4878aSAndroid Build Coastguard Worker     @pigweed//pw_chrono_freertos:system_clock
937*61c4878aSAndroid Build Coastguard Worker
938*61c4878aSAndroid Build Coastguard Workeron the command line, you told Bazel to override the default and use the
939*61c4878aSAndroid Build Coastguard WorkerFreeRTOS backend.
940*61c4878aSAndroid Build Coastguard Worker
941*61c4878aSAndroid Build Coastguard WorkerDefining a custom backend
942*61c4878aSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~
943*61c4878aSAndroid Build Coastguard WorkerContinuing with our scenario, let's say that you have read
944*61c4878aSAndroid Build Coastguard Worker:ref:`docs-module-structure` and now want to implement your own backend for
945*61c4878aSAndroid Build Coastguard Worker``//pw_chrono:system_clock`` using a hardware RTC. In this case you would
946*61c4878aSAndroid Build Coastguard Workercreate a new directory ``pw_chrono_my_hardware_rtc``, containing some header
947*61c4878aSAndroid Build Coastguard Workerfiles and a BUILD file like,
948*61c4878aSAndroid Build Coastguard Worker
949*61c4878aSAndroid Build Coastguard Worker.. code-block:: python
950*61c4878aSAndroid Build Coastguard Worker
951*61c4878aSAndroid Build Coastguard Worker   # //pw_chrono_my_hardware_rtc/BUILD.bazel
952*61c4878aSAndroid Build Coastguard Worker
953*61c4878aSAndroid Build Coastguard Worker   cc_library(
954*61c4878aSAndroid Build Coastguard Worker       name = "system_clock",
955*61c4878aSAndroid Build Coastguard Worker       hdrs = [
956*61c4878aSAndroid Build Coastguard Worker           "public/pw_chrono_stl/system_clock_config.h",
957*61c4878aSAndroid Build Coastguard Worker           "public/pw_chrono_stl/system_clock_inline.h",
958*61c4878aSAndroid Build Coastguard Worker           "public_overrides/pw_chrono_backend/system_clock_config.h",
959*61c4878aSAndroid Build Coastguard Worker           "public_overrides/pw_chrono_backend/system_clock_inline.h",
960*61c4878aSAndroid Build Coastguard Worker       ],
961*61c4878aSAndroid Build Coastguard Worker       includes = [
962*61c4878aSAndroid Build Coastguard Worker           "public",
963*61c4878aSAndroid Build Coastguard Worker           "public_overrides",
964*61c4878aSAndroid Build Coastguard Worker       ],
965*61c4878aSAndroid Build Coastguard Worker       deps = [
966*61c4878aSAndroid Build Coastguard Worker           "@pigweed//pw_chrono:system_clock.facade",
967*61c4878aSAndroid Build Coastguard Worker       ],
968*61c4878aSAndroid Build Coastguard Worker   )
969*61c4878aSAndroid Build Coastguard Worker
970*61c4878aSAndroid Build Coastguard WorkerTo build your ``//:time_is_relative`` target using this backend, you'd run,
971*61c4878aSAndroid Build Coastguard Worker
972*61c4878aSAndroid Build Coastguard Worker.. code-block:: console
973*61c4878aSAndroid Build Coastguard Worker
974*61c4878aSAndroid Build Coastguard Worker   bazelisk build //:time_is_relative \
975*61c4878aSAndroid Build Coastguard Worker     --@pigweed//pw_chrono:system_clock_backend=//pw_chrono_my_hardware_rtc:system_clock
976*61c4878aSAndroid Build Coastguard Worker
977*61c4878aSAndroid Build Coastguard WorkerThis modifies the build graph to look something like this:
978*61c4878aSAndroid Build Coastguard Worker
979*61c4878aSAndroid Build Coastguard Worker.. code-block::
980*61c4878aSAndroid Build Coastguard Worker
981*61c4878aSAndroid Build Coastguard Worker   //:time_is_relative
982*61c4878aSAndroid Build Coastguard Worker    |
983*61c4878aSAndroid Build Coastguard Worker    v
984*61c4878aSAndroid Build Coastguard Worker   @pigweed//pw_chrono:system_clock  -------> @pigweed//pw_chrono:system_clock_backend
985*61c4878aSAndroid Build Coastguard Worker    |                                                    (Injectable)
986*61c4878aSAndroid Build Coastguard Worker    |                                                         |
987*61c4878aSAndroid Build Coastguard Worker    |                                                         v
988*61c4878aSAndroid Build Coastguard Worker    |                                         //pw_chrono_my_hardware_rtc:system_clock
989*61c4878aSAndroid Build Coastguard Worker    |                                                   (Actual backend)
990*61c4878aSAndroid Build Coastguard Worker    v                                                         |
991*61c4878aSAndroid Build Coastguard Worker   @pigweed//pw_chrono:system_clock.facade <------------------.
992*61c4878aSAndroid Build Coastguard Worker
993*61c4878aSAndroid Build Coastguard WorkerAssociating backends with platforms through bazelrc
994*61c4878aSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
995*61c4878aSAndroid Build Coastguard WorkerAs your project grows, you will want to select backends for an increasing
996*61c4878aSAndroid Build Coastguard Workernumber of facades. The right backend to choose will depend on the target
997*61c4878aSAndroid Build Coastguard Workerplatform (host vs embedded, with potentially multiple target embedded
998*61c4878aSAndroid Build Coastguard Workerplatforms). Managing this directly through command-line flags is generally an
999*61c4878aSAndroid Build Coastguard Workeranti-pattern. Instead, group these flags into configs in your
1000*61c4878aSAndroid Build Coastguard Worker`bazelrc <https://bazel.build/run/bazelrc>`_. Eventually, your bazelrc may look
1001*61c4878aSAndroid Build Coastguard Workersomething like this:
1002*61c4878aSAndroid Build Coastguard Worker
1003*61c4878aSAndroid Build Coastguard Worker.. code-block:: sh
1004*61c4878aSAndroid Build Coastguard Worker
1005*61c4878aSAndroid Build Coastguard Worker   # The Cortex M7 config
1006*61c4878aSAndroid Build Coastguard Worker   build:m7 --@pigweed//pw_chrono:system_clock_backend=//pw_chrono_my_hardware_rtc:system_clock
1007*61c4878aSAndroid Build Coastguard Worker   build:m7 --@pigweed//pw_sys_io:backend=//cortex-m7:sys_io
1008*61c4878aSAndroid Build Coastguard Worker
1009*61c4878aSAndroid Build Coastguard Worker   # The Cortex M4 config
1010*61c4878aSAndroid Build Coastguard Worker   build:m4 --@pigweed//pw_chrono:system_clock_backend=//pw_chrono_my_hardware_rtc:system_clock
1011*61c4878aSAndroid Build Coastguard Worker   build:m4 --@pigweed//pw_sys_io:backend=//cortex-m4:sys_io
1012*61c4878aSAndroid Build Coastguard Worker   build:m4 --@pigweed//pw_log:backend=@pigweed//pw_log_string
1013*61c4878aSAndroid Build Coastguard Worker   build:m4 --@pigweed//pw_log_string:handler_backend=@pigweed//pw_system:log_backend
1014*61c4878aSAndroid Build Coastguard Worker
1015*61c4878aSAndroid Build Coastguard WorkerThen, to build your library for a particular configuration, on the command line
1016*61c4878aSAndroid Build Coastguard Workeryou just specify the ``--config`` on the command line:
1017*61c4878aSAndroid Build Coastguard Worker
1018*61c4878aSAndroid Build Coastguard Worker.. code-block:: console
1019*61c4878aSAndroid Build Coastguard Worker
1020*61c4878aSAndroid Build Coastguard Worker   bazelisk build --config=m4 //:time_is_relative
1021