cmGlobalVisualStudio71Generator.cxx 11 KB

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