Build: Separate Kconfig system from CMake config system

The Kconfig system now has a separated routine rather than integrated
into the CMake Config system.
It is now easy to adjust the config steps for the Kconfig system without
impacting the CMake system.

Change-Id: I41e088a571448ab74ab9e669d73a623bc9af5e51
Signed-off-by: Kevin Peng <kevin.peng@arm.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0badba6..aca3d6b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -24,7 +24,13 @@
 
 ############################ CONFIGURATION #####################################
 include(config/pre_config.cmake)
-include(config/set_config.cmake)
+
+if(USE_KCONFIG_TOOL)
+    include(${CMAKE_SOURCE_DIR}/config/kconfig.cmake)
+else()
+    include(${CMAKE_SOURCE_DIR}/config/set_config.cmake)
+endif()
+
 include(config/post_config.cmake)
 
 ############################### Compiler configuration #########################
diff --git a/config/kconfig.cmake b/config/kconfig.cmake
index a7d843b..6d9a0bd 100644
--- a/config/kconfig.cmake
+++ b/config/kconfig.cmake
@@ -5,14 +5,23 @@
 #
 #-------------------------------------------------------------------------------
 
-cmake_minimum_required(VERSION 3.15)
+set(ROOT_KCONFIG                ${CMAKE_SOURCE_DIR}/Kconfig)
+set(PLATFORM_KCONFIG            ${TARGET_PLATFORM_PATH}/Kconfig)
 
-if (NOT USE_KCONFIG_TOOL)
-    return()
+if(TFM_PROFILE)
+    # Selecting TF-M profiles is not supported yet.
+    message(FATAL_ERROR "Selecting TF-M profiles is not supported yet in Kconfig system!")
 endif()
 
-set(DOTCONFIG_FILE              "${CMAKE_BINARY_DIR}/.config")
-set(ROOT_KCONFIG                "${CMAKE_SOURCE_DIR}/Kconfig")
+if(NOT EXISTS ${PLATFORM_KCONFIG})
+    message(WARNING "The platform does not have Kconfig file! \
+                     The Kconfig system cannot get its configuration settings.
+                     So it is not guaranteed that the Kconfig system works properly.")
+
+    if(EXISTS ${TARGET_PLATFORM_PATH}/config.cmake)
+        include(${TARGET_PLATFORM_PATH}/config.cmake)
+    endif()
+endif()
 
 find_package(Python3)
 
@@ -30,11 +39,64 @@
 endif()
 
 # Component configs generated by tfm_kconfig.py
-if (EXISTS "${CMAKE_BINARY_DIR}/project_config.h")
+if(EXISTS "${CMAKE_BINARY_DIR}/project_config.h")
     set(PROJECT_CONFIG_HEADER_FILE "${CMAKE_BINARY_DIR}/project_config.h"     CACHE STRING "User defined header file for TF-M config")
 endif()
 
 # Load project cmake configs generated by tfm_kconfig.py
-if (EXISTS "${CMAKE_BINARY_DIR}/project_config.cmake")
+if(EXISTS "${CMAKE_BINARY_DIR}/project_config.cmake")
     include("${CMAKE_BINARY_DIR}/project_config.cmake")
 endif()
+
+####################################################################################################
+
+# The Kconfig system does not cover all the config options for the time being.
+# So part of them still use the CMake config system.
+
+# Load regression configs overrided by platform
+if(EXISTS ${TARGET_PLATFORM_PATH}/reg_config_override.cmake)
+    include(${TARGET_PLATFORM_PATH}/reg_config_override.cmake)
+endif()
+
+# Load build type config, setting options not already set
+if(EXISTS ${CMAKE_SOURCE_DIR}/config/build_type/${CMAKE_BUILD_TYPE_LOWERCASE}.cmake)
+    include(${CMAKE_SOURCE_DIR}/config/build_type/${CMAKE_BUILD_TYPE_LOWERCASE}.cmake)
+endif()
+
+# Load TF-M model specific default config
+# Load IPC backend config if isolation level is explicitly specified to 2/3 or IPC backend is
+# selected via build command line. Otherwise, load SFN backend config by default.
+# If a pair of invalid settings are passed via command line, it will be captured later via config
+# check.
+# Also select IPC model by default for multi-core platform unless it has already selected SFN model
+if((DEFINED TFM_ISOLATION_LEVEL AND TFM_ISOLATION_LEVEL GREATER 1) OR
+    CONFIG_TFM_SPM_BACKEND STREQUAL "IPC" OR
+    TFM_MULTI_CORE_TOPOLOGY)
+    include(config/tfm_ipc_config_default.cmake)
+else()
+    #The default backend is SFN
+    include(config/tfm_sfn_config_default.cmake)
+endif()
+
+# Load bl1 config
+if(BL1 AND PLATFORM_DEFAULT_BL1)
+    include(${CMAKE_SOURCE_DIR}/bl1/config/bl1_config_default.cmake)
+endif()
+
+# Load MCUboot specific default.cmake
+if(NOT DEFINED BL2 OR BL2)
+    include(${CMAKE_SOURCE_DIR}/bl2/ext/mcuboot/mcuboot_default_config.cmake)
+endif()
+
+# Include FWU partition configs.
+include(config/tfm_fwu_config.cmake)
+
+# Include coprocessor configs
+include(config/cp_config_default.cmake)
+
+# Load defaults, setting options not already set
+include(config/config_base.cmake)
+
+# Set secure log configs
+# It also depends on regression test config.
+include(config/tfm_secure_log.cmake)
diff --git a/config/post_config.cmake b/config/post_config.cmake
index 5c4b928..ca2c7d8 100644
--- a/config/post_config.cmake
+++ b/config/post_config.cmake
@@ -11,7 +11,7 @@
 include(lib/ext/tf-m-tests/fetch_repo.cmake)
 
 # Load TF-M regression test suites setting
-if(TFM_NS_REG_TEST OR TFM_S_REG_TEST)
+if(NOT USE_KCONFIG_TOOL AND (TFM_NS_REG_TEST OR TFM_S_REG_TEST))
     include(${TFM_TEST_PATH}/config/set_config.cmake)
 endif()
 
diff --git a/config/set_config.cmake b/config/set_config.cmake
index 8a0a095..015349f 100644
--- a/config/set_config.cmake
+++ b/config/set_config.cmake
@@ -30,9 +30,6 @@
     include(${TARGET_PLATFORM_PATH}/config.cmake)
 endif()
 
-# Load configs generated from Kconfig
-include(${CMAKE_SOURCE_DIR}/config/kconfig.cmake)
-
 # Parse tf-m-tests config prior to platform specific config.cmake
 # Some platforms select different configuration according when regression tests
 # are enabled.