Build: fetch_remote_library function to fetch remote libraries.

Change-Id: Id3e31613723fe304b310670a78bde0b600457b9a
Signed-off-by: Roman Mazurak <roman.mazurak@infineon.com>
Signed-off-by: Chris Brand <chris.brand@cypress.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4d15aea..d0d6ef2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -7,7 +7,9 @@
 
 cmake_minimum_required(VERSION 3.15)
 
+list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
 include(cmake/version.cmake)
+include(remote_library)
 
 ############################ CONFIGURATION #####################################
 
@@ -34,8 +36,6 @@
     Message(FATAL_ERROR "Unsupported generator ${CMAKE_GENERATOR}. Hint: Try -G\"Unix Makefiles\"")
 endif()
 
-list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
-
 ############################### Compiler configuration #########################
 
 #Legacy compat option - load CMAKE_TOOLCHAIN_FILE as a TFM_TOOLCHAIN_FILE
diff --git a/cmake/remote_library.cmake b/cmake/remote_library.cmake
new file mode 100644
index 0000000..b7f07f4
--- /dev/null
+++ b/cmake/remote_library.cmake
@@ -0,0 +1,219 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2022 Cypress Semiconductor Corporation (an Infineon company)
+# or an affiliate of Cypress Semiconductor Corporation. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+include(FetchContent)
+set(FETCHCONTENT_QUIET FALSE)
+
+find_package(Git)
+
+# This function applies patches if they are not applied yet.
+# It assumes that patches have not been applied if it's not possible to revert them.
+#
+# WORKING_DIRECTORY - working directory where patches should be applied.
+# PATCH_FILES - list of patches. Patches will be applied in alphabetical order.
+function(apply_patches WORKING_DIRECTORY PATCH_FILES)
+    # Sort list of patches in alphabetical order
+    # So, you can use number prefix to order patches
+    list(SORT PATCH_FILES)
+
+    # Validate if patches are already applied
+    set(EXECUTE_COMMAND "${GIT_EXECUTABLE}" apply --check -R ${PATCH_FILES})
+    execute_process(COMMAND ${EXECUTE_COMMAND}
+        WORKING_DIRECTORY ${WORKING_DIRECTORY}
+        RESULT_VARIABLE ARE_PATCHES_APPLIED
+        ERROR_QUIET
+    )
+
+    if (NOT ARE_PATCHES_APPLIED EQUAL 0)
+        # Git failed to unapply the patches, so we assume that
+        # patches should be applied
+        set(EXECUTE_COMMAND "${GIT_EXECUTABLE}" apply --verbose ${PATCH_FILES})
+        execute_process(COMMAND ${EXECUTE_COMMAND}
+            WORKING_DIRECTORY ${WORKING_DIRECTORY}
+            RESULT_VARIABLE ARE_PATCHES_APPLIED
+            COMMAND_ECHO STDOUT
+        )
+        if (NOT ARE_PATCHES_APPLIED EQUAL 0)
+            message( FATAL_ERROR "Failed to apply patches at ${WORKING_DIRECTORY}" )
+        endif()
+    endif()
+endfunction()
+
+
+# Returns a repository URL and a reference to the commit used to checkout the repository.
+#
+# REPO_URL_VAR - name of variable which receives repository URL.
+# TAG_VAR - name of variable which receives reference to commit.
+function(_get_fetch_remote_properties REPO_URL_VAR TAG_VAR)
+    # Parse arguments
+    set(options "")
+    set(oneValueArgs GIT_REPOSITORY GIT_TAG)
+    set(multiValueArgs "")
+    cmake_parse_arguments(PARSE_ARGV 2 ARG "${options}" "${oneValueArgs}" "${multiValueArgs}")
+
+    if (ARG_GIT_REPOSITORY)
+        set(${REPO_URL_VAR} ${ARG_GIT_REPOSITORY} PARENT_SCOPE)
+        set(${TAG_VAR} ${ARG_GIT_TAG} PARENT_SCOPE)
+    endif()
+endfunction()
+
+
+# This function helps to handle options with an empty string values.
+# There is a feature/bug in CMake that result in problem with the empty string arguments.
+# See https://gitlab.kitware.com/cmake/cmake/-/issues/16341 for details
+#
+# Arguments:
+#   [in]  KEY              - option name
+#   [out]  KEY_VAR         - name of variable that is set to ${KEY} on exit if value is not
+#                            an empty string otherwise to the empty string.
+#   [out]  VALUE_VAR       - name of variable that is set to option value for ${KEY}.
+#   [in/out]  ARG_LIST_VAR - name of variable that holds list of key/value pairs - arguments.
+#                            Function looks for key/value pair specified by ${KEY} variable in
+#                            this list. Function removes key/value pair specified by ${KEY} on
+#                            exit.
+#
+# Example #1:
+#   # We have following key/options:
+#   #  GIT_SUBMODULES  ""
+#   #  BOO  "abc"
+#   #  HEY  "hi"
+#   set(ARGS    GIT_SUBMODULES "" BOO "abc" HEY "hi")
+#   # Extract key/value for option "GIT_SUBMODULES"
+#   extract_key_value(GIT_SUBMODULES GIT_SUBMODULES_VAR GIT_SUBMODULES_VALUE_VAR ARGS)
+#   # ${GIT_SUBMODULES_VAR} is equal to ""
+#   # ${GIT_SUBMODULES_VALUE_VAR} is equal to ""
+#
+# Example #2:
+#   # We have following key/options:
+#   #  GIT_SUBMODULES  "name"
+#   #  BOO  "abc"
+#   #  HEY  "hi"
+#   set(ARGS    GIT_SUBMODULES "name" BOO "abc" HEY "hi")
+#   # Extract key/value for option "GIT_SUBMODULES"
+#   extract_key_value(GIT_SUBMODULES GIT_SUBMODULES_VAR GIT_SUBMODULES_VALUE_VAR ARGS)
+#   # ${GIT_SUBMODULES_VAR} is equal to "GIT_SUBMODULES"
+#   # ${GIT_SUBMODULES_VALUE_VAR} is equal to "name"
+function(extract_key_value KEY KEY_VAR VALUE_VAR ARG_LIST_VAR)
+    list(FIND ${ARG_LIST_VAR} ${KEY} KEY_INDEX)
+    if(${KEY_INDEX} GREATER_EQUAL 0)
+        # Variable has been set, remove KEY
+        list(REMOVE_AT ${ARG_LIST_VAR} ${KEY_INDEX})
+
+        # Validate that there is an option value in the list of arguments
+        list(LENGTH ${ARG_LIST_VAR} ARG_LIST_LENGTH)
+        if(${KEY_INDEX} GREATER_EQUAL ${ARG_LIST_LENGTH})
+            message(FATAL_ERROR "Missing option value for ${KEY}")
+        endif()
+
+        # Get value
+        list(GET ${ARG_LIST_VAR} ${KEY_INDEX} VALUE)
+
+        # Remove value in the list
+        list(REMOVE_AT ${ARG_LIST_VAR} ${KEY_INDEX})
+
+        # Update argument list
+        set(${ARG_LIST_VAR} ${${ARG_LIST_VAR}} PARENT_SCOPE)
+
+        # Set KEY_VAR & VALUE_VAR
+        set(${KEY_VAR} ${KEY} PARENT_SCOPE)
+        set(${VALUE_VAR} ${VALUE} PARENT_SCOPE)
+    else()
+        # Variable is not defined, set KEY_VAR & VALUE_VAR to empty strings
+        set(${KEY_VAR} "" PARENT_SCOPE)
+        set(${VALUE_VAR} "" PARENT_SCOPE)
+    endif()
+endfunction()
+
+
+# This function allows to fetch library from a remote repository or use a local
+# library copy.
+#
+# You can specify location of directory with patches. Patches are applied in
+# alphabetical order.
+#
+# Arguments:
+# [in]     LIB_NAME <name> - library name
+# [in/out] LIB_SOURCE_PATH_VAR <var> - name of variable which holds path to library source
+#           or "DOWNLOAD" if sources should be fetched from the remote repository. This
+#           variable is updated in case if library is downloaded. It will point
+#           to the path where FetchContent_Populate will locate local library copy.
+# [out]    LIB_BINARY_PATH_VAR <var> - optional name of variable which is updated to
+#           directory intended for use as a corresponding build directory if
+#           library is fetched from the remote repository.
+# [in]     LIB_BASE_DIR <path>  - is used to set FETCHCONTENT_BASE_DIR.
+# [in]     LIB_PATCH_DIR <path> - optional path to local folder which contains patches
+#           that should be applied.
+# [in]     GIT_REPOSITORY, GIT_TAG, ... - see https://cmake.org/cmake/help/latest/module/ExternalProject.html
+#           for more details
+#
+# This function set CMP0097 to NEW if CMAKE_VERSION is greater or equal than 3.18.0.
+# Because of https://gitlab.kitware.com/cmake/cmake/-/issues/20579 CMP0097 is
+# non-functional until cmake 3.18.0.
+# See https://cmake.org/cmake/help/latest/policy/CMP0097.html for more info.
+function(fetch_remote_library)
+    # Parse arguments
+    set(options "")
+    set(oneValueArgs LIB_NAME LIB_SOURCE_PATH_VAR LIB_BINARY_PATH_VAR LIB_BASE_DIR LIB_PATCH_DIR)
+    set(multiValueArgs FETCH_CONTENT_ARGS)
+    cmake_parse_arguments(PARSE_ARGV 0 ARG "${options}" "${oneValueArgs}" "${multiValueArgs}")
+
+    if(ARG_LIB_BASE_DIR)
+        set(FETCHCONTENT_BASE_DIR "${ARG_LIB_BASE_DIR}")
+    endif()
+
+    # Set to not download submodules if that option is available
+    if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.18.0")
+        cmake_policy(SET CMP0097 NEW)
+    endif()
+
+    if ("${${ARG_LIB_SOURCE_PATH_VAR}}" STREQUAL "DOWNLOAD")
+        # Process arguments which can be an empty string
+        # There is a feature/bug in CMake that result in problem with empty string arguments
+        # See https://gitlab.kitware.com/cmake/cmake/-/issues/16341 for details
+        extract_key_value(GIT_SUBMODULES GIT_SUBMODULES GIT_SUBMODULES_VALUE ARG_FETCH_CONTENT_ARGS)
+
+        # Validate that there is no empty arguments to FetchContent_Declare
+        LIST(FIND ARG_FETCH_CONTENT_ARGS "" EMPTY_VALUE_INDEX)
+        if(${EMPTY_VALUE_INDEX} GREATER_EQUAL 0)
+            # There is an unsupported empty string argument, FATAL ERROR!
+            math(EXPR EMPTY_KEY_INDEX "${EMPTY_VALUE_INDEX} - 1")
+            list(GET ARG_FETCH_CONTENT_ARGS ${EMPTY_KEY_INDEX} EMPTY_KEY)
+            # TODO: Use extract_key_value if you have argument with empty value (see GIT_SUBMODULES above)
+            message(FATAL_ERROR "fetch_remote_library: Unexpected empty string value for ${EMPTY_KEY}. "
+                                "Please, validate arguments or update fetch_remote_library to support empty value for ${EMPTY_KEY}!!!")
+        endif()
+
+        # Content fetching
+        FetchContent_Declare(${ARG_LIB_NAME}
+            ${ARG_FETCH_CONTENT_ARGS}
+            "${GIT_SUBMODULES}"      "${GIT_SUBMODULES_VALUE}"
+        )
+
+        FetchContent_GetProperties(${ARG_LIB_NAME})
+        if(NOT ${ARG_LIB_NAME}_POPULATED)
+            FetchContent_Populate(${ARG_LIB_NAME})
+
+            # Get remote properties
+            _get_fetch_remote_properties(REPO_URL_VAR TAG_VAR ${ARG_FETCH_CONTENT_ARGS})
+            set(${ARG_LIB_SOURCE_PATH_VAR} ${${ARG_LIB_NAME}_SOURCE_DIR} CACHE PATH "Library has been downloaded from \"${REPO_URL_VAR}\", tag \"${TAG_VAR}\"" FORCE)
+            if (DEFINED ARG_LIB_BINARY_PATH_VAR)
+                set(${ARG_LIB_BINARY_PATH_VAR} ${${ARG_LIB_NAME}_BINARY_DIR} CACHE PATH "Path to build directory of \"${ARG_LIB_NAME}\"")
+            endif()
+        endif()
+    endif()
+
+    if (ARG_LIB_PATCH_DIR)
+        # look for patch files
+        file(GLOB PATCH_FILES "${ARG_LIB_PATCH_DIR}/*.patch")
+
+        if(PATCH_FILES)
+            # Apply patches for existing sources
+            apply_patches("${${ARG_LIB_SOURCE_PATH_VAR}}" "${PATCH_FILES}")
+        endif()
+    endif()
+endfunction()
diff --git a/lib/ext/mbedcrypto/CMakeLists.txt b/lib/ext/mbedcrypto/CMakeLists.txt
index 9a0340a..9316aca 100644
--- a/lib/ext/mbedcrypto/CMakeLists.txt
+++ b/lib/ext/mbedcrypto/CMakeLists.txt
@@ -1,44 +1,20 @@
 #-------------------------------------------------------------------------------
 # Copyright (c) 2020, Arm Limited. All rights reserved.
+# Copyright (c) 2022 Cypress Semiconductor Corporation (an Infineon company)
+# or an affiliate of Cypress Semiconductor Corporation. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
 #-------------------------------------------------------------------------------
 
-include(FetchContent)
-set(FETCHCONTENT_QUIET FALSE)
-
-# Set to not download submodules if that option is available
-if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16.0")
-    cmake_policy(SET CMP0097 NEW)
-endif()
-
-if ("${MBEDCRYPTO_PATH}" STREQUAL "DOWNLOAD")
-    find_package(Git)
-    file(GLOB PATCH_FILES *.patch)
-
-    if (PATCH_FILES)
-        FetchContent_Declare(mbedcrypto
-            GIT_REPOSITORY ${MBEDCRYPTO_GIT_REMOTE}
-            GIT_TAG ${MBEDCRYPTO_VERSION}
-            GIT_SHALLOW TRUE
-            GIT_PROGRESS TRUE
-            GIT_SUBMODULES ""
-            PATCH_COMMAND ${GIT_EXECUTABLE} apply ${PATCH_FILES}
-        )
-    else()
-        FetchContent_Declare(mbedcrypto
-            GIT_REPOSITORY ${MBEDCRYPTO_GIT_REMOTE}
-            GIT_TAG ${MBEDCRYPTO_VERSION}
-            GIT_SHALLOW TRUE
-            GIT_PROGRESS TRUE
-            GIT_SUBMODULES ""
-        )
-    endif()
-
-    FetchContent_GetProperties(mbedcrypto)
-    if(NOT mbedcrypto_POPULATED)
-        FetchContent_Populate(mbedcrypto)
-        set(MBEDCRYPTO_PATH ${mbedcrypto_SOURCE_DIR} CACHE PATH "Path to mbed-crypto (or DOWNLOAD to get automatically" FORCE)
-    endif()
-endif()
+fetch_remote_library(
+    LIB_NAME                mbedcrypto
+    LIB_SOURCE_PATH_VAR     MBEDCRYPTO_PATH
+    LIB_PATCH_DIR           ${CMAKE_CURRENT_LIST_DIR}
+    FETCH_CONTENT_ARGS
+        GIT_REPOSITORY      ${MBEDCRYPTO_GIT_REMOTE}
+        GIT_TAG             ${MBEDCRYPTO_VERSION}
+        GIT_SHALLOW         TRUE
+        GIT_PROGRESS        TRUE
+        GIT_SUBMODULES      ""
+)
diff --git a/lib/ext/mcuboot/CMakeLists.txt b/lib/ext/mcuboot/CMakeLists.txt
index 03f888e..a1a0242 100644
--- a/lib/ext/mcuboot/CMakeLists.txt
+++ b/lib/ext/mcuboot/CMakeLists.txt
@@ -1,38 +1,28 @@
 #-------------------------------------------------------------------------------
 # Copyright (c) 2020, Arm Limited. All rights reserved.
+# Copyright (c) 2022 Cypress Semiconductor Corporation (an Infineon company)
+# or an affiliate of Cypress Semiconductor Corporation. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
 #-------------------------------------------------------------------------------
 
-include(FetchContent)
-set(FETCHCONTENT_QUIET FALSE)
-
-# Set to not download submodules if that option is available.
 # Because of https://gitlab.kitware.com/cmake/cmake/-/issues/20579 CMP0097 is
 # non-functional until cmake 3.18.0.
-if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.18.0")
-    cmake_policy(SET CMP0097 NEW)
-    set(MCUBOOT_SUBMODULES "")
-else()
-    # Workaround for not having CMP0097. Fetch the smallest submodule.
-    set(MCUBOOT_SUBMODULES "boot/cypress/libs/cy-mbedtls-acceleration/")
+if(${CMAKE_VERSION} VERSION_LESS "3.18.0")
+    # Workaround for not having CMP0097. Use existing directory.
+    set(MCUBOOT_SUBMODULES "docs")
 endif()
 
-if ("${MCUBOOT_PATH}" STREQUAL "DOWNLOAD")
-    FetchContent_Declare(mcuboot
-        GIT_REPOSITORY https://github.com/mcu-tools/mcuboot.git
-        GIT_TAG ${MCUBOOT_VERSION}
-        GIT_SHALLOW FALSE
-        GIT_PROGRESS TRUE
-        GIT_SUBMODULES "${MCUBOOT_SUBMODULES}"
-    )
-    # ToDo: set GIT_SHALLOW to 'TRUE' when MCUBOOT_VERSION will be set to a tag
-    # (instead of a commit hash) with the new release.
-
-    FetchContent_GetProperties(mcuboot)
-    if(NOT mcuboot_POPULATED)
-        FetchContent_Populate(mcuboot)
-        set(MCUBOOT_PATH ${mcuboot_SOURCE_DIR} CACHE PATH "Path to MCUBOOT (or DOWNLOAD to get automatically" FORCE)
-    endif()
-endif()
+fetch_remote_library(
+    LIB_NAME                mcuboot
+    LIB_SOURCE_PATH_VAR     MCUBOOT_PATH
+    FETCH_CONTENT_ARGS
+        GIT_REPOSITORY      https://github.com/mcu-tools/mcuboot.git
+        GIT_TAG             ${MCUBOOT_VERSION}
+        # ToDo: set GIT_SHALLOW to 'TRUE' when MCUBOOT_VERSION will be set to a tag
+        # (instead of a commit hash) with the new release.
+        GIT_SHALLOW         FALSE
+        GIT_PROGRESS        TRUE
+        GIT_SUBMODULES      "${MCUBOOT_SUBMODULES}"
+)
diff --git a/lib/ext/psa-adac/CMakeLists.txt b/lib/ext/psa-adac/CMakeLists.txt
index c79eeb5..25038c9 100644
--- a/lib/ext/psa-adac/CMakeLists.txt
+++ b/lib/ext/psa-adac/CMakeLists.txt
@@ -1,31 +1,20 @@
 #-------------------------------------------------------------------------------
 # Copyright (c) 2021, Arm Limited. All rights reserved.
+# Copyright (c) 2022 Cypress Semiconductor Corporation (an Infineon company)
+# or an affiliate of Cypress Semiconductor Corporation. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
 #-------------------------------------------------------------------------------
 
-include(FetchContent)
-set(FETCHCONTENT_QUIET FALSE)
-
-# Set to not download submodules if that option is available
-if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16.0")
-    cmake_policy(SET CMP0097 NEW)
-endif()
-
-if ("${PLATFORM_PSA_ADAC_SOURCE_PATH}" STREQUAL "DOWNLOAD")
-    FetchContent_Declare(libpsaadac
-        GIT_REPOSITORY https://git.trustedfirmware.org/shared/psa-adac.git
-        GIT_TAG ${PLATFORM_PSA_ADAC_VERSION}
-    )
-
-    FetchContent_GetProperties(libpsaadac)
-    if (NOT libpsaadac_POPULATED)
-        FetchContent_Populate(libpsaadac)
-        set(PLATFORM_PSA_ADAC_SOURCE_PATH ${libpsaadac_SOURCE_DIR} CACHE PATH "Path to source directory of psa-adac." FORCE)
-        set(PLATFORM_PSA_ADAC_BUILD_PATH ${libpsaadac_BINARY_DIR} CACHE PATH "Path to build directory of psa-adac.")
-    endif ()
-endif ()
+fetch_remote_library(
+    LIB_NAME                libpsaadac
+    LIB_SOURCE_PATH_VAR     PLATFORM_PSA_ADAC_SOURCE_PATH
+    LIB_BINARY_PATH_VAR     PLATFORM_PSA_ADAC_BUILD_PATH
+    FETCH_CONTENT_ARGS
+        GIT_REPOSITORY      https://git.trustedfirmware.org/shared/psa-adac.git
+        GIT_TAG             ${PLATFORM_PSA_ADAC_VERSION}
+)
 
 
 set(PSA_ADAC_TARGET "trusted-firmware-m")
diff --git a/lib/ext/psa_arch_tests/CMakeLists.txt b/lib/ext/psa_arch_tests/CMakeLists.txt
index cb51a71..f98fdfa 100644
--- a/lib/ext/psa_arch_tests/CMakeLists.txt
+++ b/lib/ext/psa_arch_tests/CMakeLists.txt
@@ -1,44 +1,20 @@
 #-------------------------------------------------------------------------------
 # Copyright (c) 2020, Arm Limited. All rights reserved.
+# Copyright (c) 2022 Cypress Semiconductor Corporation (an Infineon company)
+# or an affiliate of Cypress Semiconductor Corporation. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
 #-------------------------------------------------------------------------------
 
-include(FetchContent)
-set(FETCHCONTENT_QUIET FALSE)
-
-#Set to not download submodules if that option is available
-if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16.0")
-    cmake_policy(SET CMP0097 NEW)
-endif()
-
-if ("${PSA_ARCH_TESTS_PATH}" STREQUAL "DOWNLOAD")
-    find_package(Git)
-    file(GLOB PATCH_FILES *.patch)
-
-    if (PATCH_FILES)
-        FetchContent_Declare(psa_arch_tests
-            GIT_REPOSITORY https://github.com/ARM-software/psa-arch-tests.git
-            GIT_TAG ${PSA_ARCH_TESTS_VERSION}
-            GIT_SHALLOW FALSE
-            GIT_PROGRESS TRUE
-            GIT_SUBMODULES ""
-            PATCH_COMMAND ${GIT_EXECUTABLE} apply ${PATCH_FILES}
-        )
-    else()
-        FetchContent_Declare(psa_arch_tests
-            GIT_REPOSITORY https://github.com/ARM-software/psa-arch-tests.git
-            GIT_TAG ${PSA_ARCH_TESTS_VERSION}
-            GIT_SHALLOW FALSE
-            GIT_PROGRESS TRUE
-            GIT_SUBMODULES ""
-        )
-    endif()
-
-    FetchContent_GetProperties(psa_arch_tests)
-    if(NOT psa_arch_tests_POPULATED)
-        FetchContent_Populate(psa_arch_tests)
-        set(PSA_ARCH_TESTS_PATH ${psa_arch_tests_SOURCE_DIR} CACHE PATH "Path to PSA_ARCH_TESTS (or DOWNLOAD to get automatically" FORCE)
-    endif()
-endif()
+fetch_remote_library(
+    LIB_NAME                psa_arch_tests
+    LIB_SOURCE_PATH_VAR     PSA_ARCH_TESTS_PATH
+    LIB_PATCH_DIR           ${CMAKE_CURRENT_LIST_DIR}
+    FETCH_CONTENT_ARGS
+        GIT_REPOSITORY      https://github.com/ARM-software/psa-arch-tests.git
+        GIT_TAG             ${PSA_ARCH_TESTS_VERSION}
+        GIT_SHALLOW         FALSE
+        GIT_PROGRESS        TRUE
+        GIT_SUBMODULES      ""
+)
diff --git a/lib/ext/tf-m-tests/fetch_repo.cmake b/lib/ext/tf-m-tests/fetch_repo.cmake
index 593ff1d..a2a12bd 100644
--- a/lib/ext/tf-m-tests/fetch_repo.cmake
+++ b/lib/ext/tf-m-tests/fetch_repo.cmake
@@ -1,15 +1,12 @@
 #-------------------------------------------------------------------------------
 # Copyright (c) 2020-2022, Arm Limited. All rights reserved.
+# Copyright (c) 2022 Cypress Semiconductor Corporation (an Infineon company)
+# or an affiliate of Cypress Semiconductor Corporation. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
 #-------------------------------------------------------------------------------
 
-# Set to not download submodules if that option is available
-if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16.0")
-    cmake_policy(SET CMP0097 NEW)
-endif()
-
 # If NS app, secure regression test or non-secure regression test is enabled,
 # fetch tf-m-tests repo.
 # The conditiions are actually overlapped but it can make the logic more clear.
@@ -19,24 +16,15 @@
     # Set tf-m-tests repo config
     include(${CMAKE_SOURCE_DIR}/lib/ext/tf-m-tests/repo_config_default.cmake)
 
-    include(FetchContent)
-    set(FETCHCONTENT_QUIET FALSE)
-
-    set(FETCHCONTENT_BASE_DIR ${CMAKE_BINARY_DIR}/lib/ext CACHE STRING "" FORCE)
-
-    if ("${TFM_TEST_REPO_PATH}" STREQUAL "DOWNLOAD")
-        FetchContent_Declare(tfm_test_repo
-            GIT_REPOSITORY https://git.trustedfirmware.org/TF-M/tf-m-tests.git
-            GIT_TAG ${TFM_TEST_REPO_VERSION}
+    fetch_remote_library(
+        LIB_NAME                tfm_test_repo
+        LIB_SOURCE_PATH_VAR     TFM_TEST_REPO_PATH
+        LIB_BASE_DIR            "${CMAKE_BINARY_DIR}/lib/ext"
+        FETCH_CONTENT_ARGS
+            GIT_REPOSITORY      https://git.trustedfirmware.org/TF-M/tf-m-tests.git
+            GIT_TAG             ${TFM_TEST_REPO_VERSION}
             GIT_PROGRESS TRUE
-        )
-
-        FetchContent_GetProperties(tfm_test_repo)
-        if(NOT tfm_test_repo_POPULATED)
-            FetchContent_Populate(tfm_test_repo)
-            set(TFM_TEST_REPO_PATH ${tfm_test_repo_SOURCE_DIR} CACHE PATH "Path to TFM-TEST repo (or DOWNLOAD to fetch automatically" FORCE)
-        endif()
-    endif()
+    )
 
     if ("${CMSIS_5_PATH}" STREQUAL DOWNLOAD)
         set(CMSIS_5_PATH ${TFM_TEST_REPO_PATH}/CMSIS CACHE PATH "Path to CMSIS_5 (or DOWNLOAD to fetch automatically" FORCE)
diff --git a/platform/ext/target/arm/corstone1000/openamp/ext/libmetal/CMakeLists.txt b/platform/ext/target/arm/corstone1000/openamp/ext/libmetal/CMakeLists.txt
index 9fab53f..0bb89e9 100644
--- a/platform/ext/target/arm/corstone1000/openamp/ext/libmetal/CMakeLists.txt
+++ b/platform/ext/target/arm/corstone1000/openamp/ext/libmetal/CMakeLists.txt
@@ -1,29 +1,17 @@
 #-------------------------------------------------------------------------------
 # Copyright (c) 2021, Arm Limited. All rights reserved.
+# Copyright (c) 2022 Cypress Semiconductor Corporation (an Infineon company)
+# or an affiliate of Cypress Semiconductor Corporation. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
 #-------------------------------------------------------------------------------
 
-include(FetchContent)
-set(FETCHCONTENT_QUIET FALSE)
-
-# Set to not download submodules if that option is available
-if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16.0")
-    cmake_policy(SET CMP0097 NEW)
-endif()
-
-if ("${LIBMETAL_SRC_PATH}" STREQUAL "DOWNLOAD")
-    FetchContent_Declare(libmetal
-        GIT_REPOSITORY https://github.com/OpenAMP/libmetal.git
-        GIT_TAG ${LIBMETAL_VERSION}
-    )
-
-    FetchContent_GetProperties(libmetal)
-    if (NOT libmetal_POPULATED)
-        FetchContent_Populate(libmetal)
-        set(LIBMETAL_BIN_PATH ${libmetal_BINARY_DIR} CACHE PATH "Path to Libmetal (or DOWNLOAD to get automatically")
-        set(LIBMETAL_SRC_PATH ${libmetal_SOURCE_DIR} CACHE PATH "Path to Libmetal (or DOWNLOAD to get automatically" FORCE)
-    endif ()
-endif ()
-
+fetch_remote_library(
+    LIB_NAME                libmetal
+    LIB_SOURCE_PATH_VAR     LIBMETAL_SRC_PATH
+    LIB_BINARY_PATH_VAR     LIBMETAL_BIN_PATH
+    FETCH_CONTENT_ARGS
+        GIT_REPOSITORY      https://github.com/OpenAMP/libmetal.git
+        GIT_TAG             ${LIBMETAL_VERSION}
+)
diff --git a/platform/ext/target/arm/corstone1000/openamp/ext/libopenamp/CMakeLists.txt b/platform/ext/target/arm/corstone1000/openamp/ext/libopenamp/CMakeLists.txt
index 9b1602a..d6590a1 100644
--- a/platform/ext/target/arm/corstone1000/openamp/ext/libopenamp/CMakeLists.txt
+++ b/platform/ext/target/arm/corstone1000/openamp/ext/libopenamp/CMakeLists.txt
@@ -1,29 +1,17 @@
 #-------------------------------------------------------------------------------
 # Copyright (c) 2020-2021, Arm Limited. All rights reserved.
+# Copyright (c) 2022 Cypress Semiconductor Corporation (an Infineon company)
+# or an affiliate of Cypress Semiconductor Corporation. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
 #-------------------------------------------------------------------------------
 
-include(FetchContent)
-set(FETCHCONTENT_QUIET FALSE)
-
-# Set to not download submodules if that option is available
-if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16.0")
-    cmake_policy(SET CMP0097 NEW)
-endif()
-
-if ("${LIBOPENAMP_SRC_PATH}" STREQUAL "DOWNLOAD")
-    FetchContent_Declare(libopenamp
-        GIT_REPOSITORY https://github.com/OpenAMP/open-amp.git
-        GIT_TAG ${OPENAMP_VERSION}
-    )
-
-    FetchContent_GetProperties(libopenamp)
-    if (NOT libopenamp_POPULATED)
-        FetchContent_Populate(libopenamp)
-        set(LIBOPENAMP_BIN_PATH ${libopenamp_BINARY_DIR} CACHE PATH "Path to Libopenamp (or DOWNLOAD to get automatically")
-        set(LIBOPENAMP_SRC_PATH ${libopenamp_SOURCE_DIR} CACHE PATH "Path to Libopenamp (or DOWNLOAD to get automatically" FORCE)
-    endif ()
-endif ()
-
+fetch_remote_library(
+    LIB_NAME                libopenamp
+    LIB_SOURCE_PATH_VAR     LIBOPENAMP_SRC_PATH
+    LIB_BINARY_PATH_VAR     LIBOPENAMP_BIN_PATH
+    FETCH_CONTENT_ARGS
+        GIT_REPOSITORY      https://github.com/OpenAMP/open-amp.git
+        GIT_TAG             ${OPENAMP_VERSION}
+)
diff --git a/platform/ext/target/cypress/psoc64/libs/mtb-pdl-cat1/CMakeLists.txt b/platform/ext/target/cypress/psoc64/libs/mtb-pdl-cat1/CMakeLists.txt
index 7e2e457..ecae071 100644
--- a/platform/ext/target/cypress/psoc64/libs/mtb-pdl-cat1/CMakeLists.txt
+++ b/platform/ext/target/cypress/psoc64/libs/mtb-pdl-cat1/CMakeLists.txt
@@ -1,47 +1,22 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2020-2022, Cypress Semiconductor Corporation. All rights reserved.
+# Copyright (c) 2020-2022 Cypress Semiconductor Corporation (an Infineon company)
+# or an affiliate of Cypress Semiconductor Corporation. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
 #-------------------------------------------------------------------------------
 
-include(FetchContent)
-set(FETCHCONTENT_QUIET FALSE)
-
-# Set to not download submodules if that option is available
-if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16.0")
-    cmake_policy(SET CMP0097 NEW)
-endif()
-
-if ("${CY_MTB_PDL_CAT1_LIB_PATH}" STREQUAL "DOWNLOAD")
-    find_package(Git)
-    file(GLOB PATCH_FILES *.patch)
-
-    if (PATCH_FILES)
-        FetchContent_Declare(mtb-pdl-cat1
-            GIT_REPOSITORY https://github.com/Infineon/mtb-pdl-cat1.git
-            GIT_TAG ${CY_MTB_PDL_CAT1_LIB_VERSION}
-            GIT_SHALLOW TRUE
-            GIT_PROGRESS TRUE
-            GIT_SUBMODULES ""
-            PATCH_COMMAND ${GIT_EXECUTABLE} apply ${PATCH_FILES}
-        )
-    else()
-        FetchContent_Declare(mtb-pdl-cat1
-            GIT_REPOSITORY https://github.com/Infineon/mtb-pdl-cat1.git
-            GIT_TAG ${CY_MTB_PDL_CAT1_LIB_VERSION}
-            GIT_SHALLOW TRUE
-            GIT_PROGRESS TRUE
-            GIT_SUBMODULES ""
-        )
-    endif()
-
-    FetchContent_GetProperties(mtb-pdl-cat1)
-    if(NOT mtb-pdl-cat1_POPULATED)
-        FetchContent_Populate(mtb-pdl-cat1)
-        set(CY_MTB_PDL_CAT1_LIB_PATH ${mtb-pdl-cat1_SOURCE_DIR} CACHE PATH "Path to mtb-pdl-cat1 (or DOWNLOAD to get automatically" FORCE)
-    endif()
-endif()
+fetch_remote_library(
+    LIB_NAME                mtb-pdl-cat1
+    LIB_SOURCE_PATH_VAR     CY_MTB_PDL_CAT1_LIB_PATH
+    LIB_PATCH_DIR           ${CMAKE_CURRENT_LIST_DIR}
+    FETCH_CONTENT_ARGS
+        GIT_REPOSITORY      https://github.com/Infineon/mtb-pdl-cat1.git
+        GIT_TAG             ${CY_MTB_PDL_CAT1_LIB_VERSION}
+        GIT_SHALLOW         TRUE
+        GIT_PROGRESS        TRUE
+        GIT_SUBMODULES      ""
+)
 
 #-------------------------------------------------------------------------------
 
diff --git a/platform/ext/target/cypress/psoc64/libs/p64_utils/CMakeLists.txt b/platform/ext/target/cypress/psoc64/libs/p64_utils/CMakeLists.txt
index e63c6b5..81667e4 100644
--- a/platform/ext/target/cypress/psoc64/libs/p64_utils/CMakeLists.txt
+++ b/platform/ext/target/cypress/psoc64/libs/p64_utils/CMakeLists.txt
@@ -1,48 +1,22 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2021-2022, Cypress Semiconductor Corporation. All rights reserved.
+# Copyright (c) 2021-2022 Cypress Semiconductor Corporation (an Infineon company)
+# or an affiliate of Cypress Semiconductor Corporation. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
 #-------------------------------------------------------------------------------
 
-include(FetchContent)
-set(FETCHCONTENT_QUIET FALSE)
-
-# Set to not download submodules if that option is available
-if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16.0")
-    cmake_policy(SET CMP0097 NEW)
-endif()
-
-if ("${CY_P64_UTILS_LIB_PATH}" STREQUAL "DOWNLOAD")
-    find_package(Git)
-    file(GLOB PATCH_FILES *.patch)
-
-    if (PATCH_FILES)
-        FetchContent_Declare(p64_utils
-            GIT_REPOSITORY https://github.com/Infineon/p64_utils.git
-            GIT_TAG ${CY_P64_UTILS_LIB_VERSION}
-            GIT_SHALLOW TRUE
-            GIT_PROGRESS TRUE
-            GIT_SUBMODULES ""
-            PATCH_COMMAND ${GIT_EXECUTABLE} apply ${PATCH_FILES}
-        )
-    else()
-        FetchContent_Declare(p64_utils
-            GIT_REPOSITORY https://github.com/Infineon/p64_utils.git
-            GIT_TAG ${CY_P64_UTILS_LIB_VERSION}
-            GIT_SHALLOW TRUE
-            GIT_PROGRESS TRUE
-            GIT_SUBMODULES ""
-        )
-    endif()
-
-    FetchContent_GetProperties(p64_utils)
-    if(NOT p64_utils_POPULATED)
-        FetchContent_Populate(p64_utils)
-        set(CY_P64_UTILS_LIB_PATH ${p64_utils_SOURCE_DIR} CACHE PATH "Path to p64_utils repo (or DOWNLOAD to fetch automatically" FORCE)
-    endif()
-
-endif()
+fetch_remote_library(
+    LIB_NAME                p64_utils
+    LIB_SOURCE_PATH_VAR     CY_P64_UTILS_LIB_PATH
+    LIB_PATCH_DIR           ${CMAKE_CURRENT_LIST_DIR}
+    FETCH_CONTENT_ARGS
+        GIT_REPOSITORY      https://github.com/Infineon/p64_utils.git
+        GIT_TAG             ${CY_P64_UTILS_LIB_VERSION}
+        GIT_SHALLOW         TRUE
+        GIT_PROGRESS        TRUE
+        GIT_SUBMODULES      ""
+)
 
 #-------------------------------------------------------------------------------