cmDSWWriter.cxx 11 KB

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