Support installing multiple build types of libts

Enable heaving multiple libts build types installed in the same system.
This simplifies debugging and coverage measurement of projects using
libts. After this change integrators shall still choose a single release
build type, as these use conflicting naming.

A build type specific letter is appended to the binary name:
  - d: Debug
  - c: DebugCoverage
  - <empty>: release, MinSizeRel, RevWithDebInfo

Signed-off-by: Gabor Toth <gabor.toth2@arm.com>
Signed-off-by: Gyorgy Szing <gyorgy.szing@arm.com>
Change-Id: Id404ff7abe8ba79ffa8929f0ad801721ebfc60e0
diff --git a/deployments/deployment.cmake b/deployments/deployment.cmake
index b098c7c..66fa31c 100644
--- a/deployments/deployment.cmake
+++ b/deployments/deployment.cmake
@@ -75,5 +75,12 @@
 	message(FATAL_ERROR "Unknown build type \"${CMAKE_BUILD_TYPE}\" specified in CMAKE_BUILD_TYPE.")
 endif()
 
+# Set postfix of libraries according the build type.
+if (UC_CMAKE_BUILD_TYPE STREQUAL "DEBUGCOVERAGE")
+	set(CMAKE_${UC_CMAKE_BUILD_TYPE}_POSTFIX "c")
+elseif(UC_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
+	set(CMAKE_${UC_CMAKE_BUILD_TYPE}_POSTFIX "d")
+endif()
+
 # Default protocol UUID used by TS SPs.
 set(TS_RPC_UUID_CANON "bdcd76d7-825e-4751-963b-86d4f84943ac" CACHE STRING "Trusted Services PRC (protocol) UUID.")
diff --git a/deployments/libts/libts-import.cmake b/deployments/libts/libts-import.cmake
index fd016ca..eb3afc5 100644
--- a/deployments/libts/libts-import.cmake
+++ b/deployments/libts/libts-import.cmake
@@ -25,15 +25,23 @@
 						   "If you wish to debug the search process pass -DCMAKE_FIND_DEBUG_MODE=ON to cmake.")
 		message(FATAL_ERROR ${_msg})
 	endif()
+	# Set build type, if a specific value is required. This leaves the default value in the hands of the
+	# libts deployment being built.
+	if (DEFINED LIBTS_BUILD_TYPE)
+		set(_libts_build_type_arg "-DCMAKE_BUILD_TYPE=${LIBTS_BUILD_TYPE}")
+	endif()
+
 	# If not successful, build libts as a sub-project.
 	execute_process(COMMAND
 		${CMAKE_COMMAND} -E env "CROSS_COMPILE=${CROSS_COMPILE}"
 		${CMAKE_COMMAND}
+			${_libts_build_type_arg}
 			-S ${TS_ROOT}/deployments/libts/${TS_ENV}
 			-B ${CMAKE_CURRENT_BINARY_DIR}/libts
 		RESULT_VARIABLE
 			_exec_error
 		)
+	unset(_libts_build_type_arg)
 	if (NOT _exec_error EQUAL 0)
 		message(FATAL_ERROR "Configuring libts failed. ${_exec_error}")
 	endif()
@@ -66,3 +74,32 @@
 else()
 	message(STATUS "Using prebuilt libts from ${libts_DIR}")
 endif()
+
+# Cmake will use the same build type of the imported target as used by the main project. If no mapping is configured and
+# the matching build type is not found, cmake will fall back to any build type. Details of the fall back mechanism are not
+# documented.
+# If a mapping is defined, and the imported target does not define the mapped build type, cmake will treat the library
+# as not found.
+#
+# If LIBTS_BUILD_TYPE is set and the main project wants to use a specific build type, configure build type mapping to
+# only allow using the requested build type.
+if (DEFINED LIBTS_BUILD_TYPE)
+	set_target_properties(libts::ts PROPERTIES
+		MAP_IMPORTED_CONFIG_DEBUG				${LIBTS_BUILD_TYPE}
+		MAP_IMPORTED_CONFIG_MINSIZEREL			${LIBTS_BUILD_TYPE}
+		MAP_IMPORTED_CONFIG_MINSIZWITHDEBINFO	${LIBTS_BUILD_TYPE}
+		MAP_IMPORTED_CONFIG_RELEASE				${LIBTS_BUILD_TYPE}
+		MAP_IMPORTED_CONFIG_RELWITHDEBINFO		${LIBTS_BUILD_TYPE}
+		MAP_IMPORTED_CONFIG_DEBUGCOVERAGE		${LIBTS_BUILD_TYPE}
+	)
+
+	# Do a manual check and issue a better message than the default one.
+	get_property(_libts_build_type TARGET libts::ts PROPERTY IMPORTED_CONFIGURATIONS)
+	string(TOUPPER ${LIBTS_BUILD_TYPE} _uc_libts_build_type)
+	if(${_uc_libts_build_type} IN_LIST _libts_build_type)
+	else()
+		message(FATAL_ERROR "Installed libts package does not supports required build type ${LIBTS_BUILD_TYPE}.")
+	endif()
+	unset(_libts_build_type)
+	unset(_uc_libts_build_type)
+endif()
diff --git a/tools/cmake/common/ExportLibrary.cmake b/tools/cmake/common/ExportLibrary.cmake
index c171c5b..22ed313 100644
--- a/tools/cmake/common/ExportLibrary.cmake
+++ b/tools/cmake/common/ExportLibrary.cmake
@@ -44,6 +44,7 @@
 
 
 	string(TOLOWER "${MY_PARAMS_LIB_NAME}" LC_LIB_NAME)
+	set(_version_file_name "${LC_LIB_NAME}ConfigVersion.cmake")
 
 	# Set default install location if none specified
 	if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
@@ -76,7 +77,7 @@
 	include(CMakePackageConfigHelpers)
 	get_target_property(_ver ${MY_PARAMS_TARGET} VERSION)
 	write_basic_package_version_file(
-		"${CMAKE_CURRENT_BINARY_DIR}/${LC_LIB_NAME}ConfigVersion.cmake"
+		"${CMAKE_CURRENT_BINARY_DIR}/${_version_file_name}"
 		VERSION "${_ver}"
 		COMPATIBILITY SameMajorVersion
 	)
@@ -87,6 +88,7 @@
 
 	get_filename_component(_configured_pkgcfg_name "${MY_PARAMS_PKG_CONFIG_FILE}" NAME_WLE)
 	set(_configured_pkgcfg_name "${CMAKE_CURRENT_BINARY_DIR}/${_configured_pkgcfg_name}")
+
 	configure_package_config_file(
 			"${MY_PARAMS_PKG_CONFIG_FILE}"
 			"${_configured_pkgcfg_name}"
@@ -108,7 +110,7 @@
 	install(
 		FILES
 			"${_configured_pkgcfg_name}"
-			"${CMAKE_CURRENT_BINARY_DIR}/${LC_LIB_NAME}ConfigVersion.cmake"
+			"${CMAKE_CURRENT_BINARY_DIR}/${_version_file_name}"
 		DESTINATION
 			${ConfigPackageLocation}
 		COMPONENT