cmExtraCodeBlocksGenerator: port to cmXMLWriter

This commit is contained in:
Daniel Pfeifer 2015-07-16 21:52:35 +02:00 committed by Brad King
parent d740762181
commit 27e0976453
2 changed files with 144 additions and 88 deletions

View File

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

View File

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