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()