cmDSWWriter.cxx 10 KB

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