zephyr/cmake/modules/arch.cmake
Torsten Rasmussen 7a65bdbd64 cmake: kconfig: introduce dedicated unit testing board
This commit introduces a dedicated unit testing board.

Today, a dedicated Zephyr unit testing scheme exists but is different
from how a Zephyr build generally works.

For example Kconfig is not possible, resulting on various different
hacks to pass Kconfig settings from test cases / testcase.yaml through
CMake to the code.
Some directly as compile definitions, some as header files with forced
inclusion on sources, some with wrapper flags which again results in
different define being enabled. There is even cases where a second
forced header inclusion undefines previous defines.

Unit test often does a manual check for the right boards, like this:
> if (NOT BOARD STREQUAL unit_testing)
>    message(FATAL_ERROR "This project can only be used with...")
> endif()

Introducing a dedicated unit_testing board under `tests/root` allows
us to use Kconfig in unit test samples, and thus proper `prj.conf` and
extra Kconfig fragments.
Generation of autoconf.h so the overall architecture follows regular
Zephyr builds.

Proper and uniform error messages when invalid board is selected.

The unit_testing board and arch is located under: `subsys/testsuite` so
that it is only available when find_package(Zephyr COMPONENTS unittest)
is used, and not available for regular Zephyr builds.

Kconfig generates autoconf.h which is applied as compile flag to
test binary which means that kconfig defines placed in ztest.h can now
be removed.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
2022-08-18 14:29:14 +02:00

49 lines
1.6 KiB
CMake

# SPDX-License-Identifier: Apache-2.0
#
# Copyright (c) 2022, Nordic Semiconductor ASA
# Configure ARCH settings based on board directory and arch root.
#
# This CMake module will set the following variables in the build system based
# on board directory and arch root.
#
# If no implementation is available for the current arch an error will be raised.
#
# Outcome:
# The following variables will be defined when this CMake module completes:
#
# - ARCH: Name of the arch in use.
# - ARCH_DIR: Directory containing the arch implementation.
# - ARCH_ROOT: ARCH_ROOT with ZEPHYR_BASE appended
#
# Variable dependencies:
# - ARCH_ROOT: CMake list of arch roots containing arch implementations
# - BOARD_DIR: CMake variable specifying the directory of the selected BOARD
#
# Variables set by this module and not mentioned above are considered internal
# use only and may be removed, renamed, or re-purposed without prior notice.
include_guard(GLOBAL)
# 'ARCH_ROOT' is a prioritized list of directories where archs may be
# found. It always includes ${ZEPHYR_BASE} at the lowest priority (except for unittesting).
if(NOT unittest IN_LIST Zephyr_FIND_COMPONENTS)
list(APPEND ARCH_ROOT ${ZEPHYR_BASE})
endif()
cmake_path(GET BOARD_DIR PARENT_PATH board_arch_dir)
cmake_path(GET board_arch_dir FILENAME ARCH)
foreach(root ${ARCH_ROOT})
if(EXISTS ${root}/arch/${ARCH}/CMakeLists.txt)
set(ARCH_DIR ${root}/arch)
break()
endif()
endforeach()
if(NOT ARCH_DIR)
message(FATAL_ERROR "Could not find ARCH=${ARCH} for BOARD=${BOARD}, \
please check your installation. ARCH roots searched: \n\
${ARCH_ROOT}")
endif()