cmGlobalVisualStudio71Generator.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 "windows.h" // this must be first to define GetCurrentDirectory
  14. #include "cmGlobalVisualStudio71Generator.h"
  15. #include "cmLocalVisualStudio7Generator.h"
  16. #include "cmMakefile.h"
  17. #include "cmake.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 start directory with the trailing slash
  38. std::string rootdir = root->GetMakefile()->GetStartOutputDirectory();
  39. rootdir += "/";
  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->GetStartOutputDirectory();
  55. // remove the home directory and / from the source directory
  56. // this gives a relative path
  57. cmSystemTools::ReplaceString(dir, rootdir.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() && si != dspnames.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. cmTargets &atgts = generators[j]->GetMakefile()->GetTargets();
  83. for(cmTargets::iterator al = atgts.begin();
  84. al != atgts.end(); ++al)
  85. {
  86. if (al->second.IsInAll())
  87. {
  88. if (al->second.GetType() == cmTarget::UTILITY)
  89. {
  90. l->second.AddUtility(al->first.c_str());
  91. }
  92. else
  93. {
  94. l->second.AddLinkLibrary(al->first,cmTarget::GENERAL);
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101. // Write the project into the SLN file
  102. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  103. {
  104. cmCustomCommand cc = l->second.GetPostBuildCommands()[0];
  105. const cmCustomCommandLines& cmds = cc.GetCommandLines();
  106. std::string project = cmds[0][0];
  107. std::string location = cmds[0][1];
  108. this->WriteExternalProject(fout, project.c_str(), location.c_str(), cc.GetDepends());
  109. }
  110. else
  111. {
  112. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  113. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  114. {
  115. bool skip = false;
  116. if(l->first == "ALL_BUILD" )
  117. {
  118. if(doneAllBuild)
  119. {
  120. skip = true;
  121. }
  122. else
  123. {
  124. doneAllBuild = true;
  125. }
  126. }
  127. if(l->first == "INSTALL")
  128. {
  129. if(doneInstall)
  130. {
  131. skip = true;
  132. }
  133. else
  134. {
  135. doneInstall = true;
  136. }
  137. }
  138. if(l->first == "RUN_TESTS")
  139. {
  140. if(doneRunTests)
  141. {
  142. skip = true;
  143. }
  144. else
  145. {
  146. doneRunTests = true;
  147. }
  148. }
  149. if(!skip)
  150. {
  151. this->WriteProject(fout, si->c_str(), dir.c_str(),l->second);
  152. }
  153. ++si;
  154. }
  155. }
  156. }
  157. }
  158. fout << "Global\n"
  159. << "\tGlobalSection(SolutionConfiguration) = preSolution\n";
  160. for(std::vector<std::string>::iterator i = m_Configurations.begin();
  161. i != m_Configurations.end(); ++i)
  162. {
  163. fout << "\t\t" << *i << " = " << *i << "\n";
  164. }
  165. fout << "\tEndGlobalSection\n";
  166. fout << "\tGlobalSection(ProjectConfiguration) = postSolution\n";
  167. // loop over again and compute the depends
  168. for(i = 0; i < generators.size(); ++i)
  169. {
  170. cmMakefile* mf = generators[i]->GetMakefile();
  171. cmLocalVisualStudio7Generator* pg =
  172. static_cast<cmLocalVisualStudio7Generator*>(generators[i]);
  173. // Get the list of create dsp files names from the cmVCProjWriter, more
  174. // than one dsp could have been created per input CMakeLists.txt file
  175. // for each target
  176. std::vector<std::string> dspnames =
  177. pg->GetCreatedProjectNames();
  178. cmTargets &tgts = pg->GetMakefile()->GetTargets();
  179. cmTargets::iterator l = tgts.begin();
  180. std::string dir = mf->GetStartDirectory();
  181. for(std::vector<std::string>::iterator si = dspnames.begin();
  182. l != tgts.end() && si != dspnames.end(); ++l)
  183. {
  184. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  185. {
  186. cmCustomCommand cc = l->second.GetPostBuildCommands()[0];
  187. const cmCustomCommandLines& cmds = cc.GetCommandLines();
  188. std::string project = cmds[0][0];
  189. this->WriteProjectConfigurations(fout, project.c_str(), l->second.IsInAll());
  190. }
  191. else if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  192. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  193. {
  194. this->WriteProjectConfigurations(fout, si->c_str(), l->second.IsInAll());
  195. ++si;
  196. }
  197. }
  198. }
  199. fout << "\tEndGlobalSection\n";
  200. // Write the footer for the SLN file
  201. this->WriteSLNFooter(fout);
  202. }
  203. // Write a dsp file into the SLN file,
  204. // Note, that dependencies from executables to
  205. // the libraries it uses are also done here
  206. void
  207. cmGlobalVisualStudio71Generator::WriteProject(std::ostream& fout,
  208. const char* dspname,
  209. const char* dir,
  210. cmTarget& t)
  211. {
  212. std::string d = cmSystemTools::ConvertToOutputPath(dir);
  213. fout << "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \""
  214. << dspname << "\", \""
  215. << d << "\\" << dspname << ".vcproj\", \"{"
  216. << this->GetGUID(dspname) << "}\"\n";
  217. fout << "\tProjectSection(ProjectDependencies) = postProject\n";
  218. this->WriteProjectDepends(fout, dspname, dir, t);
  219. fout << "\tEndProjectSection\n";
  220. fout <<"EndProject\n";
  221. }
  222. // Write a dsp file into the SLN file,
  223. // Note, that dependencies from executables to
  224. // the libraries it uses are also done here
  225. void
  226. cmGlobalVisualStudio71Generator
  227. ::WriteProjectDepends(std::ostream& fout,
  228. const char* dspname,
  229. const char*, cmTarget& target)
  230. {
  231. // insert Begin Project Dependency Project_Dep_Name project stuff here
  232. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  233. {
  234. cmTarget::LinkLibraries::const_iterator j, jend;
  235. j = target.GetLinkLibraries().begin();
  236. jend = target.GetLinkLibraries().end();
  237. for(;j!= jend; ++j)
  238. {
  239. if(j->first != dspname)
  240. {
  241. // is the library part of this SLN ? If so add dependency
  242. std::string libPath = j->first + "_CMAKE_PATH";
  243. const char* cacheValue
  244. = m_CMakeInstance->GetCacheDefinition(libPath.c_str());
  245. if(cacheValue && *cacheValue)
  246. {
  247. fout << "\t\t{" << this->GetGUID(j->first.c_str()) << "} = {"
  248. << this->GetGUID(j->first.c_str()) << "}\n";
  249. }
  250. }
  251. }
  252. }
  253. std::set<cmStdString>::const_iterator i, end;
  254. // write utility dependencies.
  255. i = target.GetUtilities().begin();
  256. end = target.GetUtilities().end();
  257. for(;i!= end; ++i)
  258. {
  259. if(*i != dspname)
  260. {
  261. std::string name = i->c_str();
  262. if(strncmp(name.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  263. {
  264. // kind of weird removing the first 27 letters.
  265. // my recommendatsions:
  266. // use cmCustomCommand::GetCommand() to get the project name
  267. // or get rid of the target name starting with "INCLUDE_EXTERNAL_MSPROJECT_" and use another
  268. // indicator/flag somewhere. These external project names shouldn't conflict with cmake
  269. // target names anyways.
  270. name.erase(name.begin(), name.begin() + 27);
  271. }
  272. std::string guid = this->GetGUID(name.c_str());
  273. if(guid.size() == 0)
  274. {
  275. std::string m = "Target: ";
  276. m += target.GetName();
  277. m += " depends on unknown target: ";
  278. m += name;
  279. cmSystemTools::Error(m.c_str());
  280. }
  281. fout << "\t\t{" << guid << "} = {" << guid << "}\n";
  282. }
  283. }
  284. }
  285. // Write a dsp file into the SLN file,
  286. // Note, that dependencies from executables to
  287. // the libraries it uses are also done here
  288. void cmGlobalVisualStudio71Generator::WriteExternalProject(std::ostream& fout,
  289. const char* name,
  290. const char* location,
  291. const std::vector<std::string>& depends)
  292. {
  293. std::string d = cmSystemTools::ConvertToOutputPath(location);
  294. fout << "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \""
  295. << name << "\", \""
  296. << d << "\", \"{"
  297. << this->GetGUID(name)
  298. << "}\"\n";
  299. // write out the dependencies here
  300. // VS 7.1 includes dependencies with the project instead of in the global section
  301. if(!depends.empty())
  302. {
  303. fout << "\tProjectSection(ProjectDependencies) = postProject\n";
  304. std::vector<std::string>::const_iterator it;
  305. for(it = depends.begin(); it != depends.end(); ++it)
  306. {
  307. if(it->size() > 0)
  308. {
  309. fout << "\t\t{"
  310. << this->GetGUID(it->c_str())
  311. << "} = {"
  312. << this->GetGUID(it->c_str())
  313. << "}\n";
  314. }
  315. }
  316. fout << "\tEndProjectSection\n";
  317. }
  318. fout << "EndProject\n";
  319. }
  320. // Write a dsp file into the SLN file,
  321. // Note, that dependencies from executables to
  322. // the libraries it uses are also done here
  323. void
  324. cmGlobalVisualStudio71Generator::WriteProjectConfigurations(std::ostream& fout,
  325. const char* name,
  326. bool in_all_build)
  327. {
  328. std::string guid = this->GetGUID(name);
  329. for(std::vector<std::string>::iterator i = m_Configurations.begin();
  330. i != m_Configurations.end(); ++i)
  331. {
  332. fout << "\t\t{" << guid << "}." << *i << ".ActiveCfg = " << *i << "|Win32\n";
  333. if (in_all_build)
  334. {
  335. fout << "\t\t{" << guid << "}." << *i << ".Build.0 = " << *i << "|Win32\n";
  336. }
  337. }
  338. }
  339. // Standard end of dsw file
  340. void cmGlobalVisualStudio71Generator::WriteSLNFooter(std::ostream& fout)
  341. {
  342. fout << "\tGlobalSection(ExtensibilityGlobals) = postSolution\n"
  343. << "\tEndGlobalSection\n"
  344. << "\tGlobalSection(ExtensibilityAddIns) = postSolution\n"
  345. << "\tEndGlobalSection\n"
  346. << "EndGlobal\n";
  347. }
  348. // ouput standard header for dsw file
  349. void cmGlobalVisualStudio71Generator::WriteSLNHeader(std::ostream& fout)
  350. {
  351. fout << "Microsoft Visual Studio Solution File, Format Version 8.00\n";
  352. }
  353. //----------------------------------------------------------------------------
  354. void cmGlobalVisualStudio71Generator::GetDocumentation(cmDocumentationEntry& entry) const
  355. {
  356. entry.name = this->GetName();
  357. entry.brief = "Generates Visual Studio .NET 2003 project files.";
  358. entry.full = "";
  359. }