Skip to content

Commit b88e615

Browse files
tejlmandjhedberg
authored andcommitted
sysbuild: support application CMakePresets.json files with sysbuild
This commit provides a CMakePresets.json which includes the sample's CMakePresets.json file. `west build` is extended to set `APP_DIR` in environment when sysbuild is used. This allows sysbuild's CMakePresets.json to include the sample's presets file. Signed-off-by: Torsten Rasmussen <[email protected]>
1 parent 98c92d3 commit b88e615

File tree

4 files changed

+78
-5
lines changed

4 files changed

+78
-5
lines changed

doc/build/sysbuild/index.rst

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ As mentioned above, you can run sysbuild via ``west build`` or ``cmake``.
140140

141141
To use sysbuild directly with CMake, you must specify the sysbuild
142142
project as the source folder, and give ``-DAPP_DIR=<path-to-sample>`` as
143-
an extra CMake argument. ``APP_DIR`` is the path to the main Zephyr
144-
application managed by sysbuild.
143+
an extra CMake argument or set APP_DIR as environment variable.
144+
``APP_DIR`` is the path to the main Zephyr application managed by sysbuild.
145145

146146
.. tip::
147147

@@ -880,3 +880,57 @@ can be added.
880880
:maxdepth: 1
881881

882882
images.rst
883+
884+
Sysbuild and CMake presets
885+
**************************
886+
887+
`CMake presets <https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html>`_ can be used with
888+
Sysbuild but not all preset macros will work as expected.
889+
890+
.. note::
891+
892+
Using CMake presets with sysbuild requires CMake version 3.27 or higher.
893+
894+
As described in :ref:`sysbuild` then sysbuild is a higher-level build system which means that when
895+
CMake presets are used together with sysbuild, then the preset is consumed and processed by sysbuild
896+
itself and result is passed to the application.
897+
898+
Running sysbuild with preset.
899+
900+
.. tabs::
901+
902+
.. group-tab:: ``west build``
903+
904+
Here is an example where preset ``release`` should be used.
905+
For details, see :ref:`west-multi-domain-builds` in the ``west build documentation``.
906+
907+
.. zephyr-app-commands::
908+
:tool: west
909+
:zephyr-app: samples/hello_world
910+
:board: reel_board
911+
:goals: build
912+
:west-args: --sysbuild -- --preset=release
913+
:compact:
914+
915+
.. group-tab:: ``cmake``
916+
917+
Here is an example using CMake and Ninja.
918+
919+
.. code-block:: shell
920+
921+
APP_DIR=samples/hello_world cmake -Bbuild -GNinja -DBOARD=reel_board share/sysbuild
922+
ninja -Cbuild
923+
924+
When using CMake presets with sysbuild then ``APP_DIR`` must be set in environment in order
925+
for Sysbuild CMake to be able to include the ``CMakePresets.json`` from the main Zephyr
926+
application's source directory.
927+
928+
.. note::
929+
930+
As sysbuild changes the top-level cmake project to its own directory, the cmake presets are
931+
parsed from there, the application's presets are included from this file verbatim.
932+
Therefore relative paths, and macros resolving relative to the source directory will not work as
933+
expected, but as relative to share/sysbuild, for example ``${sourceDir}``.
934+
935+
The ``${fileDir}`` macro can be used to create portable paths relative to the application's
936+
directory.

scripts/west_commands/build.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,8 @@ def _run_cmake(self, board, origin, cmake_opts):
618618
if not self.run_cmake:
619619
return
620620

621+
cmake_env = None
622+
621623
self._banner('generating a build system')
622624

623625
if board is not None and origin != 'CMakeCache.txt':
@@ -644,8 +646,9 @@ def _run_cmake(self, board, origin, cmake_opts):
644646

645647
config_sysbuild = config_getboolean('sysbuild', False)
646648
if self.args.sysbuild or (config_sysbuild and not self.args.no_sysbuild):
647-
cmake_opts.extend([f'-S{SYSBUILD_PROJ_DIR}',
648-
f'-DAPP_DIR:PATH={self.source_dir}'])
649+
cmake_opts.extend([f'-S{SYSBUILD_PROJ_DIR}'])
650+
cmake_env = os.environ.copy()
651+
cmake_env["APP_DIR"] = str(self.source_dir)
649652
else:
650653
# self.args.no_sysbuild == True or config sysbuild False
651654
cmake_opts.extend([f'-S{self.source_dir}'])
@@ -661,7 +664,7 @@ def _run_cmake(self, board, origin, cmake_opts):
661664
f'-G{config_get("generator", DEFAULT_CMAKE_GENERATOR)}']
662665
if cmake_opts:
663666
final_cmake_args.extend(cmake_opts)
664-
run_cmake(final_cmake_args, dry_run=self.args.dry_run)
667+
run_cmake(final_cmake_args, dry_run=self.args.dry_run, env=cmake_env)
665668

666669
def _run_pristine(self):
667670
self._banner(f'making build dir {self.build_dir} pristine')

share/sysbuild/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
cmake_minimum_required(VERSION 3.20)
66

7+
8+
if(NOT DEFINED APP_DIR AND DEFINED ENV{APP_DIR})
9+
set(APP_DIR $ENV{APP_DIR})
10+
endif()
11+
712
if(NOT DEFINED APP_DIR)
813
message(FATAL_ERROR "No main application specified")
914
endif()

share/sysbuild/CMakePresets.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": 7,
3+
"cmakeMinimumRequired": {
4+
"major": 3,
5+
"minor": 27,
6+
"patch": 0
7+
},
8+
"include": [
9+
"$penv{APP_DIR}/CMakePresets.json"
10+
]
11+
}

0 commit comments

Comments
 (0)