Fix: change libts to export a CMake package

libts install content was not compatible to find_module() which made
using a pre-built libts binary from CMake less than ideal.

This change adds the missing files and updates export and install
commands to make libts generate a proper CMake package.

From now on an external project will be able to use find_module() to
integrate libts into its build.

Signed-off-by: Gyorgy Szing <Gyorgy.Szing@arm.com>
Change-Id: I9e86e02030f6fb3c86af45252110f939cb82670c
diff --git a/deployments/component-test/component-test.cmake b/deployments/component-test/component-test.cmake
index f9ddf76..c742e6b 100644
--- a/deployments/component-test/component-test.cmake
+++ b/deployments/component-test/component-test.cmake
@@ -176,4 +176,6 @@
 if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
 	set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/install CACHE PATH "location to install build output to." FORCE)
 endif()
-install(TARGETS component-test DESTINATION ${TS_ENV}/bin)
+install(TARGETS component-test
+		RUNTIME DESTINATION ${TS_ENV}/bin
+		PUBLIC_HEADER DESTINATION ${TS_ENV}/include)
diff --git a/deployments/libts/libts-import.cmake b/deployments/libts/libts-import.cmake
index dcabc45..84a897a 100644
--- a/deployments/libts/libts-import.cmake
+++ b/deployments/libts/libts-import.cmake
@@ -11,48 +11,55 @@
 # CMake build file allows libts to be built and installed into the binary
 # directory of the dependent.
 #-------------------------------------------------------------------------------
+option(CFG_FORCE_PREBUILT_LIBTS Off)
+# Try to find a pre-build package.
+find_package(libts "1.0.0" QUIET PATHS ${CMAKE_CURRENT_BINARY_DIR}/libts_install/${TS_ENV}/lib/cmake/libts)
+if(NOT libts_FOUND)
+	if (CFG_FORCE_PREBUILT_LIBTS)
+		string(CONCAT _msg "find_package() failed to find the \"libts\" package. Please pass -Dlibts_ROOT=<path> or"
+						   " -DCMAKE_FIND_ROOT_PATH=<path> cmake variable, where <path> is the INSTALL_PREFIX used"
+						   " when building libts. libts_ROOT can be set in the environment too."
+						   "If you wish to debug the search process pass -DCMAKE_FIND_DEBUG_MODE=ON to cmake.")
+		message(FATAL_ERROR ${_msg})
+	endif()
+	# If not successful, build libts as a sub-project.
+	execute_process(COMMAND
+		${CMAKE_COMMAND} -E env "CROSS_COMPILE=${CROSS_COMPILE}"
+		${CMAKE_COMMAND}
+			-S ${TS_ROOT}/deployments/libts/${TS_ENV}
+			-B ${CMAKE_CURRENT_BINARY_DIR}/libts
+		RESULT_VARIABLE
+			_exec_error
+		)
+	if (NOT _exec_error EQUAL 0)
+		message(FATAL_ERROR "Configuring libts failed. ${_exec_error}")
+	endif()
+	execute_process(COMMAND
+		${CMAKE_COMMAND} -E env "CROSS_COMPILE=${CROSS_COMPILE}"
+		${CMAKE_COMMAND}
+			--build ${CMAKE_CURRENT_BINARY_DIR}/libts
+			--parallel ${PROCESSOR_COUNT}
+		RESULT_VARIABLE
+			_exec_error
+		)
+	if (NOT _exec_error EQUAL 0)
+		message(FATAL_ERROR "Installing libts failed. ${_exec_error}")
+	endif()
+	execute_process(COMMAND
+		${CMAKE_COMMAND} -E env "CROSS_COMPILE=${CROSS_COMPILE}"
+		${CMAKE_COMMAND}
+			--install ${CMAKE_CURRENT_BINARY_DIR}/libts
+			--prefix ${CMAKE_CURRENT_BINARY_DIR}/libts_install
+		RESULT_VARIABLE
+			_exec_error
+		)
+	if (NOT _exec_error EQUAL 0)
+		message(FATAL_ERROR "Installing libts failed. ${_exec_error}")
+	endif()
 
-# Determine the number of processes to run while running parallel builds.
-# Pass -DPROCESSOR_COUNT=<n> to cmake to override.
-if(NOT DEFINED PROCESSOR_COUNT)
-	include(ProcessorCount)
-	ProcessorCount(PROCESSOR_COUNT)
-	set(PROCESSOR_COUNT ${PROCESSOR_COUNT} CACHE STRING "Number of cores to use for parallel builds.")
+	install(SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/libts/cmake_install.cmake)
+
+	find_package(libts "1.0.0" QUIET REQUIRED PATHS ${CMAKE_CURRENT_BINARY_DIR}/libts_install/${TS_ENV}/lib/cmake/libts)
+else()
+	message(STATUS "Using prebuilt libts from ${libts_DIR}")
 endif()
-
-set(LIBTS_INSTALL_PATH "${CMAKE_CURRENT_BINARY_DIR}/libts_install" CACHE PATH "libts installation directory")
-set(LIBTS_PACKAGE_PATH "${LIBTS_INSTALL_PATH}/lib/cmake" CACHE PATH "libts CMake package directory")
-set(LIBTS_SOURCE_DIR "${TS_ROOT}/deployments/libts/${TS_ENV}" CACHE PATH "libts source directory")
-set(LIBTS_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/_deps/libts-build" CACHE PATH "libts binary directory")
-
-file(MAKE_DIRECTORY ${LIBTS_BINARY_DIR})
-
-#Configure the library
-execute_process(COMMAND
-	${CMAKE_COMMAND}
-		-DCMAKE_INSTALL_PREFIX=${LIBTS_INSTALL_PATH}
-		-GUnix\ Makefiles
-		-S ${LIBTS_SOURCE_DIR}
-		-B ${LIBTS_BINARY_DIR}
-	RESULT_VARIABLE _exec_error
-)
-
-if (_exec_error)
-	message(FATAL_ERROR "Configuration step of libts failed with ${_exec_error}.")
-endif()
-
-#Build the library
-execute_process(COMMAND
-	${CMAKE_COMMAND} --build ${LIBTS_BINARY_DIR} --parallel ${PROCESSOR_COUNT} --target install
-	RESULT_VARIABLE _exec_error
-)
-
-if (_exec_error)
-	message(FATAL_ERROR "Build step of libts failed with ${_exec_error}.")
-endif()
-
-# Import the built library
-include(${LIBTS_INSTALL_PATH}/${TS_ENV}/lib/cmake/libts_targets.cmake)
-add_library(libts SHARED IMPORTED)
-set_property(TARGET libts PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${LIBTS_INSTALL_PATH}/${TS_ENV}/include")
-set_property(TARGET libts PROPERTY IMPORTED_LOCATION "${LIBTS_INSTALL_PATH}/${TS_ENV}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}ts${CMAKE_SHARED_LIBRARY_SUFFIX}")
diff --git a/deployments/libts/libts.cmake b/deployments/libts/libts.cmake
index 6463ca1..bcae82b 100644
--- a/deployments/libts/libts.cmake
+++ b/deployments/libts/libts.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -22,8 +22,10 @@
 unset(_minor)
 unset(_patch)
 
+add_library(libts::ts ALIAS ts)
+
 #-------------------------------------------------------------------------------
-#  Components that are common accross all deployments
+#  Components that are common across all deployments
 #
 #-------------------------------------------------------------------------------
 add_components(
@@ -53,20 +55,9 @@
 #-------------------------------------------------------------------------------
 include(${TS_ROOT}/tools/cmake/common/ExportLibrary.cmake REQUIRED)
 
-# Select public header files to export
-get_property(_rpc_caller_public_header_files TARGET ts
-	PROPERTY RPC_CALLER_PUBLIC_HEADER_FILES
-)
-
-get_property(_service_locator_public_header_files TARGET ts
-	PROPERTY SERVICE_LOCATOR_PUBLIC_HEADER_FILES
-)
-
 # Exports library information in preparation for install
 export_library(
 	TARGET "ts"
 	LIB_NAME "libts"
-	INTERFACE_FILES
-		${_rpc_caller_public_header_files}
-		${_service_locator_public_header_files}
+	PKG_CONFIG_FILE "${CMAKE_CURRENT_LIST_DIR}/libtsConfig.cmake.in"
 )
diff --git a/deployments/libts/libtsConfig.cmake.in b/deployments/libts/libtsConfig.cmake.in
new file mode 100644
index 0000000..4860135
--- /dev/null
+++ b/deployments/libts/libtsConfig.cmake.in
@@ -0,0 +1,10 @@
+#
+# Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+@PACKAGE_INIT@
+
+include("${CMAKE_CURRENT_LIST_DIR}/libtsTargets.cmake")
+
diff --git a/deployments/platform-inspect/platform-inspect.cmake b/deployments/platform-inspect/platform-inspect.cmake
index ef4ba4b..b1b316d 100644
--- a/deployments/platform-inspect/platform-inspect.cmake
+++ b/deployments/platform-inspect/platform-inspect.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -12,14 +12,14 @@
 
 #-------------------------------------------------------------------------------
 #  Use libts for locating and accessing trusted services. An appropriate version
-#  of libts will be imported for the enviroment in which platform-inspect is
+#  of libts will be imported for the environment in which platform-inspect is
 #  built.
 #-------------------------------------------------------------------------------
 include(${TS_ROOT}/deployments/libts/libts-import.cmake)
-target_link_libraries(platform-inspect PRIVATE libts)
+target_link_libraries(platform-inspect PRIVATE libts::ts)
 
 #-------------------------------------------------------------------------------
-#  Components that are common accross all deployments
+#  Components that are common across all deployments
 #
 #-------------------------------------------------------------------------------
 add_components(
diff --git a/deployments/psa-api-test/psa-api-test.cmake b/deployments/psa-api-test/psa-api-test.cmake
index d58620f..5c3469c 100644
--- a/deployments/psa-api-test/psa-api-test.cmake
+++ b/deployments/psa-api-test/psa-api-test.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -12,14 +12,14 @@
 
 #-------------------------------------------------------------------------------
 #  Use libts for locating and accessing services. An appropriate version of
-#  libts will be imported for the enviroment in which service tests are
+#  libts will be imported for the environment in which service tests are
 #  deployed.
 #-------------------------------------------------------------------------------
 include(${TS_ROOT}/deployments/libts/libts-import.cmake)
-target_link_libraries(${PROJECT_NAME} PRIVATE libts)
+target_link_libraries(${PROJECT_NAME} PRIVATE libts::ts)
 
 #-------------------------------------------------------------------------------
-#  Components that are common accross all deployments
+#  Components that are common across all deployments
 #
 #-------------------------------------------------------------------------------
 add_components(
diff --git a/deployments/ts-demo/ts-demo.cmake b/deployments/ts-demo/ts-demo.cmake
index 3e7cca0..9fd8585 100644
--- a/deployments/ts-demo/ts-demo.cmake
+++ b/deployments/ts-demo/ts-demo.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -13,11 +13,11 @@
 
 #-------------------------------------------------------------------------------
 #  Use libts for locating and accessing services. An appropriate version of
-#  libts will be imported for the enviroment in which service tests are
+#  libts will be imported for the environment in which service tests are
 #  deployed.
 #-------------------------------------------------------------------------------
 include(${TS_ROOT}/deployments/libts/libts-import.cmake)
-target_link_libraries(ts-demo PRIVATE libts)
+target_link_libraries(ts-demo PRIVATE libts::ts)
 
 #-------------------------------------------------------------------------------
 #  Common main for all deployments
@@ -28,7 +28,7 @@
 )
 
 #-------------------------------------------------------------------------------
-#  Components that are common accross all deployments
+#  Components that are common across all deployments
 #
 #-------------------------------------------------------------------------------
 add_components(
diff --git a/deployments/ts-remote-test/ts-remote-test.cmake b/deployments/ts-remote-test/ts-remote-test.cmake
index 0f35bb2..c310445 100644
--- a/deployments/ts-remote-test/ts-remote-test.cmake
+++ b/deployments/ts-remote-test/ts-remote-test.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -13,11 +13,11 @@
 
 #-------------------------------------------------------------------------------
 #  Use libts for locating and accessing services. An appropriate version of
-#  libts will be imported for the enviroment in which tests are
+#  libts will be imported for the environment in which tests are
 #  deployed.
 #-------------------------------------------------------------------------------
 include(${TS_ROOT}/deployments/libts/libts-import.cmake)
-target_link_libraries(ts-remote-test PRIVATE libts)
+target_link_libraries(ts-remote-test PRIVATE libts::ts)
 
 #-------------------------------------------------------------------------------
 #  Common main for all deployments
@@ -28,7 +28,7 @@
 )
 
 #-------------------------------------------------------------------------------
-#  Components that are common accross all deployments
+#  Components that are common across all deployments
 #
 #-------------------------------------------------------------------------------
 add_components(
diff --git a/deployments/ts-service-test/ts-service-test.cmake b/deployments/ts-service-test/ts-service-test.cmake
index 4a8c59c..3e33757 100644
--- a/deployments/ts-service-test/ts-service-test.cmake
+++ b/deployments/ts-service-test/ts-service-test.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -8,19 +8,19 @@
 #-------------------------------------------------------------------------------
 #  The base build file shared between deployments of 'ts-service-test' for
 #  different environments.  Used for running end-to-end service-level tests
-#  where test cases excerise trusted service client interfaces.
+#  where test cases exercise trusted service client interfaces.
 #-------------------------------------------------------------------------------
 
 #-------------------------------------------------------------------------------
 #  Use libts for locating and accessing services. An appropriate version of
-#  libts will be imported for the enviroment in which service tests are
+#  libts will be imported for the environment in which service tests are
 #  deployed.
 #-------------------------------------------------------------------------------
 include(${TS_ROOT}/deployments/libts/libts-import.cmake)
-target_link_libraries(ts-service-test PRIVATE libts)
+target_link_libraries(ts-service-test PRIVATE libts::ts)
 
 #-------------------------------------------------------------------------------
-#  Components that are common accross all deployments
+#  Components that are common across all deployments
 #
 #-------------------------------------------------------------------------------
 add_components(
diff --git a/deployments/uefi-test/uefi-test.cmake b/deployments/uefi-test/uefi-test.cmake
index ea678d0..2f47891 100644
--- a/deployments/uefi-test/uefi-test.cmake
+++ b/deployments/uefi-test/uefi-test.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -18,7 +18,7 @@
 #  deployed.
 #-------------------------------------------------------------------------------
 include(${TS_ROOT}/deployments/libts/libts-import.cmake)
-target_link_libraries(uefi-test PRIVATE libts)
+target_link_libraries(uefi-test PRIVATE libts::ts)
 
 #-------------------------------------------------------------------------------
 #  Components that are common accross all deployments