cmDSWWriter.cxx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. // add this makefile to the list
  80. allListFiles.push_back(m_Makefile);
  81. // add a special target that depends on ALL projects for easy build
  82. // of Debug only
  83. m_Makefile->AddUtilityCommand("ALL_BUILD", "echo \"Build all projects\"", false);
  84. m_Makefile->FindSubDirectoryCMakeListsFiles(allListFiles);
  85. // For each cmMakefile, create a DSP for it, and
  86. // add it to this DSW file
  87. for(std::vector<cmMakefile*>::iterator k = allListFiles.begin();
  88. k != allListFiles.end(); ++k)
  89. {
  90. cmMakefile* mf = *k;
  91. // Create an MS generator with DSW off, so it only creates dsp files
  92. cmMSProjectGenerator* pg = new cmMSProjectGenerator;
  93. pg->BuildDSWOff();
  94. mf->SetMakefileGenerator(pg);
  95. mf->GenerateMakefile();
  96. // Get the source directory from the makefile
  97. std::string dir = mf->GetStartDirectory();
  98. // Get the home directory with the trailing slash
  99. std::string homedir = m_Makefile->GetHomeDirectory();
  100. homedir += "/";
  101. // remove the home directory and / from the source directory
  102. // this gives a relative path
  103. cmSystemTools::ReplaceString(dir, homedir.c_str(), "");
  104. // Get the list of create dsp files names from the cmDSPMakefile, more
  105. // than one dsp could have been created per input CMakeLists.txt file
  106. // for each target
  107. std::vector<std::string> dspnames = pg->GetDSPMakefile()->GetCreatedProjectNames();
  108. cmTargets &tgts = pg->GetDSPMakefile()->GetMakefile()->GetTargets();
  109. cmTargets::iterator l = tgts.begin();
  110. for(std::vector<std::string>::iterator si = dspnames.begin();
  111. l != tgts.end(); ++l)
  112. {
  113. // special handling for the current makefile
  114. if(mf == m_Makefile)
  115. {
  116. dir = "."; // no subdirectory for project generated
  117. // if this is the special ALL_BUILD utility, then
  118. // make it depend on every other non UTILITY project.
  119. // This is done by adding the names to the GetUtilities
  120. // vector on the makefile
  121. if(l->first == "ALL_BUILD")
  122. {
  123. for(std::vector<cmMakefile*>::iterator a = allListFiles.begin();
  124. a != allListFiles.end(); ++a)
  125. {
  126. const cmTargets &atgts = (*a)->GetTargets();
  127. for(cmTargets::const_iterator al = atgts.begin();
  128. al != atgts.end(); ++al)
  129. {
  130. if(al->second.IsInAll())
  131. {
  132. l->second.GetLinkLibraries().push_back(
  133. cmTarget::LinkLibraries::value_type(al->first, cmTarget::GENERAL));
  134. }
  135. }
  136. }
  137. }
  138. }
  139. // Write the project into the DSW file
  140. if (l->second.GetType() != cmTarget::INSTALL)
  141. {
  142. this->WriteProject(fout, si->c_str(), dir.c_str(),
  143. pg->GetDSPMakefile(),l->second);
  144. ++si;
  145. }
  146. }
  147. // delete the cmMakefile which also deletes the cmMSProjectGenerator
  148. if(mf != m_Makefile)
  149. {
  150. delete mf;
  151. }
  152. }
  153. // Write the footer for the DSW file
  154. this->WriteDSWFooter(fout);
  155. }
  156. // Write a dsp file into the DSW file,
  157. // Note, that dependencies from executables to
  158. // the libraries it uses are also done here
  159. void cmDSWMakefile::WriteProject(std::ostream& fout,
  160. const char* dspname,
  161. const char* dir,
  162. cmDSPMakefile* project,
  163. const cmTarget &l
  164. )
  165. {
  166. fout << "#########################################################"
  167. "######################\n\n";
  168. fout << "Project: \"" << dspname << "\"="
  169. << dir << "\\" << dspname << ".dsp - Package Owner=<4>\n\n";
  170. fout << "Package=<5>\n{{{\n}}}\n\n";
  171. fout << "Package=<4>\n";
  172. fout << "{{{\n";
  173. // insert Begin Project Dependency Project_Dep_Name project stuff here
  174. cmTarget::LinkLibraries::const_iterator j, jend;
  175. j = l.GetLinkLibraries().begin();
  176. jend = l.GetLinkLibraries().end();
  177. for(;j!= jend; ++j)
  178. {
  179. if(j->first != dspname)
  180. {
  181. if (!(l.GetType() == cmTarget::LIBRARY) ||
  182. project->GetLibraryBuildType() == cmDSPMakefile::DLL)
  183. {
  184. // is the library part of this DSW ? If so add dependency
  185. const char* cacheValue
  186. = cmCacheManager::GetInstance()->GetCacheValue(j->first.c_str());
  187. if(cacheValue)
  188. {
  189. fout << "Begin Project Dependency\n";
  190. fout << "Project_Dep_Name " << j->first << "\n";
  191. fout << "End Project Dependency\n";
  192. }
  193. }
  194. }
  195. }
  196. std::vector<std::string>::iterator i, end;
  197. // write utility dependencies.
  198. i = project->GetMakefile()->GetUtilities().begin();
  199. end = project->GetMakefile()->GetUtilities().end();
  200. for(;i!= end; ++i)
  201. {
  202. if(*i != dspname)
  203. {
  204. fout << "Begin Project Dependency\n";
  205. fout << "Project_Dep_Name " << *i << "\n";
  206. fout << "End Project Dependency\n";
  207. }
  208. }
  209. fout << "}}}\n\n";
  210. }
  211. // Standard end of dsw file
  212. void cmDSWMakefile::WriteDSWFooter(std::ostream& fout)
  213. {
  214. fout << "######################################################"
  215. "#########################\n\n";
  216. fout << "Global:\n\n";
  217. fout << "Package=<5>\n{{{\n}}}\n\n";
  218. fout << "Package=<3>\n{{{\n}}}\n\n";
  219. fout << "#####################################################"
  220. "##########################\n\n";
  221. }
  222. // ouput standard header for dsw file
  223. void cmDSWMakefile::WriteDSWHeader(std::ostream& fout)
  224. {
  225. fout << "Microsoft Developer Studio Workspace File, Format Version 6.00\n";
  226. fout << "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\n\n";
  227. }