cmGlobalVisualStudio8Generator.cxx 17 KB

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