cmGlobalVisualStudio8Generator.cxx 13 KB

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