Add tools for exporting SP memory regions

Create a Python script and CMake functions for parsing the SP ELF file
and exporting the memory region information into the SP manifest file.

Signed-off-by: Imre Kis <imre.kis@arm.com>
Change-Id: I800bc2c85fa11416843b71958a796a7b5321bd2d
diff --git a/tools/cmake/common/ExportMemoryRegionsToManifest.cmake b/tools/cmake/common/ExportMemoryRegionsToManifest.cmake
new file mode 100644
index 0000000..affd746
--- /dev/null
+++ b/tools/cmake/common/ExportMemoryRegionsToManifest.cmake
@@ -0,0 +1,46 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+#[===[.rst:
+.. cmake:command:: export_memory_regions_to_manifest
+
+	Exports the memory regions from an ELF format SP into a manifest file fragment.
+
+	.. code:: cmake
+
+		export_memory_regions_to_manifest(TARGET NAME RES)
+
+	INPUTS:
+
+	``TARGET``
+	Build target
+
+	``NAME``
+	The UUID of the SP as a string.
+
+	``RES``
+	The name of the SP.
+
+#]===]
+function(export_memory_regions_to_manifest)
+	set(options)
+	set(oneValueArgs TARGET NAME RES)
+	set(multiValueArgs)
+	cmake_parse_arguments(MY "${options}" "${oneValueArgs}"
+						"${multiValueArgs}" ${ARGN} )
+
+	find_package(Python3 REQUIRED COMPONENTS Interpreter)
+
+	add_custom_command(
+		TARGET ${MY_TARGET} POST_BUILD
+		COMMAND ${Python3_EXECUTABLE} ${TS_ROOT}/tools/python/elf_segments_to_manifest.py
+				$<TARGET_FILE:${MY_TARGET}>
+				$<TARGET_FILE_DIR:${MY_TARGET}>/${MY_NAME})
+	if (MY_RES)
+		set(${MY_RES} $<TARGET_FILE_DIR:${MY_TARGET}>/${MY_NAME} PARENT_SCOPE)
+	endif()
+endfunction()
diff --git a/tools/cmake/common/ExportSp.cmake b/tools/cmake/common/ExportSp.cmake
index f324327..f22e9cb 100644
--- a/tools/cmake/common/ExportSp.cmake
+++ b/tools/cmake/common/ExportSp.cmake
@@ -10,7 +10,13 @@
 
 	.. code:: cmake
 
-		export_sp(SP_UUID <uuid> SP_NAME <name> MK_IN <.mk path> DTS_IN <DTS path> JSON_IN <JSON path>)
+		export_sp(
+			SP_UUID <uuid> SP_NAME
+			<name> MK_IN <.mk path>
+			DTS_IN <DTS path>
+			DTS_MEM_REGIONS <Memory region manifest path>
+			JSON_IN <JSON path>
+		)
 
 	INPUTS:
 
@@ -26,13 +32,16 @@
 	``DTS_IN``
 	Manifest file template
 
+	`DTS_MEM_REGIONS`
+	Optional, Memory region manifest file
+
 	``JSON_IN``
 	Optional, SP layout JSON file template for TF-A
 
 #]===]
 function (export_sp)
 	set(options)
-	set(oneValueArgs SP_UUID SP_NAME MK_IN DTS_IN JSON_IN)
+	set(oneValueArgs SP_UUID SP_NAME MK_IN DTS_IN DTS_MEM_REGIONS JSON_IN)
 	set(multiValueArgs)
 	cmake_parse_arguments(EXPORT "${options}" "${oneValueArgs}"
 						"${multiValueArgs}" ${ARGN} )
@@ -74,8 +83,12 @@
 	configure_file(${EXPORT_DTS_IN} ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_SP_UUID}.dts @ONLY NEWLINE_STYLE UNIX)
 	install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_SP_UUID}.dts DESTINATION ${TS_ENV}/manifest)
 
+	if (DEFINED EXPORT_DTS_MEM_REGIONS)
+		install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_DTS_MEM_REGIONS} DESTINATION ${TS_ENV}/manifest)
+	endif()
+
 	if (DEFINED EXPORT_JSON_IN)
 		configure_file(${EXPORT_JSON_IN} ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_SP_NAME}.json @ONLY NEWLINE_STYLE UNIX)
 		install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_SP_NAME}.json DESTINATION ${TS_ENV}/json)
 	endif()
-endfunction()
\ No newline at end of file
+endfunction()