浏览代码

cmExtraCodeBlocksGenerator: port to cmXMLWriter

Daniel Pfeifer 10 年之前
父节点
当前提交
27e0976453
共有 2 个文件被更改,包括 147 次插入91 次删除
  1. 145 89
      Source/cmExtraCodeBlocksGenerator.cxx
  2. 2 2
      Source/cmExtraCodeBlocksGenerator.h

+ 145 - 89
Source/cmExtraCodeBlocksGenerator.cxx

@@ -18,7 +18,7 @@
 #include "cmSourceFile.h"
 #include "cmGeneratedFileStream.h"
 #include "cmSystemTools.h"
-#include "cmXMLSafe.h"
+#include "cmXMLWriter.h"
 
 #include <cmsys/SystemTools.hxx>
 
@@ -101,11 +101,11 @@ struct Tree
   void InsertPath(const std::vector<std::string>& splitted,
                   std::vector<std::string>::size_type start,
                   const std::string& fileName);
-  void BuildVirtualFolder(std::string& virtualFolders) const;
+  void BuildVirtualFolder(cmXMLWriter& xml) const;
   void BuildVirtualFolderImpl(std::string& virtualFolders,
                               const std::string& prefix) const;
-  void BuildUnit(std::string& unitString, const std::string& fsPath) const;
-  void BuildUnitImpl(std::string& unitString,
+  void BuildUnit(cmXMLWriter& xml, const std::string& fsPath) const;
+  void BuildUnitImpl(cmXMLWriter& xml,
                      const std::string& virtualFolderPath,
                      const std::string& fsPath) const;
 };
@@ -159,16 +159,18 @@ void Tree::InsertPath(const std::vector<std::string>& splitted,
 }
 
 
-void Tree::BuildVirtualFolder(std::string& virtualFolders) const
+void Tree::BuildVirtualFolder(cmXMLWriter& xml) const
 {
-  virtualFolders += "<Option virtualFolders=\"CMake Files\\;";
+  xml.StartElement("Option");
+  std::string virtualFolders = "CMake Files\\;";
   for (std::vector<Tree>::const_iterator it = folders.begin();
      it != folders.end();
      ++it)
     {
     it->BuildVirtualFolderImpl(virtualFolders, "");
     }
-  virtualFolders += "\" />";
+  xml.Attribute("virtualFolders", virtualFolders);
+  xml.EndElement();
 }
 
 
@@ -185,26 +187,31 @@ void Tree::BuildVirtualFolderImpl(std::string& virtualFolders,
 }
 
 
-void Tree::BuildUnit(std::string& unitString, const std::string& fsPath) const
+void Tree::BuildUnit(cmXMLWriter& xml, const std::string& fsPath) const
 {
   for (std::vector<std::string>::const_iterator it = files.begin();
        it != files.end();
        ++it)
     {
-    unitString += "      <Unit filename=\"" + fsPath + *it + "\">\n";
-    unitString += "          <Option virtualFolder=\"CMake Files\\\" />\n";
-    unitString += "      </Unit>\n";
+    xml.StartElement("Unit");
+    xml.Attribute("filename", fsPath + *it);
+
+    xml.StartElement("Option");
+    xml.Attribute("virtualFolder", "CMake Files\\");
+    xml.EndElement();
+
+    xml.EndElement();
     }
   for (std::vector<Tree>::const_iterator it = folders.begin();
      it != folders.end();
      ++it)
     {
-    it->BuildUnitImpl(unitString, "", fsPath);
+    it->BuildUnitImpl(xml, "", fsPath);
     }
 }
 
 
-void Tree::BuildUnitImpl(std::string& unitString,
+void Tree::BuildUnitImpl(cmXMLWriter& xml,
                          const std::string& virtualFolderPath,
                          const std::string& fsPath) const
 {
@@ -212,16 +219,21 @@ void Tree::BuildUnitImpl(std::string& unitString,
        it != files.end();
        ++it)
     {
-    unitString += "      <Unit filename=\"" +fsPath+path+ "/" + *it + "\">\n";
-    unitString += "          <Option virtualFolder=\"CMake Files\\"
-               + virtualFolderPath + path + "\\\" />\n";
-    unitString += "      </Unit>\n";
+    xml.StartElement("Unit");
+    xml.Attribute("filename", fsPath + path + "/" + *it);
+
+    xml.StartElement("Option");
+    xml.Attribute("virtualFolder",
+        "CMake Files\\" + virtualFolderPath + path + "\\");
+    xml.EndElement();
+
+    xml.EndElement();
     }
   for (std::vector<Tree>::const_iterator it = folders.begin();
      it != folders.end();
      ++it)
     {
-    it->BuildUnitImpl(unitString,
+    it->BuildUnitImpl(xml,
                       virtualFolderPath + path + "\\", fsPath + path + "/");
     }
 }
@@ -289,30 +301,41 @@ void cmExtraCodeBlocksGenerator
       }
     }
 
-  // Now build a virtual tree string
-  std::string virtualFolders;
-  tree.BuildVirtualFolder(virtualFolders);
-  // And one for <Unit>
-  std::string unitFiles;
-  tree.BuildUnit(unitFiles, std::string(lgs[0]->GetSourceDirectory()) + "/");
-
   // figure out the compiler
   std::string compiler = this->GetCBCompilerId(mf);
   std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
   const std::string makeArgs = mf->GetSafeDefinition(
                                             "CMAKE_CODEBLOCKS_MAKE_ARGUMENTS");
 
-  fout<<"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n"
-        "<CodeBlocks_project_file>\n"
-        "   <FileVersion major=\"1\" minor=\"6\" />\n"
-        "   <Project>\n"
-        "      <Option title=\"" << lgs[0]->GetProjectName()<<"\" />\n"
-        "      <Option makefile_is_custom=\"1\" />\n"
-        "      <Option compiler=\"" << compiler << "\" />\n"
-        "      "<<virtualFolders<<"\n"
-        "      <Build>\n";
-
-  this->AppendTarget(fout, "all", 0, make.c_str(), lgs[0], compiler.c_str(),
+  cmXMLWriter xml(fout);
+  xml.StartDocument();
+  xml.StartElement("CodeBlocks_project_file");
+
+  xml.StartElement("FileVersion");
+  xml.Attribute("major", 1);
+  xml.Attribute("minor", 6);
+  xml.EndElement();
+
+  xml.StartElement("Project");
+
+  xml.StartElement("Option");
+  xml.Attribute("title", lgs[0]->GetProjectName());
+  xml.EndElement();
+
+  xml.StartElement("Option");
+  xml.Attribute("makefile_is_custom", 1);
+  xml.EndElement();
+
+  xml.StartElement("Option");
+  xml.Attribute("compiler", compiler);
+  xml.EndElement();
+
+  // Now build a virtual tree
+  tree.BuildVirtualFolder(xml);
+
+  xml.StartElement("Build");
+
+  this->AppendTarget(xml, "all", 0, make.c_str(), lgs[0], compiler.c_str(),
                      makeArgs);
 
   // add all executable and library targets and some of the GLOBAL
@@ -334,7 +357,7 @@ void cmExtraCodeBlocksGenerator
           if (strcmp((*lg)->GetCurrentBinaryDirectory(),
                      (*lg)->GetBinaryDirectory())==0)
             {
-            this->AppendTarget(fout, targetName, 0,
+            this->AppendTarget(xml, targetName, 0,
                                make.c_str(), *lg, compiler.c_str(),
                                makeArgs);
             }
@@ -352,7 +375,7 @@ void cmExtraCodeBlocksGenerator
             break;
             }
 
-          this->AppendTarget(fout, targetName, 0,
+          this->AppendTarget(xml, targetName, 0,
                                  make.c_str(), *lg, compiler.c_str(),makeArgs);
           break;
         case cmState::EXECUTABLE:
@@ -362,11 +385,11 @@ void cmExtraCodeBlocksGenerator
         case cmState::OBJECT_LIBRARY:
           {
           cmGeneratorTarget* gt = *ti;
-          this->AppendTarget(fout, targetName, gt,
+          this->AppendTarget(xml, targetName, gt,
                              make.c_str(), *lg, compiler.c_str(), makeArgs);
           std::string fastTarget = targetName;
           fastTarget += "/fast";
-          this->AppendTarget(fout, fastTarget, gt,
+          this->AppendTarget(xml, fastTarget, gt,
                              make.c_str(), *lg, compiler.c_str(), makeArgs);
           }
           break;
@@ -376,8 +399,7 @@ void cmExtraCodeBlocksGenerator
       }
     }
 
-  fout<<"      </Build>\n";
-
+  xml.EndElement(); // Build
 
   // Collect all used source files in the project.
   // Keep a list of C/C++ source files which might have an acompanying header
@@ -505,24 +527,27 @@ void cmExtraCodeBlocksGenerator
     std::string const& unitFilename = sit->first;
     CbpUnit const& unit = sit->second;
 
-    fout<<"      <Unit filename=\""<< cmXMLSafe(unitFilename) <<"\">\n";
+    xml.StartElement("Unit");
+    xml.Attribute("filename", unitFilename);
 
     for(std::vector<const cmGeneratorTarget*>::const_iterator ti =
         unit.Targets.begin();
         ti != unit.Targets.end(); ++ti)
       {
-      std::string const& targetName = (*ti)->GetName();
-      fout<<"         <Option target=\""<< cmXMLSafe(targetName) <<"\"/>\n";
+      xml.StartElement("Option");
+      xml.Attribute("target", (*ti)->GetName());
+      xml.EndElement();
       }
 
-    fout<<"      </Unit>\n";
+    xml.EndElement();
     }
 
   // Add CMakeLists.txt
-  fout<<unitFiles;
+  tree.BuildUnit(xml, std::string(mf->GetHomeDirectory()) + "/");
 
-  fout<<"   </Project>\n"
-        "</CodeBlocks_project_file>\n";
+  xml.EndElement(); // Project
+  xml.EndElement(); // CodeBlocks_project_file
+  xml.EndDocument();
 }
 
 
@@ -553,7 +578,7 @@ std::string cmExtraCodeBlocksGenerator::CreateDummyTargetFile(
 
 
 // Generate the xml code for one target.
-void cmExtraCodeBlocksGenerator::AppendTarget(cmGeneratedFileStream& fout,
+void cmExtraCodeBlocksGenerator::AppendTarget(cmXMLWriter& xml,
                                               const std::string& targetName,
                                               cmGeneratorTarget* target,
                                               const char* make,
@@ -565,7 +590,9 @@ void cmExtraCodeBlocksGenerator::AppendTarget(cmGeneratedFileStream& fout,
   std::string makefileName = lg->GetCurrentBinaryDirectory();
   makefileName += "/Makefile";
 
-  fout<<"      <Target title=\"" << targetName << "\">\n";
+  xml.StartElement("Target");
+  xml.Attribute("title", targetName);
+
   if (target!=0)
     {
     int cbTargetType = this->GetCBTargetType(target);
@@ -603,13 +630,29 @@ void cmExtraCodeBlocksGenerator::AppendTarget(cmGeneratedFileStream& fout,
       location = target->GetLocation(buildType);
       }
 
-    fout<<"         <Option output=\"" << location
-                            << "\" prefix_auto=\"0\" extension_auto=\"0\" />\n"
-          "         <Option working_dir=\"" << workingDir << "\" />\n"
-          "         <Option object_output=\"./\" />\n"
-          "         <Option type=\"" << cbTargetType << "\" />\n"
-          "         <Option compiler=\"" << compiler << "\" />\n"
-          "         <Compiler>\n";
+    xml.StartElement("Option");
+    xml.Attribute("output", location);
+    xml.Attribute("prefix_auto", 0);
+    xml.Attribute("extension_auto", 0);
+    xml.EndElement();
+
+    xml.StartElement("Option");
+    xml.Attribute("working_dir", workingDir);
+    xml.EndElement();
+
+    xml.StartElement("Option");
+    xml.Attribute("object_output", "./");
+    xml.EndElement();
+
+    xml.StartElement("Option");
+    xml.Attribute("type", cbTargetType);
+    xml.EndElement();
+
+    xml.StartElement("Option");
+    xml.Attribute("compiler", compiler);
+    xml.EndElement();
+
+    xml.StartElement("Compiler");
 
     // the compilerdefines for this target
     std::vector<std::string> cdefs;
@@ -619,8 +662,9 @@ void cmExtraCodeBlocksGenerator::AppendTarget(cmGeneratedFileStream& fout,
     for(std::vector<std::string>::const_iterator di = cdefs.begin();
         di != cdefs.end(); ++di)
       {
-      cmXMLSafe safedef(di->c_str());
-      fout <<"            <Add option=\"-D" << safedef.str() << "\" />\n";
+      xml.StartElement("Add");
+      xml.Attribute("option", "-D" + *di);
+      xml.EndElement();
       }
 
     // the include directories for this target
@@ -653,36 +697,48 @@ void cmExtraCodeBlocksGenerator::AppendTarget(cmGeneratedFileStream& fout,
         dirIt != uniqIncludeDirs.end();
         ++dirIt)
       {
-      fout <<"            <Add directory=\"" << *dirIt << "\" />\n";
+      xml.StartElement("Add");
+      xml.Attribute("directory", *dirIt);
+      xml.EndElement();
       }
 
-    fout<<"         </Compiler>\n";
+    xml.EndElement(); // Compiler
     }
   else // e.g. all and the GLOBAL and UTILITY targets
     {
-    fout<<"         <Option working_dir=\""
-        << lg->GetCurrentBinaryDirectory() << "\" />\n"
-        <<"         <Option type=\"" << 4 << "\" />\n";
-    }
-
-  fout<<"         <MakeCommands>\n"
-        "            <Build command=\""
-      << this->BuildMakeCommand(make, makefileName.c_str(), targetName,
-                                makeFlags)
-      << "\" />\n"
-        "            <CompileFile command=\""
-      << this->BuildMakeCommand(make, makefileName.c_str(),"&quot;$file&quot;",
-                                makeFlags)
-      << "\" />\n"
-        "            <Clean command=\""
-      << this->BuildMakeCommand(make, makefileName.c_str(), "clean", makeFlags)
-      << "\" />\n"
-        "            <DistClean command=\""
-      << this->BuildMakeCommand(make, makefileName.c_str(), "clean", makeFlags)
-      << "\" />\n"
-        "         </MakeCommands>\n"
-        "      </Target>\n";
+    xml.StartElement("Option");
+    xml.Attribute("working_dir", lg->GetCurrentBinaryDirectory());
+    xml.EndElement();
+
+    xml.StartElement("Option");
+    xml.Attribute("type", 4);
+    xml.EndElement();
+    }
+
+  xml.StartElement("MakeCommands");
+
+  xml.StartElement("Build");
+  xml.Attribute("command",
+    this->BuildMakeCommand(make, makefileName.c_str(), targetName, makeFlags));
+  xml.EndElement();
+
+  xml.StartElement("CompileFile");
+  xml.Attribute("command",
+    this->BuildMakeCommand(make, makefileName.c_str(),"\"$file\"", makeFlags));
+  xml.EndElement();
 
+  xml.StartElement("Clean");
+  xml.Attribute("command",
+    this->BuildMakeCommand(make, makefileName.c_str(), "clean", makeFlags));
+  xml.EndElement();
+
+  xml.StartElement("DistClean");
+  xml.Attribute("command",
+    this->BuildMakeCommand(make, makefileName.c_str(), "clean", makeFlags));
+  xml.EndElement();
+
+  xml.EndElement(); //MakeCommands
+  xml.EndElement(); //Target
 }
 
 
@@ -825,7 +881,7 @@ std::string cmExtraCodeBlocksGenerator::BuildMakeCommand(
     // http://public.kitware.com/Bug/view.php?id=13952
     std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
     command += " /NOLOGO /f ";
-    command += cmXMLSafe(makefileName).str();
+    command += makefileName;
     command += " VERBOSE=1 ";
     command += target;
     }
@@ -834,9 +890,9 @@ std::string cmExtraCodeBlocksGenerator::BuildMakeCommand(
     // no escaping of spaces in this case, see
     // http://public.kitware.com/Bug/view.php?id=10014
     std::string makefileName = makefile;
-    command += " -f &quot;";
+    command += " -f \"";
     command += makefileName;
-    command += "&quot; ";
+    command += "\" ";
     command += " VERBOSE=1 ";
     command += target;
     }
@@ -848,9 +904,9 @@ std::string cmExtraCodeBlocksGenerator::BuildMakeCommand(
   else
     {
     std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
-    command += " -f &quot;";
+    command += " -f \"";
     command += makefileName;
-    command += "&quot; ";
+    command += "\" ";
     command += " VERBOSE=1 ";
     command += target;
     }

+ 2 - 2
Source/cmExtraCodeBlocksGenerator.h

@@ -18,7 +18,7 @@
 class cmLocalGenerator;
 class cmMakefile;
 class cmGeneratorTarget;
-class cmGeneratedFileStream;
+class cmXMLWriter;
 
 /** \class cmExtraCodeBlocksGenerator
  * \brief Write CodeBlocks project files for Makefile based projects
@@ -56,7 +56,7 @@ private:
   std::string BuildMakeCommand(const std::string& make, const char* makefile,
                                const std::string& target,
                                const std::string& makeFlags);
-  void AppendTarget(cmGeneratedFileStream& fout,
+  void AppendTarget(cmXMLWriter& xml,
                     const std::string& targetName,
                     cmGeneratorTarget* target,
                     const char* make,