blob: 147c6917c515188ace07600b343fce9b049740d4 [file] [log] [blame]
Rob Mohr84f234e2019-12-06 09:16:50 -08001.. default-domain:: cpp
2
3.. highlight:: sh
4
Alexei Frolov9d169d52020-03-03 17:20:06 -08005.. _chapter-build:
6
Rob Mohr84f234e2019-12-06 09:16:50 -08007--------
8pw_build
9--------
Alexei Frolov9d169d52020-03-03 17:20:06 -080010Pigweed's modules aim to be easily integratable into both new and existing
11embedded projects. To that goal, the ``pw_build`` module provides support for
12multiple build systems. Our personal favorite is `GN`_/`Ninja`_, which is used
13by upstream developers for its speed and flexibility. `CMake`_ and `Bazel`_
14build files are also provided by all modules, allowing Pigweed to be added to a
15project with minimal effort.
Rob Mohr84f234e2019-12-06 09:16:50 -080016
17.. _GN: https://gn.googlesource.com/gn/
18.. _Ninja: https://ninja-build.org/
Alexei Frolov9d169d52020-03-03 17:20:06 -080019.. _CMake: https://cmake.org/
Rob Mohr84f234e2019-12-06 09:16:50 -080020.. _Bazel: https://bazel.build/
21
Alexei Frolov9d169d52020-03-03 17:20:06 -080022Beyond just compiling code, Pigweed’s GN build system can also:
23
24* Generate HTML documentation, via our Sphinx integration (with ``pw_docgen``)
Armando Montanez0054a9b2020-03-13 13:06:24 -070025* Display memory usage report cards (with ``pw_bloat``)
Alexei Frolov9d169d52020-03-03 17:20:06 -080026* Incrementally run unit tests after code changes (with ``pw_target_runner``)
27* And more!
28
29These are only supported in the GN build, so we recommend using it if possible.
30
Rob Mohr84f234e2019-12-06 09:16:50 -080031GN / Ninja
32==========
Armando Montanezbcc194b2020-03-10 10:23:18 -070033The GN / Ninja build system is the primary build system used for upstream
34Pigweed development, and is the most tested and feature-rich build system
35Pigweed offers.
Rob Mohr84f234e2019-12-06 09:16:50 -080036
Armando Montanezbcc194b2020-03-10 10:23:18 -070037This module's ``build.gn`` file contains a number of C/C++ ``config``
38declarations that are used by upstream Pigweed to set some architecture-agnostic
39compiler defaults. (See Pigweed's ``//BUILDCONFIG.gn``)
40
Armando Montanez0054a9b2020-03-13 13:06:24 -070041``pw_build`` also provides several useful GN templates that are used throughout
Armando Montanezbcc194b2020-03-10 10:23:18 -070042Pigweed.
Alexei Frolov69ad1922019-12-13 13:11:32 -080043
44Templates
45---------
46
47pw_executable
48^^^^^^^^^^^^^
49.. code::
50
51 import("$dir_pw_build/pw_executable.gni")
52
53The ``pw_executable`` template is a wrapper around executable targets which
54builds for a globally-defined target type. This is controlled by the build
55variable ``pw_executable_config.target_type``. For example, setting this
56variable to ``stm32f429i_executable`` causes all executable targets to build
57using that template.
58
59.. tip::
60
61 Prefer to use ``pw_executable`` over plain ``executable`` targets to allow
62 cleanly building the same code for multiple target configs.
63
64**Arguments**
65
66``pw_executable`` accepts any arguments, as it simply forwards them through to
67the specified custom template.
68
69pw_python_script
70^^^^^^^^^^^^^^^^
71The ``pw_python_script`` template is a convenience wrapper around ``action`` for
72running Python scripts. The main benefit it provides is automatic resolution of
73GN paths to filesystem paths and GN target names to compiled binary files. This
74allows Python scripts to be written independent of GN, taking only filesystem as
75arguments.
76
77Another convenience provided by the template is to allow running scripts without
78any outputs. Sometimes scripts run in a build do not directly produce output
79files, but GN requires that all actions have an output. ``pw_python_script``
80solves this by accepting a boolean ``stamp`` argument which tells it to create a
81dummy output file for the action.
82
83**Arguments**
84
85``pw_python_script`` accepts all of the arguments of a regular ``action``
86target. Additionally, it has some of its own arguments:
87
88* ``stamp``: Optional boolean indicating whether to automatically create a dummy
89 output file for the script. This allows running scripts without specifying any
90 ``outputs``.
91
92**Example**
93
94.. code::
95
96 import("$dir_pw_build/python_script.gni")
97
98 python_script("hello_world") {
99 script = "py/say_hello.py"
100 args = [ "world" ]
101 stamp = true
102 }
103
104pw_input_group
105^^^^^^^^^^^^^^
106``pw_input_group`` defines a group of input files which are not directly
107processed by the build but are still important dependencies of later build
108steps. This is commonly used alongside metadata to propagate file dependencies
109through the build graph and force rebuilds on file modifications.
110
111For example ``pw_docgen`` defines a ``pw_doc_group`` template which outputs
112metadata from a list of input files. The metadata file is not actually part of
113the build, and so changes to any of the input files do not trigger a rebuild.
114This is problematic, as targets that depend on the metadata should rebuild when
115the inputs are modified but GN cannot express this dependency.
116
117``pw_input_group`` solves this problem by allowing a list of files to be listed
118in a target that does not output any build artifacts, causing all dependent
119targets to correctly rebuild.
120
121**Arguments**
122
123``pw_input_group`` accepts all arguments that can be passed to a ``group``
124target, as well as requiring one extra:
125
126* ``inputs``: List of input files.
127
128**Example**
129
130.. code::
131
132 import("$dir_pw_build/input_group.gni")
133
134 pw_input_group("foo_metadata") {
135 metadata = {
136 files = [
137 "x.foo",
138 "y.foo",
139 "z.foo",
140 ]
141 }
142 inputs = metadata.files
143 }
144
145Targets depending on ``foo_metadata`` will rebuild when any of the ``.foo``
146files are modified.
Rob Mohr84f234e2019-12-06 09:16:50 -0800147
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800148CMake / Ninja
149=============
Armando Montanezbcc194b2020-03-10 10:23:18 -0700150
151Pigweed's CMake support is provided primarily for projects that have an existing
152CMake build and wish to integrate Pigweed without switching to a new build
153system.
154
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800155The following command generates Ninja build files in the out/cmake directory.
156
157.. code:: sh
158
159 cmake -B out/cmake -S /path/to/pigweed -G Ninja
160
161Tests can be executed with the ``pw_run_tests_GROUP`` targets. To run the basic
162Pigweed tests, run ``ninja -C out/cmake pw_run_tests_modules``.
163
164CMake functions
165---------------
166CMake convenience functions are defined in ``pw_build/pigweed.cmake``.
167
168* ``pw_auto_add_simple_module`` -- For modules with only one library,
169 automatically declare the library and its tests.
170* ``pw_add_facade`` -- Declare a module facade.
171* ``pw_add_module_library`` -- Add a library that is part of a module.
172* ``pw_add_test`` -- Declare a test target.
173
174See ``pw_build/pigweed.cmake`` for the complete documentation of these
175functions.
176
177Special libraries that do not fit well with these functions are created with the
178standard CMake functions, such as ``add_library`` and ``target_link_libraries``.
179
180Use Pigweed from an existing CMake project
181------------------------------------------
182To use Pigweed libraries form a CMake-based project, simply include the Pigweed
183repository from a ``CMakeLists.txt``.
184
185.. code:: cmake
186
187 add_subdirectory(path/to/pigweed pigweed)
188
189All module libraries will be available as ``module_name`` or
190``module_name.sublibrary``.
191
192If desired, modules can be included individually.
193
194.. code:: cmake
195
196 include(path/to/pigweed/pw_build/pigweed.cmake)
197
198 add_subdirectory(path/to/pigweed/pw_some_module pw_some_module)
199 add_subdirectory(path/to/pigweed/pw_another_module pw_another_module)
200
Rob Mohr84f234e2019-12-06 09:16:50 -0800201Bazel
202=====
Armando Montanezbcc194b2020-03-10 10:23:18 -0700203
204Bazel is currently very experimental, and only builds for host.
205
Rob Mohr84f234e2019-12-06 09:16:50 -0800206The common configuration for Bazel for all modules is in the ``pigweed.bzl``
207file. The built-in Bazel rules ``cc_binary``, ``cc_library``, and ``cc_test``
208are wrapped with ``pw_cc_binary``, ``pw_cc_library``, and ``pw_cc_test``.
209These wrappers add parameters to calls to the compiler and linker.
210
211The ``BUILD`` file is merely a placeholder and currently does nothing.