Fix: remove TS dependency from exported DTS files

Device Tree source files exported by the project must not depend on any
TS specific processing information. Otherwise, external processing needs
to become TS specific or will not be possible.
This change fixes SP manifest file processing and ensures exported DTS
files are "self-contained" and do not depend on TS specific processing
information. This is achieved by executing the C preprocessor part of
the SP build flow to resolve and remove C macros and include files.

Signed-off-by: Imre Kis <imre.kis@arm.com>
Change-Id: I985113b4a015775fc264a05e15f45ff33e66a8af
diff --git a/tools/cmake/common/ExportSp.cmake b/tools/cmake/common/ExportSp.cmake
index faa69fe..124489e 100644
--- a/tools/cmake/common/ExportSp.cmake
+++ b/tools/cmake/common/ExportSp.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2020-2023, Arm Limited and Contributors. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -75,19 +75,29 @@
 	# /dts-v1/ tag and its node should be unique, i.e. the SP name.
 	set(DTS_TAG "")
 	set(DTS_NODE "${EXPORT_SP_NAME}")
-	configure_file(${EXPORT_DTS_IN} ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_SP_UUID_CANON}.dtsi @ONLY NEWLINE_STYLE UNIX)
+	configure_file(${EXPORT_DTS_IN} ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_SP_UUID_CANON}_before_preprocessing.dtsi @ONLY NEWLINE_STYLE UNIX)
+
+	compiler_preprocess_file(
+		SRC ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_SP_UUID_CANON}_before_preprocessing.dtsi
+		DST ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_SP_UUID_CANON}.dtsi
+		TARGET ${EXPORT_SP_NAME}
+	)
+
 	install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_SP_UUID_CANON}.dtsi DESTINATION ${TS_ENV}/manifest)
 
 	# The .dts file is a standalone structure, thus it should have the /dts-v1/ tag and it
 	# starts with the root node.
 	set(DTS_TAG "/dts-v1/;")
 	set(DTS_NODE "/")
-	configure_file(${EXPORT_DTS_IN} ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_SP_UUID_CANON}.dts @ONLY NEWLINE_STYLE UNIX)
-	install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_SP_UUID_CANON}.dts DESTINATION ${TS_ENV}/manifest)
+	configure_file(${EXPORT_DTS_IN} ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_SP_UUID_CANON}_before_preprocessing.dts @ONLY NEWLINE_STYLE UNIX)
 
-	if (DEFINED EXPORT_DTS_MEM_REGIONS)
-		install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_DTS_MEM_REGIONS} DESTINATION ${TS_ENV}/manifest)
-	endif()
+	compiler_preprocess_file(
+		SRC ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_SP_UUID_CANON}_before_preprocessing.dts
+		DST ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_SP_UUID_CANON}.dts
+		TARGET ${EXPORT_SP_NAME}
+	)
+
+	install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_SP_UUID_CANON}.dts DESTINATION ${TS_ENV}/manifest)
 
 	if (DEFINED EXPORT_JSON_IN)
 		configure_file(${EXPORT_JSON_IN} ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_SP_NAME}.json @ONLY NEWLINE_STYLE UNIX)
diff --git a/tools/cmake/compiler/GCC.cmake b/tools/cmake/compiler/GCC.cmake
index 993bae0..79f9158 100644
--- a/tools/cmake/compiler/GCC.cmake
+++ b/tools/cmake/compiler/GCC.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2019-2022, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2019-2023, Arm Limited and Contributors. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -91,6 +91,9 @@
   ``DST``
 	Where to write the preprocessed output.
 
+  ``TARGET`` (optional)
+	Target that the custom command is tied to.
+
   ``DEFINES`` (multi, optional)
 	Definitions for the preprocessor.
 
@@ -100,7 +103,7 @@
 #]===]
 function(compiler_preprocess_file)
 	set(_OPTIONS_ARGS)
-	set(_ONE_VALUE_ARGS SRC DST)
+	set(_ONE_VALUE_ARGS TARGET SRC DST)
 	set(_MULTI_VALUE_ARGS DEFINES INCLUDES)
 	cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN})
 
@@ -116,11 +119,22 @@
 		list(APPEND _flags ${_MY_PARAMS_INCLUDES})
 	endif()
 
-	add_custom_command(
-		DEPENDS ${_MY_PARAMS_SRC} OUTPUT ${_MY_PARAMS_DST}
-		COMMAND ${CMAKE_C_COMPILER} -E -P -x assembler-with-cpp ${_flags}
-				${_MY_PARAMS_SRC} -o ${_MY_PARAMS_DST}
-	)
+	if(_MY_PARAMS_TARGET)
+		add_custom_command(
+			TARGET ${_MY_PARAMS_TARGET}
+			POST_BUILD
+			DEPENDS ${_MY_PARAMS_SRC}
+			COMMAND ${CMAKE_C_COMPILER} -E -P -x assembler-with-cpp ${_flags}
+					${_MY_PARAMS_SRC} -o ${_MY_PARAMS_DST}
+		)
+	else()
+		add_custom_command(
+			DEPENDS ${_MY_PARAMS_SRC}
+			OUTPUT ${_MY_PARAMS_DST}
+			COMMAND ${CMAKE_C_COMPILER} -E -P -x assembler-with-cpp ${_flags}
+					${_MY_PARAMS_SRC} -o ${_MY_PARAMS_DST}
+		)
+	endif()
 endfunction()
 
 #[===[.rst: