cmGlobalVisualStudio71Generator.cxx 14 KB

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