Build: Move out some codes from set_config.cmake

CMake config and Kconfig system will be separated later.
Moving out some common codes from set_config.cmake

- Add pre_config.cmake for common codes
  - Move out platform preload.cmake to pre_config.cmake
  - Move out Library Model deprecation message to pre_config.cmake
  - Move out CMAKE_BUILD_TYPE setup to pre_config.cmake
  - Error checks (Library Model, TFM_SYSTEM_DSP and CMAKE_GENERATOR) are
    gathered together into pre_config.cmake
- Add post_config.cmake for common codes
  - Move out the tfm_config setup from set_config.cmake to
    post_config.cmake.
  - Move out tfm_build_log_config.cmake inclusion to post_config.cmake
  - Move test repo fetch and config are separated and move to
    post_config.cmake

Change-Id: Idf39766d4f7c3e3933443c7eef31b9e0479064ea
Signed-off-by: Kevin Peng <kevin.peng@arm.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 64f70e8..0badba6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2020-2022, Arm Limited. All rights reserved.
+# Copyright (c) 2020-2023, Arm Limited. All rights reserved.
 # Copyright (c) 2022 Cypress Semiconductor Corporation (an Infineon company)
 # or an affiliate of Cypress Semiconductor Corporation. All rights reserved.
 #
@@ -23,20 +23,9 @@
 include(remote_library)
 
 ############################ CONFIGURATION #####################################
-
-# Configure TFM_PLATFORM
-include(${CMAKE_SOURCE_DIR}/config/tfm_platform.cmake)
-
-if(TFM_SYSTEM_DSP)
-    message(FATAL_ERROR "Hardware DSP is currently not supported in TF-M")
-endif()
-
+include(config/pre_config.cmake)
 include(config/set_config.cmake)
-
-if(NOT ${CMAKE_GENERATOR} STREQUAL "Unix Makefiles" AND
-   NOT ${CMAKE_GENERATOR} STREQUAL "Ninja")
-    Message(FATAL_ERROR "Unsupported generator ${CMAKE_GENERATOR}. Hint: Try -G\"Unix Makefiles\"")
-endif()
+include(config/post_config.cmake)
 
 ############################### Compiler configuration #########################
 
diff --git a/config/post_config.cmake b/config/post_config.cmake
new file mode 100644
index 0000000..5c4b928
--- /dev/null
+++ b/config/post_config.cmake
@@ -0,0 +1,48 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2023, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+# Fetch tf-m-tests repo during config, if NS or regression test is required.
+# Therefore tf-m-tests configs can be set with TF-M configs since their configs
+# are coupled.
+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)
+    include(${TFM_TEST_PATH}/config/set_config.cmake)
+endif()
+
+# Build system log config
+include(${CMAKE_SOURCE_DIR}/config/tfm_build_log_config.cmake)
+
+# The library to collect compile definitions of config options.
+add_library(tfm_config INTERFACE)
+
+target_compile_definitions(tfm_config
+    INTERFACE
+        $<$<STREQUAL:${TEST_PSA_API},CRYPTO>:TEST_PSA_API_CRYPTO>
+        $<$<STREQUAL:${TEST_PSA_API},IPC>:TEST_PSA_API_IPC>
+)
+
+# Set user defined TF-M config header file
+if(PROJECT_CONFIG_HEADER_FILE)
+    if(NOT EXISTS ${PROJECT_CONFIG_HEADER_FILE})
+        message(FATAL_ERROR "${PROJECT_CONFIG_HEADER_FILE} does not exist! Please use absolute path.")
+    endif()
+    target_compile_definitions(tfm_config
+        INTERFACE
+            PROJECT_CONFIG_HEADER_FILE="${PROJECT_CONFIG_HEADER_FILE}"
+    )
+endif()
+
+# Set platform defined TF-M config header file
+set(TARGET_CONFIG_HEADER_FILE ${TARGET_PLATFORM_PATH}/config_tfm_target.h)
+if(EXISTS ${TARGET_CONFIG_HEADER_FILE})
+    target_compile_definitions(tfm_config
+        INTERFACE
+            TARGET_CONFIG_HEADER_FILE="${TARGET_CONFIG_HEADER_FILE}"
+    )
+endif()
diff --git a/config/pre_config.cmake b/config/pre_config.cmake
new file mode 100644
index 0000000..760dd6c
--- /dev/null
+++ b/config/pre_config.cmake
@@ -0,0 +1,38 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2023, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+if(NOT ${CMAKE_GENERATOR} STREQUAL "Unix Makefiles" AND
+   NOT ${CMAKE_GENERATOR} STREQUAL "Ninja")
+    Message(FATAL_ERROR "Unsupported generator ${CMAKE_GENERATOR}. Hint: Try -G\"Unix Makefiles\"")
+endif()
+
+if(TFM_SYSTEM_DSP)
+    message(FATAL_ERROR "Hardware DSP is currently not supported in TF-M")
+endif()
+
+if(TFM_LIB_MODEL)
+    message(FATAL_ERROR "Library Model is deprecated, please DO NOT use TFM_LIB_MODEL anymore."
+                        "SFN model is a replacement for Library Model.
+                         You can use -DCONFIG_TFM_SPM_BACKEND=SFN to select SFN model.")
+endif()
+
+# The default build type is MinSizeRel. If debug symbols are needed then
+# -DCMAKE_BUILD_TYPE=debug should be used (likewise with other build types)
+if(NOT CMAKE_BUILD_TYPE)
+    set(CMAKE_BUILD_TYPE "MinSizeRel" CACHE STRING "Build type: [Debug, Release, RelWithDebInfo, MinSizeRel]" FORCE)
+endif()
+
+# Configure TFM_PLATFORM
+include(${CMAKE_SOURCE_DIR}/config/tfm_platform.cmake)
+
+# Some compiler flags depend on the CPU / platform config. This include should
+# be run before the toolchain file so the compiler can be configured properly.
+if (NOT EXISTS ${TARGET_PLATFORM_PATH}/preload.cmake)
+    Message(FATAL_ERROR "preload.cmake is not found in ${TFM_PLATFORM}")
+else()
+    include(${TARGET_PLATFORM_PATH}/preload.cmake)
+endif()
diff --git a/config/set_config.cmake b/config/set_config.cmake
index e34124a..8a0a095 100644
--- a/config/set_config.cmake
+++ b/config/set_config.cmake
@@ -9,46 +9,24 @@
 # docs/getting_started/tfm_build_instructions.rst under Cmake Configuration. If
 # the sequence is updated here the docs must also be updated.
 
-# The default build type is MinSizeRel. If debug symbols are needed then
-# -DCMAKE_BUILD_TYPE=debug should be used (likewise with other build types)
-if (NOT CMAKE_BUILD_TYPE)
-    set(CMAKE_BUILD_TYPE "MinSizeRel" CACHE STRING "Build type: [Debug, Release, RelWithDebInfo, MinSizeRel]" FORCE)
-endif()
-
-if (TFM_LIB_MODEL)
-    message(FATAL_ERROR "Library Model is deprecated, please DO NOT use TFM_LIB_MODEL anymore."
-                        "SFN model is a replacement for Library Model. You can use -DCONFIG_TFM_SPM_BACKEND=SFN to select SFN model.")
-endif()
-
 # Load extra config
-if (TFM_EXTRA_CONFIG_PATH)
+if(TFM_EXTRA_CONFIG_PATH)
     include(${TFM_EXTRA_CONFIG_PATH})
 endif()
 
-# Some compiler flags depend on the CPU / platform config. This include should
-# be run before the toolchain file so the compiler can be configured properly.
-if (NOT EXISTS ${TARGET_PLATFORM_PATH}/preload.cmake)
-    Message(FATAL_ERROR "preload.cmake is not found in ${TFM_PLATFORM}")
-else()
-    include(${TARGET_PLATFORM_PATH}/preload.cmake)
-endif()
-
-# Set TF-M project config header file
-add_library(tfm_config INTERFACE)
-
 # Load PSA config, setting options not already set
-if (TEST_PSA_API)
+if(TEST_PSA_API)
     include(config/tests/config_test_psa_api.cmake)
 endif()
 
 # Load build type config, setting options not already set
 string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWERCASE)
-if (EXISTS ${CMAKE_SOURCE_DIR}/config/build_type/${CMAKE_BUILD_TYPE_LOWERCASE}.cmake)
+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 platform config, setting options not already set
-if (EXISTS ${TARGET_PLATFORM_PATH}/config.cmake)
+if(EXISTS ${TARGET_PLATFORM_PATH}/config.cmake)
     include(${TARGET_PLATFORM_PATH}/config.cmake)
 endif()
 
@@ -61,7 +39,7 @@
 include(lib/ext/tf-m-tests/reg_parse.cmake)
 
 # Load profile config, setting options not already set
-if (TFM_PROFILE)
+if(TFM_PROFILE)
     include(config/profile/${TFM_PROFILE}.cmake)
 endif()
 
@@ -71,15 +49,13 @@
     include(${CMAKE_CURRENT_LIST_DIR}/tests/regression_config.cmake)
 endif()
 
-include(${CMAKE_SOURCE_DIR}/config/tfm_build_log_config.cmake)
-
 # 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
+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)
@@ -89,12 +65,12 @@
 endif()
 
 # Load bl1 config
-if (BL1 AND PLATFORM_DEFAULT_BL1)
+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)
+if(NOT DEFINED BL2 OR BL2)
     include(${CMAKE_SOURCE_DIR}/bl2/ext/mcuboot/mcuboot_default_config.cmake)
 endif()
 
@@ -107,31 +83,6 @@
 # Load defaults, setting options not already set
 include(config/config_base.cmake)
 
-# Fetch tf-m-tests repo during config, if NS or regression test is required.
-# Therefore tf-m-tests configs can be set with TF-M configs since their configs
-# are coupled.
-include(lib/ext/tf-m-tests/fetch_repo.cmake)
-
 # Set secure log configs
 # It also depends on regression test config.
 include(config/tfm_secure_log.cmake)
-
-# Set user defined TF-M config header file
-if(PROJECT_CONFIG_HEADER_FILE)
-    if(NOT EXISTS ${PROJECT_CONFIG_HEADER_FILE})
-        message(FATAL_ERROR "${PROJECT_CONFIG_HEADER_FILE} does not exist! Please use absolute path.")
-    endif()
-    target_compile_definitions(tfm_config
-        INTERFACE
-            PROJECT_CONFIG_HEADER_FILE="${PROJECT_CONFIG_HEADER_FILE}"
-    )
-endif()
-
-# Set platform defined TF-M config header file
-set(TARGET_CONFIG_HEADER_FILE ${TARGET_PLATFORM_PATH}/config_tfm_target.h)
-if(EXISTS ${TARGET_CONFIG_HEADER_FILE})
-    target_compile_definitions(tfm_config
-        INTERFACE
-            TARGET_CONFIG_HEADER_FILE="${TARGET_CONFIG_HEADER_FILE}"
-    )
-endif()
diff --git a/config/tests/config_test_psa_api.cmake b/config/tests/config_test_psa_api.cmake
index c7e323d..3573872 100644
--- a/config/tests/config_test_psa_api.cmake
+++ b/config/tests/config_test_psa_api.cmake
@@ -1,5 +1,5 @@
 #------------------------------------------------------------------------------
-# Copyright (c) 2020-2022, Arm Limited. All rights reserved.
+# Copyright (c) 2020-2023, Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -34,10 +34,4 @@
     set(TFM_PARTITION_PLATFORM                 ON       CACHE BOOL      "Enable Platform partition")
 endif()
 
-target_compile_definitions(tfm_config
-    INTERFACE
-        $<$<STREQUAL:${TEST_PSA_API},CRYPTO>:TEST_PSA_API_CRYPTO>
-        $<$<STREQUAL:${TEST_PSA_API},IPC>:TEST_PSA_API_IPC>
-)
-
 set(PROJECT_CONFIG_HEADER_FILE  "${CMAKE_SOURCE_DIR}/config/tests/config_test_psa_api.h" CACHE FILEPATH "User defined header file for TF-M config")
diff --git a/lib/ext/tf-m-tests/fetch_repo.cmake b/lib/ext/tf-m-tests/fetch_repo.cmake
index a2a12bd..442e924 100644
--- a/lib/ext/tf-m-tests/fetch_repo.cmake
+++ b/lib/ext/tf-m-tests/fetch_repo.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2020-2022, Arm Limited. All rights reserved.
+# Copyright (c) 2020-2023, Arm Limited. All rights reserved.
 # Copyright (c) 2022 Cypress Semiconductor Corporation (an Infineon company)
 # or an affiliate of Cypress Semiconductor Corporation. All rights reserved.
 #
@@ -33,9 +33,4 @@
     if (NOT TFM_TEST_PATH)
         set(TFM_TEST_PATH ${TFM_TEST_REPO_PATH}/test CACHE PATH "Path to TFM tests" FORCE)
     endif()
-
-    # Load TF-M regression test suites setting
-    if (TFM_NS_REG_TEST OR TFM_S_REG_TEST)
-        include(${TFM_TEST_PATH}/config/set_config.cmake)
-    endif()
 endif()