Pārlūkot izejas kodu

cmake_policy: Add undocumented PARENT_SCOPE option to GET

Policies affecting the behavior of CMake-provided macros and functions
need to be able to get the policy setting as of the call site rather
than the definition site.  Add an undocumented option to do this.
Brad King 7 gadi atpakaļ
vecāks
revīzija
391a5837ee

+ 7 - 2
Source/cmCMakePolicyCommand.cxx

@@ -95,7 +95,11 @@ bool cmCMakePolicyCommand::HandleSetMode(std::vector<std::string> const& args)
 
 bool cmCMakePolicyCommand::HandleGetMode(std::vector<std::string> const& args)
 {
-  if (args.size() != 3) {
+  bool parent_scope = false;
+  if (args.size() == 4 && args[3] == "PARENT_SCOPE") {
+    // Undocumented PARENT_SCOPE option for use within CMake.
+    parent_scope = true;
+  } else if (args.size() != 3) {
     this->SetError("GET must be given exactly 2 additional arguments.");
     return false;
   }
@@ -115,7 +119,8 @@ bool cmCMakePolicyCommand::HandleGetMode(std::vector<std::string> const& args)
   }
 
   // Lookup the policy setting.
-  cmPolicies::PolicyStatus status = this->Makefile->GetPolicyStatus(pid);
+  cmPolicies::PolicyStatus status =
+    this->Makefile->GetPolicyStatus(pid, parent_scope);
   switch (status) {
     case cmPolicies::OLD:
       // Report that the policy is set to OLD.

+ 3 - 3
Source/cmMakefile.cxx

@@ -4089,10 +4089,10 @@ const char* cmMakefile::GetDefineFlagsCMP0059() const
   return this->DefineFlagsOrig.c_str();
 }
 
-cmPolicies::PolicyStatus cmMakefile::GetPolicyStatus(
-  cmPolicies::PolicyID id) const
+cmPolicies::PolicyStatus cmMakefile::GetPolicyStatus(cmPolicies::PolicyID id,
+                                                     bool parent_scope) const
 {
-  return this->StateSnapshot.GetPolicy(id);
+  return this->StateSnapshot.GetPolicy(id, parent_scope);
 }
 
 bool cmMakefile::PolicyOptionalWarningEnabled(std::string const& var)

+ 2 - 1
Source/cmMakefile.h

@@ -284,7 +284,8 @@ public:
      */
   bool SetPolicy(cmPolicies::PolicyID id, cmPolicies::PolicyStatus status);
   bool SetPolicy(const char* id, cmPolicies::PolicyStatus status);
-  cmPolicies::PolicyStatus GetPolicyStatus(cmPolicies::PolicyID id) const;
+  cmPolicies::PolicyStatus GetPolicyStatus(cmPolicies::PolicyID id,
+                                           bool parent_scope = false) const;
   bool SetPolicyVersion(std::string const& version_min,
                         std::string const& version_max);
   void RecordPolicies(cmPolicies::PolicyMap& pm);

+ 6 - 2
Source/cmStateSnapshot.cxx

@@ -160,8 +160,8 @@ void cmStateSnapshot::SetPolicy(cmPolicies::PolicyID id,
   }
 }
 
-cmPolicies::PolicyStatus cmStateSnapshot::GetPolicy(
-  cmPolicies::PolicyID id) const
+cmPolicies::PolicyStatus cmStateSnapshot::GetPolicy(cmPolicies::PolicyID id,
+                                                    bool parent_scope) const
 {
   cmPolicies::PolicyStatus status = cmPolicies::GetPolicyStatus(id);
 
@@ -180,6 +180,10 @@ cmPolicies::PolicyStatus cmStateSnapshot::GetPolicy(
     cmLinkedTree<cmStateDetail::PolicyStackEntry>::iterator root =
       dir->DirectoryEnd->PolicyRoot;
     for (; leaf != root; ++leaf) {
+      if (parent_scope) {
+        parent_scope = false;
+        continue;
+      }
       if (leaf->IsDefined(id)) {
         status = leaf->Get(id);
         return status;

+ 2 - 1
Source/cmStateSnapshot.h

@@ -43,7 +43,8 @@ public:
   cmStateEnums::SnapshotType GetType() const;
 
   void SetPolicy(cmPolicies::PolicyID id, cmPolicies::PolicyStatus status);
-  cmPolicies::PolicyStatus GetPolicy(cmPolicies::PolicyID id) const;
+  cmPolicies::PolicyStatus GetPolicy(cmPolicies::PolicyID id,
+                                     bool parent_scope = false) const;
   bool HasDefinedPolicyCMP0011();
   void PushPolicy(cmPolicies::PolicyMap const& entry, bool weak);
   bool PopPolicy();

+ 8 - 0
Tests/PolicyScope/CMakeLists.txt

@@ -52,6 +52,10 @@ if(1)
     # CMP0002 should be changed when this function is invoked
     cmake_policy(GET CMP0002 cmp)
     check(CMP0002 "OLD" "${cmp}")
+
+    # The undocumented PARENT_SCOPE option sees the caller's setting.
+    cmake_policy(GET CMP0002 cmp PARENT_SCOPE)
+    check(CMP0002 "NEW" "${cmp}")
   endfunction()
 
   # Unset CMP0002
@@ -61,6 +65,10 @@ if(1)
     cmake_policy(GET CMP0002 cmp)
     check(CMP0002 "" "${cmp}")
 
+    # The undocumented PARENT_SCOPE option sees the caller's setting.
+    cmake_policy(GET CMP0002 cmp PARENT_SCOPE)
+    check(CMP0002 "NEW" "${cmp}")
+
     # Setting the policy should work here and also in the caller.
     cmake_policy(SET CMP0002 OLD)
     cmake_policy(GET CMP0002 cmp)