Make elf output names compatible to symbolize.py

Change the name of elf output files of SP builds to follow the
{SP_NAME}_{UUID}.elf pattern. This allows symbolize.py to find the
relevant elf file on disk when analyzing stack dumps.

Signed-off-by: Jelle Sels <jelle.sels@arm.com>
Signed-off-by: Gyorgy Szing <Gyorgy.Szing@arm.com>
Change-Id: Id8135c6a37ecf02ab7a27f56b39663bccb250948
diff --git a/components/service/spm_test/spm_test.cmake b/components/service/spm_test/spm_test.cmake
index f4add98..666429b 100644
--- a/components/service/spm_test/spm_test.cmake
+++ b/components/service/spm_test/spm_test.cmake
@@ -67,8 +67,6 @@
 	set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/install CACHE PATH "location to install build output to." FORCE)
 endif()
 
-set_target_properties(spm-test${SP_NUMBER} PROPERTIES OUTPUT_NAME "spm-test${SP_NUMBER}-${SP_UUID_CANON}.elf" )
-
 install(TARGETS spm-test${SP_NUMBER}
 			PUBLIC_HEADER DESTINATION ${TS_ENV}/include
 			RUNTIME DESTINATION ${TS_ENV}/bin
diff --git a/environments/opteesp/component.cmake b/environments/opteesp/component.cmake
index 8cd3883..ec0cf34 100644
--- a/environments/opteesp/component.cmake
+++ b/environments/opteesp/component.cmake
@@ -5,9 +5,14 @@
 #
 #-------------------------------------------------------------------------------
 
-if (NOT DEFINED TGT)
-	message(FATAL_ERROR "mandatory parameter TGT is not defined.")
-endif()
+# Check mandatory variables.
+foreach(_var IN ITEMS TGT)
+	if (NOT DEFINED ${_var})
+		message(FATAL_ERROR "Mandatory parameter '${_var}' missing.")
+	endif()
+endforeach()
+
+ts_add_uuid_to_exe_name(TGT "${TGT}" UUID "${SP_UUID_CANON}" )
 
 target_sources(${TGT} PRIVATE
 	"${CMAKE_CURRENT_LIST_DIR}/optee_sp_header.c"
diff --git a/environments/sp/component.cmake b/environments/sp/component.cmake
index 56db858..2a4a502 100644
--- a/environments/sp/component.cmake
+++ b/environments/sp/component.cmake
@@ -5,21 +5,14 @@
 #
 #-------------------------------------------------------------------------------
 
-if (NOT DEFINED TGT)
-	message(FATAL_ERROR "mandatory parameter TGT is not defined.")
-endif()
+# Check mandatory variables.
+foreach(_var IN ITEMS TGT TRACE_PREFIX SP_HEAP_SIZE SP_STACK_SIZE SP_UUID_CANON)
+	if (NOT DEFINED ${_var})
+		message(FATAL_ERROR "Mandatory parameter '${_var}' missing.")
+	endif()
+endforeach()
 
-if (NOT DEFINED TRACE_PREFIX)
-	message(FATAL_ERROR "mandatory parameter TRACE_PREFIX is not defined.")
-endif()
-
-if (NOT DEFINED SP_HEAP_SIZE)
-	message(FATAL_ERROR "mandatory parameter SP_HEAP_SIZE is not defined.")
-endif()
-
-if (NOT DEFINED SP_STACK_SIZE)
-	message(FATAL_ERROR "mandatory parameter SP_STACK_SIZE is not defined.")
-endif()
+ts_add_uuid_to_exe_name(TGT "${TGT}" UUID "${SP_UUID_CANON}" )
 
 target_sources(${TGT} PRIVATE
 	"${CMAKE_CURRENT_LIST_DIR}/entry.S"
diff --git a/tools/cmake/common/Utils.cmake b/tools/cmake/common/Utils.cmake
index c889320..256bbf9 100644
--- a/tools/cmake/common/Utils.cmake
+++ b/tools/cmake/common/Utils.cmake
@@ -49,3 +49,60 @@
         endif()
     endif()
 endFunction()
+
+#[===[.rst:
+.. cmake:command:: ts_add_uuid_to_exe_name
+
+  .. code-block:: cmake
+
+    ts_add_uuid_to_exe_name(TGT <target name> UUID "canonical string")
+
+	A function to modify the file name of the binary produced by a deployment to allow the OP-TEE symbolize.py tool to
+	find it when analyzing stack dumps. This is only useful for SP deployments targeting OP-TEE.
+	The filename will follow the template <file name>_<UUID>.elf format, where
+	   - file name is the original name already configured for the target
+	   - UUID is an argument of this function
+
+	INPUTS:
+
+	``TGT``
+	Mandatory. The name of the target to manipulate.
+
+	``UUID``
+	Mandatory. The UUID to be used to identify the SP. This has to match the UUID used by OP-TEE OS to identify the SP
+	runtime.
+
+#]===]
+function(ts_add_uuid_to_exe_name)
+	set(options)
+	set(oneValueArgs TGT UUID)
+	set(multiValueArgs)
+	cmake_parse_arguments(_MY_PARAMS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
+
+	check_args(ts_add_uuid_to_exe_name TGT)
+
+	get_target_property(_tgt_type ${_MY_PARAMS_TGT} TYPE)
+	if ("${_tgt_type}" STREQUAL "EXECUTABLE")
+		check_args(ts_add_uuid_to_exe_name UUID)
+		get_target_property(_out_name ${_MY_PARAMS_TGT} OUTPUT_NAME)
+		if (NOT _out_name)
+			set(_out_name "${_MY_PARAMS_TGT}")
+		endif()
+		get_target_property(_suffix ${_MY_PARAMS_TGT} SUFFIX)
+		if (NOT _suffix)
+			# Note CMAKE_EXECUTABLE_SUFFIX_<lang> might be needed here. Unfortunately
+			# this is only set, when it is set manually. It overrides the EXE_SUFFIX
+			# when set.
+			set(_suffix ${CMAKE_EXECUTABLE_SUFFIX})
+		endif()
+		# If executable suffix is still not set at this point, use the full name as basename.
+		if (_suffix)
+			string(REGEX REPLACE "${_suffix}$" "" _base_name "${_out_name}")
+		else()
+			set(_base_name "${_out_name}")
+		endif()
+
+		set(_out_name "${_base_name}_${_MY_PARAMS_UUID}${_suffix}")
+		set_target_properties(${_MY_PARAMS_TGT} PROPERTIES OUTPUT_NAME "${_out_name}")
+	endif()
+endfunction()