cmGlobalVisualStudio8Generator.cxx 15 KB

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