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>
This commit is contained in:
Torsten Rasmussen 2022-08-05 11:09:26 +02:00 committed by Carles Cufí
commit 7a65bdbd64
11 changed files with 85 additions and 18 deletions

View file

@ -26,8 +26,10 @@
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.
list(APPEND ARCH_ROOT ${ZEPHYR_BASE})
# 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)

View file

@ -52,8 +52,10 @@ include(extensions)
zephyr_check_cache(BOARD REQUIRED)
# 'BOARD_ROOT' is a prioritized list of directories where boards may
# be found. It always includes ${ZEPHYR_BASE} at the lowest priority.
list(APPEND BOARD_ROOT ${ZEPHYR_BASE})
# be found. It always includes ${ZEPHYR_BASE} at the lowest priority (except for unittesting).
if(NOT unittest IN_LIST Zephyr_FIND_COMPONENTS)
list(APPEND BOARD_ROOT ${ZEPHYR_BASE})
endif()
string(FIND "${BOARD}" "@" REVISION_SEPARATOR_INDEX)
if(NOT (REVISION_SEPARATOR_INDEX EQUAL -1))

View file

@ -32,3 +32,10 @@ zephyr_file(APPLICATION_ROOT SOC_ROOT)
# Convert paths to absolute, relative from APPLICATION_SOURCE_DIR
zephyr_file(APPLICATION_ROOT ARCH_ROOT)
if(unittest IN_LIST Zephyr_FIND_COMPONENTS)
# Zephyr used in unittest mode, use dedicated unittest root.
set(BOARD_ROOT ${ZEPHYR_BASE}/subsys/testsuite)
set(ARCH_ROOT ${ZEPHYR_BASE}/subsys/testsuite)
set(SOC_ROOT ${ZEPHYR_BASE}/subsys/testsuite)
endif()

View file

@ -4,6 +4,12 @@ cmake_minimum_required(VERSION 3.20.0)
enable_language(C CXX ASM)
include(root)
include(boards)
include(arch)
include(configuration_files)
include(kconfig)
# Parameters:
# SOURCES: list of source files, default main.c
# INCLUDE: list of additional include paths relative to ZEPHYR_BASE
@ -55,6 +61,7 @@ endif(M64_MODE)
endif()
target_compile_options(testbinary PRIVATE
-imacros ${AUTOCONF_H}
-Wall
-I ${KOBJ_GEN_DIR}
${EXTRA_CPPFLAGS_AS_LIST}
@ -89,7 +96,6 @@ if(LIBS)
endif()
if(CONFIG_ZTEST_NEW_API)
add_definitions( -DCONFIG_ZTEST_NEW_API=y )
target_sources(testbinary PRIVATE
${ZEPHYR_BASE}/subsys/testsuite/ztest/src/ztest_new.c
${ZEPHYR_BASE}/subsys/testsuite/ztest/src/ztest_mock.c
@ -115,7 +121,7 @@ if(VALGRIND_PROGRAM)
set(VALGRIND_FLAGS
--leak-check=full
--error-exitcode=1
--log-file=valgrind.log
--log-file=valgrind.log
)
endif()

View file

@ -0,0 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
#
# Copyright (c) 2022 Nordic Semiconductor
# Intentionally left empty

View file

@ -0,0 +1,24 @@
# Copyright (c) 2022 Nordic Semiconductor
#
# SPDX-License-Identifier: Apache-2.0
# Current the use of X86 is for consistency with old testsuite/ztest which
# defined CONFIG_X86 manually. To consider, is NATIVE_POSIX a better choice?
config X86
bool
default y
help
The unit_testing architecture identifies itself as X86 for basic
ztest and kernel support.
if CONSOLE
config NATIVE_POSIX_CONSOLE
bool
default y
select CONSOLE_HAS_DRIVER
help
The unit testing architecture is expected to always have access to a
standard terminal for printing.
endif # CONSOLE

View file

@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0
#
# Copyright (c) 2022 Nordic Semiconductor
config BOARD_UNIT_TESTING
bool "Unit testing board"
help
Board for unit testing

View file

@ -0,0 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
#
# Copyright (c) 2022 Nordic Semiconductor
# Intentionally left empty

View file

@ -0,0 +1,12 @@
# SPDX-License-Identifier: Apache-2.0
#
# Copyright (c) 2022 Nordic Semiconductor
CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC=1000000
CONFIG_CONSOLE=y
CONFIG_ZTEST=y
CONFIG_ZTEST_MOCKING=y
# unit testing only build a fraction of code, and hence assert may fail linking.
# Disable default for board. A unit test can still enable asserts.
CONFIG_ASSERT=n

View file

@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0
#
# Copyright (c) 2022 Nordic Semiconductor
config SOC_UNIT_TESTING
bool "Unit testing SoC"
help
SoC for unit testing

View file

@ -29,20 +29,8 @@
#endif
#ifndef KERNEL
#define CONFIG_STDOUT_CONSOLE 1
#define CONFIG_ZTEST_ASSERT_VERBOSE 1
#define CONFIG_ZTEST_MOCKING
#define CONFIG_NUM_COOP_PRIORITIES 16
#define CONFIG_COOP_ENABLED 1
#define CONFIG_PREEMPT_ENABLED 1
#define CONFIG_MP_NUM_CPUS 1
#define CONFIG_SYS_CLOCK_TICKS_PER_SEC 100
#define CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC 10000000
#define CONFIG_SYS_CLOCK_MAX_TIMEOUT_DAYS 365
#define ARCH_STACK_PTR_ALIGN 8
/* FIXME: Properly integrate with Zephyr's arch specific code */
#define CONFIG_X86 1
#define CONFIG_PRINTK 1
#ifdef __cplusplus
extern "C" {
#endif