Revert "Build: fetch_remote_library function to fetch remote libraries."

This reverts commit 2e5af6eb005b6c4e9544bc72e8f4200fbc5651a1.

Reason for revert: git apply logic breaks incremental build.

Change-Id: Iee63ab12f78d76f4f0d21e95d017d7cc6c49d5bb
diff --git a/lib/ext/mbedcrypto/CMakeLists.txt b/lib/ext/mbedcrypto/CMakeLists.txt
index 9316aca..9a0340a 100644
--- a/lib/ext/mbedcrypto/CMakeLists.txt
+++ b/lib/ext/mbedcrypto/CMakeLists.txt
@@ -1,20 +1,44 @@
 #-------------------------------------------------------------------------------
 # 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
 #
 #-------------------------------------------------------------------------------
 
-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      ""
-)
+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()
diff --git a/lib/ext/mcuboot/CMakeLists.txt b/lib/ext/mcuboot/CMakeLists.txt
index a1a0242..03f888e 100644
--- a/lib/ext/mcuboot/CMakeLists.txt
+++ b/lib/ext/mcuboot/CMakeLists.txt
@@ -1,28 +1,38 @@
 #-------------------------------------------------------------------------------
 # 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_LESS "3.18.0")
-    # Workaround for not having CMP0097. Use existing directory.
-    set(MCUBOOT_SUBMODULES "docs")
+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/")
 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}"
-)
+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()
diff --git a/lib/ext/psa-adac/CMakeLists.txt b/lib/ext/psa-adac/CMakeLists.txt
index 25038c9..c79eeb5 100644
--- a/lib/ext/psa-adac/CMakeLists.txt
+++ b/lib/ext/psa-adac/CMakeLists.txt
@@ -1,20 +1,31 @@
 #-------------------------------------------------------------------------------
 # 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
 #
 #-------------------------------------------------------------------------------
 
-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}
-)
+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 ()
 
 
 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 f98fdfa..cb51a71 100644
--- a/lib/ext/psa_arch_tests/CMakeLists.txt
+++ b/lib/ext/psa_arch_tests/CMakeLists.txt
@@ -1,20 +1,44 @@
 #-------------------------------------------------------------------------------
 # 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
 #
 #-------------------------------------------------------------------------------
 
-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      ""
-)
+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()
diff --git a/lib/ext/tf-m-tests/fetch_repo.cmake b/lib/ext/tf-m-tests/fetch_repo.cmake
index a2a12bd..593ff1d 100644
--- a/lib/ext/tf-m-tests/fetch_repo.cmake
+++ b/lib/ext/tf-m-tests/fetch_repo.cmake
@@ -1,12 +1,15 @@
 #-------------------------------------------------------------------------------
 # 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.
@@ -16,15 +19,24 @@
     # Set tf-m-tests repo config
     include(${CMAKE_SOURCE_DIR}/lib/ext/tf-m-tests/repo_config_default.cmake)
 
-    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}
+    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}
             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)