cmDSWWriter.cxx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmDSWWriter.h"
  14. #include "cmStandardIncludes.h"
  15. #include "cmSystemTools.h"
  16. #include "cmDSPWriter.h"
  17. #include "cmMSProjectGenerator.h"
  18. #include "cmCacheManager.h"
  19. cmDSWWriter::cmDSWWriter(cmMakefile* m)
  20. {
  21. m_Makefile = m;
  22. }
  23. // output the DSW file
  24. void cmDSWWriter::OutputDSWFile()
  25. {
  26. // if this is an out of source build, create the output directory
  27. if(strcmp(m_Makefile->GetStartOutputDirectory(),
  28. m_Makefile->GetHomeDirectory()) != 0)
  29. {
  30. if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
  31. {
  32. cmSystemTools::Error("Error creating output directory for DSW file",
  33. m_Makefile->GetStartOutputDirectory());
  34. }
  35. }
  36. // create the dsw file name
  37. std::string fname;
  38. fname = m_Makefile->GetStartOutputDirectory();
  39. fname += "/";
  40. if(strlen(m_Makefile->GetProjectName()))
  41. {
  42. fname += m_Makefile->GetProjectName();
  43. }
  44. else
  45. {
  46. fname += "Project";
  47. }
  48. fname += ".dsw";
  49. std::ofstream fout(fname.c_str());
  50. if(!fout)
  51. {
  52. cmSystemTools::Error("Error can not open DSW file for write: "
  53. ,fname.c_str());
  54. return;
  55. }
  56. this->WriteDSWFile(fout);
  57. }
  58. // Write a DSW file to the stream
  59. void cmDSWWriter::WriteDSWFile(std::ostream& fout)
  60. {
  61. // Write out the header for a DSW file
  62. this->WriteDSWHeader(fout);
  63. // Create a list of cmMakefile created from all the
  64. // CMakeLists.txt files that are in sub directories of
  65. // this one.
  66. std::vector<cmMakefile*> allListFiles;
  67. // add this makefile to the list
  68. allListFiles.push_back(m_Makefile);
  69. // add a special target that depends on ALL projects for easy build
  70. // of Debug only
  71. m_Makefile->AddUtilityCommand("ALL_BUILD", "echo", "\"Build all projects\"",
  72. false);
  73. m_Makefile->FindSubDirectoryCMakeListsFiles(allListFiles);
  74. // For each cmMakefile, create a DSP for it, and
  75. // add it to this DSW file
  76. for(std::vector<cmMakefile*>::iterator k = allListFiles.begin();
  77. k != allListFiles.end(); ++k)
  78. {
  79. cmMakefile* mf = *k;
  80. cmMSProjectGenerator* pg = 0;
  81. // if not this makefile, then create a new generator
  82. if(m_Makefile != mf)
  83. {
  84. // Create an MS generator with DSW off, so it only creates dsp files
  85. pg = new cmMSProjectGenerator;
  86. }
  87. else
  88. {
  89. pg = (cmMSProjectGenerator*)m_Makefile->GetMakefileGenerator();
  90. }
  91. // make sure the generator is building dsp files
  92. pg->BuildDSWOff();
  93. mf->SetMakefileGenerator(pg);
  94. mf->GenerateMakefile();
  95. // Get the source directory from the makefile
  96. std::string dir = mf->GetStartDirectory();
  97. // Get the home directory with the trailing slash
  98. std::string homedir = m_Makefile->GetHomeDirectory();
  99. homedir += "/";
  100. // remove the home directory and / from the source directory
  101. // this gives a relative path
  102. cmSystemTools::ReplaceString(dir, homedir.c_str(), "");
  103. // Get the list of create dsp files names from the cmDSPWriter, more
  104. // than one dsp could have been created per input CMakeLists.txt file
  105. // for each target
  106. std::vector<std::string> dspnames =
  107. pg->GetDSPWriter()->GetCreatedProjectNames();
  108. cmTargets &tgts = pg->GetDSPWriter()->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. if (al->second.GetType() == cmTarget::UTILITY)
  133. {
  134. l->second.AddUtility(al->first.c_str());
  135. }
  136. else
  137. {
  138. l->second.GetLinkLibraries().push_back(
  139. cmTarget::LinkLibraries::value_type(al->first, cmTarget::GENERAL));
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. // Write the project into the DSW file
  147. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  148. {
  149. cmCustomCommand cc = l->second.GetCustomCommands()[0];
  150. // dodgy use of the cmCustomCommand's members to store the
  151. // arguments from the INCLUDE_EXTERNAL_MSPROJECT command
  152. std::vector<std::string> stuff = cc.GetDepends();
  153. std::vector<std::string> depends = cc.GetOutputs();
  154. this->WriteExternalProject(fout, stuff[0].c_str(), stuff[1].c_str(), depends);
  155. ++si;
  156. }
  157. else if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  158. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  159. {
  160. this->WriteProject(fout, si->c_str(), dir.c_str(),
  161. pg->GetDSPWriter(),l->second);
  162. ++si;
  163. }
  164. }
  165. // delete the cmMakefile which also deletes the cmMSProjectGenerator
  166. if(mf != m_Makefile)
  167. {
  168. delete mf;
  169. }
  170. }
  171. // Write the footer for the DSW file
  172. this->WriteDSWFooter(fout);
  173. }
  174. // Write a dsp file into the DSW file,
  175. // Note, that dependencies from executables to
  176. // the libraries it uses are also done here
  177. void cmDSWWriter::WriteProject(std::ostream& fout,
  178. const char* dspname,
  179. const char* dir,
  180. cmDSPWriter*,
  181. const cmTarget& target
  182. )
  183. {
  184. fout << "#########################################################"
  185. "######################\n\n";
  186. fout << "Project: \"" << dspname << "\"="
  187. << dir << "\\" << dspname << ".dsp - Package Owner=<4>\n\n";
  188. fout << "Package=<5>\n{{{\n}}}\n\n";
  189. fout << "Package=<4>\n";
  190. fout << "{{{\n";
  191. // insert Begin Project Dependency Project_Dep_Name project stuff here
  192. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  193. {
  194. cmTarget::LinkLibraries::const_iterator j, jend;
  195. j = target.GetLinkLibraries().begin();
  196. jend = target.GetLinkLibraries().end();
  197. for(;j!= jend; ++j)
  198. {
  199. if(j->first != dspname)
  200. {
  201. // is the library part of this DSW ? If so add dependency
  202. const char* cacheValue
  203. = m_Makefile->GetDefinition(j->first.c_str());
  204. if(cacheValue)
  205. {
  206. fout << "Begin Project Dependency\n";
  207. fout << "Project_Dep_Name " << j->first << "\n";
  208. fout << "End Project Dependency\n";
  209. }
  210. }
  211. }
  212. }
  213. std::set<std::string>::const_iterator i, end;
  214. // write utility dependencies.
  215. i = target.GetUtilities().begin();
  216. end = target.GetUtilities().end();
  217. for(;i!= end; ++i)
  218. {
  219. if(*i != dspname)
  220. {
  221. fout << "Begin Project Dependency\n";
  222. fout << "Project_Dep_Name " << *i << "\n";
  223. fout << "End Project Dependency\n";
  224. }
  225. }
  226. fout << "}}}\n\n";
  227. }
  228. // Write a dsp file into the DSW file,
  229. // Note, that dependencies from executables to
  230. // the libraries it uses are also done here
  231. void cmDSWWriter::WriteExternalProject(std::ostream& fout,
  232. const char* name,
  233. const char* location,
  234. const std::vector<std::string>& dependencies)
  235. {
  236. fout << "#########################################################"
  237. "######################\n\n";
  238. fout << "Project: \"" << name << "\"="
  239. << location << " - Package Owner=<4>\n\n";
  240. fout << "Package=<5>\n{{{\n}}}\n\n";
  241. fout << "Package=<4>\n";
  242. fout << "{{{\n";
  243. std::vector<std::string>::const_iterator i, end;
  244. // write dependencies.
  245. i = dependencies.begin();
  246. end = dependencies.end();
  247. for(;i!= end; ++i)
  248. {
  249. fout << "Begin Project Dependency\n";
  250. fout << "Project_Dep_Name " << *i << "\n";
  251. fout << "End Project Dependency\n";
  252. }
  253. fout << "}}}\n\n";
  254. }
  255. // Standard end of dsw file
  256. void cmDSWWriter::WriteDSWFooter(std::ostream& fout)
  257. {
  258. fout << "######################################################"
  259. "#########################\n\n";
  260. fout << "Global:\n\n";
  261. fout << "Package=<5>\n{{{\n}}}\n\n";
  262. fout << "Package=<3>\n{{{\n}}}\n\n";
  263. fout << "#####################################################"
  264. "##########################\n\n";
  265. }
  266. // ouput standard header for dsw file
  267. void cmDSWWriter::WriteDSWHeader(std::ostream& fout)
  268. {
  269. fout << "Microsoft Developer Studio Workspace File, Format Version 6.00\n";
  270. fout << "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\n\n";
  271. }