cmDSWWriter.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. std::string ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  74. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  75. ctest += "/";
  76. ctest += "ctest";
  77. ctest += cmSystemTools::GetExecutableExtension();
  78. if(!cmSystemTools::FileExists(ctest.c_str()))
  79. {
  80. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  81. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  82. ctest += "/Debug/";
  83. ctest += "ctest";
  84. ctest += cmSystemTools::GetExecutableExtension();
  85. }
  86. if(!cmSystemTools::FileExists(ctest.c_str()))
  87. {
  88. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  89. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  90. ctest += "/Release/";
  91. ctest += "ctest";
  92. ctest += cmSystemTools::GetExecutableExtension();
  93. }
  94. m_Makefile->AddUtilityCommand("RUN_TESTS", ctest.c_str(), "-D $(IntDir)",
  95. false);
  96. m_Makefile->FindSubDirectoryCMakeListsFiles(allListFiles);
  97. // For each cmMakefile, create a DSP for it, and
  98. // add it to this DSW file
  99. for(std::vector<cmMakefile*>::iterator k = allListFiles.begin();
  100. k != allListFiles.end(); ++k)
  101. {
  102. cmMakefile* mf = *k;
  103. cmMSProjectGenerator* pg = 0;
  104. // if not this makefile, then create a new generator
  105. if(m_Makefile != mf)
  106. {
  107. // Create an MS generator with DSW off, so it only creates dsp files
  108. pg = new cmMSProjectGenerator;
  109. }
  110. else
  111. {
  112. pg = (cmMSProjectGenerator*)m_Makefile->GetMakefileGenerator();
  113. }
  114. // make sure the generator is building dsp files
  115. pg->BuildDSWOff();
  116. mf->SetMakefileGenerator(pg);
  117. mf->GenerateMakefile();
  118. // Get the source directory from the makefile
  119. std::string dir = mf->GetStartDirectory();
  120. // Get the home directory with the trailing slash
  121. std::string homedir = m_Makefile->GetHomeDirectory();
  122. homedir += "/";
  123. // remove the home directory and / from the source directory
  124. // this gives a relative path
  125. cmSystemTools::ReplaceString(dir, homedir.c_str(), "");
  126. // Get the list of create dsp files names from the cmDSPWriter, more
  127. // than one dsp could have been created per input CMakeLists.txt file
  128. // for each target
  129. std::vector<std::string> dspnames =
  130. pg->GetDSPWriter()->GetCreatedProjectNames();
  131. cmTargets &tgts = pg->GetDSPWriter()->GetMakefile()->GetTargets();
  132. cmTargets::iterator l = tgts.begin();
  133. for(std::vector<std::string>::iterator si = dspnames.begin();
  134. l != tgts.end(); ++l)
  135. {
  136. // special handling for the current makefile
  137. if(mf == m_Makefile)
  138. {
  139. dir = "."; // no subdirectory for project generated
  140. // if this is the special ALL_BUILD utility, then
  141. // make it depend on every other non UTILITY project.
  142. // This is done by adding the names to the GetUtilities
  143. // vector on the makefile
  144. if(l->first == "ALL_BUILD")
  145. {
  146. for(std::vector<cmMakefile*>::iterator a = allListFiles.begin();
  147. a != allListFiles.end(); ++a)
  148. {
  149. const cmTargets &atgts = (*a)->GetTargets();
  150. for(cmTargets::const_iterator al = atgts.begin();
  151. al != atgts.end(); ++al)
  152. {
  153. if (al->second.IsInAll())
  154. {
  155. if (al->second.GetType() == cmTarget::UTILITY)
  156. {
  157. l->second.AddUtility(al->first.c_str());
  158. }
  159. else
  160. {
  161. l->second.GetLinkLibraries().push_back(
  162. cmTarget::LinkLibraries::value_type(al->first, cmTarget::GENERAL));
  163. }
  164. }
  165. }
  166. }
  167. }
  168. }
  169. // Write the project into the DSW file
  170. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  171. {
  172. cmCustomCommand cc = l->second.GetCustomCommands()[0];
  173. // dodgy use of the cmCustomCommand's members to store the
  174. // arguments from the INCLUDE_EXTERNAL_MSPROJECT command
  175. std::vector<std::string> stuff = cc.GetDepends();
  176. std::vector<std::string> depends = cc.GetOutputs();
  177. this->WriteExternalProject(fout, stuff[0].c_str(), stuff[1].c_str(), depends);
  178. ++si;
  179. }
  180. else if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  181. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  182. {
  183. this->WriteProject(fout, si->c_str(), dir.c_str(),
  184. pg->GetDSPWriter(),l->second);
  185. ++si;
  186. }
  187. }
  188. // delete the cmMakefile which also deletes the cmMSProjectGenerator
  189. if(mf != m_Makefile)
  190. {
  191. delete mf;
  192. }
  193. }
  194. // Write the footer for the DSW file
  195. this->WriteDSWFooter(fout);
  196. }
  197. // Write a dsp file into the DSW file,
  198. // Note, that dependencies from executables to
  199. // the libraries it uses are also done here
  200. void cmDSWWriter::WriteProject(std::ostream& fout,
  201. const char* dspname,
  202. const char* dir,
  203. cmDSPWriter*,
  204. const cmTarget& target
  205. )
  206. {
  207. fout << "#########################################################"
  208. "######################\n\n";
  209. fout << "Project: \"" << dspname << "\"="
  210. << dir << "\\" << dspname << ".dsp - Package Owner=<4>\n\n";
  211. fout << "Package=<5>\n{{{\n}}}\n\n";
  212. fout << "Package=<4>\n";
  213. fout << "{{{\n";
  214. // insert Begin Project Dependency Project_Dep_Name project stuff here
  215. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  216. {
  217. cmTarget::LinkLibraries::const_iterator j, jend;
  218. j = target.GetLinkLibraries().begin();
  219. jend = target.GetLinkLibraries().end();
  220. for(;j!= jend; ++j)
  221. {
  222. if(j->first != dspname)
  223. {
  224. // is the library part of this DSW ? If so add dependency
  225. const char* cacheValue
  226. = m_Makefile->GetDefinition(j->first.c_str());
  227. if(cacheValue)
  228. {
  229. fout << "Begin Project Dependency\n";
  230. fout << "Project_Dep_Name " << j->first << "\n";
  231. fout << "End Project Dependency\n";
  232. }
  233. }
  234. }
  235. }
  236. std::set<std::string>::const_iterator i, end;
  237. // write utility dependencies.
  238. i = target.GetUtilities().begin();
  239. end = target.GetUtilities().end();
  240. for(;i!= end; ++i)
  241. {
  242. if(*i != dspname)
  243. {
  244. fout << "Begin Project Dependency\n";
  245. fout << "Project_Dep_Name " << *i << "\n";
  246. fout << "End Project Dependency\n";
  247. }
  248. }
  249. fout << "}}}\n\n";
  250. }
  251. // Write a dsp file into the DSW file,
  252. // Note, that dependencies from executables to
  253. // the libraries it uses are also done here
  254. void cmDSWWriter::WriteExternalProject(std::ostream& fout,
  255. const char* name,
  256. const char* location,
  257. const std::vector<std::string>& dependencies)
  258. {
  259. fout << "#########################################################"
  260. "######################\n\n";
  261. fout << "Project: \"" << name << "\"="
  262. << location << " - Package Owner=<4>\n\n";
  263. fout << "Package=<5>\n{{{\n}}}\n\n";
  264. fout << "Package=<4>\n";
  265. fout << "{{{\n";
  266. std::vector<std::string>::const_iterator i, end;
  267. // write dependencies.
  268. i = dependencies.begin();
  269. end = dependencies.end();
  270. for(;i!= end; ++i)
  271. {
  272. fout << "Begin Project Dependency\n";
  273. fout << "Project_Dep_Name " << *i << "\n";
  274. fout << "End Project Dependency\n";
  275. }
  276. fout << "}}}\n\n";
  277. }
  278. // Standard end of dsw file
  279. void cmDSWWriter::WriteDSWFooter(std::ostream& fout)
  280. {
  281. fout << "######################################################"
  282. "#########################\n\n";
  283. fout << "Global:\n\n";
  284. fout << "Package=<5>\n{{{\n}}}\n\n";
  285. fout << "Package=<3>\n{{{\n}}}\n\n";
  286. fout << "#####################################################"
  287. "##########################\n\n";
  288. }
  289. // ouput standard header for dsw file
  290. void cmDSWWriter::WriteDSWHeader(std::ostream& fout)
  291. {
  292. fout << "Microsoft Developer Studio Workspace File, Format Version 6.00\n";
  293. fout << "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\n\n";
  294. }