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/components/messaging/ffa/libsp/component.cmake b/components/messaging/ffa/libsp/component.cmake
index a21c630..64e8814 100644
--- a/components/messaging/ffa/libsp/component.cmake
+++ b/components/messaging/ffa/libsp/component.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
 #
@@ -21,7 +21,7 @@
 	"${CMAKE_CURRENT_LIST_DIR}/sp_rxtx.c"
 	)
 
-set_property(TARGET ${TGT} PROPERTY PUBLIC_HEADER
+set_property(TARGET ${TGT} APPEND PROPERTY PUBLIC_HEADER
 	${CMAKE_CURRENT_LIST_DIR}/include/ffa_api.h
 	${CMAKE_CURRENT_LIST_DIR}/include/ffa_api_defines.h
 	${CMAKE_CURRENT_LIST_DIR}/include/ffa_api_types.h
@@ -49,5 +49,5 @@
 target_include_directories(${TGT}
 	 PUBLIC
 		"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
-		"$<INSTALL_INTERFACE:include>"
+		"$<INSTALL_INTERFACE:${TS_ENV}/include>"
 	)
diff --git a/components/rpc/common/interface/component.cmake b/components/rpc/common/interface/component.cmake
index d567602..e4b2477 100644
--- a/components/rpc/common/interface/component.cmake
+++ b/components/rpc/common/interface/component.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2020, 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,11 +8,12 @@
 	message(FATAL_ERROR "mandatory parameter TGT is not defined.")
 endif()
 
-set_property(TARGET ${TGT} PROPERTY RPC_CALLER_PUBLIC_HEADER_FILES
+set_property(TARGET ${TGT} APPEND PROPERTY PUBLIC_HEADER
 	"${CMAKE_CURRENT_LIST_DIR}/rpc_caller.h"
 	"${CMAKE_CURRENT_LIST_DIR}/rpc_status.h"
 	)
 
 target_include_directories(${TGT} PUBLIC
-	"${CMAKE_CURRENT_LIST_DIR}"
+	"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>"
+	"$<INSTALL_INTERFACE:${TS_ENV}/include>"
 	)
diff --git a/components/service/locator/interface/component.cmake b/components/service/locator/interface/component.cmake
index b5aefa3..84a4d75 100644
--- a/components/service/locator/interface/component.cmake
+++ b/components/service/locator/interface/component.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2020, 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,10 +8,11 @@
 	message(FATAL_ERROR "mandatory parameter TGT is not defined.")
 endif()
 
-set_property(TARGET ${TGT} PROPERTY SERVICE_LOCATOR_PUBLIC_HEADER_FILES
+set_property(TARGET ${TGT} APPEND PROPERTY PUBLIC_HEADER
 	"${CMAKE_CURRENT_LIST_DIR}/service_locator.h"
 	)
 
 target_include_directories(${TGT} PUBLIC
-	"${CMAKE_CURRENT_LIST_DIR}"
+	"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>"
+	"$<INSTALL_INTERFACE:${TS_ENV}/include>"
 	)
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
diff --git a/tools/cmake/common/ExportLibrary.cmake b/tools/cmake/common/ExportLibrary.cmake
index fed4e75..c171c5b 100644
--- a/tools/cmake/common/ExportLibrary.cmake
+++ b/tools/cmake/common/ExportLibrary.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
 #
@@ -10,7 +10,7 @@
 
 	.. code:: cmake
 
-		export_library(TARGET LIB_NAME INTERFACE_FILES)
+		export_library(TARGET <target name> LIB_NAME <library name> PKG_CONFIG_FILE <file name>)
 
 	INPUTS:
 
@@ -20,23 +20,30 @@
 	``LIB_NAME``
 	The name of the library.
 
-	``INTERFACE_FILES``
-	List of header files to declare the library's public interface.
+	``PKG_CONFIG_FILE``
+	Name of the package configuration file to generate.
 
 #]===]
 function(export_library)
 	set(options  )
-	set(oneValueArgs TARGET LIB_NAME)
-	set(multiValueArgs INTERFACE_FILES)
+	set(oneValueArgs TARGET LIB_NAME PKG_CONFIG_FILE)
+	set(multiValueArgs)
 	cmake_parse_arguments(MY_PARAMS "${options}" "${oneValueArgs}"
 						"${multiValueArgs}" ${ARGN} )
 
-	if(NOT DEFINED MY_PARAMS_TARGET)
-		message(FATAL_ERROR "export_library: mandatory parameter TARGET not defined!")
+	foreach(_param IN ITEMS MY_PARAMS_TARGET MY_PARAMS_LIB_NAME MY_PARAMS_PKG_CONFIG_FILE)
+		if(NOT DEFINED ${_param})
+			list(APPEND _miss_params "${_param}" )
+		endif()
+	endforeach()
+
+	if (_miss_params)
+		string(REPLACE ";" ", " _miss_params "${_miss_params}")
+		message(FATAL_ERROR "export_library: mandatory parameter(s) ${_miss_params} not defined!")
 	endif()
-	if(NOT DEFINED MY_PARAMS_LIB_NAME)
-		message(FATAL_ERROR "export_library: mandatory parameter LIB_NAME not defined!")
-	endif()
+
+
+	string(TOLOWER "${MY_PARAMS_LIB_NAME}" LC_LIB_NAME)
 
 	# Set default install location if none specified
 	if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
@@ -55,18 +62,56 @@
 			DESTINATION ${TS_ENV}/include
 	)
 
-	# Install library header files files
-	install(
-		FILES ${MY_PARAMS_INTERFACE_FILES}
-		DESTINATION ${TS_ENV}/include
+	# Create targets file.
+	export(
+		EXPORT
+			${MY_PARAMS_LIB_NAME}_targets
+		FILE
+			"${CMAKE_CURRENT_BINARY_DIR}/${MY_PARAMS_LIB_NAME}Targets.cmake"
+		NAMESPACE
+			${MY_PARAMS_LIB_NAME}::
+	)
+
+	# Create a config file package.
+	include(CMakePackageConfigHelpers)
+	get_target_property(_ver ${MY_PARAMS_TARGET} VERSION)
+	write_basic_package_version_file(
+		"${CMAKE_CURRENT_BINARY_DIR}/${LC_LIB_NAME}ConfigVersion.cmake"
+		VERSION "${_ver}"
+		COMPATIBILITY SameMajorVersion
+	)
+
+	# Finalize config file.
+	# Config package location relative to install root.
+	set(ConfigPackageLocation ${TS_ENV}/lib/cmake/${MY_PARAMS_LIB_NAME})
+
+	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}"
+		INSTALL_DESTINATION
+			${ConfigPackageLocation}
 	)
 
 	# Install the export details
 	install(
 		EXPORT ${MY_PARAMS_LIB_NAME}_targets
-		FILE ${MY_PARAMS_LIB_NAME}_targets.cmake
+		FILE ${MY_PARAMS_LIB_NAME}Targets.cmake
 		NAMESPACE ${MY_PARAMS_LIB_NAME}::
-		DESTINATION ${TS_ENV}/lib/cmake
+		DESTINATION ${ConfigPackageLocation}
 		COMPONENT ${MY_PARAMS_LIB_NAME}
 	)
+
+
+	# install config and version files
+	install(
+		FILES
+			"${_configured_pkgcfg_name}"
+			"${CMAKE_CURRENT_BINARY_DIR}/${LC_LIB_NAME}ConfigVersion.cmake"
+		DESTINATION
+			${ConfigPackageLocation}
+		COMPONENT
+			${MY_PARAMS_LIB_NAME}
+	)
 endfunction()