Browse Source

ENH: fix list command with empty elements

Bill Hoffman 17 years ago
parent
commit
0a0672c01f
3 changed files with 69 additions and 2 deletions
  1. 56 2
      Source/cmListCommand.cxx
  2. 12 0
      Source/cmPolicies.cxx
  3. 1 0
      Source/cmPolicies.h

+ 56 - 2
Source/cmListCommand.cxx

@@ -103,8 +103,62 @@ bool cmListCommand::GetList(std::vector<std::string>& list, const char* var)
     {
     return false;
     }
-  // expand the variable
-  cmSystemTools::ExpandListArgument(listString, list);
+  // expand the variable into a list
+  cmSystemTools::ExpandListArgument(listString, list, true);
+  // check the list for empty values
+  bool hasEmpty = false;
+  for(std::vector<std::string>::iterator i = list.begin(); 
+      i != list.end(); ++i)
+    {
+    if(i->size() == 0)
+      {
+      hasEmpty = true;
+      break;
+      }
+    }
+  // if no empty elements then just return 
+  if(!hasEmpty)
+    {
+    return true;
+    }
+  // if we have empty elements we need to check policy CMP0007
+  switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0007))
+    {
+    case cmPolicies::WARN: 
+      {
+      // Default is to warn and use old behavior
+      // OLD behavior is to allow compatibility, so recall
+      // ExpandListArgument without the true which will remove
+      // empty values
+      list.clear();
+      cmSystemTools::ExpandListArgument(listString, list);
+      std::string warn = this->Makefile->GetPolicies()->
+        GetPolicyWarning(cmPolicies::CMP0007);
+      warn += " List has value = [";
+      warn += listString;
+      warn += "].";
+      this->Makefile->IssueMessage(cmake::AUTHOR_WARNING,
+                                   warn);
+      return true;
+      }
+    case cmPolicies::OLD:
+      // OLD behavior is to allow compatibility, so recall
+      // ExpandListArgument without the true which will remove
+      // empty values
+      list.clear();
+      cmSystemTools::ExpandListArgument(listString, list);
+      return true;
+    case cmPolicies::NEW:
+      return true;
+    case cmPolicies::REQUIRED_IF_USED:
+    case cmPolicies::REQUIRED_ALWAYS:
+      this->Makefile->IssueMessage(
+        cmake::FATAL_ERROR,
+        this->Makefile->GetPolicies()
+        ->GetRequiredPolicyError(cmPolicies::CMP0007)
+        );
+      return false;
+    }
   return true;
 }
 

+ 12 - 0
Source/cmPolicies.cxx

@@ -269,6 +269,18 @@ cmPolicies::cmPolicies()
     "The NEW behavior for this policy is to produce an error if a bundle "
     "target is installed without a BUNDLE DESTINATION.",
     2,6,0, cmPolicies::WARN);
+  
+  this->DefinePolicy(
+    CMP0007, "CMP0007",
+    "list command no longer ignores empty elements.",
+    "This policy determines whether the list command will "
+    "ignore empty elements in the list. " 
+    "CMake 2.4 and below list commands ignored all empty elements"
+    " in the list.  For example, a;b;;c would have length 3 and not 4. "
+    "The OLD behavior for this policy is to ignore empty list elements. "
+    "The NEW behavior for this policy is to correctly count empty "
+    "elements in a list. ",
+    2,6,0, cmPolicies::WARN);
 }
 
 cmPolicies::~cmPolicies()

+ 1 - 0
Source/cmPolicies.h

@@ -47,6 +47,7 @@ public:
     CMP0004, // Libraries linked may not have leading or trailing whitespace
     CMP0005, // Definition value escaping
     CMP0006, // BUNDLE install rules needed for MACOSX_BUNDLE targets
+    CMP0007, // list command handling of empty elements
 
     // Always the last entry.  Useful mostly to avoid adding a comma
     // the last policy when adding a new one.