Part 1
요약·해설
User registry, ZEPHYR_BASE, ZEPHYR_PREFER와 workspace·application configuration package를 설명합니다.
find_package
zephyr-export
Version · EXACT
ZEPHYR_PREFER
Part 2
접을 수 있는 영어 원문 전체
영어 원문 전체 펼치기
11E3F5918C81A2A009D1443B285A7EE3E135BFE11E9C8D94A3748DAA2E5FEAAC
.. _cmake_pkg:
Zephyr CMake Package
####################
The Zephyr `CMake package`_ is a convenient way to create a Zephyr-based application.
.. note::
The :ref:`zephyr-app-types` section introduces the application types
used in this page.
The Zephyr CMake package ensures that CMake can automatically select a Zephyr installation to use for building
the application, whether it is a :ref:`Zephyr repository application <zephyr-repo-app>`,
a :ref:`Zephyr workspace application <zephyr-workspace-app>`, or a
:ref:`Zephyr freestanding application <zephyr-freestanding-app>`.
When developing a Zephyr-based application, then a developer simply needs to write
``find_package(Zephyr)`` in the beginning of the application :file:`CMakeLists.txt` file.
To use the Zephyr CMake package it must first be exported to the `CMake user package registry`_.
This is means creating a reference to the current Zephyr installation inside the
CMake user package registry.
.. tabs::
.. group-tab:: Ubuntu
In Linux, the CMake user package registry is found in:
``~/.cmake/packages/Zephyr``
.. group-tab:: macOS
In macOS, the CMake user package registry is found in:
``~/.cmake/packages/Zephyr``
.. group-tab:: Windows
In Windows, the CMake user package registry is found in:
``HKEY_CURRENT_USER\Software\Kitware\CMake\Packages\Zephyr``
The Zephyr CMake package allows CMake to automatically find a Zephyr base.
One or more Zephyr installations must be exported.
Exporting multiple Zephyr installations may be useful when developing or testing
Zephyr freestanding applications, Zephyr workspace application with vendor forks, etc..
Zephyr CMake package export (west)
**********************************
When installing Zephyr using :ref:`west <get_the_code>` then it is recommended
to export Zephyr using ``west zephyr-export``.
.. _zephyr_cmake_package_export:
Zephyr CMake package export (without west)
******************************************
Zephyr CMake package is exported to the CMake user package registry using the following commands:
.. code-block:: bash
cmake -P <PATH-TO-ZEPHYR>/share/zephyr-package/cmake/zephyr_export.cmake
This will export the current Zephyr to the CMake user package registry.
To also export the Zephyr Unittest CMake package, run the following command in addition:
.. code-block:: bash
cmake -P <PATH-TO-ZEPHYR>/share/zephyrunittest-package/cmake/zephyr_export.cmake
.. _zephyr_cmake_package_zephyr_base:
Zephyr Base Environment Setting
*******************************
The Zephyr CMake package search functionality allows for explicitly specifying
a Zephyr base using an environment variable.
To do this, use the following ``find_package()`` syntax:
.. code-block:: cmake
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
This syntax instructs CMake to first search for Zephyr using the Zephyr base environment setting
:envvar:`ZEPHYR_BASE` and then use the normal search paths.
.. _zephyr_cmake_search_order:
Zephyr CMake Package Search Order
*********************************
When Zephyr base environment setting is not used for searching, the Zephyr installation matching
the following criteria will be used:
* A Zephyr repository application will use the Zephyr in which it is located.
For example:
.. code-block:: none
<projects>/zephyr-workspace/zephyr
└── samples
└── hello_world
in this example, ``hello_world`` will use ``<projects>/zephyr-workspace/zephyr``.
* Zephyr workspace application will use the Zephyr that share the same workspace.
For example:
.. code-block:: none
<projects>/zephyr-workspace
├── zephyr
├── ...
└── my_applications
└── my_first_app
in this example, ``my_first_app`` will use ``<projects>/zephyr-workspace/zephyr`` as this Zephyr
is located in the same workspace as the Zephyr workspace application.
.. note::
The root of a Zephyr workspace is identical to ``west topdir`` if the workspace was
installed using ``west``
* Zephyr freestanding application will use the Zephyr registered in the CMake user package registry.
For example:
.. code-block:: none
<projects>/zephyr-workspace-1
└── zephyr (Not exported to CMake)
<projects>/zephyr-workspace-2
└── zephyr (Exported to CMake)
<home>/app
├── CMakeLists.txt
├── prj.conf
└── src
└── main.c
in this example, only ``<projects>/zephyr-workspace-2/zephyr`` is exported to the CMake package
registry and therefore this Zephyr will be used by the Zephyr freestanding application
``<home>/app``.
If user wants to test the application with ``<projects>/zephyr-workspace-1/zephyr``, this can be
done by using the Zephyr Base environment setting, meaning set
``ZEPHYR_BASE=<projects>/zephyr-workspace-1/zephyr``, before
running CMake.
.. note::
The Zephyr package selected on the first CMake invocation will be used for all subsequent
builds. To change the Zephyr package, for example to test the application using Zephyr base
environment setting, then it is necessary to do a pristine build first
(See :ref:`application_rebuild`).
Zephyr CMake Package Version
****************************
When writing an application then it is possible to specify a Zephyr version number ``x.y.z`` that
must be used in order to build the application.
Specifying a version is especially useful for a Zephyr freestanding application as it ensures the
application is built with a minimal Zephyr version.
It also helps CMake to select the correct Zephyr to use for building, when there are multiple
Zephyr installations in the system.
For example:
.. code-block:: cmake
find_package(Zephyr 2.2.0)
project(app)
will require ``app`` to be built with Zephyr 2.2.0 as minimum.
CMake will search all exported candidates to find a Zephyr installation which matches this version
criteria.
Thus it is possible to have multiple Zephyr installations and have CMake automatically select
between them based on the version number provided, see `CMake package version`_ for details.
For example:
.. code-block:: none
<projects>/zephyr-workspace-2.a
└── zephyr (Exported to CMake)
<projects>/zephyr-workspace-2.b
└── zephyr (Exported to CMake)
<home>/app
├── CMakeLists.txt
├── prj.conf
└── src
└── main.c
in this case, there are two released versions of Zephyr installed at their own workspaces.
Workspace 2.a and 2.b, corresponding to the Zephyr version.
To ensure ``app`` is built with minimum version ``2.a`` the following ``find_package``
syntax may be used:
.. code-block:: cmake
find_package(Zephyr 2.a)
project(app)
Note that both ``2.a`` and ``2.b`` fulfill this requirement.
CMake also supports the keyword ``EXACT``, to ensure an exact version is used, if that is required.
In this case, the application CMakeLists.txt could be written as:
.. code-block:: cmake
find_package(Zephyr 2.a EXACT)
project(app)
In case no Zephyr is found which satisfies the version required, as example, the application specifies
.. code-block:: cmake
find_package(Zephyr 2.z)
project(app)
then an error similar to below will be printed:
.. code-block:: none
Could not find a configuration file for package "Zephyr" that is compatible
with requested version "2.z".
The following configuration files were considered but not accepted:
<projects>/zephyr-workspace-2.a/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake, version: 2.a.0
<projects>/zephyr-workspace-2.b/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake, version: 2.b.0
.. note:: It can also be beneficial to specify a version number for Zephyr repository applications
and Zephyr workspace applications. Specifying a version in those cases ensures the
application will only build if the Zephyr repository or workspace is matching.
This can be useful to avoid accidental builds when only part of a workspace has been
updated.
Multiple Zephyr Installations (Zephyr workspace)
************************************************
Testing out a new Zephyr version, while at the same time keeping the existing Zephyr in the
workspace untouched is sometimes beneficial.
Or having both an upstream Zephyr, Vendor specific, and a custom Zephyr in same workspace.
For example:
.. code-block:: none
<projects>/zephyr-workspace
├── zephyr
├── zephyr-vendor
├── zephyr-custom
├── ...
└── my_applications
└── my_first_app
in this setup, ``find_package(Zephyr)`` has the following order of precedence for selecting
which Zephyr to use:
* Project name: ``zephyr``
* First project, when Zephyr projects are ordered lexicographical, in this case.
* ``zephyr-custom``
* ``zephyr-vendor``
This means that ``my_first_app`` will use ``<projects>/zephyr-workspace/zephyr``.
It is possible to specify a Zephyr preference list in the application.
A Zephyr preference list can be specified as:
.. code-block:: cmake
set(ZEPHYR_PREFER "zephyr-custom" "zephyr-vendor")
find_package(Zephyr)
project(my_first_app)
the ``ZEPHYR_PREFER`` is a list, allowing for multiple Zephyrs.
If a Zephyr is specified in the list, but not found in the system, it is simply ignored and
``find_package(Zephyr)`` will continue to the next candidate.
This allows for temporary creation of a new Zephyr release to be tested, without touching current
Zephyr. When testing is done, the ``zephyr-test`` folder can simply be removed.
Such a CMakeLists.txt could look as:
.. code-block:: cmake
set(ZEPHYR_PREFER "zephyr-test")
find_package(Zephyr)
project(my_first_app)
.. _cmake_build_config_package:
Zephyr Build Configuration CMake packages
*****************************************
There are two Zephyr Build configuration packages which provide control over the build
settings in Zephyr in a more generic way. These packages are:
* **ZephyrBuildConfiguration**: Applies to all Zephyr applications in the workspace
* **ZephyrAppConfiguration**: Applies only to the application you are currently building
They are similar to the per-user :file:`.zephyrrc` file that can be used to set :ref:`env_vars`,
but they set CMake variables instead. They also allow you to automatically share the build
configuration among all users through the project repository. They also allow more advanced use
cases, such as loading of additional CMake boilerplate code.
The Zephyr Build Configuration CMake packages will be loaded in the Zephyr boilerplate code after
initial properties and ``ZEPHYR_BASE`` has been defined, but before CMake code execution. The
ZephyrBuildConfiguration is included first and ZephyrAppConfiguration afterwards. That means the
application-specific package could override the workspace settings, if needed.
This allows the Zephyr Build Configuration CMake packages to setup or extend properties such as:
``DTS_ROOT``, ``BOARD_ROOT``, ``TOOLCHAIN_ROOT`` / other toolchain setup, fixed overlays, and any
other property that can be controlled. It also allows inclusion of additional boilerplate code.
To provide a ZephyrBuildConfiguration or ZephyrAppConfiguration, create
:file:`ZephyrBuildConfig.cmake` and/or :file:`ZephyrAppConfig.cmake` respectively and place them
in the appropriate location. The CMake ``find_package`` mechanism will search for these files with
the steps below. Other default CMake package search paths and hints are disabled and there is no
version checking implemented for these packages. This also means that these packages cannot be
installed in the CMake package registry. The search steps are:
1. If ``ZephyrBuildConfiguration_ROOT``, or ``ZephyrAppConfiguration_ROOT`` respectively, is set,
search within this prefix path. If a matching file is found, execute this file. If no matching
file is found, go to step 2.
2. Search within ``${ZEPHYR_BASE}/../*``, or ``${APPLICATION_SOURCE_DIR}`` respectively. If a
matching file is found, execute this file. If no matching file is found, abort the search.
It is recommended to place the files in the default paths from step 2, but with the
``<PackageName>_ROOT`` variables you have the flexibility to place them anywhere. This is
especially necessary for freestanding applications, for which the default path to
ZephyrBuildConfiguration usually does not work. In this case the ``<PackageName>_ROOT`` variables
can be set on the CMake command line, **before** ``find_package(Zephyr ...)``, as environment
variable or from a CMake cache initialization file with the ``-C`` command line option.
.. note:: The ``<PackageName>_ROOT`` variables, as well as the default paths, are just the prefixes
to the search path. These prefixes get combined with additional path suffixes, which together
form the actual search path. Any combination that honors the
`CMake package search procedure`_ is valid and will work.
If you want to completely disable the search for these packages, you can use the special CMake
``CMAKE_DISABLE_FIND_PACKAGE_<PackageName>`` variable for that. Just set
``CMAKE_DISABLE_FIND_PACKAGE_ZephyrBuildConfiguration`` or
``CMAKE_DISABLE_FIND_PACKAGE_ZephyrAppConfiguration`` to ``TRUE`` to disable the package.
An example folder structure could look like this:
.. code-block:: none
<projects>/zephyr-workspace
├── zephyr
├── ...
├── manifest repo (can be named anything)
│ └── cmake/ZephyrBuildConfig.cmake
├── ...
└── zephyr application
└── share/zephyrapp-package/cmake/ZephyrAppConfig.cmake
A sample :file:`ZephyrBuildConfig.cmake` can be seen below.
.. code-block:: cmake
# ZephyrBuildConfig.cmake sample code
# To ensure final path is absolute and does not contain ../.. in variable.
get_filename_component(APPLICATION_PROJECT_DIR
${CMAKE_CURRENT_LIST_DIR}/../../..
ABSOLUTE
)
# Add this project to list of board roots
list(APPEND BOARD_ROOT ${APPLICATION_PROJECT_DIR})
# Default to GNU Arm Embedded toolchain if no toolchain is set
if(NOT ENV{ZEPHYR_TOOLCHAIN_VARIANT})
set(ZEPHYR_TOOLCHAIN_VARIANT gnuarmemb)
find_program(GNU_ARM_GCC arm-none-eabi-gcc)
if(NOT ${GNU_ARM_GCC} STREQUAL GNU_ARM_GCC-NOTFOUND)
# The toolchain root is located above the path to the compiler.
get_filename_component(GNUARMEMB_TOOLCHAIN_PATH ${GNU_ARM_GCC}/../.. ABSOLUTE)
endif()
endif()
Zephyr CMake package source code
********************************
The Zephyr CMake package source code in
:zephyr_file:`share/zephyr-package/cmake` and
:zephyr_file:`share/zephyrunittest-package/cmake` contains the CMake config
package which is used by the CMake ``find_package`` function.
It also contains code for exporting Zephyr as a CMake config package.
The following is an overview of the files in these directories:
:file:`ZephyrConfigVersion.cmake`
The Zephyr package version file. This file is called by CMake to determine
if this installation fulfils the requirements specified by user when calling
``find_package(Zephyr ...)``. It is also responsible for detection of Zephyr
repository or workspace only installations.
:file:`ZephyrUnittestConfigVersion.cmake`
Same responsibility as ``ZephyrConfigVersion.cmake``, but for unit tests.
Includes ``ZephyrConfigVersion.cmake``.
:file:`ZephyrConfig.cmake`
The Zephyr package file. This file is called by CMake to for the package
meeting which fulfils the requirements specified by user when calling
``find_package(Zephyr ...)``. This file is responsible for sourcing of
boilerplate code.
:file:`ZephyrUnittestConfig.cmake`
Same responsibility as ``ZephyrConfig.cmake``, but for unit tests.
Includes ``ZephyrConfig.cmake``.
:file:`zephyr_package_search.cmake`
Common file used for detection of Zephyr repository and workspace candidates.
Used by ``ZephyrConfigVersion.cmake`` and ``ZephyrConfig.cmake`` for common code.
:file:`zephyr_export.cmake`
See :ref:`zephyr_cmake_package_export`.
.. _CMake package: https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html
.. _CMake user package registry: https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html#user-package-registry
.. _CMake package version: https://cmake.org/cmake/help/latest/command/find_package.html#version-selection
.. _CMake package search procedure: https://cmake.org/cmake/help/latest/command/find_package.html#search-procedure
Part 3
한국어 전문 번역
Zephyr CMake package
Zephyr 기반 application을 만들 때 CMake가 사용할 Zephyr installation을 자동 선택하게 하는 package입니다. Repository application, workspace application, freestanding application 모두 지원하며 application CMakeLists.txt 시작 부분에 find_package(Zephyr)를 씁니다.
먼저 현재 Zephyr installation reference를 CMake user package registry에 export해야 합니다. Linux와 macOS registry는 ~/.cmake/packages/Zephyr, Windows는 HKEY_CURRENT_USER\Software\Kitware\CMake\Packages\Zephyr입니다. 여러 Zephyr installation을 export하면 freestanding app이나 vendor fork를 시험할 때 유용합니다.
Package export
West 설치에서는 west zephyr-export를 권장합니다. West 없이 현재 Zephyr를 export하려면:
cmake -P <PATH-TO-ZEPHYR>/share/zephyr-package/cmake/zephyr_export.cmakeZephyr Unittest package도 export하려면 추가로:
cmake -P <PATH-TO-ZEPHYR>/share/zephyrunittest-package/cmake/zephyr_export.cmakeZEPHYR_BASE 명시
Environment variable의 Zephyr base를 먼저 검색한 뒤 일반 path를 검색하게 합니다.
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})Zephyr package 검색 순서
Repository application
Zephyr repository 내부 application은 자신을 포함한 Zephyr를 사용합니다.
<projects>/zephyr-workspace/zephyr
└── samples
└── hello_worldWorkspace application
같은 workspace의 Zephyr를 사용합니다. West workspace의 root는 west topdir과 같습니다.
<projects>/zephyr-workspace
├── zephyr
├── ...
└── my_applications
└── my_first_appFreestanding application
CMake user package registry에 등록된 Zephyr를 사용합니다.
<projects>/zephyr-workspace-1
└── zephyr (Not exported to CMake)
<projects>/zephyr-workspace-2
└── zephyr (Exported to CMake)
<home>/app
├── CMakeLists.txt
├── prj.conf
└── src
└── main.c예제에서는 export된 workspace-2를 사용합니다. Workspace-1로 시험하려면 CMake 전에 ZEPHYR_BASE를 workspace-1 경로로 설정합니다. 첫 CMake invocation에서 선택한 package는 이후 build에서도 유지되므로 다른 package로 바꾸려면 pristine build가 필요합니다.
Package version 선택
Application이 요구하는 최소 Zephyr version x.y.z를 지정할 수 있습니다. 특히 freestanding application에서 최소 지원 version을 보장하고 여러 installation 중 올바른 후보를 선택하는 데 유용합니다.
find_package(Zephyr 2.2.0)
project(app)2.2.0 이상을 요구하며 export된 후보 전체에서 조건을 만족하는 installation을 찾습니다. Version별 workspace 두 개가 있다고 하겠습니다.
<projects>/zephyr-workspace-2.a
└── zephyr (Exported to CMake)
<projects>/zephyr-workspace-2.b
└── zephyr (Exported to CMake)
<home>/app
├── CMakeLists.txt
├── prj.conf
└── src
└── main.c최소 2.a를 요구합니다.
find_package(Zephyr 2.a)
project(app)2.a와 2.b가 모두 만족합니다. 정확한 version만 허용하려면 EXACT를 사용합니다.
find_package(Zephyr 2.a EXACT)
project(app)존재하지 않는 2.z를 요구하면:
find_package(Zephyr 2.z)
project(app)검토했지만 거부된 candidate와 version을 포함한 error가 출력됩니다.
Could not find a configuration file for package "Zephyr" that is compatible
with requested version "2.z".
The following configuration files were considered but not accepted:
<projects>/zephyr-workspace-2.a/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake, version: 2.a.0
<projects>/zephyr-workspace-2.b/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake, version: 2.b.0Repository·workspace application에도 version을 명시하면 workspace 일부만 update된 상태의 accidental build를 막을 수 있습니다.
Workspace 안의 여러 Zephyr
Upstream, vendor, custom Zephyr를 같은 workspace에 두거나 기존 tree를 유지한 채 새 version을 시험할 수 있습니다.
<projects>/zephyr-workspace
├── zephyr
├── zephyr-vendor
├── zephyr-custom
├── ...
└── my_applications
└── my_first_app기본 우선순위는 project 이름이 정확히 zephyr인 후보, 그 다음 lexicographical order의 첫 Zephyr project입니다. 예제에서는 zephyr이 선택됩니다.
Application에서 선호 목록을 지정할 수 있습니다.
set(ZEPHYR_PREFER "zephyr-custom" "zephyr-vendor")
find_package(Zephyr)
project(my_first_app)목록에 있지만 설치되지 않은 후보는 무시하고 다음 candidate로 넘어갑니다. 임시 test tree도 다음처럼 우선 지정한 뒤 folder를 제거할 수 있습니다.
set(ZEPHYR_PREFER "zephyr-test")
find_package(Zephyr)
project(my_first_app)Build configuration CMake package
- ZephyrBuildConfiguration: Workspace의 모든 Zephyr application에 적용
- ZephyrAppConfiguration: 현재 build하는 application에만 적용
User별 .zephyrrc와 비슷하지만 environment가 아니라 CMake variable을 설정하며 repository를 통해 모든 user가 공유할 수 있고 추가 boilerplate도 load할 수 있습니다.
초기 property와 ZEPHYR_BASE 정의 뒤, 일반 CMake code 실행 전에 load합니다. BuildConfiguration이 먼저이고 AppConfiguration이 나중이므로 application package가 workspace setting을 override할 수 있습니다. DTS_ROOT, BOARD_ROOT, TOOLCHAIN_ROOT, overlay, toolchain과 기타 property를 설정할 수 있습니다.
ZephyrBuildConfig.cmake 또는 ZephyrAppConfig.cmake를 만듭니다. 검색 순서는 다음과 같습니다.
- 각
<PackageName>_ROOT가 있으면 그 prefix에서 검색하고 찾으면 실행 - 없으면 Build package는
${ZEPHYR_BASE}/../*, App package는${APPLICATION_SOURCE_DIR}에서 검색. 없으면 search 종료
Freestanding application은 default BuildConfiguration path가 맞지 않을 수 있어 root variable을 command line, environment, 또는 -C cache initialization file로 find_package(Zephyr) 전에 설정합니다. Root는 search prefix이며 CMake package search suffix와 결합됩니다.
검색을 완전히 끄려면 CMAKE_DISABLE_FIND_PACKAGE_ZephyrBuildConfiguration=TRUE 또는 CMAKE_DISABLE_FIND_PACKAGE_ZephyrAppConfiguration=TRUE를 사용합니다.
예제 folder 구조:
<projects>/zephyr-workspace
├── zephyr
├── ...
├── manifest repo (can be named anything)
│ └── cmake/ZephyrBuildConfig.cmake
├── ...
└── zephyr application
└── share/zephyrapp-package/cmake/ZephyrAppConfig.cmakeZephyrBuildConfig.cmake 예제:
# ZephyrBuildConfig.cmake sample code
# To ensure final path is absolute and does not contain ../.. in variable.
get_filename_component(APPLICATION_PROJECT_DIR
${CMAKE_CURRENT_LIST_DIR}/../../..
ABSOLUTE
)
# Add this project to list of board roots
list(APPEND BOARD_ROOT ${APPLICATION_PROJECT_DIR})
# Default to GNU Arm Embedded toolchain if no toolchain is set
if(NOT ENV{ZEPHYR_TOOLCHAIN_VARIANT})
set(ZEPHYR_TOOLCHAIN_VARIANT gnuarmemb)
find_program(GNU_ARM_GCC arm-none-eabi-gcc)
if(NOT ${GNU_ARM_GCC} STREQUAL GNU_ARM_GCC-NOTFOUND)
# The toolchain root is located above the path to the compiler.
get_filename_component(GNUARMEMB_TOOLCHAIN_PATH ${GNU_ARM_GCC}/../.. ABSOLUTE)
endif()
endif()Package source code
share/zephyr-package/cmake와 share/zephyrunittest-package/cmake의 주요 file은 다음과 같습니다.
ZephyrConfigVersion.cmake: version requirement와 repository/workspace-only installation 탐지ZephyrUnittestConfigVersion.cmake: Unit test용 version file이며 ZephyrConfigVersion includeZephyrConfig.cmake: 조건을 만족한 package와 boilerplate sourceZephyrUnittestConfig.cmake: Unit test용이며 ZephyrConfig includezephyr_package_search.cmake: Repository·workspace candidate 탐지 공통 codezephyr_export.cmake: Package registry export
Source
출처
영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.