cmDSWWriter.cxx 9.0 KB

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