From 1222f02e343e804f1fcc8ff09e56dc13ac233d98 Mon Sep 17 00:00:00 2001
From: Robert Maynard <robert.maynard@kitware.com>
Date: Fri, 2 Nov 2018 17:27:09 -0400
Subject: If: Support the 'DEFINED CACHE{}' syntax

---
 Help/command/if.rst                            | 9 ++++-----
 Help/release/dev/if-supports-cache-defined.rst | 5 +++++
 Source/cmConditionEvaluator.cxx                | 6 ++++++
 Tests/Unset/CMakeLists.txt                     | 9 +++++++++
 4 files changed, 24 insertions(+), 5 deletions(-)
 create mode 100644 Help/release/dev/if-supports-cache-defined.rst

diff --git a/Help/command/if.rst b/Help/command/if.rst
index 1cd9965..a682c83 100644
--- a/Help/command/if.rst
+++ b/Help/command/if.rst
@@ -187,11 +187,10 @@ Possible conditions are:
 ``if(<variable|string> IN_LIST <variable>)``
  True if the given element is contained in the named list variable.
 
-``if(DEFINED <name>|ENV{<name>})``
- True if a variable or environment variable
- with given ``<name>`` is defined.
- The value of the variable does not matter.
- Note that macro arguments are not variables.
+``if(DEFINED <name>|CACHE{<name>}|ENV{<name>})``
+ True if a variable, cache variable or environment variable
+ with given ``<name>`` is defined. The value of the variable
+ does not matter. Note that macro arguments are not variables.
 
 ``if((condition) AND (condition OR (condition)))``
  The conditions inside the parenthesis are evaluated first and then
diff --git a/Help/release/dev/if-supports-cache-defined.rst b/Help/release/dev/if-supports-cache-defined.rst
new file mode 100644
index 0000000..1e700c0
--- /dev/null
+++ b/Help/release/dev/if-supports-cache-defined.rst
@@ -0,0 +1,5 @@
+if-supports-cache-defined
+-------------------------
+
+* The :command:`if` command gained support for checking if cache variables
+  are defined with the  ``DEFINED CACHE{VAR}`` syntax.
diff --git a/Source/cmConditionEvaluator.cxx b/Source/cmConditionEvaluator.cxx
index 172ef92..3b4206f 100644
--- a/Source/cmConditionEvaluator.cxx
+++ b/Source/cmConditionEvaluator.cxx
@@ -495,6 +495,12 @@ bool cmConditionEvaluator::HandleLevel1(cmArgumentList& newArgs, std::string&,
             argP1->GetValue().operator[](argP1len - 1) == '}') {
           std::string env = argP1->GetValue().substr(4, argP1len - 5);
           bdef = cmSystemTools::HasEnv(env);
+        } else if (argP1len > 6 &&
+                   argP1->GetValue().substr(0, 6) == "CACHE{" &&
+                   argP1->GetValue().operator[](argP1len - 1) == '}') {
+          std::string cache = argP1->GetValue().substr(6, argP1len - 7);
+          bdef =
+            this->Makefile.GetState()->GetCacheEntryValue(cache) != nullptr;
         } else {
           bdef = this->Makefile.IsDefinitionSet(argP1->GetValue());
         }
diff --git a/Tests/Unset/CMakeLists.txt b/Tests/Unset/CMakeLists.txt
index 07aa68e..a40367b 100644
--- a/Tests/Unset/CMakeLists.txt
+++ b/Tests/Unset/CMakeLists.txt
@@ -21,17 +21,26 @@ set(x 43)
 if(NOT x EQUAL 43)
   message(FATAL_ERROR "x!=43")
 endif()
+if(DEFINED CACHE{x})
+  message(FATAL_ERROR "x shouldn't be found in the cache")
+endif()
+
 set(x)
 if(DEFINED x)
   message(FATAL_ERROR "x should be undefined now!")
 endif()
 
+
 # Cache variable
 set(BAR "test" CACHE STRING "documentation")
 if(NOT DEFINED BAR)
   message(FATAL_ERROR "BAR not defined")
 endif()
 
+if(NOT DEFINED CACHE{BAR})
+  message(FATAL_ERROR "BAR could not be found by CACHE{BAR}")
+endif()
+
 # Test interaction of cache entries with variables.
 set(BAR "test-var")
 if(NOT "$CACHE{BAR}" STREQUAL "test")
-- 
cgit v0.12