Skip to content

Commit c33de41

Browse files
committed
[nrf fromlist] cmake: introduction of ZephyrBuildConfiguration package
The ZephyrBuildConfiguration package allow downstream users to control the Zephyr build system using a cmake package. A Zephyr Build Configuration package allows for setting of additional DTS_ROOT, BOARD_ROOT, and similar variables without having to patch Zephyr repo, but it also allows for inclusion of additional boilerplate code for more advanced use cases. The repository or folder containing the Zephyr Build Configuration package must be on toplevel in the Zephyr workspace. Signed-off-by: Torsten Rasmussen <[email protected]>
1 parent b477654 commit c33de41

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

cmake/app/boilerplate.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ if((NOT DEFINED ZEPHYR_BASE) AND (DEFINED ENV_ZEPHYR_BASE))
8989
set(ZEPHYR_BASE ${ENV_ZEPHYR_BASE} CACHE PATH "Zephyr base")
9090
endif()
9191

92+
find_package(ZephyrBuildConfiguration NAMES ZephyrBuild PATHS ${ZEPHYR_BASE}/../* QUIET NO_DEFAULT_PATH NO_POLICY_SCOPE)
93+
9294
# Note any later project() resets PROJECT_SOURCE_DIR
9395
file(TO_CMAKE_PATH "${ZEPHYR_BASE}" PROJECT_SOURCE_DIR)
9496

doc/guides/env_vars.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ To get this value back into your current terminal environment, **you must run**
106106
The value will be lost if you close the window, etc.; run ``zephyr-env.cmd``
107107
again to get it back.
108108

109+
Option 4: Using Zephyr Build Configuration CMake package
110+
--------------------------------------------------------
111+
112+
Choose this option if you want to make those variable settings shared among all
113+
users of your project.
114+
115+
Using a :ref:`cmake_build_config_package` allows you to commit the shared
116+
settings into the repository, so that all users can share them.
117+
118+
It also removes the need for running ``source zephyr-env.sh`` or
119+
``zephyr-env.cmd`` when opening a new terminal.
120+
109121
.. _zephyr-env:
110122

111123
Zephyr Environment Scripts

doc/guides/zephyr_cmake_package.rst

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,72 @@ Such a CMakeLists.txt could look as:
382382
383383
project(my_first_app)
384384
385+
.. _cmake_build_config_package:
386+
387+
Zephyr Build Configuration CMake package
388+
****************************************
389+
390+
The Zephyr Build Configuration CMake package provides a possibility for a Zephyr based project to
391+
control Zephyr build settings in a generic way.
392+
393+
It is similar to the use of ``.zephyrrc`` but with the possibility to automatically allow all users
394+
to share the build configuration through the project repository.
395+
But it also allows more advanced use cases than a ``.zephyrrc``-file, such as loading of additional
396+
CMake boilerplate code.
397+
398+
The Zephyr Build Configuration CMake package will be loaded in the Zephyr boilerplate code after
399+
initial properties and ``ZEPHYR_BASE`` has been defined, but before CMake code execution.
400+
This allows the Zephyr Build Configuration CMake package to setup or extend properties such as:
401+
``DTS_ROOT``, ``BOARD_ROOT``, ``TOOLCHAIN_ROOT`` / other toolchain setup, fixed overlays, and any
402+
other property that can be controlled. It also allows inclusion of additional boilerplate code.
403+
404+
To provide a Zephyr Build Configuration CMake package, create ``ZephyrBuildConfig.cmake`` and place
405+
it in a Zephyr workspace top-level folder as:
406+
407+
.. code-block:: none
408+
409+
<projects>/zephyr-workspace
410+
├── zephyr
411+
├── ...
412+
└── zephyr application (can be named anything)
413+
└── share/zephyrbuild-package/cmake/ZephyrBuildConfig.cmake
414+
415+
The Zephyr Build Configuration CMake package will not search in any CMake default search paths, and
416+
thus cannot be installed in the CMake package registry. There will be no version checking on the
417+
Zephyr Build Configuration package.
418+
419+
.. note:: ``share/zephyrbuild-package/cmake/ZephyrBuildConfig.cmake`` follows the same folder
420+
structure as the Zephyr CMake package.
421+
422+
It is possible to place ``ZephyrBuildConfig.cmake`` directly in a
423+
``<zephyr application>/cmake`` folder or another folder, as long as that folder is
424+
honoring the `CMake package search`_ algorithm.
425+
426+
A sample ``ZephyrBuildConfig.cmake`` can be seen below.
427+
428+
.. code-block:: cmake
429+
430+
# ZephyrBuildConfig.cmake sample code
431+
432+
# To ensure final path is absolute and does not contain ../.. in variable.
433+
get_filename_component(APPLICATION_PROJECT_DIR
434+
${CMAKE_CURRENT_LIST_DIR}/../../..
435+
ABSOLUTE
436+
)
437+
438+
# Add this project to list of board roots
439+
list(APPEND BOARD_ROOT ${APPLICATION_PROJECT_DIR})
440+
441+
# Default to GNU Arm Embedded toolchain if no toolchain is set
442+
if(NOT ENV{ZEPHYR_TOOLCHAIN_VARIANT})
443+
set(ZEPHYR_TOOLCHAIN_VARIANT gnuarmemb)
444+
find_program(GNU_ARM_GCC arm-none-eabi-gcc)
445+
if(NOT ${GNU_ARM_GCC} STREQUAL GNU_ARM_GCC-NOTFOUND)
446+
# The toolchain root is located above the path to the compiler.
447+
get_filename_component(GNUARMEMB_TOOLCHAIN_PATH ${GNU_ARM_GCC}/../.. ABSOLUTE)
448+
endif()
449+
endif()
450+
385451
Zephyr CMake package source code
386452
********************************
387453

@@ -428,3 +494,4 @@ The following is an overview of those files
428494
.. _CMake package: https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html
429495
.. _CMake user package registry: https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html#user-package-registry
430496
.. _CMake package version: https://cmake.org/cmake/help/latest/command/find_package.html#version-selection
497+
.. _CMake package search: https://cmake.org/cmake/help/latest/command/find_package.html#search-procedure

0 commit comments

Comments
 (0)