cmGlobalVisualStudio8Generator.cxx 15 KB

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