aboutsummaryrefslogtreecommitdiff
path: root/pw_build/bazel.rst
blob: bc3c0427ad0e4ffbd94b342fc380ea049a6a3c16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
Bazel
=====
Bazel is currently very experimental, and only builds for host and ARM Cortex-M
microcontrollers.

.. _module-pw_build-bazel-wrapper-rules:

Wrapper rules
-------------
The common configuration for Bazel for all modules is in the ``pigweed.bzl``
file. The built-in Bazel rules ``cc_binary``, ``cc_library``, and ``cc_test``
are wrapped with ``pw_cc_binary``, ``pw_cc_library``, and ``pw_cc_test``.
These wrappers add parameters to calls to the compiler and linker.

pw_linker_script
----------------
In addition to wrapping the built-in rules, Pigweed also provides a custom
rule for handling linker scripts with Bazel. e.g.

.. code-block:: python

  pw_linker_script(
    name = "some_linker_script",
    linker_script = ":some_configurable_linker_script.ld",
    defines = [
        "PW_BOOT_FLASH_BEGIN=0x08000200",
        "PW_BOOT_FLASH_SIZE=1024K",
        "PW_BOOT_HEAP_SIZE=112K",
        "PW_BOOT_MIN_STACK_SIZE=1K",
        "PW_BOOT_RAM_BEGIN=0x20000000",
        "PW_BOOT_RAM_SIZE=192K",
        "PW_BOOT_VECTOR_TABLE_BEGIN=0x08000000",
        "PW_BOOT_VECTOR_TABLE_SIZE=512",
    ],
  )

  # You can include the linker script in the deps.
  cc_binary(
    name = "some_binary",
    srcs = ["some_source.cc"],
    deps = [":some_linker_script"],
  )

  # Alternatively, you can use additional_linker_inputs and linkopts. This
  # allows you to explicitly specify the command line order of linker scripts,
  # and may be useful if your project defines more than one.
  cc_binary(
    name = "some_binary",
    srcs = ["some_source.cc"],
    additional_linker_inputs = [":some_linker_script"],
    linkopts = ["-T $(location :some_linker_script)"],
  )

.. _module-pw_build-bazel-pw_cc_facade:

pw_cc_facade
------------
In Bazel, a :ref:`facade <docs-module-structure-facades>` module has a few
components:

#. The **facade target**, i.e. the interface to the module. This is what
   *backend implementations* depend on to know what interface they're supposed
   to implement.  The facade is declared by creating a ``pw_cc_facade`` target,
   which is just a thin wrapper for ``cc_library``. For example,

   .. code-block:: python

     pw_cc_facade(
         name = "binary_semaphore_facade",
         # The header that constitues the facade.
         hdrs = [
             "public/pw_sync/binary_semaphore.h",
         ],
         includes = ["public"],
         # Dependencies of this header.
         deps = [
             "//pw_chrono:system_clock",
             "//pw_preprocessor",
         ],
     )

   .. note::
     As pure interfaces, ``pw_cc_facade`` targets should not include any source
     files. Backend-independent source files should be placed in the "library
     target" instead.

#. The **library target**, i.e. both the facade (interface) and backend
   (implementation). This is what *users of the module* depend on. It's a
   regular ``pw_cc_library`` that exposes the same headers as the facade, but
   has a dependency on the "backend label flag" (discussed next). It may also
   include some source files (if these are backend-independent). For example,

   .. code-block:: python

     pw_cc_library(
         name = "binary_semaphore",
         # A backend-independent source file.
         srcs = [
             "binary_semaphore.cc",
         ],
         # The same header as exposed by the facade.
         hdrs = [
             "public/pw_sync/binary_semaphore.h",
         ],
         deps = [
             # Dependencies of this header
             "//pw_chrono:system_clock",
             "//pw_preprocessor",
             # The backend, hidden behind a label_flag.
             "@pigweed//targets:pw_sync_binary_semaphore_backend",
         ],
     )

   .. note::
     You may be tempted to reduce duplication in the BUILD.bazel files and
     simply add the facade target to the ``deps`` of the library target,
     instead of re-declaring the facade's ``hdrs`` and ``deps``. *Do not do
     this!* It's a layering check violation: the facade headers provide the
     module's interface, and should be directly exposed by the target the users
     depend on.

#. The **backend label flag**. This is a `label_flag
   <https://bazel.build/extending/config#label-typed-build-settings>`_: a
   dependency edge in the build graph that can be overridden by downstream projects.
   For facades defined in upstream Pigweed, the ``label_flags`` are collected in
   ``//targets/BUILD.bazel``.

#. The **backend target** implements a particular backend for a facade. It's
   just a plain ``pw_cc_library``, with a dependency on the facade target. For example,

   .. code-block:: python

     pw_cc_library(
         name = "binary_semaphore",
         srcs = [
             "binary_semaphore.cc",
         ],
         hdrs = [
             "public/pw_sync_stl/binary_semaphore_inline.h",
             "public/pw_sync_stl/binary_semaphore_native.h",
             "public_overrides/pw_sync_backend/binary_semaphore_inline.h",
             "public_overrides/pw_sync_backend/binary_semaphore_native.h",
         ],
         includes = [
             "public",
             "public_overrides",
         ],
         deps = [
             # Dependencies of the backend's headers and sources.
             "//pw_assert",
             "//pw_chrono:system_clock",
             # A dependency on the facade target, which defines the interface
             # this backend target implements.
             "//pw_sync:binary_semaphore_facade",
         ],
     )

   If a project uses only one backend for a given facade, the backend label
   flag should point at that backend target.

#. The **facade constraint setting** and **backend constraint values**. Every
   facade has an associated `constraint setting
   <https://bazel.build/concepts/platforms#api-review>`_ (enum used in platform
   definition), and each backend for this facade has an associated
   ``constraint_value`` (enum value). Example:

   .. code-block:: python

     # //pw_sync/BUILD.bazel
     constraint_setting(
       name = "binary_semaphore_backend_constraint_setting",
     )

     # //pw_sync_stl/BUILD.bazel
     constraint_value(
       name = "binary_semaphore_backend",
       constraint_setting = "//pw_sync:binary_semaphore_backend_constraint_setting",
     )

     # //pw_sync_freertos/BUILD.bazel
     constraint_value(
       name = "binary_semaphore_backend",
       constraint_setting = "//pw_sync:binary_semaphore_backend_constraint_setting",
     )

   `Target platforms <https://bazel.build/extending/platforms>`_ for Pigweed
   projects should indicate which backend they select for each facade by
   listing the corresponding ``constraint_value`` in their definition. This can
   be used in a couple of ways:

   #.  It allows projects to switch between multiple backends based only on the
       `target platform <https://bazel.build/extending/platforms>`_ using a
       *backend multiplexer* (see below) instead of setting label flags in
       their ``.bazelrc``.

   #.  It allows tests or libraries that only support a particular backend to
       express this through the `target_compatible_with
       <https://bazel.build/reference/be/common-definitions#common.target_compatible_with>`_
       attribute. Bazel will use this to `automatically skip incompatible
       targets in wildcard builds
       <https://bazel.build/extending/platforms#skipping-incompatible-targets>`_.

#. The **backend multiplexer**. If a project uses more than one backend for a
   given facade (e.g., it uses different backends for host and embedded target
   builds), the backend label flag will point to a target that resolves to the
   correct backend based on the target platform. This will typically be an
   `alias <https://bazel.build/reference/be/general#alias>`_ with a ``select``
   statement mapping constraint values to the appropriate backend targets. For
   example,

   .. code-block:: python

     alias(
         name = "pw_sync_binary_semaphore_backend_multiplexer",
         actual = select({
             "//pw_sync_stl:binary_semaphore_backend": "@pigweed//pw_sync_stl:binary_semaphore",
             "//pw_sync_freertos:binary_semaphore_backend": "@pigweed//pw_sync_freertos:binary_semaphore_backend",
             # If we're building for a host OS, use the STL backend.
             "@platforms//os:macos": "@pigweed//pw_sync_stl:binary_semaphore",
             "@platforms//os:linux": "@pigweed//pw_sync_stl:binary_semaphore",
             "@platforms//os:windows": "@pigweed//pw_sync_stl:binary_semaphore",
             # Unless the target platform is the host platform, it must
             # explicitly specify which backend to use. The unspecified_backend
             # is not compatible with any platform; taking this branch will produce
             # an informative error.
             "//conditions:default": "@pigweed//pw_build:unspecified_backend",
         }),
     )

pw_cc_blob_library
------------------
The ``pw_cc_blob_library`` rule is useful for embedding binary data into a
program. The rule takes in a mapping of symbol names to file paths, and
generates a set of C++ source and header files that embed the contents of the
passed-in files as arrays of ``std::byte``.

The blob byte arrays are constant initialized and are safe to access at any
time, including before ``main()``.

``pw_cc_blob_library`` is also available in the :ref:`GN <module-pw_build-cc_blob_library>`
and CMake builds.

Arguments
^^^^^^^^^
* ``blobs``: A list of ``pw_cc_blob_info`` targets, where each target
  corresponds to a binary blob to be transformed from file to byte array. This
  is a required field. ``pw_cc_blob_info`` attributes include:

  * ``symbol_name``: The C++ symbol for the byte array.
  * ``file_path``: The file path for the binary blob.
  * ``linker_section``: If present, places the byte array in the specified
    linker section.
  * ``alignas``: If present, uses the specified string verbatim in
    the ``alignas()`` specifier for the byte array.

* ``out_header``: The header file to generate. Users will include this file
  exactly as it is written here to reference the byte arrays.
* ``namespace``: C++ namespace to place the generated blobs within.

Example
^^^^^^^
**BUILD.bazel**

.. code-block::

   pw_cc_blob_info(
     name = "foo_blob",
     file_path = "foo.bin",
     symbol_name = "kFooBlob",
   )

   pw_cc_blob_info(
     name = "bar_blob",
     file_path = "bar.bin",
     symbol_name = "kBarBlob",
     linker_section = ".bar_section",
   )

   pw_cc_blob_library(
     name = "foo_bar_blobs",
     blobs = [
       ":foo_blob",
       ":bar_blob",
     ],
     out_header = "my/stuff/foo_bar_blobs.h",
     namespace = "my::stuff",
   )

.. note:: If the binary blobs are generated as part of the build, be sure to
          list them as deps to the pw_cc_blob_library target.

**Generated Header**

.. code-block::

   #pragma once

   #include <array>
   #include <cstddef>

   namespace my::stuff {

   extern const std::array<std::byte, 100> kFooBlob;

   extern const std::array<std::byte, 50> kBarBlob;

   }  // namespace my::stuff

**Generated Source**

.. code-block::

   #include "my/stuff/foo_bar_blobs.h"

   #include <array>
   #include <cstddef>

   #include "pw_preprocessor/compiler.h"

   namespace my::stuff {

   const std::array<std::byte, 100> kFooBlob = { ... };

   PW_PLACE_IN_SECTION(".bar_section")
   const std::array<std::byte, 50> kBarBlob = { ... };

   }  // namespace my::stuff

Toolchains and platforms
------------------------
Pigweed provides clang-based host toolchains for Linux and Mac Arm gcc
toolchain. The clang-based Linux and Arm gcc toolchains are entirely hermetic.
We don't currently provide a host toolchain for Windows.