build/snippets/writing.html · build/snippets/writing.rst 공식 원문 ↗

Zephyr 3.7.0 · Build · Snippets

Snippet 작성법

YAML 하나에 overlay·Kconfig·DTS flag를 묶고 board별로 특화합니다.

Official pathbuild/snippets/writing.html
Source filebuild/snippets/writing.rst
Source versionZephyr 3.7.0
TranslationFull · reviewed

본문 기준: Zephyr 3.7.0 LTS3 · 이후 유지보수 패치는 별도입니다.

Part 1

요약·해설

Namespace, SNIPPET_ROOT, module discovery, override 순서와 regular expression board match를 설명합니다.

File

snippet.yml

Discover

SNIPPET_ROOT

Order

Last wins

Board

Name · regex

Part 2

접을 수 있는 영어 원문 전체

영어 원문 전체 펼치기
원문 SHA-256 582359B074F9E8A9AFC3525749400E53C3391E400CD5FA4197CB14572EF7968C
Writing Snippets
################

.. contents::
   :local:

Basics
******

Snippets are defined using YAML files named :file:`snippet.yml`.

A :file:`snippet.yml` file contains the name of the snippet, along with
additional build system settings, like this:

.. code-block:: yaml

   name: snippet-name
   # ... build system settings go here ...

Build system settings go in other keys in the file as described later on in
this page.

You can combine settings whenever they appear under the same keys. For example,
you can combine a snippet-specific devicetree overlay and a ``.conf`` file like
this:

.. code-block:: yaml

   name: foo
   append:
     EXTRA_DTC_OVERLAY_FILE: foo.overlay
     EXTRA_CONF_FILE: foo.conf

Namespacing
***********

When writing devicetree overlays in a snippet, use ``snippet_<name>`` or
``snippet-<name>`` as a namespace prefix when choosing names for node labels,
node names, etc. This avoids namespace conflicts.

For example, if your snippet is named ``foo-bar``, write your devicetree
overlays like this:

.. code-block:: DTS

   chosen {
           zephyr,baz = &snippet_foo_bar_dev;
   };

   snippet_foo_bar_dev: device@12345678 {
           /* ... */
   };

Where snippets are located
**************************

The build system looks for snippets in these places:

#. In directories configured by the :makevar:`SNIPPET_ROOT` CMake variable.
   This always includes the zephyr repository (so
   :zephyr_file:`snippets/` is always a source of snippets) and the
   application source directory (so :file:`<app>/snippets` is also).

   Additional directories can be added manually at CMake time.

   The variable is a whitespace- or semicolon-separated list of directories
   which may contain snippet definitions.

   For each directory in the list, the build system looks for
   :file:`snippet.yml` files underneath a subdirectory named :file:`snippets/`,
   if one exists.

   For example, if :makevar:`SNIPPET_ROOT` is set to ``/foo;/bar``, the build
   system will look for :file:`snippet.yml` files underneath the following
   subdirectories:

   - :file:`/foo/snippets/`
   - :file:`/bar/snippets/`

   The :file:`snippet.yml` files can be nested anywhere underneath these
   locations.

#. In any :ref:`module <modules>` whose :file:`module.yml` file provides a
   ``snippet_root`` setting.

   For example, in a zephyr module named ``baz``, you can add this to your
   :file:`module.yml` file:

   .. code-block:: yaml

      settings:
        snippet_root: .

   And then any :file:`snippet.yml` files in ``baz/snippets`` will
   automatically be discovered by the build system, just as if
   the path to ``baz`` had appeared in :makevar:`SNIPPET_ROOT`.

Processing order
****************

Snippets are processed in the order they are listed in the :makevar:`SNIPPET`
variable, or in the order of the ``-S`` arguments if using west.

To apply ``bar`` after ``foo``:

.. code-block:: console

   cmake -Sapp -Bbuild -DSNIPPET="foo;bar" [...]
   cmake --build build

The same can be achieved with west as follows:

.. code-block:: console

   west build -S foo -S bar [...] app

When multiple snippets set the same configuration, the configuration value set
by the last processed snippet ends up in the final configurations.

For instance, if ``foo`` sets ``CONFIG_FOO=1`` and ``bar`` sets
``CONFIG_FOO=2`` in the above example, the resulting final configuration will
be ``CONFIG_FOO=2`` because ``bar`` is processed after ``foo``.

This principle applies to both Kconfig fragments (``.conf`` files) and
devicetree overlays (``.overlay`` files).

.. _snippets-devicetree-overlays:

Devicetree overlays (``.overlay``)
**********************************

This :file:`snippet.yml` adds :file:`foo.overlay` to the build:

.. code-block:: yaml

   name: foo
   append:
     EXTRA_DTC_OVERLAY_FILE: foo.overlay

The path to :file:`foo.overlay` is relative to the directory containing
:file:`snippet.yml`.

.. _snippets-conf-files:

``.conf`` files
***************

This :file:`snippet.yml` adds :file:`foo.conf` to the build:

.. code-block:: yaml

   name: foo
   append:
     EXTRA_CONF_FILE: foo.conf

The path to :file:`foo.conf` is relative to the directory containing
:file:`snippet.yml`.

``DTS_EXTRA_CPPFLAGS``
**********************

This :file:`snippet.yml` adds ``DTS_EXTRA_CPPFLAGS`` CMake Cache variables
to the build:

.. code-block:: yaml

   name: foo
   append:
     DTS_EXTRA_CPPFLAGS: -DMY_DTS_CONFIGURE

Adding these flags enables control over the content of a devicetree file.

Board-specific settings
***********************

You can write settings that only apply to some boards.

The settings described here are applied in **addition** to snippet settings
that apply to all boards. (This is similar, for example, to the way that an
application with both :file:`prj.conf` and :file:`boards/foo.conf` files will
use both ``.conf`` files in the build when building for board ``foo``, instead
of just :file:`boards/foo.conf`)

By name
=======

.. code-block:: yaml

   name: ...
   boards:
     bar: # settings for board "bar" go here
       append:
         EXTRA_DTC_OVERLAY_FILE: bar.overlay
     baz: # settings for board "baz" go here
       append:
         EXTRA_DTC_OVERLAY_FILE: baz.overlay

The above example uses :file:`bar.overlay` when building for board ``bar``, and
:file:`baz.overlay` when building for ``baz``.

By regular expression
=====================

You can enclose the board name in slashes (``/``) to match the name against a
regular expression in the `CMake syntax`_. The regular expression must match
the entire board name.

.. _CMake syntax:
   https://cmake.org/cmake/help/latest/command/string.html#regex-specification

For example:

.. code-block:: yaml

   name: foo
   boards:
     /my_vendor_.*/:
       append:
         EXTRA_DTC_OVERLAY_FILE: my_vendor.overlay

The above example uses devicetree overlay :file:`my_vendor.overlay` when
building for either board ``my_vendor_board1`` or ``my_vendor_board2``. It
would not use the overlay when building for either ``another_vendor_board`` or
``x_my_vendor_board``.

Part 3

한국어 전문 번역

Snippet 작성 기본

Snippet은 snippet.yml이라는 YAML file로 정의합니다. File에는 snippet 이름과 build system 설정이 들어갑니다.

name: snippet-name
# ... build system settings go here ...

Build setting은 이 문서에서 설명하는 다른 key에 둡니다. 같은 key 아래의 setting은 결합할 수 있습니다. 예를 들어 snippet 전용 Devicetree overlay와 .conf를 함께 적용합니다.

name: foo
append:
  EXTRA_DTC_OVERLAY_FILE: foo.overlay
  EXTRA_CONF_FILE: foo.conf

Namespace

Snippet overlay에서 node label·node name 등을 정할 때 snippet_<name> 또는 snippet-<name> prefix를 사용해 namespace 충돌을 피합니다. 이름이 foo-bar인 snippet은 다음처럼 씁니다.

chosen {
        zephyr,baz = &snippet_foo_bar_dev;
};

snippet_foo_bar_dev: device@12345678 {
        /* ... */
};

Snippet 검색 위치

Build system은 다음 위치에서 snippet을 찾습니다.

  1. CMake SNIPPET_ROOT variable에 설정한 directory. Zephyr repository와 application source directory는 항상 포함되므로 zephyr/snippets와 <app>/snippets가 기본 source입니다.
  2. CMake 실행 때 directory를 추가할 수 있습니다. SNIPPET_ROOT는 whitespace 또는 semicolon으로 구분한 directory 목록이며, 각 root 아래의 snippets/ 안에서 깊이에 관계없이 snippet.yml을 찾습니다. /foo;/bar라면 /foo/snippets/와 /bar/snippets/를 검색합니다.
  3. Zephyr module의 module.yml이 snippet_root를 제공하는 경우. Module baz의 예는 다음과 같습니다.
settings:
  snippet_root: .

그러면 baz/snippets 아래의 snippet.yml은 baz 경로가 SNIPPET_ROOT에 있었던 것처럼 자동 발견됩니다.

처리 순서

Snippet은 CMake SNIPPET variable에 나열된 순서 또는 west의 -S argument 순서로 처리합니다. Foo 뒤에 bar를 적용하는 CMake 명령은 다음과 같습니다.

cmake -Sapp -Bbuild -DSNIPPET="foo;bar" [...]
cmake --build build

West에서는 같은 순서를 다음처럼 표현합니다.

west build -S foo -S bar [...] app

여러 snippet이 같은 configuration을 설정하면 마지막으로 처리한 값이 최종 결과에 남습니다. Foo가 CONFIG_FOO=1, bar가 CONFIG_FOO=2라면 최종 값은 2입니다. 이 원칙은 Kconfig fragment와 Devicetree overlay 모두에 적용됩니다.

Devicetree overlay 추가

name: foo
append:
  EXTRA_DTC_OVERLAY_FILE: foo.overlay

foo.overlay 경로는 snippet.yml이 있는 directory 기준입니다.

.conf file 추가

name: foo
append:
  EXTRA_CONF_FILE: foo.conf

foo.conf도 snippet.yml directory 기준 상대 경로입니다.

DTS_EXTRA_CPPFLAGS

다음은 CMake cache variable에 Devicetree preprocessor flag를 추가해 DTS content를 제어합니다.

name: foo
append:
  DTS_EXTRA_CPPFLAGS: -DMY_DTS_CONFIGURE

Board별 설정

특정 board에만 적용되는 setting도 작성할 수 있습니다. 이 setting은 모든 board에 공통인 snippet setting을 대체하지 않고 추가됩니다. Application이 prj.conf와 boards/foo.conf를 함께 쓰는 것과 같습니다.

Board 이름으로 지정

name: ...
boards:
  bar: # settings for board "bar" go here
    append:
      EXTRA_DTC_OVERLAY_FILE: bar.overlay
  baz: # settings for board "baz" go here
    append:
      EXTRA_DTC_OVERLAY_FILE: baz.overlay

Board bar에는 bar.overlay, baz에는 baz.overlay를 사용합니다.

Regular expression으로 지정

Board 이름을 slash로 감싸 CMake syntax의 regular expression과 전체 이름을 match할 수 있습니다.

name: foo
boards:
  /my_vendor_.*/:
    append:
      EXTRA_DTC_OVERLAY_FILE: my_vendor.overlay

my_vendor_board1과 my_vendor_board2에는 overlay를 적용하지만 another_vendor_board와 x_my_vendor_board에는 적용하지 않습니다.

Source

출처

영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.