浏览代码

Cleanup custom command .rule file internal handling

Teach cmMakefile::AddCustomCommandToOutput to return the cmSourceFile
instance to which the custom command is attached.  Use the return value
instead of separately adding a .rule extension and searching for the
source.  Mark CMake-generated .rule files explicitly with a property
instead of trusting the file extension.
Brad King 13 年之前
父节点
当前提交
f9b758e91a

+ 5 - 7
Source/cmGlobalVisualStudio8Generator.cxx

@@ -214,13 +214,11 @@ void cmGlobalVisualStudio8Generator::AddCheckTarget()
   // (this could be avoided with per-target source files)
   const char* no_main_dependency = 0;
   const char* no_working_directory = 0;
-  mf->AddCustomCommandToOutput(
-    stamps, listFiles,
-    no_main_dependency, commandLines, "Checking Build System",
-    no_working_directory, true);
-  std::string ruleName = stamps[0];
-  ruleName += ".rule";
-  if(cmSourceFile* file = mf->GetSource(ruleName.c_str()))
+  if(cmSourceFile* file =
+     mf->AddCustomCommandToOutput(
+       stamps, listFiles,
+       no_main_dependency, commandLines, "Checking Build System",
+       no_working_directory, true))
     {
     tgt->AddSourceFile(file);
     }

+ 1 - 1
Source/cmLocalVisualStudio6Generator.cxx

@@ -320,7 +320,7 @@ void cmLocalVisualStudio6Generator::WriteDSPFile(std::ostream& fout,
     sourceGroup.AssignSource(*i);
     // while we are at it, if it is a .rule file then for visual studio 6 we
     // must generate it
-    if ((*i)->GetExtension() == "rule")
+    if ((*i)->GetPropertyAsBool("__CMAKE_RULE"))
       {
       if(!cmSystemTools::FileExists(source.c_str()))
         {

+ 18 - 18
Source/cmMakefile.cxx

@@ -883,7 +883,7 @@ cmMakefile::AddCustomCommandToTarget(const char* target,
 }
 
 //----------------------------------------------------------------------------
-void
+cmSourceFile*
 cmMakefile::AddCustomCommandToOutput(const std::vector<std::string>& outputs,
                                      const std::vector<std::string>& depends,
                                      const char* main_dependency,
@@ -897,7 +897,7 @@ cmMakefile::AddCustomCommandToOutput(const std::vector<std::string>& outputs,
   if(outputs.empty())
     {
     cmSystemTools::Error("Attempt to add a custom rule with no output!");
-    return;
+    return 0;
     }
 
   // Validate custom commands.  TODO: More strict?
@@ -910,7 +910,7 @@ cmMakefile::AddCustomCommandToOutput(const std::vector<std::string>& outputs,
       cmOStringStream e;
       e << "COMMAND may not contain literal quotes:\n  " << cl[0] << "\n";
       this->IssueMessage(cmake::FATAL_ERROR, e.str());
-      return;
+      return 0;
       }
     }
 
@@ -928,7 +928,7 @@ cmMakefile::AddCustomCommandToOutput(const std::vector<std::string>& outputs,
         {
         // The existing custom command is identical.  Silently ignore
         // the duplicate.
-        return;
+        return file;
         }
       else
         {
@@ -970,11 +970,12 @@ cmMakefile::AddCustomCommandToOutput(const std::vector<std::string>& outputs,
                              outName.c_str(),
                              "\" which already has a custom rule.");
         }
-      return;
+      return file;
       }
 
     // Create a cmSourceFile for the rule file.
     file = this->GetOrCreateSource(outName.c_str(), true);
+    file->SetProperty("__CMAKE_RULE", "1");
     }
 
   // Always create the output sources and mark them generated.
@@ -1004,10 +1005,11 @@ cmMakefile::AddCustomCommandToOutput(const std::vector<std::string>& outputs,
     cc->SetEscapeAllowMakeVars(true);
     file->SetCustomCommand(cc);
     }
+  return file;
 }
 
 //----------------------------------------------------------------------------
-void
+cmSourceFile*
 cmMakefile::AddCustomCommandToOutput(const char* output,
                                      const std::vector<std::string>& depends,
                                      const char* main_dependency,
@@ -1019,9 +1021,9 @@ cmMakefile::AddCustomCommandToOutput(const char* output,
 {
   std::vector<std::string> outputs;
   outputs.push_back(output);
-  this->AddCustomCommandToOutput(outputs, depends, main_dependency,
-                                 commandLines, comment, workingDir,
-                                 replace, escapeOldStyle);
+  return this->AddCustomCommandToOutput(outputs, depends, main_dependency,
+                                        commandLines, comment, workingDir,
+                                        replace, escapeOldStyle);
 }
 
 //----------------------------------------------------------------------------
@@ -1054,13 +1056,14 @@ cmMakefile::AddCustomCommandOldStyle(const char* target,
     {
     // Get the name of this output.
     const char* output = oi->c_str();
+    cmSourceFile* sf;
 
     // Choose whether to use a main dependency.
     if(sourceFiles.find(source))
       {
       // The source looks like a real file.  Use it as the main dependency.
-      this->AddCustomCommandToOutput(output, depends, source,
-                                     commandLines, comment, 0);
+      sf = this->AddCustomCommandToOutput(output, depends, source,
+                                          commandLines, comment, 0);
       }
     else
       {
@@ -1068,20 +1071,18 @@ cmMakefile::AddCustomCommandOldStyle(const char* target,
       const char* no_main_dependency = 0;
       std::vector<std::string> depends2 = depends;
       depends2.push_back(source);
-      this->AddCustomCommandToOutput(output, depends2, no_main_dependency,
-                                     commandLines, comment, 0);
+      sf = this->AddCustomCommandToOutput(output, depends2, no_main_dependency,
+                                          commandLines, comment, 0);
       }
 
     // If the rule was added to the source (and not a .rule file),
     // then add the source to the target to make sure the rule is
     // included.
-    std::string sname = output;
-    sname += ".rule";
-    if(!this->GetSource(sname.c_str()))
+    if(sf && !sf->GetPropertyAsBool("__CMAKE_RULE"))
       {
       if (this->Targets.find(target) != this->Targets.end())
         {
-        this->Targets[target].AddSource(source);
+        this->Targets[target].AddSourceFile(sf);
         }
       else
         {
@@ -1976,7 +1977,6 @@ cmSourceFile *cmMakefile::GetSourceFileWithOutput(const char *cname)
 
   // look through all the source files that have custom commands
   // and see if the custom command has the passed source file as an output
-  // keep in mind the possible .rule extension that may be tacked on
   for(std::vector<cmSourceFile*>::const_iterator i =
         this->SourceFiles.begin(); i != this->SourceFiles.end(); ++i)
     {

+ 16 - 14
Source/cmMakefile.h

@@ -175,20 +175,22 @@ public:
                                 cmTarget::CustomCommandType type,
                                 const char* comment, const char* workingDir,
                                 bool escapeOldStyle = true);
-  void AddCustomCommandToOutput(const std::vector<std::string>& outputs,
-                                const std::vector<std::string>& depends,
-                                const char* main_dependency,
-                                const cmCustomCommandLines& commandLines,
-                                const char* comment, const char* workingDir,
-                                bool replace = false,
-                                bool escapeOldStyle = true);
-  void AddCustomCommandToOutput(const char* output,
-                                const std::vector<std::string>& depends,
-                                const char* main_dependency,
-                                const cmCustomCommandLines& commandLines,
-                                const char* comment, const char* workingDir,
-                                bool replace = false,
-                                bool escapeOldStyle = true);
+  cmSourceFile* AddCustomCommandToOutput(
+    const std::vector<std::string>& outputs,
+    const std::vector<std::string>& depends,
+    const char* main_dependency,
+    const cmCustomCommandLines& commandLines,
+    const char* comment, const char* workingDir,
+    bool replace = false,
+    bool escapeOldStyle = true);
+  cmSourceFile* AddCustomCommandToOutput(
+    const char* output,
+    const std::vector<std::string>& depends,
+    const char* main_dependency,
+    const cmCustomCommandLines& commandLines,
+    const char* comment, const char* workingDir,
+    bool replace = false,
+    bool escapeOldStyle = true);
   void AddCustomCommandOldStyle(const char* target,
                                 const std::vector<std::string>& outputs,
                                 const std::vector<std::string>& depends,

+ 1 - 1
Source/cmVisualStudio10TargetGenerator.cxx

@@ -460,7 +460,7 @@ cmVisualStudio10TargetGenerator::WriteCustomRule(cmSourceFile* source,
 {
   std::string sourcePath = source->GetFullPath();
   // the rule file seems to need to exist for vs10
-  if (source->GetExtension() == "rule")
+  if (source->GetPropertyAsBool("__CMAKE_RULE"))
     {
     if(!cmSystemTools::FileExists(sourcePath.c_str()))
       {