cmDSWWriter.cxx 11 KB

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