浏览代码

ENH: Add AppendProperty methods for use by C++ code in CMake. Simplify implementation of SET_PROPERTY command by using them.

Brad King 18 年之前
父节点
当前提交
caca9b8065

+ 36 - 0
Source/cmMakefile.cxx

@@ -2613,6 +2613,42 @@ void cmMakefile::SetProperty(const char* prop, const char* value)
   this->Properties.SetProperty(prop,value,cmProperty::DIRECTORY);
 }
 
+void cmMakefile::AppendProperty(const char* prop, const char* value)
+{
+  if (!prop)
+    {
+    return;
+    }
+
+  // handle special props
+  std::string propname = prop;
+  if ( propname == "INCLUDE_DIRECTORIES" )
+    {
+    std::vector<std::string> varArgsExpanded;
+    cmSystemTools::ExpandListArgument(value, varArgsExpanded);
+    for(std::vector<std::string>::const_iterator vi = varArgsExpanded.begin();
+        vi != varArgsExpanded.end(); ++vi)
+      {
+      this->AddIncludeDirectory(vi->c_str());
+      }
+    return;
+    }
+
+  if ( propname == "LINK_DIRECTORIES" )
+    {
+    std::vector<std::string> varArgsExpanded;
+    cmSystemTools::ExpandListArgument(value, varArgsExpanded);
+    for(std::vector<std::string>::const_iterator vi = varArgsExpanded.begin();
+        vi != varArgsExpanded.end(); ++vi)
+      {
+      this->AddLinkDirectory(vi->c_str());
+      }
+    return;
+    }
+
+  this->Properties.AppendProperty(prop,value,cmProperty::DIRECTORY);
+}
+
 const char *cmMakefile::GetPropertyOrDefinition(const char* prop)
 {
   const char *ret = this->GetProperty(prop, cmProperty::DIRECTORY);

+ 1 - 0
Source/cmMakefile.h

@@ -713,6 +713,7 @@ public:
 
   ///! Set/Get a property of this directory 
   void SetProperty(const char *prop, const char *value);
+  void AppendProperty(const char *prop, const char *value);
   const char *GetProperty(const char *prop);
   const char *GetPropertyOrDefinition(const char *prop);
   const char *GetProperty(const char *prop, cmProperty::ScopeType scope);

+ 11 - 0
Source/cmProperty.cxx

@@ -24,6 +24,17 @@ void cmProperty::Set(const char *name, const char *value)
   this->ValueHasBeenSet = true;
 }
 
+void cmProperty::Append(const char *name, const char *value)
+{
+  this->Name = name;
+  if(!this->Value.empty() && *value)
+    {
+    this->Value += ";";
+    }
+  this->Value += value;
+  this->ValueHasBeenSet = true;
+}
+
 const char *cmProperty::GetValue() const
 {
   if (this->ValueHasBeenSet)

+ 3 - 0
Source/cmProperty.h

@@ -28,6 +28,9 @@ public:
   // set this property
   void Set(const char *name, const char *value);
 
+  // append to this property
+  void Append(const char *name, const char *value);
+
   // get the value
   const char *GetValue() const;
 

+ 26 - 0
Source/cmPropertyMap.cxx

@@ -63,6 +63,32 @@ void cmPropertyMap::SetProperty(const char *name, const char *value,
   prop->Set(name,value);
 }
 
+void cmPropertyMap::AppendProperty(const char* name, const char* value,
+                                   cmProperty::ScopeType scope)
+{
+  // Skip if nothing to append.
+  if(!name || !value || !*value)
+    {
+    return;
+    }
+#ifdef CMAKE_STRICT
+  if (!this->CMakeInstance)
+    {
+    cmSystemTools::Error("CMakeInstance not set on a property map!");
+    abort();
+    }
+  else
+    {
+    this->CMakeInstance->RecordPropertyAccess(name,scope);
+    }
+#else
+  (void)scope;
+#endif
+
+  cmProperty *prop = this->GetOrCreateProperty(name);
+  prop->Append(name,value);
+}
+
 const char *cmPropertyMap
 ::GetPropertyValue(const char *name, 
                    cmProperty::ScopeType scope, 

+ 3 - 0
Source/cmPropertyMap.h

@@ -29,6 +29,9 @@ public:
   void SetProperty(const char *name, const char *value, 
                    cmProperty::ScopeType scope);
 
+  void AppendProperty(const char* name, const char* value,
+                      cmProperty::ScopeType scope);
+
   const char *GetPropertyValue(const char *name, 
                                cmProperty::ScopeType scope,
                                bool &chain) const;

+ 15 - 61
Source/cmSetPropertyCommand.cxx

@@ -128,37 +128,6 @@ bool cmSetPropertyCommand::InitialPass(std::vector<std::string> const& args)
   return true;
 }
 
-//----------------------------------------------------------------------------
-bool cmSetPropertyCommand::ConstructValue(std::string& value,
-                                          const char* old)
-{
-  if(this->AppendMode)
-    {
-    // This is an append.  Start with the original value.
-    if(old)
-      {
-      value = old;
-      }
-    }
-  else if(this->PropertyValue.empty())
-    {
-    // This is a set to no values.  Remove the property.
-    return false;
-    }
-
-  // Add the new value.
-  if(!this->PropertyValue.empty())
-    {
-    if(!value.empty())
-      {
-      value += ";";
-      }
-    value += this->PropertyValue;
-    }
-
-  return true;
-}
-
 //----------------------------------------------------------------------------
 bool cmSetPropertyCommand::HandleGlobalMode()
 {
@@ -171,16 +140,13 @@ bool cmSetPropertyCommand::HandleGlobalMode()
   // Set or append the property.
   cmake* cm = this->Makefile->GetCMakeInstance();
   const char* name = this->PropertyName.c_str();
-  std::string value;
-  if(this->ConstructValue(value, cm->GetProperty(name)))
+  if(this->AppendMode)
     {
-    // Set the new property.
-    cm->SetProperty(name, value.c_str());
+    cm->AppendProperty(name, this->PropertyValue.c_str());
     }
   else
     {
-    // Remove the property.
-    cm->SetProperty(name, 0);
+    cm->SetProperty(name, this->PropertyValue.c_str());
     }
 
   return true;
@@ -235,16 +201,13 @@ bool cmSetPropertyCommand::HandleDirectoryMode()
 
   // Set or append the property.
   const char* name = this->PropertyName.c_str();
-  std::string value;
-  if(this->ConstructValue(value, mf->GetProperty(name)))
+  if(this->AppendMode)
     {
-    // Set the new property.
-    mf->SetProperty(name, value.c_str());
+    mf->AppendProperty(name, this->PropertyValue.c_str());
     }
   else
     {
-    // Remove the property.
-    mf->SetProperty(name, 0);
+    mf->SetProperty(name, this->PropertyValue.c_str());
     }
 
   return true;
@@ -283,16 +246,13 @@ bool cmSetPropertyCommand::HandleTarget(cmTarget* target)
 {
   // Set or append the property.
   const char* name = this->PropertyName.c_str();
-  std::string value;
-  if(this->ConstructValue(value, target->GetProperty(name)))
+  if(this->AppendMode)
     {
-    // Set the new property.
-    target->SetProperty(name, value.c_str());
+    target->AppendProperty(name, this->PropertyValue.c_str());
     }
   else
     {
-    // Remove the property.
-    target->SetProperty(name, 0);
+    target->SetProperty(name, this->PropertyValue.c_str());
     }
 
   return true;
@@ -328,16 +288,13 @@ bool cmSetPropertyCommand::HandleSource(cmSourceFile* sf)
 {
   // Set or append the property.
   const char* name = this->PropertyName.c_str();
-  std::string value;
-  if(this->ConstructValue(value, sf->GetProperty(name)))
+  if(this->AppendMode)
     {
-    // Set the new property.
-    sf->SetProperty(name, value.c_str());
+    sf->AppendProperty(name, this->PropertyValue.c_str());
     }
   else
     {
-    // Remove the property.
-    sf->SetProperty(name, 0);
+    sf->SetProperty(name, this->PropertyValue.c_str());
     }
 
   // TODO: MACOSX_PACKAGE_LOCATION special case in
@@ -392,16 +349,13 @@ bool cmSetPropertyCommand::HandleTest(cmTest* test)
 {
   // Set or append the property.
   const char* name = this->PropertyName.c_str();
-  std::string value;
-  if(this->ConstructValue(value, test->GetProperty(name)))
+  if(this->AppendMode)
     {
-    // Set the new property.
-    test->SetProperty(name, value.c_str());
+    test->AppendProperty(name, this->PropertyValue.c_str());
     }
   else
     {
-    // Remove the property.
-    test->SetProperty(name, 0);
+    test->SetProperty(name, this->PropertyValue.c_str());
     }
 
   return true;

+ 0 - 3
Source/cmSetPropertyCommand.h

@@ -93,9 +93,6 @@ private:
   std::string PropertyValue;
   bool AppendMode;
 
-  // Implementation of value construction.
-  bool ConstructValue(std::string& value, const char* old);
-
   // Implementation of each property type.
   bool HandleGlobalMode();
   bool HandleDirectoryMode();

+ 10 - 0
Source/cmSourceFile.cxx

@@ -270,6 +270,16 @@ void cmSourceFile::SetProperty(const char* prop, const char* value)
   this->Properties.SetProperty(prop, value, cmProperty::SOURCE_FILE);
 }
 
+//----------------------------------------------------------------------------
+void cmSourceFile::AppendProperty(const char* prop, const char* value)
+{
+  if (!prop)
+    {
+    return;
+    }
+  this->Properties.AppendProperty(prop, value, cmProperty::SOURCE_FILE);
+}
+
 //----------------------------------------------------------------------------
 const char* cmSourceFile::GetProperty(const char* prop) const
 {

+ 1 - 0
Source/cmSourceFile.h

@@ -49,6 +49,7 @@ public:
 
   ///! Set/Get a property of this source file
   void SetProperty(const char *prop, const char *value);
+  void AppendProperty(const char* prop, const char* value);
   const char *GetProperty(const char *prop) const;
   bool GetPropertyAsBool(const char *prop) const;
 

+ 10 - 0
Source/cmTarget.cxx

@@ -1345,6 +1345,16 @@ void cmTarget::SetProperty(const char* prop, const char* value)
   this->Properties.SetProperty(prop, value, cmProperty::TARGET);
 }
 
+//----------------------------------------------------------------------------
+void cmTarget::AppendProperty(const char* prop, const char* value)
+{
+  if (!prop)
+    {
+    return;
+    }
+  this->Properties.AppendProperty(prop, value, cmProperty::TARGET);
+}
+
 //----------------------------------------------------------------------------
 void cmTarget::MarkAsImported()
 {

+ 1 - 0
Source/cmTarget.h

@@ -187,6 +187,7 @@ public:
 
   ///! Set/Get a property of this target file
   void SetProperty(const char *prop, const char *value);
+  void AppendProperty(const char* prop, const char* value);
   const char *GetProperty(const char *prop);
   const char *GetProperty(const char *prop, cmProperty::ScopeType scope);
   bool GetPropertyAsBool(const char *prop);

+ 10 - 0
Source/cmTest.cxx

@@ -84,6 +84,16 @@ void cmTest::SetProperty(const char* prop, const char* value)
   this->Properties.SetProperty(prop, value, cmProperty::TEST);
 }
 
+//----------------------------------------------------------------------------
+void cmTest::AppendProperty(const char* prop, const char* value)
+{
+  if (!prop)
+    {
+    return;
+    }
+  this->Properties.AppendProperty(prop, value, cmProperty::TEST);
+}
+
 //----------------------------------------------------------------------------
 void cmTest::SetMakefile(cmMakefile* mf)
 {

+ 1 - 0
Source/cmTest.h

@@ -52,6 +52,7 @@ public:
 
   ///! Set/Get a property of this source file
   void SetProperty(const char *prop, const char *value);
+  void AppendProperty(const char* prop, const char* value);
   const char *GetProperty(const char *prop) const;
   bool GetPropertyAsBool(const char *prop) const;
   cmPropertyMap &GetProperties() { return this->Properties; };

+ 9 - 0
Source/cmake.cxx

@@ -3435,6 +3435,15 @@ void cmake::SetProperty(const char* prop, const char* value)
   this->Properties.SetProperty(prop, value, cmProperty::GLOBAL);
 }
 
+void cmake::AppendProperty(const char* prop, const char* value)
+{
+  if (!prop)
+    {
+    return;
+    }
+  this->Properties.AppendProperty(prop, value, cmProperty::GLOBAL);
+}
+
 const char *cmake::GetProperty(const char* prop)
 {
   return this->GetProperty(prop, cmProperty::GLOBAL);

+ 1 - 0
Source/cmake.h

@@ -258,6 +258,7 @@ class cmake
 
   ///! Set/Get a property of this target file
   void SetProperty(const char *prop, const char *value);
+  void AppendProperty(const char *prop, const char *value);
   const char *GetProperty(const char *prop);
   const char *GetProperty(const char *prop, cmProperty::ScopeType scope);
   bool GetPropertyAsBool(const char *prop);