Zephyr×Rustのインテグレーションにチャレンジ!②~アプリとして組み込みたかった~

はじめに

Rustプロジェクトを直接、Zephyrアプリケーションとしてインテグレーションしたいです。

が、私には無理でした!

app boilerplate

Zephyrのアプリケーション作成では、boilerplateの利用がおすすめされています。

|> zephyr/cmake/app/boilerplate.cmake

# This file must be included into the toplevel CMakeLists.txt file of
# Zephyr applications, e.g. zephyr/samples/hello_world/CMakeLists.txt
# must start with the line:
#
# include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
#
# It exists to reduce boilerplate code that Zephyr expects to be in
# application CMakeLists.txt code.
#
# Omitting it is permitted, but doing so incurs a maintenance cost as
# the application must manage upstream changes to this file.

# app is a CMake library containing all the application code and is
# modified by the entry point ${APPLICATION_SOURCE_DIR}/CMakeLists.txt
# that was specified when cmake was called.

appは、CMakeのライブラリであれば良いようです。アプリケーションを作成する際のCMakeエントリーポイントで修正をしても良い、とあります。 Rustのプロジェクトにごっそり入れ替えてあげればなんとかなるのかな?と考えながら色々と触ってみます。

# "app" is a CMake library containing all the application code and is
# modified by the entry point ${APPLICATION_SOURCE_DIR}/CMakeLists.txt
# that was specified when cmake was called.
zephyr_library_named(app)
set_property(TARGET app PROPERTY ARCHIVE_OUTPUT_DIRECTORY app)

Zephyrプロジェクトの用意しているボイラープレートでは、C言語ソースを指定することを前提としているようです。 下記のように、target_sourcesをコメントアウトすると、cmakeコマンドが失敗します。

#target_sources(app PRIVATE src/main.c)

エラーは次の通りで、zephyr_library_namedというマクロの呼び出しで失敗しています。

-- Configuring done
CMake Error at /home/tomoyuki/work/05.nrf52840/zephyr/cmake/extensions.cmake:357 (add_library):
  No SOURCES given to target: app
Call Stack (most recent call first):
  /home/tomoyuki/work/05.nrf52840/zephyr/cmake/app/boilerplate.cmake:347 (zephyr_library_named)
  CMakeLists.txt:3 (include)

このマクロは次のように定義されています。

# Constructor with an explicitly given name.
macro(zephyr_library_named name)
  # This is a macro because we need add_library() to be executed
  # within the scope of the caller.
  set(ZEPHYR_CURRENT_LIBRARY ${name})
  add_library(${name} STATIC "")

  zephyr_append_cmake_library(${name})

  target_link_libraries(${name} PUBLIC zephyr_interface)
endmacro()

このくらいなら、なんとかなる気がしなくもないです。

appライブラリをIMPORTEDに変更できれば、良い気がしましたが、結局ダメでした。

cmake.org

cmake.org

Read-only indication of whether a target is IMPORTED.

The boolean value of this property is True for targets created with the IMPORTED option to add_executable() or add_library(). It is False for targets built within the project.

ぐへぇ。

この後、しばらくボイラープレートと格闘していましたが、今の私には太刀打ちできませんでした…。

Zephyr cmakeのいたるところが、appソースコードがあることを前提に書かれているようで、どうにもなりませんでした…。