From 2a3cb52b7e85722d9a9ceed6a0ef221a8a9288bc Mon Sep 17 00:00:00 2001
From: Tim Haase <timhaase@gmx.net>
Date: Tue, 28 Jun 2022 11:30:31 +0200
Subject: [PATCH] Switch from parent scope to cache for internal variables

If this routine is called in a scope nested deeper than one, the parent scope
is not the global scope and it breaks.
---
 README.md                        |  2 +-
 src/cmake/CompareToLastRun.cmake | 40 ++++++++++++++++++++------------
 2 files changed, 26 insertions(+), 16 deletions(-)

diff --git a/README.md b/README.md
index 6e01229..d4b8e35 100644
--- a/README.md
+++ b/README.md
@@ -13,7 +13,7 @@ include(FetchContent)
 FetchContent_Declare(
     CompareToLastRun
     GIT_REPOSITORY "https://github.com/lepus2589/CompareToLastRun.git"
-    GIT_TAG v1.0
+    GIT_TAG v1.1
 )
 FetchContent_MakeAvailable(CompareToLastRun)
 FetchContent_GetProperties(
diff --git a/src/cmake/CompareToLastRun.cmake b/src/cmake/CompareToLastRun.cmake
index 9d77142..42cdb99 100644
--- a/src/cmake/CompareToLastRun.cmake
+++ b/src/cmake/CompareToLastRun.cmake
@@ -24,10 +24,10 @@ SOFTWARE.
 
 #[[
 This function creates a backup of the variable to check and returns `true`, if
-the variable is different from the last CMake run. It creates two variables in
-the parent scope (`_LAST_RUN_CHECKED_${VARIABLE_NAME_TO_CHECK}`,
-`_LAST_RUN_DIFFERS_${VARIABLE_NAME_TO_CHECK}`). It also creates the backup cache
-variable (`_LAST_RUN_${VARIABLE_NAME_TO_CHECK}`).
+the variable is different from the last CMake run. It creates three variables
+in the cache (`_LAST_RUN_CHECKED_${VARIABLE_NAME_TO_CHECK}`,
+`_LAST_RUN_DIFFERS_${VARIABLE_NAME_TO_CHECK}`,
+`_LAST_RUN_${VARIABLE_NAME_TO_CHECK}`).
 \param IS_DIFFERENT The result variable name
 \param VARIABLE_NAME_TO_CHECK The variable name to check
 ]]
@@ -38,23 +38,33 @@ function (compare_to_last_run IS_DIFFERENT VARIABLE_NAME_TO_CHECK)
         # the result is always true.
         if (_LAST_RUN_${VARIABLE_NAME_TO_CHECK} STREQUAL ${VARIABLE_NAME_TO_CHECK})
             # Write the result to variable in local function scope.
-            set(_LAST_RUN_DIFFERS_${VARIABLE_NAME_TO_CHECK} False)
-            # Write the result to variable in parent scope. This is used as a
-            # cache for subsequent queries for the same variable during this run
-            # of CMake.
-            set(_LAST_RUN_DIFFERS_${VARIABLE_NAME_TO_CHECK} False PARENT_SCOPE)
+            set(
+                _LAST_RUN_DIFFERS_${VARIABLE_NAME_TO_CHECK}
+                False
+                CACHE
+                INTERNAL
+                "Result flag of ${VARIABLE_NAME_TO_CHECK} variable for this run."
+            )
         else ()
             # Write the result to variable in local function scope.
-            set(_LAST_RUN_DIFFERS_${VARIABLE_NAME_TO_CHECK} True)
-            # Write the result to variable in parent scope. This is used as a
-            # cache for subsequent queries for the same variable during this run
-            # of CMake.
-            set(_LAST_RUN_DIFFERS_${VARIABLE_NAME_TO_CHECK} True PARENT_SCOPE)
+            set(
+                _LAST_RUN_DIFFERS_${VARIABLE_NAME_TO_CHECK}
+                True
+                CACHE
+                INTERNAL
+                "Result flag of ${VARIABLE_NAME_TO_CHECK} variable for this run."
+            )
         endif ()
 
         # The check has been performed once. It will not be checked again this
         # run.
-        set(_LAST_RUN_CHECKED_${VARIABLE_NAME_TO_CHECK} True PARENT_SCOPE)
+        set(
+            _LAST_RUN_CHECKED_${VARIABLE_NAME_TO_CHECK}
+            True
+            CACHE
+            INTERNAL
+            "Checked flag of ${VARIABLE_NAME_TO_CHECK} variable for this run."
+        )
         # Update the backup in the cache for next run.
         set(
             _LAST_RUN_${VARIABLE_NAME_TO_CHECK}
-- 
GitLab