cmDSWWriter.cxx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #include "cmDSWMakefile.h"
  33. #include "cmStandardIncludes.h"
  34. #include "cmSystemTools.h"
  35. #include "cmDSPMakefile.h"
  36. #include "cmMSProjectGenerator.h"
  37. cmDSWMakefile::cmDSWMakefile(cmMakefile* m)
  38. {
  39. m_Makefile = m;
  40. }
  41. // output the DSW file
  42. void cmDSWMakefile::OutputDSWFile()
  43. {
  44. // if this is an out of source build, create the output directory
  45. if(strcmp(m_Makefile->GetStartOutputDirectory(),
  46. m_Makefile->GetHomeDirectory()) != 0)
  47. {
  48. if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
  49. {
  50. cmSystemTools::Error("Error creating output directory for DSW file",
  51. m_Makefile->GetStartOutputDirectory());
  52. }
  53. }
  54. // create the dsw file name
  55. std::string fname;
  56. fname = m_Makefile->GetStartOutputDirectory();
  57. fname += "/";
  58. fname += m_Makefile->GetProjectName();
  59. fname += ".dsw";
  60. std::ofstream fout(fname.c_str());
  61. if(!fout)
  62. {
  63. cmSystemTools::Error("Error can not open DSW file for write: "
  64. ,fname.c_str());
  65. return;
  66. }
  67. this->WriteDSWFile(fout);
  68. }
  69. // Write a DSW file to the stream
  70. void cmDSWMakefile::WriteDSWFile(std::ostream& fout)
  71. {
  72. // Write out the header for a DSW file
  73. this->WriteDSWHeader(fout);
  74. // Create a list of cmMakefile created from all the
  75. // CMakeLists.txt files that are in sub directories of
  76. // this one.
  77. std::vector<cmMakefile*> allListFiles;
  78. m_Makefile->FindSubDirectoryCMakeListsFiles(allListFiles);
  79. // For each cmMakefile, create a DSP for it, and
  80. // add it to this DSW file
  81. for(std::vector<cmMakefile*>::iterator k = allListFiles.begin();
  82. k != allListFiles.end(); ++k)
  83. {
  84. cmMakefile* mf = *k;
  85. // Create an MS generator with DSW off, so it only creates dsp files
  86. cmMSProjectGenerator* pg = new cmMSProjectGenerator;
  87. pg->BuildDSWOff();
  88. mf->SetMakefileGenerator(pg);
  89. mf->GenerateMakefile();
  90. // Get the source directory from the makefile
  91. std::string dir = mf->GetStartDirectory();
  92. // Get the home directory with the trailing slash
  93. std::string homedir = m_Makefile->GetHomeDirectory();
  94. homedir += "/";
  95. // remove the home directory and / from the source directory
  96. // this gives a relative path
  97. cmSystemTools::ReplaceString(dir, homedir.c_str(), "");
  98. // Get the list of create dsp files names from the cmDSPMakefile, more
  99. // than one dsp could have been created per input CMakeLists.txt file
  100. // for each target
  101. std::vector<std::string> dspnames = pg->GetDSPMakefile()->GetCreatedProjectNames();
  102. const cmTargets &tgts = pg->GetDSPMakefile()->GetMakefile()->GetTargets();
  103. cmTargets::const_iterator l = tgts.begin();
  104. for(std::vector<std::string>::iterator si = dspnames.begin();
  105. l != tgts.end(); ++l, ++si)
  106. {
  107. // Write the project into the DSW file
  108. this->WriteProject(fout, si->c_str(), dir.c_str(),
  109. pg->GetDSPMakefile(),l->second);
  110. }
  111. // delete the cmMakefile which also deletes the cmMSProjectGenerator
  112. delete mf;
  113. }
  114. // Write the footer for the DSW file
  115. this->WriteDSWFooter(fout);
  116. }
  117. // Write a dsp file into the DSW file,
  118. // Note, that dependencies from executables to
  119. // the libraries it uses are also done here
  120. void cmDSWMakefile::WriteProject(std::ostream& fout,
  121. const char* dspname,
  122. const char* dir,
  123. cmDSPMakefile* project,
  124. const cmTarget &l)
  125. {
  126. fout << "#########################################################"
  127. "######################\n\n";
  128. fout << "Project: \"" << dspname << "\"="
  129. << dir << "\\" << dspname << ".dsp - Package Owner=<4>\n\n";
  130. fout << "Package=<5>\n{{{\n}}}\n\n";
  131. fout << "Package=<4>\n";
  132. fout << "{{{\n";
  133. // insert Begin Project Dependency Project_Dep_Name project stuff here
  134. cmMakefile::LinkLibraries::const_iterator j, jend;
  135. j = project->GetMakefile()->GetLinkLibraries().begin();
  136. jend = project->GetMakefile()->GetLinkLibraries().end();
  137. for(;j!= jend; ++j)
  138. {
  139. if(j->first != dspname)
  140. {
  141. if (!l.IsALibrary() ||
  142. project->GetLibraryBuildType() == cmDSPMakefile::DLL)
  143. {
  144. fout << "Begin Project Dependency\n";
  145. fout << "Project_Dep_Name " << j->first << "\n";
  146. fout << "End Project Dependency\n";
  147. }
  148. }
  149. }
  150. std::vector<std::string>::iterator i, end;
  151. // write utility dependencies.
  152. i = project->GetMakefile()->GetUtilities().begin();
  153. end = project->GetMakefile()->GetUtilities().end();
  154. for(;i!= end; ++i)
  155. {
  156. if(*i != dspname)
  157. {
  158. fout << "Begin Project Dependency\n";
  159. fout << "Project_Dep_Name " << *i << "\n";
  160. fout << "End Project Dependency\n";
  161. }
  162. }
  163. fout << "}}}\n\n";
  164. }
  165. // Standard end of dsw file
  166. void cmDSWMakefile::WriteDSWFooter(std::ostream& fout)
  167. {
  168. fout << "######################################################"
  169. "#########################\n\n";
  170. fout << "Global:\n\n";
  171. fout << "Package=<5>\n{{{\n}}}\n\n";
  172. fout << "Package=<3>\n{{{\n}}}\n\n";
  173. fout << "#####################################################"
  174. "##########################\n\n";
  175. }
  176. // ouput standard header for dsw file
  177. void cmDSWMakefile::WriteDSWHeader(std::ostream& fout)
  178. {
  179. fout << "Microsoft Developer Studio Workspace File, Format Version 6.00\n";
  180. fout << "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\n\n";
  181. }