cmVisualStudio10TargetGenerator: XML refactoring

This commit is contained in:
Vitaly Stakhovsky
2018-04-26 13:45:29 -04:00
parent e52cf1034f
commit 4465a27882
2 changed files with 49 additions and 56 deletions

View File

@@ -42,7 +42,7 @@ struct cmVisualStudio10TargetGenerator::Elem
std::ostream& S; std::ostream& S;
int Indent; int Indent;
bool HasElements = false; bool HasElements = false;
const char* Tag = nullptr; std::string Tag;
Elem(std::ostream& s, int i) Elem(std::ostream& s, int i)
: S(s) : S(s)
@@ -71,7 +71,7 @@ struct cmVisualStudio10TargetGenerator::Elem
} }
} }
std::ostream& WriteString(const char* line); std::ostream& WriteString(const char* line);
Elem& StartElement(const char* tag) Elem& StartElement(const std::string& tag)
{ {
this->Tag = tag; this->Tag = tag;
this->WriteString("<") << tag; this->WriteString("<") << tag;
@@ -103,7 +103,7 @@ struct cmVisualStudio10TargetGenerator::Elem
{ {
S << ">" << cmVS10EscapeXML(val) << "</" << this->Tag << ">\n"; S << ">" << cmVS10EscapeXML(val) << "</" << this->Tag << ">\n";
} }
void WriteEndTag(const char* tag) void WriteEndTag(const std::string& tag)
{ {
if (HasElements) { if (HasElements) {
this->WriteString("</") << tag << ">\n"; this->WriteString("</") << tag << ">\n";
@@ -277,7 +277,6 @@ std::string cmVisualStudio10TargetGenerator::CalcCondition(
void cmVisualStudio10TargetGenerator::WritePlatformConfigTag( void cmVisualStudio10TargetGenerator::WritePlatformConfigTag(
const char* tag, const std::string& config, int indentLevel, const char* tag, const std::string& config, int indentLevel,
const char* attribute) const char* attribute)
{ {
std::ostream& stream = *this->BuildFileStream; std::ostream& stream = *this->BuildFileStream;
stream.fill(' '); stream.fill(' ');
@@ -924,7 +923,8 @@ void cmVisualStudio10TargetGenerator::WriteXamlFilesGroup()
std::vector<cmSourceFile const*> xamlObjs; std::vector<cmSourceFile const*> xamlObjs;
this->GeneratorTarget->GetXamlSources(xamlObjs, ""); this->GeneratorTarget->GetXamlSources(xamlObjs, "");
if (!xamlObjs.empty()) { if (!xamlObjs.empty()) {
this->WriteString("<ItemGroup>\n", 1); Elem e1(*this->BuildFileStream, 1);
e1.StartElement("ItemGroup");
for (cmSourceFile const* oi : xamlObjs) { for (cmSourceFile const* oi : xamlObjs) {
std::string obj = oi->GetFullPath(); std::string obj = oi->GetFullPath();
const char* xamlType; const char* xamlType;
@@ -935,8 +935,8 @@ void cmVisualStudio10TargetGenerator::WriteXamlFilesGroup()
xamlType = "Page"; xamlType = "Page";
} }
Elem e2(*this->BuildFileStream, 2); Elem e2(e1);
this->WriteSource(xamlType, oi); this->WriteSource(e2, xamlType, oi);
e2.SetHasElements(); e2.SetHasElements();
if (this->ProjectType == csproj && !this->InSourceBuild) { if (this->ProjectType == csproj && !this->InSourceBuild) {
// add <Link> tag to written XAML source if necessary // add <Link> tag to written XAML source if necessary
@@ -952,13 +952,13 @@ void cmVisualStudio10TargetGenerator::WriteXamlFilesGroup()
} }
if (!link.empty()) { if (!link.empty()) {
ConvertToWindowsSlash(link); ConvertToWindowsSlash(link);
this->WriteElem("Link", link, 3); e2.Element("Link", link);
} }
} }
this->WriteElem("SubType", "Designer", 3); e2.Element("SubType", "Designer");
e2.WriteEndTag(xamlType); e2.EndElement();
} }
this->WriteString("</ItemGroup>\n", 1); e1.EndElement();
} }
} }
@@ -1249,13 +1249,7 @@ void cmVisualStudio10TargetGenerator::WriteCustomCommand(
} }
if (cmCustomCommand const* command = sf->GetCustomCommand()) { if (cmCustomCommand const* command = sf->GetCustomCommand()) {
// C# projects write their <Target> within WriteCustomRule() // C# projects write their <Target> within WriteCustomRule()
if (this->ProjectType != csproj) {
this->WriteString("<ItemGroup>\n", 1);
}
this->WriteCustomRule(sf, *command); this->WriteCustomRule(sf, *command);
if (this->ProjectType != csproj) {
this->WriteString("</ItemGroup>\n", 1);
}
} }
} }
} }
@@ -1290,21 +1284,22 @@ void cmVisualStudio10TargetGenerator::WriteCustomRule(
} }
cmLocalVisualStudio7Generator* lg = this->LocalGenerator; cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
Elem e2(*this->BuildFileStream, 2); Elem e1(*this->BuildFileStream, 1);
e1.StartElement("ItemGroup");
Elem e2(e1);
if (this->ProjectType != csproj) { if (this->ProjectType != csproj) {
this->WriteSource("CustomBuild", source); this->WriteSource(e2, "CustomBuild", source);
e2.SetHasElements(); e2.SetHasElements();
} else { } else {
this->WriteString("<ItemGroup>\n", 1);
std::string link; std::string link;
this->GetCSharpSourceLink(source, link); this->GetCSharpSourceLink(source, link);
this->WriteSource("None", source); this->WriteSource(e2, "None", source);
e2.SetHasElements(); e2.SetHasElements();
if (!link.empty()) { if (!link.empty()) {
this->WriteElem("Link", link, 3); e2.Element("Link", link);
} }
e2.WriteEndTag("None"); e2.EndElement();
this->WriteString("</ItemGroup>\n", 1); e1.EndElement();
} }
for (std::string const& c : this->Configurations) { for (std::string const& c : this->Configurations) {
cmCustomCommandGenerator ccg(command, c, lg); cmCustomCommandGenerator ccg(command, c, lg);
@@ -1341,7 +1336,8 @@ void cmVisualStudio10TargetGenerator::WriteCustomRule(
} }
} }
if (this->ProjectType != csproj) { if (this->ProjectType != csproj) {
e2.WriteEndTag("CustomBuild"); e2.EndElement();
e1.EndElement();
} }
} }
@@ -1370,19 +1366,20 @@ void cmVisualStudio10TargetGenerator::WriteCustomRuleCSharp(
std::string const& outputs, std::string const& comment) std::string const& outputs, std::string const& comment)
{ {
this->CSharpCustomCommandNames.insert(name); this->CSharpCustomCommandNames.insert(name);
std::stringstream attributes; Elem e1(*this->BuildFileStream, 1);
attributes << "\n Name=\"" << name << "\""; e1.StartElement("Target");
attributes << "\n Inputs=\"" << cmVS10EscapeAttr(inputs) << "\""; e1.Attribute("Condition", this->CalcCondition(config));
attributes << "\n Outputs=\"" << cmVS10EscapeAttr(outputs) << "\""; e1.S << "\n Name=\"" << name << "\"";
this->WritePlatformConfigTag("Target", config, 1, attributes.str().c_str()); e1.S << "\n Inputs=\"" << cmVS10EscapeAttr(inputs) << "\"";
e1.S << "\n Outputs=\"" << cmVS10EscapeAttr(outputs) << "\"";
if (!comment.empty()) { if (!comment.empty()) {
this->WriteString("<Exec Command=\"", 2); Elem(e1)
(*this->BuildFileStream) << "echo " << cmVS10EscapeAttr(comment) .StartElement("Exec")
<< "\" />\n"; .Attribute("Command", "echo " + comment)
.EndElement();
} }
this->WriteString("<Exec Command=\"", 2); Elem(e1).StartElement("Exec").Attribute("Command", script).EndElement();
(*this->BuildFileStream) << cmVS10EscapeAttr(script) << "\" />\n"; e1.EndElement();
this->WriteString("</Target>\n", 1);
} }
std::string cmVisualStudio10TargetGenerator::ConvertPath( std::string cmVisualStudio10TargetGenerator::ConvertPath(
@@ -1605,16 +1602,14 @@ void cmVisualStudio10TargetGenerator::WriteHeaderSource(cmSourceFile const* sf)
{ {
std::string const& fileName = sf->GetFullPath(); std::string const& fileName = sf->GetFullPath();
Elem e2(*this->BuildFileStream, 2); Elem e2(*this->BuildFileStream, 2);
this->WriteSource("ClInclude", sf); this->WriteSource(e2, "ClInclude", sf);
if (this->IsResxHeader(fileName)) { if (this->IsResxHeader(fileName)) {
e2.SetHasElements(); e2.Element("FileType", "CppForm");
this->WriteElem("FileType", "CppForm", 3);
} else if (this->IsXamlHeader(fileName)) { } else if (this->IsXamlHeader(fileName)) {
std::string xamlFileName = fileName.substr(0, fileName.find_last_of(".")); std::string xamlFileName = fileName.substr(0, fileName.find_last_of("."));
e2.SetHasElements(); e2.Element("DependentUpon", xamlFileName);
this->WriteElem("DependentUpon", xamlFileName, 3);
} }
e2.WriteEndTag("ClInclude"); e2.EndElement();
} }
void cmVisualStudio10TargetGenerator::WriteExtraSource(cmSourceFile const* sf) void cmVisualStudio10TargetGenerator::WriteExtraSource(cmSourceFile const* sf)
@@ -1776,7 +1771,7 @@ void cmVisualStudio10TargetGenerator::WriteExtraSource(cmSourceFile const* sf)
} }
Elem e2(*this->BuildFileStream, 2); Elem e2(*this->BuildFileStream, 2);
this->WriteSource(tool, sf); this->WriteSource(e2, tool, sf);
if (toolHasSettings) { if (toolHasSettings) {
e2.SetHasElements(); e2.SetHasElements();
@@ -1880,7 +1875,8 @@ void cmVisualStudio10TargetGenerator::WriteExtraSource(cmSourceFile const* sf)
e2.WriteEndTag(tool); e2.WriteEndTag(tool);
} }
void cmVisualStudio10TargetGenerator::WriteSource(std::string const& tool, void cmVisualStudio10TargetGenerator::WriteSource(Elem& e2,
std::string const& tool,
cmSourceFile const* sf) cmSourceFile const* sf)
{ {
// Visual Studio tools append relative paths to the current dir, as in: // Visual Studio tools append relative paths to the current dir, as in:
@@ -1916,9 +1912,8 @@ void cmVisualStudio10TargetGenerator::WriteSource(std::string const& tool,
} }
} }
ConvertToWindowsSlash(sourceFile); ConvertToWindowsSlash(sourceFile);
this->WriteString("<", 2); e2.StartElement(tool.c_str());
(*this->BuildFileStream) << tool << " Include=\"" e2.Attribute("Include", sourceFile);
<< cmVS10EscapeAttr(sourceFile) << "\"";
ToolSource toolSource = { sf, forceRelative }; ToolSource toolSource = { sf, forceRelative };
this->Tools[tool].push_back(toolSource); this->Tools[tool].push_back(toolSource);
@@ -2025,14 +2020,14 @@ void cmVisualStudio10TargetGenerator::WriteAllSources()
std::back_inserter(exclude_configs)); std::back_inserter(exclude_configs));
Elem e2(*this->BuildFileStream, 2); Elem e2(*this->BuildFileStream, 2);
this->WriteSource(tool, si.Source); this->WriteSource(e2, tool, si.Source);
if (si.Kind == cmGeneratorTarget::SourceKindObjectSource) { if (si.Kind == cmGeneratorTarget::SourceKindObjectSource) {
this->OutputSourceSpecificFlags(e2, si.Source); this->OutputSourceSpecificFlags(e2, si.Source);
} }
if (!exclude_configs.empty()) { if (!exclude_configs.empty()) {
this->WriteExcludeFromBuild(e2, exclude_configs); this->WriteExcludeFromBuild(e2, exclude_configs);
} }
e2.WriteEndTag(tool); e2.EndElement();
} }
} }
@@ -2232,13 +2227,11 @@ void cmVisualStudio10TargetGenerator::OutputSourceSpecificFlags(
void cmVisualStudio10TargetGenerator::WriteExcludeFromBuild( void cmVisualStudio10TargetGenerator::WriteExcludeFromBuild(
Elem& e2, std::vector<size_t> const& exclude_configs) Elem& e2, std::vector<size_t> const& exclude_configs)
{ {
e2.SetHasElements();
for (size_t ci : exclude_configs) { for (size_t ci : exclude_configs) {
this->WriteString("", 3); Elem e3(e2, "ExcludedFromBuild");
(*this->BuildFileStream) e3.Attribute("Condition", "'$(Configuration)|$(Platform)'=='" +
<< "<ExcludedFromBuild Condition=\"'$(Configuration)|$(Platform)'=='" this->Configurations[ci] + "|" + this->Platform + "'");
<< cmVS10EscapeAttr(this->Configurations[ci]) << "|" e3.Content("true");
<< cmVS10EscapeAttr(this->Platform) << "'\">true</ExcludedFromBuild>\n";
} }
} }

View File

@@ -68,7 +68,7 @@ private:
void WriteHeaderSource(cmSourceFile const* sf); void WriteHeaderSource(cmSourceFile const* sf);
void WriteExtraSource(cmSourceFile const* sf); void WriteExtraSource(cmSourceFile const* sf);
void WriteNsightTegraConfigurationValues(std::string const& config); void WriteNsightTegraConfigurationValues(std::string const& config);
void WriteSource(std::string const& tool, cmSourceFile const* sf); void WriteSource(Elem& e2, std::string const& tool, cmSourceFile const* sf);
void WriteExcludeFromBuild(Elem&, void WriteExcludeFromBuild(Elem&,
std::vector<size_t> const& exclude_configs); std::vector<size_t> const& exclude_configs);
void WriteAllSources(); void WriteAllSources();