blob: e25076a7706a29417b8977b28bbfd752d3b43a79 [file] [log] [blame]
Rob Mohr84f234e2019-12-06 09:16:50 -08001.. _chapter-build:
2
3.. default-domain:: cpp
4
5.. highlight:: sh
6
7--------
8pw_build
9--------
10The build module contains the configuration necessary to build Pigweed using
11either `GN`_/`Ninja`_ or `Bazel`_.
12
13.. _GN: https://gn.googlesource.com/gn/
14.. _Ninja: https://ninja-build.org/
15.. _Bazel: https://bazel.build/
16
17GN / Ninja
18==========
19The common configuration for GN for all modules is in the ``BUILD.gn`` file.
20It contains ``config`` declarations referenced by ``BUILD.gn`` files in other
21modules.
22
Alexei Frolov69ad1922019-12-13 13:11:32 -080023The module also provides some useful GN templates for build targets.
24
25Templates
26---------
27
28pw_executable
29^^^^^^^^^^^^^
30.. code::
31
32 import("$dir_pw_build/pw_executable.gni")
33
34The ``pw_executable`` template is a wrapper around executable targets which
35builds for a globally-defined target type. This is controlled by the build
36variable ``pw_executable_config.target_type``. For example, setting this
37variable to ``stm32f429i_executable`` causes all executable targets to build
38using that template.
39
40.. tip::
41
42 Prefer to use ``pw_executable`` over plain ``executable`` targets to allow
43 cleanly building the same code for multiple target configs.
44
45**Arguments**
46
47``pw_executable`` accepts any arguments, as it simply forwards them through to
48the specified custom template.
49
50pw_python_script
51^^^^^^^^^^^^^^^^
52The ``pw_python_script`` template is a convenience wrapper around ``action`` for
53running Python scripts. The main benefit it provides is automatic resolution of
54GN paths to filesystem paths and GN target names to compiled binary files. This
55allows Python scripts to be written independent of GN, taking only filesystem as
56arguments.
57
58Another convenience provided by the template is to allow running scripts without
59any outputs. Sometimes scripts run in a build do not directly produce output
60files, but GN requires that all actions have an output. ``pw_python_script``
61solves this by accepting a boolean ``stamp`` argument which tells it to create a
62dummy output file for the action.
63
64**Arguments**
65
66``pw_python_script`` accepts all of the arguments of a regular ``action``
67target. Additionally, it has some of its own arguments:
68
69* ``stamp``: Optional boolean indicating whether to automatically create a dummy
70 output file for the script. This allows running scripts without specifying any
71 ``outputs``.
72
73**Example**
74
75.. code::
76
77 import("$dir_pw_build/python_script.gni")
78
79 python_script("hello_world") {
80 script = "py/say_hello.py"
81 args = [ "world" ]
82 stamp = true
83 }
84
85pw_input_group
86^^^^^^^^^^^^^^
87``pw_input_group`` defines a group of input files which are not directly
88processed by the build but are still important dependencies of later build
89steps. This is commonly used alongside metadata to propagate file dependencies
90through the build graph and force rebuilds on file modifications.
91
92For example ``pw_docgen`` defines a ``pw_doc_group`` template which outputs
93metadata from a list of input files. The metadata file is not actually part of
94the build, and so changes to any of the input files do not trigger a rebuild.
95This is problematic, as targets that depend on the metadata should rebuild when
96the inputs are modified but GN cannot express this dependency.
97
98``pw_input_group`` solves this problem by allowing a list of files to be listed
99in a target that does not output any build artifacts, causing all dependent
100targets to correctly rebuild.
101
102**Arguments**
103
104``pw_input_group`` accepts all arguments that can be passed to a ``group``
105target, as well as requiring one extra:
106
107* ``inputs``: List of input files.
108
109**Example**
110
111.. code::
112
113 import("$dir_pw_build/input_group.gni")
114
115 pw_input_group("foo_metadata") {
116 metadata = {
117 files = [
118 "x.foo",
119 "y.foo",
120 "z.foo",
121 ]
122 }
123 inputs = metadata.files
124 }
125
126Targets depending on ``foo_metadata`` will rebuild when any of the ``.foo``
127files are modified.
Rob Mohr84f234e2019-12-06 09:16:50 -0800128
129Bazel
130=====
131The common configuration for Bazel for all modules is in the ``pigweed.bzl``
132file. The built-in Bazel rules ``cc_binary``, ``cc_library``, and ``cc_test``
133are wrapped with ``pw_cc_binary``, ``pw_cc_library``, and ``pw_cc_test``.
134These wrappers add parameters to calls to the compiler and linker.
135
136The ``BUILD`` file is merely a placeholder and currently does nothing.