blob: 61357556b01236b3029ee0bf209e851cf2edecbc [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 Heplerb3ea9802021-02-23 09:46:09 -080075Python packages
76---------------
77GN templates for :ref:`Python build automation <docs-python-build>` are
78described in :ref:`module-pw_build-python`.
79
80.. toctree::
81 :hidden:
82
83 python
84
Wyatt Heplere0575f72020-10-16 10:47:03 -070085.. _module-pw_build-facade:
86
87pw_facade
Wyatt Heplerb3ea9802021-02-23 09:46:09 -080088---------
Wyatt Heplere0575f72020-10-16 10:47:03 -070089In their simplest form, a :ref:`facade<docs-module-structure-facades>` is a GN
90build arg used to change a dependency at compile time. Pigweed targets configure
91these facades as needed.
92
93The ``pw_facade`` template bundles a ``pw_source_set`` with a facade build arg.
94This allows the facade to provide header files, compilation options or anything
95else a GN ``source_set`` provides.
96
97The ``pw_facade`` template declares two targets:
98
99* ``$target_name``: the public-facing ``pw_source_set``, with a ``public_dep``
100 on the backend
101* ``$target_name.facade``: target used by the backend to avoid circular
102 dependencies
103
104.. code-block::
105
106 # Declares ":foo" and ":foo.facade" GN targets
107 pw_facade("foo") {
108 backend = pw_log_BACKEND
109 public_configs = [ ":public_include_path" ]
110 public = [ "public/pw_foo/foo.h" ]
111 }
112
Wyatt Hepler0d32d1d2020-10-23 08:05:23 -0700113.. _module-pw_build-python-action:
Alexei Frolov199045a2020-08-28 13:02:30 -0700114
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700115pw_python_action
Wyatt Heplerb3ea9802021-02-23 09:46:09 -0800116----------------
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700117The ``pw_python_action`` template is a convenience wrapper around ``action`` for
Wyatt Hepler8224a642020-07-29 08:55:56 -0700118running Python scripts. The main benefit it provides is resolution of GN target
119labels to compiled binary files. This allows Python scripts to be written
120independently of GN, taking only filesystem paths as arguments.
Alexei Frolov69ad1922019-12-13 13:11:32 -0800121
122Another convenience provided by the template is to allow running scripts without
123any outputs. Sometimes scripts run in a build do not directly produce output
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700124files, but GN requires that all actions have an output. ``pw_python_action``
Alexei Frolov69ad1922019-12-13 13:11:32 -0800125solves this by accepting a boolean ``stamp`` argument which tells it to create a
126dummy output file for the action.
127
128**Arguments**
129
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700130``pw_python_action`` accepts all of the arguments of a regular ``action``
Alexei Frolov69ad1922019-12-13 13:11:32 -0800131target. Additionally, it has some of its own arguments:
132
Wyatt Hepler79d983f2020-10-12 08:46:34 -0700133* ``module``: Run the specified Python module instead of a script. Either
134 ``script`` or ``module`` must be specified, but not both.
Wyatt Heplera74f7b02020-07-23 14:10:56 -0700135* ``capture_output``: Optional boolean. If true, script output is hidden unless
136 the script fails with an error. Defaults to true.
137* ``stamp``: Optional variable indicating whether to automatically create a
138 dummy output file for the script. This allows running scripts without
139 specifying ``outputs``. If ``stamp`` is true, a generic output file is
140 used. If ``stamp`` is a file path, that file is used as a stamp file. Like any
141 output file, ``stamp`` must be in the build directory. Defaults to false.
Wyatt Hepler79d983f2020-10-12 08:46:34 -0700142* ``directory``: Optional path. Change to this directory before executing the
143 command. Paths in arguments may need to be adjusted.
144* ``environment``: Optional list of strings. Environment variables to set,
145 passed as NAME=VALUE strings.
Alexei Frolov69ad1922019-12-13 13:11:32 -0800146
Wyatt Hepler8224a642020-07-29 08:55:56 -0700147**Expressions**
148
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700149``pw_python_action`` evaluates expressions in ``args``, the arguments passed to
Wyatt Hepler8224a642020-07-29 08:55:56 -0700150the script. These expressions function similarly to generator expressions in
151CMake. Expressions may be passed as a standalone argument or as part of another
152argument. A single argument may contain multiple expressions.
153
154Generally, these expressions are used within templates rather than directly in
155BUILD.gn files. This allows build code to use GN labels without having to worry
156about converting them to files.
157
Wyatt Heplera2ddc412021-03-16 13:47:29 -0700158.. note::
159
160 We intend to replace these expressions with native GN features when possible.
161 See `pwbug/347 <http://bugs.pigweed.dev/347>`_.
162
Wyatt Hepler53a06fb2020-07-31 13:04:56 -0700163The following expressions are supported:
Wyatt Hepler8224a642020-07-29 08:55:56 -0700164
165.. describe:: <TARGET_FILE(gn_target)>
166
167 Evaluates to the output file of the provided GN target. For example, the
168 expression
169
Wyatt Heplere0575f72020-10-16 10:47:03 -0700170 .. code-block::
Wyatt Hepler8224a642020-07-29 08:55:56 -0700171
172 "<TARGET_FILE(//foo/bar:static_lib)>"
173
174 might expand to
175
Wyatt Heplere0575f72020-10-16 10:47:03 -0700176 .. code-block::
Wyatt Hepler8224a642020-07-29 08:55:56 -0700177
178 "/home/User/project_root/out/obj/foo/bar/static_lib.a"
179
180 ``TARGET_FILE`` parses the ``.ninja`` file for the GN target, so it should
181 always find the correct output file, regardless of the toolchain's or target's
182 configuration. Some targets, such as ``source_set`` and ``group`` targets, do
183 not have an output file, and attempting to use ``TARGET_FILE`` with them
184 results in an error.
185
186 ``TARGET_FILE`` only resolves GN target labels to their outputs. To resolve
187 paths generally, use the standard GN approach of applying the
188 ``rebase_path(path)`` function. With default arguments, ``rebase_path``
189 converts the provided GN path or list of paths to be relative to the build
190 directory, from which all build commands and scripts are executed.
191
Wyatt Hepler53a06fb2020-07-31 13:04:56 -0700192.. describe:: <TARGET_FILE_IF_EXISTS(gn_target)>
193
194 ``TARGET_FILE_IF_EXISTS`` evaluates to the output file of the provided GN
195 target, if the output file exists. If the output file does not exist, the
196 entire argument that includes this expression is omitted, even if there is
197 other text or another expression.
198
199 For example, consider this expression:
200
Wyatt Heplere0575f72020-10-16 10:47:03 -0700201 .. code-block::
Wyatt Hepler53a06fb2020-07-31 13:04:56 -0700202
203 "--database=<TARGET_FILE_IF_EXISTS(//alpha/bravo)>"
204
205 If the ``//alpha/bravo`` target file exists, this might expand to the
206 following:
207
Wyatt Heplere0575f72020-10-16 10:47:03 -0700208 .. code-block::
Wyatt Hepler53a06fb2020-07-31 13:04:56 -0700209
210 "--database=/home/User/project/out/obj/alpha/bravo/bravo.elf"
211
212 If the ``//alpha/bravo`` target file does not exist, the entire
213 ``--database=`` argument is omitted from the script arguments.
214
215.. describe:: <TARGET_OBJECTS(gn_target)>
216
217 Evaluates to the object files of the provided GN target. Expands to a separate
218 argument for each object file. If the target has no object files, the argument
219 is omitted entirely. Because it does not expand to a single expression, the
220 ``<TARGET_OBJECTS(...)>`` expression may not have leading or trailing text.
221
222 For example, the expression
223
Wyatt Heplere0575f72020-10-16 10:47:03 -0700224 .. code-block::
Wyatt Hepler53a06fb2020-07-31 13:04:56 -0700225
226 "<TARGET_OBJECTS(//foo/bar:a_source_set)>"
227
228 might expand to multiple separate arguments:
229
Wyatt Heplere0575f72020-10-16 10:47:03 -0700230 .. code-block::
Wyatt Hepler53a06fb2020-07-31 13:04:56 -0700231
232 "/home/User/project_root/out/obj/foo/bar/a_source_set.file_a.cc.o"
233 "/home/User/project_root/out/obj/foo/bar/a_source_set.file_b.cc.o"
234 "/home/User/project_root/out/obj/foo/bar/a_source_set.file_c.cc.o"
235
Alexei Frolov69ad1922019-12-13 13:11:32 -0800236**Example**
237
Wyatt Heplere0575f72020-10-16 10:47:03 -0700238.. code-block::
Alexei Frolov69ad1922019-12-13 13:11:32 -0800239
Wyatt Hepler51ded742020-10-19 14:45:27 -0700240 import("$dir_pw_build/python_action.gni")
Alexei Frolov69ad1922019-12-13 13:11:32 -0800241
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700242 pw_python_action("postprocess_main_image") {
Wyatt Hepler8224a642020-07-29 08:55:56 -0700243 script = "py/postprocess_binary.py"
244 args = [
245 "--database",
246 rebase_path("my/database.csv"),
247 "--binary=<TARGET_FILE(//firmware/images:main)>",
248 ]
Alexei Frolov69ad1922019-12-13 13:11:32 -0800249 stamp = true
250 }
251
252pw_input_group
Wyatt Heplerb3ea9802021-02-23 09:46:09 -0800253--------------
Alexei Frolov69ad1922019-12-13 13:11:32 -0800254``pw_input_group`` defines a group of input files which are not directly
255processed by the build but are still important dependencies of later build
256steps. This is commonly used alongside metadata to propagate file dependencies
257through the build graph and force rebuilds on file modifications.
258
259For example ``pw_docgen`` defines a ``pw_doc_group`` template which outputs
260metadata from a list of input files. The metadata file is not actually part of
261the build, and so changes to any of the input files do not trigger a rebuild.
262This is problematic, as targets that depend on the metadata should rebuild when
263the inputs are modified but GN cannot express this dependency.
264
265``pw_input_group`` solves this problem by allowing a list of files to be listed
266in a target that does not output any build artifacts, causing all dependent
267targets to correctly rebuild.
268
269**Arguments**
270
271``pw_input_group`` accepts all arguments that can be passed to a ``group``
272target, as well as requiring one extra:
273
274* ``inputs``: List of input files.
275
276**Example**
277
Wyatt Heplere0575f72020-10-16 10:47:03 -0700278.. code-block::
Alexei Frolov69ad1922019-12-13 13:11:32 -0800279
280 import("$dir_pw_build/input_group.gni")
281
282 pw_input_group("foo_metadata") {
283 metadata = {
284 files = [
285 "x.foo",
286 "y.foo",
287 "z.foo",
288 ]
289 }
290 inputs = metadata.files
291 }
292
293Targets depending on ``foo_metadata`` will rebuild when any of the ``.foo``
294files are modified.
Rob Mohr84f234e2019-12-06 09:16:50 -0800295
Sam McCauley0da412c2020-10-02 10:34:56 -0700296pw_zip
Wyatt Heplerb3ea9802021-02-23 09:46:09 -0800297------
Sam McCauley0da412c2020-10-02 10:34:56 -0700298``pw_zip`` is a target that allows users to zip up a set of input files and
299directories into a single output ``.zip`` file—a simple automation of a
300potentially repetitive task.
301
302**Arguments**
303
304* ``inputs``: List of source files as well as the desired relative zip
305 destination. See below for the input syntax.
306* ``dirs``: List of entire directories to be zipped as well as the desired
307 relative zip destination. See below for the input syntax.
308* ``output``: Filename of output ``.zip`` file.
309* ``deps``: List of dependencies for the target.
310
311**Input Syntax**
312
313Inputs all need to follow the correct syntax:
314
315#. Path to source file or directory. Directories must end with a ``/``.
316#. The delimiter (defaults to ``>``).
317#. The desired destination of the contents within the ``.zip``. Must start
318 with ``/`` to indicate the zip root. Any number of subdirectories are
319 allowed. If the source is a file it can be put into any subdirectory of the
320 root. If the source is a file, the zip copy can also be renamed by ending
321 the zip destination with a filename (no trailing ``/``).
322
323Thus, it should look like the following: ``"[source file or dir] > /"``.
324
325**Example**
326
327Let's say we have the following structure for a ``//source/`` directory:
328
Wyatt Heplere0575f72020-10-16 10:47:03 -0700329.. code-block::
Sam McCauley0da412c2020-10-02 10:34:56 -0700330
331 source/
332 ├── file1.txt
333 ├── file2.txt
334 ├── file3.txt
335 └── some_dir/
336 ├── file4.txt
337 └── some_other_dir/
338 └── file5.txt
339
340And we create the following build target:
341
Wyatt Heplere0575f72020-10-16 10:47:03 -0700342.. code-block::
Sam McCauley0da412c2020-10-02 10:34:56 -0700343
344 import("$dir_pw_build/zip.gni")
345
346 pw_zip("target_name") {
347 inputs = [
348 "//source/file1.txt > /", # Copied to the zip root dir.
349 "//source/file2.txt > /renamed.txt", # File renamed.
350 "//source/file3.txt > /bar/", # File moved to the /bar/ dir.
351 ]
352
353 dirs = [
354 "//source/some_dir/ > /bar/some_dir/", # All /some_dir/ contents copied
355 # as /bar/some_dir/.
356 ]
357
358 # Note on output: if the specific output directory isn't defined
359 # (such as output = "zoo.zip") then the .zip will output to the
360 # same directory as the BUILD.gn file that called the target.
361 output = "//$target_out_dir/foo.zip" # Where the foo.zip will end up
362 }
363
364This will result in a ``.zip`` file called ``foo.zip`` stored in
365``//$target_out_dir`` with the following structure:
366
Wyatt Heplere0575f72020-10-16 10:47:03 -0700367.. code-block::
Sam McCauley0da412c2020-10-02 10:34:56 -0700368
369 foo.zip
370 ├── bar/
371 │   ├── file3.txt
372 │   └── some_dir/
373 │   ├── file4.txt
374 │   └── some_other_dir/
375 │   └── file5.txt
376 ├── file1.txt
377 └── renamed.txt
378
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800379CMake / Ninja
380=============
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700381Pigweed's `CMake`_ support is provided primarily for projects that have an
382existing CMake build and wish to integrate Pigweed without switching to a new
383build system.
Armando Montanezbcc194b2020-03-10 10:23:18 -0700384
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700385The following command generates Ninja build files for a host build in the
386``out/cmake_host`` directory:
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800387
Wyatt Heplere0575f72020-10-16 10:47:03 -0700388.. code-block:: sh
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800389
Wyatt Heplerdcfa92b2020-11-10 09:47:30 -0800390 cmake -B out/cmake_host -S "$PW_ROOT" -G Ninja -DCMAKE_TOOLCHAIN_FILE=$PW_ROOT/pw_toolchain/host_clang/toolchain.cmake
391
392The ``PW_ROOT`` environment variable must point to the root of the Pigweed
393directory. This variable is set by Pigweed's environment setup.
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800394
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700395Tests can be executed with the ``pw_run_tests.GROUP`` targets. To run Pigweed
396module tests, execute ``pw_run_tests.modules``:
397
398.. code-block:: sh
399
400 ninja -C out/cmake_host pw_run_tests.modules
401
402:ref:`module-pw_watch` supports CMake, so you can also run
403
404.. code-block:: sh
405
Wyatt Hepler00efe182020-11-23 08:25:14 -0800406 pw watch -C out/cmake_host pw_run_tests.modules
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800407
408CMake functions
409---------------
410CMake convenience functions are defined in ``pw_build/pigweed.cmake``.
411
412* ``pw_auto_add_simple_module`` -- For modules with only one library,
413 automatically declare the library and its tests.
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700414* ``pw_auto_add_module_tests`` -- Create test targets for all tests in a module.
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800415* ``pw_add_facade`` -- Declare a module facade.
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700416* ``pw_set_backend`` -- Set the backend library to use for a facade.
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800417* ``pw_add_module_library`` -- Add a library that is part of a module.
418* ``pw_add_test`` -- Declare a test target.
419
420See ``pw_build/pigweed.cmake`` for the complete documentation of these
421functions.
422
423Special libraries that do not fit well with these functions are created with the
424standard CMake functions, such as ``add_library`` and ``target_link_libraries``.
425
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700426Facades and backends
427--------------------
428The CMake build uses CMake cache variables for configuring
429:ref:`facades<docs-module-structure-facades>` and backends. Cache variables are
430similar to GN's build args set with ``gn args``. Unlike GN, CMake does not
431support multi-toolchain builds, so these variables have a single global value
432per build directory.
433
434The ``pw_add_facade`` function declares a cache variable named
435``<module_name>_BACKEND`` for each facade. Cache variables can be awkward to
436work with, since their values only change when they're assigned, but then
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800437persist accross CMake invocations. These variables should be set in one of the
438following ways:
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700439
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800440* Call ``pw_set_backend`` to set backends appropriate for the target in the
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700441 target's toolchain file. The toolchain file is provided to ``cmake`` with
442 ``-DCMAKE_TOOLCHAIN_FILE=<toolchain file>``.
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800443* Call ``pw_set_backend`` in the top-level ``CMakeLists.txt`` before other
444 CMake code executes.
445* Set the backend variable at the command line with the ``-D`` option.
446
447 .. code-block:: sh
448
449 cmake -B out/cmake_host -S "$PW_ROOT" -G Ninja \
450 -DCMAKE_TOOLCHAIN_FILE=$PW_ROOT/pw_toolchain/host_clang/toolchain.cmake \
451 -Dpw_log_BACKEND=pw_log_basic
452
453* Temporarily override a backend by setting it interactively with ``ccmake`` or
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700454 ``cmake-gui``.
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700455
Wyatt Hepler73439f72020-11-03 14:28:11 -0800456Toolchain setup
457---------------
458In CMake, the toolchain is configured by setting CMake variables, as described
459in the `CMake documentation <https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html>`_.
460These variables are typically set in a toolchain CMake file passed to ``cmake``
461with the ``-D`` option (``-DCMAKE_TOOLCHAIN_FILE=path/to/file.cmake``).
462For Pigweed embedded builds, set ``CMAKE_SYSTEM_NAME`` to the empty string
463(``""``).
464
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700465Third party libraries
466---------------------
467The CMake build includes third-party libraries similarly to the GN build. A
468``dir_pw_third_party_<library>`` cache variable is defined for each third-party
Wyatt Hepler8f357f42021-03-17 15:37:09 -0700469dependency. The variable must be set to the absolute path of the library in
470order to use it. If the variable is empty
471(``if("${dir_pw_third_party_<library>}" STREQUAL "")``), the dependency is not
472available.
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700473
Wyatt Hepler8f357f42021-03-17 15:37:09 -0700474Third-party dependencies are not automatically added to the build. They can be
475manually added with ``add_subdirectory`` or by setting the
476``pw_third_party_<library>_ADD_SUBDIRECTORY`` option to ``ON``.
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700477
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800478Third party variables are set like any other cache global variable in CMake. It
479is recommended to set these in one of the following ways:
Wyatt Heplerdcfa92b2020-11-10 09:47:30 -0800480
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800481* Set with the CMake ``set`` function in the toolchain file or a
482 ``CMakeLists.txt`` before other CMake code executes.
Wyatt Heplerdcfa92b2020-11-10 09:47:30 -0800483
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800484 .. code-block:: cmake
Wyatt Heplerdcfa92b2020-11-10 09:47:30 -0800485
Wyatt Hepler8f357f42021-03-17 15:37:09 -0700486 set(dir_pw_third_party_nanopb ${CMAKE_CURRENT_SOURCE_DIR}/external/nanopb CACHE PATH "" FORCE)
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800487
488* Set the variable at the command line with the ``-D`` option.
489
490 .. code-block:: sh
491
492 cmake -B out/cmake_host -S "$PW_ROOT" -G Ninja \
493 -DCMAKE_TOOLCHAIN_FILE=$PW_ROOT/pw_toolchain/host_clang/toolchain.cmake \
494 -Ddir_pw_third_party_nanopb=/path/to/nanopb
495
496* Set the variable interactively with ``ccmake`` or ``cmake-gui``.
Wyatt Heplerdcfa92b2020-11-10 09:47:30 -0800497
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800498Use Pigweed from an existing CMake project
499------------------------------------------
500To use Pigweed libraries form a CMake-based project, simply include the Pigweed
501repository from a ``CMakeLists.txt``.
502
Wyatt Heplere0575f72020-10-16 10:47:03 -0700503.. code-block:: cmake
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800504
505 add_subdirectory(path/to/pigweed pigweed)
506
507All module libraries will be available as ``module_name`` or
508``module_name.sublibrary``.
509
510If desired, modules can be included individually.
511
Wyatt Heplere0575f72020-10-16 10:47:03 -0700512.. code-block:: cmake
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800513
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800514 add_subdirectory(path/to/pigweed/pw_some_module pw_some_module)
515 add_subdirectory(path/to/pigweed/pw_another_module pw_another_module)
516
Rob Mohr84f234e2019-12-06 09:16:50 -0800517Bazel
518=====
Nathaniel Brough82cf6872021-02-16 15:51:57 +0800519Bazel is currently very experimental, and only builds for host and ARM Cortex-M
520microcontrollers.
Armando Montanezbcc194b2020-03-10 10:23:18 -0700521
Rob Mohr84f234e2019-12-06 09:16:50 -0800522The common configuration for Bazel for all modules is in the ``pigweed.bzl``
523file. The built-in Bazel rules ``cc_binary``, ``cc_library``, and ``cc_test``
524are wrapped with ``pw_cc_binary``, ``pw_cc_library``, and ``pw_cc_test``.
525These wrappers add parameters to calls to the compiler and linker.
526
Nathaniel Brough82cf6872021-02-16 15:51:57 +0800527Currently Pigweed is making use of a set of
528[open source](https://github.com/silvergasp/bazel-embedded) toolchains. The host
529builds are only supported on Linux/Mac based systems. Additionally the host
530builds are not entirely hermetic, and will make use of system
531libraries and headers. This is close to the default configuration for Bazel,
532though slightly more hermetic. The host toolchain is based around clang-11 which
533has a system dependency on 'libtinfo.so.5' which is often included as part of
534the libncurses packages. On Debian based systems this can be installed using the
535command below:
536
537.. code-block:: sh
538
539 sudo apt install libncurses5
540
541The host toolchain does not currently support native Windows, though using WSL
542is a viable alternative.
543
544The ARM Cortex-M Bazel toolchains are based around gcc-arm-non-eabi and are
545entirely hermetic. You can target Cortex-M, by using the platforms command line
546option. This set of toolchains is supported from hosts; Windows, Mac and Linux.
Nathaniel Broughce910982021-02-16 15:51:57 +0800547The platforms that are currently supported are listed below:
Nathaniel Brough82cf6872021-02-16 15:51:57 +0800548
549.. code-block:: sh
550
Nathaniel Broughce910982021-02-16 15:51:57 +0800551 bazel build //:your_target --platforms=@pigweed//pw_build/platforms:cortex_m0
552 bazel build //:your_target --platforms=@pigweed//pw_build/platforms:cortex_m1
553 bazel build //:your_target --platforms=@pigweed//pw_build/platforms:cortex_m3
554 bazel build //:your_target --platforms=@pigweed//pw_build/platforms:cortex_m4
555 bazel build //:your_target --platforms=@pigweed//pw_build/platforms:cortex_m7
Nathaniel Brough82cf6872021-02-16 15:51:57 +0800556 bazel build //:your_target \
Nathaniel Broughce910982021-02-16 15:51:57 +0800557 --platforms=@pigweed//pw_build/platforms:cortex_m4_fpu
Nathaniel Brough82cf6872021-02-16 15:51:57 +0800558 bazel build //:your_target \
Nathaniel Broughce910982021-02-16 15:51:57 +0800559 --platforms=@pigweed//pw_build/platforms:cortex_m7_fpu
560
561
562The above examples are cpu/fpu oriented platforms and can be used where
563applicable for your application. There some more specific platforms for the
564types of boards that are included as examples in Pigweed. It is strongly
565encouraged that you create your own set of platforms specific for your project,
566that implement the constraint_settings in this repository. e.g.
567
568New board constraint_value:
569
570.. code-block:: python
571
572 #your_repo/build_settings/constraints/board/BUILD
573 constraint_value(
574 name = "nucleo_l432kc",
575 constraint_setting = "@pigweed//pw_build/constraints/board",
576 )
577
578New chipset constraint_value:
579
580.. code-block:: python
581
582 # your_repo/build_settings/constraints/chipset/BUILD
583 constraint_value(
584 name = "stm32l432kc",
585 constraint_setting = "@pigweed//pw_build/constraints/chipset",
586 )
587
588New platforms for chipset and board:
589
590.. code-block:: python
591
592 #your_repo/build_settings/platforms/BUILD
593 # Works with all stm32l432kc
594 platforms(
595 name = "stm32l432kc",
596 parents = ["@pigweed//pw_build/platforms:cortex_m4"],
597 constraint_values =
598 ["@your_repo//build_settings/constraints/chipset:stm32l432kc"],
599 )
600
601 # Works with only the nucleo_l432kc
602 platforms(
603 name = "nucleo_l432kc",
604 parents = [":stm32l432kc"],
605 constraint_values =
606 ["@your_repo//build_settings/constraints/board:nucleo_l432kc"],
607 )
608
609In the above example you can build your code with the command line:
610
611.. code-block:: python
612
613 bazel build //:your_target_for_nucleo_l432kc \
614 --platforms=@your_repo//build_settings:nucleo_l432kc
615
616
617You can also specify that a specific target is only compatible with one
618platform:
619
620.. code-block:: python
621
622 cc_library(
623 name = "compatible_with_all_stm32l432kc",
624 srcs = ["tomato_src.c"],
625 target_compatible_with =
626 ["@your_repo//build_settings/constraints/chipset:stm32l432kc"],
627 )
628
629 cc_library(
630 name = "compatible_with_only_nucleo_l432kc",
631 srcs = ["bbq_src.c"],
632 target_compatible_with =
633 ["@your_repo//build_settings/constraints/board:nucleo_l432kc"],
634 )
635