cmGlobalVisualStudio8Generator.cxx 12 KB

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