cmake: By default only apply patches when downloading remote libraries

By default only apply the required patches to remote libraries when they
are using the DOWNLOAD strategy.
When remote library locations are provided manually the patches
should be manually managed.
Add option to force applying patches always.

Change-Id: I7c121a70851d8545bc7e61e0022b2692157fb3ef
Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
diff --git a/cmake/remote_library.cmake b/cmake/remote_library.cmake
index b4e7070..d1097c6 100644
--- a/cmake/remote_library.cmake
+++ b/cmake/remote_library.cmake
@@ -217,6 +217,8 @@
 # [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]     LIB_FORCE_PATCH - optional argument to force applying patches when the path
+#            is a local folder instead of fetching from the remote repository.
 # [in]     GIT_REPOSITORY, GIT_TAG, ... - see https://cmake.org/cmake/help/latest/module/ExternalProject.html
 #           for more details
 #
@@ -227,7 +229,7 @@
 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(oneValueArgs LIB_NAME LIB_SOURCE_PATH_VAR LIB_BINARY_PATH_VAR LIB_BASE_DIR LIB_PATCH_DIR LIB_FORCE_PATCH)
     set(multiValueArgs FETCH_CONTENT_ARGS)
     cmake_parse_arguments(PARSE_ARGV 0 ARG "${options}" "${oneValueArgs}" "${multiValueArgs}")
 
@@ -241,6 +243,7 @@
     endif()
 
     if ("${${ARG_LIB_SOURCE_PATH_VAR}}" STREQUAL "DOWNLOAD")
+        set(SOURCE_PATH_IS_DOWNLOAD TRUE)
         # 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
@@ -276,7 +279,7 @@
         endif()
     endif()
 
-    if (ARG_LIB_PATCH_DIR)
+    if (ARG_LIB_PATCH_DIR AND (SOURCE_PATH_IS_DOWNLOAD OR ARG_LIB_FORCE_PATCH))
         # look for patch files
         file(GLOB PATCH_FILES "${ARG_LIB_PATCH_DIR}/*.patch")