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. 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.GetLinkLibraries().push_back(
  160. cmTarget::LinkLibraries::value_type(al->first, cmTarget::GENERAL));
  161. }
  162. }
  163. }
  164. }
  165. }
  166. }
  167. // Write the project into the DSW file
  168. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  169. {
  170. cmCustomCommand cc = l->second.GetCustomCommands()[0];
  171. // dodgy use of the cmCustomCommand's members to store the
  172. // arguments from the INCLUDE_EXTERNAL_MSPROJECT command
  173. std::vector<std::string> stuff = cc.GetDepends();
  174. std::vector<std::string> depends = cc.GetOutputs();
  175. this->WriteExternalProject(fout, stuff[0].c_str(), stuff[1].c_str(), depends);
  176. ++si;
  177. }
  178. else if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  179. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  180. {
  181. this->WriteProject(fout, si->c_str(), dir.c_str(),
  182. pg->GetDSPWriter(),l->second);
  183. ++si;
  184. }
  185. }
  186. // delete the cmMakefile which also deletes the cmMSProjectGenerator
  187. if(mf != m_Makefile)
  188. {
  189. delete mf;
  190. }
  191. }
  192. // Write the footer for the DSW file
  193. this->WriteDSWFooter(fout);
  194. }
  195. // Write a dsp file into the DSW file,
  196. // Note, that dependencies from executables to
  197. // the libraries it uses are also done here
  198. void cmDSWWriter::WriteProject(std::ostream& fout,
  199. const char* dspname,
  200. const char* dir,
  201. cmDSPWriter*,
  202. const cmTarget& target
  203. )
  204. {
  205. fout << "#########################################################"
  206. "######################\n\n";
  207. fout << "Project: \"" << dspname << "\"="
  208. << dir << "\\" << dspname << ".dsp - Package Owner=<4>\n\n";
  209. fout << "Package=<5>\n{{{\n}}}\n\n";
  210. fout << "Package=<4>\n";
  211. fout << "{{{\n";
  212. // insert Begin Project Dependency Project_Dep_Name project stuff here
  213. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  214. {
  215. cmTarget::LinkLibraries::const_iterator j, jend;
  216. j = target.GetLinkLibraries().begin();
  217. jend = target.GetLinkLibraries().end();
  218. for(;j!= jend; ++j)
  219. {
  220. if(j->first != dspname)
  221. {
  222. // is the library part of this DSW ? If so add dependency
  223. const char* cacheValue
  224. = m_Makefile->GetDefinition(j->first.c_str());
  225. if(cacheValue)
  226. {
  227. fout << "Begin Project Dependency\n";
  228. fout << "Project_Dep_Name " << j->first << "\n";
  229. fout << "End Project Dependency\n";
  230. }
  231. }
  232. }
  233. }
  234. std::set<std::string>::const_iterator i, end;
  235. // write utility dependencies.
  236. i = target.GetUtilities().begin();
  237. end = target.GetUtilities().end();
  238. for(;i!= end; ++i)
  239. {
  240. if(*i != dspname)
  241. {
  242. fout << "Begin Project Dependency\n";
  243. fout << "Project_Dep_Name " << *i << "\n";
  244. fout << "End Project Dependency\n";
  245. }
  246. }
  247. fout << "}}}\n\n";
  248. }
  249. // Write a dsp file into the DSW file,
  250. // Note, that dependencies from executables to
  251. // the libraries it uses are also done here
  252. void cmDSWWriter::WriteExternalProject(std::ostream& fout,
  253. const char* name,
  254. const char* location,
  255. const std::vector<std::string>& dependencies)
  256. {
  257. fout << "#########################################################"
  258. "######################\n\n";
  259. fout << "Project: \"" << name << "\"="
  260. << location << " - Package Owner=<4>\n\n";
  261. fout << "Package=<5>\n{{{\n}}}\n\n";
  262. fout << "Package=<4>\n";
  263. fout << "{{{\n";
  264. std::vector<std::string>::const_iterator i, end;
  265. // write dependencies.
  266. i = dependencies.begin();
  267. end = dependencies.end();
  268. for(;i!= end; ++i)
  269. {
  270. fout << "Begin Project Dependency\n";
  271. fout << "Project_Dep_Name " << *i << "\n";
  272. fout << "End Project Dependency\n";
  273. }
  274. fout << "}}}\n\n";
  275. }
  276. // Standard end of dsw file
  277. void cmDSWWriter::WriteDSWFooter(std::ostream& fout)
  278. {
  279. fout << "######################################################"
  280. "#########################\n\n";
  281. fout << "Global:\n\n";
  282. fout << "Package=<5>\n{{{\n}}}\n\n";
  283. fout << "Package=<3>\n{{{\n}}}\n\n";
  284. fout << "#####################################################"
  285. "##########################\n\n";
  286. }
  287. // ouput standard header for dsw file
  288. void cmDSWWriter::WriteDSWHeader(std::ostream& fout)
  289. {
  290. fout << "Microsoft Developer Studio Workspace File, Format Version 6.00\n";
  291. fout << "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\n\n";
  292. }