cmGlobalVisualStudio8Generator.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 "cmGlobalVisualStudio8Generator.h"
  15. #include "cmLocalVisualStudio7Generator.h"
  16. #include "cmMakefile.h"
  17. #include "cmake.h"
  18. #include "cmGeneratedFileStream.h"
  19. //----------------------------------------------------------------------------
  20. cmGlobalVisualStudio8Generator::cmGlobalVisualStudio8Generator()
  21. {
  22. this->FindMakeProgramFile = "CMakeVS8FindMake.cmake";
  23. this->ProjectConfigurationSectionName = "ProjectConfigurationPlatforms";
  24. this->PlatformName = "Win32";
  25. }
  26. //----------------------------------------------------------------------------
  27. ///! Create a local generator appropriate to this Global Generator
  28. cmLocalGenerator *cmGlobalVisualStudio8Generator::CreateLocalGenerator()
  29. {
  30. cmLocalVisualStudio7Generator *lg = new cmLocalVisualStudio7Generator;
  31. lg->SetVersion8();
  32. lg->SetExtraFlagTable(this->GetExtraFlagTableVS8());
  33. lg->SetGlobalGenerator(this);
  34. return lg;
  35. }
  36. //----------------------------------------------------------------------------
  37. // ouput standard header for dsw file
  38. void cmGlobalVisualStudio8Generator::WriteSLNHeader(std::ostream& fout)
  39. {
  40. fout << "Microsoft Visual Studio Solution File, Format Version 9.00\n";
  41. fout << "# Visual Studio 2005\n";
  42. }
  43. //----------------------------------------------------------------------------
  44. void cmGlobalVisualStudio8Generator
  45. ::GetDocumentation(cmDocumentationEntry& entry) const
  46. {
  47. entry.Name = this->GetName();
  48. entry.Brief = "Generates Visual Studio .NET 2005 project files.";
  49. entry.Full = "";
  50. }
  51. //----------------------------------------------------------------------------
  52. void cmGlobalVisualStudio8Generator::AddPlatformDefinitions(cmMakefile* mf)
  53. {
  54. mf->AddDefinition("MSVC80", "1");
  55. }
  56. //----------------------------------------------------------------------------
  57. void cmGlobalVisualStudio8Generator::Configure()
  58. {
  59. this->cmGlobalVisualStudio7Generator::Configure();
  60. this->CreateGUID(CMAKE_CHECK_BUILD_SYSTEM_TARGET);
  61. }
  62. //----------------------------------------------------------------------------
  63. std::string cmGlobalVisualStudio8Generator::GetUserMacrosDirectory()
  64. {
  65. // Some VS8 sp0 versions cannot run macros.
  66. // See http://support.microsoft.com/kb/928209
  67. const char* vc8sp1Registry =
  68. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0\\"
  69. "InstalledProducts\\KB926601;";
  70. const char* vc8exSP1Registry =
  71. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0\\"
  72. "InstalledProducts\\KB926748;";
  73. std::string vc8sp1;
  74. if (!cmSystemTools::ReadRegistryValue(vc8sp1Registry, vc8sp1) &&
  75. !cmSystemTools::ReadRegistryValue(vc8exSP1Registry, vc8sp1))
  76. {
  77. return "";
  78. }
  79. std::string base;
  80. std::string path;
  81. // base begins with the VisualStudioProjectsLocation reg value...
  82. if (cmSystemTools::ReadRegistryValue(
  83. "HKEY_CURRENT_USER\\Software\\Microsoft\\VisualStudio\\8.0;"
  84. "VisualStudioProjectsLocation",
  85. base))
  86. {
  87. cmSystemTools::ConvertToUnixSlashes(base);
  88. // 8.0 macros folder:
  89. path = base + "/VSMacros80";
  90. }
  91. // path is (correctly) still empty if we did not read the base value from
  92. // the Registry value
  93. return path;
  94. }
  95. //----------------------------------------------------------------------------
  96. std::string cmGlobalVisualStudio8Generator::GetUserMacrosRegKeyBase()
  97. {
  98. return "Software\\Microsoft\\VisualStudio\\8.0\\vsmacros";
  99. }
  100. //----------------------------------------------------------------------------
  101. void cmGlobalVisualStudio8Generator::Generate()
  102. {
  103. // Add a special target on which all other targets depend that
  104. // checks the build system and optionally re-runs CMake.
  105. const char* no_working_directory = 0;
  106. std::vector<std::string> no_depends;
  107. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  108. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  109. {
  110. std::vector<cmLocalGenerator*>& generators = it->second;
  111. if(!generators.empty())
  112. {
  113. // Add the build-system check target to the first local
  114. // generator of this project.
  115. cmLocalVisualStudio7Generator* lg =
  116. static_cast<cmLocalVisualStudio7Generator*>(generators[0]);
  117. cmMakefile* mf = lg->GetMakefile();
  118. // Skip the target if no regeneration is to be done.
  119. if(mf->IsOn("CMAKE_SUPPRESS_REGENERATION"))
  120. {
  121. continue;
  122. }
  123. std::string cmake_command = mf->GetRequiredDefinition("CMAKE_COMMAND");
  124. cmCustomCommandLines noCommandLines;
  125. mf->AddUtilityCommand(CMAKE_CHECK_BUILD_SYSTEM_TARGET, false,
  126. no_working_directory, no_depends,
  127. noCommandLines);
  128. cmTarget* tgt = mf->FindTarget(CMAKE_CHECK_BUILD_SYSTEM_TARGET);
  129. if(!tgt)
  130. {
  131. cmSystemTools::Error("Error adding target "
  132. CMAKE_CHECK_BUILD_SYSTEM_TARGET);
  133. continue;
  134. }
  135. // Create a list of all stamp files for this project.
  136. std::vector<std::string> stamps;
  137. std::string stampList = cmake::GetCMakeFilesDirectoryPostSlash();
  138. stampList += "generate.stamp.list";
  139. {
  140. std::string stampListFile =
  141. generators[0]->GetMakefile()->GetCurrentOutputDirectory();
  142. stampListFile += "/";
  143. stampListFile += stampList;
  144. std::string stampFile;
  145. cmGeneratedFileStream fout(stampListFile.c_str());
  146. for(std::vector<cmLocalGenerator*>::const_iterator
  147. gi = generators.begin(); gi != generators.end(); ++gi)
  148. {
  149. stampFile = (*gi)->GetMakefile()->GetCurrentOutputDirectory();
  150. stampFile += "/";
  151. stampFile += cmake::GetCMakeFilesDirectoryPostSlash();
  152. stampFile += "generate.stamp";
  153. stampFile = generators[0]->Convert(stampFile.c_str(),
  154. cmLocalGenerator::START_OUTPUT);
  155. fout << stampFile << "\n";
  156. stamps.push_back(stampFile);
  157. }
  158. }
  159. // Add a custom rule to re-run CMake if any input files changed.
  160. {
  161. // Collect the input files used to generate all targets in this
  162. // project.
  163. std::vector<std::string> listFiles;
  164. for(unsigned int j = 0; j < generators.size(); ++j)
  165. {
  166. cmMakefile* lmf = generators[j]->GetMakefile();
  167. listFiles.insert(listFiles.end(), lmf->GetListFiles().begin(),
  168. lmf->GetListFiles().end());
  169. }
  170. // Sort the list of input files and remove duplicates.
  171. std::sort(listFiles.begin(), listFiles.end(),
  172. std::less<std::string>());
  173. std::vector<std::string>::iterator new_end =
  174. std::unique(listFiles.begin(), listFiles.end());
  175. listFiles.erase(new_end, listFiles.end());
  176. // Create a rule to re-run CMake.
  177. std::string stampName = cmake::GetCMakeFilesDirectoryPostSlash();
  178. stampName += "generate.stamp";
  179. const char* dsprule = mf->GetRequiredDefinition("CMAKE_COMMAND");
  180. cmCustomCommandLine commandLine;
  181. commandLine.push_back(dsprule);
  182. std::string argH = "-H";
  183. argH += lg->Convert(mf->GetHomeDirectory(),
  184. cmLocalGenerator::START_OUTPUT,
  185. cmLocalGenerator::UNCHANGED, true);
  186. commandLine.push_back(argH);
  187. std::string argB = "-B";
  188. argB += lg->Convert(mf->GetHomeOutputDirectory(),
  189. cmLocalGenerator::START_OUTPUT,
  190. cmLocalGenerator::UNCHANGED, true);
  191. commandLine.push_back(argB);
  192. commandLine.push_back("--check-stamp-list");
  193. commandLine.push_back(stampList.c_str());
  194. commandLine.push_back("--vs-solution-file");
  195. commandLine.push_back("\"$(SolutionPath)\"");
  196. cmCustomCommandLines commandLines;
  197. commandLines.push_back(commandLine);
  198. // Add the rule. Note that we cannot use the CMakeLists.txt
  199. // file as the main dependency because it would get
  200. // overwritten by the CreateVCProjBuildRule.
  201. // (this could be avoided with per-target source files)
  202. const char* no_main_dependency = 0;
  203. const char* no_working_directory = 0;
  204. mf->AddCustomCommandToOutput(
  205. stamps, listFiles,
  206. no_main_dependency, commandLines, "Checking Build System",
  207. no_working_directory, true);
  208. std::string ruleName = stamps[0];
  209. ruleName += ".rule";
  210. if(cmSourceFile* file = mf->GetSource(ruleName.c_str()))
  211. {
  212. tgt->AddSourceFile(file);
  213. }
  214. else
  215. {
  216. cmSystemTools::Error("Error adding rule for ", stamps[0].c_str());
  217. }
  218. }
  219. }
  220. }
  221. // Now perform the main generation.
  222. this->cmGlobalVisualStudio7Generator::Generate();
  223. }
  224. //----------------------------------------------------------------------------
  225. void cmGlobalVisualStudio8Generator::WriteSLNFile(
  226. std::ostream& fout, cmLocalGenerator* root,
  227. std::vector<cmLocalGenerator*>& generators)
  228. {
  229. // Make all targets depend on their respective project's build
  230. // system check target.
  231. unsigned int i;
  232. for(i = 0; i < generators.size(); ++i)
  233. {
  234. if(this->IsExcluded(root, generators[i]))
  235. {
  236. continue;
  237. }
  238. cmMakefile* mf = generators[i]->GetMakefile();
  239. cmTargets& tgts = mf->GetTargets();
  240. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); ++l)
  241. {
  242. if(l->first == CMAKE_CHECK_BUILD_SYSTEM_TARGET)
  243. {
  244. for(unsigned int j = 0; j < generators.size(); ++j)
  245. {
  246. // Every target in all generators should depend on this target.
  247. cmMakefile* lmf = generators[j]->GetMakefile();
  248. cmTargets &atgts = lmf->GetTargets();
  249. for(cmTargets::iterator al = atgts.begin(); al != atgts.end(); ++al)
  250. {
  251. al->second.AddUtility(l->first.c_str());
  252. }
  253. }
  254. }
  255. }
  256. }
  257. // Now write the solution file.
  258. this->cmGlobalVisualStudio71Generator::WriteSLNFile(fout, root, generators);
  259. }
  260. //----------------------------------------------------------------------------
  261. void
  262. cmGlobalVisualStudio8Generator
  263. ::WriteSolutionConfigurations(std::ostream& fout)
  264. {
  265. fout << "\tGlobalSection(SolutionConfigurationPlatforms) = 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 << "|" << this->PlatformName << " = " << *i << "|"
  270. << this->PlatformName << "\n";
  271. }
  272. fout << "\tEndGlobalSection\n";
  273. }
  274. //----------------------------------------------------------------------------
  275. void
  276. cmGlobalVisualStudio8Generator
  277. ::WriteProjectConfigurations(std::ostream& fout, const char* name,
  278. bool partOfDefaultBuild)
  279. {
  280. std::string guid = this->GetGUID(name);
  281. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  282. i != this->Configurations.end(); ++i)
  283. {
  284. fout << "\t\t{" << guid << "}." << *i
  285. << "|" << this->PlatformName << ".ActiveCfg = "
  286. << *i << "|" << this->PlatformName << "\n";
  287. if(partOfDefaultBuild)
  288. {
  289. fout << "\t\t{" << guid << "}." << *i
  290. << "|" << this->PlatformName << ".Build.0 = "
  291. << *i << "|" << this->PlatformName << "\n";
  292. }
  293. }
  294. }
  295. //----------------------------------------------------------------------------
  296. static cmVS7FlagTable cmVS8ExtraFlagTable[] =
  297. {
  298. {"CallingConvention", "Gd", "cdecl", "0", 0 },
  299. {"CallingConvention", "Gr", "fastcall", "1", 0 },
  300. {"CallingConvention", "Gz", "stdcall", "2", 0 },
  301. {"Detect64BitPortabilityProblems", "Wp64",
  302. "Detect 64Bit Portability Problems", "true", 0 },
  303. {"ErrorReporting", "errorReport:prompt", "Report immediately", "1", 0 },
  304. {"ErrorReporting", "errorReport:queue", "Queue for next login", "2", 0 },
  305. // Precompiled header and related options. Note that the
  306. // UsePrecompiledHeader entries are marked as "Continue" so that the
  307. // corresponding PrecompiledHeaderThrough entry can be found.
  308. {"UsePrecompiledHeader", "Yu", "Use Precompiled Header", "2",
  309. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue},
  310. {"PrecompiledHeaderThrough", "Yu", "Precompiled Header Name", "",
  311. cmVS7FlagTable::UserValueRequired},
  312. // There is no YX option in the VS8 IDE.
  313. // Exception handling mode. If no entries match, it will be FALSE.
  314. {"ExceptionHandling", "GX", "enable c++ exceptions", "1", 0},
  315. {"ExceptionHandling", "EHsc", "enable c++ exceptions", "1", 0},
  316. {"ExceptionHandling", "EHa", "enable SEH exceptions", "2", 0},
  317. {0,0,0,0,0}
  318. };
  319. cmIDEFlagTable const* cmGlobalVisualStudio8Generator::GetExtraFlagTableVS8()
  320. {
  321. return cmVS8ExtraFlagTable;
  322. }