cmGlobalVisualStudio8Generator.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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->ArchitectureId = "X86";
  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->SetPlatformName(this->GetPlatformName());
  30. lg->SetExtraFlagTable(this->GetExtraFlagTableVS8());
  31. lg->SetGlobalGenerator(this);
  32. return lg;
  33. }
  34. //----------------------------------------------------------------------------
  35. // ouput standard header for dsw file
  36. void cmGlobalVisualStudio8Generator::WriteSLNHeader(std::ostream& fout)
  37. {
  38. fout << "Microsoft Visual Studio Solution File, Format Version 9.00\n";
  39. fout << "# Visual Studio 2005\n";
  40. }
  41. //----------------------------------------------------------------------------
  42. void cmGlobalVisualStudio8Generator
  43. ::GetDocumentation(cmDocumentationEntry& entry) const
  44. {
  45. entry.Name = this->GetName();
  46. entry.Brief = "Generates Visual Studio .NET 2005 project files.";
  47. entry.Full = "";
  48. }
  49. //----------------------------------------------------------------------------
  50. void cmGlobalVisualStudio8Generator::AddPlatformDefinitions(cmMakefile* mf)
  51. {
  52. mf->AddDefinition("MSVC_C_ARCHITECTURE_ID", this->ArchitectureId);
  53. mf->AddDefinition("MSVC_CXX_ARCHITECTURE_ID", this->ArchitectureId);
  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::AddCheckTarget()
  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::vector<cmLocalGenerator*> const& generators = this->LocalGenerators;
  108. cmLocalVisualStudio7Generator* lg =
  109. static_cast<cmLocalVisualStudio7Generator*>(generators[0]);
  110. cmMakefile* mf = lg->GetMakefile();
  111. // Skip the target if no regeneration is to be done.
  112. if(mf->IsOn("CMAKE_SUPPRESS_REGENERATION"))
  113. {
  114. return;
  115. }
  116. std::string cmake_command = mf->GetRequiredDefinition("CMAKE_COMMAND");
  117. cmCustomCommandLines noCommandLines;
  118. cmTarget* tgt =
  119. mf->AddUtilityCommand(CMAKE_CHECK_BUILD_SYSTEM_TARGET, false,
  120. no_working_directory, no_depends,
  121. noCommandLines);
  122. // Organize in the "predefined targets" folder:
  123. //
  124. if (this->UseFolderProperty())
  125. {
  126. tgt->SetProperty("FOLDER", this->GetPredefinedTargetsFolder());
  127. }
  128. // Create a list of all stamp files for this project.
  129. std::vector<std::string> stamps;
  130. std::string stampList = cmake::GetCMakeFilesDirectoryPostSlash();
  131. stampList += "generate.stamp.list";
  132. {
  133. std::string stampListFile =
  134. generators[0]->GetMakefile()->GetCurrentOutputDirectory();
  135. stampListFile += "/";
  136. stampListFile += stampList;
  137. std::string stampFile;
  138. cmGeneratedFileStream fout(stampListFile.c_str());
  139. for(std::vector<cmLocalGenerator*>::const_iterator
  140. gi = generators.begin(); gi != generators.end(); ++gi)
  141. {
  142. stampFile = (*gi)->GetMakefile()->GetCurrentOutputDirectory();
  143. stampFile += "/";
  144. stampFile += cmake::GetCMakeFilesDirectoryPostSlash();
  145. stampFile += "generate.stamp";
  146. fout << stampFile << "\n";
  147. stamps.push_back(stampFile);
  148. }
  149. }
  150. // Add a custom rule to re-run CMake if any input files changed.
  151. {
  152. // Collect the input files used to generate all targets in this
  153. // project.
  154. std::vector<std::string> listFiles;
  155. for(unsigned int j = 0; j < generators.size(); ++j)
  156. {
  157. cmMakefile* lmf = generators[j]->GetMakefile();
  158. listFiles.insert(listFiles.end(), lmf->GetListFiles().begin(),
  159. lmf->GetListFiles().end());
  160. }
  161. // Sort the list of input files and remove duplicates.
  162. std::sort(listFiles.begin(), listFiles.end(),
  163. std::less<std::string>());
  164. std::vector<std::string>::iterator new_end =
  165. std::unique(listFiles.begin(), listFiles.end());
  166. listFiles.erase(new_end, listFiles.end());
  167. // Create a rule to re-run CMake.
  168. std::string stampName = cmake::GetCMakeFilesDirectoryPostSlash();
  169. stampName += "generate.stamp";
  170. const char* dsprule = mf->GetRequiredDefinition("CMAKE_COMMAND");
  171. cmCustomCommandLine commandLine;
  172. commandLine.push_back(dsprule);
  173. std::string argH = "-H";
  174. argH += lg->Convert(mf->GetHomeDirectory(),
  175. cmLocalGenerator::START_OUTPUT,
  176. cmLocalGenerator::UNCHANGED, true);
  177. commandLine.push_back(argH);
  178. std::string argB = "-B";
  179. argB += lg->Convert(mf->GetHomeOutputDirectory(),
  180. cmLocalGenerator::START_OUTPUT,
  181. cmLocalGenerator::UNCHANGED, true);
  182. commandLine.push_back(argB);
  183. commandLine.push_back("--check-stamp-list");
  184. commandLine.push_back(stampList.c_str());
  185. commandLine.push_back("--vs-solution-file");
  186. commandLine.push_back("\"$(SolutionPath)\"");
  187. cmCustomCommandLines commandLines;
  188. commandLines.push_back(commandLine);
  189. // Add the rule. Note that we cannot use the CMakeLists.txt
  190. // file as the main dependency because it would get
  191. // overwritten by the CreateVCProjBuildRule.
  192. // (this could be avoided with per-target source files)
  193. const char* no_main_dependency = 0;
  194. const char* no_working_directory = 0;
  195. mf->AddCustomCommandToOutput(
  196. stamps, listFiles,
  197. no_main_dependency, commandLines, "Checking Build System",
  198. no_working_directory, true);
  199. std::string ruleName = stamps[0];
  200. ruleName += ".rule";
  201. if(cmSourceFile* file = mf->GetSource(ruleName.c_str()))
  202. {
  203. tgt->AddSourceFile(file);
  204. }
  205. else
  206. {
  207. cmSystemTools::Error("Error adding rule for ", stamps[0].c_str());
  208. }
  209. }
  210. }
  211. //----------------------------------------------------------------------------
  212. void cmGlobalVisualStudio8Generator::Generate()
  213. {
  214. this->AddCheckTarget();
  215. // All targets depend on the build-system check target.
  216. for(std::map<cmStdString,cmTarget *>::const_iterator
  217. ti = this->TotalTargets.begin();
  218. ti != this->TotalTargets.end(); ++ti)
  219. {
  220. if(ti->first != CMAKE_CHECK_BUILD_SYSTEM_TARGET)
  221. {
  222. ti->second->AddUtility(CMAKE_CHECK_BUILD_SYSTEM_TARGET);
  223. }
  224. }
  225. // Now perform the main generation.
  226. this->cmGlobalVisualStudio7Generator::Generate();
  227. }
  228. //----------------------------------------------------------------------------
  229. void
  230. cmGlobalVisualStudio8Generator
  231. ::WriteSolutionConfigurations(std::ostream& fout)
  232. {
  233. fout << "\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
  234. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  235. i != this->Configurations.end(); ++i)
  236. {
  237. fout << "\t\t" << *i << "|" << this->GetPlatformName()
  238. << " = " << *i << "|" << this->GetPlatformName() << "\n";
  239. }
  240. fout << "\tEndGlobalSection\n";
  241. }
  242. //----------------------------------------------------------------------------
  243. void
  244. cmGlobalVisualStudio8Generator
  245. ::WriteProjectConfigurations(std::ostream& fout, const char* name,
  246. bool partOfDefaultBuild)
  247. {
  248. std::string guid = this->GetGUID(name);
  249. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  250. i != this->Configurations.end(); ++i)
  251. {
  252. fout << "\t\t{" << guid << "}." << *i
  253. << "|" << this->GetPlatformName() << ".ActiveCfg = "
  254. << *i << "|" << this->GetPlatformName() << "\n";
  255. if(partOfDefaultBuild)
  256. {
  257. fout << "\t\t{" << guid << "}." << *i
  258. << "|" << this->GetPlatformName() << ".Build.0 = "
  259. << *i << "|" << this->GetPlatformName() << "\n";
  260. }
  261. }
  262. }
  263. //----------------------------------------------------------------------------
  264. bool cmGlobalVisualStudio8Generator::NeedLinkLibraryDependencies(
  265. cmTarget& target)
  266. {
  267. // Look for utility dependencies that magically link.
  268. for(std::set<cmStdString>::const_iterator ui =
  269. target.GetUtilities().begin();
  270. ui != target.GetUtilities().end(); ++ui)
  271. {
  272. if(cmTarget* depTarget = this->FindTarget(0, ui->c_str()))
  273. {
  274. if(depTarget->GetProperty("EXTERNAL_MSPROJECT"))
  275. {
  276. // This utility dependency names an external .vcproj target.
  277. // We use LinkLibraryDependencies="true" to link to it without
  278. // predicting the .lib file location or name.
  279. return true;
  280. }
  281. }
  282. }
  283. return false;
  284. }
  285. //----------------------------------------------------------------------------
  286. static cmVS7FlagTable cmVS8ExtraFlagTable[] =
  287. {
  288. {"CallingConvention", "Gd", "cdecl", "0", 0 },
  289. {"CallingConvention", "Gr", "fastcall", "1", 0 },
  290. {"CallingConvention", "Gz", "stdcall", "2", 0 },
  291. {"Detect64BitPortabilityProblems", "Wp64",
  292. "Detect 64Bit Portability Problems", "true", 0 },
  293. {"ErrorReporting", "errorReport:prompt", "Report immediately", "1", 0 },
  294. {"ErrorReporting", "errorReport:queue", "Queue for next login", "2", 0 },
  295. // Precompiled header and related options. Note that the
  296. // UsePrecompiledHeader entries are marked as "Continue" so that the
  297. // corresponding PrecompiledHeaderThrough entry can be found.
  298. {"UsePrecompiledHeader", "Yu", "Use Precompiled Header", "2",
  299. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue},
  300. {"PrecompiledHeaderThrough", "Yu", "Precompiled Header Name", "",
  301. cmVS7FlagTable::UserValueRequired},
  302. // There is no YX option in the VS8 IDE.
  303. // Exception handling mode. If no entries match, it will be FALSE.
  304. {"ExceptionHandling", "GX", "enable c++ exceptions", "1", 0},
  305. {"ExceptionHandling", "EHsc", "enable c++ exceptions", "1", 0},
  306. {"ExceptionHandling", "EHa", "enable SEH exceptions", "2", 0},
  307. {0,0,0,0,0}
  308. };
  309. cmIDEFlagTable const* cmGlobalVisualStudio8Generator::GetExtraFlagTableVS8()
  310. {
  311. return cmVS8ExtraFlagTable;
  312. }