cmGlobalVisualStudio71Generator.cxx 13 KB

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