cmDSWWriter.cxx 9.1 KB

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