cmGlobalVisualStudio8Generator.cxx 12 KB

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