cmGlobalVisualStudio8Generator.cxx 12 KB

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