cmGlobalVisualStudio71Generator.cxx 14 KB

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