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>
diff --git a/cmake/modules/arch.cmake b/cmake/modules/arch.cmake
index bd6d6ca..806b5c1 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 5032b57..371b6e4 100644
--- a/cmake/modules/boards.cmake
+++ b/cmake/modules/boards.cmake
@@ -52,8 +52,10 @@
 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 3800d81..24ff388 100644
--- a/cmake/modules/root.cmake
+++ b/cmake/modules/root.cmake
@@ -32,3 +32,10 @@
 
 # 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 c2d97ce..1b33478 100644
--- a/cmake/modules/unittest.cmake
+++ b/cmake/modules/unittest.cmake
@@ -4,6 +4,12 @@
 
 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()
 
 target_compile_options(testbinary PRIVATE
+  -imacros ${AUTOCONF_H}
   -Wall
   -I ${KOBJ_GEN_DIR}
   ${EXTRA_CPPFLAGS_AS_LIST}
@@ -89,7 +96,6 @@
 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 @@
   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 0000000..b2efdb2
--- /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 0000000..c2fdd2e
--- /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 0000000..88c43c2
--- /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 0000000..b2efdb2
--- /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 0000000..8880d9c
--- /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 0000000..38c319f
--- /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 77038ae..7178bff 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