cmDSWWriter.cxx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #include "cmCacheManager.h"
  38. cmDSWMakefile::cmDSWMakefile(cmMakefile* m)
  39. {
  40. m_Makefile = m;
  41. }
  42. // output the DSW file
  43. void cmDSWMakefile::OutputDSWFile()
  44. {
  45. // if this is an out of source build, create the output directory
  46. if(strcmp(m_Makefile->GetStartOutputDirectory(),
  47. m_Makefile->GetHomeDirectory()) != 0)
  48. {
  49. if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
  50. {
  51. cmSystemTools::Error("Error creating output directory for DSW file",
  52. m_Makefile->GetStartOutputDirectory());
  53. }
  54. }
  55. // create the dsw file name
  56. std::string fname;
  57. fname = m_Makefile->GetStartOutputDirectory();
  58. fname += "/";
  59. fname += m_Makefile->GetProjectName();
  60. fname += ".dsw";
  61. std::ofstream fout(fname.c_str());
  62. if(!fout)
  63. {
  64. cmSystemTools::Error("Error can not open DSW file for write: "
  65. ,fname.c_str());
  66. return;
  67. }
  68. this->WriteDSWFile(fout);
  69. }
  70. // Write a DSW file to the stream
  71. void cmDSWMakefile::WriteDSWFile(std::ostream& fout)
  72. {
  73. // Write out the header for a DSW file
  74. this->WriteDSWHeader(fout);
  75. // Create a list of cmMakefile created from all the
  76. // CMakeLists.txt files that are in sub directories of
  77. // this one.
  78. std::vector<cmMakefile*> allListFiles;
  79. m_Makefile->FindSubDirectoryCMakeListsFiles(allListFiles);
  80. // For each cmMakefile, create a DSP for it, and
  81. // add it to this DSW file
  82. for(std::vector<cmMakefile*>::iterator k = allListFiles.begin();
  83. k != allListFiles.end(); ++k)
  84. {
  85. cmMakefile* mf = *k;
  86. // Create an MS generator with DSW off, so it only creates dsp files
  87. cmMSProjectGenerator* pg = new cmMSProjectGenerator;
  88. pg->BuildDSWOff();
  89. mf->SetMakefileGenerator(pg);
  90. mf->GenerateMakefile();
  91. // Get the source directory from the makefile
  92. std::string dir = mf->GetStartDirectory();
  93. // Get the home directory with the trailing slash
  94. std::string homedir = m_Makefile->GetHomeDirectory();
  95. homedir += "/";
  96. // remove the home directory and / from the source directory
  97. // this gives a relative path
  98. cmSystemTools::ReplaceString(dir, homedir.c_str(), "");
  99. // Get the list of create dsp files names from the cmDSPMakefile, more
  100. // than one dsp could have been created per input CMakeLists.txt file
  101. // for each target
  102. std::vector<std::string> dspnames = pg->GetDSPMakefile()->GetCreatedProjectNames();
  103. const cmTargets &tgts = pg->GetDSPMakefile()->GetMakefile()->GetTargets();
  104. cmTargets::const_iterator l = tgts.begin();
  105. for(std::vector<std::string>::iterator si = dspnames.begin();
  106. l != tgts.end(); ++l, ++si)
  107. {
  108. // Write the project into the DSW file
  109. this->WriteProject(fout, si->c_str(), dir.c_str(),
  110. pg->GetDSPMakefile(),l->second);
  111. }
  112. // delete the cmMakefile which also deletes the cmMSProjectGenerator
  113. delete mf;
  114. }
  115. // Write the footer for the DSW file
  116. this->WriteDSWFooter(fout);
  117. }
  118. // Write a dsp file into the DSW file,
  119. // Note, that dependencies from executables to
  120. // the libraries it uses are also done here
  121. void cmDSWMakefile::WriteProject(std::ostream& fout,
  122. const char* dspname,
  123. const char* dir,
  124. cmDSPMakefile* project,
  125. const cmTarget &l)
  126. {
  127. fout << "#########################################################"
  128. "######################\n\n";
  129. fout << "Project: \"" << dspname << "\"="
  130. << dir << "\\" << dspname << ".dsp - Package Owner=<4>\n\n";
  131. fout << "Package=<5>\n{{{\n}}}\n\n";
  132. fout << "Package=<4>\n";
  133. fout << "{{{\n";
  134. // insert Begin Project Dependency Project_Dep_Name project stuff here
  135. cmTarget::LinkLibraries::const_iterator j, jend;
  136. j = l.GetLinkLibraries().begin();
  137. jend = l.GetLinkLibraries().end();
  138. for(;j!= jend; ++j)
  139. {
  140. if(j->first != dspname)
  141. {
  142. if (!l.IsALibrary() ||
  143. project->GetLibraryBuildType() == cmDSPMakefile::DLL)
  144. {
  145. // is the library part of this DSW ? If so add dependency
  146. const char* cacheValue
  147. = cmCacheManager::GetInstance()->GetCacheValue(j->first.c_str());
  148. if(cacheValue)
  149. {
  150. fout << "Begin Project Dependency\n";
  151. fout << "Project_Dep_Name " << j->first << "\n";
  152. fout << "End Project Dependency\n";
  153. }
  154. }
  155. }
  156. }
  157. std::vector<std::string>::iterator i, end;
  158. // write utility dependencies.
  159. i = project->GetMakefile()->GetUtilities().begin();
  160. end = project->GetMakefile()->GetUtilities().end();
  161. for(;i!= end; ++i)
  162. {
  163. if(*i != dspname)
  164. {
  165. fout << "Begin Project Dependency\n";
  166. fout << "Project_Dep_Name " << *i << "\n";
  167. fout << "End Project Dependency\n";
  168. }
  169. }
  170. fout << "}}}\n\n";
  171. }
  172. // Standard end of dsw file
  173. void cmDSWMakefile::WriteDSWFooter(std::ostream& fout)
  174. {
  175. fout << "######################################################"
  176. "#########################\n\n";
  177. fout << "Global:\n\n";
  178. fout << "Package=<5>\n{{{\n}}}\n\n";
  179. fout << "Package=<3>\n{{{\n}}}\n\n";
  180. fout << "#####################################################"
  181. "##########################\n\n";
  182. }
  183. // ouput standard header for dsw file
  184. void cmDSWMakefile::WriteDSWHeader(std::ostream& fout)
  185. {
  186. fout << "Microsoft Developer Studio Workspace File, Format Version 6.00\n";
  187. fout << "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\n\n";
  188. }