From 4a9dd4aad401c6310b4c546aa5442b7f684e7343 Mon Sep 17 00:00:00 2001 From: Brad King Date: Fri, 12 Jun 2009 15:28:48 -0400 Subject: [PATCH] ENH: Refactor VS 7,8,9 build event generation In cmLocalVisualStudio7Generator we generate pre-build, pre-link, and post-build events into project files. This refactors the generation code for the three event types into a private EventWriter class to avoid duplicate code. --- Source/cmLocalVisualStudio7Generator.cxx | 196 +++++++++-------------- Source/cmLocalVisualStudio7Generator.h | 3 + 2 files changed, 79 insertions(+), 120 deletions(-) diff --git a/Source/cmLocalVisualStudio7Generator.cxx b/Source/cmLocalVisualStudio7Generator.cxx index bb061ea9d..72ae0e603 100644 --- a/Source/cmLocalVisualStudio7Generator.cxx +++ b/Source/cmLocalVisualStudio7Generator.cxx @@ -549,6 +549,64 @@ private: bool& flag_handled); }; +//---------------------------------------------------------------------------- +// Helper class to write build event elements. +class cmLocalVisualStudio7Generator::EventWriter +{ +public: + EventWriter(cmLocalVisualStudio7Generator* lg, + const char* config, std::ostream& os): + LG(lg), Config(config), Stream(os), First(true) {} + void Start(const char* tool) + { + this->First = true; + this->Stream << "\t\t\tStream << (this->First? "" : "\"") << "/>\n"; + } + void Write(std::vector const& ccs) + { + for(std::vector::const_iterator ci = ccs.begin(); + ci != ccs.end(); ++ci) + { + this->Write(*ci); + } + } + void Write(cmCustomCommand const& cc) + { + if(this->First) + { + const char* comment = cc.GetComment(); + if(comment && *comment) + { + this->Stream << "\nDescription=\"" + << this->LG->EscapeForXML(comment) << "\""; + } + this->Stream << "\nCommandLine=\""; + this->First = false; + } + else + { + this->Stream << this->LG->EscapeForXML("\n"); + } + std::string script = + this->LG->ConstructScript(cc.GetCommandLines(), + cc.GetWorkingDirectory(), + this->Config, + cc.GetEscapeOldStyle(), + cc.GetEscapeAllowMakeVars()); + this->Stream << this->LG->EscapeForXML(script.c_str()); + } +private: + cmLocalVisualStudio7Generator* LG; + const char* Config; + std::ostream& Stream; + bool First; +}; + +//---------------------------------------------------------------------------- void cmLocalVisualStudio7Generator::WriteConfiguration(std::ostream& fout, const char* configName, const char *libName, @@ -1628,128 +1686,26 @@ void cmLocalVisualStudio7Generator { return; } - const char* tool = "VCPreBuildEventTool"; - if(this->FortranProject) - { - tool = "VFPreBuildEventTool"; - } - // add the pre build rules - fout << "\t\t\t::const_iterator cr = - target.GetPreBuildCommands().begin(); - cr != target.GetPreBuildCommands().end(); ++cr) - { - if(!init) - { - const char* comment = cr->GetComment(); - if(comment && *comment) - { - fout << "\nDescription=\"" - << this->EscapeForXML(comment) << "\""; - } - fout << "\nCommandLine=\""; - init = true; - } - else - { - fout << this->EscapeForXML("\n"); - } - std::string script = - this->ConstructScript(cr->GetCommandLines(), - cr->GetWorkingDirectory(), - configName, - cr->GetEscapeOldStyle(), - cr->GetEscapeAllowMakeVars()); - fout << this->EscapeForXML(script.c_str()).c_str(); - } - if (init) - { - fout << "\""; - } - fout << "/>\n"; + EventWriter event(this, configName, fout); - // add the pre Link rules - tool = "VCPreLinkEventTool"; - if(this->FortranProject) - { - tool = "VFPreLinkEventTool"; - } - fout << "\t\t\t::const_iterator cr = - target.GetPreLinkCommands().begin(); - cr != target.GetPreLinkCommands().end(); ++cr) - { - if(!init) - { - const char* comment = cr->GetComment(); - if(comment && *comment) - { - fout << "\nDescription=\"" - << this->EscapeForXML(comment) << "\""; - } - fout << "\nCommandLine=\""; - init = true; - } - else - { - fout << this->EscapeForXML("\n"); - } - std::string script = - this->ConstructScript(cr->GetCommandLines(), - cr->GetWorkingDirectory(), - configName, - cr->GetEscapeOldStyle(), - cr->GetEscapeAllowMakeVars()); - fout << this->EscapeForXML(script.c_str()).c_str(); - } - if (init) - { - fout << "\""; - } - fout << "/>\n"; + // Add pre-build event. + const char* tool = + this->FortranProject? "VFPreBuildEventTool":"VCPreBuildEventTool"; + event.Start(tool); + event.Write(target.GetPreBuildCommands()); + event.Finish(); - // add the PostBuild rules - tool = "VCPostBuildEventTool"; - if(this->FortranProject) - { - tool = "VFPostBuildEventTool"; - } - fout << "\t\t\t::const_iterator cr = - target.GetPostBuildCommands().begin(); - cr != target.GetPostBuildCommands().end(); ++cr) - { - if(!init) - { - const char* comment = cr->GetComment(); - if(comment && *comment) - { - fout << "\nDescription=\"" - << this->EscapeForXML(comment) << "\""; - } - fout << "\nCommandLine=\""; - init = true; - } - else - { - fout << this->EscapeForXML("\n"); - } - std::string script = - this->ConstructScript(cr->GetCommandLines(), - cr->GetWorkingDirectory(), - configName, - cr->GetEscapeOldStyle(), - cr->GetEscapeAllowMakeVars()); - fout << this->EscapeForXML(script.c_str()).c_str(); - } - if (init) - { - fout << "\""; - } - fout << "/>\n"; + // Add pre-link event. + tool = this->FortranProject? "VFPreLinkEventTool":"VCPreLinkEventTool"; + event.Start(tool); + event.Write(target.GetPreLinkCommands()); + event.Finish(); + + // Add post-build event. + tool = this->FortranProject? "VFPostBuildEventTool":"VCPostBuildEventTool"; + event.Start(tool); + event.Write(target.GetPostBuildCommands()); + event.Finish(); } void diff --git a/Source/cmLocalVisualStudio7Generator.h b/Source/cmLocalVisualStudio7Generator.h index edb90aa87..8796130a2 100644 --- a/Source/cmLocalVisualStudio7Generator.h +++ b/Source/cmLocalVisualStudio7Generator.h @@ -123,6 +123,9 @@ private: friend class cmLocalVisualStudio7GeneratorFCInfo; friend class cmLocalVisualStudio7GeneratorInternals; + class EventWriter; + friend class EventWriter; + cmVS7FlagTable const* ExtraFlagTable; std::string ModuleDefinitionFile; int Version;