From 7a65bdbd64df68d0c1fcc49db8aa1493b0dc22a0 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Fri, 5 Aug 2022 11:09:26 +0200 Subject: [PATCH] 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 --- cmake/modules/arch.cmake | 6 +++-- cmake/modules/boards.cmake | 6 +++-- cmake/modules/root.cmake | 7 ++++++ cmake/modules/unittest.cmake | 10 ++++++-- .../arch/unit_testing/CMakeLists.txt | 5 ++++ subsys/testsuite/arch/unit_testing/Kconfig | 24 +++++++++++++++++++ .../unit_testing/unit_testing/Kconfig.board | 8 +++++++ .../unit_testing/Kconfig.defconfig | 5 ++++ .../unit_testing/unit_testing_defconfig | 12 ++++++++++ .../soc/unit_testing/unit_testing/Kconfig.soc | 8 +++++++ subsys/testsuite/ztest/include/zephyr/ztest.h | 12 ---------- 11 files changed, 85 insertions(+), 18 deletions(-) create mode 100644 subsys/testsuite/arch/unit_testing/CMakeLists.txt create mode 100644 subsys/testsuite/arch/unit_testing/Kconfig create mode 100644 subsys/testsuite/boards/unit_testing/unit_testing/Kconfig.board create mode 100644 subsys/testsuite/boards/unit_testing/unit_testing/Kconfig.defconfig create mode 100644 subsys/testsuite/boards/unit_testing/unit_testing/unit_testing_defconfig create mode 100644 subsys/testsuite/soc/unit_testing/unit_testing/Kconfig.soc diff --git a/cmake/modules/arch.cmake b/cmake/modules/arch.cmake index bd6d6cab87f..806b5c12b58 100644 --- a/cmake/modules/arch.cmake +++ b/cmake/modules/arch.cmake @@ -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) diff --git a/cmake/modules/boards.cmake b/cmake/modules/boards.cmake index 5032b579f55..371b6e479dc 100644 --- a/cmake/modules/boards.cmake +++ b/cmake/modules/boards.cmake @@ -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)) diff --git a/cmake/modules/root.cmake b/cmake/modules/root.cmake index 3800d812645..24ff3884cef 100644 --- a/cmake/modules/root.cmake +++ b/cmake/modules/root.cmake @@ -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() diff --git a/cmake/modules/unittest.cmake b/cmake/modules/unittest.cmake index c2d97cebbb4..1b33478c2e2 100644 --- a/cmake/modules/unittest.cmake +++ b/cmake/modules/unittest.cmake @@ -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() diff --git a/subsys/testsuite/arch/unit_testing/CMakeLists.txt b/subsys/testsuite/arch/unit_testing/CMakeLists.txt new file mode 100644 index 00000000000..b2efdb2bee7 --- /dev/null +++ b/subsys/testsuite/arch/unit_testing/CMakeLists.txt @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: Apache-2.0 +# +# Copyright (c) 2022 Nordic Semiconductor + +# Intentionally left empty diff --git a/subsys/testsuite/arch/unit_testing/Kconfig b/subsys/testsuite/arch/unit_testing/Kconfig new file mode 100644 index 00000000000..c2fdd2e2744 --- /dev/null +++ b/subsys/testsuite/arch/unit_testing/Kconfig @@ -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 diff --git a/subsys/testsuite/boards/unit_testing/unit_testing/Kconfig.board b/subsys/testsuite/boards/unit_testing/unit_testing/Kconfig.board new file mode 100644 index 00000000000..88c43c26dc8 --- /dev/null +++ b/subsys/testsuite/boards/unit_testing/unit_testing/Kconfig.board @@ -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 diff --git a/subsys/testsuite/boards/unit_testing/unit_testing/Kconfig.defconfig b/subsys/testsuite/boards/unit_testing/unit_testing/Kconfig.defconfig new file mode 100644 index 00000000000..b2efdb2bee7 --- /dev/null +++ b/subsys/testsuite/boards/unit_testing/unit_testing/Kconfig.defconfig @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: Apache-2.0 +# +# Copyright (c) 2022 Nordic Semiconductor + +# Intentionally left empty diff --git a/subsys/testsuite/boards/unit_testing/unit_testing/unit_testing_defconfig b/subsys/testsuite/boards/unit_testing/unit_testing/unit_testing_defconfig new file mode 100644 index 00000000000..8880d9c9896 --- /dev/null +++ b/subsys/testsuite/boards/unit_testing/unit_testing/unit_testing_defconfig @@ -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 diff --git a/subsys/testsuite/soc/unit_testing/unit_testing/Kconfig.soc b/subsys/testsuite/soc/unit_testing/unit_testing/Kconfig.soc new file mode 100644 index 00000000000..38c319fc513 --- /dev/null +++ b/subsys/testsuite/soc/unit_testing/unit_testing/Kconfig.soc @@ -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 diff --git a/subsys/testsuite/ztest/include/zephyr/ztest.h b/subsys/testsuite/ztest/include/zephyr/ztest.h index 77038ae3fe0..7178bff1338 100644 --- a/subsys/testsuite/ztest/include/zephyr/ztest.h +++ b/subsys/testsuite/ztest/include/zephyr/ztest.h @@ -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