Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake: tfm: support for custom CMake args when building TF-M #34868

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions modules/trusted-firmware-m/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ set(TFM_VALID_PARTITIONS
# TF-M regression tests
# BL2: Boolean if the TF-M build uses MCUboot. Default: True
# ENABLED_PARTITIONS: List of TFM partitions to enable.
# CMAKE_ARGS: Additional CMake flags to be used when building TF-M
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tejlmand @erwango would this be better to be called CMAKE_ADDITIONAL_ARGS, or CMAKE_CUSTOM_ARGS, to reflect better what it is used for?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, this is the description of the function: trusted_firmware_build()

So from the point of view of that function, it's just CMake args.

You could ideally use that function from other locations.

But when called from the internal Zephyr build here: https://github.com/ioannisg/zephyr/blob/b6678723a282a99e62aebbc62570e37748c03df9/modules/trusted-firmware-m/CMakeLists.txt#L321

    CMAKE_ARGS $<GENEX_EVAL:$<TARGET_PROPERTY:zephyr_property_target,TFM_CMAKE_OPTIONS>>

you may consider it custom args.

# This is a list of flags, such as
# `CMAKE_ARGS -DARG0=val0 -DARG1=val1`
# or a generator expression, such as:
# `CMAKE_ARGS $<TARGET_PROPERTY:target,property>
#
# Example usage:
#
Expand Down Expand Up @@ -160,30 +165,44 @@ function(trusted_firmware_build)
message(FATAL_ERROR "Unsupported ZEPHYR_TOOLCHAIN_VARIANT: ${ZEPHYR_TOOLCHAIN_VARIANT}")
endif()

file(MAKE_DIRECTORY ${TFM_BINARY_DIR})
add_custom_target(tfm_cmake
COMMAND ${CMAKE_COMMAND}
-G${CMAKE_GENERATOR}
-DTFM_TOOLCHAIN_FILE=${ZEPHYR_TRUSTED_FIRMWARE_M_MODULE_DIR}/${TFM_TOOLCHAIN_FILE}
-DTFM_PLATFORM=${TFM_BOARD}
-DCROSS_COMPILE=${TFM_TOOLCHAIN_PATH}/${TFM_TOOLCHAIN_PREFIX}
${TFM_CMAKE_BUILD_TYPE_ARG}
-DBL2=${TFM_BL2}
${TFM_IPC_ARG}
${TFM_ISOLATION_LEVEL_ARG}
${TFM_REGRESSION_S_ARG}
${TFM_REGRESSION_NS_ARG}
${TFM_PROFILE_ARG}
${MCUBOOT_IMAGE_NUM_ARG}
${PSA_TEST_ARG}
${TFM_CMAKE_ARGS}
-DTFM_TEST_REPO_PATH=${ZEPHYR_TRUSTED_FIRMWARE_M_MODULE_DIR}/tf-m-tests
-DMCUBOOT_PATH=${ZEPHYR_TRUSTED_FIRMWARE_M_MODULE_DIR}/../tfm-mcuboot
-DPSA_ARCH_TESTS_PATH=${ZEPHYR_TRUSTED_FIRMWARE_M_MODULE_DIR}/psa-arch-tests
${TFM_PARTITIONS_ARGS}
${ZEPHYR_TRUSTED_FIRMWARE_M_MODULE_DIR}/trusted-firmware-m
WORKING_DIRECTORY ${TFM_BINARY_DIR}
COMMAND_EXPAND_LISTS
)

include(ExternalProject)

ExternalProject_Add(
tfm
SOURCE_DIR ${ZEPHYR_TRUSTED_FIRMWARE_M_MODULE_DIR}/trusted-firmware-m
BINARY_DIR ${TFM_BINARY_DIR}
CMAKE_ARGS -DTFM_TOOLCHAIN_FILE=${ZEPHYR_TRUSTED_FIRMWARE_M_MODULE_DIR}/${TFM_TOOLCHAIN_FILE}
-DTFM_PLATFORM=${TFM_BOARD}
-DCROSS_COMPILE=${TFM_TOOLCHAIN_PATH}/${TFM_TOOLCHAIN_PREFIX}
${TFM_CMAKE_BUILD_TYPE_ARG}
-DBL2=${TFM_BL2}
${TFM_IPC_ARG}
${TFM_ISOLATION_LEVEL_ARG}
${TFM_REGRESSION_S_ARG}
${TFM_REGRESSION_NS_ARG}
${TFM_PROFILE_ARG}
${MCUBOOT_IMAGE_NUM_ARG}
${PSA_TEST_ARG}
-DTFM_TEST_REPO_PATH=${ZEPHYR_TRUSTED_FIRMWARE_M_MODULE_DIR}/tf-m-tests
-DMCUBOOT_PATH=${ZEPHYR_TRUSTED_FIRMWARE_M_MODULE_DIR}/../tfm-mcuboot
-DPSA_ARCH_TESTS_PATH=${ZEPHYR_TRUSTED_FIRMWARE_M_MODULE_DIR}/psa-arch-tests
${TFM_PARTITIONS_ARGS}
CONFIGURE_COMMAND ""
BUILD_COMMAND ${CMAKE_COMMAND} --build .
INSTALL_COMMAND ${CMAKE_COMMAND} --install .
BUILD_ALWAYS True
USES_TERMINAL_BUILD True
DEPENDS tfm_cmake
BUILD_BYPRODUCTS ${BUILD_BYPRODUCTS}
)

Expand Down Expand Up @@ -299,6 +318,7 @@ if (CONFIG_BUILD_WITH_TFM)
${TFM_IPC_ARG}
${TFM_REGRESSION_S_ARG}
${TFM_REGRESSION_NS_ARG}
CMAKE_ARGS $<GENEX_EVAL:$<TARGET_PROPERTY:zephyr_property_target,TFM_CMAKE_OPTIONS>>
ENABLED_PARTITIONS ${TFM_ENABLED_PARTITIONS_ARG}
${TFM_PSA_TEST_ARG}
CMAKE_BUILD_TYPE ${TFM_CMAKE_BUILD_TYPE}
Expand Down
27 changes: 27 additions & 0 deletions samples/tfm_integration/tfm_integration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,30 @@ contains all the binaries, bl2, tfm_s, and the zephyr app.

.. _PSA Certified Level 1:
https://www.psacertified.org/security-certification/psa-certified-level-1/

Custom CMake arguments
======================

When building a Zephyr application with TF-M it might be necessary to control
the CMake arguments passed to the TF-M build.

Zephyr TF-M build offers several Kconfig options for controlling the build, but
doesn't cover every CMake argument supported by the TF-M build system.

The ``TFM_CMAKE_OPTIONS`` property on the ``zephyr_property_target`` can be used
to pass custom CMake arguments to the TF-M build system.

To pass the CMake argument ``-DFOO=bar`` to the TF-M build system, place the
following CMake snippet in your CMakeLists.txt file.

.. code-block:: cmake

set_property(TARGET zephyr_property_target
APPEND PROPERTY TFM_CMAKE_OPTIONS
-DFOO=bar
)

.. note::
The ``TFM_CMAKE_OPTIONS`` is a list so it is possible to append multiple
options. Also CMake generator expressions are supported, such as
``$<1:-DFOO=bar>``