cmGlobalVisualStudio71Generator.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html 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 "cmGlobalVisualStudio71Generator.h"
  14. #include "cmLocalVisualStudio7Generator.h"
  15. #include "cmMakefile.h"
  16. #include "cmake.h"
  17. #include "windows.h"
  18. cmGlobalVisualStudio71Generator::cmGlobalVisualStudio71Generator()
  19. {
  20. m_FindMakeProgramFile = "CMakeVS71FindMake.cmake";
  21. }
  22. ///! Create a local generator appropriate to this Global Generator
  23. cmLocalGenerator *cmGlobalVisualStudio71Generator::CreateLocalGenerator()
  24. {
  25. cmLocalVisualStudio7Generator *lg = new cmLocalVisualStudio7Generator;
  26. lg->SetVersion71();
  27. lg->SetGlobalGenerator(this);
  28. return lg;
  29. }
  30. // Write a SLN file to the stream
  31. void cmGlobalVisualStudio71Generator::WriteSLNFile(std::ostream& fout,
  32. cmLocalGenerator* root,
  33. std::vector<cmLocalGenerator*>& generators)
  34. {
  35. // Write out the header for a SLN file
  36. this->WriteSLNHeader(fout);
  37. // Get the home directory with the trailing slash
  38. std::string homedir = m_CMakeInstance->GetHomeDirectory();
  39. homedir += "/";
  40. bool doneAllBuild = false;
  41. bool doneRunTests = false;
  42. bool doneInstall = false;
  43. // For each cmMakefile, create a VCProj for it, and
  44. // add it to this SLN file
  45. unsigned int i;
  46. for(i = 0; i < generators.size(); ++i)
  47. {
  48. if(this->IsExcluded(root, generators[i]))
  49. {
  50. continue;
  51. }
  52. cmMakefile* mf = generators[i]->GetMakefile();
  53. // Get the source directory from the makefile
  54. std::string dir = mf->GetStartDirectory();
  55. // remove the home directory and / from the source directory
  56. // this gives a relative path
  57. cmSystemTools::ReplaceString(dir, homedir.c_str(), "");
  58. // Get the list of create dsp files names from the cmVCProjWriter, more
  59. // than one dsp could have been created per input CMakeLists.txt file
  60. // for each target
  61. std::vector<std::string> dspnames =
  62. static_cast<cmLocalVisualStudio7Generator *>(generators[i])
  63. ->GetCreatedProjectNames();
  64. cmTargets &tgts = generators[i]->GetMakefile()->GetTargets();
  65. cmTargets::iterator l = tgts.begin();
  66. for(std::vector<std::string>::iterator si = dspnames.begin();
  67. l != tgts.end(); ++l)
  68. {
  69. // special handling for the current makefile
  70. if(mf == generators[0]->GetMakefile())
  71. {
  72. dir = "."; // no subdirectory for project generated
  73. // if this is the special ALL_BUILD utility, then
  74. // make it depend on every other non UTILITY project.
  75. // This is done by adding the names to the GetUtilities
  76. // vector on the makefile
  77. if(l->first == "ALL_BUILD" && !doneAllBuild)
  78. {
  79. unsigned int j;
  80. for(j = 0; j < generators.size(); ++j)
  81. {
  82. const cmTargets &atgts =
  83. generators[j]->GetMakefile()->GetTargets();
  84. for(cmTargets::const_iterator al = atgts.begin();
  85. al != atgts.end(); ++al)
  86. {
  87. if (al->second.IsInAll())
  88. {
  89. if (al->second.GetType() == cmTarget::UTILITY)
  90. {
  91. l->second.AddUtility(al->first.c_str());
  92. }
  93. else
  94. {
  95. l->second.AddLinkLibrary(al->first,cmTarget::GENERAL);
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. // Write the project into the SLN file
  103. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  104. {
  105. cmCustomCommand cc = l->second.GetPreLinkCommands()[0];
  106. // dodgy use of the cmCustomCommand's members to store the
  107. // arguments from the INCLUDE_EXTERNAL_MSPROJECT command
  108. std::vector<std::string> stuff = cc.GetDepends();
  109. std::vector<std::string> depends;
  110. depends.push_back(cc.GetOutput());
  111. this->WriteExternalProject(fout, stuff[0].c_str(),
  112. stuff[1].c_str(), depends);
  113. ++si;
  114. }
  115. else
  116. {
  117. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  118. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  119. {
  120. bool skip = false;
  121. if(l->first == "ALL_BUILD" )
  122. {
  123. if(doneAllBuild)
  124. {
  125. skip = true;
  126. }
  127. else
  128. {
  129. doneAllBuild = true;
  130. }
  131. }
  132. if(l->first == "INSTALL")
  133. {
  134. if(doneInstall)
  135. {
  136. skip = true;
  137. }
  138. else
  139. {
  140. doneInstall = true;
  141. }
  142. }
  143. if(l->first == "RUN_TESTS")
  144. {
  145. if(doneRunTests)
  146. {
  147. skip = true;
  148. }
  149. else
  150. {
  151. doneRunTests = true;
  152. }
  153. }
  154. if(!skip)
  155. {
  156. this->WriteProject(fout, si->c_str(), dir.c_str(),l->second);
  157. }
  158. ++si;
  159. }
  160. }
  161. }
  162. }
  163. fout << "Global\n"
  164. << "\tGlobalSection(SolutionConfiguration) = preSolution\n";
  165. for(std::vector<std::string>::iterator i = m_Configurations.begin();
  166. i != m_Configurations.end(); ++i)
  167. {
  168. fout << "\t\t" << *i << " = " << *i << "\n";
  169. }
  170. fout << "\tEndGlobalSection\n";
  171. fout << "\tGlobalSection(ProjectConfiguration) = postSolution\n";
  172. // loop over again and compute the depends
  173. for(i = 0; i < generators.size(); ++i)
  174. {
  175. cmMakefile* mf = generators[i]->GetMakefile();
  176. cmLocalVisualStudio7Generator* pg =
  177. static_cast<cmLocalVisualStudio7Generator*>(generators[i]);
  178. // Get the list of create dsp files names from the cmVCProjWriter, more
  179. // than one dsp could have been created per input CMakeLists.txt file
  180. // for each target
  181. std::vector<std::string> dspnames =
  182. pg->GetCreatedProjectNames();
  183. cmTargets &tgts = pg->GetMakefile()->GetTargets();
  184. cmTargets::iterator l = tgts.begin();
  185. std::string dir = mf->GetStartDirectory();
  186. for(std::vector<std::string>::iterator si = dspnames.begin();
  187. l != tgts.end(); ++l)
  188. {
  189. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  190. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  191. {
  192. this->WriteProjectConfigurations(fout, si->c_str(), l->second.IsInAll());
  193. ++si;
  194. }
  195. }
  196. }
  197. fout << "\tEndGlobalSection\n";
  198. // Write the footer for the SLN file
  199. this->WriteSLNFooter(fout);
  200. }
  201. // Write a dsp file into the SLN file,
  202. // Note, that dependencies from executables to
  203. // the libraries it uses are also done here
  204. void cmGlobalVisualStudio71Generator::WriteProject(std::ostream& fout,
  205. const char* dspname,
  206. const char* dir,
  207. const cmTarget& t)
  208. {
  209. std::string d = cmSystemTools::ConvertToOutputPath(dir);
  210. fout << "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \""
  211. << dspname << "\", \""
  212. << d << "\\" << dspname << ".vcproj\", \"{"
  213. << this->CreateGUID(dspname) << "}\"\n";
  214. fout << "\tProjectSection(ProjectDependencies) = postProject\n";
  215. this->WriteProjectDepends(fout, dspname, dir, t);
  216. fout << "\tEndProjectSection\n";
  217. fout <<"EndProject\n";
  218. }
  219. // Write a dsp file into the SLN file,
  220. // Note, that dependencies from executables to
  221. // the libraries it uses are also done here
  222. void cmGlobalVisualStudio71Generator::WriteProjectDepends(std::ostream& fout,
  223. const char* dspname,
  224. const char* ,
  225. const cmTarget& target
  226. )
  227. {
  228. // insert Begin Project Dependency Project_Dep_Name project stuff here
  229. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  230. {
  231. cmTarget::LinkLibraries::const_iterator j, jend;
  232. j = target.GetLinkLibraries().begin();
  233. jend = target.GetLinkLibraries().end();
  234. for(;j!= jend; ++j)
  235. {
  236. if(j->first != dspname)
  237. {
  238. // is the library part of this SLN ? If so add dependency
  239. std::string libPath = j->first + "_CMAKE_PATH";
  240. const char* cacheValue
  241. = m_CMakeInstance->GetCacheDefinition(libPath.c_str());
  242. if(cacheValue && *cacheValue)
  243. {
  244. fout << "\t\t{" << this->CreateGUID(j->first.c_str()) << "} = {"
  245. << this->CreateGUID(j->first.c_str()) << "}\n";
  246. }
  247. }
  248. }
  249. }
  250. std::set<cmStdString>::const_iterator i, end;
  251. // write utility dependencies.
  252. i = target.GetUtilities().begin();
  253. end = target.GetUtilities().end();
  254. for(;i!= end; ++i)
  255. {
  256. if(*i != dspname)
  257. {
  258. fout << "\t\t{" << this->CreateGUID(i->c_str()) << "} = {"
  259. << this->CreateGUID(i->c_str()) << "}\n";
  260. }
  261. }
  262. }
  263. // Write a dsp file into the SLN file,
  264. // Note, that dependencies from executables to
  265. // the libraries it uses are also done here
  266. void
  267. cmGlobalVisualStudio71Generator::WriteProjectConfigurations(std::ostream& fout,
  268. const char* name,
  269. bool in_all_build)
  270. {
  271. std::string guid = this->CreateGUID(name);
  272. for(std::vector<std::string>::iterator i = m_Configurations.begin();
  273. i != m_Configurations.end(); ++i)
  274. {
  275. fout << "\t\t{" << guid << "}." << *i << ".ActiveCfg = " << *i << "|Win32\n";
  276. if (in_all_build)
  277. {
  278. fout << "\t\t{" << guid << "}." << *i << ".Build.0 = " << *i << "|Win32\n";
  279. }
  280. }
  281. }
  282. // Standard end of dsw file
  283. void cmGlobalVisualStudio71Generator::WriteSLNFooter(std::ostream& fout)
  284. {
  285. fout << "\tGlobalSection(ExtensibilityGlobals) = postSolution\n"
  286. << "\tEndGlobalSection\n"
  287. << "\tGlobalSection(ExtensibilityAddIns) = postSolution\n"
  288. << "\tEndGlobalSection\n"
  289. << "EndGlobal\n";
  290. }
  291. // ouput standard header for dsw file
  292. void cmGlobalVisualStudio71Generator::WriteSLNHeader(std::ostream& fout)
  293. {
  294. fout << "Microsoft Visual Studio Solution File, Format Version 8.00\n";
  295. }
  296. //----------------------------------------------------------------------------
  297. void cmGlobalVisualStudio71Generator::GetDocumentation(cmDocumentationEntry& entry) const
  298. {
  299. entry.name = this->GetName();
  300. entry.brief = "Generates Visual Studio .NET 2003 project files.";
  301. entry.full = "";
  302. }