cmGlobalVisualStudio8Generator.cxx 17 KB

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