cmGlobalVisualStudio71Generator.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. std::vector<cmLocalGenerator*>& generators)
  33. {
  34. // Write out the header for a SLN file
  35. this->WriteSLNHeader(fout);
  36. // Get the home directory with the trailing slash
  37. std::string homedir = m_CMakeInstance->GetHomeDirectory();
  38. homedir += "/";
  39. bool doneAllBuild = false;
  40. bool doneRunTests = false;
  41. // For each cmMakefile, create a VCProj for it, and
  42. // add it to this SLN file
  43. unsigned int i;
  44. for(i = 0; i < generators.size(); ++i)
  45. {
  46. cmMakefile* mf = generators[i]->GetMakefile();
  47. // Get the source directory from the makefile
  48. std::string dir = mf->GetStartDirectory();
  49. // remove the home directory and / from the source directory
  50. // this gives a relative path
  51. cmSystemTools::ReplaceString(dir, homedir.c_str(), "");
  52. // Get the list of create dsp files names from the cmVCProjWriter, more
  53. // than one dsp could have been created per input CMakeLists.txt file
  54. // for each target
  55. std::vector<std::string> dspnames =
  56. static_cast<cmLocalVisualStudio7Generator *>(generators[i])
  57. ->GetCreatedProjectNames();
  58. cmTargets &tgts = generators[i]->GetMakefile()->GetTargets();
  59. cmTargets::iterator l = tgts.begin();
  60. for(std::vector<std::string>::iterator si = dspnames.begin();
  61. l != tgts.end(); ++l)
  62. {
  63. // special handling for the current makefile
  64. if(mf == generators[0]->GetMakefile())
  65. {
  66. dir = "."; // no subdirectory for project generated
  67. // if this is the special ALL_BUILD utility, then
  68. // make it depend on every other non UTILITY project.
  69. // This is done by adding the names to the GetUtilities
  70. // vector on the makefile
  71. if(l->first == "ALL_BUILD" && !doneAllBuild)
  72. {
  73. unsigned int j;
  74. for(j = 0; j < generators.size(); ++j)
  75. {
  76. const cmTargets &atgts =
  77. generators[j]->GetMakefile()->GetTargets();
  78. for(cmTargets::const_iterator al = atgts.begin();
  79. al != atgts.end(); ++al)
  80. {
  81. if (al->second.IsInAll())
  82. {
  83. if (al->second.GetType() == cmTarget::UTILITY)
  84. {
  85. l->second.AddUtility(al->first.c_str());
  86. }
  87. else
  88. {
  89. l->second.AddLinkLibrary(al->first,cmTarget::GENERAL);
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. // Write the project into the SLN file
  97. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  98. {
  99. cmCustomCommand cc = l->second.GetPreLinkCommands()[0];
  100. // dodgy use of the cmCustomCommand's members to store the
  101. // arguments from the INCLUDE_EXTERNAL_MSPROJECT command
  102. std::vector<std::string> stuff = cc.GetDepends();
  103. std::vector<std::string> depends;
  104. depends.push_back(cc.GetOutput());
  105. this->WriteExternalProject(fout, stuff[0].c_str(),
  106. stuff[1].c_str(), depends);
  107. ++si;
  108. }
  109. else
  110. {
  111. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  112. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  113. {
  114. bool skip = false;
  115. if(l->first == "ALL_BUILD" )
  116. {
  117. if(doneAllBuild)
  118. {
  119. skip = true;
  120. }
  121. else
  122. {
  123. doneAllBuild = true;
  124. }
  125. }
  126. if(l->first == "RUN_TESTS")
  127. {
  128. if(doneRunTests)
  129. {
  130. skip = true;
  131. }
  132. else
  133. {
  134. doneRunTests = true;
  135. }
  136. }
  137. if(!skip)
  138. {
  139. this->WriteProject(fout, si->c_str(), dir.c_str(),l->second);
  140. }
  141. ++si;
  142. }
  143. }
  144. }
  145. }
  146. fout << "Global\n"
  147. << "\tGlobalSection(SolutionConfiguration) = preSolution\n";
  148. for(std::vector<std::string>::iterator i = m_Configurations.begin();
  149. i != m_Configurations.end(); ++i)
  150. {
  151. fout << "\t\t" << *i << " = " << *i << "\n";
  152. }
  153. fout << "\tEndGlobalSection\n";
  154. fout << "\tGlobalSection(ProjectConfiguration) = postSolution\n";
  155. // loop over again and compute the depends
  156. for(i = 0; i < generators.size(); ++i)
  157. {
  158. cmMakefile* mf = generators[i]->GetMakefile();
  159. cmLocalVisualStudio7Generator* pg =
  160. static_cast<cmLocalVisualStudio7Generator*>(generators[i]);
  161. // Get the list of create dsp files names from the cmVCProjWriter, more
  162. // than one dsp could have been created per input CMakeLists.txt file
  163. // for each target
  164. std::vector<std::string> dspnames =
  165. pg->GetCreatedProjectNames();
  166. cmTargets &tgts = pg->GetMakefile()->GetTargets();
  167. cmTargets::iterator l = tgts.begin();
  168. std::string dir = mf->GetStartDirectory();
  169. for(std::vector<std::string>::iterator si = dspnames.begin();
  170. l != tgts.end(); ++l)
  171. {
  172. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  173. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  174. {
  175. this->WriteProjectConfigurations(fout, si->c_str(), l->second.IsInAll());
  176. ++si;
  177. }
  178. }
  179. }
  180. fout << "\tEndGlobalSection\n";
  181. // Write the footer for the SLN file
  182. this->WriteSLNFooter(fout);
  183. }
  184. // Write a dsp file into the SLN file,
  185. // Note, that dependencies from executables to
  186. // the libraries it uses are also done here
  187. void cmGlobalVisualStudio71Generator::WriteProject(std::ostream& fout,
  188. const char* dspname,
  189. const char* dir,
  190. const cmTarget& t)
  191. {
  192. std::string d = cmSystemTools::ConvertToOutputPath(dir);
  193. fout << "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \""
  194. << dspname << "\", \""
  195. << d << "\\" << dspname << ".vcproj\", \"{"
  196. << this->CreateGUID(dspname) << "}\"\n";
  197. fout << "\tProjectSection(ProjectDependencies) = postProject\n";
  198. this->WriteProjectDepends(fout, dspname, dir, t);
  199. fout << "\tEndProjectSection\n";
  200. fout <<"EndProject\n";
  201. }
  202. // Write a dsp file into the SLN file,
  203. // Note, that dependencies from executables to
  204. // the libraries it uses are also done here
  205. void cmGlobalVisualStudio71Generator::WriteProjectDepends(std::ostream& fout,
  206. const char* dspname,
  207. const char* ,
  208. const cmTarget& target
  209. )
  210. {
  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 SLN ? If so add dependency
  222. std::string libPath = j->first + "_CMAKE_PATH";
  223. const char* cacheValue
  224. = m_CMakeInstance->GetCacheDefinition(libPath.c_str());
  225. if(cacheValue && *cacheValue)
  226. {
  227. fout << "\t\t{" << this->CreateGUID(j->first.c_str()) << "} = {"
  228. << this->CreateGUID(j->first.c_str()) << "}\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 << "\t\t{" << this->CreateGUID(i->c_str()) << "} = {"
  242. << this->CreateGUID(i->c_str()) << "}\n";
  243. }
  244. }
  245. }
  246. // Write a dsp file into the SLN file,
  247. // Note, that dependencies from executables to
  248. // the libraries it uses are also done here
  249. void
  250. cmGlobalVisualStudio71Generator::WriteProjectConfigurations(std::ostream& fout,
  251. const char* name,
  252. bool in_all_build)
  253. {
  254. std::string guid = this->CreateGUID(name);
  255. for(std::vector<std::string>::iterator i = m_Configurations.begin();
  256. i != m_Configurations.end(); ++i)
  257. {
  258. fout << "\t\t{" << guid << "}." << *i << ".ActiveCfg = " << *i << "|Win32\n";
  259. if (in_all_build)
  260. {
  261. fout << "\t\t{" << guid << "}." << *i << ".Build.0 = " << *i << "|Win32\n";
  262. }
  263. }
  264. }
  265. // Standard end of dsw file
  266. void cmGlobalVisualStudio71Generator::WriteSLNFooter(std::ostream& fout)
  267. {
  268. fout << "\tGlobalSection(ExtensibilityGlobals) = postSolution\n"
  269. << "\tEndGlobalSection\n"
  270. << "\tGlobalSection(ExtensibilityAddIns) = postSolution\n"
  271. << "\tEndGlobalSection\n"
  272. << "EndGlobal\n";
  273. }
  274. // ouput standard header for dsw file
  275. void cmGlobalVisualStudio71Generator::WriteSLNHeader(std::ostream& fout)
  276. {
  277. fout << "Microsoft Visual Studio Solution File, Format Version 8.00\n";
  278. }
  279. //----------------------------------------------------------------------------
  280. void cmGlobalVisualStudio71Generator::GetDocumentation(cmDocumentationEntry& entry) const
  281. {
  282. entry.name = this->GetName();
  283. entry.brief = "Generates Visual Studio .NET 2003 project files.";
  284. entry.full = "";
  285. }