Twincpu: Support building multi-core TF-M in a single building execution

Enable building multi-core TF-M in a single building execution.
- Add MultiCore.cmake to support bulding in multi-core scenario.
  Provide functions to platform specific cmake script to select
  secure or non-secure configuration, including cpu type and
  platform specific definitions.
- Add a building flag TFM_BUILD_IN_SPE to indicate whether current
  building is for SPE or not.
- According to TFM_BUILD_IN_SPE flag value, select corresponding
  configuration.

Change-Id: Ic1aca49190af9a9a5ec8ef1b855239a839fabe65
Signed-off-by: David Hu <david.hu@arm.com>
diff --git a/CommonConfig.cmake b/CommonConfig.cmake
index f08a355..4fb7b06 100644
--- a/CommonConfig.cmake
+++ b/CommonConfig.cmake
@@ -56,6 +56,19 @@
 	include(${PLATFORM_CMAKE_FILE})
 endif()
 
+# Select the corresponding CPU type and configuration according to current
+# building status in multi-core scenario.
+# The updated configuration will be used in following compiler setting.
+if (DEFINED TFM_MULTI_CORE_TOPOLOGY AND TFM_MULTI_CORE_TOPOLOGY)
+	include("Common/MultiCore")
+
+	if (NOT DEFINED TFM_BUILD_IN_SPE)
+		message(FATAL_ERROR "Flag of building in SPE is not specified. Please set TFM_BUILD_IN_SPE.")
+	else()
+		select_arm_cpu_type(${TFM_BUILD_IN_SPE})
+	endif()
+endif()
+
 if (DEFINED TFM_MULTI_CORE_TOPOLOGY AND TFM_MULTI_CORE_TOPOLOGY)
 	# CMSE is unnecessary in multi-core scenarios.
 	# TODO: Need further discussion about if CMSE is required when an Armv8-M
diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
index 5aba66d..a4b9dc8 100644
--- a/app/CMakeLists.txt
+++ b/app/CMakeLists.txt
@@ -7,6 +7,8 @@
 
 cmake_minimum_required(VERSION 3.7)
 
+set(TFM_BUILD_IN_SPE OFF)
+
 #Tell cmake where our modules can be found
 list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/../cmake)
 
diff --git a/bl2/ext/mcuboot/CMakeLists.txt b/bl2/ext/mcuboot/CMakeLists.txt
index 4bb7f14..80f0610 100644
--- a/bl2/ext/mcuboot/CMakeLists.txt
+++ b/bl2/ext/mcuboot/CMakeLists.txt
@@ -7,6 +7,8 @@
 
 cmake_minimum_required(VERSION 3.7)
 
+set(TFM_BUILD_IN_SPE ON)
+
 #Tell cmake where our modules can be found
 list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/../../../cmake)
 
@@ -60,6 +62,12 @@
 	include(${PLATFORM_CMAKE_FILE})
 endif()
 
+#Add platform specific definitions in SPE
+if (DEFINED TFM_PLATFORM_SECURE_DEFS)
+	embedded_set_target_compile_defines(TARGET ${PROJECT_NAME} LANGUAGE C DEFINES ${TFM_PLATFORM_SECURE_DEFS} APPEND)
+	embedded_set_target_compile_defines(TARGET ${PROJECT_NAME} LANGUAGE ASM DEFINES ${TFM_PLATFORM_SECURE_DEFS} APPEND)
+endif()
+
 #Append all our source files to global lists.
 list(APPEND ALL_SRC_C
 		"${MCUBOOT_DIR}/bl2_main.c"
@@ -106,7 +114,7 @@
 endif()
 embedded_set_target_link_includes(TARGET ${PROJECT_NAME} INCLUDES "${PLATFORM_LINK_INCLUDES}")
 
-add_executable(${PROJECT_NAME} ${ALL_SRC_ASM_BL2} ${ALL_SRC_C} ${ALL_SRC_CXX})
+add_executable(${PROJECT_NAME} ${ALL_SRC_ASM} ${ALL_SRC_C_BL2} ${ALL_SRC_ASM_BL2} ${ALL_SRC_C} ${ALL_SRC_CXX})
 
 #Set common compiler and linker flags
 config_setting_shared_compiler_flags(${PROJECT_NAME})
diff --git a/cmake/Common/MultiCore.cmake b/cmake/Common/MultiCore.cmake
new file mode 100644
index 0000000..46effbd
--- /dev/null
+++ b/cmake/Common/MultiCore.cmake
@@ -0,0 +1,88 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2019, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+# This file provides functions to support multi-core building.
+cmake_minimum_required(VERSION 3.7)
+
+# Platform specific cmake script calls this function to select and enable
+# building configuration in mulit-core scenario
+function(enable_multi_core_topology_config)
+	set(TFM_MULTI_CORE_TOPOLOGY ON PARENT_SCOPE)
+endfunction(enable_multi_core_topology_config)
+
+# Platform specific cmake script calls this function to set secure core cpu type
+# Argument CPU_TYPE_CMAKE represents the CMake file of the corresponding secure
+# CPU type.
+# CPU_TYPE_CMAKE value   CMake file       CPU type to be selected
+# CpuM0p                 CpuM0p.cmake     Cortex-M0+
+# CpuM4                  CpuM4.cmake      Cortex-M4
+# CpuM23                 CpuM23.cmake     Cortex-M23
+# CpuM33                 CpuM33.cmake     Cortex-M33
+# Other CPU types are not yet supported.
+function(set_secure_cpu_type CPU_TYPE_CMAKE)
+	include("Common/${CPU_TYPE_CMAKE}")
+
+	if (NOT DEFINED ARM_CPU_TYPE)
+		message(FATAL_ERROR "Error: Fail to set secure cpu type.")
+	else ()
+		set(SECURE_ARM_CPU_TYPE ${ARM_CPU_TYPE} PARENT_SCOPE)
+	endif ()
+endfunction(set_secure_cpu_type)
+
+# Platform specific cmake script calls this function to set non-secure core cpu
+# type
+# Argument CPU_TYPE_CMAKE represents the CMake file of the corresponding
+# non-secure CPU type.
+# CPU_TYPE_CMAKE value   CMake file       CPU type to be selected
+# CpuM0p                 CpuM0p.cmake     Cortex-M0+
+# CpuM4                  CpuM4.cmake      Cortex-M4
+# CpuM23                 CpuM23.cmake     Cortex-M23
+# CpuM33                 CpuM33.cmake     Cortex-M33
+# Other CPU types are not yet supported.
+function(set_ns_cpu_type CPU_TYPE_CMAKE)
+	include("Common/${CPU_TYPE_CMAKE}")
+
+	if (NOT DEFINED ARM_CPU_TYPE)
+		message(FATAL_ERROR "Error: Fail to set non-secure cpu type.")
+	else ()
+		set(NS_ARM_CPU_TYPE ${ARM_CPU_TYPE} PARENT_SCOPE)
+	endif ()
+endfunction(set_ns_cpu_type)
+
+# Platform specific cmake script calls this function to add platform specific
+# secure definitions.
+# Multiple definitions and options can be organized in argument PLATFORM_DEFS,
+# separated by spaces:
+#     add_platform_secure_definitions(DEF1 DEF2=VAL DEF3)
+# The `-D` option flag before preprocessor macros should be skipped.
+function(add_platform_secure_definitions PLATFORM_DEFS)
+	# Check if the same definition is already set
+	string(FIND TFM_PLATFORM_SECURE_DEFS PLATFORM_DEFS find_output)
+
+	if (find_output EQUAL -1)
+		# Not set yet. Add it into secure definition list.
+		set(TFM_PLATFORM_SECURE_DEFS ${TFM_PLATFORM_SECURE_DEFS}
+			${PLATFORM_DEFS}
+			PARENT_SCOPE)
+	endif ()
+endfunction(add_platform_secure_definitions)
+
+function(select_arm_cpu_type BUILD_IN_SPE_FLAG)
+	if (BUILD_IN_SPE_FLAG)
+		if (NOT DEFINED SECURE_ARM_CPU_TYPE)
+			message(FATAL_ERROR "Error: cannot find definition of SECURE_ARM_CPU_TYPE")
+		else ()
+			set(ARM_CPU_TYPE ${SECURE_ARM_CPU_TYPE} PARENT_SCOPE)
+		endif ()
+	else ()
+		if (NOT DEFINED NS_ARM_CPU_TYPE)
+			message(FATAL_ERROR "Error: cannot find definition of NS_ARM_CPU_TYPE")
+		else ()
+			set(ARM_CPU_TYPE ${NS_ARM_CPU_TYPE} PARENT_SCOPE)
+		endif ()
+	endif ()
+endfunction(select_arm_cpu_type)
diff --git a/secure_fw/CMakeLists.txt b/secure_fw/CMakeLists.txt
index 850f831..04b8f61 100644
--- a/secure_fw/CMakeLists.txt
+++ b/secure_fw/CMakeLists.txt
@@ -7,6 +7,8 @@
 
 cmake_minimum_required(VERSION 3.7)
 
+set(TFM_BUILD_IN_SPE ON)
+
 #Tell cmake where our modules can be found
 list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/../cmake)
 
@@ -88,6 +90,12 @@
 set(PROJECT_OBJ_LIB ${PROJECT_NAME}_obj_lib)
 add_library(${PROJECT_OBJ_LIB} OBJECT ${ALL_SRC_C} ${ALL_SRC_C_S} ${ALL_SRC_ASM_S})
 
+#Add platform specific definitions in SPE
+if (DEFINED TFM_PLATFORM_SECURE_DEFS)
+	embedded_set_target_compile_defines(TARGET ${PROJECT_OBJ_LIB} LANGUAGE C DEFINES ${TFM_PLATFORM_SECURE_DEFS} APPEND)
+	embedded_set_target_compile_defines(TARGET ${PROJECT_OBJ_LIB} LANGUAGE ASM DEFINES ${TFM_PLATFORM_SECURE_DEFS} APPEND)
+endif()
+
 #Set common compiler flags
 config_setting_shared_compiler_flags(${PROJECT_OBJ_LIB})