Merge topic 'CMP0126-WARNING' into release-3.21

16208ac113 CMP0126: Add control for warnings

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: Ben Boeckel <ben.boeckel@kitware.com>
Merge-request: !6282
diff --git a/Help/policy/CMP0126.rst b/Help/policy/CMP0126.rst
index 4c8e928..0ced8fa 100644
--- a/Help/policy/CMP0126.rst
+++ b/Help/policy/CMP0126.rst
@@ -15,6 +15,8 @@
 This policy was introduced in CMake version 3.21. Use the
 :command:`cmake_policy` command to set it to ``OLD`` or ``NEW`` explicitly.
 Unlike many policies, CMake version |release| does *not* warn when the policy
-is not set and simply uses ``OLD`` behavior.
+is not set and simply uses ``OLD`` behavior.  See documentation of the
+:variable:`CMAKE_POLICY_WARNING_CMP0126 <CMAKE_POLICY_WARNING_CMP<NNNN>>`
+variable to control the warning.
 
 .. include:: DEPRECATED.txt
diff --git a/Help/variable/CMAKE_POLICY_WARNING_CMPNNNN.rst b/Help/variable/CMAKE_POLICY_WARNING_CMPNNNN.rst
index 9f68741..b991db2 100644
--- a/Help/variable/CMAKE_POLICY_WARNING_CMPNNNN.rst
+++ b/Help/variable/CMAKE_POLICY_WARNING_CMPNNNN.rst
@@ -29,6 +29,8 @@
   policy :policy:`CMP0112`.
 * ``CMAKE_POLICY_WARNING_CMP0116`` controls the warning for
   policy :policy:`CMP0116`.
+* ``CMAKE_POLICY_WARNING_CMP0126`` controls the warning for
+  policy :policy:`CMP0126`.
 
 This variable should not be set by a project in CMake code.  Project
 developers running CMake may set this variable in their cache to
diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx
index d2d34f9..c970abe 100644
--- a/Source/cmMakefile.cxx
+++ b/Source/cmMakefile.cxx
@@ -1962,9 +1962,26 @@
     }
   }
   this->GetCMakeInstance()->AddCacheEntry(name, value, doc, type);
-  if (this->GetPolicyStatus(cmPolicies::CMP0126) != cmPolicies::NEW) {
-    // if there was a definition then remove it
-    this->StateSnapshot.RemoveDefinition(name);
+  switch (this->GetPolicyStatus(cmPolicies::CMP0126)) {
+    case cmPolicies::WARN:
+      if (this->PolicyOptionalWarningEnabled("CMAKE_POLICY_WARNING_CMP0126") &&
+          this->IsNormalDefinitionSet(name)) {
+        this->IssueMessage(
+          MessageType::AUTHOR_WARNING,
+          cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0126),
+                   "\nFor compatibility with older versions of CMake, normal "
+                   "variable \"",
+                   name, "\" will be removed from the current scope."));
+      }
+      CM_FALLTHROUGH;
+    case cmPolicies::OLD:
+      // if there was a definition then remove it
+      this->StateSnapshot.RemoveDefinition(name);
+      break;
+    case cmPolicies::NEW:
+    case cmPolicies::REQUIRED_IF_USED:
+    case cmPolicies::REQUIRED_ALWAYS:
+      break;
   }
 }
 
diff --git a/Source/cmProjectCommand.cxx b/Source/cmProjectCommand.cxx
index acdb09f..6950c19 100644
--- a/Source/cmProjectCommand.cxx
+++ b/Source/cmProjectCommand.cxx
@@ -71,7 +71,7 @@
   // CMAKE_PROJECT_NAME will match PROJECT_NAME, and cmake --build
   // will work.
   if (!mf.GetDefinition("CMAKE_PROJECT_NAME") || mf.IsRootMakefile()) {
-    mf.AddDefinition("CMAKE_PROJECT_NAME", projectName);
+    mf.RemoveDefinition("CMAKE_PROJECT_NAME");
     mf.AddCacheDefinition("CMAKE_PROJECT_NAME", projectName,
                           "Value Computed by CMake", cmStateEnums::STATIC);
   }
@@ -395,7 +395,7 @@
   // in the same CMakeLists.txt file, and it is the top level
   // CMakeLists.txt file, then go with the last one.
   if (!mf.GetDefinition(name) || mf.IsRootMakefile()) {
-    mf.AddDefinition(name, value);
+    mf.RemoveDefinition(name);
     mf.AddCacheDefinition(name, value, "Value Computed by CMake",
                           cmStateEnums::STATIC);
   }
diff --git a/Tests/RunCMake/CMP0126/CMP0126-WARN-default.cmake b/Tests/RunCMake/CMP0126/CMP0126-WARN-default.cmake
new file mode 100644
index 0000000..3147fc4
--- /dev/null
+++ b/Tests/RunCMake/CMP0126/CMP0126-WARN-default.cmake
@@ -0,0 +1,3 @@
+
+set(MY_VAR 1)
+set(MY_VAR 2 CACHE STRING "")
diff --git a/Tests/RunCMake/CMP0126/CMP0126-WARN-stderr.txt b/Tests/RunCMake/CMP0126/CMP0126-WARN-stderr.txt
new file mode 100644
index 0000000..2301511
--- /dev/null
+++ b/Tests/RunCMake/CMP0126/CMP0126-WARN-stderr.txt
@@ -0,0 +1,10 @@
+CMake Warning \(dev\) at CMP0126-WARN.cmake:[0-9]+ \(set\):
+  Policy CMP0126 is not set: set\(CACHE\) does not remove a normal variable of
+  the same name\.  Run "cmake --help-policy CMP0126" for policy details\.  Use
+  the cmake_policy command to set the policy and suppress this warning\.
+
+  For compatibility with older versions of CMake, normal variable "MY_VAR"
+  will be removed from the current scope\.
+Call Stack \(most recent call first\):
+  CMakeLists.txt:[0-9]+ \(include\)
+This warning is for project developers\.  Use -Wno-dev to suppress it\.
diff --git a/Tests/RunCMake/CMP0126/CMP0126-WARN.cmake b/Tests/RunCMake/CMP0126/CMP0126-WARN.cmake
new file mode 100644
index 0000000..111c824
--- /dev/null
+++ b/Tests/RunCMake/CMP0126/CMP0126-WARN.cmake
@@ -0,0 +1,5 @@
+
+set(CMAKE_POLICY_WARNING_CMP0126 1)
+
+set(MY_VAR 1)
+set(MY_VAR 2 CACHE STRING "")
diff --git a/Tests/RunCMake/CMP0126/RunCMakeTest.cmake b/Tests/RunCMake/CMP0126/RunCMakeTest.cmake
index ae988f4..77c3878 100644
--- a/Tests/RunCMake/CMP0126/RunCMakeTest.cmake
+++ b/Tests/RunCMake/CMP0126/RunCMakeTest.cmake
@@ -4,3 +4,5 @@
 run_cmake_with_options(CMP0126-OLD_CL -DVAR=3)
 run_cmake(CMP0126-NEW)
 run_cmake_with_options(CMP0126-NEW_CL -DVAR=3)
+run_cmake(CMP0126-WARN)
+run_cmake(CMP0126-WARN-default)