xref: /aosp_15_r20/external/pigweed/pw_build/cmake.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_build-cmake:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard WorkerCMake
4*61c4878aSAndroid Build Coastguard Worker=====
5*61c4878aSAndroid Build Coastguard Worker.. pigweed-module-subpage::
6*61c4878aSAndroid Build Coastguard Worker   :name: pw_build
7*61c4878aSAndroid Build Coastguard Worker
8*61c4878aSAndroid Build Coastguard WorkerPigweed's `CMake`_ support is provided primarily for projects that have an
9*61c4878aSAndroid Build Coastguard Workerexisting CMake build and wish to integrate Pigweed without switching to a new
10*61c4878aSAndroid Build Coastguard Workerbuild system.
11*61c4878aSAndroid Build Coastguard Worker
12*61c4878aSAndroid Build Coastguard Worker.. tip::
13*61c4878aSAndroid Build Coastguard Worker   To run upstream Pigweed's CMake build use the ``pw build`` command:
14*61c4878aSAndroid Build Coastguard Worker
15*61c4878aSAndroid Build Coastguard Worker   .. code-block:: console
16*61c4878aSAndroid Build Coastguard Worker
17*61c4878aSAndroid Build Coastguard Worker      pw build -r default_cmake
18*61c4878aSAndroid Build Coastguard Worker
19*61c4878aSAndroid Build Coastguard Worker   This will install any required packages, generate cmake build files and invkoke ninja.
20*61c4878aSAndroid Build Coastguard Worker
21*61c4878aSAndroid Build Coastguard Worker   .. code-block:: text
22*61c4878aSAndroid Build Coastguard Worker
23*61c4878aSAndroid Build Coastguard Worker      19:36:58 INF [1/1] Starting ==> Recipe: default_cmake Targets: pw_run_tests.modules pw_apps pw_run_tests.pw_bluetooth Logfile: /out/build_default_cmake.txt
24*61c4878aSAndroid Build Coastguard Worker      19:36:58 INF [1/1] Run ==> pw --no-banner package install emboss
25*61c4878aSAndroid Build Coastguard Worker      19:36:59 INF [1/1] Run ==> pw --no-banner package install nanopb
26*61c4878aSAndroid Build Coastguard Worker      19:37:00 INF [1/1] Run ==> pw --no-banner package install boringssl
27*61c4878aSAndroid Build Coastguard Worker      19:37:10 INF [1/1] Run ==> cmake --fresh --debug-output -DCMAKE_MESSAGE_LOG_LEVEL=WARNING -S . -B ./out/cmake -G Ninja -DCMAKE_TOOLCHAIN_FILE=./pw_toolchain/host_clang/toolchain.cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -Ddir_pw_third_party_nanopb=./environment/packages/nanopb -Dpw_third_party_nanopb_ADD_SUBDIRECTORY=ON -Ddir_pw_third_party_emboss=./environment/packages/emboss -Ddir_pw_third_party_boringssl=./environment/packages/boringssl -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
28*61c4878aSAndroid Build Coastguard Worker      19:37:10 INF [1/1] Run ==> ninja -C out/cmake pw_apps pw_run_tests.modules pw_run_tests.pw_bluetooth
29*61c4878aSAndroid Build Coastguard Worker
30*61c4878aSAndroid Build Coastguard Worker   :ref:`module-pw_watch` works with ``pw build`` as well. You can run the
31*61c4878aSAndroid Build Coastguard Worker   following to automatically rebuild when files change.
32*61c4878aSAndroid Build Coastguard Worker
33*61c4878aSAndroid Build Coastguard Worker   .. code-block:: console
34*61c4878aSAndroid Build Coastguard Worker
35*61c4878aSAndroid Build Coastguard Worker      pw build -r default_cmake --watch
36*61c4878aSAndroid Build Coastguard Worker
37*61c4878aSAndroid Build Coastguard WorkerCMake functions
38*61c4878aSAndroid Build Coastguard Worker---------------
39*61c4878aSAndroid Build Coastguard WorkerCMake convenience functions are defined in ``pw_build/pigweed.cmake``.
40*61c4878aSAndroid Build Coastguard Worker
41*61c4878aSAndroid Build Coastguard Worker* ``pw_add_library_generic`` -- The base helper used to instantiate CMake
42*61c4878aSAndroid Build Coastguard Worker  libraries. This is meant for use in downstream projects as upstream Pigweed
43*61c4878aSAndroid Build Coastguard Worker  modules are expected to use ``pw_add_library``.
44*61c4878aSAndroid Build Coastguard Worker* ``pw_add_library`` -- Add an upstream Pigweed library.
45*61c4878aSAndroid Build Coastguard Worker* ``pw_add_facade_generic`` -- The base helper used to instantiate facade
46*61c4878aSAndroid Build Coastguard Worker  libraries. This is meant for use in downstream projects as upstream Pigweed
47*61c4878aSAndroid Build Coastguard Worker  modules are expected to use ``pw_add_facade``.
48*61c4878aSAndroid Build Coastguard Worker* ``pw_add_facade`` -- Declare an upstream Pigweed facade.
49*61c4878aSAndroid Build Coastguard Worker* ``pw_set_backend`` -- Set the backend library to use for a facade.
50*61c4878aSAndroid Build Coastguard Worker* ``pw_add_test_generic`` -- The base helper used to instantiate test targets.
51*61c4878aSAndroid Build Coastguard Worker  This is meant for use in downstrema projects as upstream Pigweed modules are
52*61c4878aSAndroid Build Coastguard Worker  expected to use ``pw_add_test``.
53*61c4878aSAndroid Build Coastguard Worker* ``pw_add_test`` -- Declare an upstream Pigweed test target.
54*61c4878aSAndroid Build Coastguard Worker* ``pw_add_test_group`` -- Declare a target to group and bundle test targets.
55*61c4878aSAndroid Build Coastguard Worker* ``pw_target_link_targets`` -- Helper wrapper around ``target_link_libraries``
56*61c4878aSAndroid Build Coastguard Worker  which only supports CMake targets and detects when the target does not exist.
57*61c4878aSAndroid Build Coastguard Worker  Note that generator expressions are not supported.
58*61c4878aSAndroid Build Coastguard Worker* ``pw_add_global_compile_options`` -- Applies compilation options to all
59*61c4878aSAndroid Build Coastguard Worker  targets in the build. This should only be used to add essential compilation
60*61c4878aSAndroid Build Coastguard Worker  options, such as those that affect the ABI. Use ``pw_add_library`` or
61*61c4878aSAndroid Build Coastguard Worker  ``target_compile_options`` to apply other compile options.
62*61c4878aSAndroid Build Coastguard Worker* ``pw_add_error_target`` -- Declares target which reports a message and causes
63*61c4878aSAndroid Build Coastguard Worker  a build failure only when compiled. This is useful when ``FATAL_ERROR``
64*61c4878aSAndroid Build Coastguard Worker  messages cannot be used to catch problems during the CMake configuration
65*61c4878aSAndroid Build Coastguard Worker  phase.
66*61c4878aSAndroid Build Coastguard Worker* ``pw_parse_arguments`` -- Helper to parse CMake function arguments.
67*61c4878aSAndroid Build Coastguard Worker* ``pw_rebase_paths`` -- Helper to update a set of file paths to be rooted on a
68*61c4878aSAndroid Build Coastguard Worker  new directory.
69*61c4878aSAndroid Build Coastguard Worker
70*61c4878aSAndroid Build Coastguard WorkerSee ``pw_build/pigweed.cmake`` for the complete documentation of these
71*61c4878aSAndroid Build Coastguard Workerfunctions.
72*61c4878aSAndroid Build Coastguard Worker
73*61c4878aSAndroid Build Coastguard WorkerSpecial libraries that do not fit well with these functions are created with the
74*61c4878aSAndroid Build Coastguard Workerstandard CMake functions, such as ``add_library`` and ``target_link_libraries``.
75*61c4878aSAndroid Build Coastguard Worker
76*61c4878aSAndroid Build Coastguard WorkerFacades and backends
77*61c4878aSAndroid Build Coastguard Worker--------------------
78*61c4878aSAndroid Build Coastguard WorkerThe CMake build uses CMake cache variables for configuring
79*61c4878aSAndroid Build Coastguard Worker:ref:`facades<docs-module-structure-facades>` and backends. Cache variables are
80*61c4878aSAndroid Build Coastguard Workersimilar to GN's build args set with ``gn args``. Unlike GN, CMake does not
81*61c4878aSAndroid Build Coastguard Workersupport multi-toolchain builds, so these variables have a single global value
82*61c4878aSAndroid Build Coastguard Workerper build directory.
83*61c4878aSAndroid Build Coastguard Worker
84*61c4878aSAndroid Build Coastguard WorkerThe ``pw_add_module_facade`` function declares a cache variable named
85*61c4878aSAndroid Build Coastguard Worker``<module_name>_BACKEND`` for each facade. Cache variables can be awkward to
86*61c4878aSAndroid Build Coastguard Workerwork with, since their values only change when they're assigned, but then
87*61c4878aSAndroid Build Coastguard Workerpersist accross CMake invocations. These variables should be set in one of the
88*61c4878aSAndroid Build Coastguard Workerfollowing ways:
89*61c4878aSAndroid Build Coastguard Worker
90*61c4878aSAndroid Build Coastguard Worker* Prior to setting a backend, your application should include
91*61c4878aSAndroid Build Coastguard Worker  ``$ENV{PW_ROOT}/backends.cmake``. This file will setup all the backend targets
92*61c4878aSAndroid Build Coastguard Worker  such that any misspelling of a facade or backend will yield a warning.
93*61c4878aSAndroid Build Coastguard Worker
94*61c4878aSAndroid Build Coastguard Worker  .. note::
95*61c4878aSAndroid Build Coastguard Worker    Zephyr developers do not need to do this, backends can be set automatically
96*61c4878aSAndroid Build Coastguard Worker    by enabling the appropriate Kconfig options.
97*61c4878aSAndroid Build Coastguard Worker
98*61c4878aSAndroid Build Coastguard Worker* Call ``pw_set_backend`` to set backends appropriate for the target in the
99*61c4878aSAndroid Build Coastguard Worker  target's toolchain file. The toolchain file is provided to ``cmake`` with
100*61c4878aSAndroid Build Coastguard Worker  ``-DCMAKE_TOOLCHAIN_FILE=<toolchain file>``.
101*61c4878aSAndroid Build Coastguard Worker* Call ``pw_set_backend`` in the top-level ``CMakeLists.txt`` before other
102*61c4878aSAndroid Build Coastguard Worker  CMake code executes.
103*61c4878aSAndroid Build Coastguard Worker* Set the backend variable at the command line with the ``-D`` option.
104*61c4878aSAndroid Build Coastguard Worker
105*61c4878aSAndroid Build Coastguard Worker  .. code-block:: sh
106*61c4878aSAndroid Build Coastguard Worker
107*61c4878aSAndroid Build Coastguard Worker     cmake -B out/cmake_host -S "$PW_ROOT" -G Ninja \
108*61c4878aSAndroid Build Coastguard Worker         -DCMAKE_TOOLCHAIN_FILE=$PW_ROOT/pw_toolchain/host_clang/toolchain.cmake \
109*61c4878aSAndroid Build Coastguard Worker         -Dpw_log_BACKEND=pw_log_basic
110*61c4878aSAndroid Build Coastguard Worker
111*61c4878aSAndroid Build Coastguard Worker* Temporarily override a backend by setting it interactively with ``ccmake`` or
112*61c4878aSAndroid Build Coastguard Worker  ``cmake-gui``.
113*61c4878aSAndroid Build Coastguard Worker
114*61c4878aSAndroid Build Coastguard WorkerIf the backend is set to a build target that does not exist, there will be an
115*61c4878aSAndroid Build Coastguard Workererror message like the following:
116*61c4878aSAndroid Build Coastguard Worker
117*61c4878aSAndroid Build Coastguard Worker.. code-block::
118*61c4878aSAndroid Build Coastguard Worker
119*61c4878aSAndroid Build Coastguard Worker   CMake Error at pw_build/pigweed.cmake:257 (message):
120*61c4878aSAndroid Build Coastguard Worker     my_module.my_facade's INTERFACE dep "my_nonexistent_backend" is not
121*61c4878aSAndroid Build Coastguard Worker     a target.
122*61c4878aSAndroid Build Coastguard Worker   Call Stack (most recent call first):
123*61c4878aSAndroid Build Coastguard Worker     pw_build/pigweed.cmake:238:EVAL:1 (_pw_target_link_targets_deferred_check)
124*61c4878aSAndroid Build Coastguard Worker     CMakeLists.txt:DEFERRED
125*61c4878aSAndroid Build Coastguard Worker
126*61c4878aSAndroid Build Coastguard Worker
127*61c4878aSAndroid Build Coastguard WorkerToolchain setup
128*61c4878aSAndroid Build Coastguard Worker---------------
129*61c4878aSAndroid Build Coastguard WorkerIn CMake, the toolchain is configured by setting CMake variables, as described
130*61c4878aSAndroid Build Coastguard Workerin the `CMake documentation <https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html>`_.
131*61c4878aSAndroid Build Coastguard WorkerThese variables are typically set in a toolchain CMake file passed to ``cmake``
132*61c4878aSAndroid Build Coastguard Workerwith the ``-D`` option (``-DCMAKE_TOOLCHAIN_FILE=path/to/file.cmake``).
133*61c4878aSAndroid Build Coastguard WorkerFor Pigweed embedded builds, set ``CMAKE_SYSTEM_NAME`` to the empty string
134*61c4878aSAndroid Build Coastguard Worker(``""``).
135*61c4878aSAndroid Build Coastguard Worker
136*61c4878aSAndroid Build Coastguard WorkerToolchains may set the ``pw_build_WARNINGS`` variable to a list of ``INTERFACE``
137*61c4878aSAndroid Build Coastguard Workerlibraries with compilation options for Pigweed's upstream libraries. This
138*61c4878aSAndroid Build Coastguard Workerdefaults to a strict set of warnings. Projects may need to use less strict
139*61c4878aSAndroid Build Coastguard Workercompilation warnings to compile backends exposed to Pigweed code (such as
140*61c4878aSAndroid Build Coastguard Worker``pw_log``) that cannot compile with Pigweed's flags. If desired, Projects can
141*61c4878aSAndroid Build Coastguard Workeraccess these warnings by depending on ``pw_build.warnings``.
142*61c4878aSAndroid Build Coastguard Worker
143*61c4878aSAndroid Build Coastguard WorkerThird party libraries
144*61c4878aSAndroid Build Coastguard Worker---------------------
145*61c4878aSAndroid Build Coastguard WorkerThe CMake build includes third-party libraries similarly to the GN build. A
146*61c4878aSAndroid Build Coastguard Worker``dir_pw_third_party_<library>`` cache variable is defined for each third-party
147*61c4878aSAndroid Build Coastguard Workerdependency. The variable must be set to the absolute path of the library in
148*61c4878aSAndroid Build Coastguard Workerorder to use it. If the variable is empty
149*61c4878aSAndroid Build Coastguard Worker(``if("${dir_pw_third_party_<library>}" STREQUAL "")``), the dependency is not
150*61c4878aSAndroid Build Coastguard Workeravailable.
151*61c4878aSAndroid Build Coastguard Worker
152*61c4878aSAndroid Build Coastguard WorkerThird-party dependencies are not automatically added to the build. They can be
153*61c4878aSAndroid Build Coastguard Workermanually added with ``add_subdirectory`` or by setting the
154*61c4878aSAndroid Build Coastguard Worker``pw_third_party_<library>_ADD_SUBDIRECTORY`` option to ``ON``.
155*61c4878aSAndroid Build Coastguard Worker
156*61c4878aSAndroid Build Coastguard WorkerThird party variables are set like any other cache global variable in CMake. It
157*61c4878aSAndroid Build Coastguard Workeris recommended to set these in one of the following ways:
158*61c4878aSAndroid Build Coastguard Worker
159*61c4878aSAndroid Build Coastguard Worker* Set with the CMake ``set`` function in the toolchain file or a
160*61c4878aSAndroid Build Coastguard Worker  ``CMakeLists.txt`` before other CMake code executes.
161*61c4878aSAndroid Build Coastguard Worker
162*61c4878aSAndroid Build Coastguard Worker  .. code-block:: cmake
163*61c4878aSAndroid Build Coastguard Worker
164*61c4878aSAndroid Build Coastguard Worker     set(dir_pw_third_party_nanopb ${CMAKE_CURRENT_SOURCE_DIR}/external/nanopb CACHE PATH "" FORCE)
165*61c4878aSAndroid Build Coastguard Worker
166*61c4878aSAndroid Build Coastguard Worker* Set the variable at the command line with the ``-D`` option.
167*61c4878aSAndroid Build Coastguard Worker
168*61c4878aSAndroid Build Coastguard Worker  .. code-block:: sh
169*61c4878aSAndroid Build Coastguard Worker
170*61c4878aSAndroid Build Coastguard Worker     cmake -B out/cmake_host -S "$PW_ROOT" -G Ninja \
171*61c4878aSAndroid Build Coastguard Worker         -DCMAKE_TOOLCHAIN_FILE=$PW_ROOT/pw_toolchain/host_clang/toolchain.cmake \
172*61c4878aSAndroid Build Coastguard Worker         -Ddir_pw_third_party_nanopb=/path/to/nanopb
173*61c4878aSAndroid Build Coastguard Worker
174*61c4878aSAndroid Build Coastguard Worker* Set the variable interactively with ``ccmake`` or ``cmake-gui``.
175*61c4878aSAndroid Build Coastguard Worker
176*61c4878aSAndroid Build Coastguard Worker.. _module-pw_build-existing-cmake-project:
177*61c4878aSAndroid Build Coastguard Worker
178*61c4878aSAndroid Build Coastguard WorkerUse Pigweed from an existing CMake project
179*61c4878aSAndroid Build Coastguard Worker------------------------------------------
180*61c4878aSAndroid Build Coastguard WorkerTo use Pigweed libraries form a CMake-based project, simply include the Pigweed
181*61c4878aSAndroid Build Coastguard Workerrepository from a ``CMakeLists.txt``.
182*61c4878aSAndroid Build Coastguard Worker
183*61c4878aSAndroid Build Coastguard Worker.. code-block:: cmake
184*61c4878aSAndroid Build Coastguard Worker
185*61c4878aSAndroid Build Coastguard Worker   add_subdirectory(path/to/pigweed pigweed)
186*61c4878aSAndroid Build Coastguard Worker
187*61c4878aSAndroid Build Coastguard WorkerAll module libraries will be available as ``module_name`` or
188*61c4878aSAndroid Build Coastguard Worker``module_name.sublibrary``.
189*61c4878aSAndroid Build Coastguard Worker
190*61c4878aSAndroid Build Coastguard WorkerIf desired, modules can be included individually.
191*61c4878aSAndroid Build Coastguard Worker
192*61c4878aSAndroid Build Coastguard Worker.. code-block:: cmake
193*61c4878aSAndroid Build Coastguard Worker
194*61c4878aSAndroid Build Coastguard Worker   add_subdirectory(path/to/pigweed/pw_some_module pw_some_module)
195*61c4878aSAndroid Build Coastguard Worker   add_subdirectory(path/to/pigweed/pw_another_module pw_another_module)
196*61c4878aSAndroid Build Coastguard Worker
197*61c4878aSAndroid Build Coastguard Worker.. seealso::
198*61c4878aSAndroid Build Coastguard Worker   Additional Pigweed CMake function references:
199*61c4878aSAndroid Build Coastguard Worker   - :bdg-ref-primary-line:`module-pw_fuzzer-guides-using_fuzztest-toolchain`
200*61c4878aSAndroid Build Coastguard Worker   - :bdg-ref-primary-line:`module-pw_protobuf_compiler-cmake`
201*61c4878aSAndroid Build Coastguard Worker   - :bdg-ref-primary-line:`module-pw_unit_test-cmake`
202