cmGlobalVisualStudio8Generator.cxx 17 KB

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