Modify external dependencies to use LazyFetch

Refactor external dependencies use the new module. This gives more
control over how external components is made available and allows
the environment to use pre-build binaries or pre-fetched content.
Moreover, passing parameters to external CMake build systems is made
more robust by using initial cache files.

Signed-off-by: Benedek Tomasik <benedek.tomasik@arm.com>
Signed-off-by: Gyorgy Szing <Gyorgy.Szing@arm.com>
Change-Id: I85d1990fc7697847307b0ca3a91052b35423d823
diff --git a/external/CppUTest/CppUTest.cmake b/external/CppUTest/CppUTest.cmake
index c18f3e3..061e030 100644
--- a/external/CppUTest/CppUTest.cmake
+++ b/external/CppUTest/CppUTest.cmake
@@ -1,98 +1,37 @@
 #-------------------------------------------------------------------------------
-# 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
 #
 #-------------------------------------------------------------------------------
 
+
 set(CPPUTEST_URL "https://github.com/cpputest/cpputest.git" CACHE STRING "CppUTest repository URL")
 set(CPPUTEST_REFSPEC "v3.8" CACHE STRING "CppUTest git refspec")
-set(CPPUTEST_INSTALL_PATH ${CMAKE_CURRENT_BINARY_DIR}/CppUTest_install CACHE PATH "CppUTest installation directory")
+set(CPPUTEST_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/CppUTest_install CACHE PATH "CppUTest installation directory")
+set(CPPUTEST_SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/_deps/cpputest-src CACHE PATH "CppUTest source directory")
 
-include(FetchContent)
-
-# Checking git
-find_program(GIT_COMMAND "git")
-if (NOT GIT_COMMAND)
-	message(FATAL_ERROR "Please install git")
-endif()
-
-# Fetching CppUTest
-FetchContent_Declare(
-	cpputest
+set(GIT_OPTIONS
 	GIT_REPOSITORY ${CPPUTEST_URL}
 	GIT_TAG ${CPPUTEST_REFSPEC}
 	GIT_SHALLOW TRUE
 	PATCH_COMMAND git stash
-		COMMAND git apply ${CMAKE_CURRENT_LIST_DIR}/cpputest-cmake-fix.patch
-)
-
-# FetchContent_GetProperties exports cpputest_SOURCE_DIR and cpputest_BINARY_DIR variables
-FetchContent_GetProperties(cpputest)
-if(NOT cpputest_POPULATED)
-	message(STATUS "Fetching CppUTest")
-	FetchContent_Populate(cpputest)
-endif()
-
-# Build and install CppUTest configuration time. This makes us able to use CppUTest as a CMake package.
-# Memory leak detection is turned off to avoid conflict with memcheck.
-if(NOT CMAKE_CROSSCOMPILING)
-	execute_process(COMMAND
-		${CMAKE_COMMAND}
-			-DMEMORY_LEAK_DETECTION=OFF
-			-DLONGLONG=ON
-			-DC++11=ON
-			-DCMAKE_INSTALL_PREFIX=${CPPUTEST_INSTALL_PATH}
-			-DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
-			-G${CMAKE_GENERATOR}
-			${cpputest_SOURCE_DIR}
-		WORKING_DIRECTORY
-			${cpputest_BINARY_DIR}
-			RESULT_VARIABLE
-				_exec_error
+	COMMAND git apply ${CMAKE_CURRENT_LIST_DIR}/cpputest-cmake-fix.patch
 	)
-else()
-	execute_process(COMMAND
-	${CMAKE_COMMAND}
-		-DMEMORY_LEAK_DETECTION=OFF
-		-DLONGLONG=ON
-		-DC++11=ON
-		-DCMAKE_INSTALL_PREFIX=${CPPUTEST_INSTALL_PATH}
-		-DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
-		-DTESTS=OFF
-		-DEXTENSIONS=OFF
-		-DHAVE_FORK=OFF
-		-DCPP_PLATFORM=armcc
-		-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY
-		-G${CMAKE_GENERATOR}
-		${cpputest_SOURCE_DIR}
-	WORKING_DIRECTORY
-		${cpputest_BINARY_DIR}
-	RESULT_VARIABLE
-		_exec_error
-	)
-endif()
-if (NOT _exec_error EQUAL 0)
-	message(FATAL_ERROR "Configuriong CppUTest build failed.")
-endif()
-execute_process(COMMAND
-	${CMAKE_COMMAND}
-		--build ${cpputest_BINARY_DIR}
-		-- install -j8
-	RESULT_VARIABLE
-		_exec_error
-	)
-if (NOT _exec_error EQUAL 0)
-	message(FATAL_ERROR "Building CppUTest failed.")
-endif()
 
-# Finding CppUTest package. CMake will check [package name]_DIR variable.
-set(CppUTest_DIR ${CPPUTEST_INSTALL_PATH}/lib/CppUTest/cmake CACHE PATH "CppUTest package location" FORCE)
-find_package(CppUTest CONFIG REQUIRED NO_DEFAULT_PATH PATHS ${CppUTest_DIR})
+include(${TS_ROOT}/tools/cmake/common/LazyFetch.cmake REQUIRED)
+LazyFetch_MakeAvailable(DEP_NAME CppUTest
+	FETCH_OPTIONS "${GIT_OPTIONS}"
+	INSTALL_DIR ${CPPUTEST_INSTALL_DIR}
+	PACKAGE_DIR ${CPPUTEST_INSTALL_DIR}/lib/CppUTest/cmake
+	CACHE_FILE "${TS_ROOT}/external/CppUTest/cpputest-init-cache.cmake.in"
+	SOURCE_DIR ${CPPUTEST_SOURCE_DIR}
+	)
+
 # CppUTest package files do not set include path properties on the targets.
 # Fix this here.
 foreach(_cpputest_target IN LISTS CppUTest_LIBRARIES)
-	if (TARGET  ${_cpputest_target})
+	if (TARGET	${_cpputest_target})
 		target_include_directories(${_cpputest_target} INTERFACE ${CppUTest_INCLUDE_DIRS})
 		target_compile_features(${_cpputest_target} INTERFACE cxx_std_11)
 	endif()
diff --git a/external/CppUTest/cpputest-init-cache.cmake.in b/external/CppUTest/cpputest-init-cache.cmake.in
new file mode 100644
index 0000000..b24f5de
--- /dev/null
+++ b/external/CppUTest/cpputest-init-cache.cmake.in
@@ -0,0 +1,21 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+set(CMAKE_INSTALL_PREFIX "@BUILD_INSTALL_DIR@" CACHE STRING "")
+set(CMAKE_TOOLCHAIN_FILE "@TS_EXTERNAL_LIB_TOOLCHAIN_FILE@" CACHE STRING "")
+set(MEMORY_LEAK_DETECTION OFF CACHE BOOL "")
+set(LONGLONG ON CACHE BOOL "")
+set(C++11 ON CACHE BOOL "")
+
+string(TOUPPER @CMAKE_CROSSCOMPILING@ CMAKE_CROSSCOMPILING) # CMake expects TRUE
+if (CMAKE_CROSSCOMPILING)
+	set(TESTS OFF CACHE BOOL "")
+	set(EXTENSIONS OFF BOOL "")
+	set(HAVE_FORK OFF CACHE BOOL "")
+	set(CPP_PLATFORM "armcc" CACHE STRING "")
+	set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY CACHE BOOL "")
+endif()
diff --git a/external/LinuxFFAUserShim/LinuxFFAUserShim.cmake b/external/LinuxFFAUserShim/LinuxFFAUserShim.cmake
index c815e0d..dbc3e28 100644
--- a/external/LinuxFFAUserShim/LinuxFFAUserShim.cmake
+++ b/external/LinuxFFAUserShim/LinuxFFAUserShim.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,47 +8,39 @@
 # Find Linux FF-A user space shim repo location.
 # It contains a kernel module which exposes FF-A operations to user space using DebugFS.
 
-# If a CMake variable exists, use it as is.
-# If not, try to copy the value from the environment.
-# If neither is present, try to download.
-if(NOT DEFINED LINUX_FFA_USER_SHIM_DIR)
-	if(DEFINED ENV{LINUX_FFA_USER_SHIM_DIR})
-		set(LINUX_FFA_USER_SHIM_DIR $ENV{LINUX_FFA_USER_SHIM_DIR}
-				CACHE STRING "Linux FF-A user space shim dir")
-	else()
-		set(LINUX_FFA_USER_SHIM_URL "https://git.gitlab.arm.com/linux-arm/linux-trusted-services.git"
-				CACHE STRING "Linux FF-A user space shim repository URL")
-		set(LINUX_FFA_USER_SHIM_REFSPEC "v4.0.0"
-				CACHE STRING "Linux FF-A user space shim git refspec")
 
-		find_program(GIT_COMMAND "git")
-		if (NOT GIT_COMMAND)
-			message(FATAL_ERROR "Please install git")
-		endif()
+set(LINUX_FFA_USER_SHIM_URL "https://git.gitlab.arm.com/linux-arm/linux-trusted-services.git"
+	CACHE STRING "Linux FF-A user space shim repository URL")
+set(LINUX_FFA_USER_SHIM_REFSPEC "v4.0.0"
+	CACHE STRING "Linux FF-A user space shim git refspec")
 
-		include(FetchContent)
-		FetchContent_Declare(linux_ffa_user_shim
-			GIT_REPOSITORY ${LINUX_FFA_USER_SHIM_URL}
-			GIT_TAG ${LINUX_FFA_USER_SHIM_REFSPEC}
-			GIT_SHALLOW TRUE
-		)
+set(LINUX_FFA_USER_SHIM_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/_deps/linux_ffa_user_shim-src"
+	CACHE PATH "Location of Linux driver source.")
 
-		# FetchContent_GetProperties exports <name>_SOURCE_DIR and <name>_BINARY_DIR variables
-		FetchContent_GetProperties(linux_ffa_user_shim)
-		if(NOT linux_ffa_user_shim_POPULATED)
-			message(STATUS "Fetching Linux FF-A user space shim")
-			FetchContent_Populate(linux_ffa_user_shim)
-		endif()
-
-		set(LINUX_FFA_USER_SHIM_DIR ${linux_ffa_user_shim_SOURCE_DIR}
-				CACHE STRING "Linux FF-A user space shim dir")
-	endif()
+if (DEFINED ENV{LINUX_FFA_USER_SHIM_SOURCE_DIR})
+	set(LINUX_FFA_USER_SHIM_SOURCE_DIR $ENV{LINUX_FFA_USER_SHIM_SOURCE_DIR}
+		CACHE PATH "Location of Linux driver source." FORCE)
 endif()
 
+set(GIT_OPTIONS
+	GIT_REPOSITORY ${LINUX_FFA_USER_SHIM_URL}
+	GIT_TAG ${LINUX_FFA_USER_SHIM_REFSPEC}
+	GIT_SHALLOW TRUE
+	)
+	include(${TS_ROOT}/tools/cmake/common/LazyFetch.cmake REQUIRED)
+	LazyFetch_MakeAvailable(
+		DEP_NAME linux_ffa_user_shim
+		FETCH_OPTIONS "${GIT_OPTIONS}"
+		SOURCE_DIR ${LINUX_FFA_USER_SHIM_SOURCE_DIR}
+	)
+
 find_path(LINUX_FFA_USER_SHIM_INCLUDE_DIR
 	NAMES arm_ffa_user.h
-	PATHS ${LINUX_FFA_USER_SHIM_DIR}
+	PATHS ${LINUX_FFA_USER_SHIM_SOURCE_DIR}
 	NO_DEFAULT_PATH
 	REQUIRED
 	DOC "Linux FF-A user space shim include directory"
 )
+
+set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
+	"${LINUX_FFA_USER_SHIM_INCLUDE_DIR}/arm_ffa_user.h")
diff --git a/external/firmware_test_builder/FirmwareTestBuilder.cmake b/external/firmware_test_builder/FirmwareTestBuilder.cmake
index 4a4cf08..798e417 100644
--- a/external/firmware_test_builder/FirmwareTestBuilder.cmake
+++ b/external/firmware_test_builder/FirmwareTestBuilder.cmake
@@ -1,34 +1,24 @@
 #-------------------------------------------------------------------------------
-# 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
 #
 #-------------------------------------------------------------------------------
 
-include(FetchContent)
 
 set(FIRMWARE_TEST_BUILDER_URL "https://git.trustedfirmware.org/TS/trusted-services.git" CACHE STRING "firmware-test-builder repository URL")
 set(FIRMWARE_TEST_BUILDER_REFSPEC "topics/fwtb" CACHE STRING "firmware-test-builder git refspec")
 
-# Checking git
-find_program(GIT_COMMAND "git")
-if (NOT GIT_COMMAND)
-	message(FATAL_ERROR "Please install git")
-endif()
-
-# Fetching firmware-test-builder
-FetchContent_Declare(
-	firmware_test_builder
+set(GIT_OPTIONS
 	GIT_REPOSITORY ${FIRMWARE_TEST_BUILDER_URL}
 	GIT_TAG ${FIRMWARE_TEST_BUILDER_REFSPEC}
-	GIT_SHALLOW TRUE
-)
+	GIT_SHALLOW TRUE)
 
-FetchContent_GetProperties(firmware_test_builder)
-if(NOT firmware_test_builder_POPULATED)
-	message(STATUS "Fetching Firmware Test Builder")
-	FetchContent_Populate(firmware_test_builder)
-endif()
+include(${TS_ROOT}/tools/cmake/common/LazyFetch.cmake REQUIRED)
+LazyFetch_MakeAvailable(
+	DEP_NAME firmware_test_builder
+	FETCH_OPTIONS "${GIT_OPTIONS}"
+	)
 
 # Appending firmware-test-builder's CMake directory to CMake module path
-list(APPEND CMAKE_MODULE_PATH ${firmware_test_builder_SOURCE_DIR}/cmake)
\ No newline at end of file
+list(APPEND CMAKE_MODULE_PATH ${firmware_test_builder_SOURCE_DIR}/cmake)
diff --git a/external/nanopb/nanopb-init-cache.cmake.in b/external/nanopb/nanopb-init-cache.cmake.in
new file mode 100644
index 0000000..3f34ca9
--- /dev/null
+++ b/external/nanopb/nanopb-init-cache.cmake.in
@@ -0,0 +1,26 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+set(CMAKE_INSTALL_PREFIX @BUILD_INSTALL_DIR@ CACHE STRING "")
+set(CMAKE_TOOLCHAIN_FILE @TS_EXTERNAL_LIB_TOOLCHAIN_FILE@ CACHE STRING "")
+set(BUILD_SHARED_LIBS Off CACHE BOOL "")
+set(BUILD_STATIC_LIBS On CACHE BOOL "")
+set(nanopb_BUILD_RUNTIME On CACHE BOOL "")
+set(nanopb_BUILD_GENERATOR On CACHE BOOL "")
+set(nanopb_MSVC_STATIC_RUNTIME Off BOOL "")
+set(nanopb_PROTOC_PATH ${CMAKE_SOURCE_DIR}/generator/protoc CACHE STRING "")
+
+string(TOUPPER @CMAKE_CROSSCOMPILING@ CMAKE_CROSSCOMPILING) # CMake expects TRUE
+if (CMAKE_CROSSCOMPILING)
+	set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY CACHE STRING "")
+endif()
+
+set(NANOPB_EXTERNAL_INCLUDE_PATHS "@NANOPB_EXTERNAL_INCLUDE_PATHS@" CACHE STRING "")
+
+if (NOT "${NANOPB_EXTERNAL_INCLUDE_PATHS}" STREQUAL "")
+	include_directories(${NANOPB_EXTERNAL_INCLUDE_PATHS})
+endif()
diff --git a/external/nanopb/nanopb.cmake b/external/nanopb/nanopb.cmake
index 03e4596..2eead70 100644
--- a/external/nanopb/nanopb.cmake
+++ b/external/nanopb/nanopb.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
 #
@@ -24,22 +24,16 @@
 
 #]===]
 
-# 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.")
-endif()
-
 #### Get the dependency
 
-set(NANOPB_URL "https://github.com/nanopb/nanopb.git" CACHE STRING "nanopb repository URL")
-set(NANOPB_REFSPEC "nanopb-0.4.2" CACHE STRING "nanopb git refspec")
-set(NANOPB_INSTALL_PATH "${CMAKE_CURRENT_BINARY_DIR}/nanopb_install" CACHE PATH "nanopb installation directory")
-set(NANOPB_PACKAGE_PATH "${NANOPB_INSTALL_PATH}/libnanopb/cmake" CACHE PATH "nanopb CMake package directory")
-
-include(FetchContent)
+set(NANOPB_URL "https://github.com/nanopb/nanopb.git"
+		CACHE STRING "nanopb repository URL")
+set(NANOPB_REFSPEC "nanopb-0.4.2"
+		CACHE STRING "nanopb git refspec")
+set(NANOPB_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/_deps/nanopb-src"
+		CACHE PATH "nanopb source-code")
+set(NANOPB_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/nanopb_install"
+		CACHE PATH "nanopb installation directory")
 
 # Checking git
 find_program(GIT_COMMAND "git")
@@ -47,101 +41,33 @@
 	message(FATAL_ERROR "Please install git")
 endif()
 
-# Fetching nanopb
-FetchContent_Declare(
-	nanopb
+set(GIT_OPTIONS
 	GIT_REPOSITORY ${NANOPB_URL}
 	GIT_TAG ${NANOPB_REFSPEC}
 	GIT_SHALLOW TRUE
 	#See the .patch file for details on why it is needed.
 	PATCH_COMMAND git stash
 		COMMAND git apply ${CMAKE_CURRENT_LIST_DIR}/fix-pyhon-name.patch
-)
-
-# FetchContent_GetProperties exports nanopb_SOURCE_DIR and nanopb_BINARY_DIR variables
-FetchContent_GetProperties(nanopb)
-if(NOT nanopb_POPULATED)
-	message(STATUS "Fetching nanopb")
-	FetchContent_Populate(nanopb)
-endif()
-
-#### Build the runtime and the generator.
-
-# Pass extra include paths to nanopb build uning CFLAGS if needed
-if (NOT "${NANOPB_EXTERNAL_INCLUDE_PATHS}" STREQUAL "")
-	string(REPLACE ";" "-I " NANOPB_EXTERNAL_INCLUDE_PATHS "${NANOPB_EXTERNAL_INCLUDE_PATHS}")
-	set(_SAVED_CFLAGS $ENV{CFLAGS})
-	set(ENV{CFLAGS} "$ENV{CFLAGS} -I ${NANOPB_EXTERNAL_INCLUDE_PATHS}")
-endif()
-
-if( NOT CMAKE_CROSSCOMPILING)
-	execute_process(COMMAND
-		${CMAKE_COMMAND}
-				-DBUILD_SHARED_LIBS=Off
-				-DBUILD_STATIC_LIBS=On
-				-Dnanopb_BUILD_RUNTIME=On
-				-Dnanopb_BUILD_GENERATOR=On
-				-Dnanopb_PROTOC_PATH=${nanopb_SOURCE_DIR}/generator/protoc
-				-Dnanopb_MSVC_STATIC_RUNTIME=Off
-				-DCMAKE_INSTALL_PREFIX=${NANOPB_INSTALL_PATH}
-				-DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
-				-GUnix\ Makefiles
-				${nanopb_SOURCE_DIR}
-			WORKING_DIRECTORY
-				${nanopb_BINARY_DIR}
-			RESULT_VARIABLE _exec_error
-	)
-else()
-	execute_process(COMMAND
-		${CMAKE_COMMAND}
-				-DBUILD_SHARED_LIBS=Off
-				-DBUILD_STATIC_LIBS=On
-				-Dnanopb_BUILD_RUNTIME=On
-				-Dnanopb_BUILD_GENERATOR=On
-				-Dnanopb_PROTOC_PATH=${nanopb_SOURCE_DIR}/generator/protoc
-				-Dnanopb_MSVC_STATIC_RUNTIME=Off
-				-DCMAKE_INSTALL_PREFIX=${NANOPB_INSTALL_PATH}
-				-DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
-				-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY
-				-GUnix\ Makefiles
-				${nanopb_SOURCE_DIR}
-			WORKING_DIRECTORY
-				${nanopb_BINARY_DIR}
-			RESULT_VARIABLE _exec_error
-	)
-endif()
+  )
 
 if (NOT "${NANOPB_EXTERNAL_INCLUDE_PATHS}" STREQUAL "")
-	set($ENV{CFLAGS} ${_SAVED_CFLAGS})
-	unset(_SAVED_CFLAGS)
-	unset(NANOPB_EXTERNAL_INCLUDE_PATHS)
+	string(REPLACE ";" " " NANOPB_EXTERNAL_INCLUDE_PATHS "${NANOPB_EXTERNAL_INCLUDE_PATHS}")
 endif()
 
-if (_exec_error)
-	message(FATAL_ERROR "Configuration step of nanopb runtime failed with ${_exec_error}.")
-endif()
+include(${TS_ROOT}/tools/cmake/common/LazyFetch.cmake REQUIRED)
+LazyFetch_MakeAvailable(DEP_NAME nanopb
+	FETCH_OPTIONS ${GIT_OPTIONS}
+	INSTALL_DIR ${NANOPB_INSTALL_DIR}
+	PACKAGE_DIR ${NANOPB_INSTALL_DIR}
+	CACHE_FILE "${TS_ROOT}/external/nanopb/nanopb-init-cache.cmake.in"
+	SOURCE_DIR "${NANOPB_SOURCE_DIR}"
+  )
 
-execute_process(COMMAND
-		${CMAKE_COMMAND} --build ${nanopb_BINARY_DIR} --parallel ${PROCESSOR_COUNT} --target install
-		RESULT_VARIABLE _exec_error
-	)
-if (_exec_error)
-	message(FATAL_ERROR "Build step of nanopb runtime failed with ${_exec_error}.")
-endif()
-
-#### Include Nanopb runtime in the build.
-find_package(Nanopb
-			PATHS "${NANOPB_INSTALL_PATH}"
-			NO_DEFAULT_PATH
-		)
+#set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${NANOPB_INSTALL_DIR}/)
 
 #### Build access to the protobuf compiler
 #TODO: verify protoc dependencies: python3-protobuf
-find_package(Python3 COMPONENTS Interpreter)
-
-if (NOT Python3_Interpreter_FOUND)
-	message(FATAL_ERROR "Failed to find python3 interpreter.")
-endif()
+find_package(Python3 REQUIRED COMPONENTS Interpreter)
 
 find_file(NANOPB_GENERATOR_PATH
 			NAMES nanopb_generator.py
@@ -312,4 +238,4 @@
 				NAMESPACE ${PARAMS_NAMESPACE}
 				BASE_DIR ${PARAMS_BASE_DIR})
 	endforeach()
-endfunction()
\ No newline at end of file
+endfunction()
diff --git a/external/psa_arch_tests/pas-arch-test-init-cache.cmake.in b/external/psa_arch_tests/pas-arch-test-init-cache.cmake.in
new file mode 100644
index 0000000..5c63596
--- /dev/null
+++ b/external/psa_arch_tests/pas-arch-test-init-cache.cmake.in
@@ -0,0 +1,16 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+set(CMAKE_INSTALL_PREFIX "@PSA_ARCH_TESTS_INSTALL_PATH@" CACHE STRING "")
+set(CMAKE_TOOLCHAIN_FILE "@TS_EXTERNAL_LIB_TOOLCHAIN_FILE@" CACHE STRING "")
+
+set(TOOLCHAIN INHERIT CACHE STRING "")
+set(PSA_INCLUDE_PATHS "@PSA_ARCH_TESTS_EXTERNAL_INCLUDE_PATHS@"  CACHE STRING "")
+set(SUITE "@TS_ARCH_TEST_SUITE@"  CACHE STRING "")
+set(ARCH_TEST_EXTERNAL_DEFS "@PSA_ARCH_TEST_EXTERNAL_DEFS@"  CACHE STRING "")
+set(CMAKE_VERBOSE_MAKEFILE OFF CACHE BOOL "")
+set(TARGET "tgt_dev_apis_linux"  CACHE STRING "")
diff --git a/external/psa_arch_tests/psa_arch_tests.cmake b/external/psa_arch_tests/psa_arch_tests.cmake
index e8a698a..03c0376 100644
--- a/external/psa_arch_tests/psa_arch_tests.cmake
+++ b/external/psa_arch_tests/psa_arch_tests.cmake
@@ -5,30 +5,11 @@
 #
 #-------------------------------------------------------------------------------
 
-# 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.")
-endif()
-
 set(PSA_ARCH_TESTS_URL "https://github.com/ARM-software/psa-arch-tests.git" CACHE STRING "psa-arch-tests repository URL")
 set(PSA_ARCH_TESTS_REFSPEC "2a1852252a9b9af655cbe02d5d3c930952d0d798" CACHE STRING "psa-arch-tests v22.01_API1.4_ADAC_BETA")
-set(PSA_ARCH_TESTS_INSTALL_PATH "${CMAKE_CURRENT_BINARY_DIR}/psa-arch-tests_install" CACHE PATH "psa-arch-tests installation directory")
-set(PSA_ARCH_TESTS_PACKAGE_PATH "${PSA_ARCH_TESTS_INSTALL_PATH}/libpsa-arch-tests/cmake" CACHE PATH "psa-arch-tests CMake package directory")
+set(PSA_ARCH_TESTS_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/_deps/psa_arch_tests-src" CACHE PATH "psa-arch-tests source.")
 
-include(FetchContent)
-
-# Checking git
-find_program(GIT_COMMAND "git")
-if (NOT GIT_COMMAND)
-	message(FATAL_ERROR "Please install git")
-endif()
-
-# Fetching psa-arch-tests
-FetchContent_Declare(
-	psa-arch-tests
+set(GIT_OPTIONS
 	GIT_REPOSITORY ${PSA_ARCH_TESTS_URL}
 	GIT_TAG ${PSA_ARCH_TESTS_REFSPEC}
 	GIT_SHALLOW TRUE
@@ -36,58 +17,33 @@
 		COMMAND git apply ${CMAKE_CURRENT_LIST_DIR}/modify_attest_config.patch
 )
 
-# FetchContent_GetProperties exports psa-arch-tests_SOURCE_DIR and psa-arch-tests_BINARY_DIR variables
-FetchContent_GetProperties(psa-arch-tests)
-if(NOT psa-arch-tests_POPULATED)
-	message(STATUS "Fetching psa-arch-tests")
-	FetchContent_Populate(psa-arch-tests)
-endif()
-
 # Ensure list of include paths is separated correctly
-string(REPLACE ";" "\\;" PSA_ARCH_TESTS_EXTERNAL_INCLUDE_PATHS "${PSA_ARCH_TESTS_EXTERNAL_INCLUDE_PATHS}")
+#string(REPLACE ";" "\\;" PSA_ARCH_TESTS_EXTERNAL_INCLUDE_PATHS "${PSA_ARCH_TESTS_EXTERNAL_INCLUDE_PATHS}")
 
 # Ensure list of defines is separated correctly
 string(REPLACE ";" " " PSA_ARCH_TEST_EXTERNAL_DEFS "${PSA_ARCH_TEST_EXTERNAL_DEFS}")
 
-# Configure the psa-arch-test library
-execute_process(COMMAND
-	${CMAKE_COMMAND}
-			-DTOOLCHAIN=INHERIT
-			-DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
-			-DPSA_INCLUDE_PATHS=${PSA_ARCH_TESTS_EXTERNAL_INCLUDE_PATHS}
-			-DSUITE=${TS_ARCH_TEST_SUITE}
-			-DARCH_TEST_EXTERNAL_DEFS=${PSA_ARCH_TEST_EXTERNAL_DEFS}
-			-DCMAKE_VERBOSE_MAKEFILE=OFF
-			-DTARGET=tgt_dev_apis_linux
-			-GUnix\ Makefiles
-			${psa-arch-tests_SOURCE_DIR}/api-tests
-		WORKING_DIRECTORY
-			${psa-arch-tests_BINARY_DIR}
-		RESULT_VARIABLE _exec_error
-)
-
-# Build the library
-if (_exec_error)
-	message(FATAL_ERROR "Configuration step of psa-arch-tests runtime failed with ${_exec_error}.")
-endif()
-
-execute_process(COMMAND
-		${CMAKE_COMMAND} --build ${psa-arch-tests_BINARY_DIR} --parallel ${PROCESSOR_COUNT}
-		RESULT_VARIABLE _exec_error
+include(${TS_ROOT}/tools/cmake/common/LazyFetch.cmake REQUIRED)
+LazyFetch_MakeAvailable(DEP_NAME psa_arch_tests
+	FETCH_OPTIONS "${GIT_OPTIONS}"
+	SOURCE_DIR ${PSA_ARCH_TESTS_SOURCE_DIR}
+	SOURCE_SUBDIR "api-tests"
+	CACHE_FILE "${CMAKE_CURRENT_LIST_DIR}/pas-arch-test-init-cache.cmake.in"
 	)
-if (_exec_error)
-	message(FATAL_ERROR "Build step of psa-arch-tests runtime failed with ${_exec_error}.")
-endif()
 
 # Create targets for generated libraries
 add_library(test_combine STATIC IMPORTED)
 set_property(TARGET test_combine PROPERTY IMPORTED_LOCATION
-	"${psa-arch-tests_BINARY_DIR}/dev_apis/${TS_ARCH_TEST_BUILD_SUBDIR}/test_combine${CMAKE_STATIC_LIBRARY_SUFFIX}")
+	"${psa_arch_tests_BINARY_DIR}/dev_apis/${TS_ARCH_TEST_BUILD_SUBDIR}/test_combine${CMAKE_STATIC_LIBRARY_SUFFIX}")
+set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
+	"${psa_arch_tests_BINARY_DIR}/dev_apis/${TS_ARCH_TEST_BUILD_SUBDIR}/test_combine${CMAKE_STATIC_LIBRARY_SUFFIX}")
+set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
+	"${PSA_ARCH_TESTS_SOURCE_DIR}/api-tests/val/nspe/val_entry.h")
 
 add_library(val_nspe STATIC IMPORTED)
 set_property(TARGET val_nspe PROPERTY IMPORTED_LOCATION
-	"${psa-arch-tests_BINARY_DIR}/val/val_nspe${CMAKE_STATIC_LIBRARY_SUFFIX}")
+	"${psa_arch_tests_BINARY_DIR}/val/val_nspe${CMAKE_STATIC_LIBRARY_SUFFIX}")
 
 add_library(pal_nspe STATIC IMPORTED)
 set_property(TARGET pal_nspe PROPERTY IMPORTED_LOCATION
-	"${psa-arch-tests_BINARY_DIR}/platform/pal_nspe${CMAKE_STATIC_LIBRARY_SUFFIX}")
+	"${psa_arch_tests_BINARY_DIR}/platform/pal_nspe${CMAKE_STATIC_LIBRARY_SUFFIX}")
diff --git a/external/qcbor/qcbor-init-cache.cmake.in b/external/qcbor/qcbor-init-cache.cmake.in
new file mode 100644
index 0000000..28c53ec
--- /dev/null
+++ b/external/qcbor/qcbor-init-cache.cmake.in
@@ -0,0 +1,20 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+set(CMAKE_INSTALL_PREFIX "@BUILD_INSTALL_DIR@" CACHE STRING "")
+set(CMAKE_TOOLCHAIN_FILE "@TS_EXTERNAL_LIB_TOOLCHAIN_FILE@" CACHE STRING "")
+
+set(QCBOR_EXTERNAL_INCLUDE_PATHS "@QCBOR_EXTERNAL_INCLUDE_PATHS@")
+if (QCBOR_EXTERNAL_INCLUDE_PATHS)
+	set(thirdparty_inc "${QCBOR_EXTERNAL_INCLUDE_PATHS}" CACHE STRING "")
+endif()
+
+# Determine floating point configuration
+set(TS_NO_FLOAT_HW "@TS_NO_FLOAT_HW@")
+if (TS_NO_FLOAT_HW)
+	set(thirdparty_def "-DQCBOR_DISABLE_FLOAT_HW_USE" CACHE STRING "")
+endif()
diff --git a/external/qcbor/qcbor.cmake b/external/qcbor/qcbor.cmake
index 96e5dcb..f563fe2 100644
--- a/external/qcbor/qcbor.cmake
+++ b/external/qcbor/qcbor.cmake
@@ -1,36 +1,17 @@
 #-------------------------------------------------------------------------------
-# 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
 #
 # QCBOR is a library for encoding and decoding CBOR objects, as per RFC8949
 #-------------------------------------------------------------------------------
 
-# 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.")
-endif()
-
-# External component details
 set(QCBOR_URL "https://github.com/laurencelundblade/QCBOR.git" CACHE STRING "qcbor repository URL")
 set(QCBOR_REFSPEC "master" CACHE STRING "qcbor git refspec")
-set(QCBOR_INSTALL_PATH "${CMAKE_CURRENT_BINARY_DIR}/qcbor_install" CACHE PATH "qcbor installation directory")
-set(QCBOR_PACKAGE_PATH "${QCBOR_INSTALL_PATH}/libqcbor/cmake" CACHE PATH "qcbor CMake package directory")
+set(QCBOR_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/_deps/qcbor-src" CACHE PATH "qcbor installation directory")
+set(QCBOR_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/qcbor_install" CACHE PATH "qcbor installation directory")
 
-include(FetchContent)
-
-# Checking git
-find_program(GIT_COMMAND "git")
-if (NOT GIT_COMMAND)
-	message(FATAL_ERROR "Please install git")
-endif()
-
-# Fetching qcbor
-FetchContent_Declare(
-	qcbor
+set(GIT_OPTIONS
 	GIT_REPOSITORY ${QCBOR_URL}
 	GIT_TAG ${QCBOR_REFSPEC}
 	GIT_SHALLOW TRUE
@@ -41,50 +22,18 @@
 		COMMAND git reset bf-patch
 )
 
-# FetchContent_GetProperties exports qcbor_SOURCE_DIR and qcbor_BINARY_DIR variables
-FetchContent_GetProperties(qcbor)
-if(NOT qcbor_POPULATED)
-	message(STATUS "Fetching qcbor")
-	FetchContent_Populate(qcbor)
-endif()
 
-# Determine floating point configuration
-if (TS_NO_FLOAT_HW)
-	set(_thirdparty_def -DQCBOR_DISABLE_FLOAT_HW_USE)
-endif()
-
-# Configure the qcbor library
-if (NOT "${QCBOR_EXTERNAL_INCLUDE_PATHS}" STREQUAL "")
-	string(REPLACE ";" "\\;" QCBOR_EXTERNAL_INCLUDE_PATHS "${QCBOR_EXTERNAL_INCLUDE_PATHS}")
-	set(QCBOR_EXTRA_OPTION -Dthirdparty_inc=${QCBOR_EXTERNAL_INCLUDE_PATHS})
-	unset(QCBOR_EXTERNAL_INCLUDE_PATHS)
-else()
-	set(QCBOR_EXTRA_OPTION "")
-endif()
-
-execute_process(COMMAND
-	${CMAKE_COMMAND}
-		-DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
-		-GUnix\ Makefiles
-		-Dthirdparty_def=${_thirdparty_def}
-		-DCMAKE_INSTALL_PREFIX=${QCBOR_INSTALL_PATH}
-		${QCBOR_EXTRA_OPTION}
-		${qcbor_SOURCE_DIR}
-	WORKING_DIRECTORY
-		${qcbor_BINARY_DIR}
-)
-unset(QCBOR_EXTRA_OPTION)
-
-# Build the library
-execute_process(COMMAND
-		${CMAKE_COMMAND} --build ${qcbor_BINARY_DIR} --parallel ${PROCESSOR_COUNT} --target install
-		RESULT_VARIABLE _exec_error
+include(${TS_ROOT}/tools/cmake/common/LazyFetch.cmake REQUIRED)
+LazyFetch_MakeAvailable(DEP_NAME qcbor
+	FETCH_OPTIONS "${GIT_OPTIONS}"
+	INSTALL_DIR "${QCBOR_INSTALL_DIR}"
+	CACHE_FILE "${CMAKE_CURRENT_LIST_DIR}/qcbor-init-cache.cmake.in"
+	SOURCE_DIR "${QCBOR_SOURCE_DIR}"
 	)
-if (_exec_error)
-	message(FATAL_ERROR "Build step of qcbor failed with ${_exec_error}.")
-endif()
 
 # Create an imported target to have clean abstraction in the build-system.
 add_library(qcbor STATIC IMPORTED)
-set_property(TARGET qcbor PROPERTY IMPORTED_LOCATION "${QCBOR_INSTALL_PATH}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}qcbor${CMAKE_STATIC_LIBRARY_SUFFIX}")
-set_property(TARGET qcbor PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${QCBOR_INSTALL_PATH}/include")
+set_property(TARGET qcbor PROPERTY IMPORTED_LOCATION "${QCBOR_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}qcbor${CMAKE_STATIC_LIBRARY_SUFFIX}")
+set_property(TARGET qcbor PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${QCBOR_INSTALL_DIR}/include")
+
+set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${QCBOR_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}qcbor${CMAKE_STATIC_LIBRARY_SUFFIX}")
diff --git a/external/t_cose/t_cose-init-cache.cmake.in b/external/t_cose/t_cose-init-cache.cmake.in
new file mode 100644
index 0000000..3c2879f
--- /dev/null
+++ b/external/t_cose/t_cose-init-cache.cmake.in
@@ -0,0 +1,16 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+set(CMAKE_INSTALL_PREFIX "@BUILD_INSTALL_DIR@" CACHE STRING "")
+set(CMAKE_TOOLCHAIN_FILE "@TS_EXTERNAL_LIB_TOOLCHAIN_FILE@" CACHE STRING "")
+
+set(TCOSE_EXTERNAL_INCLUDE_PATHS "@TCOSE_EXTERNAL_INCLUDE_PATHS@")
+if (TCOSE_EXTERNAL_INCLUDE_PATHS)
+	set(thirdparty_inc "${TCOSE_EXTERNAL_INCLUDE_PATHS}" CACHE STRING "")
+endif()
+
+set(MBEDTLS On CACHE STRING "")
diff --git a/external/t_cose/t_cose.cmake b/external/t_cose/t_cose.cmake
index 9321466..2458a3f 100644
--- a/external/t_cose/t_cose.cmake
+++ b/external/t_cose/t_cose.cmake
@@ -6,84 +6,39 @@
 # t_cose is a library for signing CBOR tokens using COSE_Sign1
 #-------------------------------------------------------------------------------
 
-# 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.")
-endif()
-
-# External component details
 set(T_COSE_URL "https://github.com/laurencelundblade/t_cose.git" CACHE STRING "t_cose repository URL")
 set(T_COSE_REFSPEC "fc3a4b2c7196ff582e8242de8bd4a1bc4eec577f" CACHE STRING "t_cose git refspec")
-set(T_COSE_INSTALL_PATH "${CMAKE_CURRENT_BINARY_DIR}/t_cose_install" CACHE PATH "t_cose installation directory")
-set(T_COSE_PACKAGE_PATH "${T_COSE_INSTALL_PATH}/libt_cose/cmake" CACHE PATH "t_cose CMake package directory")
+set(T_COSE_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/_deps/t_cose-src" CACHE PATH "t_cose installation directory")
+set(T_COSE_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/t_cose_install" CACHE PATH "t_cose installation directory")
 
-include(FetchContent)
-
-# Checking git
-find_program(GIT_COMMAND "git")
-if (NOT GIT_COMMAND)
-	message(FATAL_ERROR "Please install git")
-endif()
-
-# Fetching t_cose
-FetchContent_Declare(
-	t_cose
+set(GIT_OPTIONS
 	GIT_REPOSITORY ${T_COSE_URL}
 	GIT_TAG ${T_COSE_REFSPEC}
 	GIT_SHALLOW TRUE
 
 	PATCH_COMMAND git stash
+		COMMAND git branch -f bf-patch
 		COMMAND git am ${CMAKE_CURRENT_LIST_DIR}/0001-add-install-definition.patch
-		COMMAND git reset HEAD~1
-
+		COMMAND git reset bf-patch
 )
 
-# FetchContent_GetProperties exports t_cose_SOURCE_DIR and t_cose_BINARY_DIR variables
-FetchContent_GetProperties(t_cose)
-if(NOT t_cose_POPULATED)
-	message(STATUS "Fetching t_cose")
-	FetchContent_Populate(t_cose)
-endif()
-
 # Prepare include paths for dependencies that t_codse has on external components
 get_target_property(_qcbor_inc qcbor INTERFACE_INCLUDE_DIRECTORIES)
-set(_ext_inc_paths
-	${_qcbor_inc}
-	${PSA_CRYPTO_API_INCLUDE})
+list(APPEND TCOSE_EXTERNAL_INCLUDE_PATHS ${_qcbor_inc} ${PSA_CRYPTO_API_INCLUDE})
 
-if (NOT TCOSE_EXTERNAL_INCLUDE_PATHS STREQUAL "")
-	list(APPEND _ext_inc_paths  "${TCOSE_EXTERNAL_INCLUDE_PATHS}")
-	unset(TCOSE_EXTERNAL_INCLUDE_PATHS)
-endif()
-
-string(REPLACE ";" "\\;" _ext_inc_paths "${_ext_inc_paths}")
-
-# Configure the t_cose library
-execute_process(COMMAND
-${CMAKE_COMMAND}
-	-DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
-	-Dthirdparty_inc=${_ext_inc_paths}
-	-DCMAKE_INSTALL_PREFIX=${T_COSE_INSTALL_PATH}
-	-DMBEDTLS=On
-	-GUnix\ Makefiles
-	${t_cose_SOURCE_DIR}
-WORKING_DIRECTORY
-	${t_cose_BINARY_DIR}
-)
-
-# Build the library
-execute_process(COMMAND
-		${CMAKE_COMMAND} --build ${t_cose_BINARY_DIR} --parallel ${PROCESSOR_COUNT} --target install
-		RESULT_VARIABLE _exec_error
+include(${TS_ROOT}/tools/cmake/common/LazyFetch.cmake REQUIRED)
+LazyFetch_MakeAvailable(DEP_NAME t_cose
+	FETCH_OPTIONS "${GIT_OPTIONS}"
+	INSTALL_DIR ${T_COSE_INSTALL_DIR}
+	CACHE_FILE "${CMAKE_CURRENT_LIST_DIR}/t_cose-init-cache.cmake.in"
+	SOURCE_DIR "${T_COSE_SOURCE_DIR}"
 	)
-if (_exec_error)
-	message(FATAL_ERROR "Build step of t_cose failed with ${_exec_error}.")
-endif()
+
+unset(TCOSE_EXTERNAL_INCLUDE_PATHS)
 
 # Create an imported target to have clean abstraction in the build-system.
 add_library(t_cose STATIC IMPORTED)
-set_property(TARGET t_cose PROPERTY IMPORTED_LOCATION "${T_COSE_INSTALL_PATH}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}t_cose${CMAKE_STATIC_LIBRARY_SUFFIX}")
-set_property(TARGET t_cose PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${T_COSE_INSTALL_PATH}/include")
+target_link_libraries(t_cose INTERFACE qcbor)
+set_property(TARGET t_cose PROPERTY IMPORTED_LOCATION "${T_COSE_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}t_cose${CMAKE_STATIC_LIBRARY_SUFFIX}")
+set_property(TARGET t_cose PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${T_COSE_INSTALL_DIR}/include")
+set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${T_COSE_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}t_cose${CMAKE_STATIC_LIBRARY_SUFFIX}")