cmGlobalVisualStudio8Generator.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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->VersionId = "MSVC80";
  20. this->FindMakeProgramFile = "CMakeVS8FindMake.cmake";
  21. this->ProjectConfigurationSectionName = "ProjectConfigurationPlatforms";
  22. }
  23. //----------------------------------------------------------------------------
  24. ///! Create a local generator appropriate to this Global Generator
  25. cmLocalGenerator *cmGlobalVisualStudio8Generator::CreateLocalGenerator()
  26. {
  27. cmLocalVisualStudio7Generator *lg =
  28. new cmLocalVisualStudio7Generator(cmLocalVisualStudioGenerator::VS8);
  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::Configure()
  51. {
  52. this->cmGlobalVisualStudio7Generator::Configure();
  53. this->CreateGUID(CMAKE_CHECK_BUILD_SYSTEM_TARGET);
  54. }
  55. //----------------------------------------------------------------------------
  56. std::string cmGlobalVisualStudio8Generator::GetUserMacrosDirectory()
  57. {
  58. // Some VS8 sp0 versions cannot run macros.
  59. // See http://support.microsoft.com/kb/928209
  60. const char* vc8sp1Registry =
  61. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0\\"
  62. "InstalledProducts\\KB926601;";
  63. const char* vc8exSP1Registry =
  64. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0\\"
  65. "InstalledProducts\\KB926748;";
  66. std::string vc8sp1;
  67. if (!cmSystemTools::ReadRegistryValue(vc8sp1Registry, vc8sp1) &&
  68. !cmSystemTools::ReadRegistryValue(vc8exSP1Registry, vc8sp1))
  69. {
  70. return "";
  71. }
  72. std::string base;
  73. std::string path;
  74. // base begins with the VisualStudioProjectsLocation reg value...
  75. if (cmSystemTools::ReadRegistryValue(
  76. "HKEY_CURRENT_USER\\Software\\Microsoft\\VisualStudio\\8.0;"
  77. "VisualStudioProjectsLocation",
  78. base))
  79. {
  80. cmSystemTools::ConvertToUnixSlashes(base);
  81. // 8.0 macros folder:
  82. path = base + "/VSMacros80";
  83. }
  84. // path is (correctly) still empty if we did not read the base value from
  85. // the Registry value
  86. return path;
  87. }
  88. //----------------------------------------------------------------------------
  89. std::string cmGlobalVisualStudio8Generator::GetUserMacrosRegKeyBase()
  90. {
  91. return "Software\\Microsoft\\VisualStudio\\8.0\\vsmacros";
  92. }
  93. //----------------------------------------------------------------------------
  94. void cmGlobalVisualStudio8Generator::AddCheckTarget()
  95. {
  96. // Add a special target on which all other targets depend that
  97. // checks the build system and optionally re-runs CMake.
  98. const char* no_working_directory = 0;
  99. std::vector<std::string> no_depends;
  100. std::vector<cmLocalGenerator*> const& generators = this->LocalGenerators;
  101. cmLocalVisualStudio7Generator* lg =
  102. static_cast<cmLocalVisualStudio7Generator*>(generators[0]);
  103. cmMakefile* mf = lg->GetMakefile();
  104. // Skip the target if no regeneration is to be done.
  105. if(mf->IsOn("CMAKE_SUPPRESS_REGENERATION"))
  106. {
  107. return;
  108. }
  109. std::string cmake_command = mf->GetRequiredDefinition("CMAKE_COMMAND");
  110. cmCustomCommandLines noCommandLines;
  111. cmTarget* tgt =
  112. mf->AddUtilityCommand(CMAKE_CHECK_BUILD_SYSTEM_TARGET, false,
  113. no_working_directory, no_depends,
  114. noCommandLines);
  115. // Organize in the "predefined targets" folder:
  116. //
  117. if (this->UseFolderProperty())
  118. {
  119. tgt->SetProperty("FOLDER", this->GetPredefinedTargetsFolder());
  120. }
  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. if(cmSourceFile* file =
  189. mf->AddCustomCommandToOutput(
  190. stamps, listFiles,
  191. no_main_dependency, commandLines, "Checking Build System",
  192. no_working_directory, true))
  193. {
  194. tgt->AddSourceFile(file);
  195. }
  196. else
  197. {
  198. cmSystemTools::Error("Error adding rule for ", stamps[0].c_str());
  199. }
  200. }
  201. }
  202. //----------------------------------------------------------------------------
  203. void cmGlobalVisualStudio8Generator::Generate()
  204. {
  205. this->AddCheckTarget();
  206. // All targets depend on the build-system check target.
  207. for(std::map<cmStdString,cmTarget *>::const_iterator
  208. ti = this->TotalTargets.begin();
  209. ti != this->TotalTargets.end(); ++ti)
  210. {
  211. if(ti->first != CMAKE_CHECK_BUILD_SYSTEM_TARGET)
  212. {
  213. ti->second->AddUtility(CMAKE_CHECK_BUILD_SYSTEM_TARGET);
  214. }
  215. }
  216. // Now perform the main generation.
  217. this->cmGlobalVisualStudio7Generator::Generate();
  218. }
  219. //----------------------------------------------------------------------------
  220. void
  221. cmGlobalVisualStudio8Generator
  222. ::WriteSolutionConfigurations(std::ostream& fout)
  223. {
  224. fout << "\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
  225. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  226. i != this->Configurations.end(); ++i)
  227. {
  228. fout << "\t\t" << *i << "|" << this->GetPlatformName()
  229. << " = " << *i << "|" << this->GetPlatformName() << "\n";
  230. }
  231. fout << "\tEndGlobalSection\n";
  232. }
  233. //----------------------------------------------------------------------------
  234. void
  235. cmGlobalVisualStudio8Generator
  236. ::WriteProjectConfigurations(std::ostream& fout, const char* name,
  237. bool partOfDefaultBuild,
  238. const char* platformMapping)
  239. {
  240. std::string guid = this->GetGUID(name);
  241. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  242. i != this->Configurations.end(); ++i)
  243. {
  244. fout << "\t\t{" << guid << "}." << *i
  245. << "|" << this->GetPlatformName() << ".ActiveCfg = " << *i << "|"
  246. << (platformMapping ? platformMapping : this->GetPlatformName())
  247. << "\n";
  248. if(partOfDefaultBuild)
  249. {
  250. fout << "\t\t{" << guid << "}." << *i
  251. << "|" << this->GetPlatformName() << ".Build.0 = " << *i << "|"
  252. << (platformMapping ? platformMapping : this->GetPlatformName())
  253. << "\n";
  254. }
  255. }
  256. }
  257. //----------------------------------------------------------------------------
  258. bool cmGlobalVisualStudio8Generator::ComputeTargetDepends()
  259. {
  260. // Skip over the cmGlobalVisualStudioGenerator implementation!
  261. // We do not need the support that VS <= 7.1 needs.
  262. return this->cmGlobalGenerator::ComputeTargetDepends();
  263. }
  264. //----------------------------------------------------------------------------
  265. void cmGlobalVisualStudio8Generator::WriteProjectDepends(
  266. std::ostream& fout, const char*, const char*, cmTarget& t)
  267. {
  268. TargetDependSet const& unordered = this->GetTargetDirectDepends(t);
  269. OrderedTargetDependSet depends(unordered);
  270. for(OrderedTargetDependSet::const_iterator i = depends.begin();
  271. i != depends.end(); ++i)
  272. {
  273. std::string guid = this->GetGUID((*i)->GetName());
  274. fout << "\t\t{" << guid << "} = {" << guid << "}\n";
  275. }
  276. }
  277. //----------------------------------------------------------------------------
  278. bool cmGlobalVisualStudio8Generator::NeedLinkLibraryDependencies(
  279. cmTarget& target)
  280. {
  281. // Look for utility dependencies that magically link.
  282. for(std::set<cmStdString>::const_iterator ui =
  283. target.GetUtilities().begin();
  284. ui != target.GetUtilities().end(); ++ui)
  285. {
  286. if(cmTarget* depTarget = this->FindTarget(0, ui->c_str()))
  287. {
  288. if(depTarget->GetProperty("EXTERNAL_MSPROJECT"))
  289. {
  290. // This utility dependency names an external .vcproj target.
  291. // We use LinkLibraryDependencies="true" to link to it without
  292. // predicting the .lib file location or name.
  293. return true;
  294. }
  295. }
  296. }
  297. return false;
  298. }
  299. //----------------------------------------------------------------------------
  300. static cmVS7FlagTable cmVS8ExtraFlagTable[] =
  301. {
  302. {"CallingConvention", "Gd", "cdecl", "0", 0 },
  303. {"CallingConvention", "Gr", "fastcall", "1", 0 },
  304. {"CallingConvention", "Gz", "stdcall", "2", 0 },
  305. {"Detect64BitPortabilityProblems", "Wp64",
  306. "Detect 64Bit Portability Problems", "true", 0 },
  307. {"ErrorReporting", "errorReport:prompt", "Report immediately", "1", 0 },
  308. {"ErrorReporting", "errorReport:queue", "Queue for next login", "2", 0 },
  309. // Precompiled header and related options. Note that the
  310. // UsePrecompiledHeader entries are marked as "Continue" so that the
  311. // corresponding PrecompiledHeaderThrough entry can be found.
  312. {"UsePrecompiledHeader", "Yu", "Use Precompiled Header", "2",
  313. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue},
  314. {"PrecompiledHeaderThrough", "Yu", "Precompiled Header Name", "",
  315. cmVS7FlagTable::UserValueRequired},
  316. // There is no YX option in the VS8 IDE.
  317. // Exception handling mode. If no entries match, it will be FALSE.
  318. {"ExceptionHandling", "GX", "enable c++ exceptions", "1", 0},
  319. {"ExceptionHandling", "EHsc", "enable c++ exceptions", "1", 0},
  320. {"ExceptionHandling", "EHa", "enable SEH exceptions", "2", 0},
  321. {"EnablePREfast", "analyze", "", "true", 0},
  322. {"EnablePREfast", "analyze-", "", "false", 0},
  323. // Language options
  324. {"TreatWChar_tAsBuiltInType", "Zc:wchar_t",
  325. "wchar_t is a built-in type", "true", 0},
  326. {"TreatWChar_tAsBuiltInType", "Zc:wchar_t-",
  327. "wchar_t is not a built-in type", "false", 0},
  328. {0,0,0,0,0}
  329. };
  330. cmIDEFlagTable const* cmGlobalVisualStudio8Generator::GetExtraFlagTableVS8()
  331. {
  332. return cmVS8ExtraFlagTable;
  333. }