cmGlobalVisualStudio71Generator.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. m_ProjectConfigurationSectionName = "ProjectConfiguration";
  22. }
  23. ///! Create a local generator appropriate to this Global Generator
  24. cmLocalGenerator *cmGlobalVisualStudio71Generator::CreateLocalGenerator()
  25. {
  26. cmLocalVisualStudio7Generator *lg = new cmLocalVisualStudio7Generator;
  27. lg->SetVersion71();
  28. lg->SetGlobalGenerator(this);
  29. return lg;
  30. }
  31. // Write a SLN file to the stream
  32. void cmGlobalVisualStudio71Generator::WriteSLNFile(std::ostream& fout,
  33. cmLocalGenerator* root,
  34. std::vector<cmLocalGenerator*>& generators)
  35. {
  36. // Write out the header for a SLN file
  37. this->WriteSLNHeader(fout);
  38. // Get the start directory with the trailing slash
  39. std::string rootdir = root->GetMakefile()->GetStartOutputDirectory();
  40. rootdir += "/";
  41. bool doneAllBuild = false;
  42. bool doneCheckBuild = false;
  43. bool doneRunTests = false;
  44. bool doneInstall = false;
  45. bool doneEditCache = false;
  46. bool doneRebuildCache = false;
  47. bool donePackage = false;
  48. // For each cmMakefile, create a VCProj for it, and
  49. // add it to this SLN file
  50. unsigned int i;
  51. for(i = 0; i < generators.size(); ++i)
  52. {
  53. if(this->IsExcluded(root, generators[i]))
  54. {
  55. continue;
  56. }
  57. cmMakefile* mf = generators[i]->GetMakefile();
  58. // Get the source directory from the makefile
  59. std::string dir = mf->GetStartOutputDirectory();
  60. // remove the home directory and / from the source directory
  61. // this gives a relative path
  62. cmSystemTools::ReplaceString(dir, rootdir.c_str(), "");
  63. // Get the list of create dsp files names from the cmVCProjWriter, more
  64. // than one dsp could have been created per input CMakeLists.txt file
  65. // for each target
  66. std::vector<std::string> dspnames =
  67. static_cast<cmLocalVisualStudio7Generator *>(generators[i])
  68. ->GetCreatedProjectNames();
  69. cmTargets &tgts = generators[i]->GetMakefile()->GetTargets();
  70. cmTargets::iterator l = tgts.begin();
  71. for(std::vector<std::string>::iterator si = dspnames.begin();
  72. l != tgts.end() && si != dspnames.end(); ++l)
  73. {
  74. // special handling for the current makefile
  75. if(mf == generators[0]->GetMakefile())
  76. {
  77. dir = "."; // no subdirectory for project generated
  78. // if this is the special ALL_BUILD utility, then
  79. // make it depend on every other non UTILITY project.
  80. // This is done by adding the names to the GetUtilities
  81. // vector on the makefile
  82. if(l->first == "ALL_BUILD" && !doneAllBuild)
  83. {
  84. unsigned int j;
  85. for(j = 0; j < generators.size(); ++j)
  86. {
  87. cmTargets &atgts = generators[j]->GetMakefile()->GetTargets();
  88. for(cmTargets::iterator al = atgts.begin();
  89. al != atgts.end(); ++al)
  90. {
  91. if (al->second.IsInAll())
  92. {
  93. if (al->second.GetType() == cmTarget::UTILITY ||
  94. al->second.GetType() == cmTarget::GLOBAL_TARGET)
  95. {
  96. l->second.AddUtility(al->first.c_str());
  97. }
  98. else
  99. {
  100. l->second.AddLinkLibrary(al->first,cmTarget::GENERAL);
  101. }
  102. }
  103. }
  104. }
  105. }
  106. }
  107. // Write the project into the SLN file
  108. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  109. {
  110. cmCustomCommand cc = l->second.GetPostBuildCommands()[0];
  111. const cmCustomCommandLines& cmds = cc.GetCommandLines();
  112. std::string project = cmds[0][0];
  113. std::string location = cmds[0][1];
  114. this->WriteExternalProject(fout, project.c_str(), location.c_str(), cc.GetDepends());
  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 == CMAKE_CHECK_BUILD_SYSTEM_TARGET)
  134. {
  135. if(doneCheckBuild)
  136. {
  137. skip = true;
  138. }
  139. else
  140. {
  141. doneCheckBuild = true;
  142. }
  143. }
  144. if(l->first == "INSTALL")
  145. {
  146. if(doneInstall)
  147. {
  148. skip = true;
  149. }
  150. else
  151. {
  152. doneInstall = true;
  153. }
  154. }
  155. if(l->first == "RUN_TESTS")
  156. {
  157. if(doneRunTests)
  158. {
  159. skip = true;
  160. }
  161. else
  162. {
  163. doneRunTests = true;
  164. }
  165. }
  166. if(l->first == "EDIT_CACHE")
  167. {
  168. if(doneEditCache)
  169. {
  170. skip = true;
  171. }
  172. else
  173. {
  174. doneEditCache = true;
  175. }
  176. }
  177. if(l->first == "REBUILD_CACHE")
  178. {
  179. if(doneRebuildCache)
  180. {
  181. skip = true;
  182. }
  183. else
  184. {
  185. doneRebuildCache = true;
  186. }
  187. }
  188. if(l->first == "PACKAGE")
  189. {
  190. if(donePackage)
  191. {
  192. skip = true;
  193. }
  194. else
  195. {
  196. donePackage = true;
  197. }
  198. }
  199. if(!skip)
  200. {
  201. this->WriteProject(fout, si->c_str(), dir.c_str(),l->second);
  202. }
  203. ++si;
  204. }
  205. }
  206. }
  207. }
  208. fout << "Global\n";
  209. this->WriteSolutionConfigurations(fout);
  210. fout << "\tGlobalSection(" << m_ProjectConfigurationSectionName
  211. << ") = postSolution\n";
  212. // loop over again and compute the depends
  213. for(i = 0; i < generators.size(); ++i)
  214. {
  215. cmMakefile* mf = generators[i]->GetMakefile();
  216. cmLocalVisualStudio7Generator* pg =
  217. static_cast<cmLocalVisualStudio7Generator*>(generators[i]);
  218. // Get the list of create dsp files names from the cmVCProjWriter, more
  219. // than one dsp could have been created per input CMakeLists.txt file
  220. // for each target
  221. std::vector<std::string> dspnames =
  222. pg->GetCreatedProjectNames();
  223. cmTargets &tgts = pg->GetMakefile()->GetTargets();
  224. cmTargets::iterator l = tgts.begin();
  225. std::string dir = mf->GetStartDirectory();
  226. for(std::vector<std::string>::iterator si = dspnames.begin();
  227. l != tgts.end() && si != dspnames.end(); ++l)
  228. {
  229. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  230. {
  231. cmCustomCommand cc = l->second.GetPostBuildCommands()[0];
  232. const cmCustomCommandLines& cmds = cc.GetCommandLines();
  233. std::string project = cmds[0][0];
  234. this->WriteProjectConfigurations(fout, project.c_str(), l->second.IsInAll());
  235. }
  236. else if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  237. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  238. {
  239. this->WriteProjectConfigurations(fout, si->c_str(), l->second.IsInAll());
  240. ++si;
  241. }
  242. }
  243. }
  244. fout << "\tEndGlobalSection\n";
  245. // Write the footer for the SLN file
  246. this->WriteSLNFooter(fout);
  247. }
  248. void
  249. cmGlobalVisualStudio71Generator
  250. ::WriteSolutionConfigurations(std::ostream& fout)
  251. {
  252. fout << "\tGlobalSection(SolutionConfiguration) = preSolution\n";
  253. for(std::vector<std::string>::iterator i = m_Configurations.begin();
  254. i != m_Configurations.end(); ++i)
  255. {
  256. fout << "\t\t" << *i << " = " << *i << "\n";
  257. }
  258. fout << "\tEndGlobalSection\n";
  259. }
  260. // Write a dsp file into the SLN file,
  261. // Note, that dependencies from executables to
  262. // the libraries it uses are also done here
  263. void
  264. cmGlobalVisualStudio71Generator::WriteProject(std::ostream& fout,
  265. const char* dspname,
  266. const char* dir,
  267. cmTarget& t)
  268. {
  269. std::string d = cmSystemTools::ConvertToOutputPath(dir);
  270. fout << "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \""
  271. << dspname << "\", \""
  272. << d << "\\" << dspname << ".vcproj\", \"{"
  273. << this->GetGUID(dspname) << "}\"\n";
  274. fout << "\tProjectSection(ProjectDependencies) = postProject\n";
  275. this->WriteProjectDepends(fout, dspname, dir, t);
  276. fout << "\tEndProjectSection\n";
  277. fout <<"EndProject\n";
  278. }
  279. // Write a dsp file into the SLN file,
  280. // Note, that dependencies from executables to
  281. // the libraries it uses are also done here
  282. void
  283. cmGlobalVisualStudio71Generator
  284. ::WriteProjectDepends(std::ostream& fout,
  285. const char* dspname,
  286. const char*, cmTarget& target)
  287. {
  288. // insert Begin Project Dependency Project_Dep_Name project stuff here
  289. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  290. {
  291. cmTarget::LinkLibraries::const_iterator j, jend;
  292. j = target.GetLinkLibraries().begin();
  293. jend = target.GetLinkLibraries().end();
  294. for(;j!= jend; ++j)
  295. {
  296. if(j->first != dspname)
  297. {
  298. // is the library part of this SLN ? If so add dependency
  299. if(this->FindTarget(m_CurrentProject.c_str(), j->first.c_str()))
  300. {
  301. fout << "\t\t{" << this->GetGUID(j->first.c_str()) << "} = {"
  302. << this->GetGUID(j->first.c_str()) << "}\n";
  303. }
  304. }
  305. }
  306. }
  307. std::set<cmStdString>::const_iterator i, end;
  308. // write utility dependencies.
  309. i = target.GetUtilities().begin();
  310. end = target.GetUtilities().end();
  311. for(;i!= end; ++i)
  312. {
  313. if(*i != dspname)
  314. {
  315. std::string name = i->c_str();
  316. if(strncmp(name.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  317. {
  318. // kind of weird removing the first 27 letters.
  319. // my recommendatsions:
  320. // use cmCustomCommand::GetCommand() to get the project name
  321. // or get rid of the target name starting with "INCLUDE_EXTERNAL_MSPROJECT_" and use another
  322. // indicator/flag somewhere. These external project names shouldn't conflict with cmake
  323. // target names anyways.
  324. name.erase(name.begin(), name.begin() + 27);
  325. }
  326. std::string guid = this->GetGUID(name.c_str());
  327. if(guid.size() == 0)
  328. {
  329. std::string m = "Target: ";
  330. m += target.GetName();
  331. m += " depends on unknown target: ";
  332. m += name;
  333. cmSystemTools::Error(m.c_str());
  334. }
  335. fout << "\t\t{" << guid << "} = {" << guid << "}\n";
  336. }
  337. }
  338. }
  339. // Write a dsp file into the SLN file,
  340. // Note, that dependencies from executables to
  341. // the libraries it uses are also done here
  342. void cmGlobalVisualStudio71Generator::WriteExternalProject(std::ostream& fout,
  343. const char* name,
  344. const char* location,
  345. const std::vector<std::string>& depends)
  346. {
  347. std::string d = cmSystemTools::ConvertToOutputPath(location);
  348. fout << "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \""
  349. << name << "\", \""
  350. << d << "\", \"{"
  351. << this->GetGUID(name)
  352. << "}\"\n";
  353. // write out the dependencies here
  354. // VS 7.1 includes dependencies with the project instead of in the global section
  355. if(!depends.empty())
  356. {
  357. fout << "\tProjectSection(ProjectDependencies) = postProject\n";
  358. std::vector<std::string>::const_iterator it;
  359. for(it = depends.begin(); it != depends.end(); ++it)
  360. {
  361. if(it->size() > 0)
  362. {
  363. fout << "\t\t{"
  364. << this->GetGUID(it->c_str())
  365. << "} = {"
  366. << this->GetGUID(it->c_str())
  367. << "}\n";
  368. }
  369. }
  370. fout << "\tEndProjectSection\n";
  371. }
  372. fout << "EndProject\n";
  373. }
  374. // Write a dsp file into the SLN file,
  375. // Note, that dependencies from executables to
  376. // the libraries it uses are also done here
  377. void
  378. cmGlobalVisualStudio71Generator::WriteProjectConfigurations(std::ostream& fout,
  379. const char* name,
  380. bool in_all_build)
  381. {
  382. std::string guid = this->GetGUID(name);
  383. for(std::vector<std::string>::iterator i = m_Configurations.begin();
  384. i != m_Configurations.end(); ++i)
  385. {
  386. fout << "\t\t{" << guid << "}." << *i << ".ActiveCfg = " << *i << "|Win32\n";
  387. if (in_all_build)
  388. {
  389. fout << "\t\t{" << guid << "}." << *i << ".Build.0 = " << *i << "|Win32\n";
  390. }
  391. }
  392. }
  393. // Standard end of dsw file
  394. void cmGlobalVisualStudio71Generator::WriteSLNFooter(std::ostream& fout)
  395. {
  396. fout << "\tGlobalSection(ExtensibilityGlobals) = postSolution\n"
  397. << "\tEndGlobalSection\n"
  398. << "\tGlobalSection(ExtensibilityAddIns) = postSolution\n"
  399. << "\tEndGlobalSection\n"
  400. << "EndGlobal\n";
  401. }
  402. // ouput standard header for dsw file
  403. void cmGlobalVisualStudio71Generator::WriteSLNHeader(std::ostream& fout)
  404. {
  405. fout << "Microsoft Visual Studio Solution File, Format Version 8.00\n";
  406. }
  407. //----------------------------------------------------------------------------
  408. void cmGlobalVisualStudio71Generator::GetDocumentation(cmDocumentationEntry& entry) const
  409. {
  410. entry.name = this->GetName();
  411. entry.brief = "Generates Visual Studio .NET 2003 project files.";
  412. entry.full = "";
  413. }