blob: 7057946f945a79ab2d576be5b5b13652086a955c [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
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -080011`GN`_/`Ninja`_, `CMake`_/`Ninja`_, or `Bazel`_.
Rob Mohr84f234e2019-12-06 09:16:50 -080012
13.. _GN: https://gn.googlesource.com/gn/
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -080014.. _CMake: https://cmake.org/
Rob Mohr84f234e2019-12-06 09:16:50 -080015.. _Ninja: https://ninja-build.org/
16.. _Bazel: https://bazel.build/
17
18GN / Ninja
19==========
20The common configuration for GN for all modules is in the ``BUILD.gn`` file.
21It contains ``config`` declarations referenced by ``BUILD.gn`` files in other
22modules.
23
Alexei Frolov69ad1922019-12-13 13:11:32 -080024The module also provides some useful GN templates for build targets.
25
26Templates
27---------
28
29pw_executable
30^^^^^^^^^^^^^
31.. code::
32
33 import("$dir_pw_build/pw_executable.gni")
34
35The ``pw_executable`` template is a wrapper around executable targets which
36builds for a globally-defined target type. This is controlled by the build
37variable ``pw_executable_config.target_type``. For example, setting this
38variable to ``stm32f429i_executable`` causes all executable targets to build
39using that template.
40
41.. tip::
42
43 Prefer to use ``pw_executable`` over plain ``executable`` targets to allow
44 cleanly building the same code for multiple target configs.
45
46**Arguments**
47
48``pw_executable`` accepts any arguments, as it simply forwards them through to
49the specified custom template.
50
51pw_python_script
52^^^^^^^^^^^^^^^^
53The ``pw_python_script`` template is a convenience wrapper around ``action`` for
54running Python scripts. The main benefit it provides is automatic resolution of
55GN paths to filesystem paths and GN target names to compiled binary files. This
56allows Python scripts to be written independent of GN, taking only filesystem as
57arguments.
58
59Another convenience provided by the template is to allow running scripts without
60any outputs. Sometimes scripts run in a build do not directly produce output
61files, but GN requires that all actions have an output. ``pw_python_script``
62solves this by accepting a boolean ``stamp`` argument which tells it to create a
63dummy output file for the action.
64
65**Arguments**
66
67``pw_python_script`` accepts all of the arguments of a regular ``action``
68target. Additionally, it has some of its own arguments:
69
70* ``stamp``: Optional boolean indicating whether to automatically create a dummy
71 output file for the script. This allows running scripts without specifying any
72 ``outputs``.
73
74**Example**
75
76.. code::
77
78 import("$dir_pw_build/python_script.gni")
79
80 python_script("hello_world") {
81 script = "py/say_hello.py"
82 args = [ "world" ]
83 stamp = true
84 }
85
86pw_input_group
87^^^^^^^^^^^^^^
88``pw_input_group`` defines a group of input files which are not directly
89processed by the build but are still important dependencies of later build
90steps. This is commonly used alongside metadata to propagate file dependencies
91through the build graph and force rebuilds on file modifications.
92
93For example ``pw_docgen`` defines a ``pw_doc_group`` template which outputs
94metadata from a list of input files. The metadata file is not actually part of
95the build, and so changes to any of the input files do not trigger a rebuild.
96This is problematic, as targets that depend on the metadata should rebuild when
97the inputs are modified but GN cannot express this dependency.
98
99``pw_input_group`` solves this problem by allowing a list of files to be listed
100in a target that does not output any build artifacts, causing all dependent
101targets to correctly rebuild.
102
103**Arguments**
104
105``pw_input_group`` accepts all arguments that can be passed to a ``group``
106target, as well as requiring one extra:
107
108* ``inputs``: List of input files.
109
110**Example**
111
112.. code::
113
114 import("$dir_pw_build/input_group.gni")
115
116 pw_input_group("foo_metadata") {
117 metadata = {
118 files = [
119 "x.foo",
120 "y.foo",
121 "z.foo",
122 ]
123 }
124 inputs = metadata.files
125 }
126
127Targets depending on ``foo_metadata`` will rebuild when any of the ``.foo``
128files are modified.
Rob Mohr84f234e2019-12-06 09:16:50 -0800129
Wyatt Hepler0fbcdfc2020-01-02 07:53:39 -0800130CMake / Ninja
131=============
132The following command generates Ninja build files in the out/cmake directory.
133
134.. code:: sh
135
136 cmake -B out/cmake -S /path/to/pigweed -G Ninja
137
138Tests can be executed with the ``pw_run_tests_GROUP`` targets. To run the basic
139Pigweed tests, run ``ninja -C out/cmake pw_run_tests_modules``.
140
141CMake functions
142---------------
143CMake convenience functions are defined in ``pw_build/pigweed.cmake``.
144
145* ``pw_auto_add_simple_module`` -- For modules with only one library,
146 automatically declare the library and its tests.
147* ``pw_add_facade`` -- Declare a module facade.
148* ``pw_add_module_library`` -- Add a library that is part of a module.
149* ``pw_add_test`` -- Declare a test target.
150
151See ``pw_build/pigweed.cmake`` for the complete documentation of these
152functions.
153
154Special libraries that do not fit well with these functions are created with the
155standard CMake functions, such as ``add_library`` and ``target_link_libraries``.
156
157Use Pigweed from an existing CMake project
158------------------------------------------
159To use Pigweed libraries form a CMake-based project, simply include the Pigweed
160repository from a ``CMakeLists.txt``.
161
162.. code:: cmake
163
164 add_subdirectory(path/to/pigweed pigweed)
165
166All module libraries will be available as ``module_name`` or
167``module_name.sublibrary``.
168
169If desired, modules can be included individually.
170
171.. code:: cmake
172
173 include(path/to/pigweed/pw_build/pigweed.cmake)
174
175 add_subdirectory(path/to/pigweed/pw_some_module pw_some_module)
176 add_subdirectory(path/to/pigweed/pw_another_module pw_another_module)
177
Rob Mohr84f234e2019-12-06 09:16:50 -0800178Bazel
179=====
180The common configuration for Bazel for all modules is in the ``pigweed.bzl``
181file. The built-in Bazel rules ``cc_binary``, ``cc_library``, and ``cc_test``
182are wrapped with ``pw_cc_binary``, ``pw_cc_library``, and ``pw_cc_test``.
183These wrappers add parameters to calls to the compiler and linker.
184
185The ``BUILD`` file is merely a placeholder and currently does nothing.