blob: ad43bffd7956b90961e866ee8ec7692a6cf71929 [file] [log] [blame]
Wyatt Heplerf9fb90f2020-09-30 18:59:33 -07001.. _module-pw_build:
Alexei Frolov9d169d52020-03-03 17:20:06 -08002
Rob Mohr84f234e2019-12-06 09:16:50 -08003--------
4pw_build
5--------
Alexei Frolov9d169d52020-03-03 17:20:06 -08006Pigweed's modules aim to be easily integratable into both new and existing
7embedded projects. To that goal, the ``pw_build`` module provides support for
8multiple build systems. Our personal favorite is `GN`_/`Ninja`_, which is used
9by upstream developers for its speed and flexibility. `CMake`_ and `Bazel`_
10build files are also provided by all modules, allowing Pigweed to be added to a
11project with minimal effort.
Rob Mohr84f234e2019-12-06 09:16:50 -080012
13.. _GN: https://gn.googlesource.com/gn/
14.. _Ninja: https://ninja-build.org/
Alexei Frolov9d169d52020-03-03 17:20:06 -080015.. _CMake: https://cmake.org/
Rob Mohr84f234e2019-12-06 09:16:50 -080016.. _Bazel: https://bazel.build/
17
Alexei Frolov9d169d52020-03-03 17:20:06 -080018Beyond just compiling code, Pigweed’s GN build system can also:
19
20* Generate HTML documentation, via our Sphinx integration (with ``pw_docgen``)
Armando Montanez0054a9b2020-03-13 13:06:24 -070021* Display memory usage report cards (with ``pw_bloat``)
Alexei Frolov9d169d52020-03-03 17:20:06 -080022* Incrementally run unit tests after code changes (with ``pw_target_runner``)
23* And more!
24
25These are only supported in the GN build, so we recommend using it if possible.
26
Rob Mohr84f234e2019-12-06 09:16:50 -080027GN / Ninja
28==========
Armando Montanezbcc194b2020-03-10 10:23:18 -070029The GN / Ninja build system is the primary build system used for upstream
30Pigweed development, and is the most tested and feature-rich build system
31Pigweed offers.
Rob Mohr84f234e2019-12-06 09:16:50 -080032
Armando Montanezbcc194b2020-03-10 10:23:18 -070033This module's ``build.gn`` file contains a number of C/C++ ``config``
34declarations that are used by upstream Pigweed to set some architecture-agnostic
35compiler defaults. (See Pigweed's ``//BUILDCONFIG.gn``)
36
Armando Montanez0054a9b2020-03-13 13:06:24 -070037``pw_build`` also provides several useful GN templates that are used throughout
Armando Montanezbcc194b2020-03-10 10:23:18 -070038Pigweed.
Alexei Frolov69ad1922019-12-13 13:11:32 -080039
Alexei Frolovedd2f142020-06-09 19:11:27 -070040Target types
Wyatt Heplerb3ea9802021-02-23 09:46:09 -080041------------
Wyatt Heplere0575f72020-10-16 10:47:03 -070042.. code-block::
Alexei Frolov69ad1922019-12-13 13:11:32 -080043
Alexei Frolovedd2f142020-06-09 19:11:27 -070044 import("$dir_pw_build/target_types.gni")
Alexei Frolov69ad1922019-12-13 13:11:32 -080045
Alexei Frolovedd2f142020-06-09 19:11:27 -070046 pw_source_set("my_library") {
47 sources = [ "lib.cc" ]
48 }
49
50Pigweed defines wrappers around the four basic GN binary types ``source_set``,
51``executable``, ``static_library``, and ``shared_library``. These wrappers apply
52default arguments to each target as specified in the ``default_configs`` and
53``default_public_deps`` build args. Additionally, they allow defaults to be
54removed on a per-target basis using ``remove_configs`` and
55``remove_public_deps`` variables, respectively.
56
57The ``pw_executable`` template provides additional functionality around building
58complete binaries. As Pigweed is a collection of libraries, it does not know how
59its final targets are built. ``pw_executable`` solves this by letting each user
60of Pigweed specify a global executable template for their target, and have
61Pigweed build against it. This is controlled by the build variable
62``pw_executable_config.target_type``, specifying the name of the executable
63template for a project.
Alexei Frolov69ad1922019-12-13 13:11:32 -080064
65.. tip::
66
67 Prefer to use ``pw_executable`` over plain ``executable`` targets to allow
68 cleanly building the same code for multiple target configs.
69
70**Arguments**
71
Alexei Frolovedd2f142020-06-09 19:11:27 -070072All of the ``pw_*`` target type overrides accept any arguments, as they simply
73forward them through to the underlying target.
Alexei Frolov69ad1922019-12-13 13:11:32 -080074
Wyatt Hepler8bd4fb02021-05-03 15:30:58 -070075.. _module-pw_build-link-deps:
76
77Link-only deps
78--------------
79It may be necessary to specify additional link-time dependencies that may not be
80explicitly depended on elsewhere in the build. One example of this is a
81``pw_assert`` backend, which may need to leave out dependencies to avoid
82circular dependencies. Its dependencies need to be linked for executables and
83libraries, even if they aren't pulled in elsewhere.
84
85The ``pw_build_LINK_DEPS`` build arg is a list of dependencies to add to all
86``pw_executable``, ``pw_static_library``, and ``pw_shared_library`` targets.
87This should only be used as a last resort when dependencies cannot be properly
88expressed in the build.
89
Wyatt Heplerb3ea9802021-02-23 09:46:09 -080090Python packages
91---------------
92GN templates for :ref:`Python build automation <docs-python-build>` are
93described in :ref:`module-pw_build-python`.
94
95.. toctree::
96 :hidden:
97
98 python
99
Wyatt Heplere0575f72020-10-16 10:47:03 -0700100.. _module-pw_build-facade:
101
102pw_facade
Wyatt Heplerb3ea9802021-02-23 09:46:09 -0800103---------
Wyatt Heplere0575f72020-10-16 10:47:03 -0700104In their simplest form, a :ref:`facade<docs-module-structure-facades>` is a GN
105build arg used to change a dependency at compile time. Pigweed targets configure
106these facades as needed.
107
108The ``pw_facade`` template bundles a ``pw_source_set`` with a facade build arg.
109This allows the facade to provide header files, compilation options or anything
110else a GN ``source_set`` provides.
111
112The ``pw_facade`` template declares two targets:
113
114* ``$target_name``: the public-facing ``pw_source_set``, with a ``public_dep``
115 on the backend
116* ``$target_name.facade``: target used by the backend to avoid circular
117 dependencies
118
119.. code-block::
120
121 # Declares ":foo" and ":foo.facade" GN targets
122 pw_facade("foo") {
123 backend = pw_log_BACKEND
124 public_configs = [ ":public_include_path" ]
125 public = [ "public/pw_foo/foo.h" ]
126 }
127
Wyatt Hepler0d32d1d2020-10-23 08:05:23 -0700128.. _module-pw_build-python-action:
Alexei Frolov199045a2020-08-28 13:02:30 -0700129
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700130pw_python_action
Wyatt Heplerb3ea9802021-02-23 09:46:09 -0800131----------------
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700132The ``pw_python_action`` template is a convenience wrapper around ``action`` for
Wyatt Hepler8224a642020-07-29 08:55:56 -0700133running Python scripts. The main benefit it provides is resolution of GN target
134labels to compiled binary files. This allows Python scripts to be written
135independently of GN, taking only filesystem paths as arguments.
Alexei Frolov69ad1922019-12-13 13:11:32 -0800136
137Another convenience provided by the template is to allow running scripts without
138any outputs. Sometimes scripts run in a build do not directly produce output
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700139files, but GN requires that all actions have an output. ``pw_python_action``
Alexei Frolov69ad1922019-12-13 13:11:32 -0800140solves this by accepting a boolean ``stamp`` argument which tells it to create a
141dummy output file for the action.
142
143**Arguments**
144
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700145``pw_python_action`` accepts all of the arguments of a regular ``action``
Alexei Frolov69ad1922019-12-13 13:11:32 -0800146target. Additionally, it has some of its own arguments:
147
Wyatt Hepler79d983f2020-10-12 08:46:34 -0700148* ``module``: Run the specified Python module instead of a script. Either
149 ``script`` or ``module`` must be specified, but not both.
Wyatt Heplera74f7b02020-07-23 14:10:56 -0700150* ``capture_output``: Optional boolean. If true, script output is hidden unless
151 the script fails with an error. Defaults to true.
152* ``stamp``: Optional variable indicating whether to automatically create a
153 dummy output file for the script. This allows running scripts without
154 specifying ``outputs``. If ``stamp`` is true, a generic output file is
155 used. If ``stamp`` is a file path, that file is used as a stamp file. Like any
156 output file, ``stamp`` must be in the build directory. Defaults to false.
Wyatt Hepler79d983f2020-10-12 08:46:34 -0700157* ``directory``: Optional path. Change to this directory before executing the
158 command. Paths in arguments may need to be adjusted.
159* ``environment``: Optional list of strings. Environment variables to set,
160 passed as NAME=VALUE strings.
Alexei Frolov69ad1922019-12-13 13:11:32 -0800161
Wyatt Hepler8224a642020-07-29 08:55:56 -0700162**Expressions**
163
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700164``pw_python_action`` evaluates expressions in ``args``, the arguments passed to
Wyatt Hepler8224a642020-07-29 08:55:56 -0700165the script. These expressions function similarly to generator expressions in
166CMake. Expressions may be passed as a standalone argument or as part of another
167argument. A single argument may contain multiple expressions.
168
169Generally, these expressions are used within templates rather than directly in
170BUILD.gn files. This allows build code to use GN labels without having to worry
171about converting them to files.
172
Wyatt Heplera2ddc412021-03-16 13:47:29 -0700173.. note::
174
175 We intend to replace these expressions with native GN features when possible.
176 See `pwbug/347 <http://bugs.pigweed.dev/347>`_.
177
Wyatt Hepler53a06fb2020-07-31 13:04:56 -0700178The following expressions are supported:
Wyatt Hepler8224a642020-07-29 08:55:56 -0700179
180.. describe:: <TARGET_FILE(gn_target)>
181
182 Evaluates to the output file of the provided GN target. For example, the
183 expression
184
Wyatt Heplere0575f72020-10-16 10:47:03 -0700185 .. code-block::
Wyatt Hepler8224a642020-07-29 08:55:56 -0700186
187 "<TARGET_FILE(//foo/bar:static_lib)>"
188
189 might expand to
190
Wyatt Heplere0575f72020-10-16 10:47:03 -0700191 .. code-block::
Wyatt Hepler8224a642020-07-29 08:55:56 -0700192
193 "/home/User/project_root/out/obj/foo/bar/static_lib.a"
194
195 ``TARGET_FILE`` parses the ``.ninja`` file for the GN target, so it should
196 always find the correct output file, regardless of the toolchain's or target's
197 configuration. Some targets, such as ``source_set`` and ``group`` targets, do
198 not have an output file, and attempting to use ``TARGET_FILE`` with them
199 results in an error.
200
201 ``TARGET_FILE`` only resolves GN target labels to their outputs. To resolve
202 paths generally, use the standard GN approach of applying the
203 ``rebase_path(path)`` function. With default arguments, ``rebase_path``
204 converts the provided GN path or list of paths to be relative to the build
205 directory, from which all build commands and scripts are executed.
206
Wyatt Hepler53a06fb2020-07-31 13:04:56 -0700207.. describe:: <TARGET_FILE_IF_EXISTS(gn_target)>
208
209 ``TARGET_FILE_IF_EXISTS`` evaluates to the output file of the provided GN
210 target, if the output file exists. If the output file does not exist, the
211 entire argument that includes this expression is omitted, even if there is
212 other text or another expression.
213
214 For example, consider this expression:
215
Wyatt Heplere0575f72020-10-16 10:47:03 -0700216 .. code-block::
Wyatt Hepler53a06fb2020-07-31 13:04:56 -0700217
218 "--database=<TARGET_FILE_IF_EXISTS(//alpha/bravo)>"
219
220 If the ``//alpha/bravo`` target file exists, this might expand to the
221 following:
222
Wyatt Heplere0575f72020-10-16 10:47:03 -0700223 .. code-block::
Wyatt Hepler53a06fb2020-07-31 13:04:56 -0700224
225 "--database=/home/User/project/out/obj/alpha/bravo/bravo.elf"
226
227 If the ``//alpha/bravo`` target file does not exist, the entire
228 ``--database=`` argument is omitted from the script arguments.
229
230.. describe:: <TARGET_OBJECTS(gn_target)>
231
232 Evaluates to the object files of the provided GN target. Expands to a separate
233 argument for each object file. If the target has no object files, the argument
234 is omitted entirely. Because it does not expand to a single expression, the
235 ``<TARGET_OBJECTS(...)>`` expression may not have leading or trailing text.
236
237 For example, the expression
238
Wyatt Heplere0575f72020-10-16 10:47:03 -0700239 .. code-block::
Wyatt Hepler53a06fb2020-07-31 13:04:56 -0700240
241 "<TARGET_OBJECTS(//foo/bar:a_source_set)>"
242
243 might expand to multiple separate arguments:
244
Wyatt Heplere0575f72020-10-16 10:47:03 -0700245 .. code-block::
Wyatt Hepler53a06fb2020-07-31 13:04:56 -0700246
247 "/home/User/project_root/out/obj/foo/bar/a_source_set.file_a.cc.o"
248 "/home/User/project_root/out/obj/foo/bar/a_source_set.file_b.cc.o"
249 "/home/User/project_root/out/obj/foo/bar/a_source_set.file_c.cc.o"
250
Alexei Frolov69ad1922019-12-13 13:11:32 -0800251**Example**
252
Wyatt Heplere0575f72020-10-16 10:47:03 -0700253.. code-block::
Alexei Frolov69ad1922019-12-13 13:11:32 -0800254
Wyatt Hepler51ded742020-10-19 14:45:27 -0700255 import("$dir_pw_build/python_action.gni")
Alexei Frolov69ad1922019-12-13 13:11:32 -0800256
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700257 pw_python_action("postprocess_main_image") {
Wyatt Hepler8224a642020-07-29 08:55:56 -0700258 script = "py/postprocess_binary.py"
259 args = [
260 "--database",
261 rebase_path("my/database.csv"),
262 "--binary=<TARGET_FILE(//firmware/images:main)>",
263 ]
Alexei Frolov69ad1922019-12-13 13:11:32 -0800264 stamp = true
265 }
266
267pw_input_group
Wyatt Heplerb3ea9802021-02-23 09:46:09 -0800268--------------
Alexei Frolov69ad1922019-12-13 13:11:32 -0800269``pw_input_group`` defines a group of input files which are not directly
270processed by the build but are still important dependencies of later build
271steps. This is commonly used alongside metadata to propagate file dependencies
272through the build graph and force rebuilds on file modifications.
273
274For example ``pw_docgen`` defines a ``pw_doc_group`` template which outputs
275metadata from a list of input files. The metadata file is not actually part of
276the build, and so changes to any of the input files do not trigger a rebuild.
277This is problematic, as targets that depend on the metadata should rebuild when
278the inputs are modified but GN cannot express this dependency.
279
280``pw_input_group`` solves this problem by allowing a list of files to be listed
281in a target that does not output any build artifacts, causing all dependent
282targets to correctly rebuild.
283
284**Arguments**
285
286``pw_input_group`` accepts all arguments that can be passed to a ``group``
287target, as well as requiring one extra:
288
289* ``inputs``: List of input files.
290
291**Example**
292
Wyatt Heplere0575f72020-10-16 10:47:03 -0700293.. code-block::
Alexei Frolov69ad1922019-12-13 13:11:32 -0800294
295 import("$dir_pw_build/input_group.gni")
296
297 pw_input_group("foo_metadata") {
298 metadata = {
299 files = [
300 "x.foo",
301 "y.foo",
302 "z.foo",
303 ]
304 }
305 inputs = metadata.files
306 }
307
308Targets depending on ``foo_metadata`` will rebuild when any of the ``.foo``
309files are modified.
Rob Mohr84f234e2019-12-06 09:16:50 -0800310
Sam McCauley0da412c2020-10-02 10:34:56 -0700311pw_zip
Wyatt Heplerb3ea9802021-02-23 09:46:09 -0800312------
Sam McCauley0da412c2020-10-02 10:34:56 -0700313``pw_zip`` is a target that allows users to zip up a set of input files and
314directories into a single output ``.zip`` file—a simple automation of a
315potentially repetitive task.
316
317**Arguments**
318
319* ``inputs``: List of source files as well as the desired relative zip
320 destination. See below for the input syntax.
321* ``dirs``: List of entire directories to be zipped as well as the desired
322 relative zip destination. See below for the input syntax.
323* ``output``: Filename of output ``.zip`` file.
324* ``deps``: List of dependencies for the target.
325
326**Input Syntax**
327
328Inputs all need to follow the correct syntax:
329
330#. Path to source file or directory. Directories must end with a ``/``.
331#. The delimiter (defaults to ``>``).
332#. The desired destination of the contents within the ``.zip``. Must start
333 with ``/`` to indicate the zip root. Any number of subdirectories are
334 allowed. If the source is a file it can be put into any subdirectory of the
335 root. If the source is a file, the zip copy can also be renamed by ending
336 the zip destination with a filename (no trailing ``/``).
337
338Thus, it should look like the following: ``"[source file or dir] > /"``.
339
340**Example**
341
342Let's say we have the following structure for a ``//source/`` directory:
343
Wyatt Heplere0575f72020-10-16 10:47:03 -0700344.. code-block::
Sam McCauley0da412c2020-10-02 10:34:56 -0700345
346 source/
347 ├── file1.txt
348 ├── file2.txt
349 ├── file3.txt
350 └── some_dir/
351 ├── file4.txt
352 └── some_other_dir/
353 └── file5.txt
354
355And we create the following build target:
356
Wyatt Heplere0575f72020-10-16 10:47:03 -0700357.. code-block::
Sam McCauley0da412c2020-10-02 10:34:56 -0700358
359 import("$dir_pw_build/zip.gni")
360
361 pw_zip("target_name") {
362 inputs = [
363 "//source/file1.txt > /", # Copied to the zip root dir.
364 "//source/file2.txt > /renamed.txt", # File renamed.
365 "//source/file3.txt > /bar/", # File moved to the /bar/ dir.
366 ]
367
368 dirs = [
369 "//source/some_dir/ > /bar/some_dir/", # All /some_dir/ contents copied
370 # as /bar/some_dir/.
371 ]
372
373 # Note on output: if the specific output directory isn't defined
374 # (such as output = "zoo.zip") then the .zip will output to the
375 # same directory as the BUILD.gn file that called the target.
376 output = "//$target_out_dir/foo.zip" # Where the foo.zip will end up
377 }
378
379This will result in a ``.zip`` file called ``foo.zip`` stored in
380``//$target_out_dir`` with the following structure:
381
Wyatt Heplere0575f72020-10-16 10:47:03 -0700382.. code-block::
Sam McCauley0da412c2020-10-02 10:34:56 -0700383
384 foo.zip
385 ├── bar/
386 │   ├── file3.txt
387 │   └── some_dir/
388 │   ├── file4.txt
389 │   └── some_other_dir/
390 │   └── file5.txt
391 ├── file1.txt
392 └── renamed.txt
393
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800394CMake / Ninja
395=============
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700396Pigweed's `CMake`_ support is provided primarily for projects that have an
397existing CMake build and wish to integrate Pigweed without switching to a new
398build system.
Armando Montanezbcc194b2020-03-10 10:23:18 -0700399
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700400The following command generates Ninja build files for a host build in the
401``out/cmake_host`` directory:
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800402
Wyatt Heplere0575f72020-10-16 10:47:03 -0700403.. code-block:: sh
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800404
Wyatt Heplerdcfa92b2020-11-10 09:47:30 -0800405 cmake -B out/cmake_host -S "$PW_ROOT" -G Ninja -DCMAKE_TOOLCHAIN_FILE=$PW_ROOT/pw_toolchain/host_clang/toolchain.cmake
406
407The ``PW_ROOT`` environment variable must point to the root of the Pigweed
408directory. This variable is set by Pigweed's environment setup.
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800409
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700410Tests can be executed with the ``pw_run_tests.GROUP`` targets. To run Pigweed
411module tests, execute ``pw_run_tests.modules``:
412
413.. code-block:: sh
414
415 ninja -C out/cmake_host pw_run_tests.modules
416
417:ref:`module-pw_watch` supports CMake, so you can also run
418
419.. code-block:: sh
420
Wyatt Hepler00efe182020-11-23 08:25:14 -0800421 pw watch -C out/cmake_host pw_run_tests.modules
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800422
423CMake functions
424---------------
425CMake convenience functions are defined in ``pw_build/pigweed.cmake``.
426
427* ``pw_auto_add_simple_module`` -- For modules with only one library,
428 automatically declare the library and its tests.
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700429* ``pw_auto_add_module_tests`` -- Create test targets for all tests in a module.
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800430* ``pw_add_facade`` -- Declare a module facade.
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700431* ``pw_set_backend`` -- Set the backend library to use for a facade.
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800432* ``pw_add_module_library`` -- Add a library that is part of a module.
433* ``pw_add_test`` -- Declare a test target.
434
435See ``pw_build/pigweed.cmake`` for the complete documentation of these
436functions.
437
438Special libraries that do not fit well with these functions are created with the
439standard CMake functions, such as ``add_library`` and ``target_link_libraries``.
440
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700441Facades and backends
442--------------------
443The CMake build uses CMake cache variables for configuring
444:ref:`facades<docs-module-structure-facades>` and backends. Cache variables are
445similar to GN's build args set with ``gn args``. Unlike GN, CMake does not
446support multi-toolchain builds, so these variables have a single global value
447per build directory.
448
449The ``pw_add_facade`` function declares a cache variable named
450``<module_name>_BACKEND`` for each facade. Cache variables can be awkward to
451work with, since their values only change when they're assigned, but then
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800452persist accross CMake invocations. These variables should be set in one of the
453following ways:
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700454
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800455* Call ``pw_set_backend`` to set backends appropriate for the target in the
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700456 target's toolchain file. The toolchain file is provided to ``cmake`` with
457 ``-DCMAKE_TOOLCHAIN_FILE=<toolchain file>``.
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800458* Call ``pw_set_backend`` in the top-level ``CMakeLists.txt`` before other
459 CMake code executes.
460* Set the backend variable at the command line with the ``-D`` option.
461
462 .. code-block:: sh
463
464 cmake -B out/cmake_host -S "$PW_ROOT" -G Ninja \
465 -DCMAKE_TOOLCHAIN_FILE=$PW_ROOT/pw_toolchain/host_clang/toolchain.cmake \
466 -Dpw_log_BACKEND=pw_log_basic
467
468* Temporarily override a backend by setting it interactively with ``ccmake`` or
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700469 ``cmake-gui``.
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700470
Wyatt Hepler73439f72020-11-03 14:28:11 -0800471Toolchain setup
472---------------
473In CMake, the toolchain is configured by setting CMake variables, as described
474in the `CMake documentation <https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html>`_.
475These variables are typically set in a toolchain CMake file passed to ``cmake``
476with the ``-D`` option (``-DCMAKE_TOOLCHAIN_FILE=path/to/file.cmake``).
477For Pigweed embedded builds, set ``CMAKE_SYSTEM_NAME`` to the empty string
478(``""``).
479
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700480Third party libraries
481---------------------
482The CMake build includes third-party libraries similarly to the GN build. A
483``dir_pw_third_party_<library>`` cache variable is defined for each third-party
Wyatt Hepler8f357f42021-03-17 15:37:09 -0700484dependency. The variable must be set to the absolute path of the library in
485order to use it. If the variable is empty
486(``if("${dir_pw_third_party_<library>}" STREQUAL "")``), the dependency is not
487available.
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700488
Wyatt Hepler8f357f42021-03-17 15:37:09 -0700489Third-party dependencies are not automatically added to the build. They can be
490manually added with ``add_subdirectory`` or by setting the
491``pw_third_party_<library>_ADD_SUBDIRECTORY`` option to ``ON``.
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700492
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800493Third party variables are set like any other cache global variable in CMake. It
494is recommended to set these in one of the following ways:
Wyatt Heplerdcfa92b2020-11-10 09:47:30 -0800495
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800496* Set with the CMake ``set`` function in the toolchain file or a
497 ``CMakeLists.txt`` before other CMake code executes.
Wyatt Heplerdcfa92b2020-11-10 09:47:30 -0800498
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800499 .. code-block:: cmake
Wyatt Heplerdcfa92b2020-11-10 09:47:30 -0800500
Wyatt Hepler8f357f42021-03-17 15:37:09 -0700501 set(dir_pw_third_party_nanopb ${CMAKE_CURRENT_SOURCE_DIR}/external/nanopb CACHE PATH "" FORCE)
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800502
503* Set the variable at the command line with the ``-D`` option.
504
505 .. code-block:: sh
506
507 cmake -B out/cmake_host -S "$PW_ROOT" -G Ninja \
508 -DCMAKE_TOOLCHAIN_FILE=$PW_ROOT/pw_toolchain/host_clang/toolchain.cmake \
509 -Ddir_pw_third_party_nanopb=/path/to/nanopb
510
511* Set the variable interactively with ``ccmake`` or ``cmake-gui``.
Wyatt Heplerdcfa92b2020-11-10 09:47:30 -0800512
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800513Use Pigweed from an existing CMake project
514------------------------------------------
515To use Pigweed libraries form a CMake-based project, simply include the Pigweed
516repository from a ``CMakeLists.txt``.
517
Wyatt Heplere0575f72020-10-16 10:47:03 -0700518.. code-block:: cmake
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800519
520 add_subdirectory(path/to/pigweed pigweed)
521
522All module libraries will be available as ``module_name`` or
523``module_name.sublibrary``.
524
525If desired, modules can be included individually.
526
Wyatt Heplere0575f72020-10-16 10:47:03 -0700527.. code-block:: cmake
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800528
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800529 add_subdirectory(path/to/pigweed/pw_some_module pw_some_module)
530 add_subdirectory(path/to/pigweed/pw_another_module pw_another_module)
531
Rob Mohr84f234e2019-12-06 09:16:50 -0800532Bazel
533=====
Nathaniel Brough82cf6872021-02-16 15:51:57 +0800534Bazel is currently very experimental, and only builds for host and ARM Cortex-M
535microcontrollers.
Armando Montanezbcc194b2020-03-10 10:23:18 -0700536
Rob Mohr84f234e2019-12-06 09:16:50 -0800537The common configuration for Bazel for all modules is in the ``pigweed.bzl``
538file. The built-in Bazel rules ``cc_binary``, ``cc_library``, and ``cc_test``
539are wrapped with ``pw_cc_binary``, ``pw_cc_library``, and ``pw_cc_test``.
540These wrappers add parameters to calls to the compiler and linker.
541
Nathaniel Brough82cf6872021-02-16 15:51:57 +0800542Currently Pigweed is making use of a set of
543[open source](https://github.com/silvergasp/bazel-embedded) toolchains. The host
544builds are only supported on Linux/Mac based systems. Additionally the host
545builds are not entirely hermetic, and will make use of system
546libraries and headers. This is close to the default configuration for Bazel,
547though slightly more hermetic. The host toolchain is based around clang-11 which
548has a system dependency on 'libtinfo.so.5' which is often included as part of
549the libncurses packages. On Debian based systems this can be installed using the
550command below:
551
552.. code-block:: sh
553
554 sudo apt install libncurses5
555
556The host toolchain does not currently support native Windows, though using WSL
557is a viable alternative.
558
559The ARM Cortex-M Bazel toolchains are based around gcc-arm-non-eabi and are
560entirely hermetic. You can target Cortex-M, by using the platforms command line
561option. This set of toolchains is supported from hosts; Windows, Mac and Linux.
Nathaniel Broughce910982021-02-16 15:51:57 +0800562The platforms that are currently supported are listed below:
Nathaniel Brough82cf6872021-02-16 15:51:57 +0800563
564.. code-block:: sh
565
Nathaniel Broughce910982021-02-16 15:51:57 +0800566 bazel build //:your_target --platforms=@pigweed//pw_build/platforms:cortex_m0
567 bazel build //:your_target --platforms=@pigweed//pw_build/platforms:cortex_m1
568 bazel build //:your_target --platforms=@pigweed//pw_build/platforms:cortex_m3
569 bazel build //:your_target --platforms=@pigweed//pw_build/platforms:cortex_m4
570 bazel build //:your_target --platforms=@pigweed//pw_build/platforms:cortex_m7
Nathaniel Brough82cf6872021-02-16 15:51:57 +0800571 bazel build //:your_target \
Nathaniel Broughce910982021-02-16 15:51:57 +0800572 --platforms=@pigweed//pw_build/platforms:cortex_m4_fpu
Nathaniel Brough82cf6872021-02-16 15:51:57 +0800573 bazel build //:your_target \
Nathaniel Broughce910982021-02-16 15:51:57 +0800574 --platforms=@pigweed//pw_build/platforms:cortex_m7_fpu
575
576
577The above examples are cpu/fpu oriented platforms and can be used where
578applicable for your application. There some more specific platforms for the
579types of boards that are included as examples in Pigweed. It is strongly
580encouraged that you create your own set of platforms specific for your project,
581that implement the constraint_settings in this repository. e.g.
582
583New board constraint_value:
584
585.. code-block:: python
586
587 #your_repo/build_settings/constraints/board/BUILD
588 constraint_value(
589 name = "nucleo_l432kc",
590 constraint_setting = "@pigweed//pw_build/constraints/board",
591 )
592
593New chipset constraint_value:
594
595.. code-block:: python
596
597 # your_repo/build_settings/constraints/chipset/BUILD
598 constraint_value(
599 name = "stm32l432kc",
600 constraint_setting = "@pigweed//pw_build/constraints/chipset",
601 )
602
603New platforms for chipset and board:
604
605.. code-block:: python
606
607 #your_repo/build_settings/platforms/BUILD
608 # Works with all stm32l432kc
609 platforms(
610 name = "stm32l432kc",
611 parents = ["@pigweed//pw_build/platforms:cortex_m4"],
612 constraint_values =
613 ["@your_repo//build_settings/constraints/chipset:stm32l432kc"],
614 )
615
616 # Works with only the nucleo_l432kc
617 platforms(
618 name = "nucleo_l432kc",
619 parents = [":stm32l432kc"],
620 constraint_values =
621 ["@your_repo//build_settings/constraints/board:nucleo_l432kc"],
622 )
623
624In the above example you can build your code with the command line:
625
626.. code-block:: python
627
628 bazel build //:your_target_for_nucleo_l432kc \
629 --platforms=@your_repo//build_settings:nucleo_l432kc
630
631
632You can also specify that a specific target is only compatible with one
633platform:
634
635.. code-block:: python
636
637 cc_library(
638 name = "compatible_with_all_stm32l432kc",
639 srcs = ["tomato_src.c"],
640 target_compatible_with =
641 ["@your_repo//build_settings/constraints/chipset:stm32l432kc"],
642 )
643
644 cc_library(
645 name = "compatible_with_only_nucleo_l432kc",
646 srcs = ["bbq_src.c"],
647 target_compatible_with =
648 ["@your_repo//build_settings/constraints/board:nucleo_l432kc"],
649 )
650